WPILibC++  2020.3.2
Encoder.h
1 /*----------------------------------------------------------------------------*/
2 /* Copyright (c) 2008-2019 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/Types.h>
13 
14 #include "frc/Counter.h"
15 #include "frc/CounterBase.h"
16 #include "frc/ErrorBase.h"
17 #include "frc/PIDSource.h"
18 #include "frc/smartdashboard/Sendable.h"
19 #include "frc/smartdashboard/SendableHelper.h"
20 
21 namespace frc {
22 
23 class DigitalSource;
24 class DigitalGlitchFilter;
25 class SendableBuilder;
26 class DMA;
27 class DMASample;
28 
44 class Encoder : public ErrorBase,
45  public CounterBase,
46  public PIDSource,
47  public Sendable,
48  public SendableHelper<Encoder> {
49  friend class DMA;
50  friend class DMASample;
51 
52  public:
53  enum IndexingType {
54  kResetWhileHigh,
55  kResetWhileLow,
56  kResetOnFallingEdge,
57  kResetOnRisingEdge
58  };
59 
83  Encoder(int aChannel, int bChannel, bool reverseDirection = false,
84  EncodingType encodingType = k4X);
85 
109  Encoder(DigitalSource* aSource, DigitalSource* bSource,
110  bool reverseDirection = false, EncodingType encodingType = k4X);
111 
135  Encoder(DigitalSource& aSource, DigitalSource& bSource,
136  bool reverseDirection = false, EncodingType encodingType = k4X);
137 
138  Encoder(std::shared_ptr<DigitalSource> aSource,
139  std::shared_ptr<DigitalSource> bSource, bool reverseDirection = false,
140  EncodingType encodingType = k4X);
141 
142  ~Encoder() override;
143 
144  Encoder(Encoder&&) = default;
145  Encoder& operator=(Encoder&&) = default;
146 
147  // CounterBase interface
157  int Get() const override;
158 
164  void Reset() override;
165 
177  double GetPeriod() const override;
178 
195  void SetMaxPeriod(double maxPeriod) override;
196 
206  bool GetStopped() const override;
207 
213  bool GetDirection() const override;
214 
223  int GetRaw() const;
224 
230  int GetEncodingScale() const;
231 
238  double GetDistance() const;
239 
248  double GetRate() const;
249 
256  void SetMinRate(double minRate);
257 
275  void SetDistancePerPulse(double distancePerPulse);
276 
283  double GetDistancePerPulse() const;
284 
293  void SetReverseDirection(bool reverseDirection);
294 
304  void SetSamplesToAverage(int samplesToAverage);
305 
315  int GetSamplesToAverage() const;
316 
317  double PIDGet() override;
318 
327  void SetIndexSource(int channel, IndexingType type = kResetOnRisingEdge);
328 
337  void SetIndexSource(const DigitalSource& source,
338  IndexingType type = kResetOnRisingEdge);
339 
345  void SetSimDevice(HAL_SimDeviceHandle device);
346 
347  int GetFPGAIndex() const;
348 
349  void InitSendable(SendableBuilder& builder) override;
350 
351  private:
369  void InitEncoder(bool reverseDirection, EncodingType encodingType);
370 
375  double DecodingScaleFactor() const;
376 
377  std::shared_ptr<DigitalSource> m_aSource; // The A phase of the quad encoder
378  std::shared_ptr<DigitalSource> m_bSource; // The B phase of the quad encoder
379  std::shared_ptr<DigitalSource> m_indexSource = nullptr;
380  hal::Handle<HAL_EncoderHandle> m_encoder;
381 
382  friend class DigitalGlitchFilter;
383 };
384 
385 } // namespace frc
frc::PIDSource
PIDSource interface is a generic sensor source for the PID class.
Definition: PIDSource.h:20
frc::DMA
Definition: DMA.h:22
frc::Encoder::Get
int Get() const override
Gets the current count.
frc::Encoder::GetDistancePerPulse
double GetDistancePerPulse() const
Get the distance per pulse for this encoder.
frc::DMASample
Definition: DMASample.h:23
frc::Encoder::GetDistance
double GetDistance() const
Get the distance the robot has driven since the last reset.
frc::Encoder::SetSimDevice
void SetSimDevice(HAL_SimDeviceHandle device)
Indicates this encoder is used by a simulated device.
frc::CounterBase
Interface for counting the number of ticks on a digital input channel.
Definition: CounterBase.h:21
frc::Encoder::GetDirection
bool GetDirection() const override
The last direction the encoder value changed.
frc::Encoder::SetMinRate
void SetMinRate(double minRate)
Set the minimum rate of the device before the hardware reports it stopped.
frc::Encoder::GetStopped
bool GetStopped() const override
Determine if the encoder is stopped.
frc::Encoder::SetDistancePerPulse
void SetDistancePerPulse(double distancePerPulse)
Set the distance per pulse for this encoder.
frc::Encoder::InitSendable
void InitSendable(SendableBuilder &builder) override
Initializes this Sendable object.
frc::Encoder::SetReverseDirection
void SetReverseDirection(bool reverseDirection)
Set the direction sensing for this encoder.
frc::Encoder::SetMaxPeriod
void SetMaxPeriod(double maxPeriod) override
Sets the maximum period for stopped detection.
frc::ErrorBase
Base class for most objects.
Definition: ErrorBase.h:104
frc::Encoder::GetSamplesToAverage
int GetSamplesToAverage() const
Get the Samples to Average which specifies the number of samples of the timer to average when calcula...
frc::Encoder::GetRaw
int GetRaw() const
Gets the raw value from the encoder.
frc::DigitalGlitchFilter
Class to enable glitch filtering on a set of digital inputs.
Definition: DigitalGlitchFilter.h:33
frc::Encoder::SetIndexSource
void SetIndexSource(int channel, IndexingType type=kResetOnRisingEdge)
Set the index source for the encoder.
frc::Encoder::GetEncodingScale
int GetEncodingScale() const
The encoding scale factor 1x, 2x, or 4x, per the requested encodingType.
frc::Encoder::Reset
void Reset() override
Reset the Encoder distance to zero.
frc::Encoder::Encoder
Encoder(int aChannel, int bChannel, bool reverseDirection=false, EncodingType encodingType=k4X)
Encoder constructor.
frc::Encoder::SetSamplesToAverage
void SetSamplesToAverage(int samplesToAverage)
Set the Samples to Average which specifies the number of samples of the timer to average when calcula...
frc::Sendable
Interface for Sendable objects.
Definition: Sendable.h:17
frc::Encoder
Class to read quad encoders.
Definition: Encoder.h:44
frc
A class that enforces constraints on the differential drive kinematics.
Definition: SPIAccelerometerSim.h:16
frc::Encoder::GetRate
double GetRate() const
Get the current rate of the encoder.
frc::DigitalSource
DigitalSource Interface.
Definition: DigitalSource.h:25
frc::Encoder::GetPeriod
double GetPeriod() const override
Returns the period of the most recent pulse.
frc::SendableHelper
A helper class for use with objects that add themselves to SendableRegistry.
Definition: SendableHelper.h:28
frc::SendableBuilder
Definition: SendableBuilder.h:23