WPILibC++  unspecified
PIDBase.h
1 /*----------------------------------------------------------------------------*/
2 /* Copyright (c) 2008-2018 FIRST. 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 <memory>
11 #include <string>
12 
13 #include <wpi/deprecated.h>
14 #include <wpi/mutex.h>
15 
16 #include "Base.h"
17 #include "Filters/LinearDigitalFilter.h"
18 #include "PIDInterface.h"
19 #include "PIDOutput.h"
20 #include "PIDSource.h"
21 #include "SmartDashboard/SendableBase.h"
22 #include "Timer.h"
23 
24 namespace frc {
25 
36 class PIDBase : public SendableBase, public PIDInterface, public PIDOutput {
37  public:
47  PIDBase(double p, double i, double d, PIDSource& source, PIDOutput& output);
48 
58  PIDBase(double p, double i, double d, double f, PIDSource& source,
59  PIDOutput& output);
60 
61  ~PIDBase() override = default;
62 
63  PIDBase(const PIDBase&) = delete;
64  PIDBase& operator=(const PIDBase) = delete;
65 
73  virtual double Get() const;
74 
84  virtual void SetContinuous(bool continuous = true);
85 
92  virtual void SetInputRange(double minimumInput, double maximumInput);
93 
100  virtual void SetOutputRange(double minimumOutput, double maximumOutput);
101 
111  void SetPID(double p, double i, double d) override;
112 
123  virtual void SetPID(double p, double i, double d, double f);
124 
130  void SetP(double p);
131 
137  void SetI(double i);
138 
144  void SetD(double d);
145 
151  void SetF(double f);
152 
158  double GetP() const override;
159 
165  double GetI() const override;
166 
172  double GetD() const override;
173 
179  virtual double GetF() const;
180 
186  void SetSetpoint(double setpoint) override;
187 
193  double GetSetpoint() const override;
194 
200  double GetDeltaSetpoint() const;
201 
207  virtual double GetError() const;
208 
218  WPI_DEPRECATED("Use a LinearDigitalFilter as the input and GetError().")
219  virtual double GetAvgError() const;
220 
224  virtual void SetPIDSourceType(PIDSourceType pidSource);
225 
231  virtual PIDSourceType GetPIDSourceType() const;
232 
239  WPI_DEPRECATED("Use SetPercentTolerance() instead.")
240  virtual void SetTolerance(double percent);
241 
248  virtual void SetAbsoluteTolerance(double absValue);
249 
256  virtual void SetPercentTolerance(double percentValue);
257 
268  WPI_DEPRECATED("Use a LinearDigitalFilter as the input.")
269  virtual void SetToleranceBuffer(int buf = 1);
270 
282  virtual bool OnTarget() const;
283 
287  void Reset() override;
288 
299  void PIDWrite(double output) override;
300 
301  void InitSendable(SendableBuilder& builder) override;
302 
303  protected:
304  // Is the pid controller enabled
305  bool m_enabled = false;
306 
307  mutable wpi::mutex m_thisMutex;
308 
309  // Ensures when Disable() is called, PIDWrite() won't run if Calculate()
310  // is already running at that time.
311  mutable wpi::mutex m_pidWriteMutex;
312 
313  PIDSource* m_pidInput;
314  PIDOutput* m_pidOutput;
315  Timer m_setpointTimer;
316 
321  virtual void Calculate();
322 
338  virtual double CalculateFeedForward();
339 
347  double GetContinuousError(double error) const;
348 
349  private:
350  // Factor for "proportional" control
351  double m_P;
352 
353  // Factor for "integral" control
354  double m_I;
355 
356  // Factor for "derivative" control
357  double m_D;
358 
359  // Factor for "feed forward" control
360  double m_F;
361 
362  // |maximum output|
363  double m_maximumOutput = 1.0;
364 
365  // |minimum output|
366  double m_minimumOutput = -1.0;
367 
368  // Maximum input - limit setpoint to this
369  double m_maximumInput = 0;
370 
371  // Minimum input - limit setpoint to this
372  double m_minimumInput = 0;
373 
374  // input range - difference between maximum and minimum
375  double m_inputRange = 0;
376 
377  // Do the endpoints wrap around? eg. Absolute encoder
378  bool m_continuous = false;
379 
380  // The prior error (used to compute velocity)
381  double m_prevError = 0;
382 
383  // The sum of the errors for use in the integral calc
384  double m_totalError = 0;
385 
386  enum {
387  kAbsoluteTolerance,
388  kPercentTolerance,
389  kNoTolerance
390  } m_toleranceType = kNoTolerance;
391 
392  // The percetage or absolute error that is considered on target.
393  double m_tolerance = 0.05;
394 
395  double m_setpoint = 0;
396  double m_prevSetpoint = 0;
397  double m_error = 0;
398  double m_result = 0;
399 
400  std::shared_ptr<PIDSource> m_origSource;
401  LinearDigitalFilter m_filter{nullptr, {}, {}};
402 };
403 
404 } // namespace frc
virtual void SetPIDSourceType(PIDSourceType pidSource)
Sets what type of input the PID controller will use.
Definition: PIDBase.cpp:175
Definition: Utility.cpp:119
double GetP() const override
Get the Proportional coefficient.
Definition: PIDBase.cpp:118
double GetDeltaSetpoint() const
Returns the change in setpoint over time of the PIDBase.
Definition: PIDBase.cpp:160
void SetD(double d)
Set the Differential coefficient of the PID controller gain.
Definition: PIDBase.cpp:108
double GetD() const override
Get the Differential coefficient.
Definition: PIDBase.cpp:128
virtual double GetError() const
Returns the current difference of the input from the setpoint.
Definition: PIDBase.cpp:165
void SetF(double f)
Get the Feed forward coefficient of the PID controller gain.
Definition: PIDBase.cpp:113
virtual double GetF() const
Get the Feed forward coefficient.
Definition: PIDBase.cpp:133
virtual void SetContinuous(bool continuous=true)
Set the PID controller to consider the input to be continuous,.
Definition: PIDBase.cpp:59
void SetI(double i)
Set the Integral coefficient of the PID controller gain.
Definition: PIDBase.cpp:103
virtual void SetPercentTolerance(double percentValue)
Set the percentage error which is considered tolerable for use with OnTarget.
Definition: PIDBase.cpp:195
PIDSource interface is a generic sensor source for the PID class.
Definition: PIDSource.h:20
void SetPID(double p, double i, double d) override
Set the PID Controller gain parameters.
Definition: PIDBase.cpp:81
PIDBase(double p, double i, double d, PIDSource &source, PIDOutput &output)
Allocate a PID object with the given constants for P, I, D.
Definition: PIDBase.cpp:25
double GetContinuousError(double error) const
Wraps error around for continuous inputs.
Definition: PIDBase.cpp:347
virtual void Calculate()
Read the input, calculate the output accordingly, and write to the output.
Definition: PIDBase.cpp:251
void InitSendable(SendableBuilder &builder) override
Initializes this Sendable object.
Definition: PIDBase.cpp:236
virtual void SetToleranceBuffer(int buf=1)
Set the number of previous error samples to average for tolerancing.
Definition: PIDBase.cpp:201
virtual double GetAvgError() const
Returns the current average of the error over the past few iterations.
Definition: PIDBase.cpp:173
PIDOutput interface is a generic output for the PID class.
Definition: PIDOutput.h:20
Timer objects measure accumulated time in seconds.
Definition: Timer.h:57
virtual void SetTolerance(double percent)
Set the percentage error which is considered tolerable for use with OnTarget.
Definition: PIDBase.cpp:183
virtual void SetAbsoluteTolerance(double absValue)
Set the absolute error which is considered tolerable for use with OnTarget.
Definition: PIDBase.cpp:189
void PIDWrite(double output) override
Passes the output directly to SetSetpoint().
Definition: PIDBase.cpp:234
virtual double CalculateFeedForward()
Calculate the feed forward term.
Definition: PIDBase.cpp:336
virtual double Get() const
Return the current PID result.
Definition: PIDBase.cpp:54
Definition: PIDInterface.h:12
virtual void SetOutputRange(double minimumOutput, double maximumOutput)
Sets the minimum and maximum values to write.
Definition: PIDBase.cpp:75
double GetSetpoint() const override
Returns the current setpoint of the PIDBase.
Definition: PIDBase.cpp:155
Definition: SendableBase.h:19
virtual PIDSourceType GetPIDSourceType() const
Returns the type of input the PID controller is using.
Definition: PIDBase.cpp:179
Class implements a PID Control Loop.
Definition: PIDBase.h:36
double GetI() const override
Get the Integral coefficient.
Definition: PIDBase.cpp:123
Definition: SendableBuilder.h:23
void SetP(double p)
Set the Proportional coefficient of the PID controller gain.
Definition: PIDBase.cpp:98
virtual bool OnTarget() const
Return true if the error is within the percentage of the total input range, determined by SetToleranc...
Definition: PIDBase.cpp:209
virtual void SetInputRange(double minimumInput, double maximumInput)
Sets the maximum and minimum values expected from the input.
Definition: PIDBase.cpp:64
This class implements a linear, digital filter.
Definition: LinearDigitalFilter.h:70
void SetSetpoint(double setpoint) override
Set the setpoint for the PIDBase.
Definition: PIDBase.cpp:138
void Reset() override
Reset the previous error, the integral term, and disable the controller.
Definition: PIDBase.cpp:227