PDA

View Full Version : Ramp up down? Code ideas.



retepsnikrep
- 24th November 2023, 09:10
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.

Jerson
- 25th November 2023, 02:53
Consider this.

The error between expected and actual output be summed every second in sE

Now, decide the correction time as Ti or error Integration time in seconds. The aim is to apply the full sE in Ti seconds from the time of Error inception.

The fraction of E that gets applied to regen output will be sE / Ti

sE will be reduced by the above fraction.

To prevent a lockup condition from which your control cannot recover, you will need to put limits (both +ve and -ve) on the value of sE so that it doesn't do fancy things like rollover on addition

The output will change at the rate you specified.

Good luck with that and Happy thanksgiving

richard
- 25th November 2023, 05:39
if you were to use proper complementary numbers to represent your controller power then


F(999) could be -999 and 0(345) would be 345

ie actual = -999 desired = 345
-999-345=-1344
means desired is -1344 below required
to get there in 100 x 10mS steps [1 second] just add 13.4 for the next 100 generations


to go back
345-(-999)=1344


means desired is 1344 over required


to get there in 100 x 10mS steps [1 second] just subtract 13.4 for the next 100 generations


the controller can figure if its assist or regen simply by testing bit.15


by rounding down the incrementor you will easily get within 10% will no fuss
never underestimate the beauty of unsigned subtractions with complementary numbers


to convert from pos to negative number n=~n+1
to convert from negative to pos number n=~n+1



ps never try to divide or multiply a negative number
ie.
inc = abs(difference)/100

retepsnikrep
- 25th November 2023, 06:43
Thanks for those. :smile: