Click & WhirrAll lessons

Sensing · Lesson 06

Sense and avoid

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.

New sensors: three distance rays (front, left, and right) reporting how far the nearest obstacle is (you can see them in the sim). No line to follow now; the robot just knows which way the goal is and has to react to what it senses. This starter already seeks the goal and leans away from whatever’s close on a side, but it drives straight into anything dead-ahead, because a wall in front looks identical to the left and right rays. Give it a rule to break the tie.

Three rays, no line

The world has crates now. Give your robot a distance sense and a nerve to swerve.

There is no line to follow this time. Instead the robot has three distance rays pointed front, left, and right, each one reporting how far the nearest obstacle is along that direction (bigger means more open). It also knows which way the goal is, as a goal_bearing angle. Everything else it has to work out from what it senses, tick by tick.

The starter already does two sensible things. It heads for the goal by steering in proportion to the bearing, and it leans away from whichever side is more blocked by comparing the left and right rays. Read it in the editor and press Run: it threads most of the field, then drives straight into the first crate it meets head on. The next beat is why, and the fix.

Break the dead-ahead tie

Here is the blind spot. A crate sitting dead ahead is the same distance off your left shoulder as your right, so the two side rays read the same number. The lean rule subtracts one from the other and gets zero: no reason to go left, no reason to go right. The goal is straight through the crate, so the seek term keeps driving forward, and the robot plows in.

The front ray sees the crate coming. Use it. When the front reading drops below a threshold, stop waiting for the side rays to break the tie and commit to a side yourself: add a hard swerve toward whichever way is more open. That one rule turns the head on stall into a clean slip past. Write it, then Run and watch the robot pick a side and take it.

Go deeper: why the tie is a tie

The lean rule is a difference: it turns on left minus right. That is exactly the right instinct when the world is lopsided, an opening on one side and a wall on the other. But a crate square in front is symmetric. Both side rays glance off the same flat face at mirror-image angles and come back with the same distance, so the difference is zero and the lean rule falls silent. The information that something is in the way lives entirely in the front ray, which the difference never looks at. The tie-break is what puts the front ray in charge when the sides have nothing to say.

front (short)leftrightleft = right

Cross the field clean

The tie-break gets you past the first crate. The rest of the field is a matter of letting both rules work together: the swerve clears anything dead ahead, and the lean rule threads the gaps between. There is nothing new to write here.

Press Run and watch a full crossing. Crash into a crate and the run restarts from the start, so a clean line all the way to the goal is the real test. If it clips a crate on the way through, the swerve is firing too late or too soft, or too hard and into the next one. Nudge the threshold and the swerve strength, Run again, and find the crossing that reaches the goal untouched. That clean run finishes the lesson.

Ready to build it? The interactive lesson is where you write the code and watch the robot run.