Code idea help please..
I have a program that uses a 16 bit variable number between 0-4095 $000-FFF to control electric motor power.
It also uses a flag 'F' (high nibble) in that same 16 bit variable to indicate assist or regeneration.
So hex...
0(000) would be 0% Assist and 0(FFF) would be 100% Assist
F(FFF) would be 0% Regen and F(000) would be 100% Regeneration
Assist 100%..............0%------- 0%................ 100% Regen
0FFF.......................0000------FFFF..................F000
Now I want to gently/smoothly swap from one to the other.
So lets assume my OEM motor controller wants some assist but I am currently overriding it with some regeneration.
Actual say F(999) Regenerating
Desired by controller 0(345) Assist
I want to change the actual in steps until it matches the desired and passes smoothly through the changeover.
So in other words in the example above it ramps down the regeneration until it passes the null zone then (changes the flag nibble) and then ramps up the assist until it matches the desired.
Note the code needs to be able to cope with any combination of assist/regen actual and desired values.
So it could be assisting (Actual) and I want to go back to regen (Desired) or vice versa.
Or it could be assisting at say 10% but the OEM wants 90% assist or vice versa etc etc etc
Basically when I stop overriding the motor controller I want a smooth transition back to what the OEM is requesting.
At the moment my code just goes instantly from in the worst case scenario 100% assist to 100% regen!!!
That's a bit of a shock to the drivetrain....
The control variable is generated every 10ms and I would like the revert to normal (desired) process to take about 1 second
So I'm looking for code ideas as I seem to be stuck in loop staring at the screen.
Note the (desired) target can change at any time during the revert process.
So it has to be able to deal with moving goalposts.
Getting actual to within say +/-10 or even 50 of desired would likely be good enough that the ramp routine could then abort and relinquish control.
Bookmarks