OK I'm back from my final today! I did some thinking and programming on paper! ya! Good idea Matt!
New control loop:
// iFeedback should be in 0 to 128, since that's all the resolution I have!
// Make sure iRef (from GetThrottlePos()) is also in the range 0 to 128,
// so you would need an extra step of shifting it. how annoying. maybe fix that.
// You want pwmDuty to be in 0 to 255.
// iFeedback in [0,128]
// iError in [0,128]
// pwmDuty in [0,255]
// iSum in [0,255]
// K1 in [0,1]
// K2 in [0,1]
// Start with K2 = 0.
//
void SetCurrent(void) {
iSum = 0;
do {
iFeedback = GetCurrent();
iError = iRef - iFeedback;
pwmDuty = K1*iErr + iSum;
OCR1A = pwmDuty;
iSum += K2*iErr;
if (iSum < 0)
iSum = 0;
} while (iFeedback != iRef);
}
void SetThrottle(void) {
iRef = GetThrottlePos();
GetTemperature();
//////////////////////////////////////////
// limit iRef based on temperature here //
//////////////////////////////////////////
SetCurrent(); // Sets the current to iRef.
}
That makes a lot of sense (throttle proportional to current only). I was just thinking that if there was a failure of the hall effect current sensor, that I would lose control of the car, and I don't know how failure proof those things are. But that wouldn't matter, would it?
Let's say you have a failure of the current sensor. It fails, being stuck reading an erroneous 499 amps. The PI loop would go forever (never exiting), and pwm duty would be driven to 0%, right, desperately (do computers get desperate?) trying to make the current drop. In that case, the car stops. So what! That's fine.
Now let's say you have a failure of the current sensor where it's stuck reading an erroneous 0 amps. Then if the throttle is even at 1%, the loop will drive the PWM duty all the way to 100%, trying desperately (hehe) to get the current to be 1% of 499 amps. This would be bad. But if the throttle was 0%, then the car would stop. OK. I think that's an acceptable risk. If the current sensor fails, just remove foot from gas pedal. Not catastrophic.
Now I need to tune the PI loop, to find the just right K1 and K2 values in the range of 0 to 1. Hey, I get to use fixed point math! I used to do that when making video games on my piece of crap 386 computer! ya!
|