We can save the point cloud to a PCD (Point Cloud Data) file by using the following code. The file name is pcl_write.cpp inside the src folder.
#include <ros/ros.h> #include <pcl/point_cloud.h> #include <pcl_conversions/pcl_conversions.h> #include <sensor_msgs/PointCloud2.h> //Header file for writing PCD file #include <pcl/io/pcd_io.h> void cloudCB(const sensor_msgs::PointCloud2 &input) { pcl::PointCloud<pcl::PointXYZ> cloud; pcl::fromROSMsg(input, cloud); //Save data as test.pcd file pcl::io::savePCDFileASCII ("test.pcd", cloud); } main (int argc, char **argv) { ros::init (argc, argv, "pcl_write"); ROS_INFO("Started PCL write node"); ros::NodeHandle nh; ros::Subscriber bat_sub = nh.subscribe("pcl_output", 10, cloudCB); ros::spin(); return 0; }