25Industrial patterns
The state machine pattern: a batch sequence that cannot get stuck
A production-grade state machine: numbered states, entry and exit actions, a per-step timeout that raises a fault, and a fault state with one way out.
Why a state machine and not a pile of flags
Any process with an order to it (fill, hold, drain) belongs in a state machine. The alternative is a heap of BOOL flags that interlock over time, and six months later nobody knows which combinations are reachable. A state machine makes the state one number: the controller is in exactly one state, always, and that number goes straight to the HMI so an operator can see where the sequence stopped.
The skeleton
CASE Step OF
0: (* idle *)
1: (* fill *)
2: (* soak *)
3: (* drain *)
9: (* fault *)
END_CASE;Leave gaps in the numbering (0, 10, 20) if you expect steps to be inserted later. A transition is one line, Step := 2;, and nothing else.
Entry and exit actions
An entry action runs once when a step begins: reset a timer, clear a counter, open a valve. You detect entry by comparing the step with the previous step:
NewStep := Step <> PrevStep;
PrevStep := Step;NewStep is true for exactly one scan after every change. Use it to reset the step timers. The exit action is what you guarantee before leaving: close what you opened.
Every step needs a timeout
This is the line between a demo and a plant program. Any step that waits for a field event (a level to arrive, a limit switch to close) needs a timeout. Valves seize, pumps lose suction, transmitters die. Without a timeout the sequence waits forever and the operator sees "running" while nothing moves.
Without a timeout | With a timeout |
|---|---|
The sequence hangs silently | It goes to fault in seconds |
The operator guesses | The step number says where it failed |
Power cycle to free the logic | One reset button |
The fault state
The fault state does two things: it puts the outputs in a safe position, and it stays there. It never self-clears. Getting out needs a person: an explicit reset, and then you start from idle, not from the step that failed, because the state of the process is no longer known.
How you restart
Three rules that hold up in the field:
- Stop aborts to idle, not to pause, unless the process genuinely allows a pause.
- Reset clears the fault and does not start. A reset that also starts the machine surprises whoever is standing next to it.
- On power-up, start at state 0. Never resume from retained memory unless you designed for it on purpose.
Try it in the lab
The lab runs in your browser: an ST editor, a virtual process that suits the lesson, the same program as a live ladder diagram, a watch table, and export to Siemens SCL.
What to try
- Run the program, press START and follow Step: 1 fill, 2 mix for 3 s, 3 drain, then 0 with Batches up by one.
- Press STOP in the middle of any step: it returns to 0 and the pump and valve stop.
- Change 80.0 to 120.0 (a level that never comes) and download again: after 20 s in step 1, HiAlm lights instead of the batch hanging forever.
Terms in this lesson
People find this lesson by searching
plc state machine structured textCASE statement sequence plcbatch sequence plc programstep timeout plcfault state plc logicstate machine pattern structured textsequence control structured text
Course outline
- 01What is Structured Text, and when to use it instead of ladder
- 02How a PLC runs your code: the scan cycle and the process image
- 03Program structure: POUs, PROGRAM, FUNCTION and FUNCTION_BLOCK
- 04Your first program: starting a pump with seal-in logic
- 05Data types: BOOL, INT, REAL, TIME and the rest
- 06Declaring variables: VAR, VAR_INPUT and RETAIN
- 07Literals and constants: 16#FF, 2#1010 and T#5s
- 08Type conversion and scaling a 4-20 mA signal
- 09Assignment and arithmetic in ST: := and operator precedence
- 10Boolean logic, comparisons and bit operations
- 11Standard functions: ABS, SQRT, MIN, MAX, LIMIT, SEL, MUX
- 12IF, ELSIF and ELSE: the rules and when to avoid them
- 13The CASE statement and your first state machine
- 14The FOR loop and the watchdog
- 15WHILE, REPEAT, EXIT, CONTINUE and RETURN
- 16Edge detection: R_TRIG, F_TRIG and the "it fired every scan" bug
- 17Timers: TON, TOF and TP
- 18Counters: CTU, CTD and CTUD
- 19SR and RS flip-flops: which one wins
- 20Writing your own function
- 21Writing a function block
- 22Arrays in Structured Text
- 23Structs and enumerations
- 24References and pointers
- 25The state machine pattern: a batch sequence that cannot get stuck
- 26Alarm management to ISA-18.2: delay, deadband, latch, acknowledge, first-out
- 27Analogue signal processing: counts to engineering units, NE43 and filtering
- 28Closed-loop control in ST: hysteresis first, then a practical PID
- 29Motors and valves in practice: permissives, feedback and travel time
- 30The same ST on every platform: SCL, Studio 5000, CODESYS, TwinCAT
- 31Testing and debugging ST: the watch table, forcing and simulation
- 32An ST style guide: naming, comments, constants and version control
- 33Final project: a complete tank station with a state machine and alarms
- 3420 interview questions on ST and PLC programming, with answers