Ask Your Question

Revision history [back]

Hi @icywhite!

I think that your question involves many steps. You will probably have to:

  1. Read the velodyne_msgs/VelodyneScan messages from a bag file
  2. Convert those velodyne_msgs/VelodyneScan messages to sensor_msgs/PointCloud2
  3. Read X, Y, Z values from PointCloud

You probably could extract X Y Z directly from the velodyne_msgs/VelodyneScan, but I'm not sure how to do that, then let's see by converting to PontoCloud2.

1. Reading a bag file

To read from a bag file we can follow http://wiki.ros.org/rosbag/Code%20API that has the following example:

import rosbag
bag = rosbag.Bag('test.bag')
for topic, msg, t in bag.read_messages(topics=['chatter', 'numbers']):
    print msg
bag.close()

In the example above, the topics chatter and numbers are being read from the test.bag file.

2. Converting velodyne_msgs/VelodyneScan messages to sensor_msgs/PointCloud2

To convert Velodyne messages to PointCloud, you can take a look at the velodyne_pointcloud package.

Or if you want to convert manually by yourself, you can take a look on the function processScan that converts velodyne_msgs::VelodyneScan to PointCloud.

3. Read XYZ from PointCloud2

To read XYZ from PointClouds, you could use the example below that is shown in another question:

import sensor_msgs.point_cloud2
...
for point in sensor_msgs.point_cloud2.read_points(msg, skip_nans=True):
            pt_x = point[0]
            pt_y = point[1]
            pt_z = point[2]

Cheers.