edu.wpi.first.wpilibj
Class Encoder

java.lang.Object
  extended by edu.wpi.first.wpilibj.SensorBase
      extended by edu.wpi.first.wpilibj.Encoder
All Implemented Interfaces:
CounterBase, IDevice, ISensor, PIDSource, FrcBotSimComponent

public class Encoder
extends SensorBase
implements CounterBase, PIDSource, ISensor

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.


Nested Class Summary
static class Encoder.PIDSourceParameter
           
 
Nested classes/interfaces inherited from interface edu.wpi.first.wpilibj.CounterBase
CounterBase.EncodingType
 
Field Summary
protected  DigitalSource m_aSource
          The a source
protected  DigitalSource m_bSource
          The b source
protected  DigitalSource m_indexSource
          The index source
 
Fields inherited from class edu.wpi.first.wpilibj.SensorBase
kAnalogChannels, kAnalogModules, kDigitalChannels, kPwmChannels, kRelayChannels, kSolenoidChannels, kSolenoidModules, kSystemClockTicksPerMicrosecond, n_simProperties
 
Constructor Summary
Encoder(DigitalSource aSource, DigitalSource bSource)
          Encoder constructor.
Encoder(DigitalSource aSource, DigitalSource bSource, boolean reverseDirection)
          Encoder constructor.
Encoder(DigitalSource aSource, DigitalSource bSource, boolean reverseDirection, CounterBase.EncodingType encodingType)
          Encoder constructor.
Encoder(DigitalSource aSource, DigitalSource bSource, DigitalSource indexSource)
          Encoder constructor.
Encoder(DigitalSource aSource, DigitalSource bSource, DigitalSource indexSource, boolean reverseDirection)
          Encoder constructor.
Encoder(int aChannel, int bChannel)
          Encoder constructor.
Encoder(int aChannel, int bChannel, boolean reverseDirection)
          Encoder constructor.
Encoder(int aChannel, int bChannel, boolean reverseDirection, CounterBase.EncodingType encodingType)
          Encoder constructor.
Encoder(int aChannel, int bChannel, int indexChannel)
          Encoder constructor.
Encoder(int aChannel, int bChannel, int indexChannel, boolean reverseDirection)
          Encoder constructor.
Encoder(int aSlot, int aChannel, int bSlot, int bChannel)
          Encoder constructor.
Encoder(int aSlot, int aChannel, int bSlot, int bChannel, boolean reverseDirection)
          Encoder constructor.
Encoder(int aSlot, int aChannel, int bSlot, int bChannel, boolean reverseDirection, CounterBase.EncodingType encodingType)
          Encoder constructor.
Encoder(int aSlot, int aChannel, int bSlot, int bChannel, int indexSlot, int indexChannel)
          Encoder constructor.
Encoder(int aSlot, int aChannel, int bSlot, int bChannel, int indexSlot, int indexChannel, boolean reverseDirection)
          Encoder constructor.
 
Method Summary
 void free()
          Free the resources used by this object
 int get()
          Gets the current count.
 boolean getDirection()
          The last direction the encoder value changed.
 double getDistance()
          Get the distance the robot has driven since the last reset.
 double getPeriod()
          Deprecated. Use getRate() in favor of this method. This returns unscaled periods and getRate() scales using value from setDistancePerPulse().
 double getRate()
          Get the current rate of the encoder.
 int getRaw()
          Gets the raw value from the encoder.
 boolean getStopped()
          Determine if the encoder is stopped.
 double pidGet()
          Implement the PIDSource interface.
 void reset()
          Reset the Encoder distance to zero.
 void setDistancePerPulse(double distancePerPulse)
          Set the distance per pulse for this encoder.
 void setMaxPeriod(double maxPeriod)
          Sets the maximum period for stopped detection.
 void setMinRate(double minRate)
          Set the minimum rate of the device before the hardware reports it stopped.
 void setPIDSourceParameter(Encoder.PIDSourceParameter pidSource)
          Set which parameter of the encoder you are using as a process control variable.
 void setReverseDirection(boolean reverseDirection)
          Set the direction sensing for this encoder.
 void start()
          Start the Encoder.
 void stop()
          Stops counting pulses on the Encoder device.
 
