PwmGeneration::slipIncr unsigned  [SOLVED]

Post Reply
User avatar
catphish
Posts: 954
Joined: Fri Oct 08, 2021 11:02 pm
Location: Dorset, UK
Has thanked: 93 times
Been thanked: 179 times

PwmGeneration::slipIncr unsigned

Post by catphish »

I've been going through all the sine code making sure I understand it. I am currently confused by the value PwmGeneration::slipIncr.

It appears that this contains the number of increments of rotation for the current value of slip. However, slip is signed, and will definitely be negative during regen. However slipIncr is unsigned. Is this a bug? It works but I'm not sure if it works by accident, or whether I'm missing some intended behaviour here?

Any input from Johannes or anyone else who really understand this code would be appreciated.

Thanks, and sorry for my current flood of posts, I'm really getting my teeth into this :)
User avatar
johu
Site Admin
Posts: 5682
Joined: Thu Nov 08, 2018 10:52 pm
Location: Kassel/Germany
Has thanked: 153 times
Been thanked: 960 times
Contact:

Re: PwmGeneration::slipIncr unsigned

Post by johu »

I'd say it works half by accident as all calculations are modulo 2¹⁶ . So -1 becomes 65535. Say you have a motor angle of 30000 then subtracting -1 makes it 29999 and adding 65535 modulo 2¹⁶ also 29999.
Support R/D and forum on Patreon: https://patreon.com/openinverter - Subscribe on odysee: https://odysee.com/@openinverter:9
User avatar
catphish
Posts: 954
Joined: Fri Oct 08, 2021 11:02 pm
Location: Dorset, UK
Has thanked: 93 times
Been thanked: 179 times

Re: PwmGeneration::slipIncr unsigned  [SOLVED]

Post by catphish »

johu wrote: Tue Jan 10, 2023 4:21 pm I'd say it works half by accident as all calculations are modulo 2¹⁶ . So -1 becomes 65535. Say you have a motor angle of 30000 then subtracting -1 makes it 29999 and adding 65535 modulo 2¹⁶ also 29999.
Thanks!

I thought that might be the case. It certainly appears to work. If it was just adding and subtracting the unsigned ints I wouldn't have worried too much as overflow is expected and useful, but there's also a multiplication on it. I guess this works, but it makes me uncomfortable.

Code: Select all

slipAngle += dir * slipIncr;
Changing it to signed doesn't break anything so I will include the change as a fix in my patch set.
Post Reply