Click & Whirr needs a wider screen
Open it on a laptop or desktop (1280px or wider recommended). The Learn · Code · Watch workspace needs the room to breathe.
Lesson 09
One thing at a time
Everything so far has been pure reaction: sensors in, wheels out, no memory of what just happened. That breaks the moment a task has steps. This robot must visit A, then B, then stop, and a memoryless controller can’t: tell it to aim for B once it’s near A, and the instant it leaves A it forgets and turns back. It ping-pongs forever. A state machine fixes it: the robot keeps its current mode in self.state, does the simple thing for that mode, and latches to the next mode when a sensor says so: the same idea that runs elevators, traffic lights, and every real robot mission.
Checkpoints
- Reach waypoint A (waiting for Run)
- Then reach B, in order, and stop there (waiting for Run)
Press Run ▸ to start checking your code.
Run it first: watch it orbit A and never finish. Then uncomment the memory block (every line; the last one is what makes it act on its mode) so it latches to A → to B → done. self.stateis the whole trick: it’s what lets the robot remember which leg of the trip it’s on.
Steps
- 1
Latch from leg to leg
Touching A must flip
self.stateto"to B", and stayflipped after A is behind you. That’s the memory reaction can’t fake. - 2
Visit A, then B, and stop
▸API reference
- RobotStateMachine()
- Built once when you press Run, so anything you store on self survives from tick to tick. That persistence is the whole lesson.
- step(a_bearing, a_dist, b_bearing, b_dist)
- Called every tick. Bearings are radians (+ = the waypoint is to your left); distances are px. Return a (left, right) wheel-speed tuple.
- self.state (str)
- Your robot's current mode, shown live in the MODE HUD on the sim.
- robot.sensors.waypoints()
- The same four values as a tuple, via the facade: (a_bearing, a_dist, b_bearing, b_dist).
space pause · r reset