ROS下使用PCL库教程

最近打算做一点点云数据处理相关的工作,所以就用到PCL(点云库),ubantu已经自己带了PCL库,我用的是uantu20.04.4+Ros noetic+pcl 1.10+vscode。PCL官网提供的教程是单独PCL在cmke编译的,要在ros框架下使用则需要将PCL官方教程与ROS匹配。ROS官方提供教程。以下是在ROS下跑通第一个PCL程序整理:

ROS下使用PCL步骤

1. 创建工作空间

mkdir -p ~/PCL_ws/src
cd ~/PCL_ws
catkin_make
code .#(在vscode中打开)

2. 创建ROS包

打开vscode后直接创建ROS包。
查看vscode中创建ROS包
也可以使用命令行创建

catkin_creat_pkg my_PCL_tutorial PCL_conversions PCL_ros roscpp sensor_msgs

配置package.xml文件

  <build_depend>libpcl-all-dev</build_depend>
  <exec_depend>libpcl-all</exec_depend>

3. 创建代码ROS代码框架

src文件夹下创建cpp文件src/pcl_text.cpp,不会参考上一个链接[查看vscode中创建ROS包]。

   1 #include <ros/ros.h>
   2 // PCL specific includes
   3 #include <sensor_msgs/PointCloud2.h>
   4 #include <pcl_conversions/pcl_conversions.h>
   5 #include <pcl/point_cloud.h>
   6 #include <pcl/point_types.h>
   7 
   8 ros::Publisher pub;
   9 
  10 void 
  11 cloud_cb (const sensor_msgs::PointCloud2ConstPtr& input)
  12 {
  13   // Create a container for the data.
  14   sensor_msgs::PointCloud2 output;
  15 
  16   // Do data processing here...
  17   output = *input;
  18 
  19   // Publish the data.
  20   pub.publish (output);
  21 }
  22 
  23 int
  24 main (int argc, char** argv)
  25 {
  26   // Initialize ROS
  27   ros::init (argc, argv, "my_pcl_tutorial");
  28   ros::NodeHandle nh;
  29 
  30   // Create a ROS subscriber for the input point cloud
  31   ros::Subscriber sub = nh.subscribe ("input", 1, cloud_cb);
  32 
  33   // Create a ROS publisher for the output point cloud
  34   pub = nh.advertise<sensor_msgs::PointCloud2> ("output", 1);
  35 
  36   // Spin
  37   ros::spin ();
  38 }

4. 配置CMakeLists.txt文件

add_executable(example src/example.cpp)
target_link_libraries(example ${catkin_LIBRARIES})

5. 从PCL教程中下载pcl处理代码,放到ROS代码框架中(放在回调函数中)。

   5 void cloud_cb (const sensor_msgs::PointCloud2ConstPtr& cloud_msg)
   7 {
   8   // Container for original & filtered data
   9   pcl::PCLPointCloud2* cloud = new pcl::PCLPointCloud2; 
  10   pcl::PCLPointCloud2ConstPtr cloudPtr(cloud);
  11   pcl::PCLPointCloud2 cloud_filtered;
  12 
  13   // Convert to PCL data type
  14   pcl_conversions::toPCL(*cloud_msg, *cloud);
  15 
  16   // Perform the actual filtering
  17   pcl::VoxelGrid<pcl::PCLPointCloud2> sor;
  18   sor.setInputCloud (cloudPtr);
  19   sor.setLeafSize (0.1, 0.1, 0.1);
  20   sor.filter (cloud_filtered);
  21 
  22   // Convert to ROS data type
  23   sensor_msgs::PointCloud2 output;
  24   pcl_conversions::fromPCL(cloud_filtered, output);
  25 
  26   // Publish the data
  27   pub.publish (output);
  28 }
  • 0
    点赞
  • 24
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值