ROS

1.Time Synchronizer 时间同步器

2.Policy-Based Synchronizer基于策略的同步器

2.1ExactTime策略

message_filters :: sync_policies :: ExactTime策略要求消息具有完全相同的时间戳以便匹配。 只有在具有相同确切时间戳的所有指定通道上收到消息时,才会调用回调。 从所有消息的头域读取时间戳(这是该策略所必需的)。
C++头文件:message_filters/sync_policies/exact_time.h

  ros::NodeHandle nh;
  message_filters::Subscriber<Image> image_sub(nh, "image", 1);
  message_filters::Subscriber<CameraInfo> info_sub(nh, "camera_info", 1);

  typedef sync_policies::ExactTime<Image, CameraInfo> MySyncPolicy;
  // ExactTime takes a queue size as its constructor argument, hence MySyncPolicy(10)
  Synchronizer<MySyncPolicy> sync(MySyncPolicy(10), image_sub, info_sub);
  sync.registerCallback(boost::bind(&callback, _1, _2));

2.2ApproximateTime 策略

message_filters :: sync_policies :: ApproximateTime策略使用自适应算法来匹配基于其时间戳的消息。
如果不是所有的消息都有一个标题字段,从中可以确定时间戳,请参见下面的解决方法。
C++头文件:message_filters/sync_policies/approximate_time.h

    ros::NodeHandle nh;

    message_filters::Subscriber<sensor_msgs::Image> left_sub(nh, "/camera/left/image_raw", 1);
    message_filters::Subscriber<sensor_msgs::Image> right_sub(nh, "/camera/right/image_raw", 1);
    typedef message_filters::sync_policies::ApproximateTime<sensor_msgs::Image, sensor_msgs::Image> sync_pol;
// ApproximateTime takes a queue size as its constructor argument, hencesync_po(10)
message_filters::Synchronizer<sync_pol> sync(sync_pol(10), left_sub,right_sub);
//(&igb)->ImageGrabber::GrabStereo(left_sub,right_sub)
sync.registerCallback(boost::bind(&ImageGrabber::GrabStereo,&igb,_1,_2));

如果某些消息的类型不包含头字段,则ApproximateTimeSynchronizer默认拒绝添加此类消息。 但是,它的Python版本可以用allow_headerless = True来构造,它使用当前的ROS时间代替任何缺少的header.stamp字段:

from std_msgs.msg import Int32, Float32

def callback(mode, penalty):
  # The callback processing the pairs of numbers that arrived at approximately the same time

mode_sub = message_filters.Subscriber('mode', Int32)
penalty_sub = message_filters.Subscriber('penalty', Float32)

ts = message_filters.ApproximateTimeSynchronizer([mode_sub, penalty_sub], 10, 0.1, allow_headerless=True)
ts.registerCallback(callback)
原文地址:https://www.cnblogs.com/hitzzq/p/14663199.html