State Machine (stateMachine)
Runs a sequence of named states; guard expressions over its inputs and the time in the state move it from one state to the next.
Group: Control · Kind: {"block":"stateMachine","kind":"control"}
When to use it
Use it for start-up, precharge, takeover and fault sequences that would otherwise be a tangle of gates, latches, counters and one-shots. Use SR Latch or Monostable for a single flag or a single pulse.
Pins
| Pin | Domain | Meaning |
|---|---|---|
in1 |
signal | First input. A definition with more inputs adds in2 to in8; a guard reads an input by its name or as in1, in2 … |
out1 |
signal | First output, the value the active state gives it. A definition with more outputs adds out2 to out7 |
state |
signal | Index of the active state, counted from 0 in the order the states are listed |
Common parameters
definition: States, transitions and outputs as JSON; edit it with circuit.setStateMachine, and the pins follow it (Default: required)fs: Rate at which the machine is evaluated; 0 evaluates it at every solver step (Default: 0)
Parameters
| Key | Unit | Default | Typical range | Description |
|---|---|---|---|---|
definition |
— | required | States, transitions and outputs as JSON; edit it with circuit.setStateMachine, and the pins follow it | |
fs |
Hz | 0 | Rate at which the machine is evaluated; 0 evaluates it at every solver step |
Parameter descriptions are in English in both languages.
Model
The definition lists the inputs, the outputs, the states with each output's value (an expression; an output a state leaves out is 0), the initial state and the transitions {from, to, guard, priority}. At every sample (every 1/fs seconds from t = 0, or every solver step when fs is 0) the transitions out of the active state are tried, the smallest priority first and, at equal priorities, in the order they are listed; the first guard that is true (above 0.5) fires, and at most one transition fires per sample. A transition resets t_in_state, the time spent in the active state, also when it returns to the same state. The outputs are then set from the new state and held until the next sample. Guards and output values may read the inputs, t_in_state and the circuit variables. With fs above 0 the sample instants are reported to the solver, t_in_state counts whole samples, and code generation writes the machine as a C switch statement.
Common mistakes
- With fs = 0 a timed guard such as t_in_state > 10m fires at the first solver step after 10 ms, so the timing depends on the step. Set fs to the rate of the controller you model, and write a timed guard half a sample early (t_in_state > 9.5m at 1 kHz) so rounding never moves it a whole sample.
- A state that no transition reaches, a transition to a state that does not exist, a guard that does not parse or that reads an unknown name are errors before the run; the message names the state, the transition or the guard. Edit the definition with circuit.setStateMachine, which checks it and moves the pins.
Datasheet and sources
There is no datasheet. The states and timings come from the sequence your system needs, such as the contactor order and the precharge time of a battery pack or the takeover rules of a backup unit.
Example
Open the example with the command file.openExample and the name demo:stateMachine.
Generated by cargo xtask reference from the usage texts and parameter specs. Do not edit by hand.