Skip to content

BONK!!

Goal: drive forward, watch the distance ahead with the lidar, and brake when an obstacle is too close.

The idea

Now you use both directions: your node subscribes to the lidar to see, and publishes speeds to act. A program that reacts to what its sensors see is called a reactive controller.

The lidar publishes a LaserScan message on /scan, five times per second. It contains a list called ranges: one distance, in meters, for each direction around the robot.

The lidar measures a distance in every direction. You look at the narrow window in front.

Your function keeps the readings inside the window and returns the smallest one: the distance to the closest thing ahead.

Look at it live

  • Make sure the simulator runs in the small house, so there are walls to detect.
  • Run ros2 topic echo /scan --once. At the top, find angle_min, angle_max, angle_increment, range_min and range_max. Then find the long ranges list.
  • Run ros2 topic hz /scan. It prints how many messages arrive per second. Stop with Ctrl+C.

Good to know

The lidar is mounted turned around by 180 degrees, so the robot's front is at scan angle 3.14 (pi), not 0. Some readings are inf or nan (nothing was measured). They are bad readings: skip them.

Read the code

Numbers in bulk with numpy. The lidar sends 360 distances at once. numpy is the standard Python library for working with many numbers together. An operation on a numpy array applies to every element, and a mask keeps only the elements you want.

import numpy as np

ranges = np.array([2.0, np.inf, 0.5, 1.2])   # 4 readings. inf = nothing was measured
good = np.isfinite(ranges)                    # [True, False, True, True]
print(ranges[good].min())                     # 0.5, the smallest good reading

Your function builds a mask that keeps the good readings pointing at the front, and returns the smallest of them.

Two jobs, kept apart. Your node has two jobs, and they are kept apart on purpose.

scan_callback only decides. The timer is the only place that publishes.

  • scan_callback runs when a scan arrives. It does not publish. It only sets a flag, self.blocked, to True or False.
  • drive_forward runs on a timer, like in Challenge 1. It is the only place that publishes on /cmd_vel: speed 0 if blocked, otherwise SPEED.

Why a flag?

If the scan callback published a zero, the timer would publish the forward speed again 0.1 s later, and the robot would never really stop. One node, one publisher.

Your mission

Part A: find what is in front

Open starter_kit/lidar_utils.py and write front_min_distance().

  • TODO 1: with numpy, compute the angle of every reading, build a mask of the good readings within 15 degrees of the front, and return the smallest distance (or None if the mask selects nothing). No for loop is needed.

Part B: brake

Open starter_kit/bonk.py and complete the TODOs.

  • TODO 2: subscribe to /scan using LaserScan.
  • TODO 3: get the closest distance in front of the robot.
  • TODO 4: if it is below BONK_THRESHOLD, set self.blocked to True (otherwise False), and print BONK!! only when the robot newly becomes blocked.

One driver at a time

Only one node may publish to /cmd_vel. Stop any other driver with Ctrl+C before you start this one. The nodes warn you if you forget.

Run your node with ros2 run starter_kit bonk.

What you should see

The robot drives forward and stops at about the threshold distance, give or take 5 cm. The terminal prints BONK!! Obstacle at 0.xx m at least once.

Stuck?

Hint 1

The formula for the angle of every reading is in the comment at the top of lidar_utils.py. With numpy it is one line: np.arange(len(ranges)) gives the index of every reading.

Hint 2

Angles wrap around at a full circle. The comment in the file shows a safe way to compare them with np.arctan2.

Hint 3

Bad readings are inf, nan, or outside range_min to range_max. Combine all the conditions with &. If the mask selects nothing, return None.

Submit

When it works, open the Submit tab, choose 3 · BONK!!, and submit.