15#include "wpi/units/time.hpp"
40 wpi::units::second_t period = 20_ms)
41 : m_Kp(Kp), m_Ki(Ki), m_Kd(Kd), m_period(period) {
42 bool invalidGains =
false;
45 "Kp must be a non-negative number, got {}!", Kp);
50 "Ki must be a non-negative number, got {}!", Ki);
55 "Kd must be a non-negative number, got {}!", Kd);
67 "Controller period must be a positive number, got {}!",
71 "Controller period defaulted to 20ms.");
73 if (!std::is_constant_evaluated()) {
77 std::to_string(instances));
98 constexpr void SetPID(
double Kp,
double Ki,
double Kd) {
109 constexpr void SetP(
double Kp) { m_Kp = Kp; }
116 constexpr void SetI(
double Ki) { m_Ki = Ki; }
123 constexpr void SetD(
double Kd) { m_Kd = Kd; }
137 if (std::is_constant_evaluated() && iZone < 0) {
139 "IZone must be a non-negative number, got {}!", iZone);
149 constexpr double GetP()
const {
return m_Kp; }
156 constexpr double GetI()
const {
return m_Ki; }
163 constexpr double GetD()
const {
return m_Kd; }
170 constexpr double GetIZone()
const {
return m_iZone; }
177 constexpr wpi::units::second_t
GetPeriod()
const {
return m_period; }
192 return m_errorDerivativeTolerance;
201 [[deprecated(
"Use the GetErrorTolerance method instead.")]]
203 return m_errorTolerance;
212 [[deprecated(
"Use the GetErrorDerivativeTolerance method instead.")]]
214 return m_errorDerivativeTolerance;
231 m_setpoint = setpoint;
232 m_haveSetpoint =
true;
235 double errorBound = (m_maximumInput - m_minimumInput) / 2.0;
237 InputModulus(m_setpoint - m_measurement, -errorBound, errorBound);
239 m_error = m_setpoint - m_measurement;
242 m_errorDerivative = (m_error - m_prevError) / m_period.value();
260 return m_haveMeasurement && m_haveSetpoint &&
262 gcem::abs(m_errorDerivative) < m_errorDerivativeTolerance;
276 double maximumInput) {
278 m_minimumInput = minimumInput;
279 m_maximumInput = maximumInput;
303 double maximumIntegral) {
304 m_minimumIntegral = minimumIntegral;
305 m_maximumIntegral = maximumIntegral;
315 double errorDerivativeTolerance =
316 std::numeric_limits<double>::infinity()) {
317 m_errorTolerance = errorTolerance;
318 m_errorDerivativeTolerance = errorDerivativeTolerance;
324 constexpr double GetError()
const {
return m_error; }
335 [[deprecated(
"Use GetError method instead.")]]
344 [[deprecated(
"Use GetErrorDerivative method instead.")]]
346 return m_errorDerivative;
355 m_measurement = measurement;
356 m_prevError = m_error;
357 m_haveMeasurement =
true;
360 double errorBound = (m_maximumInput - m_minimumInput) / 2.0;
362 InputModulus(m_setpoint - m_measurement, -errorBound, errorBound);
364 m_error = m_setpoint - m_measurement;
367 m_errorDerivative = (m_error - m_prevError) / m_period.value();
373 }
else if (m_Ki != 0) {
375 std::clamp(m_totalError + m_error * m_period.value(),
376 m_minimumIntegral / m_Ki, m_maximumIntegral / m_Ki);
379 return m_Kp * m_error + m_Ki * m_totalError + m_Kd * m_errorDerivative;
388 constexpr double Calculate(
double measurement,
double setpoint) {
389 m_setpoint = setpoint;
390 m_haveSetpoint =
true;
401 m_errorDerivative = 0;
402 m_haveMeasurement =
false;
418 double m_iZone = std::numeric_limits<double>::infinity();
421 wpi::units::second_t m_period;
423 double m_maximumIntegral = 1.0;
425 double m_minimumIntegral = -1.0;
427 double m_maximumInput = 0;
429 double m_minimumInput = 0;
432 bool m_continuous =
false;
436 double m_errorDerivative = 0;
440 double m_prevError = 0;
443 double m_totalError = 0;
446 double m_errorTolerance = 0.05;
447 double m_errorDerivativeTolerance = std::numeric_limits<double>::infinity();
449 double m_setpoint = 0;
450 double m_measurement = 0;
452 bool m_haveSetpoint =
false;
453 bool m_haveMeasurement =
false;
456 inline static int instances = 0;
#define WPILIB_DLLEXPORT
Definition SymbolExports.hpp:36
static void ReportUsage(std::string_view resource, std::string_view data)
Definition MathShared.hpp:61
static void ReportError(const S &format, Args &&... args)
Definition MathShared.hpp:48
static void ReportWarning(const S &format, Args &&... args)
Definition MathShared.hpp:57
constexpr double GetIZone() const
Get the IZone range.
Definition PIDController.hpp:170
constexpr ~PIDController() override=default
constexpr double GetVelocityTolerance() const
Gets the velocity tolerance of this controller.
Definition PIDController.hpp:213
constexpr wpi::units::second_t GetPeriod() const
Gets the period of this controller.
Definition PIDController.hpp:177
constexpr void SetP(double Kp)
Sets the proportional coefficient of the PID controller gain.
Definition PIDController.hpp:109
constexpr double GetPositionError() const
Returns the difference between the setpoint and the measurement.
Definition PIDController.hpp:336
constexpr double GetErrorTolerance() const
Gets the error tolerance of this controller.
Definition PIDController.hpp:184
constexpr void SetIZone(double iZone)
Sets the IZone range.
Definition PIDController.hpp:136
constexpr double GetPositionTolerance() const
Gets the position tolerance of this controller.
Definition PIDController.hpp:202
constexpr double Calculate(double measurement)
Returns the next output of the PID controller.
Definition PIDController.hpp:354
constexpr double GetSetpoint() const
Returns the current setpoint of the PIDController.
Definition PIDController.hpp:250
constexpr PIDController(PIDController &&)=default
constexpr bool IsContinuousInputEnabled() const
Returns true if continuous input is enabled.
Definition PIDController.hpp:290
constexpr double GetD() const
Gets the differential coefficient.
Definition PIDController.hpp:163
constexpr PIDController & operator=(PIDController &&)=default
constexpr double Calculate(double measurement, double setpoint)
Returns the next output of the PID controller.
Definition PIDController.hpp:388
constexpr void Reset()
Reset the previous error, the integral term, and disable the controller.
Definition PIDController.hpp:397
constexpr void SetI(double Ki)
Sets the integral coefficient of the PID controller gain.
Definition PIDController.hpp:116
constexpr double GetError() const
Returns the difference between the setpoint and the measurement.
Definition PIDController.hpp:324
constexpr double GetAccumulatedError() const
Gets the accumulated error used in the integral calculation of this controller.
Definition PIDController.hpp:223
constexpr double GetVelocityError() const
Returns the velocity error.
Definition PIDController.hpp:345
constexpr void SetTolerance(double errorTolerance, double errorDerivativeTolerance=std::numeric_limits< double >::infinity())
Sets the error which is considered tolerable for use with AtSetpoint().
Definition PIDController.hpp:314
constexpr double GetErrorDerivativeTolerance() const
Gets the error derivative tolerance of this controller.
Definition PIDController.hpp:191
constexpr double GetP() const
Gets the proportional coefficient.
Definition PIDController.hpp:149
constexpr PIDController & operator=(const PIDController &)=default
constexpr void SetPID(double Kp, double Ki, double Kd)
Sets the PID Controller gain parameters.
Definition PIDController.hpp:98
constexpr double GetErrorDerivative() const
Returns the error derivative.
Definition PIDController.hpp:329
constexpr PIDController(double Kp, double Ki, double Kd, wpi::units::second_t period=20_ms)
Allocates a PIDController with the given constants for Kp, Ki, and Kd.
Definition PIDController.hpp:39
constexpr void EnableContinuousInput(double minimumInput, double maximumInput)
Enables continuous input.
Definition PIDController.hpp:275
constexpr double GetI() const
Gets the integral coefficient.
Definition PIDController.hpp:156
constexpr void SetD(double Kd)
Sets the differential coefficient of the PID controller gain.
Definition PIDController.hpp:123
constexpr void DisableContinuousInput()
Disables continuous input.
Definition PIDController.hpp:285
constexpr void SetSetpoint(double setpoint)
Sets the setpoint for the PIDController.
Definition PIDController.hpp:230
void InitSendable(wpi::util::SendableBuilder &builder) override
Initializes this Sendable object.
constexpr PIDController(const PIDController &)=default
constexpr void SetIntegratorRange(double minimumIntegral, double maximumIntegral)
Sets the minimum and maximum contributions of the integral term.
Definition PIDController.hpp:302
constexpr bool AtSetpoint() const
Returns true if the error is within the tolerance of the setpoint.
Definition PIDController.hpp:259
Helper class for building Sendable dashboard representations.
Definition SendableBuilder.hpp:21
A helper class for use with objects that add themselves to SendableRegistry.
Definition SendableHelper.hpp:21
Interface for Sendable objects.
Definition Sendable.hpp:16
static void Add(Sendable *sendable, std::string_view name)
Adds an object to the registry.
constexpr T abs(const T x) noexcept
Compile-time absolute value function.
Definition abs.hpp:40
Definition LinearSystem.hpp:20
constexpr T InputModulus(T input, T minimumInput, T maximumInput)
Returns modulus of input.
Definition MathUtil.hpp:205