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 <memory> |
| 11 | #include <string> |
| 12 | |
| 13 | #include "Counter.h" |
| 14 | #include "CounterBase.h" |
| 15 | #include "HAL/Encoder.h" |
| 16 | #include "LiveWindow/LiveWindowSendable.h" |
| 17 | #include "PIDSource.h" |
| 18 | #include "SensorBase.h" |
| 19 | |
| 20 | namespace frc { |
| 21 | |
| 22 | class DigitalSource; |
| 23 | class DigitalGlitchFilter; |
| 24 | |
| 25 | /** |
| 26 | * Class to read quad encoders. |
| 27 | * Quadrature encoders are devices that count shaft rotation and can sense |
| 28 | * direction. The output of the QuadEncoder class is an integer that can count |
| 29 | * either up or down, and can go negative for reverse direction counting. When |
| 30 | * creating QuadEncoders, a direction is supplied that changes the sense of the |
| 31 | * output to make code more readable if the encoder is mounted such that forward |
| 32 | * movement generates negative values. Quadrature encoders have two digital |
| 33 | * outputs, an A Channel and a B Channel that are out of phase with each other |
| 34 | * to allow the FPGA to do direction sensing. |
| 35 | * |
| 36 | * All encoders will immediately start counting - Reset() them if you need them |
| 37 | * to be zeroed before use. |
| 38 | */ |
| 39 | class Encoder : public SensorBase, |
| 40 | public CounterBase, |
| 41 | public PIDSource, |
| 42 | public LiveWindowSendable { |
| 43 | public: |
| 44 | enum IndexingType { |
| 45 | kResetWhileHigh, |
| 46 | kResetWhileLow, |
| 47 | kResetOnFallingEdge, |
| 48 | kResetOnRisingEdge |
| 49 | }; |
| 50 | |
| 51 | Encoder(int aChannel, int bChannel, bool reverseDirection = false, |
| 52 | EncodingType encodingType = k4X); |
| 53 | Encoder(std::shared_ptr<DigitalSource> aSource, |
| 54 | std::shared_ptr<DigitalSource> bSource, bool reverseDirection = false, |
| 55 | EncodingType encodingType = k4X); |
| 56 | Encoder(DigitalSource* aSource, DigitalSource* bSource, |
| 57 | bool reverseDirection = false, EncodingType encodingType = k4X); |
| 58 | Encoder(DigitalSource& aSource, DigitalSource& bSource, |
| 59 | bool reverseDirection = false, EncodingType encodingType = k4X); |
| 60 | virtual ~Encoder(); |
| 61 | |
| 62 | // CounterBase interface |
| 63 | int Get() const override; |
| 64 | int GetRaw() const; |
| 65 | int GetEncodingScale() const; |
| 66 | void Reset() override; |
| 67 | double GetPeriod() const override; |
| 68 | void SetMaxPeriod(double maxPeriod) override; |
| 69 | bool GetStopped() const override; |
| 70 | bool GetDirection() const override; |
| 71 | |
| 72 | double GetDistance() const; |
| 73 | double GetRate() const; |
| 74 | void SetMinRate(double minRate); |
| 75 | void SetDistancePerPulse(double distancePerPulse); |
| 76 | void SetReverseDirection(bool reverseDirection); |
| 77 | void SetSamplesToAverage(int samplesToAverage); |
| 78 | int GetSamplesToAverage() const; |
| 79 | double PIDGet() override; |
| 80 | |
| 81 | void SetIndexSource(int channel, IndexingType type = kResetOnRisingEdge); |
| 82 | WPI_DEPRECATED("Use pass-by-reference instead.") |
| 83 | void SetIndexSource(DigitalSource* source, |
| 84 | IndexingType type = kResetOnRisingEdge); |
| 85 | void SetIndexSource(const DigitalSource& source, |
| 86 | IndexingType type = kResetOnRisingEdge); |
| 87 | |
| 88 | void UpdateTable() override; |
| 89 | void StartLiveWindowMode() override; |
| 90 | void StopLiveWindowMode() override; |
| 91 | std::string GetSmartDashboardType() const override; |
| 92 | void InitTable(std::shared_ptr<ITable> subTable) override; |
| 93 | std::shared_ptr<ITable> GetTable() const override; |
| 94 | |
| 95 | int GetFPGAIndex() const; |
| 96 | |
| 97 | private: |
| 98 | void InitEncoder(bool reverseDirection, EncodingType encodingType); |
| 99 | |
| 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 | std::unique_ptr<DigitalSource> m_indexSource = nullptr; |
| 105 | HAL_EncoderHandle m_encoder = HAL_kInvalidHandle; |
| 106 | |
| 107 | std::shared_ptr<ITable> m_table; |
| 108 | friend class DigitalGlitchFilter; |
| 109 | }; |
| 110 | |
| 111 | } // namespace frc |