2013-12-15 18:30:16 -05:00
|
|
|
/*----------------------------------------------------------------------------*/
|
2019-08-25 18:42:00 -07:00
|
|
|
/* Copyright (c) 2008-2019 FIRST. All Rights Reserved. */
|
2013-12-15 18:30:16 -05:00
|
|
|
/* Open Source Software - may be modified and shared by FRC teams. The code */
|
2016-01-02 03:02:34 -08:00
|
|
|
/* must be accompanied by the FIRST BSD license file in the root directory of */
|
|
|
|
|
/* the project. */
|
2013-12-15 18:30:16 -05:00
|
|
|
/*----------------------------------------------------------------------------*/
|
2016-01-02 03:02:34 -08:00
|
|
|
|
2014-05-02 17:54:01 -04:00
|
|
|
#pragma once
|
2013-12-15 18:30:16 -05:00
|
|
|
|
2016-05-25 22:38:11 -07:00
|
|
|
#include <memory>
|
|
|
|
|
|
2018-10-29 12:49:17 -07:00
|
|
|
#include <hal/Types.h>
|
2017-08-27 00:11:52 -07:00
|
|
|
|
2018-07-20 00:03:45 -07:00
|
|
|
#include "frc/Counter.h"
|
|
|
|
|
#include "frc/CounterBase.h"
|
|
|
|
|
#include "frc/ErrorBase.h"
|
|
|
|
|
#include "frc/PIDSource.h"
|
|
|
|
|
#include "frc/smartdashboard/SendableBase.h"
|
2013-12-15 18:30:16 -05:00
|
|
|
|
2016-11-01 22:33:12 -07:00
|
|
|
namespace frc {
|
|
|
|
|
|
2013-12-15 18:30:16 -05:00
|
|
|
class DigitalSource;
|
2015-11-22 21:18:59 -08:00
|
|
|
class DigitalGlitchFilter;
|
2013-12-15 18:30:16 -05:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Class to read quad encoders.
|
2017-07-01 01:05:33 -04:00
|
|
|
*
|
2015-06-25 15:07:55 -04:00
|
|
|
* Quadrature encoders are devices that count shaft rotation and can sense
|
2016-05-20 17:30:37 -07:00
|
|
|
* 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.
|
2014-07-29 09:58:15 -07:00
|
|
|
*
|
|
|
|
|
* All encoders will immediately start counting - Reset() them if you need them
|
|
|
|
|
* to be zeroed before use.
|
2013-12-15 18:30:16 -05:00
|
|
|
*/
|
2018-05-23 20:22:30 -07:00
|
|
|
class Encoder : public ErrorBase,
|
|
|
|
|
public SendableBase,
|
|
|
|
|
public CounterBase,
|
|
|
|
|
public PIDSource {
|
2015-06-25 15:07:55 -04:00
|
|
|
public:
|
|
|
|
|
enum IndexingType {
|
|
|
|
|
kResetWhileHigh,
|
|
|
|
|
kResetWhileLow,
|
|
|
|
|
kResetOnFallingEdge,
|
|
|
|
|
kResetOnRisingEdge
|
|
|
|
|
};
|
2013-12-15 18:30:16 -05:00
|
|
|
|
2018-05-31 20:47:15 -07:00
|
|
|
/**
|
|
|
|
|
* Encoder constructor.
|
|
|
|
|
*
|
|
|
|
|
* Construct a Encoder given a and b channels.
|
|
|
|
|
*
|
|
|
|
|
* The counter will start counting immediately.
|
|
|
|
|
*
|
|
|
|
|
* @param aChannel The a channel DIO channel. 0-9 are on-board, 10-25
|
|
|
|
|
* are on the MXP port
|
|
|
|
|
* @param bChannel The b channel DIO channel. 0-9 are on-board, 10-25
|
|
|
|
|
* are on the MXP port
|
|
|
|
|
* @param reverseDirection represents the orientation of the encoder and
|
|
|
|
|
* inverts the output values if necessary so forward
|
|
|
|
|
* represents positive values.
|
|
|
|
|
* @param 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.
|
|
|
|
|
*/
|
2016-09-06 00:01:45 -07:00
|
|
|
Encoder(int aChannel, int bChannel, bool reverseDirection = false,
|
2015-06-25 15:07:55 -04:00
|
|
|
EncodingType encodingType = k4X);
|
2018-05-31 20:47:15 -07:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 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.
|
|
|
|
|
*
|
|
|
|
|
* @param aSource The source that should be used for the a channel.
|
|
|
|
|
* @param bSource the source that should be used for the b channel.
|
|
|
|
|
* @param reverseDirection represents the orientation of the encoder and
|
|
|
|
|
* inverts the output values if necessary so forward
|
|
|
|
|
* represents positive values.
|
|
|
|
|
* @param 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.
|
|
|
|
|
*/
|
2016-05-20 17:30:37 -07:00
|
|
|
Encoder(DigitalSource* aSource, DigitalSource* bSource,
|
2015-06-25 15:07:55 -04:00
|
|
|
bool reverseDirection = false, EncodingType encodingType = k4X);
|
2018-05-31 20:47:15 -07:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 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.
|
|
|
|
|
*
|
|
|
|
|
* @param aSource The source that should be used for the a channel.
|
|
|
|
|
* @param bSource the source that should be used for the b channel.
|
|
|
|
|
* @param reverseDirection represents the orientation of the encoder and
|
|
|
|
|
* inverts the output values if necessary so forward
|
|
|
|
|
* represents positive values.
|
|
|
|
|
* @param 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.
|
|
|
|
|
*/
|
2016-05-20 17:30:37 -07:00
|
|
|
Encoder(DigitalSource& aSource, DigitalSource& bSource,
|
2015-06-25 15:07:55 -04:00
|
|
|
bool reverseDirection = false, EncodingType encodingType = k4X);
|
2018-05-31 20:47:15 -07:00
|
|
|
|
|
|
|
|
Encoder(std::shared_ptr<DigitalSource> aSource,
|
|
|
|
|
std::shared_ptr<DigitalSource> bSource, bool reverseDirection = false,
|
|
|
|
|
EncodingType encodingType = k4X);
|
|
|
|
|
|
2017-12-04 23:28:33 -08:00
|
|
|
~Encoder() override;
|
2013-12-15 18:30:16 -05:00
|
|
|
|
2019-08-25 18:42:00 -07:00
|
|
|
Encoder(Encoder&&) = default;
|
|
|
|
|
Encoder& operator=(Encoder&&) = default;
|
2018-09-24 00:08:25 -07:00
|
|
|
|
2015-06-25 15:07:55 -04:00
|
|
|
// CounterBase interface
|
2018-05-31 20:47:15 -07:00
|
|
|
/**
|
|
|
|
|
* Gets the current count.
|
|
|
|
|
*
|
|
|
|
|
* Returns the current count on the Encoder. This method compensates for the
|
|
|
|
|
* decoding type.
|
|
|
|
|
*
|
|
|
|
|
* @return Current count from the Encoder adjusted for the 1x, 2x, or 4x scale
|
|
|
|
|
* factor.
|
|
|
|
|
*/
|
2016-09-06 00:01:45 -07:00
|
|
|
int Get() const override;
|
2018-05-31 20:47:15 -07:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Reset the Encoder distance to zero.
|
|
|
|
|
*
|
|
|
|
|
* Resets the current count to zero on the encoder.
|
|
|
|
|
*/
|
2015-06-25 15:07:55 -04:00
|
|
|
void Reset() override;
|
2018-05-31 20:47:15 -07:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 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.
|
|
|
|
|
*
|
|
|
|
|
* Warning: This returns unscaled periods. Use GetRate() for rates that are
|
|
|
|
|
* scaled using the value from SetDistancePerPulse().
|
|
|
|
|
*
|
|
|
|
|
* @return Period in seconds of the most recent pulse.
|
|
|
|
|
*/
|
2015-06-25 15:07:55 -04:00
|
|
|
double GetPeriod() const override;
|
2018-05-31 20:47:15 -07:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 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().
|
|
|
|
|
*
|
|
|
|
|
* @param maxPeriod The maximum time between rising and falling edges before
|
|
|
|
|
* the FPGA will report the device stopped. This is expressed
|
|
|
|
|
* in seconds.
|
|
|
|
|
*/
|
2015-06-25 15:07:55 -04:00
|
|
|
void SetMaxPeriod(double maxPeriod) override;
|
2018-05-31 20:47:15 -07:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 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.
|
|
|
|
|
*
|
|
|
|
|
* @return True if the encoder is considered stopped.
|
|
|
|
|
*/
|
2015-06-25 15:07:55 -04:00
|
|
|
bool GetStopped() const override;
|
2018-05-31 20:47:15 -07:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* The last direction the encoder value changed.
|
|
|
|
|
*
|
|
|
|
|
* @return The last direction the encoder value changed.
|
|
|
|
|
*/
|
2015-06-25 15:07:55 -04:00
|
|
|
bool GetDirection() const override;
|
2015-06-19 17:23:54 -07:00
|
|
|
|
2018-05-31 20:47:15 -07:00
|
|
|
/**
|
|
|
|
|
* Gets the raw value from the encoder.
|
|
|
|
|
*
|
|
|
|
|
* The raw value is the actual count unscaled by the 1x, 2x, or 4x scale
|
|
|
|
|
* factor.
|
|
|
|
|
*
|
|
|
|
|
* @return Current raw count from the encoder
|
|
|
|
|
*/
|
|
|
|
|
int GetRaw() const;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* The encoding scale factor 1x, 2x, or 4x, per the requested encodingType.
|
|
|
|
|
*
|
|
|
|
|
* Used to divide raw edge counts down to spec'd counts.
|
|
|
|
|
*/
|
|
|
|
|
int GetEncodingScale() const;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get the distance the robot has driven since the last reset.
|
|
|
|
|
*
|
|
|
|
|
* @return The distance driven since the last reset as scaled by the value
|
|
|
|
|
* from SetDistancePerPulse().
|
|
|
|
|
*/
|
2016-11-20 07:25:03 -08:00
|
|
|
double GetDistance() const;
|
2018-05-31 20:47:15 -07:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get the current rate of the encoder.
|
|
|
|
|
*
|
|
|
|
|
* Units are distance per second as scaled by the value from
|
|
|
|
|
* SetDistancePerPulse().
|
|
|
|
|
*
|
|
|
|
|
* @return The current rate of the encoder.
|
|
|
|
|
*/
|
2016-11-20 07:25:03 -08:00
|
|
|
double GetRate() const;
|
2018-05-31 20:47:15 -07:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Set the minimum rate of the device before the hardware reports it stopped.
|
|
|
|
|
*
|
|
|
|
|
* @param minRate The minimum rate. The units are in distance per second as
|
|
|
|
|
* scaled by the value from SetDistancePerPulse().
|
|
|
|
|
*/
|
2016-11-20 07:25:03 -08:00
|
|
|
void SetMinRate(double minRate);
|
2018-05-31 20:47:15 -07:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 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.
|
|
|
|
|
*
|
|
|
|
|
* @param distancePerPulse The scale factor that will be used to convert
|
|
|
|
|
* pulses to useful units.
|
|
|
|
|
*/
|
2016-11-20 07:25:03 -08:00
|
|
|
void SetDistancePerPulse(double distancePerPulse);
|
2018-05-31 20:47:15 -07:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get the distance per pulse for this encoder.
|
|
|
|
|
*
|
|
|
|
|
* @return The scale factor that will be used to convert pulses to useful
|
|
|
|
|
* units.
|
|
|
|
|
*/
|
2017-12-04 23:28:33 -08:00
|
|
|
double GetDistancePerPulse() const;
|
2018-05-31 20:47:15 -07:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 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.
|
|
|
|
|
*
|
|
|
|
|
* @param reverseDirection true if the encoder direction should be reversed
|
|
|
|
|
*/
|
2015-06-25 15:07:55 -04:00
|
|
|
void SetReverseDirection(bool reverseDirection);
|
2018-05-31 20:47:15 -07:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 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.
|
|
|
|
|
*
|
|
|
|
|
* @param samplesToAverage The number of samples to average from 1 to 127.
|
|
|
|
|
*/
|
2015-06-25 15:07:55 -04:00
|
|
|
void SetSamplesToAverage(int samplesToAverage);
|
2018-05-31 20:47:15 -07:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 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.
|
|
|
|
|
*
|
|
|
|
|
* @return The number of samples being averaged (from 1 to 127)
|
|
|
|
|
*/
|
2015-06-25 15:07:55 -04:00
|
|
|
int GetSamplesToAverage() const;
|
2018-05-31 20:47:15 -07:00
|
|
|
|
Revert changes preventing old user code from compiling.
I'm not 100% sure whether we want these, but they are a quick
find and replace to do.
Basically, there are two primary things that we have done
this summer that break existing user code:
-Changing GetInstance() calls to return references instead
of pointers. This forces users to change from doing something
like LiveWindow::GetInstance()->AddSensor() to LiveWindow::GetInstance().AddSensor().
-Making PIDGet() and related calls const, forcing users to change
the function signatures wherever they override them.
The GetInstance() calls don't really matter to me either way,
especially since there are no real ownership issues going on there,
unlike the rest of the smart pointer-related changes.
For the const stuff, it is certainly more correct to mandate that
user PIDGet() functions be const and the such, but at the same time,
I'm not sure that there is any strong need for it, and the errors
generated are not the most helpful. While this wouldn't necessarily
be an issue for more experienced teams or completely new teams (who
don't have any old code to be reusing), it may cause issues for more
average teams who aren't familiar with the intricacies of C++ anything.
Change-Id: I6e7007982069292ea70e6d0fc8ca40203340df1b
2015-07-24 19:19:40 -04:00
|
|
|
double PIDGet() override;
|
2014-05-02 17:54:01 -04:00
|
|
|
|
2018-05-31 20:47:15 -07:00
|
|
|
/**
|
|
|
|
|
* Set the index source for the encoder.
|
|
|
|
|
*
|
|
|
|
|
* When this source is activated, the encoder count automatically resets.
|
|
|
|
|
*
|
|
|
|
|
* @param channel A DIO channel to set as the encoder index
|
|
|
|
|
* @param type The state that will cause the encoder to reset
|
|
|
|
|
*/
|
2016-09-06 00:01:45 -07:00
|
|
|
void SetIndexSource(int channel, IndexingType type = kResetOnRisingEdge);
|
2018-05-31 20:47:15 -07:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Set the index source for the encoder.
|
|
|
|
|
*
|
|
|
|
|
* When this source is activated, the encoder count automatically resets.
|
|
|
|
|
*
|
|
|
|
|
* @param channel A digital source to set as the encoder index
|
|
|
|
|
* @param type The state that will cause the encoder to reset
|
|
|
|
|
*/
|
2016-05-20 17:30:37 -07:00
|
|
|
void SetIndexSource(const DigitalSource& source,
|
2015-06-25 15:07:55 -04:00
|
|
|
IndexingType type = kResetOnRisingEdge);
|
2015-01-06 16:39:24 -05:00
|
|
|
|
2016-09-06 00:01:45 -07:00
|
|
|
int GetFPGAIndex() const;
|
2014-12-30 17:36:12 -05:00
|
|
|
|
2018-05-31 20:47:15 -07:00
|
|
|
void InitSendable(SendableBuilder& builder) override;
|
|
|
|
|
|
2015-06-25 15:07:55 -04:00
|
|
|
private:
|
2018-05-31 20:47:15 -07:00
|
|
|
/**
|
|
|
|
|
* Common initialization code for Encoders.
|
|
|
|
|
*
|
|
|
|
|
* This code allocates resources for Encoders and is common to all
|
|
|
|
|
* constructors. The counter will start counting immediately.
|
|
|
|
|
*
|
|
|
|
|
* @param reverseDirection If true, counts down instead of up (this is all
|
|
|
|
|
* relative)
|
|
|
|
|
* @param 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.
|
|
|
|
|
*/
|
2016-07-03 15:22:22 -07:00
|
|
|
void InitEncoder(bool reverseDirection, EncodingType encodingType);
|
|
|
|
|
|
2018-05-31 20:47:15 -07:00
|
|
|
/**
|
|
|
|
|
* The scale needed to convert a raw counter value into a number of encoder
|
|
|
|
|
* pulses.
|
|
|
|
|
*/
|
2016-11-20 07:25:03 -08:00
|
|
|
double DecodingScaleFactor() const;
|
2013-12-15 18:30:16 -05:00
|
|
|
|
2017-11-16 00:33:51 -08:00
|
|
|
std::shared_ptr<DigitalSource> m_aSource; // The A phase of the quad encoder
|
|
|
|
|
std::shared_ptr<DigitalSource> m_bSource; // The B phase of the quad encoder
|
2017-12-29 19:48:39 -08:00
|
|
|
std::shared_ptr<DigitalSource> m_indexSource = nullptr;
|
2019-08-25 18:42:00 -07:00
|
|
|
hal::Handle<HAL_EncoderHandle> m_encoder;
|
2014-06-13 17:45:10 -04:00
|
|
|
|
2015-11-22 21:18:59 -08:00
|
|
|
friend class DigitalGlitchFilter;
|
2013-12-15 18:30:16 -05:00
|
|
|
};
|
2016-11-01 22:33:12 -07:00
|
|
|
|
|
|
|
|
} // namespace frc
|