# 離散 PI 控制器 (`dpi`)

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

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

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

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

## 何時使用

用於在 MCU 中斷內運行的電流或電壓迴路。連續設計請改用 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)
- `lo`: Lowest value the output can reach (預設值: -1e30)
- `hi`: Highest value the output can reach (預設值: 1e30)

## 參數

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

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

## 模型

out = kp·e + I,積分 I 依 `method` 計算:forward 為 I[k] = I[k-1] + ki·ts·e[k-1],backward 為 I[k] = I[k-1] + ki·ts·e[k],tustin 用 e[k] 與 e[k-1] 的平均。輸出被限制在 `lo` 到 `hi`,積分器收到 kb·(限幅後 - 限幅前) 的回授;`kb` 未設時取 |ki/kp|。`kd` 與 `n` 不會被使用。kp 不為 0 或方法不是 `forward` 時有直通項。

## 常見錯誤

- ki 的單位是每秒，不是每個取樣。請把 ts 設成與 MCU 輸出級的 1/fs 相同，offset 也要一致，否則控制器的執行時刻會和 PWM 階段預期的不同。
- 只由控制方塊組成、又經過這個方塊的迴路會被拒絕，除非 kp 為 0 且 method 是 forward，所以請加一個單位延遲。lo 與 hi 預設為正負 1e30，請設成調變器的範圍。

Problems 規則: `compile.error`

## 規格書與來源

ts 是 MCU 的控制週期 1/fs。kp 與 ki 來自迴路設計：電流迴路在交越頻率 fc 下，kp 取 2·pi·fc 乘以 L，ki 取 2·pi·fc 乘以 R。lo、hi 來自調變器能輸出的電壓或責任比範圍。

## 範例

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

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