IPM Motor Simulation and FOC Software

User avatar
Ev8
Posts: 818
Joined: Sat Jan 30, 2021 11:05 am
Has thanked: 43 times
Been thanked: 163 times

Re: IPM Motor Simulation and FOC Software

Post by Ev8 »

Ahh ok cool well either way it’s certainly better
User avatar
johu
Site Admin
Posts: 6713
Joined: Thu Nov 08, 2018 10:52 pm
Location: Kassel/Germany
Has thanked: 368 times
Been thanked: 1542 times
Contact:

Re: IPM Motor Simulation and FOC Software

Post by johu »

More test driving, 9 and B uphill, A and C downhill

First uphill, fwkp=5, fwki=40, fwmargin=3000. Field weakening kicks in early enough but causes oscillation in both id and iq
run9.png
Now downhill, fwkp=5, fwki=10, fwmargin=3000. Field weakening kicks in but as soon as I enter regen the inverter faults out with DESAT! I haven't seen that for ages. Maybe the overflow in the inverse transform?
runA.png
And uphill again, fwkp=5, fwki=1, fwmargin=3000. Pretty good, feels stable while on throttle but when getting near the coasting point oscillation comes back in. We still see some voltage saturation but it is resolved.
runB.png
And the last run, downhill, fwkp=5, fwki=1, fwmargin=4000. Regen comes in even at regen but the whole thing still looks far from stable.
runC.png
Another method could be removing fwki and instead filter the amplitude. Of course it slows down response but it seems like the FW controller doesn't have to be very fast. With the filter fwkp could be raised again.
EDIT: might also be worthwhile to run the torque throttling in a separate controller as you did.
Support R/D and forum on Patreon: https://patreon.com/openinverter - Subscribe on odysee: https://odysee.com/@openinverter:9
Pete9008
Posts: 1801
Joined: Sun Apr 03, 2022 1:57 pm
Has thanked: 102 times
Been thanked: 350 times

Re: IPM Motor Simulation and FOC Software

Post by Pete9008 »

Getting there though.

Did you try winding fwkp right down? I found anything above 1 started to cause oscillation. I think it is because the controller is run much less often now it needs to rely a lot more on the i than the p.
User avatar
Bigpie
Posts: 1771
Joined: Wed Apr 10, 2019 8:11 pm
Location: South Yorkshire, UK
Has thanked: 82 times
Been thanked: 422 times

Re: IPM Motor Simulation and FOC Software

Post by Bigpie »

I did a build of latest code and give it a little test. Feels perkier. 0.6 ldminuslq, 90 fluxlinkage, 0 syncadv

Will have more time to mess with params at the weekend.
Screenshot 2022-09-08 at 5.26.11 pm.png
The more interesting bit of the graph had gone off screen by the time I'd stopped :D
Attachments
stm32_foc.bin
(48.35 KiB) Downloaded 283 times
BMW E91 2006
ZombieVerter
GS450h
Outlander Charger DC/DC
Renault Kangoo 36kWh battery
FOCCCI CCS
User avatar
johu
Site Admin
Posts: 6713
Joined: Thu Nov 08, 2018 10:52 pm
Location: Kassel/Germany
Has thanked: 368 times
Been thanked: 1542 times
Contact:

Re: IPM Motor Simulation and FOC Software

Post by johu »

Good to see, that was Outlander+Prius, right?
Bigpie wrote: Thu Sep 08, 2022 4:28 pm The more interesting bit of the graph had gone off screen by the time I'd stopped :D
Sounds familiar. I now put the focus on the pause button so I just need to hit space when something interesting happens

Speaking of which... The FW-controller seems to output something meaningful but then the d-controller has trouble implementing it at higher speeds. To I took a closeup and we see the two controllers awfully oscillate. I'm thinking the PI gains aren't right yet.
runE.png
So starting to think this through. In the pesky "new tricks" video ki=R/L. So bexander measured 75mOhm and Ld=2.15 mH, Lq=6.33 mH. So then ki_d=75/2.15=35 and ki_q=75/6.33=12. But I have set it to 20000. Or have I?

Above factor expects SI units, not weird digits. So lets say full amplitude 37500 yields 255V AC (360V DC input). A factor of 148. That leaves us with 20000/148=135. So from the supposed 20000 loop gain we are left with 135. Going back from that we'd end up with curki=35*148=5180 and 12*148=1776.

