blob: 5e743894e2869cbe98536b876a3760f81b418cdb [file] [log] [blame]
Brian Silverman26e4e522015-12-17 01:56:40 -05001/*----------------------------------------------------------------------------*/
Brian Silverman1a675112016-02-20 20:42:49 -05002/* Copyright (c) FIRST 2008-2016. All Rights Reserved. */
Brian Silverman26e4e522015-12-17 01:56:40 -05003/* Open Source Software - may be modified and shared by FRC teams. The code */
Brian Silverman1a675112016-02-20 20:42:49 -05004/* must be accompanied by the FIRST BSD license file in the root directory of */
5/* the project. */
Brian Silverman26e4e522015-12-17 01:56:40 -05006/*----------------------------------------------------------------------------*/
Brian Silverman1a675112016-02-20 20:42:49 -05007
Brian Silverman26e4e522015-12-17 01:56:40 -05008#pragma once
9
10#include "Base.h"
11#include "Controller.h"
12#include "LiveWindow/LiveWindow.h"
13#include "PIDInterface.h"
14#include "PIDSource.h"
15#include "Notifier.h"
16#include "HAL/cpp/priority_mutex.h"
Brian Silverman1a675112016-02-20 20:42:49 -050017#include "Timer.h"
Brian Silverman26e4e522015-12-17 01:56:40 -050018
19#include <memory>
20
21#include <atomic>
22#include <queue>
23
24class PIDOutput;
25
26/**
27 * Class implements a PID Control Loop.
28 *
29 * Creates a separate thread which reads the given PIDSource and takes
30 * care of the integral calculations, as well as writing the given
31 * PIDOutput
32 */
33class PIDController : public LiveWindowSendable,
34 public PIDInterface,
35 public ITableListener {
36 public:
37 PIDController(float p, float i, float d, PIDSource *source, PIDOutput *output,
38 float period = 0.05);
39 PIDController(float p, float i, float d, float f, PIDSource *source,
40 PIDOutput *output, float period = 0.05);
41 virtual ~PIDController();
42
43 PIDController(const PIDController&) = delete;
44 PIDController& operator=(const PIDController) = delete;
45
46 virtual float Get() const;
47 virtual void SetContinuous(bool continuous = true);
48 virtual void SetInputRange(float minimumInput, float maximumInput);
49 virtual void SetOutputRange(float minimumOutput, float maximumOutput);
50 virtual void SetPID(double p, double i, double d) override;
51 virtual void SetPID(double p, double i, double d, double f);
52 virtual double GetP() const override;
53 virtual double GetI() const override;
54 virtual double GetD() const override;
55 virtual double GetF() const;
56
57 virtual void SetSetpoint(float setpoint) override;
58 virtual double GetSetpoint() const override;
Brian Silverman1a675112016-02-20 20:42:49 -050059 double GetDeltaSetpoint() const;
Brian Silverman26e4e522015-12-17 01:56:40 -050060
61 virtual float GetError() const;
62 virtual float GetAvgError() const;
63
64 virtual void SetPIDSourceType(PIDSourceType pidSource);
65 virtual PIDSourceType GetPIDSourceType() const;
66
67 virtual void SetTolerance(float percent);
68 virtual void SetAbsoluteTolerance(float absValue);
69 virtual void SetPercentTolerance(float percentValue);
70 virtual void SetToleranceBuffer(unsigned buf = 1);
71 virtual bool OnTarget() const;
72
73 virtual void Enable() override;
74 virtual void Disable() override;
75 virtual bool IsEnabled() const override;
76
77 virtual void Reset() override;
78
79 virtual void InitTable(std::shared_ptr<ITable> table) override;
80
81 protected:
82 PIDSource *m_pidInput;
83 PIDOutput *m_pidOutput;
84
85 std::shared_ptr<ITable> m_table;
86 virtual void Calculate();
Brian Silverman1a675112016-02-20 20:42:49 -050087 virtual double CalculateFeedForward();
Brian Silverman26e4e522015-12-17 01:56:40 -050088
89 private:
90 float m_P; // factor for "proportional" control
91 float m_I; // factor for "integral" control
92 float m_D; // factor for "derivative" control
93 float m_F; // factor for "feed forward" control
94 float m_maximumOutput = 1.0; // |maximum output|
95 float m_minimumOutput = -1.0; // |minimum output|
96 float m_maximumInput = 0; // maximum input - limit setpoint to this
97 float m_minimumInput = 0; // minimum input - limit setpoint to this
98 bool m_continuous = false; // do the endpoints wrap around? eg. Absolute encoder
99 bool m_enabled = false; // is the pid controller enabled
Brian Silverman1a675112016-02-20 20:42:49 -0500100 float m_prevError = 0; // the prior error (used to compute velocity)
Brian Silverman26e4e522015-12-17 01:56:40 -0500101 double m_totalError = 0; // the sum of the errors for use in the integral calc
102 enum {
103 kAbsoluteTolerance,
104 kPercentTolerance,
105 kNoTolerance
106 } m_toleranceType = kNoTolerance;
107
108 // the percetage or absolute error that is considered on target.
109 float m_tolerance = 0.05;
110 float m_setpoint = 0;
Brian Silverman1a675112016-02-20 20:42:49 -0500111 float m_prevSetpoint = 0;
Brian Silverman26e4e522015-12-17 01:56:40 -0500112 float m_error = 0;
113 float m_result = 0;
114 float m_period;
115
116 // Length of buffer for averaging for tolerances.
117 std::atomic<unsigned> m_bufLength{1};
118 std::queue<double> m_buf;
119 double m_bufTotal = 0;
120
Brian Silverman1a675112016-02-20 20:42:49 -0500121 mutable priority_recursive_mutex m_mutex;
Brian Silverman26e4e522015-12-17 01:56:40 -0500122
123 std::unique_ptr<Notifier> m_controlLoop;
Brian Silverman1a675112016-02-20 20:42:49 -0500124 Timer m_setpointTimer;
Brian Silverman26e4e522015-12-17 01:56:40 -0500125
126 void Initialize(float p, float i, float d, float f, PIDSource *source,
127 PIDOutput *output, float period = 0.05);
Brian Silverman26e4e522015-12-17 01:56:40 -0500128
129 virtual std::shared_ptr<ITable> GetTable() const override;
130 virtual std::string GetSmartDashboardType() const override;
131 virtual void ValueChanged(ITable *source, llvm::StringRef key,
132 std::shared_ptr<nt::Value> value,
133 bool isNew) override;
134 virtual void UpdateTable() override;
135 virtual void StartLiveWindowMode() override;
136 virtual void StopLiveWindowMode() override;
137};