The PIDController library allows you to easily run a PID controller on a microprocessor such as Arduino.
PIDController is based on the Arduino PID library, a great work done by Brett Beauregard. In this post by the author, the improvements implemented with respect to a basic PID controller are detailed.
However, the Arduino PID library has not been updated since 2017. PIDController continues the work on this library, while adapting it to new times and processors.
Although it is based on Arduino PID, the PIDController library has been rewritten from scratch. Therefore, the code has been refactored and cleaned up to make it more easily understandable, maintainable, and expandable.
Therefore, the PIDController library is not backward compatible with Arduino PID. Projects will need to be adapted to the new library, as explained in “Differences with Arduino PID”.
Once the code has been adapted to the library, the response obtained with PIDController to the same inputs and controller parameters will be the same as that obtained with Arduino PID.
Usage
Here is an example of using the PIDController library.
#include <PIDController.hpp>
const int PIN_INPUT = 0;
const int PIN_OUTPUT = 3;
PID::PIDParameters<double> parameters(4.0, 0.2, 1);
PID::PIDController<double> pidController(parameters);
void setup()
{
pidController.Input = analogRead(PIN_INPUT);
pidController.Setpoint = 100;
pidController.TurnOn();
}
void loop()
{
pidController.Input = analogRead(PIN_INPUT);
pidController.Update();
analogWrite(PIN_OUTPUT, pidController.Output);
}
Changes with respect to Arduno-PID-Library
The PIDController library incorporates the following improvements over the Arduino PID library. Your project code will need to be adapted to take these changes into account.
- The PID class is renamed PIDController.
- The PID namespace is used to avoid collisions
- Templating is used, so now it is possible to use both
double
andfloat
- Input, Output, and SetPoint are no longer pointers, but internal variables
- The
Calculate()
method is renamed toUpdate()
- The
SetMode
method is replaced by the functionsTurnOn
,TurnOff
, andToggle
- Added method
Update(T input)
that simultaneously sets Input and calls theUpdate()
method - The Kp, Ki, and Kd parameters are now wrapped in a
PIDParameters
class - #define for options are now enumerations.
Other PIDController Functions
Other features added to PIDController are as follows
- Added
Resolution
parameter to work with bothmillis()
andmicros()
- Added the
ForzeUpdate()
method to update even when the sampling interval has not passed. (Not the preferred option, but necessary in certain projects) - Added separate parameters for OutpuxMin, OutputMax, and AntiWindupMin, AntiWindupMax
- Added abstract classes
IController
andISisoController
- Added
PIDParametersAdaptative
class to simplify the use of adaptive PID. - Added
Get...()
methods for all controller properties - Added methods to obtain the contribution of each term (GetTermP, GetTermI, GetTermD)
- Added trapezoidal integration for improved integral term calculation
Installation
- Download the latest version from GitHub
- Unzip the file
- Copy to your libraries folder (usually My Documents\Arduino\libraries)
- Restart the Arduino IDE