posted on
Saturday, April 26, 2008 9:06 PM |
I've been reading up on PID controllers and they actually make sense now. It is a nice Saturday in St. Louis and I'm inside researching PID controllers. Yes, I'm a nerd. A very large nerd. A nerd who's toy isn't playing nicely with him.
So here is the break down of a PID controller as I see it along with how I'll be implementing it.
P = Short term corrections
I = Adds long-term precision
D = This gives you a rough estimate of the velocity (delta position/sample time), which predicts where the position will be in a while. (quote from PID without a PhD)
The D directly relates to my Gyro. It measures change in degrees per second.
* images from PID without a PhD too
With only P
With PI
![0010feat3fig14[1]](http://betterthaneveryone.com/images/PIDinanutshell_128C8/0010feat3fig141_thumb.gif)
With a full PID system
![0010feat3fig18[1]](http://betterthaneveryone.com/images/PIDinanutshell_128C8/0010feat3fig181_thumb.gif)
pseudo code from the PID without a PhD site:
typedef struct {
double dState; // Last position input
double iState; // Integrator state
double iMax, iMin; // Maximum and minimum allowable integrator state
double iGain, // integral gain
pGain, // proportional gain
dGain; // derivative gain
} SPid;
double UpdatePID(SPid * pid, double error, double position) {
double pTerm, dTerm, iTerm;
pTerm = pid->pGain * error; // calculate the proportional term
// calculate the integral state with appropriate limiting
pid->iState += error;
if (pid->iState > pid->iMax)
pid->iState = pid->iMax;
else if (pid->iState < pid->iMin)
pid->iState = pid->iMin;
iTerm = pid->iGain * iState; // calculate the integral term
dTerm = pid->dGain * (position - pid->dState);
pid->dState = position;
return pTerm + iTerm - dTerm;
}