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
Checkpoints · 0 of 2 (waiting for Run)
- Slip past the first obstacle (waiting for Run)
- Reach the goal without crashing (waiting for Run)
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
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 · ctrl/cmd+enter run