WPILibC++  2020.3.2
PIDBase.h
1 /*----------------------------------------------------------------------------*/
2 /* Copyright (c) 2008-2019 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/LinearFilter.h"
18 #include "frc/PIDInterface.h"
19 #include "frc/PIDOutput.h"
20 #include "frc/PIDSource.h"
21 #include "frc/Timer.h"
22 #include "frc/smartdashboard/Sendable.h"
23 #include "frc/smartdashboard/SendableHelper.h"
24 
25 namespace frc {
26 
27 class SendableBuilder;
28 
39 class PIDBase : public PIDInterface,
40  public PIDOutput,
41  public Sendable,
42  public SendableHelper<PIDBase> {
43  public:
53  WPI_DEPRECATED("All APIs which use this have been deprecated.")
54  PIDBase(double p, double i, double d, PIDSource& source, PIDOutput& output);
55 
65  WPI_DEPRECATED("All APIs which use this have been deprecated.")
66  PIDBase(double p, double i, double d, double f, PIDSource& source,
67  PIDOutput& output);
68 
69  virtual ~PIDBase() = default;
70 
78  virtual double Get() const;
79 
89  virtual void SetContinuous(bool continuous = true);
90 
97  virtual void SetInputRange(double minimumInput, double maximumInput);
98 
105  virtual void SetOutputRange(double minimumOutput, double maximumOutput);
106 
116  void SetPID(double p, double i, double d) override;
117 
128  virtual void SetPID(double p, double i, double d, double f);
129 
135  void SetP(double p);
136 
142  void SetI(double i);
143 
149  void SetD(double d);
150 
156  void SetF(double f);
157 
163  double GetP() const override;
164 
170  double GetI() const override;
171 
177  double GetD() const override;
178 
184  virtual double GetF() const;
185 
191  void SetSetpoint(double setpoint) override;
192 
198  double GetSetpoint() const override;
199 
205  double GetDeltaSetpoint() const;
206 
212  virtual double GetError() const;
213 
223  WPI_DEPRECATED("Use a LinearFilter as the input and GetError().")
224  virtual double GetAvgError() const;
225 
229  virtual void SetPIDSourceType(PIDSourceType pidSource);
230 
236  virtual PIDSourceType GetPIDSourceType() const;
237 
244  WPI_DEPRECATED("Use SetPercentTolerance() instead.")
245  virtual void SetTolerance(double percent);
246 
253  virtual void SetAbsoluteTolerance(double absValue);
254 
261  virtual void SetPercentTolerance(double percentValue);
262 
273  WPI_DEPRECATED("Use a LinearDigitalFilter as the input.")
274  virtual void SetToleranceBuffer(int buf = 1);
275 
287  virtual bool OnTarget() const;
288 
292  void Reset() override;
293 
304  void PIDWrite(double output) override;
305 
306  void InitSendable(SendableBuilder& builder) override;
307 
308  protected:
309  // Is the pid controller enabled
310  bool m_enabled = false;
311 
312  mutable wpi::mutex m_thisMutex;
313 
314  // Ensures when Disable() is called, PIDWrite() won't run if Calculate()
315  // is already running at that time.
316  mutable wpi::mutex m_pidWriteMutex;
317 
318  PIDSource* m_pidInput;
319  PIDOutput* m_pidOutput;
320  Timer m_setpointTimer;
321 
326  virtual void Calculate();
327 
343  virtual double CalculateFeedForward();
344 
352  double GetContinuousError(double error) const;
353 
354  private:
355  // Factor for "proportional" control
356  double m_P;
357 
358  // Factor for "integral" control
359  double m_I;
360 
361  // Factor for "derivative" control
362  double m_D;
363 
364  // Factor for "feed forward" control
365  double m_F;
366 
367  // |maximum output|
368  double m_maximumOutput = 1.0;
369 
370  // |minimum output|
371  double m_minimumOutput = -1.0;
372 
373  // Maximum input - limit setpoint to this
374  double m_maximumInput = 0;
375 
376  // Minimum input - limit setpoint to this
377  double m_minimumInput = 0;
378 
379  // input range - difference between maximum and minimum
380  double m_inputRange = 0;
381 
382  // Do the endpoints wrap around? eg. Absolute encoder
383  bool m_continuous = false;
384 
385  // The prior error (used to compute velocity)
386  double m_prevError = 0;
387 
388  // The sum of the errors for use in the integral calc
389  double m_totalError = 0;
390 
391  enum {
392  kAbsoluteTolerance,
393  kPercentTolerance,
394  kNoTolerance
395  } m_toleranceType = kNoTolerance;
396 
397  // The percetage or absolute error that is considered on target.
398  double m_tolerance = 0.05;
399 
400  double m_setpoint = 0;
401  double m_prevSetpoint = 0;
402  double m_error = 0;
403  double m_result = 0;
404 
405  LinearFilter<double> m_filter{{}, {}};
406 };
407 
408 } // namespace frc
frc::PIDSource
PIDSource interface is a generic sensor source for the PID class.
Definition: PIDSource.h:20
frc::PIDBase::GetI
double GetI() const override
Get the Integral coefficient.
frc::PIDBase::SetPID
void SetPID(double p, double i, double d) override
Set the PID Controller gain parameters.
frc::PIDBase::SetAbsoluteTolerance
virtual void SetAbsoluteTolerance(double absValue)
Set the absolute error which is considered tolerable for use with OnTarget.
frc::PIDBase::GetDeltaSetpoint
double GetDeltaSetpoint() const
Returns the change in setpoint over time of the PIDBase.
frc::PIDBase::GetContinuousError
double GetContinuousError(double error) const
Wraps error around for continuous inputs.
frc::PIDBase
Class implements a PID Control Loop.
Definition: PIDBase.h:39
frc::PIDBase::SetContinuous
virtual void SetContinuous(bool continuous=true)
Set the PID controller to consider the input to be continuous,.
frc::Timer
Timer objects measure accumulated time in seconds.
Definition: Timer.h:47
frc::PIDBase::Get
virtual double Get() const
Return the current PID result.
frc::PIDBase::SetI
void SetI(double i)
Set the Integral coefficient of the PID controller gain.
frc::PIDBase::Calculate
virtual void Calculate()
Read the input, calculate the output accordingly, and write to the output.
frc::PIDBase::GetSetpoint
double GetSetpoint() const override
Returns the current setpoint of the PIDBase.
frc::PIDBase::SetD
void SetD(double d)
Set the Differential coefficient of the PID controller gain.
frc::PIDBase::GetError
virtual double GetError() const
Returns the current difference of the input from the setpoint.
frc::PIDInterface
Definition: PIDInterface.h:14
frc::PIDBase::OnTarget
virtual bool OnTarget() const
Return true if the error is within the percentage of the total input range, determined by SetToleranc...
frc::PIDBase::SetP
void SetP(double p)
Set the Proportional coefficient of the PID controller gain.
frc::PIDBase::GetP
double GetP() const override
Get the Proportional coefficient.
frc::PIDBase::GetAvgError
virtual double GetAvgError() const
Returns the current average of the error over the past few iterations.
frc::PIDBase::SetOutputRange
virtual void SetOutputRange(double minimumOutput, double maximumOutput)
Sets the minimum and maximum values to write.
frc::PIDBase::SetPIDSourceType
virtual void SetPIDSourceType(PIDSourceType pidSource)
Sets what type of input the PID controller will use.
frc::Sendable
Interface for Sendable objects.
Definition: Sendable.h:17
frc::PIDBase::SetToleranceBuffer
virtual void SetToleranceBuffer(int buf=1)
Set the number of previous error samples to average for tolerancing.
frc::PIDBase::InitSendable
void InitSendable(SendableBuilder &builder) override
Initializes this Sendable object.
frc::PIDBase::SetInputRange
virtual void SetInputRange(double minimumInput, double maximumInput)
Sets the maximum and minimum values expected from the input.
frc::PIDBase::Reset
void Reset() override
Reset the previous error, the integral term, and disable the controller.
frc::PIDBase::GetPIDSourceType
virtual PIDSourceType GetPIDSourceType() const
Returns the type of input the PID controller is using.
frc::PIDBase::SetSetpoint
void SetSetpoint(double setpoint) override
Set the setpoint for the PIDBase.
frc
A class that enforces constraints on the differential drive kinematics.
Definition: SPIAccelerometerSim.h:16
frc::PIDBase::PIDBase
PIDBase(double p, double i, double d, PIDSource &source, PIDOutput &output)
Allocate a PID object with the given constants for P, I, D.
frc::PIDBase::GetD
double GetD() const override
Get the Differential coefficient.
frc::PIDBase::CalculateFeedForward
virtual double CalculateFeedForward()
Calculate the feed forward term.
frc::PIDBase::GetF
virtual double GetF() const
Get the Feed forward coefficient.
frc::LinearFilter< double >
frc::PIDOutput
PIDOutput interface is a generic output for the PID class.
Definition: PIDOutput.h:20
frc::PIDBase::SetTolerance
virtual void SetTolerance(double percent)
Set the percentage error which is considered tolerable for use with OnTarget.
frc::PIDBase::PIDWrite
void PIDWrite(double output) override
Passes the output directly to SetSetpoint().
frc::PIDBase::SetPercentTolerance
virtual void SetPercentTolerance(double percentValue)
Set the percentage error which is considered tolerable for use with OnTarget.
frc::SendableHelper
A helper class for use with objects that add themselves to SendableRegistry.
Definition: SendableHelper.h:28
frc::SendableBuilder
Definition: SendableBuilder.h:23
frc::PIDBase::SetF
void SetF(double f)
Get the Feed forward coefficient of the PID controller gain.