WPILibC++ 2027.0.0-alpha-4
Loading...
Searching...
No Matches
Encoder.hpp
Go to the documentation of this file.
1// Copyright (c) FIRST and other WPILib contributors.
2// Open Source Software; you can modify and/or share it under the terms of
3// the WPILib BSD license file in the root directory of this project.
4
5#pragma once
6
7#include "wpi/hal/Encoder.h"
8#include "wpi/hal/Types.hpp"
12
13namespace wpi {
14/**
15 * Class to read quad encoders.
16 *
17 * Quadrature encoders are devices that count shaft rotation and can sense
18 * direction. The output of the QuadEncoder class is an integer that can count
19 * either up or down, and can go negative for reverse direction counting. When
20 * creating QuadEncoders, a direction is supplied that changes the sense of the
21 * output to make code more readable if the encoder is mounted such that forward
22 * movement generates negative values. Quadrature encoders have two digital
23 * outputs, an A Channel and a B Channel that are out of phase with each other
24 * to allow the FPGA to do direction sensing.
25 *
26 * All encoders will immediately start counting - Reset() them if you need them
27 * to be zeroed before use.
28 */
29class Encoder : public CounterBase,
31 public wpi::util::SendableHelper<Encoder> {
32 public:
33 /**
34 * Encoder constructor.
35 *
36 * Construct a Encoder given a and b channels.
37 *
38 * The counter will start counting immediately.
39 *
40 * @param aChannel The a channel DIO channel. 0-9 are on-board, 10-25
41 * are on the MXP port
42 * @param bChannel The b channel DIO channel. 0-9 are on-board, 10-25
43 * are on the MXP port
44 * @param reverseDirection represents the orientation of the encoder and
45 * inverts the output values if necessary so forward
46 * represents positive values.
47 * @param encodingType either k1X, k2X, or k4X to indicate 1X, 2X or 4X
48 * decoding. If 4X is selected, then an encoder FPGA
49 * object is used and the returned counts will be 4x
50 * the encoder spec'd value since all rising and
51 * falling edges are counted. If 1X or 2X are selected
52 * then a counter object will be used and the returned
53 * value will either exactly match the spec'd count or
54 * be double (2x) the spec'd count.
55 */
56 Encoder(int aChannel, int bChannel, bool reverseDirection = false,
57 EncodingType encodingType = k4X);
58
59 Encoder(Encoder&&) = default;
60 Encoder& operator=(Encoder&&) = default;
61
62 ~Encoder() override = default;
63
64 // CounterBase interface
65 /**
66 * Gets the current count.
67 *
68 * Returns the current count on the Encoder. This method compensates for the
69 * decoding type.
70 *
71 * @return Current count from the Encoder adjusted for the 1x, 2x, or 4x scale
72 * factor.
73 */
74 int Get() const override;
75
76 /**
77 * Reset the Encoder distance to zero.
78 *
79 * Resets the current count to zero on the encoder.
80 */
81 void Reset() override;
82
83 /**
84 * Returns the period of the most recent pulse.
85 *
86 * Returns the period of the most recent Encoder pulse in seconds. This method
87 * compensates for the decoding type.
88 *
89 * Warning: This returns unscaled periods. Use GetRate() for rates that are
90 * scaled using the value from SetDistancePerPulse().
91 *
92 * @return Period in seconds of the most recent pulse.
93 * @deprecated Use getRate() in favor of this method.
94 */
95 [[deprecated("Use GetRate() in favor of this method")]]
96 wpi::units::second_t GetPeriod() const override;
97
98 /**
99 * Sets the maximum period for stopped detection.
100 *
101 * Sets the value that represents the maximum period of the Encoder before it
102 * will assume that the attached device is stopped. This timeout allows users
103 * to determine if the wheels or other shaft has stopped rotating.
104 * This method compensates for the decoding type.
105 *
106 * @param maxPeriod The maximum time between rising and falling edges before
107 * the FPGA will report the device stopped. This is expressed
108 * in seconds.
109 * @deprecated Use SetMinRate() in favor of this method. This takes unscaled
110 * periods and SetMinRate() scales using value from
111 * SetDistancePerPulse().
112 */
113 [[deprecated(
114 "Use SetMinRate() in favor of this method. This takes unscaled periods "
115 "and SetMinRate() scales using value from SetDistancePerPulse().")]]
116 void SetMaxPeriod(wpi::units::second_t maxPeriod) override;
117
118 /**
119 * Determine if the encoder is stopped.
120 *
121 * Using the MaxPeriod value, a boolean is returned that is true if the
122 * encoder is considered stopped and false if it is still moving. A stopped
123 * encoder is one where the most recent pulse width exceeds the MaxPeriod.
124 *
125 * @return True if the encoder is considered stopped.
126 */
127 bool GetStopped() const override;
128
129 /**
130 * The last direction the encoder value changed.
131 *
132 * @return The last direction the encoder value changed.
133 */
134 bool GetDirection() const override;
135
136 /**
137 * Gets the raw value from the encoder.
138 *
139 * The raw value is the actual count unscaled by the 1x, 2x, or 4x scale
140 * factor.
141 *
142 * @return Current raw count from the encoder
143 */
144 int GetRaw() const;
145
146 /**
147 * The encoding scale factor 1x, 2x, or 4x, per the requested encodingType.
148 *
149 * Used to divide raw edge counts down to spec'd counts.
150 */
151 int GetEncodingScale() const;
152
153 /**
154 * Get the distance the robot has driven since the last reset.
155 *
156 * @return The distance driven since the last reset as scaled by the value
157 * from SetDistancePerPulse().
158 */
159 double GetDistance() const;
160
161 /**
162 * Get the current rate of the encoder.
163 *
164 * Units are distance per second as scaled by the value from
165 * SetDistancePerPulse().
166 *
167 * @return The current rate of the encoder.
168 */
169 double GetRate() const;
170
171 /**
172 * Set the minimum rate of the device before the hardware reports it stopped.
173 *
174 * @param minRate The minimum rate. The units are in distance per second as
175 * scaled by the value from SetDistancePerPulse().
176 */
177 void SetMinRate(double minRate);
178
179 /**
180 * Set the distance per pulse for this encoder.
181 *
182 * This sets the multiplier used to determine the distance driven based on the
183 * count value from the encoder.
184 *
185 * Do not include the decoding type in this scale. The library already
186 * compensates for the decoding type.
187 *
188 * Set this value based on the encoder's rated Pulses per Revolution and
189 * factor in gearing reductions following the encoder shaft.
190 *
191 * This distance can be in any units you like, linear or angular.
192 *
193 * @param distancePerPulse The scale factor that will be used to convert
194 * pulses to useful units.
195 */
196 void SetDistancePerPulse(double distancePerPulse);
197
198 /**
199 * Get the distance per pulse for this encoder.
200 *
201 * @return The scale factor that will be used to convert pulses to useful
202 * units.
203 */
204 double GetDistancePerPulse() const;
205
206 /**
207 * Set the direction sensing for this encoder.
208 *
209 * This sets the direction sensing on the encoder so that it could count in
210 * the correct software direction regardless of the mounting.
211 *
212 * @param reverseDirection true if the encoder direction should be reversed
213 */
214 void SetReverseDirection(bool reverseDirection);
215
216 /**
217 * Set the Samples to Average which specifies the number of samples of the
218 * timer to average when calculating the period.
219 *
220 * Perform averaging to account for mechanical imperfections or as
221 * oversampling to increase resolution.
222 *
223 * @param samplesToAverage The number of samples to average from 1 to 127.
224 */
225 void SetSamplesToAverage(int samplesToAverage);
226
227 /**
228 * Get the Samples to Average which specifies the number of samples of the
229 * timer to average when calculating the period.
230 *
231 * Perform averaging to account for mechanical imperfections or as
232 * oversampling to increase resolution.
233 *
234 * @return The number of samples being averaged (from 1 to 127)
235 */
237
238 /**
239 * Indicates this encoder is used by a simulated device.
240 *
241 * @param device simulated device handle
242 */
244
245 int GetFPGAIndex() const;
246
248
249 private:
250 /**
251 * Common initialization code for Encoders.
252 *
253 * This code allocates resources for Encoders and is common to all
254 * constructors. The counter will start counting immediately.
255 * @param aChannel The a channel.
256 * @param bChannel The b channel.
257 * @param reverseDirection If true, counts down instead of up (this is all
258 * relative)
259 * @param encodingType either k1X, k2X, or k4X to indicate 1X, 2X or 4X
260 * decoding. If 4X is selected, then an encoder FPGA
261 * object is used and the returned counts will be 4x
262 * the encoder spec'd value since all rising and
263 * falling edges are counted. If 1X or 2X are selected
264 * then a counter object will be used and the returned
265 * value will either exactly match the spec'd count or
266 * be double (2x) the spec'd count.
267 */
268 void InitEncoder(int aChannel, int bChannel, bool reverseDirection,
269 EncodingType encodingType);
270
271 /**
272 * The scale needed to convert a raw counter value into a number of encoder
273 * pulses.
274 */
275 double DecodingScaleFactor() const;
276
278};
279
280} // namespace wpi
EncodingType
Definition CounterBase.hpp:22
@ k4X
Definition CounterBase.hpp:22
CounterBase()=default
void SetMinRate(double minRate)
Set the minimum rate of the device before the hardware reports it stopped.
~Encoder() override=default
double GetDistance() const
Get the distance the robot has driven since the last reset.
int GetSamplesToAverage() const
Get the Samples to Average which specifies the number of samples of the timer to average when calcula...
void Reset() override
Reset the Encoder distance to zero.
int GetEncodingScale() const
The encoding scale factor 1x, 2x, or 4x, per the requested encodingType.
void SetReverseDirection(bool reverseDirection)
Set the direction sensing for this encoder.
void SetSimDevice(HAL_SimDeviceHandle device)
Indicates this encoder is used by a simulated device.
double GetDistancePerPulse() const
Get the distance per pulse for this encoder.
void InitSendable(wpi::util::SendableBuilder &builder) override
Initializes this Sendable object.
Encoder(Encoder &&)=default
wpi::units::second_t GetPeriod() const override
Returns the period of the most recent pulse.
bool GetStopped() const override
Determine if the encoder is stopped.
bool GetDirection() const override
The last direction the encoder value changed.
void SetDistancePerPulse(double distancePerPulse)
Set the distance per pulse for this encoder.
void SetSamplesToAverage(int samplesToAverage)
Set the Samples to Average which specifies the number of samples of the timer to average when calcula...
double GetRate() const
Get the current rate of the encoder.
Encoder & operator=(Encoder &&)=default
int GetFPGAIndex() const
int Get() const override
Gets the current count.
int GetRaw() const
Gets the raw value from the encoder.
Encoder(int aChannel, int bChannel, bool reverseDirection=false, EncodingType encodingType=k4X)
Encoder constructor.
void SetMaxPeriod(wpi::units::second_t maxPeriod) override
Sets the maximum period for stopped detection.
A move-only C++ wrapper around a HAL handle.
Definition Types.hpp:16
Helper class for building Sendable dashboard representations.
Definition SendableBuilder.hpp:21
A helper class for use with objects that add themselves to SendableRegistry.
Definition SendableHelper.hpp:21
Interface for Sendable objects.
Definition Sendable.hpp:16
HAL_Handle HAL_SimDeviceHandle
Definition Types.h:53
Definition CvSource.hpp:15