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