ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question
0

How to read and publish pointcloud2.pointfield[].name ?

asked 2019-10-28 05:05:38 -0500

7929one gravatar image

updated 2022-07-14 14:50:52 -0500

lucasw gravatar image

Hi all,

[Linux Distribution: Ubuntu 18.04, ROS Distribution: ROS Melodic, 3D Camera: ifm 03D303]

Full disclaimer: I am new to ROS.

I would like to display the string data of name from the pointfield array. Any hints or direction to how would i go about doing this?

Appreciate the help.

#include <ros/ros.h>

// PCL specific includes
#include <sensor_msgs/PointCloud2.h>
#include <pcl_conversions/pcl_conversions.h>
#include <pcl/point_cloud.h>
#include <pcl/point_types.h>

#include <sensor_msgs/PointCloud.h>
#include <sensor_msgs/point_cloud_conversion.h>

ros::Publisher pub;

void
cloud_cb (const sensor_msgs::PointCloud2ConstPtr& input)
{
  // Create a container for the data.
  sensor_msgs::PointCloud2 output;
  output = *input;

  for(int i = 0; i < output.fields.size(); ++i) {
   geometry_msgs::Point32 point
   double arr[i];
   arr[i] = output.point[i];
   ROS_INFO("[%s]", arr.c_str());
  }

  pub.publish (output);
}

int
main (int argc, char** argv)
{
  // Initialize ROS
  ros::init (argc, argv, "test_node");
  ros::NodeHandle nh;

  // Create a ROS subscriber for the input point cloud
  ros::Subscriber sub = nh.subscribe<sensor_msgs::PointCloud2> ("/ifm3d/camera/cloud", 1, cloud_cb);

  // Create a ROS publisher for the output point cloud
  pub = nh.advertise<sensor_msgs::PointCloud2> ("output", 1);

  // Spin
  ros::spin ();
}
edit retag flag offensive close merge delete

Comments

You can get name of field from output.fields.name and publish it as std_msgs/String

Choco93 gravatar image Choco93  ( 2019-10-28 08:09:03 -0500 )edit

Appreciate your time, @Choco93.

I have edited my code to read output.fields.name but it mentioned has no member named ‘name’. Any lights on this?

void cloud_cb (const sensor_msgs::PointCloud2ConstPtr& input)
{
  // Create a container for the data.
  sensor_msgs::PointCloud2 output;
  output = *input;

  // Publishing the camera frame id
  // ROS_INFO("[%s]", output.header.frame_id.c_str());

  for(int i = 0; i < output.fields.size(); ++i) {
    ROS_INFO("[%s]", output.fields.name.c_str());
  }

  pub.publish (output);
}
7929one gravatar image 7929one  ( 2019-10-28 20:09:33 -0500 )edit

1 Answer

Sort by » oldest newest most voted
0

answered 2019-10-28 20:24:08 -0500

7929one gravatar image

updated 2019-10-28 20:24:47 -0500

Alright, I have figured it out! :)

void cloud_cb (const sensor_msgs::PointCloud2ConstPtr& input)
{
  // Create a container for the data.
  sensor_msgs::PointCloud2 output;
  output = *input;

  // Publishing the camera frame id
  ROS_INFO("Frame ID is: [%s]", output.header.frame_id.c_str());

  // Publishing the name string in PointField array 
  for(int i = 0; i < output.fields.size(); ++i) {
    ROS_INFO("PointField array consists of: [%s]", output.fields[i].name.c_str());
  }

  pub.publish (output);
}
edit flag offensive delete link more

Comments

1

You are just printing the names of fields (not actually publishing them). And you don't have to publish the same pointcloud again. And also if you just want to read fields once there is no need for callback, you can use waitForMessage which will only get the first message it receives instead of a continuous callback. Take a look here

Choco93 gravatar image Choco93  ( 2019-10-29 03:16:58 -0500 )edit

Question Tools

1 follower

Stats

Asked: 2019-10-28 05:05:38 -0500

Seen: 1,419 times

Last updated: Oct 28 '19