WPILibC++ 2024.3.2
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 */
26};
27
28/**
29 * The encoding scaling of the encoder.
30 */
35};
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 * @param[out] status Error status variable. 0 on success.
69 */
70void HAL_FreeEncoder(HAL_EncoderHandle encoderHandle, int32_t* status);
71
72/**
73 * Indicates the encoder is used by a simulated device.
74 *
75 * @param handle the encoder handle
76 * @param device simulated device handle
77 */
79 HAL_SimDeviceHandle device);
80
81/**
82 * Gets the current counts of the encoder after encoding type scaling.
83 *
84 * This is scaled by the value passed during initialization to encodingType.
85 *
86 * @param[in] encoderHandle the encoder handle
87 * @param[out] status Error status variable. 0 on success.
88 * @return the current scaled count
89 */
90int32_t HAL_GetEncoder(HAL_EncoderHandle encoderHandle, int32_t* status);
91
92/**
93 * Gets the raw counts of the encoder.
94 *
95 * This is not scaled by any values.
96 *
97 * @param[in] encoderHandle the encoder handle
98 * @param[out] status Error status variable. 0 on success.
99 * @return the raw encoder count
100 */
101int32_t HAL_GetEncoderRaw(HAL_EncoderHandle encoderHandle, int32_t* status);
102
103/**
104 * Gets the encoder scale value.
105 *
106 * This is set by the value passed during initialization to encodingType.
107 *
108 * @param[in] encoderHandle the encoder handle
109 * @param[out] status Error status variable. 0 on success.
110 * @return the encoder scale value
111 */
113 int32_t* status);
114
115/**
116 * Reads the current encoder value.
117 *
118 * Read the value at this instant. It may still be running, so it reflects the
119 * current value. Next time it is read, it might have a different value.
120 *
121 * @param[in] encoderHandle the encoder handle
122 * @param[out] status Error status variable. 0 on success.
123 */
124void HAL_ResetEncoder(HAL_EncoderHandle encoderHandle, int32_t* status);
125
126/**
127 * Gets the Period of the most recent count.
128 *
129 * Returns the time interval of the most recent count. This can be used for
130 * velocity calculations to determine shaft speed.
131 *
132 * @param[in] encoderHandle the encoder handle
133 * @param[out] status Error status variable. 0 on success.
134 * @returns the period of the last two pulses in units of seconds
135 */
136double HAL_GetEncoderPeriod(HAL_EncoderHandle encoderHandle, int32_t* status);
137
138/**
139 * Sets the maximum period where the device is still considered "moving".
140 *
141 * Sets the maximum period where the device is considered moving. This value is
142 * used to determine the "stopped" state of the encoder using the
143 * HAL_GetEncoderStopped method.
144 *
145 * @param[in] encoderHandle the encoder handle
146 * @param[in] maxPeriod the maximum period where the counted device is
147 * considered moving in seconds
148 * @param[out] status Error status variable. 0 on success.
149 */
150void HAL_SetEncoderMaxPeriod(HAL_EncoderHandle encoderHandle, double maxPeriod,
151 int32_t* status);
152
153/**
154 * Determines if the clock is stopped.
155 *
156 * Determines if the clocked input is stopped based on the MaxPeriod value set
157 * using the SetMaxPeriod method. If the clock exceeds the MaxPeriod, then the
158 * device (and encoder) are assumed to be stopped and it returns true.
159 *
160 * @param[in] encoderHandle the encoder handle
161 * @param[out] status Error status variable. 0 on success.
162 * @return true if the most recent encoder period exceeds the MaxPeriod value
163 * set by SetMaxPeriod
164 */
166 int32_t* status);
167
168/**
169 * Gets the last direction the encoder value changed.
170 *
171 * @param[in] encoderHandle the encoder handle
172 * @param[out] status Error status variable. 0 on success.
173 * @return the last direction the encoder value changed
174 */
176 int32_t* status);
177
178/**
179 * Gets the current distance traveled by the encoder.
180 *
181 * This is the encoder count scaled by the distance per pulse set for the
182 * encoder.
183 *
184 * @param[in] encoderHandle the encoder handle
185 * @param[out] status Error status variable. 0 on success.
186 * @return the encoder distance (units are determined by the units
187 * passed to HAL_SetEncoderDistancePerPulse)
188 */
189double HAL_GetEncoderDistance(HAL_EncoderHandle encoderHandle, int32_t* status);
190
191/**
192 * Gets the current rate of the encoder.
193 *
194 * This is the encoder period scaled by the distance per pulse set for the
195 * encoder.
196 *
197 * @param[in] encoderHandle the encoder handle
198 * @param[out] status Error status variable. 0 on success.
199 * @return the encoder rate (units are determined by the units passed to
200 * HAL_SetEncoderDistancePerPulse, time value is seconds)
201 */
202double HAL_GetEncoderRate(HAL_EncoderHandle encoderHandle, int32_t* status);
203
204/**
205 * Sets the minimum rate to be considered moving by the encoder.
206 *
207 * Units need to match what is set by HAL_SetEncoderDistancePerPulse, with time
208 * as seconds.
209 *
210 * @param[in] encoderHandle the encoder handle
211 * @param[in] minRate the minimum rate to be considered moving (units are
212 * determined by the units passed to
213 * HAL_SetEncoderDistancePerPulse, time value is
214 * seconds)
215 * @param[out] status Error status variable. 0 on success.
216 */
217void HAL_SetEncoderMinRate(HAL_EncoderHandle encoderHandle, double minRate,
218 int32_t* status);
219
220/**
221 * Sets the distance traveled per encoder pulse. This is used as a scaling
222 * factor for the rate and distance calls.
223 *
224 * @param[in] encoderHandle the encoder handle
225 * @param[in] distancePerPulse the distance traveled per encoder pulse (units
226 * user defined)
227 * @param[out] status Error status variable. 0 on success.
228 */
230 double distancePerPulse, int32_t* status);
231
232/**
233 * Sets if to reverse the direction of the encoder.
234 *
235 * Note that this is not a toggle. It is an absolute set.
236 *
237 * @param[in] encoderHandle the encoder handle
238 * @param[in] reverseDirection true to reverse the direction, false to not.
239 * @param[out] status Error status variable. 0 on success.
240 */
242 HAL_Bool reverseDirection, int32_t* status);
243
244/**
245 * Sets the number of encoder samples to average when calculating encoder rate.
246 *
247 * @param[in] encoderHandle the encoder handle
248 * @param[in] samplesToAverage the number of samples to average
249 * @param[out] status Error status variable. 0 on success.
250 */
252 int32_t samplesToAverage, int32_t* status);
253
254/**
255 * Gets the current samples to average value.
256 *
257 * @param[in] encoderHandle the encoder handle
258 * @param[out] status Error status variable. 0 on success.
259 * @return the current samples to average value
260 */
262 int32_t* status);
263
264/**
265 * Sets the source for an index pulse on the encoder.
266 *
267 * The index pulse can be used to cause an encoder to reset based on an external
268 * input.
269 *
270 * @param[in] encoderHandle the encoder handle
271 * @param[in] digitalSourceHandle the index source handle (either a
272 * HAL_AnalogTriggerHandle or a
273 * HAL_DigitalHandle)
274 * @param[in] analogTriggerType the analog trigger type if the source is an
275 * analog trigger
276 * @param[in] type the index triggering type
277 * @param[out] status Error status variable. 0 on success.
278 */
280 HAL_Handle digitalSourceHandle,
281 HAL_AnalogTriggerType analogTriggerType,
282 HAL_EncoderIndexingType type, int32_t* status);
283
284/**
285 * Gets the FPGA index of the encoder.
286 *
287 * @param[in] encoderHandle the encoder handle
288 * @param[out] status Error status variable. 0 on success.
289 * @return the FPGA index of the encoder
290 */
292 int32_t* status);
293
294/**
295 * Gets the decoding scale factor of the encoder.
296 *
297 * This is used to perform the scaling from raw to type scaled values.
298 *
299 * @param[in] encoderHandle the encoder handle
300 * @param[out] status Error status variable. 0 on success.
301 * @return the scale value for the encoder
302 */
304 int32_t* status);
305
306/**
307 * Gets the user set distance per pulse of the encoder.
308 *
309 * @param[in] encoderHandle the encoder handle
310 * @param[out] status Error status variable. 0 on success.
311 * @return the set distance per pulse
312 */
314 int32_t* status);
315
316/**
317 * Gets the encoding type of the encoder.
318 *
319 * @param[in] encoderHandle the encoder handle
320 * @param[out] status Error status variable. 0 on success.
321 * @return the encoding type
322 */
324 HAL_EncoderHandle encoderHandle, int32_t* status);
325#ifdef __cplusplus
326} // extern "C"
327#endif
328/** @} */
@ HAL_ENUM
Definition: Value.h:14
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_FreeEncoder(HAL_EncoderHandle encoderHandle, int32_t *status)
Frees an encoder.
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.
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
type
Definition: core.h:556