public class Encoder extends SensorBase implements CounterBase, PIDSource, Sendable
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.
Modifier and Type | Class and Description |
---|---|
static class |
Encoder.IndexingType |
CounterBase.EncodingType
Modifier and Type | Field and Description |
---|---|
protected DigitalSource |
m_aSource
The a source.
|
protected DigitalSource |
m_bSource
The b source.
|
protected DigitalSource |
m_indexSource
The index source.
|
kAnalogInputChannels, kAnalogOutputChannels, kDigitalChannels, kPCMModules, kPDPChannels, kPDPModules, kPwmChannels, kRelayChannels, kSolenoidChannels, kSystemClockTicksPerMicrosecond
Constructor and Description |
---|
Encoder(DigitalSource sourceA,
DigitalSource sourceB)
Encoder constructor.
|
Encoder(DigitalSource sourceA,
DigitalSource sourceB,
boolean reverseDirection)
Encoder constructor.
|
Encoder(DigitalSource sourceA,
DigitalSource sourceB,
boolean reverseDirection,
CounterBase.EncodingType encodingType)
Encoder constructor.
|
Encoder(DigitalSource sourceA,
DigitalSource sourceB,
DigitalSource indexSource)
Encoder constructor.
|
Encoder(DigitalSource sourceA,
DigitalSource sourceB,
DigitalSource indexSource,
boolean reverseDirection)
Encoder constructor.
|
Encoder(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.
|
Encoder(int channelA,
int channelB,
int indexChannel)
Encoder constructor.
|
Encoder(int channelA,
int channelB,
int indexChannel,
boolean reverseDirection)
Encoder constructor.
|
Modifier and Type | Method and Description |
---|---|
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 as scaled by the value from
setDistancePerPulse(double) . |
double |
getDistancePerPulse()
Get the distance per pulse for this encoder.
|
int |
getEncodingScale()
Used to divide raw edge counts down to spec'd counts.
|
int |
getFPGAIndex()
Get the FPGA index of the encoder.
|
double |
getPeriod()
Deprecated.
Use getRate() in favor of this method.
|
PIDSourceType |
getPIDSourceType()
Get which parameter of the device you are using as a process control variable.
|
double |
getRate()
Get the current rate of the encoder.
|
int |
getRaw()
Gets the raw value from the encoder.
|
int |
getSamplesToAverage()
Get the Samples to Average which specifies the number of samples of the timer to average when
calculating the period.
|
boolean |
getStopped()
Determine if the encoder is stopped.
|
void |
initSendable(SendableBuilder builder)
Initializes this
Sendable object. |
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 |
setIndexSource(DigitalSource source)
Set the index source for the encoder.
|
void |
setIndexSource(DigitalSource source,
Encoder.IndexingType type)
Set the index source for the encoder.
|
void |
setIndexSource(int channel)
Set the index source for the encoder.
|
void |
setIndexSource(int channel,
Encoder.IndexingType type)
Set the index source for the 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 |
setPIDSourceType(PIDSourceType 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 |
setSamplesToAverage(int samplesToAverage)
Set the Samples to Average which specifies the number of samples of the timer to average when
calculating the period.
|
checkAnalogInputChannel, checkAnalogOutputChannel, checkDigitalChannel, checkPDPChannel, checkPDPModule, checkPWMChannel, checkRelayChannel, checkSolenoidChannel, checkSolenoidModule, getDefaultSolenoidModule, setDefaultSolenoidModule
addChild, getName, getSubsystem, setName, setName, setName, setSubsystem
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
getName, getSubsystem, setName, setName, setSubsystem
protected DigitalSource m_aSource
protected DigitalSource m_bSource
protected DigitalSource m_indexSource
public Encoder(int channelA, int channelB, boolean reverseDirection)
The encoder will start counting immediately.
channelA
- The a channel DIO channel. 0-9 are on-board, 10-25 are on the MXP portchannelB
- The b channel DIO channel. 0-9 are on-board, 10-25 are on the MXP portreverseDirection
- represents the orientation of the encoder and inverts the output values
if necessary so forward represents positive values.public Encoder(int channelA, int channelB)
The encoder will start counting immediately.
channelA
- The a channel digital input channel.channelB
- The b channel digital input channel.public Encoder(int channelA, int channelB, boolean reverseDirection, CounterBase.EncodingType encodingType)
The encoder will start counting immediately.
channelA
- The a channel digital input channel.channelB
- 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 m_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.public Encoder(int channelA, int channelB, int indexChannel, boolean reverseDirection)
The encoder will start counting immediately.
channelA
- The a channel digital input channel.channelB
- 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.public Encoder(int channelA, int channelB, int indexChannel)
The encoder will start counting immediately.
channelA
- The a channel digital input channel.channelB
- The b channel digital input channel.indexChannel
- The index channel digital input channel.public Encoder(DigitalSource sourceA, DigitalSource sourceB, boolean reverseDirection)
The encoder will start counting immediately.
sourceA
- The source that should be used for the a channel.sourceB
- 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.public Encoder(DigitalSource sourceA, DigitalSource sourceB)
The encoder will start counting immediately.
sourceA
- The source that should be used for the a channel.sourceB
- the source that should be used for the b channel.public Encoder(DigitalSource sourceA, DigitalSource sourceB, boolean reverseDirection, CounterBase.EncodingType encodingType)
The encoder will start counting immediately.
sourceA
- The source that should be used for the a channel.sourceB
- 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 m_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.public Encoder(DigitalSource sourceA, DigitalSource sourceB, DigitalSource indexSource, boolean reverseDirection)
The encoder will start counting immediately.
sourceA
- The source that should be used for the a channel.sourceB
- 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.public Encoder(DigitalSource sourceA, DigitalSource sourceB, DigitalSource indexSource)
The encoder will start counting immediately.
sourceA
- The source that should be used for the a channel.sourceB
- the source that should be used for the b channel.indexSource
- the source that should be used for the index channel.public int getFPGAIndex()
public int getEncodingScale()
public void free()
free
in class SendableBase
public int getRaw()
public int get()
get
in interface CounterBase
public void reset()
reset
in interface CounterBase
@Deprecated public double getPeriod()
Warning: This returns unscaled periods. Use getRate() for rates that are scaled using the value from setDistancePerPulse().
getPeriod
in interface CounterBase
public void setMaxPeriod(double maxPeriod)
setMaxPeriod
in interface CounterBase
maxPeriod
- The maximum time between rising and falling edges before the FPGA will report
the device stopped. This is expressed in seconds.public boolean getStopped()
getStopped
in interface CounterBase
public boolean getDirection()
getDirection
in interface CounterBase
public double getDistance()
setDistancePerPulse(double)
.public double getRate()
public void setMinRate(double minRate)
minRate
- The minimum rate. The units are in distance per second as scaled by the value
from setDistancePerPulse().public void setDistancePerPulse(double distancePerPulse)
distancePerPulse
- The scale factor that will be used to convert pulses to useful units.public double getDistancePerPulse()
public void setReverseDirection(boolean reverseDirection)
reverseDirection
- true if the encoder direction should be reversedpublic void setSamplesToAverage(int samplesToAverage)
samplesToAverage
- The number of samples to average from 1 to 127.public int getSamplesToAverage()
public void setPIDSourceType(PIDSourceType pidSource)
setPIDSourceType
in interface PIDSource
pidSource
- An enum to select the parameter.public PIDSourceType getPIDSourceType()
PIDSource
getPIDSourceType
in interface PIDSource
public double pidGet()
public void setIndexSource(int channel)
channel
- A DIO channel to set as the encoder indexpublic void setIndexSource(DigitalSource source)
source
- A digital source to set as the encoder indexpublic void setIndexSource(int channel, Encoder.IndexingType type)
channel
- A DIO channel to set as the encoder indextype
- The state that will cause the encoder to resetpublic void setIndexSource(DigitalSource source, Encoder.IndexingType type)
source
- A digital source to set as the encoder indextype
- The state that will cause the encoder to resetpublic void initSendable(SendableBuilder builder)
Sendable
Sendable
object.initSendable
in interface Sendable
builder
- sendable builder