# Closed-loop buck: design the compensator

The buck of the [first tutorial](https://voltecx-web.nguyenvanqui291.workers.dev/docs/tutorials/01-first-buck/) runs at a fixed duty, so its output follows the input. Here a feedback loop holds the output at 12 V. You let the Design panel size a PI controller, read why it fails its check on this circuit, and then take the Type III compensator it suggests. About 15 minutes.

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

## 1. Open the closed-loop example

Choose `File › Open Example › DC-DC › Buck converter, closed loop`. The power stage is the one you know; the control loop is drawn above it.

![The closed-loop buck with its loop parts, marked 1 to 5](https://voltecx-web.nguyenvanqui291.workers.dev/docs/tutorials/img/en/closed-loop-buck/01-loop.png)

1. `VM` measures the output voltage.
2. `Ref` is the set point, `Vref` = 12 V.
3. `Err` subtracts the measurement from the set point.
4. `PI` turns the error into a duty.
5. `PWM` drives the switch with that duty.

## 2. Open the voltage-loop design

Choose `Design › Design Steps › Voltage Loop`.

![The Design menu with Voltage Loop, marked 1](https://voltecx-web.nguyenvanqui291.workers.dev/docs/tutorials/img/en/closed-loop-buck/02-design-menu.png)

1. `Voltage Loop`.

## 3. Look at the Design panel

The Design page opens in the editor area, as a tab beside the circuit's. The fields start filled from the circuit, and a first design is shown at once.

![The Design panel on the Voltage Loop task, the Controller field, marked 1 to 2](https://voltecx-web.nguyenvanqui291.workers.dev/docs/tutorials/img/en/closed-loop-buck/03-panel.png)

1. `Voltage Loop` is the task.
2. `Controller` is `Auto`, and the note under it says `Auto picked Type III.`: the panel picked a Type III network, because a PI cannot reach the default crossover (a tenth of the 100 kHz switching frequency) on this circuit.

## 4. Ask for a PI

In the `Controller` row click `PI`.

![The Controller row with PI, marked 1](https://voltecx-web.nguyenvanqui291.workers.dev/docs/tutorials/img/en/closed-loop-buck/04-pick-pi.png)

1. `PI`.

## 5. Design the PI

Click `[Run]` (it reads `[Design again]` until you change a field), then collapse the bottom panel (`↓` at its top right) to see more of the result.

![The PI design: Kp, Ki and the Bode plot of the loop, marked 1 to 2](https://voltecx-web.nguyenvanqui291.workers.dev/docs/tutorials/img/en/closed-loop-buck/05-pi-result.png)

1. `[Design again]`: the design is computed; changing a field turns the button back into `[Run]`.
2. `[Apply]` writes the design into the circuit.

**Result:** `Proportional gain` 0.003017 and `Integral gain` 73.69 1/s. The crossover field shows 685.4 Hz: with 60° of phase margin, this is as high as a PI can cross, well below the LC resonance at 1.59 kHz.

## 6. Apply it and read the check

Click `[Apply]`. The panel writes `kp` and `ki` into the `PI` block as one undo step, then checks the loop on a copy of the circuit. Fold `Corner check` and `Changes made` and scroll down the `Verification` list.

![Check failed: the recovery overshoot after a load step is too large, marked 1](https://voltecx-web.nguyenvanqui291.workers.dev/docs/tutorials/img/en/closed-loop-buck/06-pi-check.png)

1. `Recovery overshoot` fails: 73 % against a 40 % limit. The output rings after a 50 % load step, because a loop that crosses below the resonance cannot damp it.

**Result:** the PI regulates (the output error after the step is near zero), but the response is poorly damped. The note under the plot says what to do: a Type III network reaches 5.78 kHz here.

## 7. Take the Type III compensator

Click `Type III` in the `Controller` row, click `[Run]`, then `[Apply]`.

![The Type III design applied and verified, marked 1 to 2](https://voltecx-web.nguyenvanqui291.workers.dev/docs/tutorials/img/en/closed-loop-buck/07-type-iii.png)

1. `Controller` is `Type III`. The crossover is 5.78 kHz with 60° of margin.
2. `✔ Verified`: the loop passes its checks. On the schematic, the `PI` block is now a transfer-function block `B(s)/A(s)` with the same name and wires. `⌘Z` brings the PI back.

## 8. Simulate the closed loop

Close the Design page with the `×` on its tab and open the bottom panel again. Click `[▶ Run]` in the toolbar or press `F5`, then `Maximize` at the panel's top right for a larger plot.

![The scope in the panel: v(out) settles on vref, marked 1 to 2](https://voltecx-web.nguyenvanqui291.workers.dev/docs/tutorials/img/en/closed-loop-buck/08-run.png)

1. `v(out)`: the output sits on the set point. The plot shows the last cycles; `Whole run` above it shows the output rising from the start.
2. `vref`: the 12 V set point.

**Result:** from 8 ms on, the output averages 12.00 V.

## The same steps from a script

```sh
powercraft-cli run \
    --cmd file.openExample    --params '{"name":"buckClosedLoop"}' \
    --cmd design.voltageLoop  --params '{"controller":"pi"}' \
    --cmd design.voltageLoop  --params '{"controller":"typeIII","apply":true}' \
    --cmd simulation.run      --params '{}' \
    --cmd scope.measure       --params '{"signals":["v(out)"],"t0":"8m"}'
```

Next: [design the transformer of a flyback](https://voltecx-web.nguyenvanqui291.workers.dev/docs/tutorials/03-flyback-magnetics/).
