# PID Controller (`pid`)

> Adds up proportional, integral and derivative action on an error signal.

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

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

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

## When to use it

Use it for an analog loop design. Use PI Controller when there is no derivative, and Discrete PID Controller for firmware.

## Pins

| Pin | Domain | Meaning |
|---|---|---|
| `in` | signal | Error: reference minus feedback |
| `out` | signal | Controller output, limited to lo and hi |

## Common parameters

- `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)
- `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 |
|---|---|---|---|---|
| `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 |
| `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._

**Use for:** Analog loop design · Position loops · Voltage loops

**Not for:** Firmware PID at a sample rate → [`dpid`](https://voltecx-web.nguyenvanqui291.workers.dev/docs/reference/control/dpid/) · Type II or III error amplifiers → [`compensator`](https://voltecx-web.nguyenvanqui291.workers.dev/docs/reference/control/compensator/)

## Formulas

| Formula | Equation | Source |
|---|---|---|
| PID with derivative filter | C(s) = kp + ki/s + kd·n·s/(s + n) | Åström & Hägglund, Advanced PID Control (2006), ch. 3 |
| High-frequency derivative gain | Kd∞ = kd·n | Åström & Hägglund, Advanced PID Control (2006), ch. 3 |
| Derivative filter corner | fn = n/(2π) | Åström & Hägglund, Advanced PID Control (2006), ch. 3 |
| Default tracking gain | kb = abs(ki/kp) | Åström & Hägglund, Advanced PID Control (2006), ch. 3 (back-calculation anti-windup) |

## Quick check

Inputs:

- `kp`: Proportional gain = 1 —, `kp`
- `ki`: Integral gain = 200 1/s, `ki`
- `kd`: Derivative gain = 1m s, `kd`
- `n`: Derivative filter pole = 10k rad/s, `n`

Outputs:

- `kdhf`: High-frequency derivative gain = `kd*n` —
- `fn`: Filter corner = `n/(2*pi)` Hz
- `kb`: Default anti-windup gain = `if(kp != 0, abs(ki/kp), 1)` 1/s

## Model logic

- Once per accepted step the integral and the derivative filter dx/dt = n·(in - x) advance by the trapezoid; the derivative term kd·n·(in - x) acts on the error.
- The sum is clamped to lo and hi, and the integral receives kb·(clamped - unclamped output) of the previous step; unset kb takes abs(ki/kp), or 1 when kp is 0.
- The integral and the filter start at 0 and there is no y0, so an error at t = 0 gives a derivative kick kd·n·in that decays at the rate n.
- The block has direct feedthrough when kp or kd is not 0; kd not 0 with n at or below 0, a negative kb or lo above hi is refused.

## Model

out = kp·e + ki·integral(e) + kd·N·s/(s + N)·e with N = n, so the derivative passes through a first-order lag and acts on the error, not on the measurement. The output is clamped to lo ... hi. While it is clamped, the integrator receives kb·(clamped output − unclamped output). An unset kb takes |ki/kp|, or 1 when kp is 0, and kb = 0 turns anti-windup off. The block has direct feedthrough when kp or kd is not 0.

## Common mistakes

- ki is per second and kd is in seconds, so enter ki as kp divided by Ti and kd as kp times Td. When kd is not 0, n must be above zero and well above the loop crossover, but far below 1/maxStep.
- lo and hi start at plus and minus 1e30, so nothing is limited until you set them to the actuator range, such as 0 to 1 for a duty command. A lo above hi is refused.

Problems rules: `compile.error`

## Datasheet and sources

There is no datasheet. kp, ki and kd come from your loop design, lo and hi from the actuator range, and n from the derivative filter you want, usually a few times the crossover frequency in rad/s.

## Example

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

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