WPILibC++ 2025.2.1
Loading...
Searching...
No Matches
Counter.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_counter Counter Functions
14 * @ingroup hal_capi
15 * @{
16 */
17
18/**
19 * The counter mode.
20 */
22 /** Two pulse mode. */
24 /** Semi-period mode. */
26 /** Pulse length mode. */
28 /** External direction mode. */
30};
31
32#ifdef __cplusplus
33extern "C" {
34#endif
35
36/**
37 * Initializes a counter.
38 *
39 * @param[in] mode the counter mode
40 * @param[in] index the compressor index (output)
41 * @param[out] status Error status variable. 0 on success.
42 * @return the created handle
43 */
45 int32_t* status);
46
47/**
48 * Frees a counter.
49 *
50 * @param[in] counterHandle the counter handle
51 */
53
54/**
55 * Sets the average sample size of a counter.
56 *
57 * @param[in] counterHandle the counter handle
58 * @param[in] size the size of samples to average
59 * @param[out] status Error status variable. 0 on success.
60 */
61void HAL_SetCounterAverageSize(HAL_CounterHandle counterHandle, int32_t size,
62 int32_t* status);
63
64/**
65 * Sets the source object that causes the counter to count up.
66 *
67 * @param[in] counterHandle the counter handle
68 * @param[in] digitalSourceHandle the digital source handle (either a
69 * HAL_AnalogTriggerHandle or a
70 * HAL_DigitalHandle)
71 * @param[in] analogTriggerType the analog trigger type if the source is an
72 * analog trigger
73 * @param[out] status Error status variable. 0 on success.
74 */
76 HAL_Handle digitalSourceHandle,
77 HAL_AnalogTriggerType analogTriggerType,
78 int32_t* status);
79
80/**
81 * Sets the up source to either detect rising edges or falling edges.
82 *
83 * Note that both are allowed to be set true at the same time without issues.
84 *
85 * @param[in] counterHandle the counter handle
86 * @param[in] risingEdge true to trigger on rising
87 * @param[in] fallingEdge true to trigger on falling
88 * @param[out] status Error status variable. 0 on success.
89 */
91 HAL_Bool risingEdge, HAL_Bool fallingEdge,
92 int32_t* status);
93
94/**
95 * Disables the up counting source to the counter.
96 *
97 * @param[in] counterHandle the counter handle
98 * @param[out] status Error status variable. 0 on success.
99 */
100void HAL_ClearCounterUpSource(HAL_CounterHandle counterHandle, int32_t* status);
101
102/**
103 * Sets the source object that causes the counter to count down.
104 *
105 * @param[in] counterHandle the counter handle
106 * @param[in] digitalSourceHandle the digital source handle (either a
107 * HAL_AnalogTriggerHandle or a
108 * HAL_DigitalHandle)
109 * @param[in] analogTriggerType the analog trigger type if the source is an
110 * analog trigger
111 * @param[out] status Error status variable. 0 on success.
112 */
114 HAL_Handle digitalSourceHandle,
115 HAL_AnalogTriggerType analogTriggerType,
116 int32_t* status);
117
118/**
119 * Sets the down source to either detect rising edges or falling edges.
120 * Note that both are allowed to be set true at the same time without issues.
121 *
122 * @param[in] counterHandle the counter handle
123 * @param[in] risingEdge true to trigger on rising
124 * @param[in] fallingEdge true to trigger on falling
125 * @param[out] status Error status variable. 0 on success.
126 */
128 HAL_Bool risingEdge, HAL_Bool fallingEdge,
129 int32_t* status);
130
131/**
132 * Disables the down counting source to the counter.
133 *
134 * @param[in] counterHandle the counter handle
135 * @param[out] status Error status variable. 0 on success.
136 */
138 int32_t* status);
139
140/**
141 * Sets standard up / down counting mode on this counter.
142 *
143 * Up and down counts are sourced independently from two inputs.
144 *
145 * @param[in] counterHandle the counter handle
146 * @param[out] status Error status variable. 0 on success.
147 */
148void HAL_SetCounterUpDownMode(HAL_CounterHandle counterHandle, int32_t* status);
149
150/**
151 * Sets directional counting mode on this counter.
152 *
153 * The direction is determined by the B input, with counting happening with the
154 * A input.
155 *
156 * @param[in] counterHandle the counter handle
157 * @param[out] status Error status variable. 0 on success.
158 */
160 int32_t* status);
161
162/**
163 * Sets Semi-period mode on this counter.
164 *
165 * The counter counts up based on the time the input is triggered. High or Low
166 * depends on the highSemiPeriod parameter.
167 *
168 * @param[in] counterHandle the counter handle
169 * @param[in] highSemiPeriod true for counting when the input is high, false for
170 * low
171 * @param[out] status Error status variable. 0 on success.
172 */
174 HAL_Bool highSemiPeriod, int32_t* status);
175
176/**
177 * Configures the counter to count in up or down based on the length of the
178 * input pulse.
179 *
180 * This mode is most useful for direction sensitive gear tooth sensors.
181 *
182 * @param[in] counterHandle the counter handle
183 * @param[in] threshold The pulse length beyond which the counter counts the
184 * opposite direction (seconds)
185 * @param[out] status Error status variable. 0 on success.
186 */
188 double threshold, int32_t* status);
189
190/**
191 * Gets the Samples to Average which specifies the number of samples of the
192 * timer to average when calculating the period. Perform averaging to account
193 * for mechanical imperfections or as oversampling to increase resolution.
194 *
195 * @param[in] counterHandle the counter handle
196 * @param[out] status Error status variable. 0 on success.
197 * @return SamplesToAverage The number of samples being averaged (from 1 to 127)
198 */
200 int32_t* status);
201
202/**
203 * Sets the Samples to Average which specifies the number of samples of the
204 * timer to average when calculating the period. Perform averaging to account
205 * for mechanical imperfections or as oversampling to increase resolution.
206 *
207 * @param[in] counterHandle the counter handle
208 * @param[in] samplesToAverage The number of samples to average from 1 to 127
209 * @param[out] status Error status variable. 0 on success.
210 */
212 int32_t samplesToAverage, int32_t* status);
213
214/**
215 * Resets the Counter to zero.
216 *
217 * Sets the counter value to zero. This does not effect the running state of the
218 * counter, just sets the current value to zero.
219 *
220 * @param[in] counterHandle the counter handle
221 * @param[out] status Error status variable. 0 on success.
222 */
223void HAL_ResetCounter(HAL_CounterHandle counterHandle, int32_t* status);
224
225/**
226 * Reads the current counter value.
227 *
228 * Reads the value at this instant. It may still be running, so it reflects the
229 * current value. Next time it is read, it might have a different value.
230 *
231 * @param[in] counterHandle the counter handle
232 * @param[out] status Error status variable. 0 on success.
233 * @return the current counter value
234 */
235int32_t HAL_GetCounter(HAL_CounterHandle counterHandle, int32_t* status);
236
237/**
238 * Gets the Period of the most recent count.
239 *
240 * Returns the time interval of the most recent count. This can be used for
241 * velocity calculations to determine shaft speed.
242 *
243 * @param[in] counterHandle the counter handle
244 * @param[out] status Error status variable. 0 on success.
245 * @return the period of the last two pulses in units of seconds
246 */
247double HAL_GetCounterPeriod(HAL_CounterHandle counterHandle, int32_t* status);
248
249/**
250 * Sets the maximum period where the device is still considered "moving".
251 *
252 * Sets the maximum period where the device is considered moving. This value is
253 * used to determine the "stopped" state of the counter using the
254 * HAL_GetCounterStopped method.
255 *
256 * @param[in] counterHandle the counter handle
257 * @param[in] maxPeriod the maximum period where the counted device is
258 * considered moving in seconds
259 * @param[out] status Error status variable. 0 on success.
260 */
261void HAL_SetCounterMaxPeriod(HAL_CounterHandle counterHandle, double maxPeriod,
262 int32_t* status);
263
264/**
265 * Selects whether you want to continue updating the event timer output when
266 * there are no samples captured.
267 *
268 * The output of the event timer has a buffer of periods that are averaged and
269 * posted to a register on the FPGA. When the timer detects that the event
270 * source has stopped (based on the MaxPeriod) the buffer of samples to be
271 * averaged is emptied.
272 *
273 * If you enable the update when empty, you will be
274 * notified of the stopped source and the event time will report 0 samples.
275 *
276 * If you disable update when empty, the most recent average will remain on the
277 * output until a new sample is acquired.
278 *
279 * You will never see 0 samples output (except when there have been no events
280 * since an FPGA reset) and you will likely not see the stopped bit become true
281 * (since it is updated at the end of an average and there are no samples to
282 * average).
283 *
284 * @param[in] counterHandle the counter handle
285 * @param[in] enabled true to enable counter updating with no samples
286 * @param[out] status Error status variable. 0 on success.
287 */
289 HAL_Bool enabled, int32_t* status);
290
291/**
292 * Determines if the clock is stopped.
293 *
294 * Determine if the clocked input is stopped based on the MaxPeriod value set
295 * using the SetMaxPeriod method. If the clock exceeds the MaxPeriod, then the
296 * device (and counter) are assumed to be stopped and it returns true.
297 *
298 * @param[in] counterHandle the counter handle
299 * @param[out] status Error status variable. 0 on success.
300 * @return true if the most recent counter period exceeds the MaxPeriod value
301 * set by SetMaxPeriod
302 */
304 int32_t* status);
305
306/**
307 * Gets the last direction the counter value changed.
308 *
309 * @param[in] counterHandle the counter handle
310 * @param[out] status Error status variable. 0 on success.
311 * @return the last direction the counter value changed
312 */
314 int32_t* status);
315
316/**
317 * Sets the Counter to return reversed sensing on the direction.
318 *
319 * This allows counters to change the direction they are counting in the case of
320 * 1X and 2X quadrature encoding only. Any other counter mode isn't supported.
321 *
322 * @param[in] counterHandle the counter handle
323 * @param[in] reverseDirection true if the value counted should be negated.
324 * @param[out] status Error status variable. 0 on success.
325 */
327 HAL_Bool reverseDirection, int32_t* status);
328#ifdef __cplusplus
329} // extern "C"
330#endif
331/** @} */
HAL_AnalogTriggerType
The type of analog trigger to trigger on.
Definition AnalogTrigger.h:20
void HAL_SetCounterUpDownMode(HAL_CounterHandle counterHandle, int32_t *status)
Sets standard up / down counting mode on this counter.
void HAL_SetCounterAverageSize(HAL_CounterHandle counterHandle, int32_t size, int32_t *status)
Sets the average sample size of a counter.
HAL_Counter_Mode
The counter mode.
Definition Counter.h:21
void HAL_SetCounterUpSourceEdge(HAL_CounterHandle counterHandle, HAL_Bool risingEdge, HAL_Bool fallingEdge, int32_t *status)
Sets the up source to either detect rising edges or falling edges.
void HAL_SetCounterReverseDirection(HAL_CounterHandle counterHandle, HAL_Bool reverseDirection, int32_t *status)
Sets the Counter to return reversed sensing on the direction.
void HAL_SetCounterExternalDirectionMode(HAL_CounterHandle counterHandle, int32_t *status)
Sets directional counting mode on this counter.
void HAL_ClearCounterUpSource(HAL_CounterHandle counterHandle, int32_t *status)
Disables the up counting source to the counter.
void HAL_FreeCounter(HAL_CounterHandle counterHandle)
Frees a counter.
void HAL_SetCounterSemiPeriodMode(HAL_CounterHandle counterHandle, HAL_Bool highSemiPeriod, int32_t *status)
Sets Semi-period mode on this counter.
HAL_CounterHandle HAL_InitializeCounter(HAL_Counter_Mode mode, int32_t *index, int32_t *status)
Initializes a counter.
HAL_Bool HAL_GetCounterStopped(HAL_CounterHandle counterHandle, int32_t *status)
Determines if the clock is stopped.
void HAL_SetCounterUpdateWhenEmpty(HAL_CounterHandle counterHandle, HAL_Bool enabled, int32_t *status)
Selects whether you want to continue updating the event timer output when there are no samples captur...
void HAL_ResetCounter(HAL_CounterHandle counterHandle, int32_t *status)
Resets the Counter to zero.
int32_t HAL_GetCounterSamplesToAverage(HAL_CounterHandle counterHandle, int32_t *status)
Gets the Samples to Average which specifies the number of samples of the timer to average when calcul...
void HAL_SetCounterDownSourceEdge(HAL_CounterHandle counterHandle, HAL_Bool risingEdge, HAL_Bool fallingEdge, int32_t *status)
Sets the down source to either detect rising edges or falling edges.
void HAL_SetCounterDownSource(HAL_CounterHandle counterHandle, HAL_Handle digitalSourceHandle, HAL_AnalogTriggerType analogTriggerType, int32_t *status)
Sets the source object that causes the counter to count down.
void HAL_SetCounterMaxPeriod(HAL_CounterHandle counterHandle, double maxPeriod, int32_t *status)
Sets the maximum period where the device is still considered "moving".
void HAL_SetCounterPulseLengthMode(HAL_CounterHandle counterHandle, double threshold, int32_t *status)
Configures the counter to count in up or down based on the length of the input pulse.
void HAL_SetCounterUpSource(HAL_CounterHandle counterHandle, HAL_Handle digitalSourceHandle, HAL_AnalogTriggerType analogTriggerType, int32_t *status)
Sets the source object that causes the counter to count up.
double HAL_GetCounterPeriod(HAL_CounterHandle counterHandle, int32_t *status)
Gets the Period of the most recent count.
void HAL_SetCounterSamplesToAverage(HAL_CounterHandle counterHandle, int32_t samplesToAverage, int32_t *status)
Sets the Samples to Average which specifies the number of samples of the timer to average when calcul...
int32_t HAL_GetCounter(HAL_CounterHandle counterHandle, int32_t *status)
Reads the current counter value.
void HAL_ClearCounterDownSource(HAL_CounterHandle counterHandle, int32_t *status)
Disables the down counting source to the counter.
HAL_Bool HAL_GetCounterDirection(HAL_CounterHandle counterHandle, int32_t *status)
Gets the last direction the counter value changed.
@ HAL_Counter_kExternalDirection
External direction mode.
Definition Counter.h:29
@ HAL_Counter_kTwoPulse
Two pulse mode.
Definition Counter.h:23
@ HAL_Counter_kSemiperiod
Semi-period mode.
Definition Counter.h:25
@ HAL_Counter_kPulseLength
Pulse length mode.
Definition Counter.h:27
int32_t HAL_Bool
Definition Types.h:73
int32_t HAL_Handle
Definition Types.h:17
HAL_Handle HAL_CounterHandle
Definition Types.h:29
#define HAL_ENUM(name)
Definition Types.h:76