Skip to main content

Section 10.4 ActiveCode Exercise: LaserScan Safety Stop Callback

This exercise writes a callback function that listens to a mock sensor_msgs/LaserScan topic stream, extracts the minimum range value from each scan, and flags a safety stop if an obstacle is within 0.5 meters.

Subsection 10.4.1 Callback Structure

Each simulated LaserScan message is represented as a list of range readings. The callback function finds the minimum value in that list, compares it against a safety threshold of 0.5 meters, and returns or prints "SAFETY STOP" if the minimum is below the threshold, or "CLEAR" otherwise.

Subsection 10.4.2 Try It

Run the code below, then edit the scan lists to test other combinations of readings.

Subsection 10.4.3 Threshold Logic

Using the minimum reading, rather than the average or a single fixed index, matters here: any single beam detecting an obstacle close to the robot is enough to warrant a stop, even if most of the surrounding readings show open space.

Subsection 10.4.4 Test Case Interpretation

Given the mock scan \([1.2, 0.9, 0.45, 1.5, 2.0]\text{,}\) the minimum reading is 0.45 meters, which is below the 0.5 meter threshold, so the callback should report "SAFETY STOP". Given the scan \([1.2, 0.9, 0.8, 1.5, 2.0]\text{,}\) the minimum is 0.8 meters, above the threshold, so the callback should report "CLEAR".
You have attempted of activities on this page.