CTRE Phoenix 6 C++ 25.0.0-beta-4
Loading...
Searching...
No Matches
PhoenixPIDController.hpp
Go to the documentation of this file.
1/*
2 * Copyright (C) Cross The Road Electronics.  All rights reserved.
3 * License information can be found in CTRE_LICENSE.txt
4 * For support and suggestions contact support@ctr-electronics.com or file
5 * an issue tracker at https://github.com/CrossTheRoadElec/Phoenix-Releases
6 */
7#pragma once
8
9#include <functional>
10#include <limits>
11#include <units/time.h>
12
13namespace ctre {
14namespace phoenix6 {
15namespace swerve {
16
17/**
18 * \brief Phoenix-centric PID controller taken from WPI's frc#PIDController class.
19 *
20 * This class differs from the WPI implementation by using explicit timestamps for
21 * integral/derivative calculations. Ideally, these timestamps come from the StatusSignal.
22 */
24public:
25 /**
26 * Allocates a PhoenixPIDController with the given constants for Kp, Ki, and Kd.
27 *
28 * \param Kp The proportional coefficient. Must be >= 0.
29 * \param Ki The integral coefficient. Must be >= 0.
30 * \param Kd The derivative coefficient. Must be >= 0.
31 */
32 PhoenixPIDController(double Kp, double Ki, double Kd);
33
34 /**
35 * Sets the PID Controller gain parameters.
36 *
37 * Sets the proportional, integral, and differential coefficients.
38 *
39 * \param Kp The proportional coefficient. Must be >= 0.
40 * \param Ki The integral coefficient. Must be >= 0.
41 * \param Kd The differential coefficient. Must be >= 0.
42 */
43 void SetPID(double Kp, double Ki, double Kd);
44
45 /**
46 * Sets the proportional coefficient of the PID controller gain.
47 *
48 * \param Kp The proportional coefficient. Must be >= 0.
49 */
50 void SetP(double Kp);
51
52 /**
53 * Sets the integral coefficient of the PID controller gain.
54 *
55 * \param Ki The integral coefficient. Must be >= 0.
56 */
57 void SetI(double Ki);
58
59 /**
60 * Sets the differential coefficient of the PID controller gain.
61 *
62 * \param Kd The differential coefficient. Must be >= 0.
63 */
64 void SetD(double Kd);
65
66 /**
67 * Sets the IZone range. When the absolute value of the position error is
68 * greater than IZone, the total accumulated error will reset to zero,
69 * disabling integral gain until the absolute value of the position error is
70 * less than IZone. This is used to prevent integral windup. Must be
71 * non-negative. Passing a value of zero will effectively disable integral
72 * gain. Passing a value of infinity disables IZone functionality.
73 *
74 * \param iZone Maximum magnitude of error to allow integral control. Must be
75 * >= 0.
76 */
77 void SetIZone(double iZone);
78
79 /**
80 * Gets the proportional coefficient.
81 *
82 * \returns proportional coefficient
83 */
84 double GetP() const;
85
86 /**
87 * Gets the integral coefficient.
88 *
89 * \returns integral coefficient
90 */
91 double GetI() const;
92
93 /**
94 * Gets the differential coefficient.
95 *
96 * \returns differential coefficient
97 */
98 double GetD() const;
99
100 /**
101 * Get the IZone range.
102 *
103 * \returns Maximum magnitude of error to allow integral control.
104 */
105 double GetIZone() const;
106
107 /**
108 * Gets the position tolerance of this controller.
109 *
110 * \returns The position tolerance of the controller.
111 */
112 double GetPositionTolerance() const;
113
114 /**
115 * Gets the velocity tolerance of this controller.
116 *
117 * \returns The velocity tolerance of the controller.
118 */
119 double GetVelocityTolerance() const;
120
121 /**
122 * Returns the current setpoint of the PIDController.
123 *
124 * \returns The current setpoint.
125 */
126 double GetSetpoint() const;
127
128 /**
129 * Returns true if the error is within the tolerance of the setpoint.
130 *
131 * This will return false until at least one input value has been computed.
132 */
133 bool AtSetpoint() const;
134
135 /**
136 * Enables continuous input.
137 *
138 * Rather then using the max and min input range as constraints, it considers
139 * them to be the same point and automatically calculates the shortest route
140 * to the setpoint.
141 *
142 * \param minimumInput The minimum value expected from the input.
143 * \param maximumInput The maximum value expected from the input.
144 */
145 void EnableContinuousInput(double minimumInput, double maximumInput);
146
147 /**
148 * Disables continuous input.
149 */
151
152 /**
153 * Returns true if continuous input is enabled.
154 */
156
157 /**
158 * Sets the minimum and maximum values for the integrator.
159 *
160 * When the cap is reached, the integrator value is added to the controller
161 * output rather than the integrator value times the integral gain.
162 *
163 * \param minimumIntegral The minimum value of the integrator.
164 * \param maximumIntegral The maximum value of the integrator.
165 */
166 void SetIntegratorRange(double minimumIntegral, double maximumIntegral);
167
168 /**
169 * Sets the error which is considered tolerable for use with AtSetpoint().
170 *
171 * \param positionTolerance Position error which is tolerable.
172 * \param velocityTolerance Velocity error which is tolerable.
173 */
175 double positionTolerance,
176 double velocityTolerance = std::numeric_limits<double>::infinity());
177
178 /**
179 * Returns the difference between the setpoint and the measurement.
180 */
181 double GetPositionError() const;
182
183 /**
184 * Returns the velocity error.
185 */
186 double GetVelocityError() const;
187
188 /**
189 * Returns the next output of the PID controller.
190 *
191 * \param measurement The current measurement of the process variable.
192 * \param setpoint The new setpoint of the controller.
193 * \param currentTimestamp The current timestamp to use for calculating integral/derivative error
194 */
195 double Calculate(double measurement, double setpoint, units::second_t currentTimestamp);
196
197 /**
198 * Returns the last applied output from this PID controller.
199 */
200 double GetLastAppliedOutput() const;
201
202 /**
203 * Reset the previous error, the integral term, and disable the controller.
204 */
205 void Reset();
206
207private:
208 // Factor for "proportional" control
209 double m_Kp;
210
211 // Factor for "integral" control
212 double m_Ki;
213
214 // Factor for "derivative" control
215 double m_Kd;
216
217 // The error range where "integral" control applies
218 double m_iZone = std::numeric_limits<double>::infinity();
219
220 double m_maximumIntegral = 1.0;
221
222 double m_minimumIntegral = -1.0;
223
224 double m_maximumInput = 0;
225
226 double m_minimumInput = 0;
227
228 // Do the endpoints wrap around? eg. Absolute encoder
229 bool m_continuous = false;
230
231 // The error at the time of the most recent call to Calculate()
232 double m_positionError = 0;
233 double m_velocityError = 0;
234
235 // The error at the time of the second-most-recent call to Calculate() (used
236 // to compute velocity)
237 double m_prevError = 0;
238
239 // The sum of the errors for use in the integral calc
240 double m_totalError = 0;
241
242 // The error that is considered at setpoint.
243 double m_positionTolerance = 0.05;
244 double m_velocityTolerance = std::numeric_limits<double>::infinity();
245
246 double m_setpoint = 0;
247 double m_measurement = 0;
248
249 bool m_haveSetpoint = false;
250 bool m_haveMeasurement = false;
251
252 double m_lastAppliedOutput;
253
254 // The last timestamp acquired when performing a calculation
255 units::second_t m_lastTimestamp;
256};
257
258}
259}
260}
Phoenix-centric PID controller taken from WPI's frc::PIDController class.
Definition PhoenixPIDController.hpp:23
void DisableContinuousInput()
Disables continuous input.
void SetI(double Ki)
Sets the integral coefficient of the PID controller gain.
bool IsContinuousInputEnabled() const
Returns true if continuous input is enabled.
double GetLastAppliedOutput() const
Returns the last applied output from this PID controller.
double GetPositionError() const
Returns the difference between the setpoint and the measurement.
PhoenixPIDController(double Kp, double Ki, double Kd)
Allocates a PhoenixPIDController with the given constants for Kp, Ki, and Kd.
void SetP(double Kp)
Sets the proportional coefficient of the PID controller gain.
void Reset()
Reset the previous error, the integral term, and disable the controller.
void SetIntegratorRange(double minimumIntegral, double maximumIntegral)
Sets the minimum and maximum values for the integrator.
double GetVelocityError() const
Returns the velocity error.
double GetSetpoint() const
Returns the current setpoint of the PIDController.
bool AtSetpoint() const
Returns true if the error is within the tolerance of the setpoint.
double GetP() const
Gets the proportional coefficient.
void SetD(double Kd)
Sets the differential coefficient of the PID controller gain.
double GetVelocityTolerance() const
Gets the velocity tolerance of this controller.
double GetI() const
Gets the integral coefficient.
double Calculate(double measurement, double setpoint, units::second_t currentTimestamp)
Returns the next output of the PID controller.
double GetPositionTolerance() const
Gets the position tolerance of this controller.
void EnableContinuousInput(double minimumInput, double maximumInput)
Enables continuous input.
void SetTolerance(double positionTolerance, double velocityTolerance=std::numeric_limits< double >::infinity())
Sets the error which is considered tolerable for use with AtSetpoint().
double GetIZone() const
Get the IZone range.
double GetD() const
Gets the differential coefficient.
void SetIZone(double iZone)
Sets the IZone range.
void SetPID(double Kp, double Ki, double Kd)
Sets the PID Controller gain parameters.
Definition StatusCodes.h:18