Counter

class wpilib.Counter(*args, **kwargs)[source]

Bases: wpilib.SensorBase

Counts the number of ticks on a DigitalInput 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.

Counter constructor.

The counter will start counting immediately.

Positional arguments may be either channel numbers, DigitalSource sources, or AnalogTrigger sources in the following order:

A “source” is any valid single-argument input to setUpSource() and setDownSource()

  • (none)
  • upSource
  • upSource, down source

And, to keep consistency with Java wpilib. - encodingType, up source, down source, inverted

If the passed object has a getChannelForRouting function, it is assumed to be a DigitalSource. If the passed object has a createOutput function, it is assumed to be an AnalogTrigger.

In addition, extra keyword parameters may be provided for mode, inverted, and encodingType.

Parameters:
  • upSource – The source (channel num, DigitalInput, or AnalogTrigger) that should be used for up counting.
  • downSource – The source (channel num, DigitalInput, or AnalogTrigger) that should be used for down counting or direction control.
  • mode – How and what the counter counts (see Mode). Defaults to Mode.kTwoPulse for zero or one source, and Mode.kExternalDirection for two sources.
  • inverted – Flips the direction of counting. Defaults to False if unspecified. Only used when two sources are specified.
  • encodingType – Either k1X or k2X to indicate 1X or 2X decoding. 4X decoding is not supported by Counter; use Encoder instead. Defaults to k1X if unspecified. Only used when two sources are specified.
class EncodingType

Bases: builtins.object

The number of edges for the counterbase to increment or decrement on

k1X = 0
k2X = 1
k4X = 2
class Counter.Mode[source]

Bases: builtins.object

Mode determines how and what the counter counts

kExternalDirection = 3

external direction mode

kPulseLength = 2

pulse length mode

kSemiperiod = 1

semi period mode

kTwoPulse = 0

two pulse mode

class Counter.PIDSourceParameter

Bases: builtins.object

A description for the type of output value to provide to a PIDController

kAngle = 2
kDistance = 0
kRate = 1
Counter.allocatedDownSource = False
Counter.allocatedUpSource = False
Counter.clearDownSource()[source]

Disable the down counting source to the counter.

Counter.clearUpSource()[source]

Disable the up counting source to the counter.

Counter.counter[source]
Counter.free()[source]
Counter.get()[source]

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.

Counter.getDirection()[source]

The last direction the counter value changed.

Returns:The last direction the counter value changed.
Return type:bool
Counter.getDistance()[source]

Read the current scaled counter value. Read the value at this instant, scaled by the distance per pulse (defaults to 1).

Returns:Scaled value
Return type:float
Counter.getFPGAIndex()[source]
Returns:The Counter’s FPGA index.
Counter.getPeriod()[source]

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 of the last two pulses in units of seconds.
Return type:float
Counter.getRate()[source]

Get the current rate of the Counter. Read the current rate of the counter accounting for the distance per pulse value. The default value for distance per pulse (1) yields units of pulses per second.

Returns:The rate in units/sec
Return type:float
Counter.getSamplesToAverage()[source]

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)
Return type:int
Counter.getStopped()[source]

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.
Return type:bool
Counter.pidGet()[source]
Counter.reset()[source]

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.

Counter.setDistancePerPulse(distancePerPulse)[source]

Set the distance per pulse for this counter. This sets the multiplier used to determine the distance driven based on the count value from the encoder. Set this value based on the Pulses per Revolution and factor in any gearing reductions. This distance can be in any units you like, linear or angular.

Parameters:distancePerPulse (float) – The scale factor that will be used to convert pulses to useful units.
Counter.setDownSource(*args, **kwargs)[source]

Set the down counting source for the counter.

This function accepts either a digital channel index, a DigitalSource, or an AnalogTrigger as positional arguments:

  • source
  • channel
  • analogTrigger
  • analogTrigger, triggerType

For positional arguments, if the passed object has a getChannelForRouting function, it is assumed to be a DigitalSource. If the passed object has a createOutput function, it is assumed to be an AnalogTrigger.

Alternatively, sources and/or channels may be passed as keyword arguments. The behavior of specifying both a source and a number for the same channel is undefined, as is passing both a positional and a keyword argument for the same channel.

Parameters:
  • channel (int) – the DIO channel to use as the down source. 0-9 are on-board, 10-25 are on the MXP
  • source (DigitalInput) – The digital source to count
  • analogTrigger (AnalogTrigger) – The analog trigger object that is used for the Up Source
  • triggerType (AnalogTriggerType) – The analog trigger output that will trigger the counter. Defaults to kState if not specified.
Counter.setDownSourceEdge(risingEdge, fallingEdge)[source]

Set the edge sensitivity on an down counting source. Set the down source to either detect rising edges or falling edges.

Parameters:
  • risingEdge (bool) – True to count rising edge
  • fallingEdge (bool) – True to count falling edge
Counter.setExternalDirectionMode()[source]

Set external direction mode on this counter. Counts are sourced on the Up counter input. The Down counter input represents the direction to count.

Counter.setMaxPeriod(maxPeriod)[source]

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 (float or int) – The maximum period where the counted device is considered moving in seconds.
Counter.setPIDSourceParameter(pidSource)[source]

Set which parameter of the encoder you are using as a process control variable. The counter class supports the rate and distance parameters.

Parameters:pidSource (Counter.PIDSourceParameter) – An enum to select the parameter.
Counter.setPulseLengthMode(threshold)[source]

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 (float, int) – The pulse length beyond which the counter counts the opposite direction. Units are seconds.
Counter.setReverseDirection(reverseDirection)[source]

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 (bool) – True if the value counted should be negated.
Counter.setSamplesToAverage(samplesToAverage)[source]

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 (int) – The number of samples to average from 1 to 127.
Counter.setSemiPeriodMode(highSemiPeriod)[source]

Set Semi-period mode on this counter. Counts up on both rising and falling edges.

Parameters:highSemiPeriod (bool) – True to count up on both rising and falling
Counter.setUpDownCounterMode()[source]

Set standard up / down counting mode on this counter. Up and down counts are sourced independently from two inputs.

Counter.setUpSource(*args, **kwargs)[source]

Set the up counting source for the counter.

This function accepts either a digital channel index, a DigitalSource, or an AnalogTrigger as positional arguments:

  • source
  • channel
  • analogTrigger
  • analogTrigger, triggerType

For positional arguments, if the passed object has a getChannelForRouting function, it is assumed to be a DigitalSource. If the passed object has a createOutput function, it is assumed to be an AnalogTrigger.

Alternatively, sources and/or channels may be passed as keyword arguments. The behavior of specifying both a source and a number for the same channel is undefined, as is passing both a positional and a keyword argument for the same channel.

Parameters:
  • channel (int) – the DIO channel to use as the up source. 0-9 are on-board, 10-25 are on the MXP
  • source (DigitalInput) – The digital source to count
  • analogTrigger (AnalogTrigger) – The analog trigger object that is used for the Up Source
  • triggerType (AnalogTriggerType) – The analog trigger output that will trigger the counter. Defaults to kState if not specified.
Counter.setUpSourceEdge(risingEdge, fallingEdge)[source]

Set the edge sensitivity on an up counting source. Set the up source to either detect rising edges or falling edges.

Parameters:
  • risingEdge (bool) – True to count rising edge
  • fallingEdge (bool) – True to count falling edge
Counter.setUpdateWhenEmpty(enabled)[source]

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 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 (bool) – True to continue updating