1、将 topic 发布的的信息打包,保存位置为当前目录的 out.bag
rosbag record -O out /kinect2/qhd/points
2、创建文件夹,将文件保存在文件夹内,将 .bag 数据转换为 .pcd
rosrun pcl_ros bag_to_pcd <input_file.bag> <topic> <output_directory>
rosrun pcl_ros bag_to_pcd out.bag /kinect2/qhd/points ./pcd
3、创建 cpp 文件
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> //which contains the required definitions to load and store point clouds to PCD and other file formats.
main (int argc, char **argv)
{
ros::init (argc, argv, "UandBdetect");
ros::NodeHandle nh;
ros::Publisher pcl_pub = nh.advertise<sensor_msgs::PointCloud2> ("pcl_output1", 1);
pcl::PointCloud<pcl::PointXYZ> cloud;
sensor_msgs::PointCloud2 output;
pcl::io::loadPCDFile ("/home/lucifr/code/voxel_grid/table_scene_lms400.pcd", cloud);
//Convert the cloud to ROS message
pcl::toROSMsg(cloud, output);
output.header.frame_id = "odom1";//this has been done in order to be able to visualize our PointCloud2 message on the RViz visualizer
ros::Rate loop_rate(1);
while (ros::ok())
{
pcl_pub.publish(output);
ros::spinOnce();
loop_rate.sleep();
}
return 0;
}
4、修改CMakeList.txt 和 package.xml文件
<buildtool_depend>catkin</buildtool_depend>
<build_depend>roscpp</build_depend>
<build_depend>rospy</build_depend>
<build_depend>std_msgs</build_depend>
<build_depend>pcl_ros</build_depend>
<build_depend>sensor_msgs</build_depend>
<build_export_depend>roscpp</build_export_depend>
<build_export_depend>rospy</build_export_depend>
<build_export_depend>std_msgs</build_export_depend>
<build_export_depend>pcl_ros</build_export_depend>
<build_export_depend>sensor_msgs</build_export_depend>
<exec_depend>roscpp</exec_depend>
<exec_depend>rospy</exec_depend>
<exec_depend>std_msgs</exec_depend>
<exec_depend>pcl_ros</exec_depend>
<exec_depend>sensor_msgs</exec_depend>
find_package(catkin REQUIRED COMPONENTS
roscpp
rospy
std_msgs
pcl_ros
sensor_msgs
)
add_executable(${PROJECT_NAME}_node src/pcd2rviz1.cpp)
target_link_libraries(${PROJECT_NAME}_node
${catkin_LIBRARIES}
)
5、编译
6、运行节点
rosrun collisiondetection collisiondetection_node