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++):
or:
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>&)
例子:
is the equivalent of:
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.
Python: callback(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.)