Problem found (and solved) in quadrature encoder code  [SOLVED]

Post Reply
User avatar
johu
Site Admin
Posts: 7182
Joined: Thu Nov 08, 2018 10:52 pm
Location: Kassel/Germany
Has thanked: 552 times
Been thanked: 1913 times
Contact:

Problem found (and solved) in quadrature encoder code

Post by johu »

So this weekend I worked with Damien and Zero EV on a strange problem: when stepping on the accelerator at high motor speed you would get an INCREASING amount of regen until you sort of "punched through" and got acceleration again. Apparently this happened to a degree where the car would be dangerous to others.

The culprit was in the quadrature encoder code. Basically, the timer hardware handles that by itself, you only have to tell it how many pulses to expect per turn and it will wrap around correctly when one turn is completed. Now, a nominal n-step quadrature encoder actually generates 4 usable edges: rising and falling on each channel. So you'd program the counter to wrap at 4*n.

But would you really? Doing that would mean 360° != 0°. So you introduce one extra step per turn and that throws off position and frequency tracking. So what you really want to do is wrap at 4*n-1.

For the Tesla encoder with its 36 steps per turn this means we interpreted 145 edges as one turn instead of 144. So at 144Hz you are 1Hz off. So 1Hz of slip becomes 0Hz, together with various rounding errors it actually becomes negative slip. And that is regen. Obviously this becomes worse at higher speed.

I will draft a new release asap. For those who can't wait, the source code is on github.
Support R/D and forum on Patreon: https://patreon.com/openinverter - Subscribe on odysee: https://odysee.com/@openinverter:9
arber333
Posts: 3795
Joined: Mon Dec 24, 2018 1:37 pm
Location: Slovenia
Has thanked: 166 times
Been thanked: 411 times
Contact:

Re: Problem found (and solved) in quadrature encoder code

Post by arber333 »

I noticed like a year back i couldnt shift anymore if i just set my BMS regen reduction to -1 like before. Now i have to actually set to 0 so that motor would release torque. Is that also the consequence of this error?
jon volk
Posts: 574
Joined: Wed Apr 10, 2019 7:47 pm
Location: Connecticut
Been thanked: 3 times

Re: Problem found (and solved) in quadrature encoder code

Post by jon volk »

Awesome! Ive learned to drive around this quirk but glad to hear theres a solution.
Formerly 92 E30 BMW Cabrio with Tesla power
User avatar
Jack Bauer
Posts: 4000
Joined: Wed Dec 12, 2018 5:24 pm
Location: Ireland
Has thanked: 153 times
Been thanked: 1116 times
Contact:

Re: Problem found (and solved) in quadrature encoder code

Post by Jack Bauer »

Not only a solution but the E31 feels like it has a rocket engine in the back after this update:)
I'm going to need a hacksaw
yaroslav
Posts: 176
Joined: Tue Aug 13, 2019 4:32 pm
Location: Russia, Stavropol
Has thanked: 14 times
Been thanked: 5 times

Re: Problem found (and solved) in quadrature encoder code

Post by yaroslav »

Good evening, I do that when skatyvaetsya ago, with the included front transmission starts to spontaneously accelerate backwards, the leg is not even in the gas pedal, not whether problems?
User avatar
johu
Site Admin
Posts: 7182
Joined: Thu Nov 08, 2018 10:52 pm
Location: Kassel/Germany
Has thanked: 552 times
Been thanked: 1913 times
Contact:

Re: Problem found (and solved) in quadrature encoder code

Post by johu »