kp should be L*bandwidth in rad/s. Does that mean maximum desired electrical frequency? So curkp=2.18*600Hz*2*3.1415=8 and 24 for q-controller.

So that would be MGR parameters. Do we need to introduce a second set of curkp/ki parameters?

EDIT: above plots were done without MTPA and with filtered amplitude. Filtered in PWM ISR with filter constant of 8.
EDIT2: I've never seen felt or seen these oscillations with the d-current commanded by MTPA. So it's probably the quick variation in d-current that throws the controller off...
Attachments
runD.png
runC.png
Support R/D and forum on Patreon: https://patreon.com/openinverter - Subscribe on odysee: https://odysee.com/@openinverter:9
Pete9008
Posts: 1801
Joined: Sun Apr 03, 2022 1:57 pm
Has thanked: 102 times
Been thanked: 350 times

Re: IPM Motor Simulation and FOC Software

Post by Pete9008 »

johu wrote: Thu Sep 08, 2022 6:09 pm Speaking of which... The FW-controller seems to output something meaningful but then the d-controller has trouble implementing it at higher speeds. To I took a closeup and we see the two controllers awfully oscillate. I'm thinking the PI gains aren't right yet.
runE.png

So starting to think this through. In the pesky "new tricks" video ki=R/L. So bexander measured 75mOhm and Ld=2.15 mH, Lq=6.33 mH. So then ki_d=75/2.15=35 and ki_q=75/6.33=12. But I have set it to 20000. Or have I?

Above factor expects SI units, not weird digits. So lets say full amplitude 37500 yields 255V AC (360V DC input). A factor of 148. That leaves us with 20000/148=135. So from the supposed 20000 loop gain we are left with 135. Going back from that we'd end up with curki=35*148=5180 and 12*148=1776.

kp should be L*bandwidth in rad/s. Does that mean maximum desired electrical frequency? So curkp=2.18*600Hz*2*3.1415=8 and 24 for q-controller.
I think the equation for kp here is just taken from Xl=2πfL. Reactance is sort of V/I, if you ignore the phase shift, which is sort of the loop gain required to give the voltage needed to produce a given change in current. I'm not too keen on this and prefer to use the V = L di/dt version of the equation. For small t (such as the control loop period) this simplifies to V = L x I/t or V/I = L/t. If t is the loop period it becomes V/I = L x f (f in Hz). Think about it this way, if we have a 1A error at the start of the loop and we want to reduce this error to zero by the end of the loop how many volts do we need, V = L x f or 6.33mH x 8800 = 56V. Allowing for the voltage scaling would be 56 x 148 or 8288. This is a fairly simplistic approach but gives somewhere to start. Does the voltage compensation factor above include the effect of space vector modulation or should it be a little smaller?

I'm not too sure of the derivation of the ki equation but it looks like it is aiming to compensate for resistive winding losses. I wonder whether a better approach would be to try to calculate what is needed to compensate for the back emf on the iq term instead. Either way I'd treat it as a starting point rather than an absolute.

The trouble with treating the two separately is that they aren't, having a bit of kp means that you can get away with a bit less ki and vice versa. They also have different effects on stability.

The trouble is neither of these are intended to address stability, just to make sure there is enough gain in the system to meet the setpoint. Stability analysis is more complicated but essentially you need either enough phase margin, enough gain margin and preferably both. To understand phase margin imagine a system where you have an input that controls an output. The output is also subject to another signal that is varying with a period t. Your job on the control input is to keep the output constant regardless of the varying signal. One other factor, there is a delay between the input signal and the output. Now on a slowly moving signal it easy, the output goes up, you reduce the control input and the output comes back down. The trouble is that as the varying signal frequency increases you get to a point where the delay between the input and output means that by the time you see the output go up the input is already dropping so when you reduce the control signal to get the output back to where it should be you are actually making the error on the input worse and so a time delay on the output later you have twice the error on the output but in the opposite direction. This cycle continues with the output error getting bigger on each pass. This is instability due to inadequate phase margin. Gain margin basically says that for the output to get bigger on each cycle the gain needs to be greater than unity at that frequency. So to stay stable you can either reduce the gain or increase the phase margin but note if you have inadequate phase margin the output will still be a mess it just won't be getting worse each pass round the loop.

