WPILibC++  unspecified
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Pages
Encoder Class Reference

Class to read quad encoders. More...

#include <Encoder.h>

Inheritance diagram for Encoder:
SensorBase CounterBase PIDSource LiveWindowSendable ErrorBase Sendable

Public Types

enum  IndexingType { kResetWhileHigh, kResetWhileLow, kResetOnFallingEdge, kResetOnRisingEdge }
 
- Public Types inherited from CounterBase
enum  EncodingType { k1X, k2X, k4X }
 

Public Member Functions

 Encoder (uint32_t aChannel, uint32_t bChannel, bool reverseDirection=false, EncodingType encodingType=k4X)
 Encoder constructor. More...
 
 Encoder (std::shared_ptr< DigitalSource > aSource, std::shared_ptr< DigitalSource > bSource, bool reverseDirection=false, EncodingType encodingType=k4X)
 
 Encoder (DigitalSource *aSource, DigitalSource *bSource, bool reverseDirection=false, EncodingType encodingType=k4X)
 Encoder constructor. More...
 
 Encoder (DigitalSource &aSource, DigitalSource &bSource, bool reverseDirection=false, EncodingType encodingType=k4X)
 Encoder constructor. More...
 
virtual ~Encoder ()
 Free the resources for an Encoder. More...
 
int32_t Get () const override
 Gets the current count. More...
 
int32_t GetRaw () const
 Gets the raw value from the encoder. More...
 
int32_t GetEncodingScale () const
 The encoding scale factor 1x, 2x, or 4x, per the requested encodingType. More...
 
void Reset () override
 Reset the Encoder distance to zero. More...
 
double GetPeriod () const override
 Returns the period of the most recent pulse. More...
 
void SetMaxPeriod (double maxPeriod) override
 Sets the maximum period for stopped detection. More...
 
bool GetStopped () const override
 Determine if the encoder is stopped. More...
 
bool GetDirection () const override
 The last direction the encoder value changed. More...
 
double GetDistance () const
 Get the distance the robot has driven since the last reset. More...
 
double GetRate () const
 Get the current rate of the encoder. More...
 
void SetMinRate (double minRate)
 Set the minimum rate of the device before the hardware reports it stopped. More...
 
void SetDistancePerPulse (double distancePerPulse)
 Set the distance per pulse for this encoder. More...
 
void SetReverseDirection (bool reverseDirection)
 Set the direction sensing for this encoder. More...
 
void SetSamplesToAverage (int samplesToAverage)
 Set the Samples to Average which specifies the number of samples of the timer to average when calculating the period. More...
 
int GetSamplesToAverage () const
 Get the Samples to Average which specifies the number of samples of the timer to average when calculating the period. More...
 
double PIDGet () override
 Implement the PIDSource interface. More...
 
void SetIndexSource (uint32_t channel, IndexingType type=kResetOnRisingEdge)
 Set the index source for the encoder. More...
 
void SetIndexSource (DigitalSource *source, IndexingType type=kResetOnRisingEdge)
 Set the index source for the encoder. More...
 
void SetIndexSource (const DigitalSource &source, IndexingType type=kResetOnRisingEdge)
 Set the index source for the encoder. More...
 
void UpdateTable () override
 Update the table for this sendable object with the latest values.
 
void StartLiveWindowMode () override
 Start having this sendable object automatically respond to value changes reflect the value on the table.
 
void StopLiveWindowMode () override
 Stop having this sendable object automatically respond to value changes.
 
std::string GetSmartDashboardType () const override
 
void InitTable (std::shared_ptr< ITable > subTable) override
 Initializes a table for this sendable object. More...
 
std::shared_ptr< ITableGetTable () const override
 
int32_t GetFPGAIndex () const
 
- Public Member Functions inherited from SensorBase
 SensorBase ()
 Creates an instance of the sensor base and gets an FPGA handle.
 
 SensorBase (const SensorBase &)=delete
 
SensorBaseoperator= (const SensorBase &)=delete
 
- Public Member Functions inherited from ErrorBase
 ErrorBase (const ErrorBase &)=delete
 
ErrorBaseoperator= (const ErrorBase &)=delete
 
virtual ErrorGetError ()
 Retrieve the current error. More...
 
virtual const ErrorGetError () const
 
virtual void SetErrnoError (llvm::StringRef contextMessage, llvm::StringRef filename, llvm::StringRef function, uint32_t lineNumber) const
 Set error information associated with a C library call that set an error to the "errno" global variable. More...
 
virtual void SetImaqError (int success, llvm::StringRef contextMessage, llvm::StringRef filename, llvm::StringRef function, uint32_t lineNumber) const
 Set the current error information associated from the nivision Imaq API. More...
 
virtual void SetError (Error::Code code, llvm::StringRef contextMessage, llvm::StringRef filename, llvm::StringRef function, uint32_t lineNumber) const
 Set the current error information associated with this sensor. More...
 
