message_filters


message_filters是一个关于消息过滤器的包,从ROS subscription或者其他filter接收消息,按照指定的策略输出消息。

message_filters::SimpleFilter< M >

定义接口的过滤器基类

Connection message_filters::SimpleFilter< M >::registerCallback( callback函数指针或函数对象 )

message_filters::Subscriber< M >

ROS1

API手册

是对ROS订阅者的封装,为其他过滤器提供输入,其无法将另一个过滤器的输出作为其输入,而是使用ROS topic作为其输入。

//构造函数
template<class M>//消息类型 M
Subscriber (ros::NodeHandle &nh, const std::string &topic, uint32_t queue_size, const ros::TransportHints &transport_hints=ros::TransportHints(), ros::CallbackQueueInterface *callback_queue=0);
//实现同样效果的subscribe函数
template<class M>
void message_filters::Subscriber< M >::subscribe(ros::NodeHandle &nh, const std::string &topic, uint32_t queue_size, const ros::TransportHints &transport_hints = ros::TransportHints(), ros::CallbackQueueInterface *callback_queue = 0 ) 	

典型应用:

message_filters::Subscriber<std_msgs::UInt32> sub(nh,"my_topic",5);
sub.registerCallback(myCallback);

message_filters::TimeSynchronizer时间同步器

ROS1

TimeSynchronizer过滤器通过包含在header中的时间戳来同步输入通道,以但个回调的形式输出。C++实现最多可以同步9个通道。

  message_filters::Subscriber<sensor_msgs::Image> image_sub(nh, "image", 1);
  message_filters::Subscriber<sensor_msgs::CameraInfo> info_sub(nh, "camera_info", 1);
  TimeSynchronizer<sensor_msgs::Image, sensor_msgs::CameraInfo> sync(image_sub, info_sub, 10);
  sync.registerCallback(boost::bind(&callback, _1, _2));

Policy-Based Synchronizer

Synchronizer filter以一个决定如何同步各输入通道消息的policy为模板参数。官方有两种现有的policy:ExactTimeApproximateTime

ExactTime只有当消息的时间戳完全一样时才可以。

ApproximateTime Policy

源码

message_filters::sync_policies::ApproximateTime policy 使用自适应算法根据时间戳匹配消息。

ApproximateTime(uint32_t queue_size)
   : parent_(0)
   , queue_size_(queue_size)//存放的消息队列的长度
   , enable_reset_(false)
   , num_reset_deques_(0)
   , num_non_empty_deques_(0)
   , pivot_(NO_PIVOT)
   , max_interval_duration_(ros::DURATION_MAX)
   , age_penalty_(0.1)
   , has_dropped_messages_(9, false)
   , inter_message_lower_bounds_(9, ros::Duration(0))
   , warned_about_incorrect_bound_(9, false)
   , last_stamps_(9, ros::Time(0, 0)){}
void setAgePenalty(double age_penalty);//设置age_penalty_参数
void setInterMessageLowerBound(int i, ros::Duration lower_bound) {//单独为某个消息源设置inter_message_lower_bounds_参数
    inter_message_lower_bounds_[i] = lower_bound;
}
void setInterMessageLowerBound(ros::Duration lower_bound) {//为所有消息源设置相同的inter_message_lower_bounds_参数
    for (size_t i = 0; i < inter_message_lower_bounds_.size(); i++)
    {
        inter_message_lower_bounds_[i] = lower_bound;
    }
}
void setMaxIntervalDuration(ros::Duration max_interval_duration);//设置max_interval_duration_参数

可选参数:

  • Age penalty: 当比较后面的时间间隔会乘上一个惩罚因子(1+AgePenalty),一个非零的Age penalty会更早的输出匹配消息集,或输出更多的消息集,以牺牲质量为代价
  • inter message lower bound: 如果特定的消息两帧之间不能比已知的间隔更接近,给定这个间隔下界会帮助算法更早的得出一个给定消息集是最优匹配,从而减小延迟。不正确的下界将导致选择次优集合。一个典型的bound:相机帧率的一半。
  • Max interval duration: 如果候选匹配消息集合的时间间隔大于此值则不考虑。丢弃最早的消息,转向下一个消息。

