View Single Post
Old 05-07-2015, 12:06 PM   #7049 (permalink)
MPaulHolmes
PaulH
 
MPaulHolmes's Avatar
 
Join Date: Feb 2008
Location: Maricopa, AZ (sort of. Actually outside of town)
Posts: 3,832

Michael's Electric Beetle - '71 Volkswagen Superbeetle 500000
Thanks: 1,368
Thanked 1,202 Times in 765 Posts
I didn't do the PI loop the traditional way. I did it like this:

Let P and I be the constants. error(t) is the error between the desired current, and the actual current. pwm(t) is the duty at time t.

pwm(t) = P*error(t) + I*INTEGRAL(error(TAU)), where the integral goes from 0 to t, hence the dummy variable TAU (remember that in calculus? hehe. They always use TAU! What's up with that?).


Differentiate both sides:

(2) d (pwm)/dt = P*d(error)/dt + I*error(t), by the Fundamental Theorem of Calculus. (The one where differentiating the integral leaves the function)


Now, I approximated d(error)/dt and d(pwm)/dt by finding the slope of two points for a small time interval, like 0.001, which was my throttle sampling rate back in the day, and substitute it into equation (2).



So, d(error)/dt IS REPLACED WITH (errorNew - errorOld)/deltaT. (remember the difference quotient. We just aren't letting h --> 0. Just using small h)



Also, d(pwm)/dt IS REPLACED WITH deltaPWM/deltaT.



Also, replace error(t) with errorOld. (You could use either errorOld or errorNew if you wanted I think).



So, after substituting, Equation (2) becomes:
(2b) deltaPWM/deltaT = P*(errorNew - errorOld)/deltaT + I*errorOld

Now, multiply both sides by deltaT:

(3) deltaPWM = P*(errorNew - errorOld) + I*deltaT*errorOld

Now, distribute P, and get all the errorOld stuff together:



(4) deltaPWM = P*errorNew - P*errorOld + I*deltaT*errorOld

(5) deltaPWM = P*errorNew + (I*deltaT - P)*errorOld


So, kp = P, and ki = I*deltaT - P. kp and ki are constants, because I, P, and deltaT are constant.
__________________
kits and boards
  Reply With Quote
The Following User Says Thank You to MPaulHolmes For This Useful Post:
artc (05-07-2015)