WPILibC++ 2023.4.3-108-ge5452e3
Counter Functions

Enumerations

enum  HAL_Counter_Mode : int32_t { HAL_Counter_kTwoPulse = 0 , HAL_Counter_kSemiperiod = 1 , HAL_Counter_kPulseLength = 2 , HAL_Counter_kExternalDirection = 3 }
 The counter mode. More...
 

Functions

HAL_CounterHandle HAL_InitializeCounter (HAL_Counter_Mode mode, int32_t *index, int32_t *status)
 Initializes a counter. More...
 
void HAL_FreeCounter (HAL_CounterHandle counterHandle, int32_t *status)
 Frees a counter. More...
 
void HAL_SetCounterAverageSize (HAL_CounterHandle counterHandle, int32_t size, int32_t *status)
 Sets the average sample size of a counter. More...
 
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. More...
 
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. More...
 
void HAL_ClearCounterUpSource (HAL_CounterHandle counterHandle, int32_t *status)
 Disables the up counting source to the counter. More...
 
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. More...
 
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. More...
 
void HAL_ClearCounterDownSource (HAL_CounterHandle counterHandle, int32_t *status)
 Disables the down counting source to the counter. More...
 
void HAL_SetCounterUpDownMode (HAL_CounterHandle counterHandle, int32_t *status)
 Sets standard up / down counting mode on this counter. More...
 
void HAL_SetCounterExternalDirectionMode (HAL_CounterHandle counterHandle, int32_t *status)
 Sets directional counting mode on this counter. More...
 
void HAL_SetCounterSemiPeriodMode (HAL_CounterHandle counterHandle, HAL_Bool highSemiPeriod, int32_t *status)
 Sets Semi-period mode on this counter. More...
 
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. More...
 
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 calculating the period. More...
 
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 calculating the period. More...
 
void HAL_ResetCounter (HAL_CounterHandle counterHandle, int32_t *status)
 Resets the Counter to zero. More...
 
int32_t HAL_GetCounter (HAL_CounterHandle counterHandle, int32_t *status)
 Reads the current counter value. More...
 
double HAL_GetCounterPeriod (HAL_CounterHandle counterHandle, int32_t *status)
 Gets the Period of the most recent count. More...
 
void HAL_SetCounterMaxPeriod (HAL_CounterHandle counterHandle, double maxPeriod, int32_t *status)
 Sets the maximum period where the device is still considered "moving". More...
 
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 captured. More...
 
HAL_Bool HAL_GetCounterStopped (HAL_CounterHandle counterHandle, int32_t *status)
 Determines if the clock is stopped. More...
 
HAL_Bool HAL_GetCounterDirection (HAL_CounterHandle counterHandle, int32_t *status)
 Gets the last direction the counter value changed. More...
 
void HAL_SetCounterReverseDirection (HAL_CounterHandle counterHandle, HAL_Bool reverseDirection, int32_t *status)
 Sets the Counter to return reversed sensing on the direction. More...
 

Detailed Description

Enumeration Type Documentation

◆ HAL_Counter_Mode

enum HAL_Counter_Mode : int32_t

The counter mode.

Enumerator
HAL_Counter_kTwoPulse 
HAL_Counter_kSemiperiod 
HAL_Counter_kPulseLength 
HAL_Counter_kExternalDirection 

Function Documentation

◆ HAL_ClearCounterDownSource()

void HAL_ClearCounterDownSource ( HAL_CounterHandle  counterHandle,
int32_t *  status 
)

Disables the down counting source to the counter.

Parameters
[in]counterHandlethe counter handle
[out]statusError status variable. 0 on success.

◆ HAL_ClearCounterUpSource()

void HAL_ClearCounterUpSource ( HAL_CounterHandle  counterHandle,
int32_t *  status 
)

Disables the up counting source to the counter.

Parameters
[in]counterHandlethe counter handle
[out]statusError status variable. 0 on success.

◆ HAL_FreeCounter()

void HAL_FreeCounter ( HAL_CounterHandle  counterHandle,
int32_t *  status 
)

Frees a counter.

