# Highly scientific FOC Simulation

**URL:** https://community.simplefoc.com/t/highly-scientific-foc-simulation/3901
**Category:** Uncategorized
**Created:** [September 19, 2023, 2:10pm UTC](https://community.simplefoc.com/t/highly-scientific-foc-simulation/3901 "2023-09-19T14:10:18Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![Candas1](https://community.simplefoc.com/user_avatar/community.simplefoc.com/candas1/32/5296_2.png) [@Candas1](https://community.simplefoc.com/u/Candas1)
#### Post date: [September 19, 2023, 2:10pm UTC](https://community.simplefoc.com/t/highly-scientific-foc-simulation/3901/1 "2023-09-19T14:10:18Z")

</div>

Here is the spammer again.  
Wanted to share something I used last few days for learning purpose.  
It’s clearly not a scientific tool but gives a good intuition about how FOC works.  
It’s a very simplified FOC simulation in a [gsheet](https://docs.google.com/spreadsheets/d/1yLXF6wKdk0xLEGpTAMmxBcFIUjrvI8vpThAcZM2Sszo/copy) 😂

In the **Phase current to DG** tab:

- If you enter a different gain for phase A (e.g. 1,1), this makes D/Q currents wobbly, the PIs will have to compensate this. With good hardware I guess it shouldn’t happen.  
 ![image](https://community.simplefoc.com/uploads/default/original/2X/4/42e792747f8caa81ce9791e61a8d736b48b774ac.png)
- If you enter a delay (e.g. 0,1), it introduces a lag between the rotor angle and the sensor angle, this shows not measuring the current and the position at the same time can introduce an error in D and Q (a little) current measurement, reducing the efficiency.  
 ![image](https://community.simplefoc.com/uploads/default/original/2X/f/fcc96401c63da03c8a89bf90f21bc72fdab5d3a0.png)

in the **DQ current to PWM** tab:

- increasing D current (e.g. 0,5) not only introduces a shift in phases, but also increases the amplitude  
 ![image](https://community.simplefoc.com/uploads/default/original/2X/8/8f0af3a260594efafcfae8467c0e10a3263396fb.png)

---

<div class="post-metadata">

### Author: ![Candas1](https://community.simplefoc.com/user_avatar/community.simplefoc.com/candas1/32/5296_2.png) [@Candas1](https://community.simplefoc.com/u/Candas1)
#### Post date: [September 19, 2023, 3:59pm UTC](https://community.simplefoc.com/t/highly-scientific-foc-simulation/3901/2 "2023-09-19T15:59:04Z")

</div>

And another more advanced [motor simulator](https://github.com/markisus/motor_sim#bldc-and-pmsm-simulator)

---

<div class="post-metadata">

### Author: ![Candas1](https://community.simplefoc.com/user_avatar/community.simplefoc.com/candas1/32/5296_2.png) [@Candas1](https://community.simplefoc.com/u/Candas1)
#### Post date: [September 19, 2023, 6:11pm UTC](https://community.simplefoc.com/t/highly-scientific-foc-simulation/3901/3 "2023-09-19T18:11:27Z")

</div>

You must be thinking, my phase currents don’t look like that. Fair point, let’s add some noise:

 ![image](https://community.simplefoc.com/uploads/default/original/2X/d/d325dfe6255bbf638de898a368401ba89acbdf77.png)

Someone proposed to filter the phase current, great idea !!! let’s do it:

 ![image](https://community.simplefoc.com/uploads/default/original/2X/6/619e4455915d47fa56f3e3bc30cdb44c7b888eea.png)  
It looks much better now. But why did the D current increase ? Oh no we introduced lag !!!

The same would happen if filtering Alpha and Beta… What about filtered the D and Q currrents ?

 ![image](https://community.simplefoc.com/uploads/default/original/2X/2/28cd00b37d9d2de2000a225346c7f85851bb4a97.png)  
Ah, much better !!!

---

<div class="post-metadata">

### Author: ![Candas1](https://community.simplefoc.com/user_avatar/community.simplefoc.com/candas1/32/5296_2.png) [@Candas1](https://community.simplefoc.com/u/Candas1)
#### Post date: [September 19, 2023, 7:05pm UTC](https://community.simplefoc.com/t/highly-scientific-foc-simulation/3901/4 "2023-09-19T19:05:32Z")

</div>

But my sensor is slow, it’s not updating often!!!

 ![image](https://community.simplefoc.com/uploads/default/original/2X/0/09beea6641d6c62206ddc01697ab936dbc6704e1.png)  
Oh no !!! D axis current is off and wobbly! More work for the PI controller…  
 ![image](https://community.simplefoc.com/uploads/default/original/2X/c/cff71c4b0772f1c5c0e567adf4679eaa7f497d36.png)

Lukily we can use sensor smoothing !!!

OK I stop bothering you 😂

---

<div class="post-metadata">

### Author: ![o\_lampe](https://community.simplefoc.com/letter_avatar_proxy/v4/letter/o/85e7bf/32.png) [@o\_lampe](https://community.simplefoc.com/u/o_lampe)
#### Post date: [September 19, 2023, 7:13pm UTC](https://community.simplefoc.com/t/highly-scientific-foc-simulation/3901/5 "2023-09-19T19:13:56Z")

</div>

Very informative and entertaining.  
But we need filtering,unless we have stable signals without glitches.  
IMHO these glitches are the bigger problem.

---

<div class="post-metadata">

### Author: ![Candas1](https://community.simplefoc.com/user_avatar/community.simplefoc.com/candas1/32/5296_2.png) [@Candas1](https://community.simplefoc.com/u/Candas1)
#### Post date: [September 19, 2023, 7:16pm UTC](https://community.simplefoc.com/t/highly-scientific-foc-simulation/3901/6 "2023-09-19T19:16:14Z")

</div>

Yes I am showing filtering D Q currents works better, that’s what SimpleFOC is doing. But it’s not perfect either…

---

<div class="post-metadata">

### Author: ![Candas1](https://community.simplefoc.com/user_avatar/community.simplefoc.com/candas1/32/5296_2.png) [@Candas1](https://community.simplefoc.com/u/Candas1)
#### Post date: [September 20, 2023, 5:50am UTC](https://community.simplefoc.com/t/highly-scientific-foc-simulation/3901/7 "2023-09-20T05:50:31Z")

</div>

Changing phase C current doesn’t impact Alpha/Beta and D/Q currents, how come ? 🤔

 ![image](https://community.simplefoc.com/uploads/default/original/2X/e/e5bb1b417fd5ea50bb2ad7e0e0f76db0f3004495.png)

Because Ia+Ib+Ic= 0 (Kirchhoff’s law), only 2 phase currents are enough for FOC.  
But why do we even measure 3 currents then ?

In reality, the total of phase currents don’t add up to 0 because of inaccuracies in hardware, and noise during sampling. One of the reasons why it’s useful to have 3 phase currents is that this can be used to slightly reduce the noise by sharing this error between the 3 phase currents:

 ![image](https://community.simplefoc.com/uploads/default/original/2X/6/607a2ee3fd6eb61123dd04a2235ff9d255afbab7.png)

---

<div class="post-metadata">

### Author: ![Candas1](https://community.simplefoc.com/user_avatar/community.simplefoc.com/candas1/32/5296_2.png) [@Candas1](https://community.simplefoc.com/u/Candas1)
#### Post date: [September 26, 2023, 8:55am UTC](https://community.simplefoc.com/t/highly-scientific-foc-simulation/3901/8 "2023-09-26T08:55:14Z")

</div>

Added different modulation types and the effect of deatime on line to line voltage in the DQ to PWM tab if you are interested:

 ![image](https://community.simplefoc.com/uploads/default/original/2X/b/bb41ee09da35327ea2b153d460de21375a9139dc.png)
