# 離散 PID 控制器 (`dpid`)

> 每個取樣時間計算一次的 PID 控制器。

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

[元件參考](https://voltecx-web.nguyenvanqui291.workers.dev/zh/docs/reference/) · [English](https://voltecx-web.nguyenvanqui291.workers.dev/docs/reference/control/dpid/)

**分類:** 控制 · **種類:** `{"block":"dpid","kind":"control"}`

## 何時使用

數位迴路需要微分項時使用，例如韌體中的速度迴路。不需要微分時改用離散 PI 控制器。

## 接腳

| 接腳 | 訊號域 | 意義 |
|---|---|---|
| `in` | 訊號 | 誤差：參考值減回授值 |
| `clk` | 訊號 | 選用的時脈；接上時控制器在其每個上升緣取樣，`ts` 則是離散增益所用的標稱週期。 |
| `out` | 訊號 | 控制器輸出，限制在 lo 與 hi 之間 |

## 常用參數

- `ts`: Time between samples; with the clk input wired, its rising edges, and ts is the period the gains use (預設值: 必填)
- `kp`: Strength of the reaction to error (預設值: 1)
- `ki`: Strength of the reaction to past error (預設值: 0)
- `kd`: Strength of the reaction to change (預設值: 0)
- `n`: How fast the derivative filter reacts (預設值: 100)

## 參數

| 鍵 | 單位 | 預設值 | 典型範圍 | 說明 |
|---|---|---|---|---|
| `ts` | s | 必填 |  | 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 | 選填（預設關閉） |  | Strength of the anti-windup correction |

_參數說明在兩種語言中都是英文。_

## 模型

out = kp·e + I + kd·N·(e - xf),xf 是輸入經過極點為 `n`(rad/s)的一階低通後的值,所以微分作用在誤差上。積分 I 與濾波狀態 xf 都以 `method`(`forward`、`backward` 或 `tustin`)推進。輸出被限制在 `lo` 到 `hi`,並透過 `kb` 做反算抗飽和。kp 或 kd 不為 0,或方法不是 `forward` 時有直通項。

## 常見錯誤

- 用 forward 時，n 乘以 ts 必須小於 2，否則方塊會被拒絕。用 backward 且 n 乘以 ts 遠大於 1 時，濾波器失去作用，微分會放大雜訊。
- kd 的單位是秒，ki 的單位是每秒，lo 與 hi 預設為正負 1e30，請自行設定。只由控制方塊組成的迴路需要加單位延遲，除非 kp 與 kd 為 0 且 method 是 forward。

Problems 規則: `compile.error`

## 規格書與來源

ts 是 MCU 的控制週期 1/fs。kp、ki、kd 來自該取樣率下的迴路設計。n 是想要的微分頻寬，取交越頻率的數倍，且低於約 1/ts；lo、hi 來自致動器範圍。

## 範例

以指令 `file.openExample` 開啟範例，名稱為 `demo:dpid`.

---
由 `cargo xtask reference` 依使用說明文字與參數規格產生，請勿手動編輯。
