WPILibC++ 2025.2.1
Loading...
Searching...
No Matches
Encoder.h
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 <stdint.h>
8
9#include "hal/AnalogTrigger.h"
10#include "hal/Types.h"
11
12/**
13 * @defgroup hal_encoder Encoder Functions
14 * @ingroup hal_capi
15 * @{
16 */
17
18/**
19 * The type of index pulse for the encoder.
20 */
27
28/**
29 * The encoding scaling of the encoder.
30 */
36
37#ifdef __cplusplus
38extern "C" {
39#endif
40
41/**
42 * Initializes an encoder.
43 *
44 * @param[in] digitalSourceHandleA the A source (either a HAL_DigitalHandle or a
45 * HAL_AnalogTriggerHandle)
46 * @param[in] analogTriggerTypeA the analog trigger type of the A source if it
47 * is an analog trigger
48 * @param[in] digitalSourceHandleB the B source (either a HAL_DigitalHandle or a
49 * HAL_AnalogTriggerHandle)
50 * @param[in] analogTriggerTypeB the analog trigger type of the B source if it
51 * is an analog trigger
52 * @param[in] reverseDirection true to reverse the counting direction from
53 * standard, otherwise false
54 * @param[in] encodingType the encoding type
55 * @param[out] status Error status variable. 0 on success.
56 * @return the created encoder handle
57 */
59 HAL_Handle digitalSourceHandleA, HAL_AnalogTriggerType analogTriggerTypeA,
60 HAL_Handle digitalSourceHandleB, HAL_AnalogTriggerType analogTriggerTypeB,
61 HAL_Bool reverseDirection, HAL_EncoderEncodingType encodingType,
62 int32_t* status);
63
64/**
65 * Frees an encoder.
66 *
67 * @param[in] encoderHandle the encoder handle
68 */
70
71/**
72 * Indicates the encoder is used by a simulated device.
73 *
74 * @param handle the encoder handle
75 * @param device simulated device handle
76 */
78 HAL_SimDeviceHandle device);
79
80/**
81 * Gets the current counts of the encoder after encoding type scaling.
82 *
83 * This is scaled by the value passed during initialization to encodingType.
84 *
85 * @param[in] encoderHandle the encoder handle
86 * @param[out] status Error status variable. 0 on success.
87 * @return the current scaled count
88 */
89int32_t HAL_GetEncoder(HAL_EncoderHandle encoderHandle, int32_t* status);
90
91/**
92 * Gets the raw counts of the encoder.
93 *
94 * This is not scaled by any values.
95 *
96 * @param[in] encoderHandle the encoder handle
97 * @param[out] status Error status variable. 0 on success.
98 * @return the raw encoder count
99 */
100int32_t HAL_GetEncoderRaw(HAL_EncoderHandle encoderHandle, int32_t* status);
101
102/**
103 * Gets the encoder scale value.
104 *
105 * This is set by the value passed during initialization to encodingType.
106 *
107 * @param[in] encoderHandle the encoder handle
108 * @param[out] status Error status variable. 0 on success.
109 * @return the encoder scale value
110 */
112 int32_t* status);
113
114/**
115 * Reads the current encoder value.
116 *
117 * Read the value at this instant. It may still be running, so it reflects the
118 * current value. Next time it is read, it might have a different value.
119 *
120 * @param[in] encoderHandle the encoder handle
121 * @param[out] status Error status variable. 0 on success.
122 */
123void HAL_ResetEncoder(HAL_EncoderHandle encoderHandle, int32_t* status);
124
125/**
126 * Gets the Period of the most recent count.
127 *
128 * Returns the time interval of the most recent count. This can be used for
129 * velocity calculations to determine shaft speed.
130 *
131 * @param[in] encoderHandle the encoder handle
132 * @param[out] status Error status variable. 0 on success.
133 * @returns the period of the last two pulses in units of seconds
134 */
135double HAL_GetEncoderPeriod(HAL_EncoderHandle encoderHandle, int32_t* status);
136
137/**
138 * Sets the maximum period where the device is still considered "moving".
139 *
140 * Sets the maximum period where the device is considered moving. This value is
141 * used to determine the "stopped" state of the encoder using the
142 * HAL_GetEncoderStopped method.
143 *
144 * @param[in] encoderHandle the encoder handle
145 * @param[in] maxPeriod the maximum period where the counted device is
146 * considered moving in seconds
147 * @param[out] status Error status variable. 0 on success.
148 */
149void HAL_SetEncoderMaxPeriod(HAL_EncoderHandle encoderHandle, double maxPeriod,
150 int32_t* status);
151
152/**
153 * Determines if the clock is stopped.
154 *
155 * Determines if the clocked input is stopped based on the MaxPeriod value set
156 * using the SetMaxPeriod method. If the clock exceeds the MaxPeriod, then the
157 * device (and encoder) are assumed to be stopped and it returns true.
158 *
159 * @param[in] encoderHandle the encoder handle
160 * @param[out] status Error status variable. 0 on success.
161 * @return true if the most recent encoder period exceeds the MaxPeriod value
162 * set by SetMaxPeriod
163 */
165 int32_t* status);
166
167/**
168 * Gets the last direction the encoder value changed.
169 *
170 * @param[in] encoderHandle the encoder handle
171 * @param[out] status Error status variable. 0 on success.
172 * @return the last direction the encoder value changed
173 */
175 int32_t* status);
176
177/**
178 * Gets the current distance traveled by the encoder.
179 *
180 * This is the encoder count scaled by the distance per pulse set for the
181 * encoder.
182 *
183 * @param[in] encoderHandle the encoder handle
184 * @param[out] status Error status variable. 0 on success.
185 * @return the encoder distance (units are determined by the units
186 * passed to HAL_SetEncoderDistancePerPulse)
187 */
188double HAL_GetEncoderDistance(HAL_EncoderHandle encoderHandle, int32_t* status);
189
190/**
191 * Gets the current rate of the encoder.
192 *
193 * This is the encoder period scaled by the distance per pulse set for the
194 * encoder.
195 *
196 * @param[in] encoderHandle the encoder handle
197 * @param[out] status Error status variable. 0 on success.
198 * @return the encoder rate (units are determined by the units passed to
199 * HAL_SetEncoderDistancePerPulse, time value is seconds)
200 */
201double HAL_GetEncoderRate(HAL_EncoderHandle encoderHandle, int32_t* status);
202
203/**
204 * Sets the minimum rate to be considered moving by the encoder.
205 *
206 * Units need to match what is set by HAL_SetEncoderDistancePerPulse, with time
207 * as seconds.
208 *
209 * @param[in] encoderHandle the encoder handle
210 * @param[in] minRate the minimum rate to be considered moving (units are
211 * determined by the units passed to
212 * HAL_SetEncoderDistancePerPulse, time value is
213 * seconds)
214 * @param[out] status Error status variable. 0 on success.
215 */
216void HAL_SetEncoderMinRate(HAL_EncoderHandle encoderHandle, double minRate,
217 int32_t* status);
218
219/**
220 * Sets the distance traveled per encoder pulse. This is used as a scaling
221 * factor for the rate and distance calls.
222 *
223 * @param[in] encoderHandle the encoder handle
224 * @param[in] distancePerPulse the distance traveled per encoder pulse (units
225 * user defined)
226 * @param[out] status Error status variable. 0 on success.
227 */
229 double distancePerPulse, int32_t* status);
230
231/**
232 * Sets if to reverse the direction of the encoder.
233 *
234 * Note that this is not a toggle. It is an absolute set.
235 *
236 * @param[in] encoderHandle the encoder handle
237 * @param[in] reverseDirection true to reverse the direction, false to not.
238 * @param[out] status Error status variable. 0 on success.
239 */
241 HAL_Bool reverseDirection, int32_t* status);
242
243/**
244 * Sets the number of encoder samples to average when calculating encoder rate.
245 *
246 * @param[in] encoderHandle the encoder handle
247 * @param[in] samplesToAverage the number of samples to average
248 * @param[out] status Error status variable. 0 on success.
249 */
251 int32_t samplesToAverage, int32_t* status);
252
253/**
254 * Gets the current samples to average value.
255 *
256 * @param[in] encoderHandle the encoder handle
257 * @param[out] status Error status variable. 0 on success.
258 * @return the current samples to average value
259 */
261 int32_t* status);
262
263/**
264 * Sets the source for an index pulse on the encoder.
265 *
266 * The index pulse can be used to cause an encoder to reset based on an external
267 * input.
268 *
269 * @param[in] encoderHandle the encoder handle
270 * @param[in] digitalSourceHandle the index source handle (either a
271 * HAL_AnalogTriggerHandle or a
272 * HAL_DigitalHandle)
273 * @param[in] analogTriggerType the analog trigger type if the source is an
274 * analog trigger
275 * @param[in] type the index triggering type
276 * @param[out] status Error status variable. 0 on success.
277 */
279 HAL_Handle digitalSourceHandle,
280 HAL_AnalogTriggerType analogTriggerType,
281 HAL_EncoderIndexingType type, int32_t* status);
282
283/**
284 * Gets the FPGA index of the encoder.
285 *
286 * @param[in] encoderHandle the encoder handle
287 * @param[out] status Error status variable. 0 on success.
288 * @return the FPGA index of the encoder
289 */
291 int32_t* status);
292
293/**
294 * Gets the decoding scale factor of the encoder.
295 *
296 * This is used to perform the scaling from raw to type scaled values.
297 *
298 * @param[in] encoderHandle the encoder handle
299 * @param[out] status Error status variable. 0 on success.
300 * @return the scale value for the encoder
301 */
303 int32_t* status);
304
305/**
306 * Gets the user set distance per pulse of the encoder.
307 *
308 * @param[in] encoderHandle the encoder handle
309 * @param[out] status Error status variable. 0 on success.
310 * @return the set distance per pulse
311 */
313 int32_t* status);
314
315/**
316 * Gets the encoding type of the encoder.
317 *
318 * @param[in] encoderHandle the encoder handle
319 * @param[out] status Error status variable. 0 on success.
320 * @return the encoding type
321 */
323 HAL_EncoderHandle encoderHandle, int32_t* status);
324#ifdef __cplusplus
325} // extern "C"
326#endif
327/** @} */
HAL_AnalogTriggerType
The type of analog trigger to trigger on.
Definition AnalogTrigger.h:20
void HAL_SetEncoderSamplesToAverage(HAL_EncoderHandle encoderHandle, int32_t samplesToAverage, int32_t *status)
Sets the number of encoder samples to average when calculating encoder rate.
HAL_EncoderHandle HAL_InitializeEncoder(HAL_Handle digitalSourceHandleA, HAL_AnalogTriggerType analogTriggerTypeA, HAL_Handle digitalSourceHandleB, HAL_AnalogTriggerType analogTriggerTypeB, HAL_Bool reverseDirection, HAL_EncoderEncodingType encodingType, int32_t *status)
Initializes an encoder.
void HAL_SetEncoderDistancePerPulse(HAL_EncoderHandle encoderHandle, double distancePerPulse, int32_t *status)
Sets the distance traveled per encoder pulse.
void HAL_SetEncoderMinRate(HAL_EncoderHandle encoderHandle, double minRate, int32_t *status)
Sets the minimum rate to be considered moving by the encoder.
HAL_Bool HAL_GetEncoderStopped(HAL_EncoderHandle encoderHandle, int32_t *status)
Determines if the clock is stopped.
void HAL_SetEncoderMaxPeriod(HAL_EncoderHandle encoderHandle, double maxPeriod, int32_t *status)
Sets the maximum period where the device is still considered "moving".
int32_t HAL_GetEncoderRaw(HAL_EncoderHandle encoderHandle, int32_t *status)
Gets the raw counts of the encoder.
HAL_Bool HAL_GetEncoderDirection(HAL_EncoderHandle encoderHandle, int32_t *status)
Gets the last direction the encoder value changed.
int32_t HAL_GetEncoderSamplesToAverage(HAL_EncoderHandle encoderHandle, int32_t *status)
Gets the current samples to average value.
void HAL_SetEncoderReverseDirection(HAL_EncoderHandle encoderHandle, HAL_Bool reverseDirection, int32_t *status)
Sets if to reverse the direction of the encoder.
double HAL_GetEncoderDistance(HAL_EncoderHandle encoderHandle, int32_t *status)
Gets the current distance traveled by the encoder.
double HAL_GetEncoderDecodingScaleFactor(HAL_EncoderHandle encoderHandle, int32_t *status)
Gets the decoding scale factor of the encoder.
void HAL_ResetEncoder(HAL_EncoderHandle encoderHandle, int32_t *status)
Reads the current encoder value.
void HAL_SetEncoderSimDevice(HAL_EncoderHandle handle, HAL_SimDeviceHandle device)
Indicates the encoder is used by a simulated device.
int32_t HAL_GetEncoderFPGAIndex(HAL_EncoderHandle encoderHandle, int32_t *status)
Gets the FPGA index of the encoder.
HAL_EncoderIndexingType
The type of index pulse for the encoder.
Definition Encoder.h:21
int32_t HAL_GetEncoder(HAL_EncoderHandle encoderHandle, int32_t *status)
Gets the current counts of the encoder after encoding type scaling.
double HAL_GetEncoderDistancePerPulse(HAL_EncoderHandle encoderHandle, int32_t *status)
Gets the user set distance per pulse of the encoder.
HAL_EncoderEncodingType
The encoding scaling of the encoder.
Definition Encoder.h:31
double HAL_GetEncoderPeriod(HAL_EncoderHandle encoderHandle, int32_t *status)
Gets the Period of the most recent count.
void HAL_SetEncoderIndexSource(HAL_EncoderHandle encoderHandle, HAL_Handle digitalSourceHandle, HAL_AnalogTriggerType analogTriggerType, HAL_EncoderIndexingType type, int32_t *status)
Sets the source for an index pulse on the encoder.
HAL_EncoderEncodingType HAL_GetEncoderEncodingType(HAL_EncoderHandle encoderHandle, int32_t *status)
Gets the encoding type of the encoder.
int32_t HAL_GetEncoderEncodingScale(HAL_EncoderHandle encoderHandle, int32_t *status)
Gets the encoder scale value.
void HAL_FreeEncoder(HAL_EncoderHandle encoderHandle)
Frees an encoder.
double HAL_GetEncoderRate(HAL_EncoderHandle encoderHandle, int32_t *status)
Gets the current rate of the encoder.
@ HAL_kResetWhileLow
Definition Encoder.h:23
@ HAL_kResetWhileHigh
Definition Encoder.h:22
@ HAL_kResetOnRisingEdge
Definition Encoder.h:25
@ HAL_kResetOnFallingEdge
Definition Encoder.h:24
@ HAL_Encoder_k4X
Definition Encoder.h:34
@ HAL_Encoder_k2X
Definition Encoder.h:33
@ HAL_Encoder_k1X
Definition Encoder.h:32
int32_t HAL_Bool
Definition Types.h:73
int32_t HAL_Handle
Definition Types.h:17
HAL_Handle HAL_SimDeviceHandle
Definition Types.h:53
HAL_Handle HAL_EncoderHandle
Definition Types.h:35
#define HAL_ENUM(name)
Definition Types.h:76