当前位置:   article > 正文

ROS中的message_filter中的时间同步_error: ‘approximatetime’ in namespace ‘message_fil

error: ‘approximatetime’ in namespace ‘message_filters::sync_policies’ d
  1. #ifndef MESSAGE_FILTERS_TIME_SYNCHRONIZER_H
  2. #define MESSAGE_FILTERS_TIME_SYNCHRONIZER_H
  3. #include <memory>
  4. #include "message_filters/message_event.h"
  5. #include "message_filters/synchronizer.h"
  6. #include "message_filters/sync_policies/exact_time.h"
  7. namespace message_filters
  8. {
  9. /**
  10. * \brief Synchronizes up to 9 messages by their timestamps.
  11. *
  12. * TimeSynchronizer synchronizes up to 9 incoming channels by the timestamps contained in their messages' headers.
  13. * TimeSynchronizer takes anywhere from 2 to 9 message types as template parameters, and passes them through to a
  14. * callback which takes a shared pointer of each.
  15. *
  16. * The required queue size parameter when constructing the TimeSynchronizer tells it how many sets of messages it should
  17. * store (by timestamp) while waiting for messages to arrive and complete their "set"
  18. *
  19. * \section connections CONNECTIONS
  20. *
  21. * The input connections for the TimeSynchronizer object is the same signature as for rclcpp subscription callbacks, ie.
  22. \verbatim
  23. void callback(const std::shared_ptr<M const>&);
  24. \endverbatim
  25. * The output connection for the TimeSynchronizer object is dependent on the number of messages being synchronized. For
  26. * a 3-message synchronizer for example, it would be:
  27. \verbatim
  28. void callback(const std::shared_ptr<M0 const>&, const std::shared_ptr<M1 const>&, const std::shared_ptr<M2 const>&);
  29. \endverbatim
  30. * \section usage USAGE
  31. * Example usage would be:
  32. \verbatim
  33. TimeSynchronizer<sensor_msgs::CameraInfo, sensor_msgs::Image, sensor_msgs::Image> sync_policies(caminfo_sub, limage_sub, rimage_sub, 3);
  34. sync_policies.registerCallback(callback);
  35. \endverbatim
  36. * The callback is then of the form:
  37. \verbatim
  38. void callback(const sensor_msgs::CameraInfo::ConstPtr&, const sensor_msgs::Image::ConstPtr&, const sensor_msgs::Image::ConstPtr&);
  39. \endverbatim
  40. *
  41. */
  42. template<class M0, class M1, class M2 = NullType, class M3 = NullType, class M4 = NullType,
  43. class M5 = NullType, class M6 = NullType, class M7 = NullType, class M8 = NullType>
  44. class TimeSynchronizer : public Synchronizer<sync_policies::ExactTime<M0, M1, M2, M3, M4, M5, M6, M7, M8> >
  45. {
  46. public:
  47. typedef sync_policies::ExactTime<M0, M1, M2, M3, M4, M5, M6, M7, M8> Policy;
  48. typedef Synchronizer<Policy> Base;
  49. typedef std::shared_ptr<M0 const> M0ConstPtr;
  50. typedef std::shared_ptr<M1 const> M1ConstPtr;
  51. typedef std::shared_ptr<M2 const> M2ConstPtr;
  52. typedef std::shared_ptr<M3 const> M3ConstPtr;
  53. typedef std::shared_ptr<M4 const> M4ConstPtr;
  54. typedef std::shared_ptr<M5 const> M5ConstPtr;
  55. typedef std::shared_ptr<M6 const> M6ConstPtr;
  56. typedef std::shared_ptr<M7 const> M7ConstPtr;
  57. typedef std::shared_ptr<M8 const> M8ConstPtr;
  58. using Base::add;
  59. using Base::connectInput;
  60. using Base::registerCallback;
  61. using Base::setName;
  62. using Base::getName;
  63. using Policy::registerDropCallback;
  64. typedef typename Base::M0Event M0Event;
  65. typedef typename Base::M1Event M1Event;
  66. typedef typename Base::M2Event M2Event;
  67. typedef typename Base::M3Event M3Event;
  68. typedef typename Base::M4Event M4Event;
  69. typedef typename Base::M5Event M5Event;
  70. typedef typename Base::M6Event M6Event;
  71. typedef typename Base::M7Event M7Event;
  72. typedef typename Base::M8Event M8Event;
  73. template<class F0, class F1>
  74. TimeSynchronizer(F0& f0, F1& f1, uint32_t queue_size)
  75. : Base(Policy(queue_size))
  76. {
  77. connectInput(f0, f1);
  78. }
  79. template<class F0, class F1, class F2>
  80. TimeSynchronizer(F0& f0, F1& f1, F2& f2, uint32_t queue_size)
  81. : Base(Policy(queue_size))
  82. {
  83. connectInput(f0, f1, f2);
  84. }
  85. template<class F0, class F1, class F2, class F3>
  86. TimeSynchronizer(F0& f0, F1& f1, F2& f2, F3& f3, uint32_t queue_size)
  87. : Base(Policy(queue_size))
  88. {
  89. connectInput(f0, f1, f2, f3);
  90. }
  91. template<class F0, class F1, class F2, class F3, class F4>
  92. TimeSynchronizer(F0& f0, F1& f1, F2& f2, F3& f3, F4& f4, uint32_t queue_size)
  93. : Base(Policy(queue_size))
  94. {
  95. connectInput(f0, f1, f2, f3, f4);
  96. }
  97. template<class F0, class F1, class F2, class F3, class F4, class F5>
  98. TimeSynchronizer(F0& f0, F1& f1, F2& f2, F3& f3, F4& f4, F5& f5, uint32_t queue_size)
  99. : Base(Policy(queue_size))
  100. {
  101. connectInput(f0, f1, f2, f3, f4, f5);
  102. }
  103. template<class F0, class F1, class F2, class F3, class F4, class F5, class F6>
  104. TimeSynchronizer(F0& f0, F1& f1, F2& f2, F3& f3, F4& f4, F5& f5, F6& f6, uint32_t queue_size)
  105. : Base(Policy(queue_size))
  106. {
  107. connectInput(f0, f1, f2, f3, f4, f5, f6);
  108. }
  109. template<class F0, class F1, class F2, class F3, class F4, class F5, class F6, class F7>
  110. TimeSynchronizer(F0& f0, F1& f1, F2& f2, F3& f3, F4& f4, F5& f5, F6& f6, F7& f7, uint32_t queue_size)
  111. : Base(Policy(queue_size))
  112. {
  113. connectInput(f0, f1, f2, f3, f4, f5, f6, f7);
  114. }
  115. template<class F0, class F1, class F2, class F3, class F4, class F5, class F6, class F7, class F8>
  116. TimeSynchronizer(F0& f0, F1& f1, F2& f2, F3& f3, F4& f4, F5& f5, F6& f6, F7& f7, F8& f8, uint32_t queue_size)
  117. : Base(Policy(queue_size))
  118. {
  119. connectInput(f0, f1, f2, f3, f4, f5, f6, f7, f8);
  120. }
  121. TimeSynchronizer(uint32_t queue_size)
  122. : Base(Policy(queue_size))
  123. {
  124. }
  125. // For backwards compatibility
  126. void add0(const M0ConstPtr& msg)
  127. {
  128. this->template add<0>(M0Event(msg));
  129. }
  130. void add1(const M1ConstPtr& msg)
  131. {
  132. this->template add<1>(M1Event(msg));
  133. }
  134. void add2(const M2ConstPtr& msg)
  135. {
  136. this->template add<2>(M2Event(msg));
  137. }
  138. void add3(const M3ConstPtr& msg)
  139. {
  140. this->template add<3>(M3Event(msg));
  141. }
  142. void add4(const M4ConstPtr& msg)
  143. {
  144. this->template add<4>(M4Event(msg));
  145. }
  146. void add5(const M5ConstPtr& msg)
  147. {
  148. this->template add<5>(M5Event(msg));
  149. }
  150. void add6(const M6ConstPtr& msg)
  151. {
  152. this->template add<6>(M6Event(msg));
  153. }
  154. void add7(const M7ConstPtr& msg)
  155. {
  156. this->template add<7>(M7Event(msg));
  157. }
  158. void add8(const M8ConstPtr& msg)
  159. {
  160. this->template add<8>(M8Event(msg));
  161. }
  162. };
  163. } // namespace message_filters
  164. #endif // MESSAGE_FILTERS__TIME_SYNCHRONIZER_H_

