WPILibC++  unspecified
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Pages
PIDController.h
1 /*----------------------------------------------------------------------------*/
2 /* Copyright (c) FIRST 2008-2017. All Rights Reserved. */
3 /* Open Source Software - may be modified and shared by FRC teams. The code */
4 /* must be accompanied by the FIRST BSD license file in the root directory of */
5 /* the project. */
6 /*----------------------------------------------------------------------------*/
7 
8 #pragma once
9 
10 #include <atomic>
11 #include <memory>
12 #include <queue>
13 #include <string>
14 
15 #include "Base.h"
16 #include "Controller.h"
17 #include "HAL/cpp/priority_mutex.h"
18 #include "LiveWindow/LiveWindow.h"
19 #include "Notifier.h"
20 #include "PIDInterface.h"
21 #include "PIDSource.h"
22 #include "Timer.h"
23 
24 namespace frc {
25 
26 class PIDOutput;
27 
39  public PIDInterface,
40  public ITableListener {
41  public:
42  PIDController(double p, double i, double d, PIDSource* source,
43  PIDOutput* output, double period = 0.05);
44  PIDController(double p, double i, double d, double f, PIDSource* source,
45  PIDOutput* output, double period = 0.05);
46  virtual ~PIDController();
47 
48  PIDController(const PIDController&) = delete;
49  PIDController& operator=(const PIDController) = delete;
50 
51  virtual double Get() const;
52  virtual void SetContinuous(bool continuous = true);
53  virtual void SetInputRange(double minimumInput, double maximumInput);
54  virtual void SetOutputRange(double minimumOutput, double maximumOutput);
55  void SetPID(double p, double i, double d) override;
56  virtual void SetPID(double p, double i, double d, double f);
57  double GetP() const override;
58  double GetI() const override;
59  double GetD() const override;
60  virtual double GetF() const;
61 
62  void SetSetpoint(double setpoint) override;
63  double GetSetpoint() const override;
64  double GetDeltaSetpoint() const;
65 
66  virtual double GetError() const;
67  virtual double GetAvgError() const;
68 
69  virtual void SetPIDSourceType(PIDSourceType pidSource);
70  virtual PIDSourceType GetPIDSourceType() const;
71 
72  virtual void SetTolerance(double percent);
73  virtual void SetAbsoluteTolerance(double absValue);
74  virtual void SetPercentTolerance(double percentValue);
75  virtual void SetToleranceBuffer(int buf = 1);
76  virtual bool OnTarget() const;
77 
78  void Enable() override;
79  void Disable() override;
80  bool IsEnabled() const override;
81 
82  void Reset() override;
83 
84  void InitTable(std::shared_ptr<ITable> subtable) override;
85 
86  protected:
87  PIDSource* m_pidInput;
88  PIDOutput* m_pidOutput;
89 
90  std::shared_ptr<ITable> m_table;
91  virtual void Calculate();
92  virtual double CalculateFeedForward();
93  double GetContinuousError(double error) const;
94 
95  private:
96  // factor for "proportional" control
97  double m_P;
98  // factor for "integral" control
99  double m_I;
100  // factor for "derivative" control
101  double m_D;
102  // factor for "feed forward" control
103  double m_F;
104  // |maximum output|
105  double m_maximumOutput = 1.0;
106  // |minimum output|
107  double m_minimumOutput = -1.0;
108  // maximum input - limit setpoint to this
109  double m_maximumInput = 0;
110  // minimum input - limit setpoint to this
111  double m_minimumInput = 0;
112  // do the endpoints wrap around? eg. Absolute encoder
113  bool m_continuous = false;
114  // is the pid controller enabled
115  bool m_enabled = false;
116  // the prior error (used to compute velocity)
117  double m_prevError = 0;
118  // the sum of the errors for use in the integral calc
119  double m_totalError = 0;
120  enum {
121  kAbsoluteTolerance,
122  kPercentTolerance,
123  kNoTolerance
124  } m_toleranceType = kNoTolerance;
125 
126  // the percetage or absolute error that is considered on target.
127  double m_tolerance = 0.05;
128  double m_setpoint = 0;
129  double m_prevSetpoint = 0;
130  double m_error = 0;
131  double m_result = 0;
132  double m_period;
133 
134  // Length of buffer for averaging for tolerances.
135  std::atomic<unsigned> m_bufLength{1};
136  std::queue<double> m_buf;
137  double m_bufTotal = 0;
138 
139  mutable priority_recursive_mutex m_mutex;
140 
141  std::unique_ptr<Notifier> m_controlLoop;
142  Timer m_setpointTimer;
143 
144  std::shared_ptr<ITable> GetTable() const override;
145  std::string GetSmartDashboardType() const override;
146  void ValueChanged(ITable* source, llvm::StringRef key,
147  std::shared_ptr<nt::Value> value, bool isNew) override;
148  void UpdateTable() override;
149  void StartLiveWindowMode() override;
150  void StopLiveWindowMode() override;
151 };
152 
153 } // namespace frc
virtual void SetInputRange(double minimumInput, double maximumInput)
Sets the maximum and minimum values expected from the input.
Definition: PIDController.cpp:312
virtual double Get() const
Return the current PID result.
Definition: PIDController.cpp:287
double GetD() const override
Get the Differential coefficient.
Definition: PIDController.cpp:265
A table whose values can be read and written to.
Definition: ITable.h:22
virtual void SetPIDSourceType(PIDSourceType pidSource)
Sets what type of input the PID controller will use.
Definition: PIDController.cpp:402
Live Window Sendable is a special type of object sendable to the live window.
Definition: LiveWindowSendable.h:17
virtual double GetAvgError() const
Returns the current average of the error over the past few iterations.
Definition: PIDController.cpp:422
virtual void SetContinuous(bool continuous=true)
Set the PID controller to consider the input to be continuous,.
Definition: PIDController.cpp:301
virtual PIDSourceType GetPIDSourceType() const
Returns the type of input the PID controller is using.
Definition: PIDController.cpp:410
PIDSource interface is a generic sensor source for the PID class.
Definition: PIDSource.h:19
Class implements a PID Control Loop.
Definition: PIDController.h:38
virtual double CalculateFeedForward()
Calculate the feed forward term.
Definition: PIDController.cpp:178
PIDOutput interface is a generic output for the PID class.
Definition: PIDOutput.h:20
double GetDeltaSetpoint() const
Returns the change in setpoint over time of the PIDController.
Definition: PIDController.cpp:381
virtual double GetF() const
Get the Feed forward coefficient.
Definition: PIDController.cpp:275
Timer objects measure accumulated time in seconds.
Definition: Timer.h:29
Definition: priority_mutex.h:22
double GetContinuousError(double error) const
Wraps error around for continuous inputs.
Definition: PIDController.cpp:593
double GetP() const override
Get the Proportional coefficient.
Definition: PIDController.cpp:245
virtual double GetError() const
Returns the current difference of the input from the setpoint.
Definition: PIDController.cpp:391
void Reset() override
Reset the previous error, the integral term, and disable the controller.
Definition: PIDController.cpp:559
bool IsEnabled() const override
Return true if PIDController is enabled.
Definition: PIDController.cpp:551
Definition: PIDInterface.h:16
A listener that listens to changes in values in a ITable.
Definition: ITableListener.h:18
void SetPID(double p, double i, double d) override
Set the PID Controller gain parameters.
Definition: PIDController.cpp:198
double GetSetpoint() const override
Returns the current setpoint of the PIDController.
Definition: PIDController.cpp:371
void InitTable(std::shared_ptr< ITable > subtable) override
Initializes a table for this sendable object.
Definition: PIDController.cpp:572
virtual void SetOutputRange(double minimumOutput, double maximumOutput)
Sets the minimum and maximum values to write.
Definition: PIDController.cpp:328
double GetI() const override
Get the Integral coefficient.
Definition: PIDController.cpp:255
void SetSetpoint(double setpoint) override
Set the setpoint for the PIDController.
Definition: PIDController.cpp:341
PIDController(double p, double i, double d, PIDSource *source, PIDOutput *output, double period=0.05)
Allocate a PID object with the given constants for P, I, D.
Definition: PIDController.cpp:39
virtual void Calculate()
Read the input, calculate the output accordingly, and write to the output.
Definition: PIDController.cpp:87
void Disable() override
Stop running the PIDController, this sets the output to zero before stopping.
Definition: PIDController.cpp:536
void Enable() override
Begin running the PIDController.
Definition: PIDController.cpp:522