WPILibC++ 2023.4.3
frc::Counter Class Reference

Class for counting the number of ticks on a digital input channel. More...

#include <frc/Counter.h>

Inheritance diagram for frc::Counter:
frc::CounterBase wpi::Sendable wpi::SendableHelper< Counter >

Public Types

enum  Mode { kTwoPulse = 0 , kSemiperiod = 1 , kPulseLength = 2 , kExternalDirection = 3 }
 
- Public Types inherited from frc::CounterBase
enum  EncodingType { k1X , k2X , k4X }
 

Public Member Functions

 Counter (Mode mode=kTwoPulse)
 Create an instance of a counter where no sources are selected. More...
 
 Counter (int channel)
 Create an instance of a Counter object. More...
 
 Counter (DigitalSource *source)
 Create an instance of a counter from a Digital Source (such as a Digital Input). More...
 
 Counter (std::shared_ptr< DigitalSource > source)
 Create an instance of a counter from a Digital Source (such as a Digital Input). More...
 
 Counter (const AnalogTrigger &trigger)
 Create an instance of a Counter object. More...
 
 Counter (EncodingType encodingType, DigitalSource *upSource, DigitalSource *downSource, bool inverted)
 Create an instance of a Counter object. More...
 
 Counter (EncodingType encodingType, std::shared_ptr< DigitalSource > upSource, std::shared_ptr< DigitalSource > downSource, bool inverted)
 Create an instance of a Counter object. More...
 
 ~Counter () override
 
 Counter (Counter &&)=default
 
Counteroperator= (Counter &&)=default
 
void SetUpSource (int channel)
 Set the upsource for the counter as a digital input channel. More...
 
void SetUpSource (AnalogTrigger *analogTrigger, AnalogTriggerType triggerType)
 Set the up counting source to be an analog trigger. More...
 
void SetUpSource (std::shared_ptr< AnalogTrigger > analogTrigger, AnalogTriggerType triggerType)
 Set the up counting source to be an analog trigger. More...
 
void SetUpSource (DigitalSource *source)
 
void SetUpSource (std::shared_ptr< DigitalSource > source)
 Set the source object that causes the counter to count up. More...
 
void SetUpSource (DigitalSource &source)
 Set the source object that causes the counter to count up. More...
 
void SetUpSourceEdge (bool risingEdge, bool fallingEdge)
 Set the edge sensitivity on an up counting source. More...
 
void ClearUpSource ()
 Disable the up counting source to the counter. More...
 
void SetDownSource (int channel)
 Set the down counting source to be a digital input channel. More...
 
void SetDownSource (AnalogTrigger *analogTrigger, AnalogTriggerType triggerType)
 Set the down counting source to be an analog trigger. More...
 
void SetDownSource (std::shared_ptr< AnalogTrigger > analogTrigger, AnalogTriggerType triggerType)
 Set the down counting source to be an analog trigger. More...
 
void SetDownSource (DigitalSource *source)
 Set the source object that causes the counter to count down. More...
 
void SetDownSource (DigitalSource &source)
 Set the source object that causes the counter to count down. More...
 
void SetDownSource (std::shared_ptr< DigitalSource > source)
 
void SetDownSourceEdge (bool risingEdge, bool fallingEdge)
 Set the edge sensitivity on a down counting source. More...
 
void ClearDownSource ()
 Disable the down counting source to the counter. More...
 
void SetUpDownCounterMode ()
 Set standard up / down counting mode on this counter. More...
 
void SetExternalDirectionMode ()
 Set external direction mode on this counter. More...
 
void SetSemiPeriodMode (bool highSemiPeriod)
 Set Semi-period mode on this counter. More...
 
void SetPulseLengthMode (double threshold)
 Configure the counter to count in up or down based on the length of the input pulse. More...
 
void SetReverseDirection (bool reverseDirection)
 Set the Counter to return reversed sensing on the direction. More...
 
void SetSamplesToAverage (int samplesToAverage)
 Set the Samples to Average which specifies the number of samples of the timer to average when calculating the period. More...
 
int GetSamplesToAverage () const
 Get the Samples to Average which specifies the number of samples of the timer to average when calculating the period. More...
 
int GetFPGAIndex () const
 
int Get () const override
 Read the current counter value. More...
 
void Reset () override
 Reset the Counter to zero. More...
 
units::second_t GetPeriod () const override
 Get the Period of the most recent count. More...
 
