# Discrete PI Controller (`dpi`)

> A PI controller that runs once every sample time.

![dpi](https://voltecx-web.nguyenvanqui291.workers.dev/docs/reference/img/dpi.svg)

[Component reference](https://voltecx-web.nguyenvanqui291.workers.dev/docs/reference/) · [繁體中文](https://voltecx-web.nguyenvanqui291.workers.dev/zh/docs/reference/control/dpi/)

**Group:** Control · **Kind:** `{"block":"dpi","kind":"control"}`

## When to use it

Use it for a current or voltage loop that runs in an MCU interrupt. Use PI Controller for a continuous design.

## Pins

| Pin | Domain | Meaning |
|---|---|---|
| `in` | signal | Error: reference minus feedback |
| `clk` | signal | Optional clock; while wired the controller samples at each of its rising edges, and `ts` is the nominal period its discrete gains use. |
| `out` | signal | Controller output, limited to lo and hi |

## Common parameters

- `ts`: Time between samples; with the clk input wired, its rising edges, and ts is the period the gains use (Default: required)
- `kp`: Strength of the reaction to error (Default: 1)
- `ki`: Strength of the reaction to past error (Default: 0)
- `lo`: Lowest value the output can reach (Default: -1e30)
- `hi`: Highest value the output can reach (Default: 1e30)

## Parameters

| Key | Unit | Default | Typical range | Description |
|---|---|---|---|---|
| `ts` | s | required |  | Time between samples; with the clk input wired, its rising edges, and ts is the period the gains use |
| `offset` | s | 0 |  | Time of the first sample |
| `kp` | — | 1 |  | Strength of the reaction to error |
| `ki` | 1/s | 0 |  | Strength of the reaction to past error |
| `method` | — | backward |  | Calculation method for the integral: `forward`, `backward` or `tustin` |
| `lo` | — | -1e30 |  | Lowest value the output can reach |
| `hi` | — | 1e30 |  | Highest value the output can reach |
| `kb` | 1/s | optional (off) |  | Strength of the anti-windup correction |

_Parameter descriptions are in English in both languages._

## Model

out = kp·e + I, where the integral I follows the rule in `method`: forward I[k] = I[k-1] + ki·ts·e[k-1], backward I[k] = I[k-1] + ki·ts·e[k], tustin uses the average of e[k] and e[k-1]. The output is clamped to `lo` ... `hi` and the integrator receives kb·(clamped - unclamped) back; an unset `kb` takes |ki/kp|. `kd` and `n` are ignored. There is direct feedthrough when kp is not 0 or the method is not `forward`.

## Common mistakes

- ki is per second, not per sample. Set ts to 1/fs of the MCU Output Stage and use the same offset, or the controller runs at a different moment than the PWM stage expects.
- A loop made only of control blocks that goes through this block is refused, unless kp is 0 and method is forward, so add a Unit Delay. lo and hi start at plus and minus 1e30, so set them to the modulator range.

Problems rules: `compile.error`

## Datasheet and sources

ts is the MCU control period, 1/fs. kp and ki come from your loop design: for a current loop with crossover frequency fc, kp is 2·pi·fc times L and ki is 2·pi·fc times R. lo and hi come from the voltage or duty range the modulator can produce.

## Example

Open the example with the command `file.openExample` and the name `demo:dpi`.

---
Generated by `cargo xtask reference` from the usage texts and parameter specs. Do not edit by hand.
