ROS 可视化(一): 发布PointCloud2点云数据到Rviz
1. 相关依赖
package.xml 需要添加对 pcl_ros 包的依赖
2. CMakeLists.txt
find_package(PCL REQUIRED) include_directories(include${PCL_INCLUDE_DIRS}) link_directories(${PCL_LIBRARY_DIRS}) add_executable(pcl_create src/pcl_create.cpp) target_link_libraries(pcl_create ${catkin_LIBRARIES} ${PCL_LIBRARIES})
2. 测试代码
#include <ros/ros.h> #include <pcl/point_cloud.h> #include <pcl_conversions/pcl_conversions.h> #include <sensor_msgs/PointCloud2.h> main (int argc, char **argv) { ros::init (argc, argv, "pcl_create"); ros::NodeHandle nh; ros::Publisher pcl_pub = nh.advertise<sensor_msgs::PointCloud2> ("pcl_output", 1); pcl::PointCloud<pcl::PointXYZ> cloud; sensor_msgs::PointCloud2 output; // Fill in the cloud data cloud.width = 100; cloud.height = 1; cloud.points.resize(cloud.width * cloud.height); for (size_t i = 0; i < cloud.points.size (); ++i) { cloud.points[i].x = 1024 * rand () / (RAND_MAX + 1.0f); cloud.points[i].y = 1024 * rand () / (RAND_MAX + 1.0f); cloud.points[i].z = 1024 * rand () / (RAND_MAX + 1.0f); } //Convert the cloud to ROS message pcl::toROSMsg(cloud, output); output.header.frame_id = "odom"; ros::Rate loop_rate(1); while (ros::ok()) { pcl_pub.publish(output); ros::spinOnce(); loop_rate.sleep(); } return 0; }
作者:白菜菜白
出处:http://www.cnblogs.com/lvchaoshun/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。