void SetMaxPeriod (units::second_t maxPeriod) final
 Set the maximum period where the device is still considered "moving". More...
 
void SetUpdateWhenEmpty (bool enabled)
 Select whether you want to continue updating the event timer output when there are no samples captured. More...
 
bool GetStopped () const override
 Determine if the clock is stopped. More...
 
bool GetDirection () const override
 The last direction the counter value changed. More...
 
void InitSendable (wpi::SendableBuilder &builder) override
 Initializes this Sendable object. More...
 
- Public Member Functions inherited from frc::CounterBase
 CounterBase ()=default
 
virtual ~CounterBase ()=default
 
 CounterBase (CounterBase &&)=default
 
CounterBaseoperator= (CounterBase &&)=default
 
virtual int Get () const =0
 
virtual void Reset ()=0
 
virtual units::second_t GetPeriod () const =0
 
virtual void SetMaxPeriod (units::second_t maxPeriod)=0
 
virtual bool GetStopped () const =0
 
virtual bool GetDirection () const =0
 
- Public Member Functions inherited from wpi::Sendable
virtual ~Sendable ()=default
 
virtual void InitSendable (SendableBuilder &builder)=0
 Initializes this Sendable object. More...
 
- Public Member Functions inherited from wpi::SendableHelper< Counter >
 SendableHelper (const SendableHelper &rhs)=default
 
 SendableHelper (SendableHelper &&rhs)
 
SendableHelperoperator= (const SendableHelper &rhs)=default
 
SendableHelperoperator= (SendableHelper &&rhs)
 

Protected Attributes

std::shared_ptr< DigitalSourcem_upSource
 
std::shared_ptr< DigitalSourcem_downSource
 
hal::Handle< HAL_CounterHandlem_counter
 

Friends

class DMA
 
class DMASample
 
class DigitalGlitchFilter
 

Additional Inherited Members

- Protected Member Functions inherited from wpi::SendableHelper< Counter >
 SendableHelper ()=default
 
 ~SendableHelper ()
 

Detailed Description

Class for counting the number of ticks on a digital input channel.

This is a general purpose class for counting repetitive events. It can return the number of counts, the period of the most recent cycle, and detect when the signal being counted has stopped by supplying a maximum cycle time.

All counters will immediately start counting - Reset() them if you need them to be zeroed before use.

Member Enumeration Documentation

◆ Mode

Enumerator
kTwoPulse 
kSemiperiod 
kPulseLength 
kExternalDirection 

Constructor & Destructor Documentation

◆ Counter() [1/8]

frc::Counter::Counter ( Mode  mode = kTwoPulse)
explicit

Create an instance of a counter where no sources are selected.

They all must be selected by calling functions to specify the upsource and the downsource independently.

This creates a ChipObject counter and initializes status variables appropriately.

The counter will start counting immediately.

Parameters
modeThe counter mode

◆ Counter() [2/8]

frc::Counter::Counter ( int  channel)
explicit

Create an instance of a Counter object.

Create an up-Counter instance given a channel.

The counter will start counting immediately.

Parameters
channelThe DIO channel to use as the up source. 0-9 are on-board, 10-25 are on the MXP

◆ Counter() [3/8]

frc::Counter::Counter ( DigitalSource source)
explicit

Create an instance of a counter from a Digital Source (such as a Digital Input).

This is used if an existing digital input is to be shared by multiple other objects such as encoders or if the Digital Source is not a Digital Input channel (such as an Analog Trigger).

The counter will start counting immediately.

Parameters
sourceA pointer to the existing DigitalSource object. It will be set as the Up Source.

◆ Counter() [4/8]

frc::Counter::Counter ( std::shared_ptr< DigitalSource source)
explicit

Create an instance of a counter from a Digital Source (such as a Digital Input).

This is used if an existing digital input is to be shared by multiple other objects such as encoders or if the Digital Source is not a Digital Input channel (such as an Analog Trigger).

The counter will start counting immediately.

Parameters
sourceA pointer to the existing DigitalSource object. It will be set as the Up Source.

◆ Counter() [5/8]

frc::Counter::Counter ( const AnalogTrigger trigger)
explicit

Create an instance of a Counter object.

Create an instance of a simple up-Counter given an analog trigger. Use the trigger state output from the analog trigger.

The counter will start counting immediately.

Parameters
triggerThe reference to the existing AnalogTrigger object.

◆ Counter() [6/8]

frc::Counter::Counter ( EncodingType  encodingType,
DigitalSource upSource,
DigitalSource downSource,
bool  inverted 
)

