data losted when subscribe from a bag file
this bag file (T_01.bag)is publishing sensor_msgs::PointCloud2 on the topic "/points". i use the following code to subscribe to that topic and save the pointcloud to disk.
#include <ros/ros.h>
#include <pcl_ros/point_cloud.h>
#include <pcl/common/common.h>
#include <pcl/io/pcd_io.h>
int count = 0;
pcl::PCDWriter pcdWriter;
void cloudCallback(const sensor_msgs::PointCloud2ConstPtr& msg)
{
pcl::PointCloud<pcl::PointXYZ>::Ptr pc_col(new pcl::PointCloud<pcl::PointXYZ>());
pcl::fromROSMsg(*msg,*pc_col);
std::cout<<"saving "<<count<<std::endl;
std::stringstream ss;
ss<<"point"<<count<<".pcd";
pcdWriter.write(ss.str(),*pc_col);
count++;
}
int main(int argc, char **argv)
{
ros::init(argc, argv, "listener");
ros::NodeHandle n;
ros::Subscriber sub = n.subscribe("/points", 10, cloudCallback);
ros::spin();
return 0;
}
The problem is, there should be 4 pointclouds in this bag file. but most of the time my program can only record 3. Could anybody figure out why?
p.s. how to make the subscriber quit automatically when the publisher is terminated?
Thanks.