在ROS中订阅点云消息,生成PCD文件

消息类型

ROS中点云的消息类型:

  sensor_msgs::PointCloud2

PCL中的点云消息类型:

  pcl::PointCloud    T是指pcl::PointXYZ等point types.

步骤:

1.rosbag play 包名

2.订阅消息,进入回调函数:

  将订阅到的消息转化为pcl的类型,用pcl::fromROSMsg (*input, cloud).input是回调函数参数,是接收.cloud是输出.

  将点云转化为PCD文件,用pcl::io::savePCDFileASCII(name,input).生成的pcd文件在bag包所在路径下.

贴上我的源码:

#include 
#include 
//msgs type and conversion
#include 
#include 
//pcd io
#include 
//point types
#include 

int i=0;
char ch=(char)(int('0'));
void 
call_back(const sensor_msgs::PointCloud2ConstPtr& input)
{
//Convert the sensor_msgs/PointCloud2 data to pcl/PointCloud
    pcl::PointCloud cloud;
    pcl::fromROSMsg (*input, cloud);//cloud is the output
    
//save to PCD for 5 times    
    std::string pcd_name("pcl_2_pcd");
    if(i<5)
    {
    i++;
    ch++;
    pcd_name+=ch;
    if(pcl::io::savePCDFileASCII (pcd_name+".pcd", cloud)>=0)//input pointcloud should be pcl::PointCloud only,rather than others 
    {std::cerr << "Saved  " << pcd_name<<".pcd"<< std::endl;}
    
 
    }
}

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

// Create a ROS subscriber for the input point cloud
  ros::Subscriber sub = nh.subscribe ("velodyne_points", 1, call_back);

// Spin
  ros::spin ();
}



你可能感兴趣的:(在ROS中订阅点云消息,生成PCD文件)