So what does this mean in the real world? First only use as much gain as you need, it's tempting to wind things up to try and get zero error but it can cause more problems. Second make sure you have phase margin. Now this is difficult as it's all about delays. The best fix is to try and reduce the delay as much as possible. If you can't do that you need to add extra filtering to compensate, you will hear about adding poles and zeros on the phase diagram to do that. I must admit I don't have much time for that approach - if you have a full characterisation of the system and can represent it accurately algebraically then you have a chance - I've never managed it. I also don't like the idea of coping with one set of non-ideal behaviour by adding another one to compensate - I'm sure it can work wonders if done right, I'm also sure it can make things worse if done wrong. I always end up trying to reduce the delays, or phase shift, to improve the phase margin first (which is why I'm keen to reduce latencies as much as possible) and keep the other approaches as a backup.

Having said all that, and sorry about the long, waffly, and overly technical post, the iq and id stability in those plots doesn't look too bad at all. Pretty much all the problem looks to be coming from the FW controller. The test would be turn off the FW and see how they look, if they are OK then the leave them be and concentrate on the FW controller gains.

Edit - some of the above isn't quite complete. If you have a control loop that is unstable well outside the frequency range in which you need it to work then the first thing you do is add filtering to kill the gain at that frequency. Increasing phase margin in that case, especially if it's at high frequencies, isn't usually a realistic option to fix it.

Edit2 - filtering also needs to be done carefully as if done wrong it can reduce the phase margin considerably and make stability worse
Pete9008
Posts: 1801
Joined: Sun Apr 03, 2022 1:57 pm
Has thanked: 102 times
Been thanked: 350 times

Re: IPM Motor Simulation and FOC Software

Post by Pete9008 »

When I simulate different FW controller gains this is what I get with very low fkkp:
1.png
As soon as the fwkp is increased it turns to this:
2.png
The second looks much closer to the results you are getting which makes me wonder whether this is the issue? Of course it could be the lack of noise in the simulation that allows low kp to work so well. I'll add a bit of noise and try it again.

The other issue I had with the fw controller was that the input range to the control meant that it was very one sided. To increase FW current the max error term could only be a few thousand (fwmargin), for decreasing it could be much bigger (GetMaximumModulationIndex() - fwmargin). This means that the controller output can move in one direction much faster than the other leading to an imbalance in the response. I found limiting the error term to prevent this helped with stability.
Pete9008
Posts: 1801
Joined: Sun Apr 03, 2022 1:57 pm
Has thanked: 102 times
Been thanked: 350 times

Re: IPM Motor Simulation and FOC Software

Post by Pete9008 »

Now that's interesting, noise didn't make a difference but Lq and Ld do. First tried with a lower matched values of Lq and Ld:
3.png
Bit worse but not too bad. Then with non matching Lq and Ld:
4.png
Not good! Finally went back to larger Lq and Ld but still non-matching:
5.png
Also not good.

Looks like your idea of different kp and ki for Iq and Id may be on the money :D

Edit - Not so sure now, it seems that it's the fwmargin value, increasing this to 8000 (I think that's the value you use) fixed it:
6.png
Note : the loss of control at the right hand side (where it runs out of FW) is at around 1000rpm.

It could be that individual kp and ki could allow a reduced fwmargin though?

Rather than having separately tuned values could try scaling them according to the Lq/Ld ratio?

Edit - Just tried scaling ki and kp for d to be 2/3 the value for q - made no significant difference to the plot.
User avatar
Bigpie
Posts: 1771
Joined: Wed Apr 10, 2019 8:11 pm
Location: South Yorkshire, UK
Has thanked: 82 times
Been thanked: 422 times

Re: IPM Motor Simulation and FOC Software

Post by Bigpie »

Did some more testing, low rpm feels to have more torque, higher rpm seems better too, it used to sound a bit like an engine hitting the rev limiter as the rpm increased but now the rpm is more stable. Inverter trips out more easily though.
Didn't graph as was taking the kids to school.
BMW E91 2006
ZombieVerter
GS450h
Outlander Charger DC/DC
Renault Kangoo 36kWh battery
FOCCCI CCS
User avatar
Ev8
Posts: 818
Joined: Sat Jan 30, 2021 11:05 am
Has thanked: 43 times
Been thanked: 163 times

