Skip to content
Tactiverge

School/○ Innovator/Week 29

States, events and guards

A delivery robot gets an order with 25 % battery left. Should it go, or charge first? Who decides, and where is that rule written?

About 35 min · 3 goals · Checked by automated tests

1.Why should I care?

Behaviour written as nested if-statements becomes impossible to check. A state machine puts every behaviour in a table you can read, test and argue about, which is why lifts, traffic lights and robot task logic use them.

  • ◇ Describe robot behaviour as states, events, transitions and guards.
  • ◇ Trace a guarded state machine through a sequence of events by hand.
  • ◇ Explain why the order of rules matters when guards overlap.

2.Discover

Load the complete design. Set the battery to 25 % and send: order, charged, order, arrived, done. Write the state after each event before you press it. Then load each 'Bug' design and read what the checker says.

Loading the interactive…

Can't use the interactive? Read a description instead

The state-machine runner shows a table of rules: IDLE plus order goes to DRIVING if battery is at least 30 %, else CHARGING; DRIVING plus arrived goes to DELIVERING; DRIVING plus bump goes to FAULT; DELIVERING plus done goes to IDLE and costs 20 % battery; CHARGING plus charged goes to IDLE at 100 %; FAULT plus reset goes to IDLE. Sending events steps through the table.

3.Checkpoint: guided practice

Check what you just discovered before going on. Hints appear after a wrong answer.

Practice 1 · a fresh version every time

A delivery robot starts in IDLE (state 0) with its battery at 55 %. Rules: IDLE(0) + order → DRIVING(1) if battery ≥ 30 %, else → CHARGING(3). DRIVING(1) + arrived → DELIVERING(2). DRIVING(1) + bump → FAULT(4). DELIVERING(2) + done → IDLE(0) and the battery drops by 20 %. CHARGING(3) + charged → IDLE(0) with the battery at 100 %. FAULT(4) + reset → IDLE(0). Any other event in any state is ignored. Events arrive in this order: arrived, reset, reset, done. Which state number is it in at the end?

state number

4.Predict

The robot is DRIVING and receives the event 'order'. There is no rule for 'order' in DRIVING. What should happen?

Guess first. Being wrong here is how the surprise works.

5.Understand

The four ingredients

A state is a mode the robot can be in (IDLE, DRIVING). An event is something that happens (an order arrives, a bump sensor fires). A transition says: in this state, on this event, go to that state. A guard is a condition that must also be true, such as battery ≥ 30 %.

Guards split one event into cases

IDLE + order has two rules: battery ≥ 30 % → DRIVING, battery < 30 % → CHARGING. Together the guards cover every battery level exactly once. If they overlapped, two rules could fire and the result would depend on which is listed first; if they left a gap, some orders would vanish.

Ignored events

If no rule matches, the machine stays where it is. That is often right (a second order while driving), but it can hide a missing rule. Review each state and ask, for every event: should something happen here?

Explain it another way

A board game: your piece is on one square (state), a dice roll (event) moves it, and some squares have conditions (guards): 'only if you have a key'.

Worked example 1

Battery 45 %. Events: order, arrived, done, order. Where does the robot end?

IDLE → (45 ≥ 30) DRIVING → DELIVERING → IDLE, battery 25 %. Next order: 25 < 30, so CHARGING. Final state: CHARGING.

Common mix-ups

  • Guards are just extra if-statements you can add anywhere.
    ✓ Guards on the same state and event must together cover every case exactly once, or behaviour becomes order-dependent or silently lost.

6.Watch

→ Watch how a machine with no memory except its current state can still recognise patterns. Our robot is the same idea with actions attached.

Introduces machines that run on states and transitions, the same idea a traffic-light controller uses.

8 min 51 s · captions on YouTube · source

Nothing loads from YouTube until you press play.

The video is optional: everything you need is also in the lesson text and the interactive.

7.Practice on your own

Practice, not a test. Wrong answers get a hint and another try.

Practice 2 · a fresh version every time

A delivery robot starts in IDLE (state 0) with its battery at 35 %. Rules: IDLE(0) + order → DRIVING(1) if battery ≥ 30 %, else → CHARGING(3). DRIVING(1) + arrived → DELIVERING(2). DRIVING(1) + bump → FAULT(4). DELIVERING(2) + done → IDLE(0) and the battery drops by 20 %. CHARGING(3) + charged → IDLE(0) with the battery at 100 %. FAULT(4) + reset → IDLE(0). Any other event in any state is ignored. Events arrive in this order: arrived, arrived, charged, reset, order, reset, order. Which state number is it in at the end?

state number

Practice 3

Which pair of guards on IDLE + order is complete and non-overlapping?

Practice 4

Which of these is an event rather than a state?

Practice 5

Two rules both match DRIVING + bump: one goes to FAULT, one stays in DRIVING. What is wrong?

8.Apply it

Add a LOW_BATTERY_RETURN state to the delivery robot: while DRIVING, if a 'battery-warning' event arrives, it abandons the delivery and heads to the charger. Write the new rules as a table and trace one sequence through it.

9.Reflect

Concepts: Super-loops and finite state machines