参考:pcd文件在rviz中显示
功能包创建
cd Downloads/ROS
mkdir -p pcdreadshow_ws/src
cd src
catkin_init_workspace
catkin_create_pkg read_pcd pcl_conversions pcl_ros roscpp sensor_msgs
读取点云文件并发布
#include<ros/ros.h>
#include<pcl/point_cloud.h>
#include<pcl_conversions/pcl_conversions.h>
#include<sensor_msgs/PointCloud2.h>
#include<pcl/io/pcd_io.h>int main(int argc,char **argv){ros::init(argc,argv,"pcd_pub");ros::NodeHandle nh;ros::Publisher pcl_pub=nh.advertise<sensor_msgs::PointCloud2> ("pcl_output",1);pcl::PointCloud<pcl::PointXYZ> cloud;sensor_msgs::PointCloud2 output;std::string file_path;nh.param<std::string>("file_path", file_path, "/home/wyh/Downloads/ROS/pcdreadshow_ws/src/read_pcd/src/data/pcd/data_1/0000000001.pcd")pcl::io::loadPCDFile(file_path,cloud);//通过launch文件修改路径即可pcl::toROSMsg(cloud,output);output.header.frame_id="map";ros::Rate loop_rate(1);while (ros::ok()){pcl_pub.publish(output);ros::spinOnce();loop_rate.sleep();}return 0;
}
launch文件
<launch>
<param name="file_path" value="/home/wyh/Downloads/ROS/pcdreadshow_ws/src/read_pcd/src/data/pcd/data_1/0000000001.pcd">
<node type="read_pcd" value="read_pcd" pkg="read_pcd">
</node>
</launch>
编译CmakeLists.txt
add_executable(read_pcd src/read_pcd.cpp)
target_link_libraries(read_pcd ${catkin_LIBRARIES})
即可实现pcd文件的点云可视化。
这个是点云到栅格地图之后的效果