典型应用:

#include <message_filters/subscriber.h>
#include <message_filters/synchronizer.h>
#include <message_filters/sync_policies/approximate_time.h>
using namespace sensor_msgs;
void callback(const ImageConstPtr& image1, const ImageConstPtr& image2 ){}
int main(){
    message_filters::Subscriber<Image> image1_sub(nh, "image1", 1);
    message_filters::Subscriber<Image> image2_sub(nh, "image2", 1);
    typedef message_filters::sync_policies::ApproximateTime<Imamge, Image> SyncPolicy; 
    SyncPolicy mySyncPolicy(10);
    mySyncPolicy.setAgePenalty(0.1);
    mySyncPolicy.setInterMessageLowerBound( 0, ros::Duration(0.05) );
    mySyncPolicy.setInterMessageLowerBound( 0, ros::Duration(0.1) );
    mySyncPolicy.setMaxIntervalDuration( ros::Duration(0.2) )
    message_filters::Synchronizer<SyncPolicy> sync(mySyncPolicy, image1_sub, image2_sub);
    sync.registerCallback(boost::bind(&callback,_1,_2));    
}

tf2_ros::MessageFilter

API手册

message_filters::SimpleFilter的tf2实现,用于对Stamped类型消息进行过滤,只有当有对应Stamp时刻的tf变换时才发出消息。

//构造函数
MessageFilter(tf2::BufferCore& bc, const std::string& target_frame, uint32_t queue_size, const ros::NodeHandle& nh);
template<class F>//message_filter类型
MessageFilter(F& f, tf2::BufferCore& bc, const std::string& target_frame, uint32_t queue_size, const ros::NodeHandle& nh)
: bc_(bc), queue_size_(queue_size), callback_queue_(nh.getCallbackQueue()) {
   init();
   setTargetFrame(target_frame);
   connectInput(f);
}
MessageFilter(tf2::BufferCore& bc, const std::string& target_frame, uint32_t queue_size, ros::CallbackQueueInterface* cbqueue);
template<class F>
MessageFilter(F& f, tf2::BufferCore& bc, const std::string& target_frame, uint32_t queue_size, ros::CallbackQueueInterface* cbqueue);
virtual void setQueueSize (uint32_t new_queue_size);//设置队列长度
void setTargetFrame (const std::string &target_frame);//设置目标坐标系
void setTolerance (const ros::Duration &tolerance);//设置时间容许误差

常用用法

#include <message_filters/subscriber.h>
#include <tf2_ros/message_filter.h>
message_filters::Subscriber< M > sub(nodehandle,topic,queue_size);
tf2_ros::MessageFilter< M > filter(sub, tf_buffer, target_frame, queue_size, nodehandle);
filter.registerCallback(callback函数);
  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
message_filters.Subscriber is a ROS class that provides a convenient way to subscribe to one or more topics and synchronize the incoming messages based on their timestamps. This class is used in conjunction with the message_filters.TimeSynchronizer or message_filters.ApproximateTimeSynchronizer classes to perform message synchronization. The message_filters.Subscriber class is similar to the rospy.Subscriber class, but it provides additional functionality for message synchronization. It takes the same arguments as the rospy.Subscriber constructor, but it also allows you to specify a queue size for the incoming messages and a callback function that will be called when a new message arrives. Here is an example of how to use message_filters.Subscriber to subscribe to two topics and synchronize the incoming messages: ``` import rospy from sensor_msgs.msg import Image from message_filters import TimeSynchronizer, Subscriber def callback(image_msg, depth_msg): # do something with the synchronized messages pass rospy.init_node('my_node') image_sub = Subscriber('/image_topic', Image) depth_sub = Subscriber('/depth_topic', Image) ts = TimeSynchronizer([image_sub, depth_sub], queue_size=10) ts.registerCallback(callback) rospy.spin() ``` In this example, we create two message_filters.Subscriber objects for the '/image_topic' and '/depth_topic' topics. We then create a TimeSynchronizer object that takes these two subscribers as input and a queue size of 10. We also register a callback function that will be called whenever a new synchronized message is received. The rospy.spin() function is used to keep the node running and to process incoming messages.

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Shilong Wang

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值