Methods inherited from class edu.wpi.first.wpilibj.SensorBase
checkAnalogChannel, checkAnalogModule, checkDigitalChannel, checkDigitalModule, checkPWMChannel, checkPWMModule, checkRelayChannel, checkRelayModule, checkSolenoidChannel, checkSolenoidModule, getDefaultAnalogModule, getDefaultDigitalModule, getDefaultSolenoidModule, getSimProperties, setDefaultAnalogModule, setDefaultDigitalModule, setDefaultSolenoidModule
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

m_aSource

protected DigitalSource m_aSource
The a source


m_bSource

protected DigitalSource m_bSource
The b source


m_indexSource

protected DigitalSource m_indexSource
The index source

Constructor Detail

Encoder

public Encoder(int aSlot,
               int aChannel,
               int bSlot,
               int bChannel,
               boolean reverseDirection)
Encoder constructor. Construct a Encoder given a and b modules and channels fully specified.

Parameters:
aSlot - The a channel digital input module.
aChannel - The a channel digital input channel.
bSlot - The b channel digital input module.
bChannel - The b channel digital input channel.
reverseDirection - represents the orientation of the encoder and inverts the output values if necessary so forward represents positive values.

Encoder

public Encoder(int aSlot,
               int aChannel,
               int bSlot,
               int bChannel)
Encoder constructor. Construct a Encoder given a and b modules and channels fully specified.

Parameters:
aSlot - The a channel digital input module.
aChannel - The a channel digital input channel.
bSlot - The b channel digital input module.
bChannel - The b channel digital input channel.

Encoder

public Encoder(int aSlot,
               int aChannel,
               int bSlot,
               int bChannel,
               boolean reverseDirection,
               CounterBase.EncodingType encodingType)
Encoder constructor. Construct a Encoder given a and b modules and channels fully specified.

Parameters:
aSlot - The a channel digital input module.
aChannel - The a channel digital input channel.
bSlot - The b channel digital input module.
bChannel - The b channel digital input channel.
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.

Encoder

public Encoder(int aSlot,
               int aChannel,
               int bSlot,
               int bChannel,
               int indexSlot,
               int indexChannel,
               boolean reverseDirection)
Encoder constructor. Construct a Encoder given a and b modules and channels fully specified. Using the index pulse forces 4x encoding.

Parameters:
aSlot - The a channel digital input module.
aChannel - The a channel digital input channel.
bSlot - The b channel digital input module.
bChannel - The b channel digital input channel.
indexSlot - The index channel digital input module.
indexChannel - The index channel digital input channel.
reverseDirection - represents the orientation of the encoder and inverts the output values if necessary so forward represents positive values.

Encoder

public Encoder(int aSlot,
               int aChannel,
               int bSlot,
               int bChannel,
               int indexSlot,
               int indexChannel)
Encoder constructor. Construct a Encoder given a and b modules and channels fully specified. Using the index pulse forces 4x encoding.

Parameters:
aSlot - The a channel digital input module.
aChannel - The a channel digital input channel.
bSlot - The b channel digital input module.
bChannel - The b channel digital input channel.
indexSlot - The index channel digital input module.
indexChannel - The index channel digital input channel.

Encoder

public Encoder(int aChannel,
               int bChannel,
               boolean reverseDirection)
Encoder constructor. Construct a Encoder given a and b channels assuming the default module.

Parameters:
aChannel - The a channel digital input channel.
bChannel - The b channel digital input channel.
reverseDirection - represents the orientation of the encoder and inverts the output values if necessary so forward represents positive values.

Encoder

public Encoder(int aChannel,
               int bChannel)
Encoder constructor. Construct a Encoder given a and b channels assuming the default module.

Parameters:
aChannel - The a channel digital input channel.
bChannel - The b channel digital input channel.

Encoder

public Encoder(int aChannel,
               int bChannel,
               boolean reverseDirection,
               CounterBase.EncodingType encodingType)
Encoder constructor. Construct a Encoder given a and b channels assuming the default module.

Parameters:
aChannel - The a channel digital input channel.
bChannel - The b channel digital input channel.
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.

Encoder

public Encoder(int aChannel,
               int bChannel,
               int indexChannel,
               boolean reverseDirection)
Encoder constructor. Construct a Encoder given a and b channels assuming the default module. Using an index pulse forces 4x encoding

