How can I get all the information about the Laserscan from the sensor?

how can I find the distance with Turtlebot 3 Burger to forward,backward, right and left?

#!/usr/bin/env python
import rospy
from sensor_msgs.msg import LaserScan

def scan_callback(msg):
    global ranges,dis_foward
    ranges = msg.ranges
    dis_foward = ranges[359]
ranges = []
dis_foward = 0.0
#set sub,pub

scan_sub = rospy.Subscriber('/scan',LaserScan,scan_callback)

here is my code which print only the forward


The laser in Turtlebot 3 has a range of 360 degrees, and it also has 360 readings. So, we could consider each reading as 1 degree. This way, taking into account that the readings start in front of the robot, and that they go in counterclockwise direction, we could assume the following:

Forward: ranges[0] or ranges [360]
Backward: ranges [180]
Left: ranges[90]
Right: ranges[270]

Hope this helps!

1 Like

Thanks you so much, it’s so useful for me and others.

Great answer, but do you get it? I mean, this piece of information come from where? datasheet for instance. How do you get this from another laserScan?

Hello @leleo1972,

In my case, I just got the number of readings by using the Python len() function. Once you get the full Laser message (using a Subscriber), you can measure how many elements contains the ranges array with the following expression:


This will give the number of readings from the laser. Then, in order to get the range of the laser, you can get it by checking an specific laser message, with:

rostopic echo /scan -n1

Being /scan the name of laser the topic (which can be different for other robots). Now, from the output of this command, you will see that there are 2 values, angle_min and angle_max:

angle_min: 0.0
angle_max: 6.28000020981

These values are in radians, which means that the range starts at 0 radians (0 pi), and ends at 6.28 radians (2 pi), which is the same as saying that the range of the laser is 360º degrees.

Hope this helps,