ROS Mini Challenge #8 – Extracting the highest value from laser readings

Written by Ruben Alves











What we are going to learn

Learn how to read data from a laser topic and extract the highest value.

List of resources used in this post


Where to find the code

Once you open the ROSject (, you will get a copy of it. You just have to click Open. Once open, inside the catkin_ws folder, you will find all the ROS packages associated with this challenge. You can have a look at it using the IDE, for instance.

To see the full code, open the IDE by going to the top menu and select Tools->IDE

Code Editor (IDE) - ROSDS

Code Editor (IDE) – ROSDS

Launching the simulation

Go to the top menu and select Simulations. On the menu that appears, click on the Choose launch file… button.

Choose lunch file to open simulation in ROSDS

Choose launch file to open simulation in ROSDS

Now, from the launch files list, select the launch file named rotw5.launch from the rosbot_gazebo package.


Finally, click on the Launch button to start the simulation. In the end, you should get something like this:

Turtlebot robot inside a wall in ROSDS

Turtlebot robot inside a wall in ROSDS


The problem to be solved

As you can see, this ROSject contains 1 package inside its catkin_ws workspace: rotw8_pkg. This package contains a very simple Python script, which contains a Python class named Challenge(), with some functions in it.

The main purpose of this script is to get the highest value from all the laser readings, and also the position in the array of this particular reading.

In order to test that it works as expected, all you have to do is the following:

First, make sure you source your workspace so that ROS can find your package:
source ~/catkin_ws/devel/setup.bash


Now, start the TF Broadcaster and Listener with the following command:
rosrun rotw8_pkg

If everything is correct, you will get a message like this in the Shell:


ROS Mini Challenge #8 - Expected output

ROS Mini Challenge #8 – Expected output

NOTE: The numbers in the value of the maxim and the position in the array may vary a little bit from the ones in the image above (but not much).

Solving the ROS Mini Challenge

Ok, so… where’s the problem? If you have tried to reproduce the steps described above you have already seen that it DOES NOT WORK. When you run the programs introduced above, the values you get are not the correct ones at all. So… what’s going on?

When you launch rosrun rotw8_pkg, the output is not the expected one:

$ rosrun rotw8_pkg
Highest value is 4.50035715103 and it is in the position 0 of the array.

The difference is almost 2 meters, and position 0 (zero) is really different from 202, which is the more or less the expected one. The reason why we expect something like position 202 is because, on the right of the robot we have the position 0 in the scan, in the left, we have position 719, right in front we have 360, so the highest distance should be close to the exit, which is around the position 202. You may understand better by looking at the image below:

Laser range positions in the turtlebot simulation in ROSDS

Laser range positions in the turtlebot simulation in ROSDS


If we check the ~/catkin_ws/src/rotw8_pkg/src/ file, its original code is:

#! /usr/bin/env python

import rospy
import time
from sensor_msgs.msg import LaserScan

class Challenge:
    def __init__(self):
        self.sub = rospy.Subscriber("/kobuki/laser/scan", LaserScan, self.laser_callback)
        self.laser_msg = LaserScan()
    def laser_callback(self, msg):
        self.laser_msg = msg

    def get_laser_full(self):
        return [self.laser_msg.ranges[0], self.laser_msg.ranges[719]]

    def get_highest_lowest(self):
        l = self.get_laser_full()
        i = 0
        maxim = -1
        for value in l:
            if value >= maxim and str(value) != "inf":
                maxim = value
                max_pos = i
            i = i + 1

        print "Highest value is " + str(maxim) + " and it is in the position " + str(max_pos) + " of the array."

if __name__ == '__main__':
    rospy.init_node('rotw8_node', anonymous=True)
    challenge_object = Challenge()

    except rospy.ROSInterruptException:

If we look carefully, we can notice that the problem is in the get_laser_full function

 def get_laser_full(self):
        return [self.laser_msg.ranges[0], self.laser_msg.ranges[719]]

As we can see, it only returns two values of the laser.

To solve the problem we just change this function by:

 def get_laser_full(self):
        return self.laser_msg.ranges

If you now save the file and run the “rosrun rotw8_pkg” command again, now we should get the desired output:

$ rosrun rotw8_pkg
Highest value is 7.23973226547 and it is in the position 203 ofthe array.

Hey, now the value is the correct one. The problem was really easy, wasn’t it?

If you are still struggling to understand the code, or you want to master your ROS Skills, I highly recommend you take some courses in Robot Ignite Academy:

Youtube video

So this is the post for today. Remember that we have the live version of this post on YouTube. If you liked the content, please consider subscribing to our youtube channel. We are publishing new content ~every day.

Keep pushing your ROS Learning.


Check Out These Related Posts


Pin It on Pinterest

Share This