需求
ROS中需要同时订阅多个topic,并对多个topic的数据同时处理
解决办法
可以使用ros的message_filter消息同步方法;
具体使用方法可以参考一下链接
ros之message_filter消息同步
时间同步及在回调函数中发布ROS消息
注意事项
- error: ‘value’ is not a member of ros::message_traits::TImeStamp<消息类型>
- Synchronizer with approximate time policy in a class [C++]
在类中定义Synchronizer and the registerCallback
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
| #include "header.h" using namespace message_filters; typedef control_sys::IntWithHeader myMsg;
class Node {
public:
Node() { sub1.subscribe(node, "in1", 1); sub2.subscribe(node, "in2", 1); sync.reset(new Sync(MySyncPolicy(10), sub1, sub2)); sync->registerCallback(boost::bind(&Node::callback, this, _1, _2));
}
void callback(const control_sys::IntWithHeaderConstPtr &in1, const control_sys::IntWithHeaderConstPtr &in2) { std::cout << "Synchronization successful" << std::endl; }
private:
ros::NodeHandle node; ros::Publisher pub; std_msgs::Int32 out;
message_filters::Subscriber<myMsg> sub1; message_filters::Subscriber<myMsg> sub2; typedef sync_policies::ApproximateTime<myMsg, myMsg> MySyncPolicy; typedef Synchronizer<MySyncPolicy> Sync; boost::shared_ptr<Sync> sync;
};
int main(int argc, char **argv) {
ros::init(argc, argv, "synchronizer");
Node synchronizer;
ros::spin();
}
|
synchronizer-with-approximate-time-policy-in-a-class-c/