message_filters 对齐多种传感器数据的时间戳

https://www.cnblogs.com/gdut-gordon/p/10293446.html

联合标定三维雷达和IMU,第一步要先对齐两种传感信息的时间戳。

ros官网提供了message_filters用于对齐多种传感信息的时间戳。

http://wiki.ros.org/message_filters#Time_Synchronizer

需要提示一点,对齐时间戳有两种方式,一种是时间戳完全对齐 ExactTime Policy ,另一种是时间戳相近 

ApproximateTime Policy ,前者更为严格。

本人选用时间戳相近的对齐方法,代码如下:

复制代码

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 

using namespace std;
using namespace sensor_msgs;
using namespace message_filters;

  PointCloud2 syn_pointcloud;

  Imu syn_imu;

void callback(const PointCloud2ConstPtr& ori_pointcloud, const ImuConstPtr& ori_imu)
{
  // Solve all of perception here...
  syn_pointcloud = *ori_pointcloud;
  syn_imu = *ori_imu;
  cout << "syn velodyne points' timestamp : " << syn_pointcloud.header.stamp << endl;
  cout << "syn Imu's timestamp : " << syn_imu.header.stamp << endl;
  ROS_INFO("pointcloud stamp value is: %d", syn_pointcloud.header.stamp);
  ROS_INFO("imu stamp value is: %d", syn_imu.header.stamp);
}


int main(int argc, char** argv)
{
  ros::init(argc, argv, "msg_synchronizer");
  ros::NodeHandle nh;

  message_filters::Subscriber imu_sub(nh, "/imu/data", 1);
  message_filters::Subscriber velodyne_sub(nh, "/velodyne_points", 1);
  typedef sync_policies::ApproximateTime MySyncPolicy;    
  Synchronizer sync(MySyncPolicy(10), velodyne_sub, imu_sub); //queue size=10
  sync.registerCallback(boost::bind(&callback, _1, _2));

  ros::spin();
  return 0;
}

复制代码

callback函数订阅的所有/topic必须有数据存在,否则调用失败。

你可能感兴趣的:(Ros,学习)