Create an instance of a Counter object.

Creates a full up-down counter given two Digital Sources.

Parameters
encodingTypeThe quadrature decoding mode (1x or 2x)
upSourceThe pointer to the DigitalSource to set as the up source
downSourceThe pointer to the DigitalSource to set as the down source
invertedTrue to invert the output (reverse the direction)

◆ Counter() [7/8]

frc::Counter::Counter ( EncodingType  encodingType,
std::shared_ptr< DigitalSource upSource,
std::shared_ptr< DigitalSource downSource,
bool  inverted 
)

Create an instance of a Counter object.

Creates a full up-down counter given two Digital Sources.

Parameters
encodingTypeThe quadrature decoding mode (1x or 2x)
upSourceThe pointer to the DigitalSource to set as the up source
downSourceThe pointer to the DigitalSource to set as the down source
invertedTrue to invert the output (reverse the direction)

◆ ~Counter()

frc::Counter::~Counter ( )
override

◆ Counter() [8/8]

frc::Counter::Counter ( Counter &&  )
default

Member Function Documentation

◆ ClearDownSource()

void frc::Counter::ClearDownSource ( )

Disable the down counting source to the counter.

◆ ClearUpSource()

void frc::Counter::ClearUpSource ( )

Disable the up counting source to the counter.

◆ Get()

int frc::Counter::Get ( ) const
overridevirtual

Read the current counter value.

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

Implements frc::CounterBase.

◆ GetDirection()

bool frc::Counter::GetDirection ( ) const
overridevirtual

The last direction the counter value changed.

Returns
The last direction the counter value changed.

Implements frc::CounterBase.

◆ GetFPGAIndex()

int frc::Counter::GetFPGAIndex ( ) const

◆ GetPeriod()

units::second_t frc::Counter::GetPeriod ( ) const
overridevirtual

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

Returns
The period between the last two pulses in units of seconds.

Implements frc::CounterBase.

◆ GetSamplesToAverage()

int frc::Counter::GetSamplesToAverage ( ) const

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

Returns
The number of samples being averaged (from 1 to 127)

◆ GetStopped()

bool frc::Counter::GetStopped ( ) const
overridevirtual

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

Returns
Returns true if the most recent counter period exceeds the MaxPeriod value set by SetMaxPeriod.

Implements frc::CounterBase.

◆ InitSendable()

void frc::Counter::InitSendable ( wpi::SendableBuilder builder)
overridevirtual

Initializes this Sendable object.

Parameters
buildersendable builder

Implements wpi::Sendable.

◆ operator=()

Counter & frc::Counter::operator= ( Counter &&  )
default

◆ Reset()

void frc::Counter::Reset ( )
overridevirtual

Reset the Counter to zero.

Set the counter value to zero. This doesn't effect the running state of the counter, just sets the current value to zero.

Implements frc::CounterBase.

◆ SetDownSource() [1/6]

void frc::Counter::SetDownSource ( AnalogTrigger analogTrigger,
AnalogTriggerType  triggerType 
)

Set the down counting source to be an analog trigger.

Parameters
analogTriggerThe analog trigger object that is used for the Down Source
triggerTypeThe analog trigger output that will trigger the counter.

◆ SetDownSource() [2/6]

void frc::Counter::SetDownSource ( DigitalSource source)

Set the source object that causes the counter to count down.

Set the down counting DigitalSource.

Parameters
sourceReference to the DigitalSource object to set as the down source

◆ SetDownSource() [3/6]

void frc::Counter::SetDownSource ( DigitalSource source)

Set the source object that causes the counter to count down.

Set the down counting DigitalSource.

Parameters
sourcePointer to the DigitalSource object to set as the down source

◆ SetDownSource() [4/6]

void frc::Counter::SetDownSource ( int  channel)

Set the down counting source to be a digital input channel.

Parameters
channelThe DIO channel to use as the up source. 0-9 are on-board, 10-25 are on the MXP

◆ SetDownSource() [5/6]

void frc::Counter::SetDownSource ( std::shared_ptr< AnalogTrigger analogTrigger,
AnalogTriggerType  triggerType 
)

Set the down counting source to be an analog trigger.

Parameters
analogTriggerThe analog trigger object that is used for the Down Source
triggerTypeThe analog trigger output that will trigger the counter.

◆ SetDownSource() [6/6]

void frc::Counter::SetDownSource ( std::shared_ptr< DigitalSource source)

◆ SetDownSourceEdge()