yaroslav wrote: Tue Sep 24, 2019 3:39 pm Good evening, I do that when skatyvaetsya ago, with the included front transmission starts to spontaneously accelerate backwards, the leg is not even in the gas pedal, not whether problems?
Not sure if I unterstand your question correctly, but spontaneous reversal would not be related to this problem
Jack Bauer wrote: Tue Sep 24, 2019 3:36 pm Not only a solution but the E31 feels like it has a rocket engine in the back after this update:)
Yes with the actual configured slip on tap I'd expect that :)
arber333 wrote: Tue Sep 24, 2019 8:26 am I noticed like a year back i couldnt shift anymore if i just set my BMS regen reduction to -1 like before. Now i have to actually set to 0 so that motor would release torque. Is that also the consequence of this error?
Possibly yes, if it happened after you switched from single channel to quadrature encoder?
Support R/D and forum on Patreon: https://patreon.com/openinverter - Subscribe on odysee: https://odysee.com/@openinverter:9
arber333
Posts: 3795
Joined: Mon Dec 24, 2018 1:37 pm
Location: Slovenia
Has thanked: 166 times
Been thanked: 411 times
Contact:

Re: Problem found (and solved) in quadrature encoder code

Post by arber333 »

Er... Johannes. Can you compile for us non software minded 😎
User avatar
johu
Site Admin
Posts: 7182
Joined: Thu Nov 08, 2018 10:52 pm
Location: Kassel/Germany
Has thanked: 552 times
Been thanked: 1913 times
Contact:

Re: Problem found (and solved) in quadrature encoder code

Post by johu »

Sure :)
It also contains another change that I'm not 100% sure about: throtramp now applies to every throttle operation. 0-100, 100 back to 50, 50 to -40 etc. Throttle is just always ramped. With a slow ramp that car feels a bit weird as it doesn't stop accelerating at the instance you come off throttle.

Please try it.
Attachments
stm32_sine.zip
(62.96 KiB) Downloaded 213 times
Support R/D and forum on Patreon: https://patreon.com/openinverter - Subscribe on odysee: https://odysee.com/@openinverter:9
arber333
Posts: 3795
Joined: Mon Dec 24, 2018 1:37 pm
Location: Slovenia
Has thanked: 166 times
Been thanked: 411 times
Contact:

Re: Problem found (and solved) in quadrature encoder code

Post by arber333 »

johu wrote: Tue Sep 24, 2019 7:21 pm Sure :)
It also contains another change that I'm not 100% sure about: throtramp now applies to every throttle operation. 0-100, 100 back to 50, 50 to -40 etc. Throttle is just always ramped. With a slow ramp that car feels a bit weird as it doesn't stop accelerating at the instance you come off throttle.

Please try it.
Thanks, i will try it today on Mazda! :twisted:
User avatar
johu
Site Admin
Posts: 7182
Joined: Thu Nov 08, 2018 10:52 pm
Location: Kassel/Germany
Has thanked: 552 times
Been thanked: 1913 times
Contact:

Re: Problem found (and solved) in quadrature encoder code  [SOLVED]

Post by johu »

New release: viewtopic.php?f=7&t=288

I've changed ramping a bit:
1. Throttle positive going is ALWAYS ramped with "throtramp"
2. Throttle negative going is NEVER ramped as long as above 0
3. Negative going, including brake pedal, below 0 is ramped with "regenramp"

I find throtramp=4 and regenramp=1 quite comfy.
Support R/D and forum on Patreon: https://patreon.com/openinverter - Subscribe on odysee: https://odysee.com/@openinverter:9
arber333
Posts: 3795
Joined: Mon Dec 24, 2018 1:37 pm
Location: Slovenia
Has thanked: 166 times
Been thanked: 411 times
Contact:

Re: Problem found (and solved) in quadrature encoder code

Post by arber333 »

Well... i find it much better! Throttle is finer control and it is not thrashing so much.
But i think cruise control is not working. Can you verify it is not on my side?

tnx
User avatar
johu
Site Admin
Posts: 7182
Joined: Thu Nov 08, 2018 10:52 pm
Location: Kassel/Germany
Has thanked: 552 times
Been thanked: 1913 times
Contact:

Re: Problem found (and solved) in quadrature encoder code

Post by johu »

Still works for me
Support R/D and forum on Patreon: https://patreon.com/openinverter - Subscribe on odysee: https://odysee.com/@openinverter:9
Post Reply