DutyCycleEncoder

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

Bases: 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.

This has a fullRange of 1 and an expectedZero of 0.

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.

This has a fullRange of 1 and an expectedZero of 0.

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.

This has a fullRange of 1 and an expectedZero of 0.

Parameters:

digitalSource – the digital source to attach to

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

Construct a new DutyCycleEncoder on a specific channel.

Parameters:
  • channel – the channel to attach to

  • fullRange – the value to report at maximum travel

  • expectedZero – the reading where you would expect a 0 from get()

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

Construct a new DutyCycleEncoder attached to an existing DutyCycle object.

Parameters:
  • dutyCycle – the duty cycle to attach to

  • fullRange – the value to report at maximum travel

  • expectedZero – the reading where you would expect a 0 from get()

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

Construct a new DutyCycleEncoder attached to a DigitalSource object.

Parameters:
  • digitalSource – the digital source to attach to

  • fullRange – the value to report at maximum travel

  • expectedZero – the reading where you would expect a 0 from get()

get() float

Get the encoder value.

Returns:

the encoder value scaled by the full range input

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

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

setAssumedFrequency(frequency: wpimath.units.hertz) None

Sets the assumed frequency of the connected device.

By default, the DutyCycle engine has to compute the frequency of the input signal. This can result in both delayed readings and jumpy readings. To solve this, you can pass the expected frequency of the sensor to this function. This will use that frequency to compute the DutyCycle percentage, rather than the computed frequency.

Parameters:

frequency – the assumed frequency of the sensor

setConnectedFrequencyThreshold(frequency: int) None

Change the frequency threshold for detecting connection used by IsConnected.

Parameters:

frequency – the minimum frequency in Hz.

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)

setInverted(inverted: bool) None

Set if this encoder is inverted.

Parameters:

inverted – true to invert the encoder, false otherwise