WPILibC++ 2027.0.0-alpha-2
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/Types.h"
10
11/**
12 * @defgroup hal_counter Counter Functions
13 * @ingroup hal_capi
14 * @{
15 */
16
17#ifdef __cplusplus
18extern "C" {
19#endif
20
21/**
22 * Initializes a counter.
23 *
24 * @param[in] channel the dio channel
25 * @param[in] risingEdge true to count on rising edge, false for
26 * falling
27 * @param[in] allocationLocation the location where the allocation is
28 * occurring (can be null)
29 * @param[out] status Error status variable. 0 on success.
30 * @return the created handle
31 */
33 const char* allocationLocation,
34 int32_t* status);
35
36/**
37 * Frees a counter.
38 *
39 * @param[in] counterHandle the counter handle
40 */
42
43/**
44 * Sets the up source to either detect rising edges or falling edges.
45 *
46 * Note that both are allowed to be set true at the same time without issues.
47 *
48 * @param[in] counterHandle the counter handle
49 * @param[in] risingEdge true to trigger on rising
50 * @param[out] status Error status variable. 0 on success.
51 */
53 HAL_Bool risingEdge, int32_t* status);
54
55/**
56 * Resets the Counter to zero.
57 *
58 * Sets the counter value to zero. This does not effect the running state of the
59 * counter, just sets the current value to zero.
60 *
61 * @param[in] counterHandle the counter handle
62 * @param[out] status Error status variable. 0 on success.
63 */
64void HAL_ResetCounter(HAL_CounterHandle counterHandle, int32_t* status);
65
66/**
67 * Reads the current counter value.
68 *
69 * Reads the value at this instant. It may still be running, so it reflects the
70 * current value. Next time it is read, it might have a different value.
71 *
72 * @param[in] counterHandle the counter handle
73 * @param[out] status Error status variable. 0 on success.
74 * @return the current counter value
75 */
76int32_t HAL_GetCounter(HAL_CounterHandle counterHandle, int32_t* status);
77
78/**
79 * Gets the Period of the most recent count.
80 *
81 * Returns the time interval of the most recent count. This can be used for
82 * velocity calculations to determine shaft speed.
83 *
84 * @param[in] counterHandle the counter handle
85 * @param[out] status Error status variable. 0 on success.
86 * @return the period of the last two pulses in units of seconds
87 */
88double HAL_GetCounterPeriod(HAL_CounterHandle counterHandle, int32_t* status);
89
90/**
91 * Sets the maximum period where the device is still considered "moving".
92 *
93 * Sets the maximum period where the device is considered moving. This value is
94 * used to determine the "stopped" state of the counter using the
95 * HAL_GetCounterStopped method.
96 *
97 * @param[in] counterHandle the counter handle
98 * @param[in] maxPeriod the maximum period where the counted device is
99 * considered moving in seconds
100 * @param[out] status Error status variable. 0 on success.
101 */
102void HAL_SetCounterMaxPeriod(HAL_CounterHandle counterHandle, double maxPeriod,
103 int32_t* status);
104
105/**
106 * Determines if the clock is stopped.
107 *
108 * Determine if the clocked input is stopped based on the MaxPeriod value set
109 * using the SetMaxPeriod method. If the clock exceeds the MaxPeriod, then the
110 * device (and counter) are assumed to be stopped and it returns true.
111 *
112 * @param[in] counterHandle the counter handle
113 * @param[out] status Error status variable. 0 on success.
114 * @return true if the most recent counter period exceeds the MaxPeriod value
115 * set by SetMaxPeriod
116 */
118 int32_t* status);
119#ifdef __cplusplus
120} // extern "C"
121#endif
122/** @} */
void HAL_SetCounterEdgeConfiguration(HAL_CounterHandle counterHandle, HAL_Bool risingEdge, int32_t *status)
Sets the up source to either detect rising edges or falling edges.
void HAL_FreeCounter(HAL_CounterHandle counterHandle)
Frees a counter.
HAL_Bool HAL_GetCounterStopped(HAL_CounterHandle counterHandle, int32_t *status)
Determines if the clock is stopped.
void HAL_ResetCounter(HAL_CounterHandle counterHandle, int32_t *status)
Resets the Counter to zero.
void HAL_SetCounterMaxPeriod(HAL_CounterHandle counterHandle, double maxPeriod, int32_t *status)
Sets the maximum period where the device is still considered "moving".
HAL_CounterHandle HAL_InitializeCounter(int channel, HAL_Bool risingEdge, const char *allocationLocation, int32_t *status)
Initializes a counter.
double HAL_GetCounterPeriod(HAL_CounterHandle counterHandle, int32_t *status)
Gets the Period of the most recent count.
int32_t HAL_GetCounter(HAL_CounterHandle counterHandle, int32_t *status)
Reads the current counter value.
int32_t HAL_Bool
Definition Types.h:73
HAL_Handle HAL_CounterHandle
Definition Types.h:27