ROS message_filters

本文深入探讨了ROSmessage_filters库的功能与用法,包括滤波模式、订阅器、时间一致器等核心组件,以及如何在C++和Python环境下进行连接与配置。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

ROS message_filters

参考地址:http://wiki.ros.org/message_filters

1.概述

message_filter是roscpp和rospy的一个应用库,他集中了一些滤波算法中常用的一些消息。当一个消息到来,在之后的一个时间点,该消息可能被返回或者不返回,将这样的一个过程或者容器理解为消息滤波器。

一个典型的例子就是时间同步器,他从多个数据源采集不同类型的数据,只有每个消息源的消息具体有相同的时间戳时候,他才将信息发布出去。

message_filters is a utility library for use with roscpp and rospy. It collects commonly used message "filtering" algorithms into a common space. A message filter is defined as something which a message arrives into and may or may not be spit back out of at a later point in time.

An example is the time synchronizer, which takes in messages of different types from multiple sources, and outputs them only if it has received a message on each of those sources with the same timestamp.

2. 滤波模式

所有的消息滤波器服从相同的模式,用于连接输入和输出。输入通过滤波器的构造器或者connectInput()的方法连接。输出通过registerCallback()方法连接。

需要注意的是,每个滤波器各自定义输入输出的类型,所以并不是所有的滤波器可以直接互联。

For example, given two filters FooFilter and BarFilter where FooFilter's output is compatible with BarFilter's input, connecting foo to bar could be (in C++):

切换行号显示
   1 FooFilter foo;
   2 BarFilter bar(foo);

or:

切换行号显示
   1 FooFilter foo;
   2 BarFilter bar;
   3 bar.connectInput(foo);
2.1 registerCallback()

你可以采用registerCallback()方法,注册多个回调函数。他们将按照注册顺序进行函数的回调。

3 订阅器Subscriber

The  Subscriber  filter is simply a wrapper around a ROS subscription that provides a source for other filters. The Subscriber  filter cannot connect to another filter's output, instead it uses a ROS topic as its input.

3.1 连接

输入:没有输入连接

输出:void callback(const boost::shared_ptr<M const>&)

例子:

切换行号显示
   1 message_filters::Subscriber<std_msgs::UInt32> sub(nh, "my_topic", 1);
   2 sub.registerCallback(myCallback);

is the equivalent of:

切换行号显示
   1 ros::Subscriber sub = nh.subscribe("my_topic", 1, myCallback);

4. 时间一致器

The TimeSynchronizer filter synchronizes incoming channels by the timestamps contained in their headers, and outputs them in the form of a single callback that takes the same number of channels. The C++ implementation can synchronize up to 9 channels.

Connections

Input

  • C++: Up to 9 separate filters, each of which is of the form void callback(const boost::shared_ptr<M const>&). The number of filters supported is determined by the number of template arguments the class was created with.

    Python: N separate filters, each of which has signature callback(msg).

Output

  • C++: For message types M0..M8,void callback(const boost::shared_ptr<M0 const>&, ..., const boost::shared_ptr<M8 const>&). The number of parameters is determined by the number of template arguments the class was created with.

    Pythoncallback(msg0.. msgN). The number of parameters is determined by the number of template arguments the class was created with.

Example (C++)

Suppose you are writing a ROS node that needs to process data from two time synchronized topics. Your program will probably look something like this:

切换行号显示
   1 #include <message_filters/subscriber.h>
   2 #include <message_filters/time_synchronizer.h>
   3 #include <sensor_msgs/Image.h>
   4 #include <sensor_msgs/CameraInfo.h>
   5 
   6 using namespace sensor_msgs;
   7 using namespace message_filters;
   8 
   9 void callback(const ImageConstPtr& image, const CameraInfoConstPtr& cam_info)
  10 {
  11   // Solve all of perception here...
  12 }
  13 
  14 int main(int argc, char** argv)
  15 {
  16   ros::init(argc, argv, "vision_node");
  17 
  18   ros::NodeHandle nh;
  19 
  20   message_filters::Subscriber<Image> image_sub(nh, "image", 1);
  21   message_filters::Subscriber<CameraInfo> info_sub(nh, "camera_info", 1);
  22   TimeSynchronizer<Image, CameraInfo> sync(image_sub, info_sub, 10);
  23   sync.registerCallback(boost::bind(&callback, _1, _2));
  24 
  25   ros::spin();
  26 
  27   return 0;
  28 }

(Note: In this particular case you could use the CameraSubscriber class from image_transport, which essentially wraps the filtering code above.)







评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值