Ask Your Question

Converting x, y, z array to point cloud data

asked 2021-03-02 02:15:02 -0500

Vignesh_93 gravatar image

updated 2021-03-05 03:40:11 -0500

Hello ROS developers, I am trying to convert a2D array to a point cloud value and publish it thereby visualising it in rviz. Can anyone guide me to solve the below issue which I am facing during the conversion ? This is the code and I get the error stated below that.

def scanProcessing(msg):
    laserProj = LaserProjection()
    cloud_out = laserProj.projectLaser(msg)
    xyz_array = ros_numpy.point_cloud2.pointcloud2_to_xyz_array(cloud_out)
    stamp =
    frame_id = 'odom'
    xyz_coordinate = [[x[i],y[i], z[i]] for i in range(len(x))]
    header = std_msgs.msg.Header()
    header.stamp =
    header.frame_id = 'map'
    cloud_msg = ros_numpy.point_cloud2.array_to_pointcloud2(xyz_array, stamp, frame_id)

if __name__ == "__main__":
    pointcloud_publisher = rospy.Publisher("/my_pointcloud_topic", PointCloud, queue_size=1)
    scanSubscriber = rospy.Subscriber('/robot2/scan', LaserScan, scanProcessing, queue_size=1)        

The Error observed is:

[ERROR] [1614671648.584787, 6.342000]: bad callback: <function scanProcessing at 0x7f88928728b0>
Traceback (most recent call last):
  File "/opt/ros/noetic/lib/python3/dist-packages/rospy/", line 750, in _invoke_callback
  File "", line 120, in scanProcessing
    cloud_msg = ros_numpy.point_cloud2.array_to_pointcloud2(xyz_array, stamp, frame_id)
  File "/opt/ros/noetic/lib/python3/dist-packages/ros_numpy/", line 147, in array_to_pointcloud2
    cloud_msg.fields = dtype_to_fields(cloud_arr.dtype)
  File "/opt/ros/noetic/lib/python3/dist-packages/ros_numpy/", line 92, in dtype_to_fields
    for field_name in dtype.names:
TypeError: 'NoneType' object is not iterable

P.S: As I understand this is a bit basic question. I would really appreciate some answers which could help me to continue my ROS journey

Edit 1: To emphasize more on the issue side from the comments :

Inside the function array_to_pointcloud2 there is a conversion to a 2D array. After which an object of pointCloud2 is created and the field attribute is associated with the data type of the array which in my case is 'float64' So basically dtype = 'foat64' All all good till the below function, iteration over dtype.names takes place which is None (which is logical), is there anything which we must do to tag it along with the dtype ?

 def dtype_to_fields(dtype):
         fields = []
         for field_name in dtype.names:
             np_field_type, field_offset = dtype.fields[field_name]
             pf = PointField()
    = field_name
             if np_field_type.subdtype:
                 item_dtype, shape = np_field_type.subdtype
                 pf.count =
                 np_field_type = item_dtype
                 pf.count = 1 ...(more code)...
edit retag flag offensive close merge delete


Could you be more specific? For troubleshooting, you can do check your variables and check the output by printing it and compare with this link

ros_numpy.point_cloud2.array_to_pointcloud2(xyz_array, stamp, frame_id)

Over here, check the variables.

Ranjit gravatar image Ranjit  ( 2021-03-03 02:47:08 -0500 )edit

The gist is that the iteration takes place over an non iterate-able object. I think I am missing to add some parameters which I would be happy to learn from others if they find it correctly.

Vignesh_93 gravatar image Vignesh_93  ( 2021-03-05 03:38:17 -0500 )edit

Hello, can you please! describe what hardware you are using over here?

Ranjit gravatar image Ranjit  ( 2021-03-05 09:53:26 -0500 )edit

I am getting the scan topics from rplidar which is attached to a bot and I run the ros master on a ubuntu 20.04 pc (which does the conversion of scan to pcd , filtering and some computations and then converting back to point cloud data). Not sure why hardware comes into pplay over here.

Vignesh_93 gravatar image Vignesh_93  ( 2021-03-05 10:16:43 -0500 )edit

I have done integration with 3d camera and not explored lidar. Sorry bro

Ranjit gravatar image Ranjit  ( 2021-03-05 10:31:32 -0500 )edit

Yeah No problem. I am able to convert the laser scan to point cloud data but stuck up in reversing

Vignesh_93 gravatar image Vignesh_93  ( 2021-03-06 05:41:53 -0500 )edit

1 Answer

Sort by ยป oldest newest most voted

answered 2021-03-06 09:22:28 -0500

flynneva gravatar image

updated 2021-03-06 09:23:27 -0500

hi @Vignesh_93,

With the PointCloud2 message type you need to tell it what fields you want within it. It was written in a way so that PointCloud2 could be used for many different types of pointclouds (xyz, xyzi, xyzrgb, etc.).

I just recently contributed an example on how to publish a pointcloud2 message. You should be able to use that example to set the fields correctly for your message type (x, y, z I think).

Once you identify your fields, you should then use the create_cloud function rather than the one you are using above.

Hope this helps.

edit flag offensive delete link more



@flynneva Thank you very much for the help. I am able to publish and visualize it in rviz

Vignesh_93 gravatar image Vignesh_93  ( 2021-03-06 15:42:20 -0500 )edit

@Vignesh_93 no problem! if you could "up-vote" the answer to mark it as "answered" that would be great

flynneva gravatar image flynneva  ( 2021-03-10 13:01:58 -0500 )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



Asked: 2021-03-02 02:15:02 -0500

Seen: 168 times

Last updated: Mar 06