2013-12-15 18:30:16 -05:00
|
|
|
/*----------------------------------------------------------------------------*/
|
2017-08-23 22:06:13 -07:00
|
|
|
/* Copyright (c) 2008-2017 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>
|
|
|
|
|
|
2017-08-27 00:11:52 -07:00
|
|
|
#include <HAL/Encoder.h>
|
|
|
|
|
|
2013-12-15 18:30:16 -05:00
|
|
|
#include "Counter.h"
|
2016-05-20 17:30:37 -07:00
|
|
|
#include "CounterBase.h"
|
|
|
|
|
#include "PIDSource.h"
|
|
|
|
|
#include "SensorBase.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
|
|
|
*/
|
2017-12-04 23:28:33 -08:00
|
|
|
class Encoder : public SensorBase, 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
|
|
|
|
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);
|
2015-07-29 16:48:04 -04:00
|
|
|
Encoder(std::shared_ptr<DigitalSource> aSource,
|
2016-05-20 17:30:37 -07:00
|
|
|
std::shared_ptr<DigitalSource> bSource, bool reverseDirection = false,
|
|
|
|
|
EncodingType encodingType = k4X);
|
|
|
|
|
Encoder(DigitalSource* aSource, DigitalSource* bSource,
|
2015-06-25 15:07:55 -04:00
|
|
|
bool reverseDirection = false, EncodingType encodingType = k4X);
|
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);
|
2017-12-04 23:28:33 -08:00
|
|
|
~Encoder() override;
|
2013-12-15 18:30:16 -05:00
|
|
|
|
2015-06-25 15:07:55 -04:00
|
|
|
// CounterBase interface
|
2016-09-06 00:01:45 -07:00
|
|
|
int Get() const override;
|
|
|
|
|
int GetRaw() const;
|
|
|
|
|
int GetEncodingScale() const;
|
2015-06-25 15:07:55 -04:00
|
|
|
void Reset() override;
|
|
|
|
|
double GetPeriod() const override;
|
|
|
|
|
void SetMaxPeriod(double maxPeriod) override;
|
|
|
|
|
bool GetStopped() const override;
|
|
|
|
|
bool GetDirection() const override;
|
2015-06-19 17:23:54 -07:00
|
|
|
|
2016-11-20 07:25:03 -08:00
|
|
|
double GetDistance() const;
|
|
|
|
|
double GetRate() const;
|
|
|
|
|
void SetMinRate(double minRate);
|
|
|
|
|
void SetDistancePerPulse(double distancePerPulse);
|
2017-12-04 23:28:33 -08:00
|
|
|
double GetDistancePerPulse() const;
|
2015-06-25 15:07:55 -04:00
|
|
|
void SetReverseDirection(bool reverseDirection);
|
|
|
|
|
void SetSamplesToAverage(int samplesToAverage);
|
|
|
|
|
int GetSamplesToAverage() const;
|
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
|
|
|
|
2016-09-06 00:01:45 -07:00
|
|
|
void SetIndexSource(int channel, IndexingType type = kResetOnRisingEdge);
|
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
|
|
|
|
2017-12-04 23:28:33 -08:00
|
|
|
void InitSendable(SendableBuilder& builder) override;
|
2013-12-15 18:30:16 -05:00
|
|
|
|
2016-09-06 00:01:45 -07:00
|
|
|
int GetFPGAIndex() const;
|
2014-12-30 17:36:12 -05:00
|
|
|
|
2015-06-25 15:07:55 -04:00
|
|
|
private:
|
2016-07-03 15:22:22 -07:00
|
|
|
void InitEncoder(bool reverseDirection, EncodingType encodingType);
|
|
|
|
|
|
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
|
2016-07-07 21:43:55 -07:00
|
|
|
std::unique_ptr<DigitalSource> m_indexSource = nullptr;
|
2016-07-09 00:24:26 -07:00
|
|
|
HAL_EncoderHandle m_encoder = HAL_kInvalidHandle;
|
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
|