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