message_filters是ROS的一个实用程序库,集合了许多的常用的消息“过滤”算法。消息过滤器message_filters类似一个消息缓存,当消息到达消息过滤器的时候,可能并不会立即输出,而是在稍后的时间点里满足一定条件下输出(也就是上面代码中定义那一些列事件)。
举个例子,比如时间同步器(上面的代码就是时间同步过滤器),它接收来自多个源的不同类型的消息,并且仅当它们在具有相同时间戳的每个源上接收到消息时才输出它们,也就是起到了一个消息同步输出的效果,时间同步过滤器本身并不会修改各个数据源传过来的数据(也就是缓冲区中的数据),而是将各个缓冲区建立时间线,然后根据时间线上的各个时间点来判断是否需要输出。当这些时间线上都存在某个时间点的数据,则说明该时间点,各个数据源都有数据,这些数据自然是对齐的,然后就可以输出这一组对齐的数据,供用户应用使用。当然,这里存在严格同步和近似同步的策略

  1. // msg_filter.cpp
  2. #include <ros/ros.h>
  3. #include <sensor_msgs/PointCloud2.h>
  4. #include <sensor_msgs/Image.h>
  5. #include <message_filters/subscriber.h>
  6. #include <message_filters/time_synchronizer.h>
  7. #include <message_filters/sync_policies/approximate_time.h>
  8. #include <iostream>
  9. std::string img_topic = "/fuhong/_img";
  10. std::string point_topic = "/fuhong/_points";
  11. //rosbag::Bag bag_record;//文件直接记录
  12. ros::Publisher img_pub;
  13. ros::Publisher pointcloud_pub;
  14. typedef message_filters::sync_policies::ApproximateTime<sensor_msgs::Image, sensor_msgs::PointCloud2> testSyncPolicy; //近似同步策略
  15. //回调函数
  16. void callback(const sensor_msgs::ImageConstPtr &image, const sensor_msgs::PointCloud2ConstPtr &point) //回调中包含多个消息
  17. {
  18. //以实时发布的方式发布
  19. img_pub.publish(*image);
  20. pointcloud_pub.publish(*point);
  21. }
  22. int main(int argc, char **argv) {
  23. ros::init(argc, argv, "time_synch");
  24. ros::NodeHandle n;
  25. //发布的话题
  26. img_pub = n.advertise<sensor_msgs::Image>(img_topic, 1000);
  27. pointcloud_pub = n.advertise<sensor_msgs::PointCloud2>(point_topic, 1000);
  28. //订阅的话题
  29. message_filters::Subscriber<sensor_msgs::Image> img_sub(n, "/zed/zed_node/left/image_rect_color", 1);// topic1 输入
  30. message_filters::Subscriber<sensor_msgs::PointCloud2> pointcloud_sub(n, "/rslidar_points", 1);// topic2 输入
  31. message_filters::Synchronizer<testSyncPolicy> sync(testSyncPolicy(10), img_sub, pointcloud_sub);// 同步
  32. sync.registerCallback(boost::bind(&callback, _1, _2));
  33. ros::spin();
  34. ros::shutdown();
  35. return 0;
  36. }

同步前的效果:
在这里插入图片描述
同步后的效果:
在这里插入图片描述

上述两个时间刻度不同,同步前的频率高,同步后的频率低,也就是说对于那些没有对齐的数据,进行同步处理后,并没有输出出来。最终pub出来的只是原始数据的子集。

参考:https://blog.csdn.net/hongge_smile/article/details/106324562?utm_medium=distribute.pc_relevant.none-task-blog-BlogCommendFromBaidu-3.control&depth_1-utm_source=distribute.pc_relevant.none-task-blog-BlogCommendFromBaidu-3.control

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小丑西瓜9/article/detail/540439
推荐阅读
相关标签
  

闽ICP备14008679号