virtual void SetWPIError (llvm::StringRef errorMessage, Error::Code code, llvm::StringRef contextMessage, llvm::StringRef filename, llvm::StringRef function, uint32_t lineNumber) const
 Set the current error information associated with this sensor. More...
 
virtual void CloneError (const ErrorBase &rhs) const
 
virtual void ClearError () const
 Clear the current error information associated with this sensor.
 
virtual bool StatusIsFatal () const
 Check if the current error code represents a fatal error. More...
 
- Public Member Functions inherited from PIDSource
virtual void SetPIDSourceType (PIDSourceType pidSource)
 Set which parameter you are using as a process control variable. More...
 
PIDSourceType GetPIDSourceType () const
 

Friends

class DigitalGlitchFilter
 

Additional Inherited Members

- Static Public Member Functions inherited from SensorBase
static void DeleteSingletons ()
 Delete all the singleton classes on the list. More...
 
static uint32_t GetDefaultSolenoidModule ()
 
static bool CheckSolenoidModule (uint8_t moduleNumber)
 Check that the solenoid module number is valid. More...
 
static bool CheckDigitalChannel (uint32_t channel)
 Check that the digital channel number is valid. More...
 
static bool CheckRelayChannel (uint32_t channel)
 Check that the digital channel number is valid. More...
 
static bool CheckPWMChannel (uint32_t channel)
 Check that the digital channel number is valid. More...
 
static bool CheckAnalogInput (uint32_t channel)
 Check that the analog input number is value. More...
 
static bool CheckAnalogOutput (uint32_t channel)
 Check that the analog output number is valid. More...
 
static bool CheckSolenoidChannel (uint32_t channel)
 Verify that the solenoid channel number is within limits. More...
 
static bool CheckPDPChannel (uint32_t channel)
 Verify that the power distribution channel number is within limits. More...
 
- Static Public Member Functions inherited from ErrorBase
static void SetGlobalError (Error::Code code, llvm::StringRef contextMessage, llvm::StringRef filename, llvm::StringRef function, uint32_t lineNumber)
 
static void SetGlobalWPIError (llvm::StringRef errorMessage, llvm::StringRef contextMessage, llvm::StringRef filename, llvm::StringRef function, uint32_t lineNumber)
 
static ErrorGetGlobalError ()
 Retrieve the current global error.
 
- Static Public Attributes inherited from SensorBase
static const uint32_t kDigitalChannels = 26
 
static const uint32_t kAnalogInputs = 8
 
static const uint32_t kAnalogOutputs = 2
 
static const uint32_t kSolenoidChannels = 8
 
static const uint32_t kSolenoidModules = 2
 
static const uint32_t kPwmChannels = 20
 
static const uint32_t kRelayChannels = 8
 
static const uint32_t kPDPChannels = 16
 
static const uint32_t kChassisSlots = 8
 
- Protected Member Functions inherited from SensorBase
void AddToSingletonList ()
 Add sensor to the singleton list. More...
 
- Protected Attributes inherited from ErrorBase
Error m_error
 
- Protected Attributes inherited from PIDSource
PIDSourceType m_pidSource = PIDSourceType::kDisplacement
 
- Static Protected Attributes inherited from SensorBase
static void * m_digital_ports [kDigitalChannels]
 
static void * m_relay_ports [kRelayChannels]
 
static void * m_pwm_ports [kPwmChannels]
 
- Static Protected Attributes inherited from ErrorBase
static priority_mutex _globalErrorMutex
 
static Error _globalError
 

Detailed Description

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.

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

Constructor & Destructor Documentation

Encoder::Encoder ( uint32_t  aChannel,
uint32_t  bChannel,
bool  reverseDirection = false,
EncodingType  encodingType = k4X 
)

Encoder constructor.

Construct a Encoder given a and b channels.

The counter will start counting immediately.

Parameters
aChannelThe a channel DIO channel. 0-9 are on-board, 10-25 are on the MXP port
bChannelThe b channel DIO channel. 0-9 are on-board, 10-25 are on the MXP port
reverseDirectionrepresents the orientation of the encoder and inverts the output values if necessary so forward represents positive values.
encodingTypeeither 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::Encoder ( DigitalSource aSource,
DigitalSource bSource,
bool  reverseDirection = false,
EncodingType  encodingType = k4X 
)

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. The counter will start counting immediately.

Parameters
aSourceThe source that should be used for the a channel.
bSourcethe source that should be used for the b channel.
reverseDirectionrepresents the orientation of the encoder and inverts the output values if necessary so forward represents positive values.
encodingTypeeither 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::Encoder ( DigitalSource aSource,
DigitalSource bSource,
bool  reverseDirection = false,
EncodingType  encodingType = k4X 
)

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.

