WPILibC++  unspecified
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Pages
Counter Class Reference

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

#include <Counter.h>

Inheritance diagram for Counter:
SensorBase CounterBase LiveWindowSendable ErrorBase Sendable GearTooth

Public Member Functions

 Counter (Mode mode=kTwoPulse)
 Create an instance of a counter where no sources are selected. More...
 
 Counter (int32_t 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 (AnalogTrigger *trigger)
 Create an instance of a Counter object. 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...
 
virtual ~Counter ()
 Delete the Counter object.
 
void SetUpSource (int32_t 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.
 
void SetDownSource (int32_t 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)
 
void SetDownSource (std::shared_ptr< 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 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.
 
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 (float 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...
 
int32_t Get () const override
 Read the current counter value. More...
 
void Reset () override
 Reset the Counter to zero. More...
 
double GetPeriod () const override
 Get the Period of the most recent count. More...
 
void SetMaxPeriod (double maxPeriod) override
 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 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...
 
uint32_t GetFPGAIndex () const
 
void UpdateTable () override
 Update the table for this sendable object with the latest values.
 
void StartLiveWindowMode () override
 Start having this sendable object automatically respond to value changes reflect the value on the table.
 
void StopLiveWindowMode () override
 Stop having this sendable object automatically respond to value changes.
 
virtual std::string GetSmartDashboardType () const override
 
void InitTable (std::shared_ptr< ITable > subTable) override
 Initializes a table for this sendable object. More...
 
std::shared_ptr< ITableGetTable () const override
 
- Public Member Functions inherited from SensorBase
 SensorBase ()
 Creates an instance of the sensor base and gets an FPGA handle.
 
 SensorBase (const SensorBase &)=delete
 
SensorBaseoperator= (const SensorBase &)=delete
 
- Public Member Functions inherited from ErrorBase
 ErrorBase (const ErrorBase &)=delete
 
ErrorBaseoperator= (const ErrorBase &)=delete
 
virtual ErrorGetError ()
 Retrieve the current error. More...
 
virtual const ErrorGetError () const
 
virtual void SetErrnoError (llvm::StringRef contextMessage, llvm::StringRef filename, llvm::StringRef function, uint32_t lineNumber) const
 Set error information associated with a C library call that set an error to the "errno" global variable. More...
 
virtual void SetImaqError (int success, llvm::StringRef contextMessage, llvm::StringRef filename, llvm::StringRef function, uint32_t lineNumber) const
 Set the current error information associated from the nivision Imaq API. More...
 
virtual void SetError (Error::Code code, llvm::StringRef contextMessage, llvm::StringRef filename, llvm::StringRef function, uint32_t lineNumber) const
 Set the current error information associated with this sensor. More...
 
virtual void SetWPIError (llvm::StringRef errorMessage, Error::Code code, llvm::StringRef contextMessage, llvm::StringRef filename, llvm::StringRef function, uint32_t lineNumber) const
 Set the current error information associated with this sensor. More...
 
virtual void CloneError (const ErrorBase &rhs) const
 
virtual void ClearError () const
 Clear the current error information associated with this sensor.
 
virtual bool StatusIsFatal () const
 Check if the current error code represents a fatal error. More...
 

Protected Attributes

std::shared_ptr< DigitalSourcem_upSource
 
std::shared_ptr< DigitalSourcem_downSource
 
void * m_counter = nullptr
 The FPGA counter object.
 
- Protected Attributes inherited from ErrorBase
Error m_error
 

Friends

class DigitalGlitchFilter
 

Additional Inherited Members

- Public Types inherited from CounterBase
enum  EncodingType { k1X, k2X, k4X }
 
- Static Public Member Functions inherited from SensorBase
static void DeleteSingletons ()
 Delete all the singleton classes on the list. More...
 
static uint32_t GetDefaultSolenoidModule ()
 
static bool CheckSolenoidModule (uint8_t moduleNumber)
 Check that the solenoid module number is valid. More...
 
static bool CheckDigitalChannel (uint32_t channel)
 Check that the digital channel number is valid. More...
 
static bool CheckRelayChannel (uint32_t channel)
 Check that the digital channel number is valid. More...
 
static bool CheckPWMChannel (uint32_t channel)
 Check that the digital channel number is valid. More...
 
static bool CheckAnalogInput (uint32_t channel)
 Check that the analog input number is value. More...
 
static bool CheckAnalogOutput (uint32_t channel)
 Check that the analog output number is valid. More...
 
static bool CheckSolenoidChannel (uint32_t channel)
 Verify that the solenoid channel number is within limits. More...
 
static bool CheckPDPChannel (uint32_t channel)
 Verify that the power distribution channel number is within limits. More...
 
- Static Public Member Functions inherited from ErrorBase
static void SetGlobalError (Error::Code code, llvm::StringRef contextMessage, llvm::StringRef filename, llvm::StringRef function, uint32_t lineNumber)
 
static void SetGlobalWPIError (llvm::StringRef errorMessage, llvm::StringRef contextMessage, llvm::StringRef filename, llvm::StringRef function, uint32_t lineNumber)
 
static ErrorGetGlobalError ()
 Retrieve the current global error.
 
- Static Public Attributes inherited from SensorBase
static const uint32_t kDigitalChannels = 26
 
static const uint32_t kAnalogInputs = 8
 
static const uint32_t kAnalogOutputs = 2
 
static const uint32_t kSolenoidChannels = 8
 
static const uint32_t kSolenoidModules = 2
 
static const uint32_t kPwmChannels = 20
 
static const uint32_t kRelayChannels = 8
 
static const uint32_t kPDPChannels = 16
 
static const uint32_t kChassisSlots = 8
 
- Protected Member Functions inherited from SensorBase
void AddToSingletonList ()
 Add sensor to the singleton list. More...
 
- Static Protected Attributes inherited from SensorBase
static void * m_digital_ports [kDigitalChannels]
 
static void * m_relay_ports [kRelayChannels]
 
static void * m_pwm_ports [kPwmChannels]
 
- Static Protected Attributes inherited from ErrorBase
static priority_mutex _globalErrorMutex
 
static Error _globalError
 

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.

Constructor & Destructor Documentation

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::Counter ( int32_t  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::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::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::Counter ( 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 pointer to the existing AnalogTrigger object.
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::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::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)

Member Function Documentation

int32_t 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 CounterBase.

bool Counter::GetDirection ( ) const
overridevirtual

The last direction the counter value changed.

Returns
The last direction the counter value changed.

Implements CounterBase.

double 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 CounterBase.

int 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
SamplesToAverage The number of samples being averaged (from 1 to 127)
std::string Counter::GetSmartDashboardType ( ) const
overridevirtual
Returns
the string representation of the named data type that will be used by the smart dashboard for this sendable

Implements Sendable.

Reimplemented in GearTooth.

bool 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 CounterBase.

std::shared_ptr< ITable > Counter::GetTable ( ) const
overridevirtual
Returns
the table that is currently associated with the sendable

Implements Sendable.

void Counter::InitTable ( std::shared_ptr< ITable subtable)
overridevirtual

Initializes a table for this sendable object.

Parameters
subtableThe table to put the values in.

Implements Sendable.

void 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 CounterBase.

void Counter::SetDownSource ( int32_t  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
void 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.
void 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.
void Counter::SetDownSource ( std::shared_ptr< 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
void 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
void 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
void 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.

void Counter::SetMaxPeriod ( double  maxPeriod)
overridevirtual

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

void Counter::SetPulseLengthMode ( float  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.
void 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.
void 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.
void Counter::SetSemiPeriodMode ( bool  highSemiPeriod)

Set Semi-period mode on this counter.

Counts up on both rising and falling edges.

void 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
void Counter::SetUpDownCounterMode ( )

Set standard up / down counting mode on this counter.

Up and down counts are sourced independently from two inputs.

void Counter::SetUpSource ( int32_t  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
void 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.
void 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.
void 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
void 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
void 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

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