http://www.theconstructsim.com/read-laserscan-data/
We can reproduce the question easily using RDS. If you haven’t had an account yet, you can register one for free here. After registration, you can log in and open the project from Public(click on my projects to switch to public simulations) -> Kobuki. In the project, click simulations -> Turtlebot 2 to launch the simulation.
The simulation is up and running now. You can check all the topic available with the command
1
$ rostopic list
The LaserScan topic is called /kobuki/laser/scan. You can type the following command into the terminal to check the topic.
1
$ rostopic echo /kobuki/lase/scan -n1
The -n1 flag prints the topic exactly once. You’ll get something like this.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
1
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
Related content: [ROS Q&A] 069 - RQT Custom Plugin: undefined symbol
1
rosmsg show sensor_msgs/LaserScan
The command gives you the message’s structure.
1
2
3
4
5
6
7
8
9
10
11
12
13
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.
1
2
3
4
$ 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
1
2
3
4
5
6
7
8
9
10
11
#! /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
1
$ chmod +x src/scan.py
Then we create a launch file under lunch directory to launch the script
1
2
3
4
5
Now you can type
Related content: [ROS Q&A] 081 - Making Aibo robot walk with ROS - #Part 1
1
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.
1
2
3
4
5
6
7
8
9
…
def callback(msg):
# values at 0 degree
print msg.ranges[]
# 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. If you want to learn more about this topic, you can go to Robot Ignite Academy. We have tons of courses which teach you how to navigate the robot in a more advanced way.
手机扫一扫
移动阅读更方便
你可能感兴趣的文章