Class Encoder
- All Implemented Interfaces:
AutoCloseable, CounterBase, TelemetryLoggable
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.
-
Nested Class Summary
Nested classes/interfaces inherited from interface CounterBase
CounterBase.EncodingType -
Constructor Summary
ConstructorsConstructorDescriptionEncoder(int channelA, int channelB) Encoder constructor.Encoder(int channelA, int channelB, boolean reverseDirection) Encoder constructor.Encoder(int channelA, int channelB, boolean reverseDirection, CounterBase.EncodingType encodingType) Encoder constructor. -
Method Summary
Modifier and TypeMethodDescriptionvoidclose()intget()Gets the current count.doubleGets the decoding scale factor for scaling raw values to full counts.booleanThe last direction the encoder value changed.doubleGet the distance the robot has driven since the last reset as scaled by the value fromsetDistancePerPulse(double).doubleGet the distance per pulse for this encoder.intUsed to divide raw encoder counts down to the values returned by get().intGet the FPGA index of the encoder.doublegetRate()Get the current rate of the encoder.intgetRaw()Gets the raw value from the encoder.booleanDetermine if the encoder is stopped.Gets the telemetry table type.voidlogTo(TelemetryTable table) Logs the object to aTelemetryTable.voidreset()Reset the Encoder distance to zero.voidsetDistancePerPulse(double distancePerPulse) Set the distance per pulse for this encoder.voidsetRateWindow(int windowMilliseconds) Sets the time window used to calculate the encoder rate.voidsetReverseDirection(boolean reverseDirection) Set the direction sensing for this encoder.voidsetSimDevice(SimDevice device) Indicates this input is used by a simulated device.
-
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:
closein interfaceAutoCloseable
-
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:
getin interfaceCounterBase- 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:
resetin interfaceCounterBase
-
getStopped
public boolean getStopped()Determine if the encoder is stopped. The encoder is stopped when its current rate is zero.- Specified by:
getStoppedin interfaceCounterBase- Returns:
- True if the encoder is considered stopped.
-
getDirection
public boolean getDirection()The last direction the encoder value changed.- Specified by:
getDirectionin interfaceCounterBase- 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 fromsetDistancePerPulse(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
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
Description copied from interface:TelemetryLoggableLogs the object to aTelemetryTable.- Specified by:
logToin interfaceTelemetryLoggable- Parameters:
table- telemetry table
-
getTelemetryType
Description copied from interface:TelemetryLoggableGets 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:
getTelemetryTypein interfaceTelemetryLoggable- Returns:
- Table type
-