Counter class (Java, C++) is a versatile class that allows the counting of pulse edges on a digital input.
Counter is used as a component in several more-complicated WPILib classes (such as Encoder and Ultrasonic), but is also quite useful on its own.
There are a total of 8 counter units in the roboRIO FPGA, meaning no more than 8
Counter objects may be instantiated at any one time, including those contained as resources in other WPILib objects. For detailed information on when a
Counter may be used by another object, refer to the official API documentation.
Configuring a counter¶
Counter class can be configured in a number of ways to provide differing functionalities.
Counter object may be configured to operate in one of four different modes:
Two-pulse mode: Counts up and down based on the edges of two different channels.
Semi-period mode: Measures the duration of a pulse on a single channel.
Pulse-length mode: Counts up and down based on the edges of one channel, with the direction determined by the duration of the pulse on that channel.
External direction mode: Counts up and down based on the edges of one channel, with a separate channel specifying the direction.
In all modes except semi-period mode, the counter can be configured to increment either once per edge (2X decoding), or once per pulse (1X decoding). By default, counters are set to two-pulse mode, if only one channel is specified, the counter will only count up.
In two-pulse mode, the
Counter will count up for every edge/pulse on the specified “up channel,” and down for every edge/pulse on the specified “down channel.” A counter can be initialized in two-pulse with the following code:
In semi-period mode, the
Counter will count the duration of the pulses on a channel, either from a rising edge to the next falling edge, or from a falling edge to the next rising edge. A counter can be initialized in semi-period mode with the following code:
To get the pulse width, call the
In pulse-length mode, the counter will count either up or down depending on the length of the pulse. A pulse below the specified threshold time will be interpreted as a forward count and a pulse above the threshold is a reverse count. This is useful for some gear tooth sensors which encode direction in this manner. A counter can be initialized in this mode as follows:
External direction mode¶
In external direction mode, the counter counts either up or down depending on the level on the second channel. If the direction source is low, the counter will increase, if the direction source is high, the counter will decrease (to reverse this, see the next section). A counter can be initialized in this mode as follows:
Configuring counter parameters¶
Counter class does not make any assumptions about units of distance; it will return values in whatever units were used to calculate the distance-per-pulse value. Users thus have complete control over the distance units used. However, units of time are always in seconds.
The number of pulses used in the distance-per-pulse calculation does not depend on the decoding type - each “pulse” should always be considered to be a full cycle (rising and falling).
Apart from the mode-specific configurations, the
Counter class offers a number of additional configuration methods:
Reading information from counters¶
Regardless of mode, there is some information that the
Counter class always exposes to users:
Users can obtain the current count with the
Counters measure relative distance, not absolute; the distance value returned will depend on the position of the encoder when the robot was turned on or the encoder value was last reset.
If the distance per pulse has been configured, users can obtain the total distance traveled by the counted sensor with the
Units of time for the
Counter class are always in seconds.
Users can obtain the current rate of change of the counter with the
Users can obtain whether the counter is stationary with the
Users can obtain the direction in which the counter last moved with the
Resetting a counter¶
To reset a counter to a distance reading of zero, call the
reset() method. This is useful for ensuring that the measured distance corresponds to the actual desired physical measurement.
Using counters in code¶
Counters are useful for a wide variety of robot applications - but since the
Counter class is so varied, it is difficult to provide a good summary of them here. Many of these applications overlap with the
Encoder class - a simple counter is often a cheaper alternative to a quadrature encoder. For a summary of potential uses for encoders in code, see Encoders - Software.