Parameters:
aChannel - The a channel digital input channel.
bChannel - The b channel digital input channel.
indexChannel - The index channel digital input channel.
reverseDirection - represents the orientation of the encoder and inverts the output values if necessary so forward represents positive values.

Encoder

public Encoder(int aChannel,
               int bChannel,
               int indexChannel)
Encoder constructor. Construct a Encoder given a and b channels assuming the default module. Using an index pulse forces 4x encoding

Parameters:
aChannel - The a channel digital input channel.
bChannel - The b channel digital input channel.
indexChannel - The index channel digital input channel.

Encoder

public Encoder(DigitalSource aSource,
               DigitalSource bSource,
               boolean reverseDirection)
Encoder constructor. Construct a Encoder given a and b channels as digital inputs. This is used in the case where the digital inputs are shared. The Encoder class will not allocate the digital inputs and assume that they already are counted.

Parameters:
aSource - The source that should be used for the a channel.
bSource - the source that should be used for the b channel.
reverseDirection - represents the orientation of the encoder and inverts the output values if necessary so forward represents positive values.

Encoder

public Encoder(DigitalSource aSource,
               DigitalSource bSource)
Encoder constructor. Construct a Encoder given a and b channels as digital inputs. This is used in the case where the digital inputs are shared. The Encoder class will not allocate the digital inputs and assume that they already are counted.

Parameters:
aSource - The source that should be used for the a channel.
bSource - the source that should be used for the b channel.

Encoder

public Encoder(DigitalSource aSource,
               DigitalSource bSource,
               boolean reverseDirection,
               CounterBase.EncodingType encodingType)
Encoder constructor. Construct a Encoder given a and b channels as digital inputs. This is used in the case where the digital inputs are shared. The Encoder class will not allocate the digital inputs and assume that they already are counted.

Parameters:
aSource - The source that should be used for the a channel.
bSource - the source that should be used for the b channel.
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.

Encoder

public Encoder(DigitalSource aSource,
               DigitalSource bSource,
               DigitalSource indexSource,
               boolean reverseDirection)
Encoder constructor. Construct a Encoder given a and b channels as digital inputs. This is used in the case where the digital inputs are shared. The Encoder class will not allocate the digital inputs and assume that they already are counted.

Parameters:
aSource - The source that should be used for the a channel.
bSource - the source that should be used for the b channel.
indexSource - the source that should be used for the index channel.
reverseDirection - represents the orientation of the encoder and inverts the output values if necessary so forward represents positive values.

Encoder

public Encoder(DigitalSource aSource,
               DigitalSource bSource,
               DigitalSource indexSource)
Encoder constructor. Construct a Encoder given a and b channels as digital inputs. This is used in the case where the digital inputs are shared. The Encoder class will not allocate the digital inputs and assume that they already are counted.

Parameters:
aSource - The source that should be used for the a channel.
bSource - the source that should be used for the b channel.
indexSource - the source that should be used for the index channel.
Method Detail

free

public void free()
Description copied from class: SensorBase
Free the resources used by this object

Overrides:
free in class SensorBase

start

public void start()
Start the Encoder. Starts counting pulses on the Encoder device.

Specified by:
start in interface CounterBase

stop

public void stop()
Stops counting pulses on the Encoder device. The value is not changed.

Specified by:
stop in interface CounterBase

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

getPeriod

public double getPeriod()
Deprecated. Use getRate() in favor of this method. This returns unscaled periods and getRate() scales using value from setDistancePerPulse().

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.

Specified by:
getPeriod in interface CounterBase
Returns:
Period in seconds of the most recent pulse.

setMaxPeriod

public void setMaxPeriod(double maxPeriod)
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.

Specified by:
setMaxPeriod in interface CounterBase
Parameters:
maxPeriod - The maximum time between rising and falling edges before the FPGA will report the device stopped. This is expressed in seconds.

getStopped

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

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.

Returns:
The distance driven since the last reset as scaled by the value from setDistancePerPulse().

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.

setMinRate

public void setMinRate(double minRate)
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().

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.

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

setPIDSourceParameter

public void setPIDSourceParameter(Encoder.PIDSourceParameter pidSource)
Set which parameter of the encoder you are using as a process control variable.

Parameters:
pidSource - An enum to select the parameter.

pidGet

public double pidGet()
Implement the PIDSource interface.

Specified by:
pidGet in interface PIDSource
Returns:
The current value of the selected source parameter.