Class Encoder

java.lang.Object
org.wpilib.hardware.rotation.Encoder
All Implemented Interfaces:
AutoCloseable, CounterBase, TelemetryLoggable

public class Encoder extends Object implements CounterBase, TelemetryLoggable, AutoCloseable
Class to read quadrature encoders.

Quadrature encoders are devices that count shaft rotation and can sense direction. The output of the Encoder class is an integer that can count either up or down, and can go negative for reverse direction counting. When creating Encoders, a direction can be supplied that inverts 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 for direction sensing.

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

  • Constructor Details

    • Encoder

      public Encoder(int channelA, int channelB, boolean reverseDirection)
      Encoder constructor. Construct a Encoder given a and b channels.

      The encoder will start counting immediately.

      Parameters:
      channelA - The 'a' SmartIO channel.
      channelB - The 'b' SmartIO channel.
      reverseDirection - represents the orientation of the encoder and inverts the output values if necessary so forward represents positive values.
    • Encoder

      public Encoder(int channelA, int channelB)
      Encoder constructor. Construct an Encoder given a and b channels.

      The encoder will start counting immediately.

      Parameters:
      channelA - The 'a' SmartIO channel.
      channelB - The 'b' SmartIO channel.
    • Encoder

      public Encoder(int channelA, int channelB, boolean reverseDirection, CounterBase.EncodingType encodingType)
      Encoder constructor. Construct an Encoder given a and b channels.

      The encoder will start counting immediately.

      Parameters:
      channelA - The 'a' SmartIO channel.
      channelB - The 'b' SmartIO channel.
      reverseDirection - represents the orientation of the encoder and inverts the output values if necessary so forward represents positive values.
      encodingType - either X1, X2, or X4 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.
  • Method Details

    • getFPGAIndex

      public int getFPGAIndex()
      Get the FPGA index of the encoder.
      Returns:
      The Encoder's FPGA index.
    • getEncodingScale

      public int getEncodingScale()
      Used to divide raw encoder counts down to the values returned by get().
      Returns:
      the scale factor used to convert raw encoder counts to scaled count values
    • close

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

      public int getRaw()
      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
    • get

      public int get()
      Gets the current count. Returns the current count on the Encoder. This method compensates for the decoding type.
      Specified by:
      get in interface CounterBase
      Returns:
      Current count from the Encoder adjusted for the 1x, 2x, or 4x scale factor.
    • reset

      public void reset()
      Reset the Encoder distance to zero. Resets the current count to zero on the encoder.
      Specified by:
      reset in interface CounterBase
    • getStopped

      public boolean getStopped()
      Determine if the encoder is stopped. The encoder is stopped when its current rate is zero.
      Specified by:
      getStopped in interface CounterBase
      Returns:
      True if the encoder is considered stopped.
    • getDirection

      public boolean getDirection()
      The last direction the encoder value changed.
      Specified by:
      getDirection in interface CounterBase
      Returns:
      The last direction the encoder value changed.
    • getDistance

      public double getDistance()
      Get the distance the robot has driven since the last reset as scaled by the value from setDistancePerPulse(double).
      Returns:
      The distance driven since the last reset
    • getRate

      public double getRate()
      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.
    • setRateWindow

      public void setRateWindow(int windowMilliseconds)
      Sets the time window used to calculate the encoder rate.
      Parameters:
      windowMilliseconds - The rate calculation window in milliseconds. Valid values are 5 through 255. The default is 50.
    • setDistancePerPulse

      public void setDistancePerPulse(double distancePerPulse)
      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.
    • getDistancePerPulse

      public double getDistancePerPulse()
      Get the distance per pulse for this encoder.
      Returns:
      The scale factor that will be used to convert pulses to useful units.
    • setReverseDirection

      public void setReverseDirection(boolean reverseDirection)
      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
    • setSimDevice

      public void setSimDevice(SimDevice device)
      Indicates this input is used by a simulated device.
      Parameters:
      device - simulated device handle
    • getDecodingScaleFactor

      public double getDecodingScaleFactor()
      Gets the decoding scale factor for scaling raw values to full counts.
      Returns:
      decoding scale factor
    • logTo

      public void logTo(TelemetryTable table)
      Description copied from interface: TelemetryLoggable
      Logs the object to a TelemetryTable.
      Specified by:
      logTo in interface TelemetryLoggable
      Parameters:
      table - telemetry table
    • getTelemetryType

      public String getTelemetryType()
      Description copied from interface: TelemetryLoggable
      Gets the telemetry table type. Default is no specified table type (null). A mismatch in type during logging results in a reported warning, so this should always return the same value.
      Specified by:
      getTelemetryType in interface TelemetryLoggable
      Returns:
      Table type