Encoder

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

Bases: CounterBase, Sendable

Class to read quad encoders.

Quadrature encoders are devices that count shaft rotation and can sense direction. The output of the QuadEncoder class is an integer that can count either up or down, and can go negative for reverse direction counting. When creating QuadEncoders, a direction is supplied that changes the sense of the output to make code more readable if the encoder is mounted such that forward movement generates negative values. Quadrature encoders have two digital outputs, an A Channel and a B Channel that are out of phase with each other to allow the FPGA to do direction sensing.

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

Overloaded function.

  1. __init__(self: wpilib._wpilib.Encoder, aChannel: int, bChannel: int, reverseDirection: bool = False, encodingType: wpilib.interfaces._interfaces.CounterBase.EncodingType = <EncodingType.k4X: 2>) -> None

Encoder constructor.

Construct a Encoder given a and b channels.

The counter will start counting immediately.

Parameters:
  • aChannel – The a channel DIO channel. 0-9 are on-board, 10-25 are on the MXP port

  • bChannel – The b channel DIO channel. 0-9 are on-board, 10-25 are on the MXP port

  • reverseDirection – represents the orientation of the encoder and inverts the output values if necessary so forward represents positive values.

  • encodingType – either k1X, k2X, or k4X to indicate 1X, 2X or 4X decoding. If 4X is selected, then an encoder FPGA object is used and the returned counts will be 4x the encoder spec’d value since all rising and falling edges are counted. If 1X or 2X are selected then a counter object will be used and the returned value will either exactly match the spec’d count or be double (2x) the spec’d count.

  1. __init__(self: wpilib._wpilib.Encoder, aSource: wpilib._wpilib.DigitalSource, bSource: wpilib._wpilib.DigitalSource, reverseDirection: bool = False, encodingType: wpilib.interfaces._interfaces.CounterBase.EncodingType = <EncodingType.k4X: 2>) -> None

class IndexingType(value: int)

Bases: pybind11_object

Members:

kResetWhileHigh

kResetWhileLow

kResetOnFallingEdge

kResetOnRisingEdge

kResetOnFallingEdge = <IndexingType.kResetOnFallingEdge: 2>
kResetOnRisingEdge = <IndexingType.kResetOnRisingEdge: 3>
kResetWhileHigh = <IndexingType.kResetWhileHigh: 0>
kResetWhileLow = <IndexingType.kResetWhileLow: 1>
property name
property value
get() int

Gets the current count.

Returns the current count on the Encoder. This method compensates for the decoding type.

Returns:

Current count from the Encoder adjusted for the 1x, 2x, or 4x scale factor.

getDirection() bool

The last direction the encoder value changed.

Returns:

The last direction the encoder value changed.

getDistance() float

Get the distance the robot has driven since the last reset.

Returns:

The distance driven since the last reset as scaled by the value from SetDistancePerPulse().

getDistancePerPulse() float

Get the distance per pulse for this encoder.

Returns:

The scale factor that will be used to convert pulses to useful units.

getEncodingScale() int

The encoding scale factor 1x, 2x, or 4x, per the requested encodingType.

Used to divide raw edge counts down to spec’d counts.

getFPGAIndex() int
getPeriod() seconds

Returns the period of the most recent pulse.

Returns the period of the most recent Encoder pulse in seconds. This method compensates for the decoding type.

Warning: This returns unscaled periods. Use GetRate() for rates that are scaled using the value from SetDistancePerPulse().

Deprecated:

Use getRate() in favor of this method.

Returns:

Period in seconds of the most recent pulse.

getRate() float

Get the current rate of the encoder.

Units are distance per second as scaled by the value from SetDistancePerPulse().

Returns:

The current rate of the encoder.

getRaw() int

Gets the raw value from the encoder.

The raw value is the actual count unscaled by the 1x, 2x, or 4x scale factor.

Returns:

Current raw count from the encoder

getSamplesToAverage() 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() bool

Determine if the encoder is stopped.

Using the MaxPeriod value, a boolean is returned that is true if the encoder is considered stopped and false if it is still moving. A stopped encoder is one where the most recent pulse width exceeds the MaxPeriod.

Returns:

True if the encoder is considered stopped.

initSendable(builder: wpiutil._wpiutil.SendableBuilder) None
reset() None

Reset the Encoder distance to zero.

Resets the current count to zero on the encoder.

setDistancePerPulse(distancePerPulse: float) None

Set the distance per pulse for this encoder.

This sets the multiplier used to determine the distance driven based on the count value from the encoder.

Do not include the decoding type in this scale. The library already compensates for the decoding type.

Set this value based on the encoder’s rated Pulses per Revolution and factor in gearing reductions following the encoder shaft.

This distance can be in any units you like, linear or angular.

Parameters:

distancePerPulse – The scale factor that will be used to convert pulses to useful units.

setIndexSource(*args, **kwargs)

Overloaded function.

  1. setIndexSource(self: wpilib._wpilib.Encoder, channel: int, type: wpilib._wpilib.Encoder.IndexingType = <IndexingType.kResetOnRisingEdge: 3>) -> None

Set the index source for the encoder.

When this source is activated, the encoder count automatically resets.

Parameters:
  • channel – A DIO channel to set as the encoder index

  • type – The state that will cause the encoder to reset

  1. setIndexSource(self: wpilib._wpilib.Encoder, source: wpilib._wpilib.DigitalSource, type: wpilib._wpilib.Encoder.IndexingType = <IndexingType.kResetOnRisingEdge: 3>) -> None

Set the index source for the encoder.

When this source is activated, the encoder count automatically resets.

Parameters:
  • source – A digital source to set as the encoder index

  • type – The state that will cause the encoder to reset

setMaxPeriod(maxPeriod: seconds) None

Sets the maximum period for stopped detection.

Sets the value that represents the maximum period of the Encoder before it will assume that the attached device is stopped. This timeout allows users to determine if the wheels or other shaft has stopped rotating. This method compensates for the decoding type.

Deprecated:

Use SetMinRate() in favor of this method. This takes unscaled periods and SetMinRate() scales using value from SetDistancePerPulse().

Parameters:

maxPeriod – The maximum time between rising and falling edges before the FPGA will report the device stopped. This is expressed in seconds.

setMinRate(minRate: float) None

Set the minimum rate of the device before the hardware reports it stopped.

Parameters:

minRate – The minimum rate. The units are in distance per second as scaled by the value from SetDistancePerPulse().

setReverseDirection(reverseDirection: bool) None

Set the direction sensing for this encoder.

This sets the direction sensing on the encoder so that it could count in the correct software direction regardless of the mounting.

Parameters:

reverseDirection – true if the encoder direction should be reversed

setSamplesToAverage(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.

setSimDevice(device: int) None

Indicates this encoder is used by a simulated device.

Parameters:

device – simulated device handle