[ROS Q&A] 031 – How to read LaserScan data (ROS python)

How to read LaserScan data ros python

Written by Alberto Ezquerro

26/09/2017

About

In this video, we are going to see this question on ROS Answers – How to know the exact frame rate and angle of /scan on Turtlebot?

Step 1. Open a project on ROS Development Studio(ROSDS)

We will reproduce the question by using ROSDS. You can create a free account here.

After registration, login and add a new ROSject. Now, run the project on ROSDS and launch the Turtlebot2 by clicking the Simulation button.

Step 2. Read LaserScan data

The simulation is up and running now. You can check all the topic available with the command:

$ rostopic list

The LaserScan topic is called /kobuki/laser/scan. You can type the following command into the terminal to check the topic.

$ rostopic echo /kobuki/lase/scan -n1

The -n1 flag prints the topic exactly once. You’ll get something like this.

header:
  seq: 5
  stamp:
    secs: 2829
    nsecs:  69000000
  frame_id: "laser_sensor_link"
angle_min: -1.57079994678
angle_max: 1.57079994678
angle_increment: 0.00436940183863
time_increment: 0.0
scan_time: 0.0
range_min: 0.10000000149
range_max: 30.0
ranges: [inf, inf, inf, inf, inf, ....]

The following command will give you the information for the topic

rostopic info /kobuki/laser/scan

You’ll see that the topic is using the message type called sensor_msgs/LaserScan. You can further check it with the command

rosmsg show sensor_msgs/LaserScan

The command gives you the message’s structure.

std_msgs/Header header
  uint32 seq
  time stamp
  string frame_id
float32 angle_min
float32 angle_max
float32 angle_increment
float32 time_increment
float32 scan_time
float32 range_min
float32 range_max
float32[] ranges
float32[] intensities

The angle_min and angle_max indicate the angle range(from -90 to 90 degree in this case) that the LaserScan is measuring and the ranges is an array which gives you the distance measured for each angle bin.

To explore the range value, let’s create a package.

$ cd ~/catkin_ws/src
$ catkin_create_pkg laser_values rospy
$ cd laser_values
$ mkdir launch

Add a python script under src with the following code

#! /usr/bin/env python

import rospy
from sensor_msgs.msg import LaserScan

def callback(msg):
    print len(msg.ranges)

rospy.init_node('scan_values')
sub = rospy.Subscriber('/kobuki/laser/scan', LaserScan, callback)
rospy.spin()

Normally, you’ll need to give the script permission to execute with

$ chmod +x src/scan.py

Then we create a launch file under lunch directory to launch the script

<launch>
    <node pkg="laser_values" type="scan.py" name="scan_values" output="screen">
        
    </node>
</launch>

Now you can type

roslaunch laser_values laser.launch

to launch the script. You should also see that the length of the ranges array is 720 in the 180-degree range. So if we want to read the LaserScan data on the left, in front and on the right of the robot, we can change our script a bit.

...
def callback(msg):
    # values at 0 degree
    print msg.ranges[0]
    # values at 90 degree
    print msg.ranges[360]
    # values at 180 degree
    print msg.ranges[719]
...

That’s all now you get the value. You can play with it to navigate the robot.

Resources related to this topic:

 

Edit by Tony Huang

Topics:
Masterclass 2023 batch2 blog banner

Check Out These Related Posts

129. ros2ai

129. ros2ai

I would like to dedicate this episode to all the ROS Developers who believe that ChatGPT or...

read more

13 Comments

  1. Devan

    Hi,

    If the range of the laser beam is [-1.57rad: +1.57rad], why is the ranges[0] value not equal to -1.57rad?

    Reply
    • Alberto Ezquerro

      Hi Devan,

      The values at the ranges array are the values of the readings of the laser. So, the ranges[0] value is the lecture of the laser at -1.57rad (-90º), which will depend on the environment. If there is no obstacle in that direction, the value will be infinite, if there’s an obstacle at 1 meter of the laser in that direction, the value will be 1.0, etc…

      Hope it clarifies your doubts!

      Reply
      • Anonymous

        Is there any way to increase the range from 1m? As far as I know lidars have a greater range, for instance the range of a2 rplidars is 12m

        Reply
    • Sugun Segu

      “range” is an array, to access the data of an array we use the index and the index of “range” is from 0-719. data from those -1.57-1.57 angles come and stores into that array. so just take it as an array.

      Reply
  2. David

    Hi, do you have this video on C++ version also using catkin_make?

    Thanks

    Reply
  3. tata sudiyanto

    Hi,
    how do we access the scan data at previous time stamp? For instance, i would like to process the range data by comparing every two consecutive time stamps to get a displacement rate.

    Many thanks.

    Reply
    • Vicky

      ##I think this will do the trick. please find the code below:

      dk_1 = np.array([]) creating a global variable to hold previous timestamp data

      def scanProcessing(msg): ### A typical callback function which gets activated or called based on the topic scan
      print(“Inside scan proceesing function”)
      global i, dk_1, dmin
      count = 0
      dk = np.array(msg.ranges)
      i += 1
      for k in range(len(dk)):
      if dk[k] == math.inf:
      dk[k] = 30.0
      print(“Inf correction is completed”)
      if i == 1:
      dk_1 = dk
      else:
      ##process what you want to do with the previous timestamp

      ## Hope its not too late

      dk_1 = dk ## Assigning the current data to the previous timestamp holding variable

      Reply
  4. Vignesh

    Hello Construct team
    I am trying to plot the position of a moving object detected by a stationary robot in rviz or graphically. Can you suggest a way to do so ?

    thanks in advance

    Reply
  5. my favorite spot!

    Stealing the ingredients of potion of changing forms n faces

    Reply
  6. Anonymous

    Is there any way to increase the range from 1m? As far as I know lidars have a greater range, for instance the range of a2 rplidars is 12m

    Reply
  7. William

    HI, If i have a Lidar sensor. Can i still apply this script to get readings?

    Reply
  8. Prasham SOni

    [scan_values-2] process has died [pid 5988, exit code 127, cmd /home/vboxuser/catkin_ws1/src/laser_values/src/scan.py __name:=scan_values __log:=/home/vboxuser/.ros/log/e798bb36-cf9c-11ed-b772-11f52b4f9236/scan_values-2.log].
    log file: /home/vboxuser/.ros/log/e798bb36-cf9c-11ed-b772-11f52b4f9236/scan_values-2*.log

    iam getting this error how to solve this

    Reply

Submit a Comment

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Pin It on Pinterest

Share This