Parameters
[in]counterHandlethe counter handle
[out]statusError status variable. 0 on success.

◆ HAL_GetCounter()

int32_t HAL_GetCounter ( HAL_CounterHandle  counterHandle,
int32_t *  status 
)

Reads the current counter value.

Reads the value at this instant. It may still be running, so it reflects the current value. Next time it is read, it might have a different value.

Parameters
[in]counterHandlethe counter handle
[out]statusError status variable. 0 on success.
Returns
the current counter value

◆ HAL_GetCounterDirection()

HAL_Bool HAL_GetCounterDirection ( HAL_CounterHandle  counterHandle,
int32_t *  status 
)

Gets the last direction the counter value changed.

Parameters
[in]counterHandlethe counter handle
[out]statusError status variable. 0 on success.
Returns
the last direction the counter value changed

◆ HAL_GetCounterPeriod()

double HAL_GetCounterPeriod ( HAL_CounterHandle  counterHandle,
int32_t *  status 
)

Gets the Period of the most recent count.

Returns the time interval of the most recent count. This can be used for velocity calculations to determine shaft speed.

Parameters
[in]counterHandlethe counter handle
[out]statusError status variable. 0 on success.
Returns
the period of the last two pulses in units of seconds

◆ HAL_GetCounterSamplesToAverage()

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 calculating the period.

Perform averaging to account for mechanical imperfections or as oversampling to increase resolution.

Parameters
[in]counterHandlethe counter handle
[out]statusError status variable. 0 on success.
Returns
SamplesToAverage The number of samples being averaged (from 1 to 127)

◆ HAL_GetCounterStopped()

HAL_Bool HAL_GetCounterStopped ( HAL_CounterHandle  counterHandle,
int32_t *  status 
)

Determines if the clock is stopped.

Determine if the clocked input is stopped based on the MaxPeriod value set using the SetMaxPeriod method. If the clock exceeds the MaxPeriod, then the device (and counter) are assumed to be stopped and it returns true.

Parameters
[in]counterHandlethe counter handle
[out]statusError status variable. 0 on success.
Returns
true if the most recent counter period exceeds the MaxPeriod value set by SetMaxPeriod

◆ HAL_InitializeCounter()

HAL_CounterHandle HAL_InitializeCounter ( HAL_Counter_Mode  mode,
int32_t *  index,
int32_t *  status 
)

Initializes a counter.

Parameters
[in]modethe counter mode
[in]indexthe compressor index (output)
[out]statusError status variable. 0 on success.
Returns
the created handle

◆ HAL_ResetCounter()

void HAL_ResetCounter ( HAL_CounterHandle  counterHandle,
int32_t *  status 
)

Resets the Counter to zero.

Sets the counter value to zero. This does not effect the running state of the counter, just sets the current value to zero.

Parameters
[in]counterHandlethe counter handle
[out]statusError status variable. 0 on success.

◆ HAL_SetCounterAverageSize()

void HAL_SetCounterAverageSize ( HAL_CounterHandle  counterHandle,
int32_t  size,
int32_t *  status 
)

Sets the average sample size of a counter.

Parameters
[in]counterHandlethe counter handle
[in]sizethe size of samples to average
[out]statusError status variable. 0 on success.

◆ HAL_SetCounterDownSource()

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.

Parameters
[in]counterHandlethe counter handle
[in]digitalSourceHandlethe digital source handle (either a HAL_AnalogTriggerHandle or a HAL_DigitalHandle)
[in]analogTriggerTypethe analog trigger type if the source is an analog trigger
[out]statusError status variable. 0 on success.

◆ HAL_SetCounterDownSourceEdge()

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.

Note that both are allowed to be set true at the same time without issues.

Parameters
[in]counterHandlethe counter handle
[in]risingEdgetrue to trigger on rising
[in]fallingEdgetrue to trigger on falling
[out]statusError status variable. 0 on success.

◆ HAL_SetCounterExternalDirectionMode()

void HAL_SetCounterExternalDirectionMode ( HAL_CounterHandle  counterHandle,
int32_t *  status 
)

Sets directional counting mode on this counter.

The direction is determined by the B input, with counting happening with the A input.

