QuickPID is an updated implementation of the Arduino PID library with additional features for PID control. By default, this implementation closely follows the method of processing the p,i,d terms as in the PID_v1 library except for using a more advanced anti-windup mode. Integral anti-windup can be based on conditionally using PI terms to provide some integral correction, prevent deep saturation and reduce overshoot. Anti-windup can also be based on clamping only, or it can be turned completely off. Also, the proportional term can be based on error, measurement, or both. The derivative term can be based on error or measurement. PID controller modes include timer, which allows external timer or ISR timing control.
Development began with a fork of the Arduino PID Library. Modifications and new features have been added as described in the releases.
- New functions added:
SetProportionalMode
,SetDerivativeMode
andSetAntiWindupMode
-
timer
mode for calling PID compute by an external timer function or ISR - Proportional on error
pOnError
, measurementpOnMeas
or bothpOnErrorMeas
options - Derivative on error
dOnError
and measurementdOnMeas
options - New PID Query Functions
GetPterm
,GetIterm
,GetDterm
,GetPmode
,GetDmode
andGetAwMode
- New integral anti-windup options
iAwCondition
,iAwClamp
andiAwOff
QuickPID::QuickPID(float* Input, float* Output, float* Setpoint, float Kp, float Ki, float Kd,
pMode pMode = pMode::pOnError, dMode dMode = dMode::dOnMeas,
iAwMode iAwMode = iAwMode::iAwCondition, Action action = Action::direct)
Input
,Output
, andSetpoint
are pointers to the variables holding these values.Kp
,Ki
, andKd
are the PID proportional, integral, and derivative gains.pMode
is the proportional mode parameter with options forpOnError
proportional on error (default),pOnMeas
proportional on measurement andpOnErrorMeas
which is 0.5pOnError
+ 0.5pOnMeas
.dMode
is the derivative mode parameter with options fordOnError
derivative on error,dOnMeas
derivative on measurement (default).awMode
is the integral anti-windup parameter with an option foriAwCondition
(default) that is based on PI terms to provide some integral correction, prevent deep saturation and reduce overshoot. TheiAwClamp
option clamps the summation of the pmTerm and iTerm. TheiAwOff
option turns off all anti-windup.Action
is the controller action parameter which hasdirect
(default) andreverse
options. These options set how the controller responds to a change in input.direct
action is used if the input moves in the same direction as the controller output (i.e. heating process).reverse
action is used if the input moves in the opposite direction as the controller output (i.e. cooling process).
QuickPID::QuickPID(float* Input, float* Output, float* Setpoint,
float Kp, float Ki, float Kd, Action action)
This allows you to use Proportional on Error without explicitly saying so.
QuickPID::QuickPID(float *Input, float *Output, float *Setpoint)
This simplified version allows you to define the remaining parameters later via specific setter functions. By default, Kp, Ki, and Kd will be initialized to zero and should be later set via SetTunings
to relevant values.
bool QuickPID::Compute();
This function contains the PID algorithm and it should be called once every loop(). Most of the time it will just return false without doing anything. However, at a frequency specified by SetSampleTime
it will calculate a new Output and return true.
void QuickPID::Initialize();
Does all the things that need to happen to ensure a bump-less transfer from manual to automatic mode.
These functions query the internal state of the PID.
float GetKp(); // proportional gain
float GetKi(); // integral gain
float GetKd(); // derivative gain
float GetPterm(); // proportional component of output
float GetIterm(); // integral component of output
float GetDterm(); // derivative component of output
uint8_t GetMode(); // manual (0), automatic (1) or timer (2)
uint8_t GetDirection(); // direct (0), reverse (1)
uint8_t GetPmode(); // pOnError (0), pOnMeas (1), pOnErrorMeas (2)
uint8_t GetDmode(); // dOnError (0), dOnMeas (1)
uint8_t GetAwMode(); // iAwCondition (0, iAwClamp (1), iAwOff (2)
These functions set the internal state of the PID.
void SetMode(Control mode); // Set PID mode to manual, automatic or timer
void SetOutputLimits(float Min, float Max); // Set and clamps the output to (0-255 by default)
void SetTunings(float Kp, float Ki, float Kd, // set pid tunings and all computational modes
pMode pMode, dMode dMode, iAwMode iAwMode);
void SetTunings(float Kp, float Ki, float Kd); // only set pid tunings, other pid modes are unchanged
void SetControllerDirection(Action Action); // Set controller action to direct (default) or reverse
void SetSampleTimeUs(uint32_t NewSampleTimeUs); // Set PID compute sample time, default = 100000 µs
void SetProportionalMode(pMode pMode); // Set pTerm based on error (default), measurement, or both
void SetDerivativeMode(dMode dMode); // Set the dTerm, based error or measurement (default).
void SetAntiWindupMode(iAwMode iAwMode); // Set iTerm anti-windup to iAwCondition, iAwClamp or iAwOff
Get sTune
![PlatformIO Registry](https://camo.githubusercontent.com/2d232735d8c749b3f037cb2f2abe83fe70bf7f41264d17064708113b775db9b5/68747470733a2f2f6261646765732e72656769737472792e706c6174666f726d696f2e6f72672f7061636b616765732f646c6c6f796465762f6c6962726172792f7354756e652e737667)
A very fast autotuner capable of on-the-fly tunings and more. Example: Autotune_QuickPID.ino