Time issue when publishing laser scan message
I am publishing a laser message. However, I got confused whether I should use scan_time = ros::Time::now(); like in the tutorial or should I use the time read from a file. Is there any pitfall in order to control time later on, for both ways. Which way give me a better choice where time control is very important. Please also check the way I code and publish the laser scan message. I feel like something is wrong somewhere.
The code snippet is as below:
void PointCloud::publishLaser()
{
int num_laser_readings;
double laser_frequency;
num_laser_readings = 1080;
laser_frequency = 40; //20 could be
ros::Rate r(1.0);
int t = 0;
while (n.ok() && (t < num_row))
{
sensor_msgs::LaserScan scan;
//data_set[t][1]/1000) is time from dataset, reading row by row
ros::Time scan_time( double(data_set[t][1]/1000));
for (int i = 0; i < num_row; i++)
{
scan.header.stamp = scan_time;
}
scan.header.frame_id = "laser_frame";
scan.angle_min = - 270 / 2 * M_PI / 180; // -xx degree
scan.angle_max = 270 / 2 * M_PI / 180; // xx degree
scan.angle_increment = (scan.angle_max-scan.angle_min)/num_laser_readings;
scan.time_increment = (1 / laser_frequency) / (num_laser_readings);
scan.range_min = 0.0;
scan.range_max = 100.0;
scan.set_ranges_size(num_laser_readings);
for(int i = 0; i < num_laser_readings; ++i)
{
// Laser scanner data reside in column 10th to 1080th
scan.ranges[i] = data_set[t][i+9] / 1000.0;
}
scan_pub.publish(scan);
++count;
++t;
r.sleep();
}
}