WPILibC++  2018.4.1-20180824234721-1176-gd6d5321
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Modules Pages
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 "frc/Base.h"
17 #include "frc/PIDInterface.h"
18 #include "frc/PIDOutput.h"
19 #include "frc/PIDSource.h"
20 #include "frc/Timer.h"
21 #include "frc/filters/LinearDigitalFilter.h"
22 #include "frc/smartdashboard/SendableBase.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
WPILib FRC namespace.
Definition: SPIAccelerometerSim.h:18
virtual double CalculateFeedForward()
Calculate the feed forward term.
double GetDeltaSetpoint() const
Returns the change in setpoint over time of the PIDBase.
virtual bool OnTarget() const
Return true if the error is within the percentage of the total input range, determined by SetToleranc...
virtual void SetPercentTolerance(double percentValue)
Set the percentage error which is considered tolerable for use with OnTarget.
PIDSource interface is a generic sensor source for the PID class.
Definition: PIDSource.h:20
virtual void SetAbsoluteTolerance(double absValue)
Set the absolute error which is considered tolerable for use with OnTarget.
void SetPID(double p, double i, double d) override
Set the PID Controller gain parameters.
void SetF(double f)
Get the Feed forward coefficient of the PID controller gain.
double GetI() const override
Get the Integral coefficient.
virtual void SetOutputRange(double minimumOutput, double maximumOutput)
Sets the minimum and maximum values to write.
void SetP(double p)
Set the Proportional coefficient of the PID controller gain.
PIDOutput interface is a generic output for the PID class.
Definition: PIDOutput.h:20
double GetContinuousError(double error) const
Wraps error around for continuous inputs.
Timer objects measure accumulated time in seconds.
Definition: Timer.h:57
PIDBase(double p, double i, double d, PIDSource &source, PIDOutput &output)
Allocate a PID object with the given constants for P, I, D.
virtual void SetToleranceBuffer(int buf=1)
Set the number of previous error samples to average for tolerancing.
virtual void Calculate()
Read the input, calculate the output accordingly, and write to the output.
double GetSetpoint() const override
Returns the current setpoint of the PIDBase.
virtual void SetContinuous(bool continuous=true)
Set the PID controller to consider the input to be continuous,.
void SetI(double i)
Set the Integral coefficient of the PID controller gain.
virtual void SetInputRange(double minimumInput, double maximumInput)
Sets the maximum and minimum values expected from the input.
virtual void SetPIDSourceType(PIDSourceType pidSource)
Sets what type of input the PID controller will use.
void SetD(double d)
Set the Differential coefficient of the PID controller gain.
void SetSetpoint(double setpoint) override
Set the setpoint for the PIDBase.
Definition: PIDInterface.h:12
virtual double Get() const
Return the current PID result.
void PIDWrite(double output) override
Passes the output directly to SetSetpoint().
void InitSendable(SendableBuilder &builder) override
Initializes this Sendable object.
virtual double GetError() const
Returns the current difference of the input from the setpoint.
Definition: SendableBase.h:19
virtual double GetF() const
Get the Feed forward coefficient.
Class implements a PID Control Loop.
Definition: PIDBase.h:36
void Reset() override
Reset the previous error, the integral term, and disable the controller.
Definition: SendableBuilder.h:23
virtual void SetTolerance(double percent)
Set the percentage error which is considered tolerable for use with OnTarget.
virtual double GetAvgError() const
Returns the current average of the error over the past few iterations.
virtual PIDSourceType GetPIDSourceType() const
Returns the type of input the PID controller is using.
This class implements a linear, digital filter.
Definition: LinearDigitalFilter.h:70
double GetP() const override
Get the Proportional coefficient.
double GetD() const override
Get the Differential coefficient.