2013-12-15 18:30:16 -05:00
|
|
|
/*----------------------------------------------------------------------------*/
|
2016-01-02 03:02:34 -08:00
|
|
|
/* Copyright (c) FIRST 2008-2016. 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-09-05 13:55:31 -07:00
|
|
|
#include <atomic>
|
|
|
|
|
#include <memory>
|
|
|
|
|
#include <queue>
|
|
|
|
|
#include <string>
|
|
|
|
|
|
2013-12-15 18:30:16 -05:00
|
|
|
#include "Base.h"
|
|
|
|
|
#include "Controller.h"
|
2016-05-20 17:30:37 -07:00
|
|
|
#include "HAL/cpp/priority_mutex.h"
|
2013-12-15 18:30:16 -05:00
|
|
|
#include "LiveWindow/LiveWindow.h"
|
2016-05-20 17:30:37 -07:00
|
|
|
#include "Notifier.h"
|
2015-06-15 13:48:53 -04:00
|
|
|
#include "PIDInterface.h"
|
2015-07-14 20:37:52 -07:00
|
|
|
#include "PIDSource.h"
|
2015-08-26 03:19:35 -07:00
|
|
|
#include "Timer.h"
|
artf4154: Get rid of raw pointers in C++.
This deals with the majority of the user-facing code
in wpilibC++Devices and a substantial portion of it in
wpilibC++. wpilibC++Sim and wpilibC++IntegrationTests
are untouched except where it is necessary to make them
work with the rest of the libraries.
There is still a lot to do in the following areas:
-The HAL (which we may not want to touch at all).
-The I2C, Serial, and SPI interfaces in wpilibC++Devices,
which I haven't gotten around to doing yet.
-Most wpilibC++Devices classes have void* pointers
for interacting with the HAL.
-InterruptableSensorBase passes a void *params for
the interrupt handler.
-I haven't converted all the const char* to std::strings.
-There are plenty of other cases of raw pointers still
existing.
-This doesn't fall directly under raw pointer stuff,
but move syntax and rvalue references could be introduced
in many places.
-I haven't touched vision code.
-The Resource classes conflict (one is in the hal, the other
in wpilibC++). Someone should figure out a more
permanent fix (eg, just renaming them), then doing
what I did (making a new namespace for one of them,
essentially the same as renaming it).
A few other things:
-I created a NullDeleter class which is marked as deprecated.
What this does is it can be passed as the deleter to a
std::shared_ptr so that when you are converting raw pointers
to shared_ptrs the shared_ptr doesn't do any deletion if
someone else owns the raw pointer. This should only be
used in making old raw pointer UIs.
-I had to alter the build.gradle so that it did not
emit errors when deprecated functions called deprecated
functions. Unfortunately, gradle doesn't appear to be
actually printing out gcc warnigns for some reason.
The best way I have found to fix this is to patch
the toolchains (https://bitbucket.org/byteit101/toolchain-builder/pull-request/5/make-gcc-not-throw-warnings-for-nested/diff)
so that a deprecated function calling a deprecated
function is fine but a non-deprecated function calling
a deprecated function will throw a warning (which we
then elevate with -Werror). I believe that clang
deals with this properly, although I have not
tried it myself.
Change-Id: Ib8090c66893576fe73654f4e9d268f9d37be06a2
2015-06-30 15:01:20 -04:00
|
|
|
|
2013-12-15 18:30:16 -05:00
|
|
|
class PIDOutput;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Class implements a PID Control Loop.
|
2014-08-05 14:02:11 -04:00
|
|
|
*
|
2013-12-15 18:30:16 -05:00
|
|
|
* Creates a separate thread which reads the given PIDSource and takes
|
2016-05-20 17:30:37 -07:00
|
|
|
* care of the integral calculations, as well as writing the given PIDOutput.
|
2016-06-20 23:25:23 -07:00
|
|
|
*
|
|
|
|
|
* This feedback controller runs in discrete time, so time deltas are not used
|
|
|
|
|
* in the integral and derivative calculations. Therefore, the sample rate
|
|
|
|
|
* affects the controller's behavior for a given set of PID constants.
|
2013-12-15 18:30:16 -05:00
|
|
|
*/
|
2015-06-15 13:48:53 -04:00
|
|
|
class PIDController : public LiveWindowSendable,
|
|
|
|
|
public PIDInterface,
|
|
|
|
|
public ITableListener {
|
2015-06-25 15:07:55 -04:00
|
|
|
public:
|
2016-05-20 17:30:37 -07:00
|
|
|
PIDController(float p, float i, float d, PIDSource* source, PIDOutput* output,
|
2015-06-25 15:07:55 -04:00
|
|
|
float period = 0.05);
|
2016-05-20 17:30:37 -07:00
|
|
|
PIDController(float p, float i, float d, float f, PIDSource* source,
|
|
|
|
|
PIDOutput* output, float period = 0.05);
|
2015-08-13 23:17:19 -07:00
|
|
|
virtual ~PIDController();
|
2015-07-21 01:23:34 -07:00
|
|
|
|
|
|
|
|
PIDController(const PIDController&) = delete;
|
|
|
|
|
PIDController& operator=(const PIDController) = delete;
|
|
|
|
|
|
2015-06-25 15:07:55 -04:00
|
|
|
virtual float Get() const;
|
|
|
|
|
virtual void SetContinuous(bool continuous = true);
|
|
|
|
|
virtual void SetInputRange(float minimumInput, float maximumInput);
|
|
|
|
|
virtual void SetOutputRange(float minimumOutput, float maximumOutput);
|
2016-07-10 17:47:44 -07:00
|
|
|
void SetPID(double p, double i, double d) override;
|
2015-06-25 15:07:55 -04:00
|
|
|
virtual void SetPID(double p, double i, double d, double f);
|
2016-07-10 17:47:44 -07:00
|
|
|
double GetP() const override;
|
|
|
|
|
double GetI() const override;
|
|
|
|
|
double GetD() const override;
|
2015-06-25 15:07:55 -04:00
|
|
|
virtual double GetF() const;
|
|
|
|
|
|
2016-07-10 17:47:44 -07:00
|
|
|
void SetSetpoint(float setpoint) override;
|
|
|
|
|
double GetSetpoint() const override;
|
2015-08-26 03:19:35 -07:00
|
|
|
double GetDeltaSetpoint() const;
|
2015-06-25 15:07:55 -04:00
|
|
|
|
|
|
|
|
virtual float GetError() const;
|
2015-07-16 14:45:07 -04:00
|
|
|
virtual float GetAvgError() const;
|
2015-06-25 15:07:55 -04:00
|
|
|
|
2015-07-14 20:37:52 -07:00
|
|
|
virtual void SetPIDSourceType(PIDSourceType pidSource);
|
|
|
|
|
virtual PIDSourceType GetPIDSourceType() const;
|
|
|
|
|
|
2015-06-25 15:07:55 -04:00
|
|
|
virtual void SetTolerance(float percent);
|
|
|
|
|
virtual void SetAbsoluteTolerance(float absValue);
|
|
|
|
|
virtual void SetPercentTolerance(float percentValue);
|
2016-09-06 00:01:45 -07:00
|
|
|
virtual void SetToleranceBuffer(int buf = 1);
|
2015-06-25 15:07:55 -04:00
|
|
|
virtual bool OnTarget() const;
|
|
|
|
|
|
2016-07-10 17:47:44 -07:00
|
|
|
void Enable() override;
|
|
|
|
|
void Disable() override;
|
|
|
|
|
bool IsEnabled() const override;
|
2015-06-25 15:07:55 -04:00
|
|
|
|
2016-07-10 17:47:44 -07:00
|
|
|
void Reset() override;
|
2015-06-25 15:07:55 -04:00
|
|
|
|
2016-07-10 17:47:44 -07:00
|
|
|
void InitTable(std::shared_ptr<ITable> subtable) override;
|
2015-06-25 15:07:55 -04:00
|
|
|
|
2015-07-14 20:37:52 -07:00
|
|
|
protected:
|
2016-05-20 17:30:37 -07:00
|
|
|
PIDSource* m_pidInput;
|
|
|
|
|
PIDOutput* m_pidOutput;
|
2015-07-14 20:37:52 -07:00
|
|
|
|
|
|
|
|
std::shared_ptr<ITable> m_table;
|
|
|
|
|
virtual void Calculate();
|
2015-08-26 03:19:35 -07:00
|
|
|
virtual double CalculateFeedForward();
|
2016-07-15 13:44:04 -07:00
|
|
|
double GetContinuousError(double error) const;
|
2015-07-14 20:37:52 -07:00
|
|
|
|
2015-06-25 15:07:55 -04:00
|
|
|
private:
|
2016-05-20 17:30:37 -07:00
|
|
|
// factor for "proportional" control
|
|
|
|
|
float m_P;
|
|
|
|
|
// factor for "integral" control
|
|
|
|
|
float m_I;
|
|
|
|
|
// factor for "derivative" control
|
|
|
|
|
float m_D;
|
|
|
|
|
// factor for "feed forward" control
|
|
|
|
|
float m_F;
|
|
|
|
|
// |maximum output|
|
|
|
|
|
float m_maximumOutput = 1.0;
|
|
|
|
|
// |minimum output|
|
|
|
|
|
float m_minimumOutput = -1.0;
|
|
|
|
|
// maximum input - limit setpoint to this
|
|
|
|
|
float m_maximumInput = 0;
|
|
|
|
|
// minimum input - limit setpoint to this
|
|
|
|
|
float m_minimumInput = 0;
|
|
|
|
|
// do the endpoints wrap around? eg. Absolute encoder
|
|
|
|
|
bool m_continuous = false;
|
|
|
|
|
// is the pid controller enabled
|
|
|
|
|
bool m_enabled = false;
|
|
|
|
|
// the prior error (used to compute velocity)
|
|
|
|
|
float m_prevError = 0;
|
|
|
|
|
// the sum of the errors for use in the integral calc
|
|
|
|
|
double m_totalError = 0;
|
2015-06-24 01:06:29 -07:00
|
|
|
enum {
|
|
|
|
|
kAbsoluteTolerance,
|
|
|
|
|
kPercentTolerance,
|
|
|
|
|
kNoTolerance
|
|
|
|
|
} m_toleranceType = kNoTolerance;
|
2015-07-16 14:45:07 -04:00
|
|
|
|
|
|
|
|
// the percetage or absolute error that is considered on target.
|
|
|
|
|
float m_tolerance = 0.05;
|
2015-06-24 01:06:29 -07:00
|
|
|
float m_setpoint = 0;
|
2015-08-26 03:19:35 -07:00
|
|
|
float m_prevSetpoint = 0;
|
2015-11-15 14:49:50 -08:00
|
|
|
float m_error = 0;
|
2015-06-24 01:06:29 -07:00
|
|
|
float m_result = 0;
|
2015-06-25 15:07:55 -04:00
|
|
|
float m_period;
|
|
|
|
|
|
2015-07-16 14:45:07 -04:00
|
|
|
// Length of buffer for averaging for tolerances.
|
|
|
|
|
std::atomic<unsigned> m_bufLength{1};
|
|
|
|
|
std::queue<double> m_buf;
|
|
|
|
|
double m_bufTotal = 0;
|
|
|
|
|
|
2015-08-26 03:19:35 -07:00
|
|
|
mutable priority_recursive_mutex m_mutex;
|
2015-06-25 15:07:55 -04:00
|
|
|
|
artf4154: Get rid of raw pointers in C++.
This deals with the majority of the user-facing code
in wpilibC++Devices and a substantial portion of it in
wpilibC++. wpilibC++Sim and wpilibC++IntegrationTests
are untouched except where it is necessary to make them
work with the rest of the libraries.
There is still a lot to do in the following areas:
-The HAL (which we may not want to touch at all).
-The I2C, Serial, and SPI interfaces in wpilibC++Devices,
which I haven't gotten around to doing yet.
-Most wpilibC++Devices classes have void* pointers
for interacting with the HAL.
-InterruptableSensorBase passes a void *params for
the interrupt handler.
-I haven't converted all the const char* to std::strings.
-There are plenty of other cases of raw pointers still
existing.
-This doesn't fall directly under raw pointer stuff,
but move syntax and rvalue references could be introduced
in many places.
-I haven't touched vision code.
-The Resource classes conflict (one is in the hal, the other
in wpilibC++). Someone should figure out a more
permanent fix (eg, just renaming them), then doing
what I did (making a new namespace for one of them,
essentially the same as renaming it).
A few other things:
-I created a NullDeleter class which is marked as deprecated.
What this does is it can be passed as the deleter to a
std::shared_ptr so that when you are converting raw pointers
to shared_ptrs the shared_ptr doesn't do any deletion if
someone else owns the raw pointer. This should only be
used in making old raw pointer UIs.
-I had to alter the build.gradle so that it did not
emit errors when deprecated functions called deprecated
functions. Unfortunately, gradle doesn't appear to be
actually printing out gcc warnigns for some reason.
The best way I have found to fix this is to patch
the toolchains (https://bitbucket.org/byteit101/toolchain-builder/pull-request/5/make-gcc-not-throw-warnings-for-nested/diff)
so that a deprecated function calling a deprecated
function is fine but a non-deprecated function calling
a deprecated function will throw a warning (which we
then elevate with -Werror). I believe that clang
deals with this properly, although I have not
tried it myself.
Change-Id: Ib8090c66893576fe73654f4e9d268f9d37be06a2
2015-06-30 15:01:20 -04:00
|
|
|
std::unique_ptr<Notifier> m_controlLoop;
|
2015-08-26 03:19:35 -07:00
|
|
|
Timer m_setpointTimer;
|
2015-06-25 15:07:55 -04:00
|
|
|
|
2016-07-10 17:47:44 -07:00
|
|
|
std::shared_ptr<ITable> GetTable() const override;
|
|
|
|
|
std::string GetSmartDashboardType() const override;
|
|
|
|
|
void ValueChanged(ITable* source, llvm::StringRef key,
|
|
|
|
|
std::shared_ptr<nt::Value> value, bool isNew) override;
|
|
|
|
|
void UpdateTable() override;
|
|
|
|
|
void StartLiveWindowMode() override;
|
|
|
|
|
void StopLiveWindowMode() override;
|
2013-12-15 18:30:16 -05:00
|
|
|
};
|