ROS_PCL:接受点云并写入pcd文件

第一个程序是加载PCD数据文件,第二个程序是接收点云数据并写入pcd文档.

1.pcl_load.cpp

[html] view plain copy
  1. #include<ros/ros.h>  
  2. #include<pcl/point_cloud.h>  
  3. #include<pcl_conversions/pcl_conversions.h>  
  4. #include<sensor_msgs/PointCloud2.h>  
  5. #include<pcl/io/pcd_io.h>//which contains the required definitions to load and store point clouds to PCD and other file formats.  
  6.   
  7. main (int argc, char **argv)  
  8. {  
  9.   ros::init (argc, argv, "UandBdetect");  
  10.   ros::NodeHandle nh;  
  11.   ros::Publisher pcl_pub = nh.advertise<sensor_msgs::PointCloud2> ("pcl_output", 1);  
  12.   pcl::PointCloud<pcl::PointXYZ> cloud;  
  13.   sensor_msgs::PointCloud2 output;  
  14.   pcl::io::loadPCDFile ("/home/shuning/catkin_ws/src/imgpcl/data/test_pcd.pcd", cloud);  
  15.   //Convert the cloud to ROS message  
  16.   pcl::toROSMsg(cloud, output);  
  17.   output.header.frame_id = "odom";//this has been done in order to be able to visualize our PointCloud2 message on the RViz visualizer  
  18.   ros::Rate loop_rate(1);  
  19.   while (ros::ok())  
  20.   {  
  21.     pcl_pub.publish(output);  
  22.     ros::spinOnce();  
  23.     loop_rate.sleep();  
  24.   }  
  25.   return 0;  
  26. }  
pcl_write.cpp:
  1. #include<ros/ros.h>  
  2. #include<pcl/point_cloud.h>  
  3. #include<pcl_conversions/pcl_conversions.h>  
  4. #include<sensor_msgs/PointCloud2.h>  
  5. #include<pcl/io/pcd_io.h>  
  6.   
  7. void cloudCB(const sensor_msgs::PointCloud2 &input)  
  8. {  
  9.   pcl::PointCloud<pcl::PointXYZ> cloud;  
  10.   pcl::fromROSMsg(input, cloud);//从ROS类型消息转为PCL类型消息  
  11.   pcl::io::savePCDFileASCII ("/home/shuning/catkin_ws/src/imgpcl/data/write_pcd_test.pcd", cloud);//保存pcd  
  12. }  
  13. main (int argc, char **argv)  
  14. {  
  15.   ros::init (argc, argv, "pcl_write");  
  16.   ros::NodeHandle nh;  
  17.   ros::Subscriber bat_sub = nh.subscribe("pcl_output", 10, cloudCB);//接收点云  
  18.   ros::spin();  
  19.   return 0;  
  20. }  

2.test_pcd.pcd"

部分数据如下

FIELDS x y z intensity distance sid
SIZE 4 4 4 4 4 4
TYPE F F F F F F
COUNT 1 1 1 1 1 1
WIDTH 460400
HEIGHT 1
POINTS 460400
DATA ascii
-0.93387 -0.6825 -1.1865 12 1.485 7
-0.93173 -0.68323 -1.1878 10 1.485 8
-0.92185 -0.68054 -1.1831 10 1.475 10
-0.91748 -0.67961 -1.1815 10 1.471 11
-0.91479 -0.6799 -1.182 12 1.47 12
-0.90031 -0.68289 -1.1872 10 1.467 18
-0.89271 -0.67941 -1.1811 12 1.457 19
-0.87805 -0.67048 -1.1656 8 1.434 20

3.编译

cd catkin_ws

source devel/setup.bash

catkin_make


4.运行

roscore

rosrun imgpcl pcl_load

rosrun imgpcl pcl_write

rosrun rviz rviz

5.Rviz的配置

点击Add,添加pointcloud2,

Fixed  Frame设置为odom.

Topic 设置为/pcl_output


参考Ros机器人程序设计第二版
  • 2
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

通哈膨胀哈哈哈

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值