Brian Silverman | f7f267a | 2017-02-04 16:16:08 -0800 | [diff] [blame^] | 1 | /*----------------------------------------------------------------------------*/ |
| 2 | /* Copyright (c) FIRST 2008-2017. All Rights Reserved. */ |
| 3 | /* Open Source Software - may be modified and shared by FRC teams. The code */ |
| 4 | /* must be accompanied by the FIRST BSD license file in the root directory of */ |
| 5 | /* the project. */ |
| 6 | /*----------------------------------------------------------------------------*/ |
| 7 | |
| 8 | #pragma once |
| 9 | |
| 10 | #include <stdint.h> |
| 11 | |
| 12 | #include <memory> |
| 13 | #include <string> |
| 14 | |
| 15 | #include "CounterBase.h" |
| 16 | #include "LiveWindow/LiveWindowSendable.h" |
| 17 | #include "PIDSource.h" |
| 18 | #include "SensorBase.h" |
| 19 | #include "simulation/SimEncoder.h" |
| 20 | |
| 21 | namespace frc { |
| 22 | |
| 23 | /** |
| 24 | * Class to read quad encoders. |
| 25 | * |
| 26 | * Quadrature encoders are devices that count shaft rotation and can sense |
| 27 | * direction. The output of the QuadEncoder class is an integer that can count |
| 28 | * either up or down, and can go negative for reverse direction counting. When |
| 29 | * creating QuadEncoders, a direction is supplied that changes the sense of the |
| 30 | * output to make code more readable if the encoder is mounted such that |
| 31 | * forward movement generates negative values. Quadrature encoders have two |
| 32 | * digital outputs, an A Channel and a B Channel that are out of phase with |
| 33 | * each other to allow the FPGA to do direction sensing. |
| 34 | * |
| 35 | * All encoders will immediately start counting - Reset() them if you need them |
| 36 | * to be zeroed before use. |
| 37 | */ |
| 38 | class Encoder : public SensorBase, |
| 39 | public CounterBase, |
| 40 | public PIDSource, |
| 41 | public LiveWindowSendable { |
| 42 | public: |
| 43 | Encoder(int aChannel, int bChannel, bool reverseDirection = false, |
| 44 | EncodingType encodingType = k4X); |
| 45 | // TODO: [Not Supported] Encoder(DigitalSource *aSource, DigitalSource |
| 46 | // *bSource, bool reverseDirection=false, EncodingType encodingType = k4X); |
| 47 | // TODO: [Not Supported] Encoder(DigitalSource &aSource, DigitalSource |
| 48 | // &bSource, bool reverseDirection=false, EncodingType encodingType = k4X); |
| 49 | virtual ~Encoder() = default; |
| 50 | |
| 51 | // CounterBase interface |
| 52 | int Get() const override; |
| 53 | int GetRaw() const; |
| 54 | int GetEncodingScale() const; |
| 55 | void Reset() override; |
| 56 | double GetPeriod() const override; |
| 57 | void SetMaxPeriod(double maxPeriod) override; |
| 58 | bool GetStopped() const override; |
| 59 | bool GetDirection() const override; |
| 60 | |
| 61 | double GetDistance() const; |
| 62 | double GetRate() const; |
| 63 | void SetMinRate(double minRate); |
| 64 | void SetDistancePerPulse(double distancePerPulse); |
| 65 | void SetReverseDirection(bool reverseDirection); |
| 66 | void SetSamplesToAverage(int samplesToAverage); |
| 67 | int GetSamplesToAverage() const; |
| 68 | void SetPIDSourceType(PIDSourceType pidSource); |
| 69 | double PIDGet() override; |
| 70 | |
| 71 | void UpdateTable() override; |
| 72 | void StartLiveWindowMode() override; |
| 73 | void StopLiveWindowMode() override; |
| 74 | std::string GetSmartDashboardType() const override; |
| 75 | void InitTable(std::shared_ptr<ITable> subTable) override; |
| 76 | std::shared_ptr<ITable> GetTable() const override; |
| 77 | |
| 78 | int FPGAEncoderIndex() const { return 0; } |
| 79 | |
| 80 | private: |
| 81 | void InitEncoder(int channelA, int channelB, bool reverseDirection, |
| 82 | EncodingType encodingType); |
| 83 | double DecodingScaleFactor() const; |
| 84 | |
| 85 | // the A phase of the quad encoder |
| 86 | // TODO: [Not Supported] DigitalSource *m_aSource; |
| 87 | // the B phase of the quad encoder |
| 88 | // TODO: [Not Supported] DigitalSource *m_bSource; |
| 89 | // was the A source allocated locally? |
| 90 | // TODO: [Not Supported] bool m_allocatedASource; |
| 91 | // was the B source allocated locally? |
| 92 | // TODO: [Not Supported] bool m_allocatedBSource; |
| 93 | int channelA, channelB; |
| 94 | double m_distancePerPulse; // distance of travel for each encoder tick |
| 95 | EncodingType m_encodingType; // Encoding type |
| 96 | int m_encodingScale; // 1x, 2x, or 4x, per the encodingType |
| 97 | bool m_reverseDirection; |
| 98 | SimEncoder* impl; |
| 99 | |
| 100 | std::shared_ptr<ITable> m_table; |
| 101 | }; |
| 102 | |
| 103 | } // namespace frc |