# Three-phase motor drive: field-oriented control of a PM motor

A permanent-magnet synchronous motor (PMSM) on a three-phase inverter, under field-oriented control (FOC): a speed loop sets the torque current, and two current loops in the rotor's d-q frame set the voltages. You read the drive, simulate a speed step, and let the Design panel size the current and speed controllers. About 20 minutes.

Pictures: a numbered red box is what you click or read, `[Run]` is a button, `Design › Design Steps › Current Loop` a menu path, `F5` a key.

## 1. Open the FOC example

Choose `File › Open Example › Motor drives › PM synchronous motor, field-oriented control`.

![The File menu open at Open Example, Motor drives, PM synchronous motor, field-oriented control, marked 1 to 4](https://voltecx-web.nguyenvanqui291.workers.dev/docs/tutorials/img/en/motor-drive/01-open-example.png)

1. `File`.
2. `Open Example`.
3. `Motor drives`.
4. `PM synchronous motor, field-oriented control`.

## 2. Read the power stage

For these two pictures the side panels are hidden (`View` menu) so the schematic has the whole window.

![The power stage: DC link, three-phase bridge, current sensors, motor and load, marked 1 to 6](https://voltecx-web.nguyenvanqui291.workers.dev/docs/tutorials/img/en/motor-drive/02-power.png)

1. `Vdc`: the 300 V DC link.
2. `Sah`: the high side of phase a; the bridge has three legs, `Sa`, `Sb` and `Sc`.
3. `Ama`: measures the phase-a current (`Amb` and `Amc` the others) for the current loops.
4. `M1`: the PMSM, 8 poles, 0.5 Ω, 4 mH, 0.12 Wb of magnet flux.
5. `Fan`: a fan load, whose torque rises with the square of the speed.
6. `PS`: the position sensor, which gives the rotor angle to the Park transforms.

## 3. Read the control

![The control: speed loop, Park transform, d and q current loops, inverse Park, SVM and PWM, marked 1 to 9](https://voltecx-web.nguyenvanqui291.workers.dev/docs/tutorials/img/en/motor-drive/03-control.png)

1. `SpeedRef`: steps the speed command from 0 to 200 rad/s at 20 ms.
2. `SpdPI`: the speed controller. Its output is the q-axis (torque) current command, limited to ±8 A.
3. `Park`: turns the three phase currents into `id` and `iq`, in the frame that turns with the rotor.
4. `PIq`: the q-axis current controller.
5. `PId`: the d-axis current controller. Its command is 0 (`IdRef`), so all the current makes torque.
6. `InvPark`: turns the d-q voltages back into three phase voltages.
7. `SVM`: space-vector modulation turns them into three duties.
8. `PWM`: one 10 kHz carrier for the three legs.
9. `DTh`: the 1 µs dead time of the three high sides, on the PWM's gate bus; `N` and `DTl` give the three low sides the complement, and two bus demuxes hand the six gate signals (`gah` to `gcl`) to the bridge by label.

## 4. Run the speed step

Click `[▶ Run]` or press `F5`. The run is 350 ms and takes about a second. In the scope click `Maximize` at the panel's top right, then `Fit` in the scope's toolbar: the plot first shows only the last few switching cycles.

![The scope: wm(M1) ramps to 200 rad/s and stops there without overshoot, marked 1 to 2](https://voltecx-web.nguyenvanqui291.workers.dev/docs/tutorials/img/en/motor-drive/04-run.png)

1. `wm(M1)`: the rotor speed.
2. `wref`: the command.

**Result:** the speed rises as a straight ramp and stops at 200 rad/s without overshoot. During the ramp `SpdPI` sits at its 8 A limit, but the current loop trails that command by about 0.35 A while the back-EMF climbs with the speed: `iqm` is about 7.65 A, so the motor accelerates at `Kt·iqm / J` = 0.72 × 7.65 / 0.004 ≈ 1380 rad/s² (the full 8 A would give 1440 rad/s²), slowed near the top by the fan. The Problems tab shows no warnings: each gate goes through its dead-time block, which costs each leg about `Vdc·td·fsw` = 300 × 1 µs × 10 kHz = 3 V of the voltage the current loops ask for ([tutorial 5](https://voltecx-web.nguyenvanqui291.workers.dev/docs/tutorials/05-full-bridge-inverter/) shows the effect of dead time on a full bridge).

## 5. Design the current loop

Click `Restore` at the panel's top right, choose `Design › Design Steps › Current Loop`, then click `[Design again]`.

![The Current Loop task: Kp and Ki of the d axis, and the Bode plot, marked 1 to 3](https://voltecx-web.nguyenvanqui291.workers.dev/docs/tutorials/img/en/motor-drive/05-current-loop.png)

1. `Current Loop` is the task. It reads R and L from the motor and finds the two PIs from the wiring.
2. `Proportional gain (d axis)` = 25.13 and `Integral gain (d axis)` = 3.142k 1/s: `Kp = L·ωc` and `Ki = R·ωc` cancel the motor's R-L pole. The crossover is 1 kHz (a tenth of the switching frequency) with 72° of margin: 90° less 18° for the half-sample PWM delay.
3. `[Apply]` writes `Kpi` and `Kii`, the circuit variables both current PIs use.

## 6. Design the speed loop

Click `[Apply]`, then the `Speed Loop` task, then `[Design again]`.

![The Speed Loop task: Kp and Ki by the symmetrical optimum, marked 1 to 3](https://voltecx-web.nguyenvanqui291.workers.dev/docs/tutorials/img/en/motor-drive/06-speed-loop.png)

1. `Speed Loop` is the task. It models the current loop you just applied as a first-order lag of 1 kHz.
2. `Proportional gain` = 3.386 and `Integral gain` = 531.9 1/s, by the symmetrical optimum with a symmetry factor of 4. The loop crosses at 99.5 Hz (the speed bandwidth, a tenth of the current loop's) with 70° of margin.
3. `[Apply]` writes `Kpw` and `Kiw`.

## 7. Run again

Click `[Apply]`, close the Design page with the `×` on its tab and press `F5`. When the run ends, click `Maximize` and `Fit` as in step 4.

![The same ramp; Run History lists the four gains that changed, marked 1 to 2](https://voltecx-web.nguyenvanqui291.workers.dev/docs/tutorials/img/en/motor-drive/07-rerun.png)

1. `wm(M1)`: the same ramp to 200 rad/s. A large step drives the speed PI into its current limit, so the ramp depends on the 8 A limit and the inertia, not on the gains. The new gains act on small steps and load changes, where the loop stays linear.
2. The `Runs` tab: the second run and the four variables `Apply` changed.

## The same steps from a script

```sh
powercraft-cli run \
    --cmd file.openExample    --params '{"name":"pmsmFoc"}' \
    --cmd simulation.run      --params '{}' \
    --cmd design.currentLoop  --params '{"apply":true}' \
    --cmd design.speedLoop    --params '{"apply":true}' \
    --cmd simulation.run      --params '{}' \
    --cmd scope.measure       --params '{"signals":["wm(M1)"],"t0":"0.3"}'
```

Next: [digital control and code generation](https://voltecx-web.nguyenvanqui291.workers.dev/docs/tutorials/07-digital-control/).