The counter will start counting immediately.

Parameters
aSourceThe source that should be used for the a channel.
bSourcethe source that should be used for the b channel.
reverseDirectionrepresents the orientation of the encoder and inverts the output values if necessary so forward represents positive values.
encodingTypeeither 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::~Encoder ( )
virtual

Free the resources for an Encoder.

Frees the FPGA resources associated with an Encoder.

Member Function Documentation

int32_t Encoder::Get ( ) const
overridevirtual

Gets the current count.

Returns the current count on the Encoder. This method compensates for the decoding type.

Returns
Current count from the Encoder adjusted for the 1x, 2x, or 4x scale factor.

Implements CounterBase.

bool Encoder::GetDirection ( ) const
overridevirtual

The last direction the encoder value changed.

Returns
The last direction the encoder value changed.

Implements CounterBase.

double Encoder::GetDistance ( ) const

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().
int32_t Encoder::GetEncodingScale ( ) const

The encoding scale factor 1x, 2x, or 4x, per the requested encodingType.

Used to divide raw edge counts down to spec'd counts.

double Encoder::GetPeriod ( ) const
overridevirtual

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.

Deprecated:
Use GetRate() in favor of this method. This returns unscaled periods and GetRate() scales using value from SetDistancePerPulse().
Returns
Period in seconds of the most recent pulse.

Implements CounterBase.

double Encoder::GetRate ( ) const

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.
int32_t Encoder::GetRaw ( ) const

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
int Encoder::GetSamplesToAverage ( ) const

Get the Samples to Average which specifies the number of samples of the timer to average when calculating the period.

Perform averaging to account for mechanical imperfections or as oversampling to increase resolution.

Returns
SamplesToAverage The number of samples being averaged (from 1 to 127)
std::string Encoder::GetSmartDashboardType ( ) const
overridevirtual
Returns
the string representation of the named data type that will be used by the smart dashboard for this sendable

Implements Sendable.

bool Encoder::GetStopped ( ) const
overridevirtual

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.

Returns
True if the encoder is considered stopped.

Implements CounterBase.

std::shared_ptr< ITable > Encoder::GetTable ( ) const
overridevirtual
Returns
the table that is currently associated with the sendable

Implements Sendable.

void Encoder::InitTable ( std::shared_ptr< ITable subtable)
overridevirtual

Initializes a table for this sendable object.

Parameters
subtableThe table to put the values in.

Implements Sendable.

double Encoder::PIDGet ( )
overridevirtual

Implement the PIDSource interface.

Returns
The current value of the selected source parameter.

Implements PIDSource.

void Encoder::Reset ( )
overridevirtual

Reset the Encoder distance to zero.

Resets the current count to zero on the encoder.

Implements CounterBase.

void Encoder::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
distancePerPulseThe scale factor that will be used to convert pulses to useful units.
void Encoder::SetIndexSource ( uint32_t  channel,
Encoder::IndexingType  type = kResetOnRisingEdge 
)

Set the index source for the encoder.

When this source is activated, the encoder count automatically resets.

Parameters
channelA DIO channel to set as the encoder index
typeThe state that will cause the encoder to reset
void Encoder::SetIndexSource ( DigitalSource source,
Encoder::IndexingType  type = kResetOnRisingEdge 
)

Set the index source for the encoder.

When this source is activated, the encoder count automatically resets.

Parameters
channelA digital source to set as the encoder index
typeThe state that will cause the encoder to reset
void Encoder::SetIndexSource ( const DigitalSource source,
Encoder::IndexingType  type = kResetOnRisingEdge 
)

Set the index source for the encoder.

When this source is activated, the encoder count automatically resets.

Parameters
channelA digital source to set as the encoder index
typeThe state that will cause the encoder to reset
void Encoder::SetMaxPeriod ( double  maxPeriod)
overridevirtual

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.

Deprecated:
Use SetMinRate() in favor of this method. This takes unscaled periods and SetMinRate() scales using value from SetDistancePerPulse().
Parameters
maxPeriodThe maximum time between rising and falling edges before the FPGA will report the device stopped. This is expressed in seconds.

Implements CounterBase.

void Encoder::SetMinRate ( double  minRate)

Set the minimum rate of the device before the hardware reports it stopped.

Parameters
minRateThe minimum rate. The units are in distance per second as scaled by the value from SetDistancePerPulse().
void Encoder::SetReverseDirection ( bool  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
reverseDirectiontrue if the encoder direction should be reversed
void Encoder::SetSamplesToAverage ( int  samplesToAverage)

Set the Samples to Average which specifies the number of samples of the timer to average when calculating the period.

Perform averaging to account for mechanical imperfections or as oversampling to increase resolution.

Parameters
samplesToAverageThe number of samples to average from 1 to 127.

The documentation for this class was generated from the following files: