DutyCycleEncoder

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

Bases: wpiutil._wpiutil.Sendable

Class for supporting duty cycle/PWM encoders, such as the US Digital MA3 with PWM Output, the CTRE Mag Encoder, the Rev Hex Encoder, and the AM Mag Encoder.

Overloaded function.

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

Construct a new DutyCycleEncoder on a specific channel.

Parameters

channel – the channel to attach to

  1. __init__(self: wpilib._wpilib.DutyCycleEncoder, dutyCycle: wpilib._wpilib.DutyCycle) -> None

Construct a new DutyCycleEncoder attached to an existing DutyCycle object.

Parameters

dutyCycle – the duty cycle to attach to

  1. __init__(self: wpilib._wpilib.DutyCycleEncoder, digitalSource: wpilib._wpilib.DigitalSource) -> None

Construct a new DutyCycleEncoder attached to a DigitalSource object.

Parameters

digitalSource – the digital source to attach to

get() turns

Get the encoder value since the last reset.

This is reported in rotations since the last reset.

Returns

the encoder value in rotations

getAbsolutePosition() float

Get the absolute position of the duty cycle encoder encoder.

GetAbsolutePosition() - GetPositionOffset() will give an encoder absolute position relative to the last reset. This could potentially be negative, which needs to be accounted for.

This will not account for rollovers, and will always be just the raw absolute position.

Returns

the absolute position

getDistance() float

Get the distance the sensor has driven since the last reset as scaled by the value from SetDistancePerRotation.

Returns

The distance driven since the last reset

getDistancePerRotation() float

Get the distance per rotation for this encoder.

Returns

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

getFPGAIndex() int

Get the FPGA index for the DutyCycleEncoder.

Returns

the FPGA index

getFrequency() int

Get the frequency in Hz of the duty cycle signal from the encoder.

Returns

duty cycle frequency in Hz

getPositionOffset() float

Get the offset of position relative to the last reset.

GetAbsolutePosition() - GetPositionOffset() will give an encoder absolute position relative to the last reset. This could potentially be negative, which needs to be accounted for.

Returns

the position offset

getSourceChannel() int

Get the channel of the source.

Returns

the source channel

initSendable(builder: wpiutil._wpiutil.SendableBuilder) None
isConnected() bool

Get if the sensor is connected

This uses the duty cycle frequency to determine if the sensor is connected. By default, a value of 100 Hz is used as the threshold, and this value can be changed with SetConnectedFrequencyThreshold.

Returns

true if the sensor is connected

reset() None

Reset the Encoder distance to zero.

setConnectedFrequencyThreshold(frequency: int) None

Change the frequency threshold for detecting connection used by IsConnected.

Parameters

frequency – the minimum frequency in Hz.

setDistancePerRotation(distancePerRotation: float) None

Set the distance per rotation of the encoder. This sets the multiplier used to determine the distance driven based on the rotation value from the encoder. Set this value based on the how far the mechanism travels in 1 rotation of the encoder, and factor in gearing reductions following the encoder shaft. This distance can be in any units you like, linear or angular.

Parameters

distancePerRotation – the distance per rotation of the encoder

setDutyCycleRange(min: float, max: float) None

Set the encoder duty cycle range. As the encoder needs to maintain a duty cycle, the duty cycle cannot go all the way to 0% or all the way to 100%. For example, an encoder with a 4096 us period might have a minimum duty cycle of 1 us / 4096 us and a maximum duty cycle of 4095 / 4096 us. Setting the range will result in an encoder duty cycle less than or equal to the minimum being output as 0 rotation, the duty cycle greater than or equal to the maximum being output as 1 rotation, and values in between linearly scaled from 0 to 1.

Parameters
  • min – minimum duty cycle (0-1 range)

  • max – maximum duty cycle (0-1 range)

setPositionOffset(offset: float) None

Set the position offset.

This must be in the range of 0-1.

Parameters

offset – the offset