2#include "pros/rtos.hpp"
28 double m_previousError = 0;
29 double m_previousMeasurement = 0;
30 double m_integral = 0;
31 double m_filteredDerivative = 0;
32 uint32_t m_previousTime = 0;
33 bool m_initialized =
false;
34 double m_previousOutput = 0;
37 double m_integralLimit = 0.0;
38 double m_staticThreshold = 3.0;
40 template <
typename T>
inline int sgn(T val) {
41 return (T(0) < val) - (val < T(0));
56 PID(
double kP,
double kI,
double kD,
double kF = 0,
double windupRange = 0,
57 bool signFlipReset =
false,
double slew = 0);
67 bool signFlipReset =
false);
98 double update(
double error,
double measurement);
A standard PID controller with advanced features.
PID(const PIDGains &gains, double windupRange=0, bool signFlipReset=false)
Construct a new PID controller from a PIDGains struct.
void setWindupRange(double windupRange)
Set the windup range within which the integral term is allowed to accumulate.
double getStaticThreshold()
Get the static threshold error value.
void setStaticThreshold(double threshold)
Set the static threshold error value.
PIDGains getGains()
Get the current gains of the PID controller.
double update(double error)
Update the PID controller based on the current error.
double getAlpha()
Get the alpha value for the derivative filter.
double getIntegralLimit()
Get the integral limit.
void setGains(PIDGains gains)
Set new gains for the PID controller.
double getWindupRange()
Get the current windup range.
void setSignFlipReset(bool signFlipReset)
Enable or disable resetting the integral term when the error sign flips.
void setAlpha(double alpha)
Set the alpha value for the derivative filter.
PID(double kP, double kI, double kD, double kF=0, double windupRange=0, bool signFlipReset=false, double slew=0)
Construct a new PID controller.
bool getSignFlipReset()
Check if sign-flip integral reset is enabled.
double update(double error, double measurement)
Update the PID controller based on error and actual measurement (to prevent derivative kick).
void reset()
Reset the PID controller's internal state (integral, previous error, etc.).
void setIntegralLimit(double limit)
Set the maximum absolute value for the integral term.
Structure holding PID controller gains and parameters.
double kF
Feed-forward gain.
double slew
Maximum allowed change in output per update (slew rate)
double kP
Proportional gain.
double kD
Derivative gain.