TactivergeLite

Lite / Intelligent Robotic Systems / 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 · Full interactive version

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.

Predict

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

  1. It crashes
  2. Nothing: the event is ignored in this state
  3. It starts a second delivery
Guess first, then reveal

By the usual convention the event is ignored, which is fine here. But silent ignoring can also hide bugs, so a checker should flag events that a designer probably meant to handle.

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.

What the interactive shows (text version)

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.

You can also open the interactive on the full site when you have a faster connection.

Watch (optional)

Watch for: 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.

“Computers Without Memory - Computerphile” by Computerphile (opens YouTube; streaming video uses a lot of data)

The video is optional. Everything you need is in this lesson's text.

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

Intuitive

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'.

Technical

A finite state machine is a set of states S, events E, an initial state and a transition function δ(s, e, context) → s′. Guards make δ depend on context; for determinism, guards on the same (s, e) must be mutually exclusive.

Applied

Lift controllers, vending machines and the task layer of many service robots are state machines, often drawn as diagrams and then generated or hand-coded as transition tables.

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

Practice

Practice, not a test. Answers are checked on our server; a wrong answer gets a hint and another try. Nothing is saved.

state number

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?

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.

Reflect

Where in the delivery robot would silently ignoring an event be dangerous?

Write your answer on paper or in your own notes. Lite never stores what you write.

← One brain or many?No state left behind →