Lightweight universal PID controller library with Ziegler–Nichols auto tuning and variable cycle time
PetalPID is a floating-point based PID library that can be used on Arduino as well as on any other platform. Even in regular C++ code (it is possible to convert it to pure C). Unlike other libraries, PetalPID uses variable cycle time and the same function for the main and tuning loop, making it very easy to implement.
🚧 This's not the release version of PetalPID! Please be careful!
Example of auto-tuning process (this graph is produced by
examples/TemperatureController
)
-
Download or include this repo as a library
Example for PlatformIO:
lib_deps = https://github.com/F33RNI/PetalPID.git
-
Include header file in your project
#include <PetalPID.h>
-
Create PetalPID instance
PetalPID pid = PetalPID(); // Or PetalPID pid = PetalPID(1.f, 2.f, 3.f, 0.f, 255.f);
-
Setup limits and gains (if you're not starting with auto-tune)
pid.set_min_max_output(0.f, 255.f); pid.set_min_max_integral(-1000.f, 1000.f); pid.set_gains(1.f, 2.f, 3.f);
-
In a main loop...
// Without any delays. Call as frequent as possible float pid_output = pid.calculate(measured_value, setpoint, micros());
-
Starting auto-tuning
// TYPE_P, TYPE_PI, TYPE_PD, TYPE_CLASSIC_PID, TYPE_PESSEN_INTEGRAL_RULE, TYPE_SOME_OVERSHOOT, TYPE_NO_OVERSHOOT // 50 on-off cycles pid.start_auto_tune(TYPE_NO_OVERSHOOT, 50);
-
Getting results
Serial.print(pid.get_p()); Serial.print("\t"); Serial.print(pid.get_i()); Serial.print("\t"); Serial.println(pid.get_d());
More info in 📄 Documentation
section and examples/
You can build Doxygen documentation using provided Doxygen
file
Just clone repo and run:
doxygen
This will generate HTML and LaTeX docs. Open docs/index.html
file to view it
- in17clock by Fern Lane
IN17 Nixie tube clock with internal DC-DC converter, random melody alarm and temperature/humidity sensor
- ilet-103-motor-controller be Fern Lane
Digital speed controller for ILET 103 reel-to-reel deck
Does your project also use PetalPID? Add it to this list by creating a pull request or issue!
- Clone this repo
git clone https://github.com/F33RNI/PetalPID
- Follow the
.clang-format
style while editing the code - Follow Conventional Commits while creating commits
- Create pull request