public class Encoder extends SensorBase implements CounterBase, PIDSource, LiveWindowSendable
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) . |
int |
getEncodingScale()
Used to divide raw edge counts down to spec'd counts.
|
int |
getFPGAIndex() |
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.
|
java.lang.String |
getSmartDashboardType()
Live Window code, only does anything if live window is activated.
|
boolean |
getStopped()
Determine if the encoder is stopped.
|
ITable |
getTable()
The table that is associated with this
Sendable . |
void |
initTable(ITable subtable)
Initializes a table for 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.
|
void |
startLiveWindowMode()
Start having this sendable object automatically respond to value changes reflect the value on
the table.
|
void |
stopLiveWindowMode()
Stop having this sendable object automatically respond to value changes.
|
void |
updateTable()
Update the table for this sendable object with the latest values.
|
checkAnalogInputChannel, checkAnalogOutputChannel, checkDigitalChannel, checkPDPChannel, checkPDPModule, checkPWMChannel, checkRelayChannel, checkSolenoidChannel, checkSolenoidModule, getDefaultSolenoidModule, setDefaultSolenoidModule
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 SensorBase
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 and getRate() scales using 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 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 java.lang.String getSmartDashboardType()
getSmartDashboardType
in interface Sendable
Sendable
.public void initTable(ITable subtable)
Sendable
Sendable
object.public ITable getTable()
Sendable
Sendable
.public void updateTable()
LiveWindowSendable
updateTable
in interface LiveWindowSendable
public void startLiveWindowMode()
LiveWindowSendable
startLiveWindowMode
in interface LiveWindowSendable
public void stopLiveWindowMode()
LiveWindowSendable
stopLiveWindowMode
in interface LiveWindowSendable