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

PCD visualization in Rviz [closed]

asked 2017-10-14 23:26:13 -0500

MT gravatar image

updated 2017-10-15 17:06:44 -0500

Hi Community!

I'm trying to import and manipulate a PCD file in C++ code then visualize the results in Rviz. I feel like this is an easy problem to solve with a couple lines of code but I can't seem to figure them out. I have successfully connected Rviz to my Kinect and Lidar devices and then use PCL etc. to manipulate the live point messages. But I can't seem to import anything and visualize successfully. Any suggestions??

EDIT: Figured it out! heres the code. it might be a bit inefficient but it works.

# include <iostream>
# include <string>

#include <ros/ros.h>

#include <pcl_ros/point_cloud.h>
#include <pcl/point_types.h>
#include <pcl_conversions/pcl_conversions.h>
#include <pcl_ros/io/pcd_io.h>

typedef pcl::PointCloud<pcl::PointXYZ> PointCloud;
using namespace std;

int main(int argc, char** argv)
{
  double x_cloud; double y_cloud; double z_cloud;
  pcl::PointCloud<pcl::PointXYZ>::Ptr cloud (new pcl::PointCloud<pcl::PointXYZ>);
  ros::init (argc, argv, "my_pcl_tutorial");
  ros::NodeHandle nh;
  ros::Publisher pub = nh.advertise<PointCloud> ("points2", 1);
  pcl::io::loadPCDFile<pcl::PointXYZ> ("yourfile.pcd", *cloud);

  PointCloud::Ptr msg (new PointCloud);
  msg->header.frame_id = "frame1";
  msg->height = cloud->height;
  msg->width = cloud->width;

  for (size_t i = 0; i < cloud->size (); i++) {
  x_cloud = cloud->points[i].x;
  y_cloud = cloud->points[i].y;
  z_cloud = cloud->points[i].z;
  msg->points.push_back (pcl::PointXYZ (x_cloud, y_cloud, z_cloud));
  }

  ros::Rate loop_rate(4);


  while (nh.ok())
  {
    //msg->header.stamp = ros::Time::now().toNSec();
    pub.publish (msg);
    ros::spinOnce ();
    loop_rate.sleep ();
  }
}
edit retag flag offensive close merge delete

Comments

If you feel your code is the correct answer to your question (but please take a look at pcd_to_pointcloud), please post it as an answer yourself and accept your own answer.

gvdhoorn gravatar image gvdhoorn  ( 2017-10-16 05:13:10 -0500 )edit

The goal was to perform PCL tutorials without the kinect but the PCD files given. This was the fist step of many.

MT gravatar image MT  ( 2017-10-16 05:58:34 -0500 )edit

2 Answers

Sort by ยป oldest newest most voted
0

answered 2017-10-16 06:04:08 -0500

MT gravatar image

Answer 1:

# include <iostream>
# include <string>

#include <ros/ros.h>

#include <pcl_ros/point_cloud.h>
#include <pcl/point_types.h>
#include <pcl_conversions/pcl_conversions.h>
#include <pcl_ros/io/pcd_io.h>

typedef pcl::PointCloud<pcl::PointXYZ> PointCloud;
using namespace std;

int main(int argc, char** argv)
{
  double x_cloud; double y_cloud; double z_cloud;
  pcl::PointCloud<pcl::PointXYZ>::Ptr cloud (new pcl::PointCloud<pcl::PointXYZ>);
  ros::init (argc, argv, "my_pcl_tutorial");
  ros::NodeHandle nh;
  ros::Publisher pub = nh.advertise<PointCloud> ("points2", 1);
  pcl::io::loadPCDFile<pcl::PointXYZ> ("yourfile.pcd", *cloud);

  PointCloud::Ptr msg (new PointCloud);
  msg->header.frame_id = "frame1";
  msg->height = cloud->height;
  msg->width = cloud->width;

  for (size_t i = 0; i < cloud->size (); i++) {
  x_cloud = cloud->points[i].x;
  y_cloud = cloud->points[i].y;
  z_cloud = cloud->points[i].z;
  msg->points.push_back (pcl::PointXYZ (x_cloud, y_cloud, z_cloud));
  }

  ros::Rate loop_rate(4);


  while (nh.ok())
  {
    //msg->header.stamp = ros::Time::now().toNSec();
    pub.publish (msg);
    ros::spinOnce ();
    loop_rate.sleep ();
  }
}

Answer 2: pcd_to_pointcloud

edit flag offensive delete link more

Comments

Thank you for the code , It worked for me

Ajay gravatar image Ajay  ( 2018-05-12 02:13:53 -0500 )edit
1

answered 2017-10-15 04:58:03 -0500

gvdhoorn gravatar image

updated 2017-10-16 05:12:08 -0500

But I can't seem to import anything and visualize successfully.

RViz visualises data streams, not files, so in order for you to be able to have it visualise the output of your pointcloud processing, you'll have to publish those clouds on a topic.


Edit (after posting your own code): if all you wanted was to publish a .pcd file (instead of the results of your 'manipulation' directly (ie: without going through the FS)), then the pcd_to_pointcloud node in the pcl_ros package would probably have been better.

edit flag offensive delete link more

Comments

And a bit of a nitpick: you can't really 'import' anything into RViz. It's not a visualiser like the pcd_viewer from PCL.

gvdhoorn gravatar image gvdhoorn  ( 2017-10-15 04:58:10 -0500 )edit

What exactly is FS? Just for future reference.

MT gravatar image MT  ( 2017-10-16 05:59:46 -0500 )edit

The file system.

gvdhoorn gravatar image gvdhoorn  ( 2017-10-16 06:00:35 -0500 )edit

Question Tools

Stats

Asked: 2017-10-14 23:26:13 -0500

Seen: 5,170 times

Last updated: Oct 16 '17