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 "ErrorBase.h"
17 #include "PIDSource.h"
18 #include "SmartDashboard/SendableBase.h"
19 
20 namespace frc {
21 
22 class DigitalSource;
23 class DigitalGlitchFilter;
24 
40 class Encoder : public ErrorBase,
41  public SendableBase,
42  public CounterBase,
43  public PIDSource {
44  public:
45  enum IndexingType {
46  kResetWhileHigh,
47  kResetWhileLow,
48  kResetOnFallingEdge,
49  kResetOnRisingEdge
50  };
51 
75  Encoder(int aChannel, int bChannel, bool reverseDirection = false,
76  EncodingType encodingType = k4X);
77 
101  Encoder(DigitalSource* aSource, DigitalSource* bSource,
102  bool reverseDirection = false, EncodingType encodingType = k4X);
103 
127  Encoder(DigitalSource& aSource, DigitalSource& bSource,
128  bool reverseDirection = false, EncodingType encodingType = k4X);
129 
130  Encoder(std::shared_ptr<DigitalSource> aSource,
131  std::shared_ptr<DigitalSource> bSource, bool reverseDirection = false,
132  EncodingType encodingType = k4X);
133 
134  ~Encoder() override;
135 
136  // CounterBase interface
146  int Get() const override;
147 
153  void Reset() override;
154 
166  double GetPeriod() const override;
167 
184  void SetMaxPeriod(double maxPeriod) override;
185 
195  bool GetStopped() const override;
196 
202  bool GetDirection() const override;
203 
212  int GetRaw() const;
213 
219  int GetEncodingScale() const;
220 
227  double GetDistance() const;
228 
237  double GetRate() const;
238 
245  void SetMinRate(double minRate);
246 
264  void SetDistancePerPulse(double distancePerPulse);
265 
272  double GetDistancePerPulse() const;
273 
282  void SetReverseDirection(bool reverseDirection);
283 
293  void SetSamplesToAverage(int samplesToAverage);
294 
304  int GetSamplesToAverage() const;
305 
306  double PIDGet() override;
307 
316  void SetIndexSource(int channel, IndexingType type = kResetOnRisingEdge);
317 
326  void SetIndexSource(const DigitalSource& source,
327  IndexingType type = kResetOnRisingEdge);
328 
329  int GetFPGAIndex() const;
330 
331  void InitSendable(SendableBuilder& builder) override;
332 
333  private:
351  void InitEncoder(bool reverseDirection, EncodingType encodingType);
352 
357  double DecodingScaleFactor() const;
358 
359  std::shared_ptr<DigitalSource> m_aSource; // The A phase of the quad encoder
360  std::shared_ptr<DigitalSource> m_bSource; // The B phase of the quad encoder
361  std::shared_ptr<DigitalSource> m_indexSource = nullptr;
362  HAL_EncoderHandle m_encoder = HAL_kInvalidHandle;
363 
364  friend class DigitalGlitchFilter;
365 };
366 
367 } // namespace frc
double GetDistance() const
Get the distance the robot has driven since the last reset.
Definition: Encoder.cpp:121
int GetSamplesToAverage() const
Get the Samples to Average which specifies the number of samples of the timer to average when calcula...
Definition: Encoder.cpp:178
void SetReverseDirection(bool reverseDirection)
Set the direction sensing for this encoder.
Definition: Encoder.cpp:159
void SetMinRate(double minRate)
Set the minimum rate of the device before the hardware reports it stopped.
Definition: Encoder.cpp:137
Definition: Utility.cpp:119
void SetIndexSource(int channel, IndexingType type=kResetOnRisingEdge)
Set the index source for the encoder.
Definition: Encoder.cpp:197
int Get() const override
Gets the current count.
Definition: Encoder.cpp:60
bool GetStopped() const override
Determine if the encoder is stopped.
Definition: Encoder.cpp:90
Class to read quad encoders.
Definition: Encoder.h:40
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:32
bool GetDirection() const override
The last direction the encoder value changed.
Definition: Encoder.cpp:98
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:75
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:166
double GetDistancePerPulse() const
Get the distance per pulse for this encoder.
Definition: Encoder.cpp:151
void SetDistancePerPulse(double distancePerPulse)
Set the distance per pulse for this encoder.
Definition: Encoder.cpp:144
DigitalSource Interface.
Definition: DigitalSource.h:25
void SetMaxPeriod(double maxPeriod) override
Sets the maximum period for stopped detection.
Definition: Encoder.cpp:83
int GetEncodingScale() const
The encoding scale factor 1x, 2x, or 4x, per the requested encodingType.
Definition: Encoder.cpp:114
double GetRate() const
Get the current rate of the encoder.
Definition: Encoder.cpp:129
void Reset() override
Reset the Encoder distance to zero.
Definition: Encoder.cpp:68
Base class for most objects.
Definition: ErrorBase.h:74
int GetRaw() const
Gets the raw value from the encoder.
Definition: Encoder.cpp:106
Definition: SendableBase.h:19
Encoder(int aChannel, int bChannel, bool reverseDirection=false, EncodingType encodingType=k4X)
Encoder constructor.
Definition: Encoder.cpp:18
Definition: SendableBuilder.h:23
void InitSendable(SendableBuilder &builder) override
Initializes this Sendable object.
Definition: Encoder.cpp:221