# C 方塊 (`cblock`)

> 把你撰寫的 C 程式碼（PSIM 的 C Block 或 PLECS 的 C-Script）當成控制方塊執行；程式碼在沙箱中執行，不會讓模擬當掉或卡住。

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

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

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

## 何時使用

用於寫成程式碼比畫成方塊圖更容易的控制邏輯：狀態機、保護時序、帶分支的查表、從韌體搬來的控制器。若內建方塊（`pi`、`dpi`、`ztf`、`svpwm`）就能做到，請用內建方塊：它們較快，程式碼產生也能把它們寫成定點 C。

## 接腳

| 接腳 | 訊號域 | 意義 |
|---|---|---|
| `in1` | 訊號 | 第一個輸入：x1 或 in[0]（PSIM），InputSignal(0, 0)（PLECS）。 |
| `in2` | 訊號 | 第二個輸入，inputs 為 2 以上才讀取。 |
| `in3` | 訊號 | 第三個輸入，inputs 為 3 以上才讀取。 |
| `in4` | 訊號 | 第四個輸入，inputs 為 4 以上才讀取。 |
| `in5` | 訊號 | 第五個輸入，inputs 為 5 以上才讀取。 |
| `in6` | 訊號 | 第六個輸入，inputs 為 6 以上才讀取。 |
| `in7` | 訊號 | 第七個輸入，inputs 為 7 以上才讀取。 |
| `in8` | 訊號 | 第八個輸入，inputs 為 8 才讀取。 |
| `out1` | 訊號 | 第一個輸出：y1 或 out[0]（PSIM），OutputSignal(0, 0)（PLECS）。 |
| `out2` | 訊號 | 第二個輸出，outputs 為 2 以上才寫入。 |
| `out3` | 訊號 | 第三個輸出，outputs 為 3 以上才寫入。 |
| `out4` | 訊號 | 第四個輸出，outputs 為 4 以上才寫入。 |
| `out5` | 訊號 | 第五個輸出，outputs 為 5 以上才寫入。 |
| `out6` | 訊號 | 第六個輸出，outputs 為 6 以上才寫入。 |
| `out7` | 訊號 | 第七個輸出，outputs 為 7 以上才寫入。 |
| `out8` | 訊號 | 第八個輸出，outputs 為 8 才寫入。 |

## 常用參數

- `dialect`: Whose C block the code is written for: `psim` (C Block: t, delt, x1…, y1…) or `plecs` (C-Script: sections and the PLECS macros) (預設值: psim)
- `inputs`: Inputs the code reads, 0 to 64 (in1 … in64) (預設值: 1)
- `outputs`: Outputs the code writes, 0 to 64 (out1 … out64) (預設值: 1)
- `ts`: Sample time: the code runs at 0, ts, 2·ts, … and the outputs hold between; 0 runs it at every solver step. Imported blocks may set `sampleTime` instead (預設值: 0)

## 參數

| 鍵 | 單位 | 預設值 | 典型範圍 | 說明 |
|---|---|---|---|---|
| `dialect` | — | psim |  | Whose C block the code is written for: `psim` (C Block: t, delt, x1…, y1…) or `plecs` (C-Script: sections and the PLECS macros) |
| `inputs` | — | 1 |  | Inputs the code reads, 0 to 64 (in1 … in64) |
| `outputs` | — | 1 |  | Outputs the code writes, 0 to 64 (out1 … out64) |
| `ts` | s | 0 |  | Sample time: the code runs at 0, ts, 2·ts, … and the outputs hold between; 0 runs it at every solver step. Imported blocks may set `sampleTime` instead |
| `feedthrough` | — | 1 |  | 1 when the outputs depend on the present inputs; 0 lets the block break an algebraic loop (it may read the previous step's inputs) |
| `params` | — | 選填（預設關閉） |  | PLECS parameters, read by ParamRealData(i, 0): a comma-separated list. Imported blocks may set `param1`, `param2`, … instead |
| `discStates` | — | 0 |  | PLECS discrete states, DiscState(i) |
| `contStates` | — | 0 |  | PLECS continuous states, ContState(i), whose ContDeriv(i) the derivative section sets; integrated with Heun's method over each solver step |
| `inputWidths` | — | 選填（預設關閉） |  | PLECS input terminal widths, one per terminal, adding up to `inputs`: `1, 1, 1` is three terminals read as InputSignal(k, 0); unset is one terminal of all inputs |
| `outputWidths` | — | 選填（預設關閉） |  | PLECS output terminal widths, one per terminal, adding up to `outputs`: `1, 1, 1` is three terminals written as OutputSignal(k, 0); unset is one terminal of all outputs |

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

## 模型

程式碼以方塊名稱存放在電路中（C 程式碼視窗，或 circuit.setCode）。模擬開始時編譯；編譯錯誤會停止模擬，並指出方塊、行號與欄號。啟動程式碼在 t = 0 執行一次，之後每個被接受的求解步執行一次步進程式碼（ts > 0 時改在 0、ts、2*ts、… 執行，其間輸出保持），`t` 為時間，`delt` 為距上次執行的時間。`static` 變數與 PLECS 狀態在步與步之間保留其值。PLECS 連續狀態依 derivative 區段，在該步以 Heun 法積分。PLECS 各區段存成一段文字，每段前有一行 `//@section <名稱>`（declarations、start、output、update、derivative、terminate），與 PLECS 匯入的寫法相同。程式碼沒有指定的輸出保留先前的值。每次執行都有運算次數上限，無窮迴圈會以錯誤停止模擬，而不會卡住。

## 常見錯誤

- feedthrough = 1 時，C 方塊若位於沒有延遲的控制方塊迴路中，就形成代數迴路。輸出只取決於狀態時請設 feedthrough = 0（此時方塊在迴路中讀到前一步的輸入），或在迴路中加入 `zDelay`。
- ts = 0 時，`delt` 是求解步長，在可變步長模擬中每一步都不同：寫成 `x += k*u*delt` 的積分器仍然正確，寫成 `x += k*u` 的則不正確。若要固定的 `delt`，請把 ts 設為控制器的取樣時間。
- `#include` 只接受 C 標準標頭檔與 `Psim.h`；自己的標頭檔（或路徑）會被拒絕，請把其中的程式碼複製到方塊內。每個方塊的變數，以及電路中所有 C 方塊的變數合計，都有記憶體上限（2097152 與 4194304 個數值），而且每次呼叫函式時，每個區域數值都算一次運算，所以陣列請依控制器實際需要的大小宣告。

## 規格書與來源

沒有規格書。ts 取自韌體的中斷頻率；輸入、輸出、參數與狀態的數目取自你複製程式碼的那個方塊。

## 範例

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

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