Proportional-Integral-Derivative (PID) Control

PID Control is one of the most ubiquitous control schemes, perhaps only surpassed only by bang-bang control. However when it comes to a PID controller available for embedded applications that includes industry standard features and will match the results obtained using MATLAB, it is hard to find! Therefore I have written a simple yet powerful PID controller with the following functionality:

Discrete
Required to be practically implementable on an embedded system.
2 Degree of Freedom (DOF)
Allows more flexibility in tuning the controller by providing the setpoint and plant output separately to the controller.
P, PI, PD and PID
Flexible controller type to suit the application.
Anti WindUp
Applies back calculation to prevent integral windup.
Process Variable Derivative
Adjust the derivative weight to include or exclude changes in the setpoint from the derivative calculation.
Derivative Filter
Includes an optional 1st order derivative filter to minimize the effect of noise.
Setpoint Weighting
Apply weights to the proportional and derivative terms to aid setpoint tracking tuning.
Setpoint Ramping
Limit the rate of change of the setpoint to help prevent system instability.
Tuning Guide
A handy guide to tuning PID controllers.
Stability Guide
A handy guide to ensuring a stable control system.
Native C
No external libraries other than the C standard library.
C++ Wrapper
If you prefer a C++ object, one is available with all functionality.
Documented
Fully documented source code suitable for Doxygen auto-generated output.
Unit Tested
Validated against the MATLAB Control System Toolbox pid2 class and Simulink Discrete 2DOF PID block.


Clone GNCTools to access the PID source, including MEX interfaces and MATLAB examples and unit tests, or alternatively jump into the PID Source Code to have a look! The examples via the links above can be found in Control/PID/PID_Examples.m.

Nomenclature

The following diagram shows the format of the GNCTools PID controller, together with variable naming.

VariableGNC Tools NameAlternative Name(s)
rSetpointReference
yPlant OutputProcess Variable, Process Measurement
ytrueTrue Plant OutputSimulated Plant Output
noiseNoiseProcess Noise, Unmeasured Disturbance
uControl InputManipulated Variable
umanManual Control InputManual Input