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