My first PCL program

In this section, you will learn how to integrate PCL and ROS. Knowledge and understanding of how ROS packages are laid out and how to compile are required although the steps will be repeated for simplicity. The example used in this first PCL program has no use whatsoever other than serving as a valid ROS node, which will successfully compile.

The first step is to create the ROS package for this entire chapter in your workspace. This package will depend on the pcl_conversions, pcl_ros, pcl_msgs, and sensor_msgs packages:

    $ catkin_create_pkg chapter10_tutorials pcl_conversions pcl_ros pcl_msgs sensor_msgs
  

The following step is to create the source directory in the package using the following commands:

    $ rospack profile
    $ roscd chapter10_tutorials
    $ mkdir src
  

In this new source directory, you should create a file named pcl_sample.cpp with the following code, which creates a ROS node and publishes a point cloud with 100 elements. Again, what the code does should not really be of any concern to you as it is just for the purpose of having a valid node that uses PCL and compiles without problems:

#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::PointXYZ>::Ptr cloud (new
pcl::PointCloud<pcl::PointXYZ>); // 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; }

The next step is to add PCL libraries to CMakeLists.txt so that the ROS node executable can be properly linked against the system's PCL libraries:

find_package(PCL REQUIRED) 
 
include_directories(include ${PCL_INCLUDE_DIRS}) 
link_directories(${PCL_LIBRARY_DIRS}) 

Finally, the lines to generate the executable and link against the appropriate libraries are added:

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

Once the final step has been reached, the package can be compiled by callingcatkin_make as usual from the workspace root directory.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.221.222.47