Ask Your Question
1

point_cloud2.read_points and then?

asked 2016-07-27 07:28:35 -0600

Mehdi. gravatar image

I have a topic publishing a point cloud of type sensors_msgs.PointCloud2. I can subscribe to it and read it using sensors_msgs.point_cloud2.read_points function but then I get and object of type "generator". How do I access the coordinates of the points in this object? I want to write a service that checks the maximum allowed height in some specific target areas (the point cloud represents the ceiling). I am using python.

edit retag flag offensive close merge delete

2 Answers

Sort by ยป oldest newest most voted
3

answered 2016-07-27 09:59:22 -0600

NEngelhard gravatar image

read_points gives you a generator (keyword 'yield'). So you can just iterate over it to find your obstacles and not crash your shiny robot

from sensor_msgs.msg import PointCloud2
from sensor_msgs import point_cloud2

def callback_pointcloud(data):
    assert isinstance(data, PointCloud2)
    gen = point_cloud2.read_points(data)
    print type(gen)
    for p in gen:
      print p  # type depends on your data type, first three entries are probably x,y,z
edit flag offensive delete link more
2

answered 2016-07-27 09:58:26 -0600

Mehdi. gravatar image

updated 2016-07-27 10:00:47 -0600

After looking into the source code I found out that this read_points is a generator function that yields the next value of a cloud each time it is called. It works also as an iterator and calling it in a loop will deliver all pointcloud points:

cloud_points = []
for p in point_cloud2.read_points(pc2, field_names = ("x", "y", "z"), skip_nans=True):
    cloud_points.append(p)

Here pc2 is my pointcloud of type sensor_msgs/PointCloud2

and cloud_points will then be a list of 3D points forming the cloud.

edit flag offensive delete link more

Comments

This would be too slow. Try this instead:

cloud_points = list(point_cloud2.read_points(cloud, skip_nans=True, field_names = ("x", "y", "z")))
ozgurovic gravatar imageozgurovic ( 2017-11-14 08:23:14 -0600 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower

Stats

Asked: 2016-07-27 07:28:35 -0600

Seen: 4,037 times

Last updated: Jul 27 '16