Re: IPM Motor Simulation and FOC Software

Post by Ev8 »

Just uploaded bigpies build, set ki=100 kp=200 fwki=1 fw kp=20

Felt very smooth but experienced uncontrollable full throttle at revs over 3000, had to key off to stop, wasn’t great fun, went back a build to drive to work any ideas?
User avatar
johu
Site Admin
Posts: 6713
Joined: Thu Nov 08, 2018 10:52 pm
Location: Kassel/Germany
Has thanked: 368 times
Been thanked: 1542 times
Contact:

Re: IPM Motor Simulation and FOC Software

Post by johu »

curki=100 and curkp=200 is totally out of scope. Seems to work in simulation but not in the real world. Should be more like curki=10000 and curkp=50. Also don't forget to set syncadv=0.5
Support R/D and forum on Patreon: https://patreon.com/openinverter - Subscribe on odysee: https://odysee.com/@openinverter:9
User avatar
Bigpie
Posts: 1771
Joined: Wed Apr 10, 2019 8:11 pm
Location: South Yorkshire, UK
Has thanked: 82 times
Been thanked: 422 times

Re: IPM Motor Simulation and FOC Software

Post by Bigpie »

Incorrect syncoffset? I've got syncadv at 0 too.

I have curkp: 32, curki: 1000. Apparently my motor has poor current control (viewtopic.php?f=19&t=902&p=21451#p21451)
BMW E91 2006
ZombieVerter
GS450h
Outlander Charger DC/DC
Renault Kangoo 36kWh battery
FOCCCI CCS
Pete9008
Posts: 1801
Joined: Sun Apr 03, 2022 1:57 pm
Has thanked: 102 times
Been thanked: 350 times

Re: IPM Motor Simulation and FOC Software

Post by Pete9008 »

Bigpie wrote: Fri Sep 09, 2022 8:11 am Did some more testing, low rpm feels to have more torque, higher rpm seems better too, it used to sound a bit like an engine hitting the rev limiter as the rpm increased but now the rpm is more stable. Inverter trips out more easily though.
Didn't graph as was taking the kids to school.
Probably a noob question but what does it trip out on?
Ev8 wrote: Fri Sep 09, 2022 8:23 am Just uploaded bigpies build, set ki=100 kp=200 fwki=1 fw kp=20

Felt very smooth but experienced uncontrollable full throttle at revs over 3000, had to key off to stop, wasn’t great fun, went back a build to drive to work any ideas?
Those values do sound a bit off, could you check? Not seen any problems like that until well into the field weakening region!
johu wrote: Fri Sep 09, 2022 8:27 am curki=100 and curkp=200 is totally out of scope. Seems to work in simulation but not in the real world. Should be more like curki=10000 and curkp=50. Also don't forget to set syncadv=0.5
If the consensus is that the simulator is not representing reality then I need to figure out why and fix it. If everyone could post their settings I'll give them a try.

I'm wondering if my approach to tuning the constants is leading to a different solution. I always tend to tune for kp first (with ki set to 0) and then once that is ok start working on ki. That tends to lead to higher kp and lower ki. I do it that way because I've found less stability problems that way round but maybe its the wrong approach for motors?

Edit - is there a tuning guide anywhere for setting these?
User avatar
Bigpie
Posts: 1771
Joined: Wed Apr 10, 2019 8:11 pm
Location: South Yorkshire, UK
Has thanked: 82 times
Been thanked: 422 times

Re: IPM Motor Simulation and FOC Software

Post by Bigpie »

Pete9008 wrote: Fri Sep 09, 2022 8:42 am Probably a noob question but what does it trip out on?
Not 100% sure, either over temperature or overcurrent, my inverter is a half Yaris/half Prius mashup, basically lower half is Yaris with the capacitors from a Prius, it has built in self protection.
BMW E91 2006
ZombieVerter
GS450h
Outlander Charger DC/DC
Renault Kangoo 36kWh battery
FOCCCI CCS
User avatar
Ev8
Posts: 818
Joined: Sat Jan 30, 2021 11:05 am
Has thanked: 43 times
Been thanked: 163 times

Re: IPM Motor Simulation and FOC Software

Post by Ev8 »

Sorry got mixed up and a typo kp=100 ki=2000
User avatar
Ev8
Posts: 818
Joined: Sat Jan 30, 2021 11:05 am
Has thanked: 43 times
Been thanked: 163 times

Re: IPM Motor Simulation and FOC Software

Post by Ev8 »

I’m thinking is down to also lowering my ki, turns out I had been previously running around with ki=30000 (one more zero than I thought) so lowering it to 2000 was a huge change,
Pete9008
Posts: 1801
Joined: Sun Apr 03, 2022 1:57 pm
Has thanked: 102 times
Been thanked: 350 times

Re: IPM Motor Simulation and FOC Software

Post by Pete9008 »

There is definitely something different here, this is what I get with my settings in the simulator:
p.png
Ignore the bit on the right, I've been playing with the FW and it's broken something!

This is using Bigpie's settings:
Bigpie.png
Ev8's settings:
Ev8.png
Definitely something not right, not sure what though. The only way I can get Ev8's settings to behave is by reducing the latency in the simulator from 180us to 70us at which point it becomes:
Ev8_reduced latency.png
Much better!

The reduced latency on my kp and ki settings doesn't make any significant difference.

This suggests that the latency on real inverters isn't as bad as measurements suggest, or my simulations isn't modelling it right. Will have a look at it this today.
User avatar
Bigpie
Posts: 1771
Joined: Wed Apr 10, 2019 8:11 pm
Location: South Yorkshire, UK
Has thanked: 82 times
Been thanked: 422 times

Re: IPM Motor Simulation and FOC Software

Post by Bigpie »

My full params.json attached.
Think my throtramp might be a bit high.
Attachments
params (1).json
(1.46 KiB) Downloaded 249 times
BMW E91 2006
ZombieVerter
GS450h
Outlander Charger DC/DC
Renault Kangoo 36kWh battery
FOCCCI CCS
Pete9008
Posts: 1801
Joined: Sun Apr 03, 2022 1:57 pm
Has thanked: 102 times
Been thanked: 350 times

Re: IPM Motor Simulation and FOC Software

Post by Pete9008 »

Clutching at straws here but can't see anything wrong so worth asking just in case.

What is the real world syncofs tuning procedure? Is it as per the process in Johu and Damien's video about finding the syncofs that gives no rotation in manual mode with just Id, and then never changing it, or is it that process and then tweaking it to see if it makes the motor run better?
User avatar
Bigpie
Posts: 1771
Joined: Wed Apr 10, 2019 8:11 pm
Location: South Yorkshire, UK
Has thanked: 82 times
Been thanked: 422 times

Re: IPM Motor Simulation and FOC Software

Post by Bigpie »

Ive pretty much followed the video and haven't touched the syncofs since.
BMW E91 2006
ZombieVerter
GS450h
Outlander Charger DC/DC
Renault Kangoo 36kWh battery
FOCCCI CCS
User avatar
johu
Site Admin
Posts: 6713
Joined: Thu Nov 08, 2018 10:52 pm
Location: Kassel/Germany
Has thanked: 368 times
Been thanked: 1542 times
Contact:

Re: IPM Motor Simulation and FOC Software

Post by johu »

Pete9008 wrote: Fri Sep 09, 2022 8:42 am I'm wondering if my approach to tuning the constants is leading to a different solution. I always tend to tune for kp first (with ki set to 0) and then once that is ok start working on ki. That tends to lead to higher kp and lower ki. I do it that way because I've found less stability problems that way round but maybe its the wrong approach for motors?

Edit - is there a tuning guide anywhere for setting these?
Yes that's how I was taught also but it didn't work at all for this control problem. That's because a pure p-controller can never keep up with the increased voltage demand as the motor speeds up. I couldn't get the car to perform at all with low KI gains. No guide, as I arrived at them rather randomly by reading many papers...

Now, more input from my side. I put the FW controller back into the PWM loop with kp and ki settable. Removed the filter.
First run with fwmargin=4000, fwkp=1, fwki=10. Seems FW controller actually works very well but comes in too late and its command isn't implemented by the d-controller
run10.png
fwmargin=6000, fwkp=1, fwki=40. Super high FW command, still seems too late, still isn't really implemented as FW is entered, but later on it is followed
run11.png
And finally fwmargin=5000, fwkp=1, fwki=20. Still saturated outputs, still oscillation.
run13.png
So next up reduce throttle input with another controller I guess...
EDIT: that said, I almost came completely off the throttle but that doesn't stop the oscillation. So not too optimistic on that.
EDIT2: I also thought about that anti-windup code in case of the q-controller. The limits are dynamic here. The integrator is frozen at a potentially high value when the output saturates. That could lead to instability as well. Now I think about calculating an integrator limit whenever updating ki or the saturation points and straight limiting the integrator to those limits.
EDIT3: looking at the second plot there is a point when the FW command is being followed nicely. So it's only the instance it comes in that is too late. Maybe some more kp and ki and less fwmargin?
Support R/D and forum on Patreon: https://patreon.com/openinverter - Subscribe on odysee: https://odysee.com/@openinverter:9
Pete9008
Posts: 1801
Joined: Sun Apr 03, 2022 1:57 pm
Has thanked: 102 times
Been thanked: 350 times

Re: IPM Motor Simulation and FOC Software

Post by Pete9008 »

Found the problem with simulation. When I restart a simulation I clear out everything in the model but can't zero everything in stm32-sine so it sees a sudden jump in values when it's rerun. I must have been tuning my values to cope with this as they always seem OK. Bigpie's and Ev8's ki and kp values don't like this and the loops start oscillating and never settle back down. If I run the model with zero throttle for a few seconds between each real run I get this with Ev8's settings:
Ev8_2.png
Much closer to results I get with my settings so that's good.

What's not so good is that the Uq and Ud between the model and the logs are very different. I'd believed that the back emf showed as a voltage in phase with and opposing Uq (so reducing the ability to generate Iq and so torque). This doesn't appear to be what's happening in the logs, in those Ud seems to be proportional to speed suggesting the back emf is in phase with Ud. Need to do a review of the model and the equations to see what's going on here.

EDIT - I must be missing something obvious here, I can't understand how the back emf can be in phase with Ud. Every way I think about it says it should be in phase with Uq. Can anyone explain?
User avatar
Ev8
Posts: 818
Joined: Sat Jan 30, 2021 11:05 am
Has thanked: 43 times
Been thanked: 163 times

Re: IPM Motor Simulation and FOC Software

Post by Ev8 »

Ok changed back to known working controller gains, no more unwanted throttle, no lift of regen, but pedal regen has a strong oscillation it applied above 3000 or so rpm, can I ask what d margin is? And what to expect from changing it?
Pete9008
Posts: 1801
Joined: Sun Apr 03, 2022 1:57 pm
Has thanked: 102 times
Been thanked: 350 times

Re: IPM Motor Simulation and FOC Software

Post by Pete9008 »

Ev8 wrote: Fri Sep 09, 2022 12:29 pm Ok changed back to known working controller gains, no more unwanted throttle, no lift of regen, but pedal regen has a strong oscillation it applied above 3000 or so rpm, can I ask what d margin is? And what to expect from changing it?
It sets the threshold where field weakening starts to come in (when the total voltage requested gets to within this value of the maximum negative Id starts getting added). The bigger it is the sooner field weakening current starts getting added. Try values between 2000 and 8000 but wouldn't go outside that range for now.

Still stuck on Uq and Ud and the back emf phase. Think it's going to have to goon the back burner until I can explain the discrepancy between the logged and simulated Uq and Ud and fix it :(
Pete9008
Posts: 1801
Joined: Sun Apr 03, 2022 1:57 pm
Has thanked: 102 times
Been thanked: 350 times

Re: IPM Motor Simulation and FOC Software

Post by Pete9008 »

This is the equation from the mathworks page for Uq and Ud:
UqUd.png
UqUd.png (2.92 KiB) Viewed 4203 times
My understanding is that wLqIq and wLdId terms are the inductor voltages due to rate of change of current and the wλ term is the back emf. This would mean that it should appear as part of Vq (Uq) - doesn't it??

Logic would say it must oppose Uq to reduce torque so again in phase with Uq?

Logic would say that the back emf should peak at the maximum rate of change of flux which is the quadrature component so again Uq?

Yet in the logged plots there is a clear speed dependant component in Ud and not in Uq - or am I misreading the logged plots somehow?
Post Reply