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