Parameters
[in]counterHandlethe counter handle
[out]statusError status variable. 0 on success.

◆ HAL_SetCounterMaxPeriod()

void HAL_SetCounterMaxPeriod ( HAL_CounterHandle  counterHandle,
double  maxPeriod,
int32_t *  status 
)

Sets the maximum period where the device is still considered "moving".

Sets the maximum period where the device is considered moving. This value is used to determine the "stopped" state of the counter using the HAL_GetCounterStopped method.

Parameters
[in]counterHandlethe counter handle
[in]maxPeriodthe maximum period where the counted device is considered moving in seconds
[out]statusError status variable. 0 on success.

◆ HAL_SetCounterPulseLengthMode()

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.

This mode is most useful for direction sensitive gear tooth sensors.

Parameters
[in]counterHandlethe counter handle
[in]thresholdThe pulse length beyond which the counter counts the opposite direction (seconds)
[out]statusError status variable. 0 on success.

◆ HAL_SetCounterReverseDirection()

void HAL_SetCounterReverseDirection ( HAL_CounterHandle  counterHandle,
HAL_Bool  reverseDirection,
int32_t *  status 
)

Sets the Counter to return reversed sensing on the direction.

This allows counters to change the direction they are counting in the case of 1X and 2X quadrature encoding only. Any other counter mode isn't supported.

Parameters
[in]counterHandlethe counter handle
[in]reverseDirectiontrue if the value counted should be negated.
[out]statusError status variable. 0 on success.

◆ HAL_SetCounterSamplesToAverage()

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 calculating the period.

Perform averaging to account for mechanical imperfections or as oversampling to increase resolution.

Parameters
[in]counterHandlethe counter handle
[in]samplesToAverageThe number of samples to average from 1 to 127
[out]statusError status variable. 0 on success.

◆ HAL_SetCounterSemiPeriodMode()

void HAL_SetCounterSemiPeriodMode ( HAL_CounterHandle  counterHandle,
HAL_Bool  highSemiPeriod,
int32_t *  status 
)

Sets Semi-period mode on this counter.

The counter counts up based on the time the input is triggered. High or Low depends on the highSemiPeriod parameter.

Parameters
[in]counterHandlethe counter handle
[in]highSemiPeriodtrue for counting when the input is high, false for low
[out]statusError status variable. 0 on success.

◆ HAL_SetCounterUpdateWhenEmpty()

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 captured.

The output of the event timer has a buffer of periods that are averaged and posted to a register on the FPGA. When the timer detects that the event source has stopped (based on the MaxPeriod) the buffer of samples to be averaged is emptied.

If you enable the update when empty, you will be notified of the stopped source and the event time will report 0 samples.

If you disable update when empty, the most recent average will remain on the output until a new sample is acquired.

You will never see 0 samples output (except when there have been no events since an FPGA reset) and you will likely not see the stopped bit become true (since it is updated at the end of an average and there are no samples to average).

Parameters
[in]counterHandlethe counter handle
[in]enabledtrue to enable counter updating with no samples
[out]statusError status variable. 0 on success.

◆ HAL_SetCounterUpDownMode()

void HAL_SetCounterUpDownMode ( HAL_CounterHandle  counterHandle,
int32_t *  status 
)

Sets standard up / down counting mode on this counter.

Up and down counts are sourced independently from two inputs.

Parameters
[in]counterHandlethe counter handle
[out]statusError status variable. 0 on success.

◆ HAL_SetCounterUpSource()

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.

Parameters
[in]counterHandlethe counter handle
[in]digitalSourceHandlethe digital source handle (either a HAL_AnalogTriggerHandle or a HAL_DigitalHandle)
[in]analogTriggerTypethe analog trigger type if the source is an analog trigger
[out]statusError status variable. 0 on success.

◆ HAL_SetCounterUpSourceEdge()

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.

Note that both are allowed to be set true at the same time without issues.

Parameters
[in]counterHandlethe counter handle
[in]risingEdgetrue to trigger on rising
[in]fallingEdgetrue to trigger on falling
[out]statusError status variable. 0 on success.