Parker Schuh | d3b7a887 | 2018-02-19 16:42:27 -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 "HAL/Encoder.h" |
| 14 | |
| 15 | namespace frc { |
| 16 | |
| 17 | class DigitalSource; |
| 18 | class DigitalGlitchFilter; |
| 19 | |
| 20 | /** |
| 21 | * Class to read quad encoders. |
| 22 | * Quadrature encoders are devices that count shaft rotation and can sense |
| 23 | * direction. The output of the QuadEncoder class is an integer that can count |
| 24 | * either up or down, and can go negative for reverse direction counting. When |
| 25 | * creating QuadEncoders, a direction is supplied that changes the sense of the |
| 26 | * output to make code more readable if the encoder is mounted such that forward |
| 27 | * movement generates negative values. Quadrature encoders have two digital |
| 28 | * outputs, an A Channel and a B Channel that are out of phase with each other |
| 29 | * to allow the FPGA to do direction sensing. |
| 30 | * |
| 31 | * All encoders will immediately start counting - Reset() them if you need them |
| 32 | * to be zeroed before use. |
| 33 | */ |
| 34 | class Encoder { |
| 35 | public: |
| 36 | enum EncodingType { k1X, k2X, k4X }; |
| 37 | |
| 38 | Encoder(int aChannel, int bChannel, bool reverseDirection = false, |
| 39 | EncodingType encodingType = k4X); |
| 40 | virtual ~Encoder(); |
| 41 | |
| 42 | int GetEncodingScale() const; |
| 43 | |
| 44 | int GetRaw() const; |
| 45 | |
| 46 | double GetPeriod() const; |
| 47 | void SetMaxPeriod(double maxPeriod); |
| 48 | |
| 49 | int GetFPGAIndex() const; |
| 50 | |
| 51 | private: |
| 52 | void InitEncoder(bool reverseDirection, EncodingType encodingType); |
| 53 | |
| 54 | double DecodingScaleFactor() const; |
| 55 | |
| 56 | std::shared_ptr<DigitalSource> m_aSource; // the A phase of the quad encoder |
| 57 | std::shared_ptr<DigitalSource> m_bSource; // the B phase of the quad encoder |
| 58 | std::unique_ptr<DigitalSource> m_indexSource = nullptr; |
| 59 | HAL_EncoderHandle m_encoder = HAL_kInvalidHandle; |
| 60 | |
| 61 | friend class DigitalGlitchFilter; |
| 62 | }; |
| 63 | |
| 64 | } // namespace frc |