WPILibC++  2020.3.2-60-g3011ebe
PIDController.h
1 /*----------------------------------------------------------------------------*/
2 /* Copyright (c) 2018-2020 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 <functional>
11 #include <limits>
12 
13 #include <units/units.h>
14 
15 #include "frc/smartdashboard/Sendable.h"
16 #include "frc/smartdashboard/SendableHelper.h"
17 
18 namespace frc2 {
19 
24  public frc::SendableHelper<PIDController> {
25  public:
35  PIDController(double Kp, double Ki, double Kd,
36  units::second_t period = 20_ms);
37 
38  ~PIDController() override = default;
39 
40  PIDController(const PIDController&) = default;
41  PIDController& operator=(const PIDController&) = default;
42  PIDController(PIDController&&) = default;
43  PIDController& operator=(PIDController&&) = default;
44 
54  void SetPID(double Kp, double Ki, double Kd);
55 
61  void SetP(double Kp);
62 
68  void SetI(double Ki);
69 
75  void SetD(double Kd);
76 
82  double GetP() const;
83 
89  double GetI() const;
90 
96  double GetD() const;
97 
103  units::second_t GetPeriod() const;
104 
110  void SetSetpoint(double setpoint);
111 
117  double GetSetpoint() const;
118 
124  bool AtSetpoint() const;
125 
136  void EnableContinuousInput(double minimumInput, double maximumInput);
137 
141  void DisableContinuousInput();
142 
146  bool IsContinuousInputEnabled() const;
147 
157  void SetIntegratorRange(double minimumIntegral, double maximumIntegral);
158 
165  void SetTolerance(
166  double positionTolerance,
167  double velocityTolerance = std::numeric_limits<double>::infinity());
168 
172  double GetPositionError() const;
173 
177  double GetVelocityError() const;
178 
184  double Calculate(double measurement);
185 
192  double Calculate(double measurement, double setpoint);
193 
197  void Reset();
198 
199  void InitSendable(frc::SendableBuilder& builder) override;
200 
201  private:
202  // Factor for "proportional" control
203  double m_Kp;
204 
205  // Factor for "integral" control
206  double m_Ki;
207 
208  // Factor for "derivative" control
209  double m_Kd;
210 
211  // The period (in seconds) of the control loop running this controller
212  units::second_t m_period;
213 
214  double m_maximumIntegral = 1.0;
215 
216  double m_minimumIntegral = -1.0;
217 
218  double m_maximumInput = 0;
219 
220  double m_minimumInput = 0;
221 
222  // Do the endpoints wrap around? eg. Absolute encoder
223  bool m_continuous = false;
224 
225  // The error at the time of the most recent call to Calculate()
226  double m_positionError = 0;
227  double m_velocityError = 0;
228 
229  // The error at the time of the second-most-recent call to Calculate() (used
230  // to compute velocity)
231  double m_prevError = 0;
232 
233  // The sum of the errors for use in the integral calc
234  double m_totalError = 0;
235 
236  // The error that is considered at setpoint.
237  double m_positionTolerance = 0.05;
238  double m_velocityTolerance = std::numeric_limits<double>::infinity();
239 
240  double m_setpoint = 0;
241 };
242 
243 } // namespace frc2
frc2::PIDController::IsContinuousInputEnabled
bool IsContinuousInputEnabled() const
Returns true if continuous input is enabled.
frc2::PIDController::SetP
void SetP(double Kp)
Sets the proportional coefficient of the PID controller gain.
frc2::PIDController::DisableContinuousInput
void DisableContinuousInput()
Disables continuous input.
frc2::PIDController::Calculate
double Calculate(double measurement)
Returns the next output of the PID controller.
frc2::PIDController::Reset
void Reset()
Reset the previous error, the integral term, and disable the controller.
frc2::PIDController::EnableContinuousInput
void EnableContinuousInput(double minimumInput, double maximumInput)
Enables continuous input.
frc2::PIDController::SetTolerance
void SetTolerance(double positionTolerance, double velocityTolerance=std::numeric_limits< double >::infinity())
Sets the error which is considered tolerable for use with AtSetpoint().
frc2::PIDController::AtSetpoint
bool AtSetpoint() const
Returns true if the error is within the tolerance of the setpoint.
frc2::PIDController::SetI
void SetI(double Ki)
Sets the integral coefficient of the PID controller gain.
frc2::PIDController::InitSendable
void InitSendable(frc::SendableBuilder &builder) override
Initializes this Sendable object.
frc2::PIDController::SetSetpoint
void SetSetpoint(double setpoint)
Sets the setpoint for the PIDController.
frc2::PIDController::GetP
double GetP() const
Gets the proportional coefficient.
frc2::PIDController::SetD
void SetD(double Kd)
Sets the differential coefficient of the PID controller gain.
frc2::PIDController::GetI
double GetI() const
Gets the integral coefficient.
frc2::PIDController::SetPID
void SetPID(double Kp, double Ki, double Kd)
Sets the PID Controller gain parameters.
frc2::PIDController::GetVelocityError
double GetVelocityError() const
Returns the velocity error.
frc2::PIDController::GetSetpoint
double GetSetpoint() const
Returns the current setpoint of the PIDController.
frc::Sendable
Interface for Sendable objects.
Definition: Sendable.h:17
frc2::PIDController::GetD
double GetD() const
Gets the differential coefficient.
frc2::PIDController::GetPeriod
units::second_t GetPeriod() const
Gets the period of this controller.
frc2::PIDController::SetIntegratorRange
void SetIntegratorRange(double minimumIntegral, double maximumIntegral)
Sets the minimum and maximum values for the integrator.
frc2::PIDController
Implements a PID control loop.
Definition: PIDController.h:23
frc2::PIDController::GetPositionError
double GetPositionError() const
Returns the difference between the setpoint and the measurement.
frc2::PIDController::PIDController
PIDController(double Kp, double Ki, double Kd, units::second_t period=20_ms)
Allocates a PIDController with the given constants for Kp, Ki, and Kd.
frc::SendableHelper
A helper class for use with objects that add themselves to SendableRegistry.
Definition: SendableHelper.h:28
frc::SendableBuilder
Definition: SendableBuilder.h:23