# Discrete PID Controller (`dpid`)

> A PID controller that runs once every sample time.

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

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

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

## When to use it

Use it when a digital loop needs a derivative term, such as a speed loop in firmware. Use Discrete PI Controller when it does not.

## 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)
- `kd`: Strength of the reaction to change (Default: 0)
- `n`: How fast the derivative filter reacts (Default: 100)

## 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 |
| `kd` | s | 0 |  | Strength of the reaction to change |
| `n` | rad/s | 100 |  | How fast the derivative filter reacts |
| `method` | — | backward |  | Calculation method for the integral and filter: `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 + kd·N·(e - xf), where xf is the input after a first-order lag with pole `n` (rad/s), so the derivative acts on the error. The integral I and the filter state xf are both advanced with `method` (`forward`, `backward` or `tustin`). The output is clamped to `lo` ... `hi` with back-calculation anti-windup through `kb`. There is direct feedthrough when kp or kd is not 0 or the method is not `forward`.

## Common mistakes

- With forward, n times ts must stay below 2, or the block is refused. With backward and n times ts well above 1, the filter stops filtering and the derivative amplifies noise.
- kd is in seconds, ki is per second, and lo and hi start at plus and minus 1e30, so set them. A loop made only of control blocks needs a Unit Delay unless kp and kd are 0 and method is forward.

Problems rules: `compile.error`

## Datasheet and sources

ts is the MCU control period, 1/fs. kp, ki and kd come from your loop design at that sample rate. n is the derivative bandwidth you want, a few times the crossover and below about 1/ts, and lo and hi come from the actuator range.

## Example

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

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