void frc::Counter::SetDownSourceEdge ( bool  risingEdge,
bool  fallingEdge 
)

Set the edge sensitivity on a down counting source.

Set the down source to either detect rising edges or falling edges.

Parameters
risingEdgeTrue to trigger on rising edges
fallingEdgeTrue to trigger on falling edges

◆ SetExternalDirectionMode()

void frc::Counter::SetExternalDirectionMode ( )

Set external direction mode on this counter.

Counts are sourced on the Up counter input. The Down counter input represents the direction to count.

◆ SetMaxPeriod()

void frc::Counter::SetMaxPeriod ( units::second_t  maxPeriod)
finalvirtual

Set 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 GetStopped method.

Parameters
maxPeriodThe maximum period where the counted device is considered moving in seconds.

Implements frc::CounterBase.

◆ SetPulseLengthMode()

void frc::Counter::SetPulseLengthMode ( double  threshold)

Configure 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
thresholdThe pulse length beyond which the counter counts the opposite direction. Units are seconds.

◆ SetReverseDirection()

void frc::Counter::SetReverseDirection ( bool  reverseDirection)

Set 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
reverseDirectiontrue if the value counted should be negated.

◆ SetSamplesToAverage()

void frc::Counter::SetSamplesToAverage ( int  samplesToAverage)

Set 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
samplesToAverageThe number of samples to average from 1 to 127.

◆ SetSemiPeriodMode()

void frc::Counter::SetSemiPeriodMode ( bool  highSemiPeriod)

Set Semi-period mode on this counter.

Counts up on both rising and falling edges.

◆ SetUpdateWhenEmpty()

void frc::Counter::SetUpdateWhenEmpty ( bool  enabled)

Select 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
enabledTrue to enable update when empty

◆ SetUpDownCounterMode()

void frc::Counter::SetUpDownCounterMode ( )

Set standard up / down counting mode on this counter.

Up and down counts are sourced independently from two inputs.

◆ SetUpSource() [1/6]

void frc::Counter::SetUpSource ( AnalogTrigger analogTrigger,
AnalogTriggerType  triggerType 
)

Set the up counting source to be an analog trigger.

Parameters
analogTriggerThe analog trigger object that is used for the Up Source
triggerTypeThe analog trigger output that will trigger the counter.

◆ SetUpSource() [2/6]

void frc::Counter::SetUpSource ( DigitalSource source)

Set the source object that causes the counter to count up.

Set the up counting DigitalSource.

Parameters
sourceReference to the DigitalSource object to set as the up source

◆ SetUpSource() [3/6]

void frc::Counter::SetUpSource ( DigitalSource source)

◆ SetUpSource() [4/6]

void frc::Counter::SetUpSource ( int  channel)

Set the upsource for the counter as a digital input channel.

Parameters
channelThe DIO channel to use as the up source. 0-9 are on-board, 10-25 are on the MXP

◆ SetUpSource() [5/6]

void frc::Counter::SetUpSource ( std::shared_ptr< AnalogTrigger analogTrigger,
AnalogTriggerType  triggerType 
)

Set the up counting source to be an analog trigger.

Parameters
analogTriggerThe analog trigger object that is used for the Up Source
triggerTypeThe analog trigger output that will trigger the counter.

◆ SetUpSource() [6/6]

void frc::Counter::SetUpSource ( std::shared_ptr< DigitalSource source)

Set the source object that causes the counter to count up.

Set the up counting DigitalSource.

Parameters
sourcePointer to the DigitalSource object to set as the up source

◆ SetUpSourceEdge()

void frc::Counter::SetUpSourceEdge ( bool  risingEdge,
bool  fallingEdge 
)

Set the edge sensitivity on an up counting source.

Set the up source to either detect rising edges or falling edges or both.

Parameters
risingEdgeTrue to trigger on rising edges
fallingEdgeTrue to trigger on falling edges

Friends And Related Function Documentation

◆ DigitalGlitchFilter

friend class DigitalGlitchFilter
friend

◆ DMA

friend class DMA
friend

◆ DMASample

friend class DMASample
friend

Member Data Documentation

◆ m_counter

hal::Handle<HAL_CounterHandle> frc::Counter::m_counter
protected

◆ m_downSource

std::shared_ptr<DigitalSource> frc::Counter::m_downSource
protected

◆ m_upSource

std::shared_ptr<DigitalSource> frc::Counter::m_upSource
protected

The documentation for this class was generated from the following file: