Almost all autopilots use PID algorythms in one way or another. Now there's a basic PID library in the standard Arduino software. Even better, here's a tutorial series from Brett Beuregard on how to use it!
In conjunction with the release of the new Arduino PID Library I’ve decided to release this series of posts. The last library, while solid, didn’t really come with any code explanation. This time around the plan is to explain in great detail why the code is the way it is. I’m hoping this will be of use to two groups of people:
- People directly interested in what’s going on inside the Arduino PID library will get a detailed explanation.
- Anyone writing their own PID algorithm can take a look at how I did things and borrow whatever they like.
It’s going to be a tough slog, but I think I found a not-too-painful way to explain my code. I’m going to start with what I call “The Beginner’s PID.” I’ll then improve it step-by-step until we’re left with an efficient, robust pid algorithm.
Comments
Derivative kick is from changing the set point and not the sensor data. last week I added a better Moving average filter to clean up those spike from the sensor values in the PID library. I'll post once I can thoroughly test, but the results are very good so far. They should really help Loiter as well as alt hold.
Jason
From my experience, using error in derivative term is acceptable only for PID's where SP is fixed.
If SP is changing, using error=SP-PV in derivative is not so good idea. I always used for derivative below solution. Is working well for both airplanes and multicopters.
Error=Pv-Pv_last
Pv_last=Pv
Deriv=Kd*Error
Great addition and simple explanation to computer code.
I think in addition, a defintion of k_i,k_p,k_d (gains) are in order and a quick tuning relationship map associated to quadrotors would be nice. Like:
"tune k_i first (minimizing k_p=0/k_d=1),
then k_p, until you have stable/level flight,
then k_d to account for different environmental conditions,
and lastly adjust k_i more for reaction sensitivity".