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
Checkpoints · 0 of 2 (waiting for Run)
- Reach waypoint A (waiting for Run)
- Then reach B, in order, and stop there (waiting for Run)
Reaction cannot do “in order”
One errand at a time. Give the robot a memory so it can do things in order.
Everything so far has been pure reaction: sensors in, wheels out, nothing kept between ticks. That works right up until the job has steps. This robot must visit A, then B, then stop, and reaction alone cannot do it. Tell it to aim for B once it is near A, and the instant it leaves A it forgets where it was in the trip and turns back. It ping-pongs around A forever and never reaches B.
What it is missing is a sense of which leg of the trip it is on. That is a state machine: 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 the step is done. The same idea runs elevators, traffic lights, and every real robot mission. Read the skeleton in the editor and press Run: you will watch the memoryless version orbit A. The next beat is the latch that carries it onward.
Latch from leg to leg
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 · ctrl/cmd+enter run