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 06
Sense and avoid
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 reactto 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.
Checkpoints
- Slip past the first obstacle (waiting for Run)
- Reach the goal without crashing (waiting for Run)
Press Run ▸ to start checking your code.
Uncomment the frontrule so it commits to a side when something’s right in front. Crash and you restart: cross the field clean to win.
Steps
- 1
Break the dead-ahead tie
A wall straight ahead reads the same on both side rays, so the lean rule computes zero. When
frontis blocked, commit to a side. - 2
Cross the field clean
▸API reference
- avoid(front, left, right, goal_bearing)
- Called every tick with the three distance rays and the goal bearing. Return a (left, right) wheel-speed tuple.
- front / left / right (float)
- Distance (px) to the nearest obstacle along each ray, capped at 130. Bigger = more open; a wall dead-ahead reads the same on both side rays.
- goal_bearing (float)
- Angle (radians) from your heading to the goal; + = the goal is to your left.
- robot.sensors.ranges() / robot.sensors.goal_bearing()
- The same values via the facade: ranges() returns the (front, left, right) tuple.
space pause · r reset