Class DutyCycleEncoder

java.lang.Object
edu.wpi.first.wpilibj.DutyCycleEncoder
All Implemented Interfaces:
Sendable, AutoCloseable

public class DutyCycleEncoder
extends Object
implements Sendable, AutoCloseable
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.
  • Field Details

  • Constructor Details

  • Method Details

    • get

      public double get()
      Get the encoder value since the last reset.

      This is reported in rotations since the last reset.

      Returns:
      the encoder value in rotations
    • getPositionOffset

      public double getPositionOffset()
      Get the offset of position relative to the last reset.

      getPositionInRotation() - 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
    • setDutyCycleRange

      public void setDutyCycleRange​(double min, double max)
      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)
    • setDistancePerRotation

      public void setDistancePerRotation​(double distancePerRotation)
      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
    • getDistancePerRotation

      public double getDistancePerRotation()
      Get the distance per rotation for this encoder.
      Returns:
      The scale factor that will be used to convert rotation to useful units.
    • getDistance

      public double getDistance()
      Get the distance the sensor has driven since the last reset as scaled by the value from setDistancePerRotation(double).
      Returns:
      The distance driven since the last reset
    • getFrequency

      public int getFrequency()
      Get the frequency in Hz of the duty cycle signal from the encoder.
      Returns:
      duty cycle frequency in Hz
    • reset

      public void reset()
      Reset the Encoder distance to zero.
    • isConnected

      public boolean isConnected()
      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(int).

      Returns:
      true if the sensor is connected
    • setConnectedFrequencyThreshold

      public void setConnectedFrequencyThreshold​(int frequency)
      Change the frequency threshold for detecting connection used by isConnected().
      Parameters:
      frequency - the minimum frequency in Hz.
    • close

      public void close()
      Specified by:
      close in interface AutoCloseable
    • getFPGAIndex

      public int getFPGAIndex()
      Get the FPGA index for the DutyCycleEncoder.
      Returns:
      the FPGA index
    • getSourceChannel

      public int getSourceChannel()
      Get the channel of the source.
      Returns:
      the source channel
    • initSendable

      public void initSendable​(SendableBuilder builder)
      Description copied from interface: Sendable
      Initializes this Sendable object.
      Specified by:
      initSendable in interface Sendable
      Parameters:
      builder - sendable builder