Robotics StackExchange | Archived questions

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

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 ();
}

Asked by 7929one on 2019-10-28 05:05:38 UTC

Comments

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

Asked by Choco93 on 2019-10-28 08:09:03 UTC

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);
}

Asked by 7929one on 2019-10-28 20:09:33 UTC

Answers

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);
}

Asked by 7929one on 2019-10-28 20:24:08 UTC

Comments

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

Asked by Choco93 on 2019-10-29 03:16:58 UTC