Ok here’s PID:
**Q. First, what is this PID thing and what does it do? **
A. PID is a control loop, meaning it helps you take a given value like current_temperature and get it to a desired value, say desired_temperature.
**Q. OK, how do I do it. **
A. PID has two parts, the code (which is easy) and the tuning (which can be harder).
First lets go over the code. If you Google the subject you will find a crap-ton of complicated looking math, welcome to calculus. Fortunately you don’t need to know calculus to use a PID loop but you should know that calculus is at work here.
For now, know that PID is a acronym for Proportional Integral Derivative.
It has three parts, namely the Proportional, the Integral and the Derivative, super complicated right?
The proportional is simple, it’s just your value. Essentially here, heres a number.
The integral is a sum.
And finally the derivative is a rate of change.
So what is PID? Well PID combines these functions with the use of a variable called error.
Error is clearly defined as desired value minus the actual value. Our PID loop (when tuned correctly) will fight to keep error close to zero.
Another important part of this control loop is time. Actually more specifically it is the interval between cycles, but for simplicities sake we’ll just call it time.
And finally we need a variable to hold the last calculated error, lets call this oldError.
So heres the loop:
http://polynomic3d.com/user/smith/pid.jpg
Text version here.
As for tuning, several methods exist. I haven’t yet experimented enough to find my favorite, but I will soon enough. For now I suggest reading into the subject of PID tuning.
I hope this helps you guys wrap your head around the concept. Just note that the code provided here only works with ROBOTC because only ROBOTC supports tasking.
Also, this code uses integers but I would recommend using floats. This was code that we wrote for Clean Sweep but never ended up using. You’ll want the added accuracy of floats for this.
Oink!
-Cody