第一个PCL程序

1.创建ROS软件包

在工作空间创建一个ROS软件包,这个软件包依赖于pcl_conversions、pcl_ros、pcl_msgs和sensor_msgs包:

$ catkin_create_pkg chapter6_tutorials pcl_conversions pcl_ros pcl_msgs sensor_msgs

在软件包中创建一个源文件目录:

$ rospack profile
$ roscd chapter6_tutorials
$ mkdir src

2.创建源文件

创建一个名为pcl_sample.cpp的文件,功能是创建一个ROS节点并且发布一个带有100个点的点云,代码如下:

#include <ros/ros.h>
#include <pcl/point_cloud.h>
#include <pcl_ros/point_cloud.h>
#include <pcl_conversions/pcl_conversions.h>
#include <sensor_msgs/PointCloud2.h>

main(int argc, char** argv)
{
	ros::init (argc, argv, "pcl_sample");
	ros::NodeHandle nh;
	ros::Publisher pcl_pub = nh.advertise<sensor_msgs::PointCloud2>("pcl_output", 1);
	
	sensor_msgs::PointCloud2 output;
	pcl::PointCloud<pcl_PointCloudXYZ>::Ptr cloud (new pcl::PointCloud<pcl_PointCloudXYZ>);

	//Fill in the cloud data
	cloud->width  = 100;
	cloud->height = 1;
	cloud->points.resize(cloud->width * cloud->height);
	
	//Convert the cloud to ROS message
	pcl::toROSMsg(*cloud, output);
	
	pcl_pub.publish(output);
	ros::spinOnce();

	return 0;
}

编写CMakeLists.txt

添加PCL库到CMakeLists.txt,使可执行的ROS节点正确地链接到系统的PCL库。

find_package(PCL REQUIRED)

include_directories(include
	  ${PCL_INCLUDE_DIRS}
)
link_directories(
  ${PCL_LIBRARY_DIRS}
)

添加产生可执行程序和链接到合适的库:

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

编译

以上工作都完成后,可在工作空间的根目录执行以下命令编译:

catkin_make
  • 3
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值