WPILibC++  unspecified
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Pages
Encoder.h
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;
24 
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
double GetDistance() const
Get the distance the robot has driven since the last reset.
Definition: Encoder.cpp:313
int GetSamplesToAverage() const
Get the Samples to Average which specifies the number of samples of the timer to average when calcula...
Definition: Encoder.cpp:419
void SetReverseDirection(bool reverseDirection)
Set the direction sensing for this encoder.
Definition: Encoder.cpp:382
void SetMinRate(double minRate)
Set the minimum rate of the device before the hardware reports it stopped.
Definition: Encoder.cpp:343
std::string GetSmartDashboardType() const override
Definition: Encoder.cpp:512
void UpdateTable() override
Update the table for this sendable object with the latest values.
Definition: Encoder.cpp:496
void SetIndexSource(int channel, IndexingType type=kResetOnRisingEdge)
Set the index source for the encoder.
Definition: Encoder.cpp:451
int Get() const override
Gets the current count.
Definition: Encoder.cpp:202
bool GetStopped() const override
Determine if the encoder is stopped.
Definition: Encoder.cpp:274
Live Window Sendable is a special type of object sendable to the live window.
Definition: LiveWindowSendable.h:17
Base class for all sensors.
Definition: SensorBase.h:22
Class to read quad encoders.
Definition: Encoder.h:39
PIDSource interface is a generic sensor source for the PID class.
Definition: PIDSource.h:19
Class to enable glitch filtering on a set of digital inputs.
Definition: DigitalGlitchFilter.h:26
bool GetDirection() const override
The last direction the encoder value changed.
Definition: Encoder.cpp:287
Interface for counting the number of ticks on a digital input channel.
Definition: CounterBase.h:22
double GetPeriod() const override
Returns the period of the most recent pulse.
Definition: Encoder.cpp:234
std::shared_ptr< ITable > GetTable() const override
Definition: Encoder.cpp:527
virtual ~Encoder()
Free the resources for an Encoder.
Definition: Encoder.cpp:159
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:398
void SetDistancePerPulse(double distancePerPulse)
Set the distance per pulse for this encoder.
Definition: Encoder.cpp:367
DigitalSource Interface.
Definition: DigitalSource.h:25
void InitTable(std::shared_ptr< ITable > subTable) override
Initializes a table for this sendable object.
Definition: Encoder.cpp:522
double PIDGet() override
Implement the PIDSource interface.
Definition: Encoder.cpp:431
void SetMaxPeriod(double maxPeriod) override
Sets the maximum period for stopped detection.
Definition: Encoder.cpp:258
int GetEncodingScale() const
The encoding scale factor 1x, 2x, or 4x, per the requested encodingType.
Definition: Encoder.cpp:170
double GetRate() const
Get the current rate of the encoder.
Definition: Encoder.cpp:329
void Reset() override
Reset the Encoder distance to zero.
Definition: Encoder.cpp:215
void StopLiveWindowMode() override
Stop having this sendable object automatically respond to value changes.
Definition: Encoder.cpp:510
int GetRaw() const
Gets the raw value from the encoder.
Definition: Encoder.cpp:185
Encoder(int aChannel, int bChannel, bool reverseDirection=false, EncodingType encodingType=k4X)
Encoder constructor.
Definition: Encoder.cpp:74
void StartLiveWindowMode() override
Start having this sendable object automatically respond to value changes reflect the value on the tab...
Definition: Encoder.cpp:508