Counter

class wpilib.Counter(*args, **kwargs)

Bases: wpilib.ErrorBase, wpilib.interfaces.CounterBase, wpilib.Sendable

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.

Overloaded function.

  1. __init__(self: wpilib._wpilib.Counter, mode: wpilib._wpilib.Counter.Mode = <Mode.kTwoPulse: 0>) -> None

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

mode – The counter mode

  1. __init__(self: wpilib._wpilib.Counter, channel: int) -> None

Create an instance of a Counter object.

Create an up-Counter instance given a channel.

The counter will start counting immediately.

Parameters

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

  1. __init__(self: wpilib._wpilib.Counter, source: wpilib._wpilib.DigitalSource) -> None

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

source – A pointer to the existing DigitalSource object. It will be set as the Up Source.

  1. __init__(self: wpilib._wpilib.Counter, trigger: wpilib._wpilib.AnalogTrigger) -> None

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

trigger – The reference to the existing AnalogTrigger object.

  1. __init__(self: wpilib._wpilib.Counter, encodingType: wpilib.interfaces._interfaces.CounterBase.EncodingType, upSource: wpilib._wpilib.DigitalSource, downSource: wpilib._wpilib.DigitalSource, inverted: bool) -> None

Create an instance of a Counter object.

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

Parameters
  • encodingType – The quadrature decoding mode (1x or 2x)

  • upSource – The pointer to the DigitalSource to set as the up source

  • downSource – The pointer to the DigitalSource to set as the down source

  • inverted – True to invert the output (reverse the direction)

class Mode(self: wpilib._wpilib.Counter.Mode, value: int) → None

Bases: pybind11_builtins.pybind11_object

Members:

kTwoPulse

kSemiperiod

kPulseLength

kExternalDirection

kExternalDirection = <Mode.kExternalDirection: 3>
kPulseLength = <Mode.kPulseLength: 2>
kSemiperiod = <Mode.kSemiperiod: 1>
kTwoPulse = <Mode.kTwoPulse: 0>
property name
clearDownSource(self: wpilib.Counter) → None

Disable the down counting source to the counter.

clearUpSource(self: wpilib.Counter) → None

Disable the up counting source to the counter.

get(self: wpilib.Counter) → int

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.

getDirection(self: wpilib.Counter) → bool

The last direction the counter value changed.

Returns

The last direction the counter value changed.

getFPGAIndex(self: wpilib.Counter) → int
getPeriod(self: wpilib.Counter) → float

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.

getSamplesToAverage(self: wpilib.Counter) → int

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(self: wpilib.Counter) → bool

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.

initSendable(self: wpilib.Counter, builder: wpilib.SendableBuilder) → None
reset(self: wpilib.Counter) → None

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.

setDownSource(*args, **kwargs)

Overloaded function.

  1. setDownSource(self: wpilib._wpilib.Counter, channel: int) -> None

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

Parameters

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

  1. setDownSource(self: wpilib._wpilib.Counter, analogTrigger: wpilib._wpilib.AnalogTrigger, triggerType: wpilib._wpilib.AnalogTriggerType) -> None

Set the down counting source to be an analog trigger.

Parameters
  • analogTrigger – The analog trigger object that is used for the Down Source

  • triggerType – The analog trigger output that will trigger the counter.

  1. setDownSource(self: wpilib._wpilib.Counter, source: wpilib._wpilib.DigitalSource) -> None

setDownSourceEdge(self: wpilib.Counter, risingEdge: bool, fallingEdge: bool) → None

Set the edge sensitivity on a down counting source.

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

Parameters
  • risingEdge – True to trigger on rising edges

  • fallingEdge – True to trigger on falling edges

setExternalDirectionMode(self: wpilib.Counter) → None

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(self: wpilib.Counter, maxPeriod: float) → None

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

maxPeriod – The maximum period where the counted device is considered moving in seconds.

setPulseLengthMode(self: wpilib.Counter, threshold: float) → None

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

threshold – The pulse length beyond which the counter counts the opposite direction. Units are seconds.

setReverseDirection(self: wpilib.Counter, reverseDirection: bool) → None

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

reverseDirection – true if the value counted should be negated.

setSamplesToAverage(self: wpilib.Counter, samplesToAverage: int) → None

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

samplesToAverage – The number of samples to average from 1 to 127.

setSemiPeriodMode(self: wpilib.Counter, highSemiPeriod: bool) → None

Set Semi-period mode on this counter.

Counts up on both rising and falling edges.

setUpDownCounterMode(self: wpilib.Counter) → None

Set standard up / down counting mode on this counter.

Up and down counts are sourced independently from two inputs.

setUpSource(*args, **kwargs)

Overloaded function.

  1. setUpSource(self: wpilib._wpilib.Counter, channel: int) -> None

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

Parameters

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

  1. setUpSource(self: wpilib._wpilib.Counter, analogTrigger: wpilib._wpilib.AnalogTrigger, triggerType: wpilib._wpilib.AnalogTriggerType) -> None

Set the up counting source to be an analog trigger.

Parameters
  • analogTrigger – The analog trigger object that is used for the Up Source

  • triggerType – The analog trigger output that will trigger the counter.

  1. setUpSource(self: wpilib._wpilib.Counter, source: wpilib._wpilib.DigitalSource) -> None

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

Set the up counting DigitalSource.

Parameters

source – Pointer to the DigitalSource object to set as the up source

setUpSourceEdge(self: wpilib.Counter, risingEdge: bool, fallingEdge: bool) → None

Set the edge sensitivity on an up counting source.

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

Parameters
  • risingEdge – True to trigger on rising edges

  • fallingEdge – True to trigger on falling edges

setUpdateWhenEmpty(self: wpilib.Counter, enabled: bool) → None

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

enabled – True to enable update when empty