Lite / Discovering How the World Works / Week 21
Instructions a robot can't misunderstand
'Walk forward until you reach the door.' Simple for you. Why could it send a robot through a wall?
Why should I care?
Most bugs in real machines aren't the machine's fault: they're instructions that meant one thing to the person who wrote them and something else to the machine. Learning to write exact instructions is a superpower for any engineer.
- Rewrite vague instructions as exact ones a machine can follow.
- Use position and facing direction to predict a robot's path.
- Break a route into short instruction sequences.
Predict
The robot starts at (0, 0) facing north and runs 'Forward 1' four times, then 'Turn right' and 'Forward 1' four times. Walls are at (2, 4) among others. What happens?
- It reaches (4, 4)
- It stops at (1, 4) because the next square is a wall
- It goes round the wall
Guess first, then reveal
It stops at (1, 4). The next Forward would put it in the wall at (2, 4), so it can't continue. The robot doesn't 'see' the wall and plan round it; it just follows the list. Avoiding walls is the programmer's job.
Discover
Try the four-forwards-then-turn program from the prediction and watch it get stuck. Then find a program that reaches the flag. Count your instructions.
What the interactive shows (text version)
The grid robot can be replaced by this description: on a 6-by-6 grid with walls at (1, 1), (2, 1), (3, 1), (3, 3) and (2, 4), the program 'Forward ×4, Turn right, Forward ×4' stops at (1, 4) because (2, 4) is a wall. Going up two, then east four, then north two reaches the flag at (4, 4) in 10 instructions.
Understand
Vague words to avoid
Words like a bit, until it looks right, nearby or carefully mean nothing to a machine. Replace them with things it can count or measure: Forward 3, until the distance sensor reads less than 10 cm, turn 90° right.
Track two things
To predict a grid robot, keep track of where it is (x, y) and which way it faces. Forward changes the position in the facing direction. Turns change only the direction. Writing both down after every step is the most reliable way to follow a program.
Chunk the route
Long programs are easier to write in chunks: 'get to the corridor', 'go along the corridor', 'turn into the room'. Each chunk is a short sequence. This is decomposition, and it makes mistakes much easier to find.
Explain it another way
Intuitive
Imagine guiding a blindfolded friend through an obstacle course using only 'step', 'turn left' and 'turn right'. You'd watch every step and count carefully, because they'll do exactly what you say.
Technical
The robot's state is (x, y, heading). Forward maps (x, y) to (x + dx, y + dy) where (dx, dy) depends on heading: north (0, 1), east (1, 0), south (0, −1), west (−1, 0). Turns rotate the heading by 90°.
Applied
Factory robots are programmed with exact coordinates and angles. A pick-and-place robot told to move 'roughly above the box' would be useless; it is told to move to (412.5 mm, 128.0 mm) at a precise speed.
Worked example 1
Make this exact: 'Go forward a few squares, then turn towards the flag.' The flag is 3 squares north and 2 east of the robot, which faces north.
Forward 1, Forward 1, Forward 1, Turn right, Forward 1, Forward 1. 'A few' became 3, 'towards the flag' became 'Turn right' (north to east), and the last part is 2 forwards.
Common mix-ups
The robot will steer round obstacles it can see.
✓ A robot following a fixed sequence doesn't look. It runs each instruction exactly, even into a wall.Turning left then right moves you sideways.
✓ Left then right just puts you back to facing the same way, in the same square.
Practice
Practice, not a test. Answers are checked on our server; a wrong answer gets a hint and another try. Nothing is saved.
Apply it
Write exact instructions to guide the grid robot from (0, 0) to the flag at (4, 4) in as few instructions as you can, avoiding all the walls. Can you beat 10?
Reflect
Which vague word do you use most when giving directions? What exact version could replace it?
Write your answer on paper or in your own notes. Lite never stores what you write.