C Block (cblock)
Runs C code you write, a PSIM C Block or a PLECS C-Script, as a control block, in a sandbox that cannot crash or hang the simulation.
Group: Control · Kind: {"block":"cblock","kind":"control"}
When to use it
Use it for control logic that is easier to write as code than to draw: a state machine, a protection sequence, a lookup with branches, a controller copied from firmware. Use the built-in blocks (pi, dpi, ztf, svpwm) when one does the job: they are faster and code generation writes them as fixed-point C.
Pins
| Pin | Domain | Meaning |
|---|---|---|
in1 |
signal | First input: x1 or in[0] (PSIM), InputSignal(0, 0) (PLECS). |
in2 |
signal | Second input, read when inputs is 2 or more. |
in3 |
signal | Third input, read when inputs is 3 or more. |
in4 |
signal | Fourth input, read when inputs is 4 or more. |
in5 |
signal | Fifth input, read when inputs is 5 or more. |
in6 |
signal | Sixth input, read when inputs is 6 or more. |
in7 |
signal | Seventh input, read when inputs is 7 or more. |
in8 |
signal | Eighth input, read when inputs is 8. |
out1 |
signal | First output: y1 or out[0] (PSIM), OutputSignal(0, 0) (PLECS). |
out2 |
signal | Second output, written when outputs is 2 or more. |
out3 |
signal | Third output, written when outputs is 3 or more. |
out4 |
signal | Fourth output, written when outputs is 4 or more. |
out5 |
signal | Fifth output, written when outputs is 5 or more. |
out6 |
signal | Sixth output, written when outputs is 6 or more. |
out7 |
signal | Seventh output, written when outputs is 7 or more. |
out8 |
signal | Eighth output, written when outputs is 8. |
Common parameters
dialect: Whose C block the code is written for:psim(C Block: t, delt, x1…, y1…) orplecs(C-Script: sections and the PLECS macros) (Default: psim)inputs: Inputs the code reads, 0 to 64 (in1 … in64) (Default: 1)outputs: Outputs the code writes, 0 to 64 (out1 … out64) (Default: 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 setsampleTimeinstead (Default: 0)
Parameters
| Key | Unit | Default | Typical range | Description |
|---|---|---|---|---|
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 |
— | optional (off) | 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 |
— | optional (off) | 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 |
— | optional (off) | 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 |
Parameter descriptions are in English in both languages.
Model
The code is kept with the circuit under the block's name (C Code window, or circuit.setCode). It is compiled when the simulation starts; a compile error stops the run and names the block, the line and the column. The start-up code runs once at t = 0, then the step code runs once per accepted solver step (or at 0, ts, 2*ts, ... when ts > 0, with the outputs held between), with t the time and delt the time since the previous run. static variables and PLECS states keep their values between steps. PLECS continuous states are integrated with Heun's method over that step, from the derivative section. PLECS sections are kept in one text, each after a //@section <name> line (declarations, start, output, update, derivative, terminate), as the PLECS import writes them. An output the code does not assign keeps its previous value. Every run has an operation budget, so an endless loop stops the simulation with an error instead of hanging it.
Common mistakes
- With feedthrough = 1 a C block inside a loop of control blocks with no delay is an algebraic loop. Set feedthrough = 0 when the outputs depend only on the state (then the block reads the previous step's inputs inside the loop), or put a
zDelayin the loop. - With ts = 0,
deltis the solver step, which a variable-step run changes from step to step: an integrator written asx += k*u*deltstays right, one written asx += k*udoes not. Set ts to the controller's sample time to get a fixeddelt. #includetakes only the C standard headers andPsim.h; a header of your own (or a path) is refused, so copy the code it holds into the block. Each block's variables, and those of all C blocks in a circuit together, have a memory limit (2097152 and 4194304 values), and every function call costs one operation per local value, so size arrays to what the controller needs.
Datasheet and sources
There is no datasheet. Take ts from the firmware's interrupt rate and the counts of inputs, outputs, parameters and states from the block you copy the code from.
Example
Open the example with the command file.openExample and the name demo:cblock.
Generated by cargo xtask reference from the usage texts and parameter specs. Do not edit by hand.