WPILibC++  unspecified
Encoder.h
1 /*----------------------------------------------------------------------------*/
2 /* Copyright (c) 2008-2018 FIRST. 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 
12 #include <HAL/Encoder.h>
13 
14 #include "Counter.h"
15 #include "CounterBase.h"
16 #include "PIDSource.h"
17 #include "SensorBase.h"
18 
19 namespace frc {
20 
21 class DigitalSource;
23 
39 class Encoder : public SensorBase, public CounterBase, public PIDSource {
40  public:
41  enum IndexingType {
42  kResetWhileHigh,
43  kResetWhileLow,
44  kResetOnFallingEdge,
45  kResetOnRisingEdge
46  };
47 
48  Encoder(int aChannel, int bChannel, bool reverseDirection = false,
49  EncodingType encodingType = k4X);
50  Encoder(std::shared_ptr<DigitalSource> aSource,
51  std::shared_ptr<DigitalSource> bSource, bool reverseDirection = false,
52  EncodingType encodingType = k4X);
53  Encoder(DigitalSource* aSource, DigitalSource* bSource,
54  bool reverseDirection = false, EncodingType encodingType = k4X);
55  Encoder(DigitalSource& aSource, DigitalSource& bSource,
56  bool reverseDirection = false, EncodingType encodingType = k4X);
57  ~Encoder() override;
58 
59  // CounterBase interface
60  int Get() const override;
61  int GetRaw() const;
62  int GetEncodingScale() const;
63  void Reset() override;
64  double GetPeriod() const override;
65  void SetMaxPeriod(double maxPeriod) override;
66  bool GetStopped() const override;
67  bool GetDirection() const override;
68 
69  double GetDistance() const;
70  double GetRate() const;
71  void SetMinRate(double minRate);
72  void SetDistancePerPulse(double distancePerPulse);
73  double GetDistancePerPulse() const;
74  void SetReverseDirection(bool reverseDirection);
75  void SetSamplesToAverage(int samplesToAverage);
76  int GetSamplesToAverage() const;
77  double PIDGet() override;
78 
79  void SetIndexSource(int channel, IndexingType type = kResetOnRisingEdge);
80  void SetIndexSource(const DigitalSource& source,
81  IndexingType type = kResetOnRisingEdge);
82 
83  void InitSendable(SendableBuilder& builder) override;
84 
85  int GetFPGAIndex() const;
86 
87  private:
88  void InitEncoder(bool reverseDirection, EncodingType encodingType);
89 
90  double DecodingScaleFactor() const;
91 
92  std::shared_ptr<DigitalSource> m_aSource; // The A phase of the quad encoder
93  std::shared_ptr<DigitalSource> m_bSource; // The B phase of the quad encoder
94  std::shared_ptr<DigitalSource> m_indexSource = nullptr;
95  HAL_EncoderHandle m_encoder = HAL_kInvalidHandle;
96 
97  friend class DigitalGlitchFilter;
98 };
99 
100 } // namespace frc
double GetDistance() const
Get the distance the robot has driven since the last reset.
Definition: Encoder.cpp:314
int GetSamplesToAverage() const
Get the Samples to Average which specifies the number of samples of the timer to average when calcula...
Definition: Encoder.cpp:433
void SetReverseDirection(bool reverseDirection)
Set the direction sensing for this encoder.
Definition: Encoder.cpp:396
void SetMinRate(double minRate)
Set the minimum rate of the device before the hardware reports it stopped.
Definition: Encoder.cpp:344
Definition: RobotController.cpp:14
void SetIndexSource(int channel, IndexingType type=kResetOnRisingEdge)
Set the index source for the encoder.
Definition: Encoder.cpp:465
int Get() const override
Gets the current count.
Definition: Encoder.cpp:204
bool GetStopped() const override
Determine if the encoder is stopped.
Definition: Encoder.cpp:275
Base class for all sensors.
Definition: SensorBase.h:25
Class to read quad encoders.
Definition: Encoder.h:39
PIDSource interface is a generic sensor source for the PID class.
Definition: PIDSource.h:20
Class to enable glitch filtering on a set of digital inputs.
Definition: DigitalGlitchFilter.h:31
bool GetDirection() const override
The last direction the encoder value changed.
Definition: Encoder.cpp:288
Interface for counting the number of ticks on a digital input channel.
Definition: CounterBase.h:21
double GetPeriod() const override
Returns the period of the most recent pulse.
Definition: Encoder.cpp:235
void SetSamplesToAverage(int samplesToAverage)
Set the Samples to Average which specifies the number of samples of the timer to average when calcula...
Definition: Encoder.cpp:412
~Encoder() override
Free the resources for an Encoder.
Definition: Encoder.cpp:161
double GetDistancePerPulse() const
Get the distance per pulse for this encoder.
Definition: Encoder.cpp:380
void SetDistancePerPulse(double distancePerPulse)
Set the distance per pulse for this encoder.
Definition: Encoder.cpp:368
DigitalSource Interface.
Definition: DigitalSource.h:25
double PIDGet() override
Implement the PIDSource interface.
Definition: Encoder.cpp:445
void SetMaxPeriod(double maxPeriod) override
Sets the maximum period for stopped detection.
Definition: Encoder.cpp:259
int GetEncodingScale() const
The encoding scale factor 1x, 2x, or 4x, per the requested encodingType.
Definition: Encoder.cpp:172
double GetRate() const
Get the current rate of the encoder.
Definition: Encoder.cpp:330
void Reset() override
Reset the Encoder distance to zero.
Definition: Encoder.cpp:217
int GetRaw() const
Gets the raw value from the encoder.
Definition: Encoder.cpp:187
Encoder(int aChannel, int bChannel, bool reverseDirection=false, EncodingType encodingType=k4X)
Encoder constructor.
Definition: Encoder.cpp:74
Definition: SendableBuilder.h:23
void InitSendable(SendableBuilder &builder) override
Initializes this Sendable object.
Definition: Encoder.cpp:497