Skip to content

A LaserScan Callback Function

Based on what you have learnt throughout this course so far, you should now be able to set up a subscriber within your code to subscribe to the /scan topic:

self.subscriber = rospy.Subscriber('/scan', LaserScan, self.callback)

The above assumes that you are using a Python Class structure in your code (which you really should be!), and we also assume that you have already imported the LaserScan message type correctly at the start of your code as well:

from sensor_msgs.msg import LaserScan

You could then develop a callback function (called callback() in this case) using the approach illustrated below.

Note

This example requires the numpy Python library, which you'll need to import at the start of your code in the following way:

import numpy as np

The callback() could then be developed to obtain (for instance) a 40° arc of LaserScan data ahead of the robot:

def callback(self, scan_data: LaserScan):
    left_arc = scan_data.ranges[0:21]
    right_arc = scan_data.ranges[-20:]
    front_arc = np.array(left_arc[::-1] + right_arc[::-1])
    self.min_distance = front_arc.min()

    # Optional Extra:
    arc_angles = np.arange(-20, 21)
    self.object_angle = arc_angles[np.argmin(front_arc)]

The distance to the closest object in front of our robot is then available throughout our class, as an attribute called: self.min_distance.

What we're doing here is illustrated in the figure below:

Let's talk through it too:

  1. From the front of the robot, we obtain a 20° arc of scan data either side of the x-axis:

    left_arc = scan_data.ranges[0:21]
    right_arc = scan_data.ranges[-20:]
    
  2. Then, we combine the left_arc and right_arc data arrays, flip them so that the data is arranged from left (-20°) to right (+20°), and then convert to a numpy array:

    front_arc = np.array(left_arc[::-1] + right_arc[::-1])
    
  3. Then, we obtain the minimum distance value within the front_arc array, to tell us the distance to the closest thing up ahead:

    self.min_distance = front_arc.min()
    
  4. Optionally, we can also determine the angular position of the closest object up ahead too...

    1. Create another numpy array to represent the angles (in degrees) associated with each of the data-points in the front_arc array above:
      arc_angles = np.arange(-20, 21)
      
    2. Determine the angle at which the minimum distance value is located in front of the robot, using the numpy.argmin() method:
      self.object_angle = arc_angles[np.argmin(front_arc)]
      

← Back to Part 4 - Exercise 4