I thought of 2 worst cases. In one case, I think my method is a little faster than a control loop. In the other case, the control loop is significantly faster.
CASE 1: Parked facing up hill. Turn on car. Hit accelerator to floor INSTANTANEOUSLY.
Here's what my code does :
First iteration of loop: Throttle goes from 0 to 255 instantly (max is 255). Increment PWM duty by 1. PWM duty is now 1 (out of 255). Read current. Let's say it's 50% of MAX_CURRENT (it grows fast at low RPM. hehe). OK, no problem, because it's allowed to be 100% of MAX_CURRENT, because throttle is at 100%.
Next iteration of loop: Throttle is 255. Increment PWM duty by 1. PWM duty is now 2. Read current. Current is (let's say) OVER MAX_CURRENT! Oh No! While current > MAX_CURRENT, decrement PWM duty by 1. So, after a single iteration of that while loop, PWM duty is 1 (out of 255), and current is once again safely under MAX_CURRENT.
It is now in a sort of steady state, where PWM is right around where it should be to limit current to what it should be. Limiting the growth of PWM duty to 1 per iteration lets me get away with the "increment by 1, decrement by 1" method.
CASE 2: You are revving your motor with your wheels jacked up. The throttle is 255. The PWM duty is 255. the current is very small, let's say 1% of MAX_CURRENT. Suddenly, (you are very strong) you grab the tires and almost instantaneously stop the wheel from spinning. Suddenly, the current grows very fast, while PWM duty is 255.
Decrement PWM duty by 1. Check current. TOO HIGH!!!
Decrement PWM duty by 1. Check current. TOO HIGH!!!
Decrement PWM duty by 1. Check current. TOO HIGH!!!
Decrement PWM duty by 1. Check current. TOO HIGH!!!
...
etc. Controller is now on fire...
I think I'll use a simple PI loop where the constants are chosen so as to allow shifting and adding. Thanks, MazdaMatt!
Last edited by MPaulHolmes; 01152009 at 02:46 AM..
