Integration · Lesson 09
One thing at a time
This is the written walk-through of the lesson: the idea, stage by stage, plus the deeper asides. The interactive version, where you write real Python in the browser and drive a simulated robot, lives on the lesson page.
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.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
The starter picks its target from what it senses this instant: near A, aim for B; otherwise aim for A. That is the trap. The moment the robot pulls away from A, the sensor no longer says near A, so the choice flips straight back to A and the robot never commits to the second leg.
A latch breaks the loop. Touching A must flip self.state to "to B" and stay flipped after A is behind you, so the robot keeps driving for B even when A is far away again. Then touching B flips the state to "done". The last move is the one that matters most: choose the target from self.state, not from where the robot happens to be. That is the memory reaction cannot fake. Write the latch, then Run and watch the robot reach A and hold its course for B.
Go deeper: why self.state remembers
A plain function forgets everything the moment it returns. Every tick it starts over from an empty slate, which is exactly why pure reaction cannot sequence: there is nowhere to write down which leg you are on. A class is different. When you press Run the sim builds one instance and keeps it, so __init__ runs a single time and then the same object is handed step() sixty times a second. Anything you store on self, like self.state, is still there on the next tick because the instance itself is still there. The latch writes to that surviving box, and the box is what remembers.
Visit A, then B, and stop
With the latch in, the whole mission falls into place. Watch the MODE HUD on the sim step through to A, then to B, then done. Each transition fires exactly once, when the matching waypoint is touched, and never flips back. There is nothing new to write here.
Press Run and follow the full trip: the robot drives to A, latches, turns for B, latches again, and stops on B for good. If it ping-pongs around A instead of leaving for B, the latch is not in yet, or the target is still being read from the sensor rather than from self.state. A clean run to A, then B, then a stop finishes the lesson.
Ready to build it? The interactive lesson is where you write the code and watch the robot run.