PID library for Arduinos with greater accuracy than the legacy Arduino PID library.
PID stands for Proportional, Integral, and Derivative and is a simple type of controller. PID controllers can provide stable control of nearly any system such as the speed of a motor, position of a servo, speed of a car (cruise control), and much more.
See the explanation video here
See here
First import the library, instantiate an ArduPID class, and create 6 doubles:
#include "ArduPID.h"
ArduPID myController;
double setpoint = 512;
double input;
double output;
double p = 1;
double i = 0;
double d = 0;
Next, within the setup()
function, initialize the controller with the references to the input, output, and setpoint variables. Also pass the values of the PID gains. After initializing the controller within setup()
, you can change the settings/configuration of the controller.
void setup()
{
Serial.begin(115200);
myController.begin(&input, &output, &setpoint, p, i, d);
// ADD MORE CONFIGURATION COMMANDS HERE <--------------------
}
Here are the different examples of configuration commands available via the library:
reverse()
setSampleTime(const unsigned int& _minSamplePeriodMs)
_minSamplePeriodMs
have past between successful compute() calls - this function is not necessary, but is included for conveniencesetOutputLimits(const double& min, const double& max)
min
and max
setBias(const double& _bias)
_bias
, usually used in conjunction with setOutputLimits()
setWindUpLimits(const double& min, const double& max)
min
and max
to prevent integral wind-upstart()
reset()
stop()
compute()
will not do anything until start()
is called)Within the loop()
, update the input variable via your sensor and then call compute()
. The output variable will then be automatically updated and you can use that updated value to send an updated command to your actuator. You can also use the debug()
command to print various status information about your controller to the serial plottor/monitor:
void loop()
{
input = analogRead(A0); // Replace with sensor feedback
myController.compute();
myController.debug(&Serial, "myController", PRINT_INPUT | // Can include or comment out any of these terms to print
PRINT_OUTPUT | // in the Serial plotter
PRINT_SETPOINT |
PRINT_BIAS |
PRINT_P |
PRINT_I |
PRINT_D);
analogWrite(3, output); // Replace with plant control signal
}