State machines
On this page
A washing machine is always doing one thing: filling, washing or spinning. The same press on the door button opens the door when the machine is idle and does nothing while it spins. A state machine describes behaviour in this way: a short list of states, and the events that move the machine from one state to another.
See it
A controller has stopped a borehole pump because the borehole ran dry. It now waits for a person to press reset. Then the level in the tank falls to low. What does the pump do?
Each box is a state, with the output it gives written inside. Each arrow is a transition, with the event that causes it. Press an event: the controller moves only if an arrow for that event leaves the state it is in now.
The idea
A state is one situation the machine can be in. The list is short and fixed, and the machine is in exactly one state at any moment. This controller has three: IDLE, RUNNING and FAULT.
An event is something the machine watches for: a level switch, a timer that ends, a button. A transition is a move from one state to another, drawn as an arrow with the event that causes it. An event with no arrow out of the present state changes nothing.
The outputs follow the state. In RUNNING the pump is on. In IDLE and FAULT it is off. To know what the machine is doing, you need one fact only: its state.
The drawing is a state diagram. Draw it before the program is written, because a missing arrow, or a state with no way out, is easy to see on paper and hard to see in code. The lesson on state machines in Verilog builds one in hardware. A diagram with dozens of states stops being readable: divide such a job into two or three small machines.
One worked number
The pump controller starts in IDLE. Six events arrive in this order: tank low, dry run, tank low, reset, tank low, tank full. In which state does it end, how many times did the pump start, and how many events were ignored?
Show the worked solution, 3 steps
- Follow the first two events
IDLE with tank low moves to RUNNING: the pump starts (start 1). RUNNING with dry run moves to FAULT, and the pump stops.
- Look for the arrow that is not there
FAULT with tank low: no arrow leaves FAULT for that event, so it is ignored. FAULT with reset moves to IDLE.
- Finish the list
IDLE with tank low moves to RUNNING (start 2). RUNNING with tank full moves to IDLE. The controller ends in IDLE, with 2 starts and 1 event ignored. Check it in the figure: press Start again, then the six events in order.
The mistake to avoid
Check yourself
A generator controller has 4 states and watches 5 events. You write a table with one row for each state and one column for each event. How many cells must you fill, including those that say "stay in this state"?
The same event, tank low, starts the pump at one time and does nothing at another time. What decides which of the two happens?
A program uses four true or false flags to record what a machine is doing. The machine has 4 real states. How many combinations of the flags have no meaning?
Where you meet it
- A borehole pump controllerIdle, running and fault are its states, and the protection against a dry borehole is one arrow.
- A generator with automatic startStandby, cranking, running and cooling are states. The return of the mains is the event that begins cooling.
- A cold room in defrostCooling, defrost and drip time are states, and a timer that ends is the event between them.
- A machine is in exactly one state at a time, from a short fixed list.
- An event moves it only if an arrow for that event leaves the present state.
- The outputs follow the state.
- Draw the state diagram first, and keep the state in one variable, not in a set of flags.
Discussion
No comments yet. Questions, results from your own build, a better way to explain it: all welcome.
My notes on this page
Kept on this device only. Nothing is sent anywhere.
Tip: select words in the lesson, or tap a figure or an equation, to put a note right there.
Notes on earlier wording
The lesson has changed and these words are no longer in it. Your notes are kept, with the words you chose.