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

[build error]Converting pcl::PCLPointCloud2 to sensor_msg::PointCloud2

asked 2015-03-04 12:51:35 -0500

wkentaro gravatar image

While extracting point cloud from stl model data to publish it using ROS topic, I need to convert pcl::PCLPointCloud2 to sensor_msg::PointCloud2. So I did like below::

#include <pcl/io/vtk_lib_io.h>
#include <pcl/PolygonMesh.h>
#include <pcl/PCLPointCloud2.h>
#include <pcl_conversions/pcl_conversions.h>
#include "sensor_msgs/PointCloud2.h"

int main(int argc, char* argv[])
    const std::string meshFileName = "test.stl";

    pcl::PolygonMesh testMesh;
    pcl::io::loadPolygonFile(meshFileName, testMesh);

    pcl::PCLPointCloud2 pc2;
    sensor_msgs::PointCloud2 p_msg;

    pcl_conversions::fromPCL(pc2, p_msg);

But I got error below. How can I fix it? Or is there another way to convert PCLPointCloud2 to ROSMsg?

Scanning dependencies of target stl2pcd
[ 50%] Built target pcd_read
[100%] Building CXX object try_pkg/CMakeFiles/stl2pcd.dir/src/stl2pcd.cpp.o
Linking CXX executable /home/wkentaro/semi/devel/lib/try_pkg/stl2pcd
CMakeFiles/stl2pcd.dir/src/stl2pcd.cpp.o: In function pcl_conversions::fromPCL(unsigned long const&, ros::Time&)':
stl2pcd.cpp:(.text._ZN15pcl_conversions7fromPCLERKmRN3ros4TimeE[_ZN15pcl_conversions7fromPCLERKmRN3ros4TimeE]+0x7e): undefined reference toros::TimeBase<ros::time, ros::duration="">::fromNSec(unsigned long)'
collect2: error: ld returned 1 exit status
make[2]: * [/home/wkentaro/semi/devel/lib/try_pkg/stl2pcd] Error 1
make[1]:  [try_pkg/CMakeFiles/stl2pcd.dir/all] Error 2
make: ** [all] Error 2
Invoking "make -j4 -l4" failed

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted

answered 2015-05-01 10:51:29 -0500

I use pcl_ros instead of pcl_conversions. It defines a very handy fromROSMsg function:

#include <pcl_ros/transforms.h>

sensor_msgs::PointCloud2 rosCloud;
pcl::PointCloud<pcl::PointXYZRGB>::Ptr pclCloud (new pcl::PointCloud<pcl::PointXYZRGB>); // creates a shared pointer
pcl::fromROSMsg(rosCloud, *pclCloud);

It also converts back:

pcl::toROSMsg(*pclCloud, rosCloud);
edit flag offensive delete link more


Thanks a lot!

wkentaro gravatar image wkentaro  ( 2015-05-02 03:17:20 -0500 )edit

No problem. Please mark the answer as correct if you think it's good.

Adam Allevato gravatar image Adam Allevato  ( 2015-05-02 09:34:35 -0500 )edit

Question Tools

1 follower


Asked: 2015-03-04 12:51:35 -0500

Seen: 2,029 times

Last updated: May 01 '15