当前位置:   article > 正文

[ROS]message filters时间同步_message_filters

message_filters

1.message_filters介绍

message_filters用于对齐多种传感信息的时间戳,对齐时间戳有两种方式,一种是时间戳完全对齐 :ExactTime Policy ,另一种是时间戳相近:ApproximateTime Policy

2.message_filters作用

  1. 同时订阅并发布话题
  2. 时间同步

3.message_filters实例

  • 头文件
  1. //ros头文件
  2. #include <ros/ros.h>
  3. //时间同步
  4. #include <message_filters/subscriber.h>
  5. #include <message_filters/sync_policies/approximate_time.h>
  6. #include <message_filters/synchronizer.h>
  7. //传感器消息
  8. #include <sensor_msgs/Image.h>
  9. #include <sensor_msgs/image_encodings.h>
  10. #include <sensor_msgs/PointCloud2.h>
  • subandpub.h
  1. #ifndef __SUBANDPUB_H__
  2. #define __SUBANDPUB_H__
  3. //ros头文件
  4. #include <ros/ros.h>
  5. //时间同步
  6. #include <message_filters/subscriber.h>
  7. #include <message_filters/sync_policies/approximate_time.h>
  8. #include <message_filters/synchronizer.h>
  9. //传感器消息
  10. #include <sensor_msgs/Image.h>
  11. #include <sensor_msgs/image_encodings.h>
  12. #include <sensor_msgs/PointCloud2.h>
  13. class subscriberANDpublisher{
  14. public:
  15. subscriberANDpublisher();
  16. void callback(const sensor_msgs::ImageConstPtr &image, const sensor_msgs::PointCloud2ConstPtr &pointcloud);
  17. private:
  18. ros::NodeHandle nh;
  19. ros::Publisher camera_pub;
  20. ros::Publisher lidar_pub;
  21. message_filters::Subscriber<sensor_msgs::PointCloud2> lidar_sub;//雷达订阅
  22. message_filters::Subscriber<sensor_msgs::Image> camera_sub;//相机订阅
  23. typedef message_filters::sync_policies::ApproximateTime<sensor_msgs::Image, sensor_msgs::PointCloud2> syncpolicy;//时间戳对齐规则
  24. typedef message_filters::Synchronizer<syncpolicy> Sync;
  25. boost::shared_ptr<Sync> sync_;//时间同步器
  26. };
  27. #endif
  • subandpub.cpp
  1. #include "SubandPub.h"
  2. subscriberANDpublisher::subscriberANDpublisher()
  3. {
  4. //订阅话题
  5. lidar_sub.subscribe(nh, "/rslidar_points", 1);
  6. camera_sub.subscribe(nh, "/zed/zed_node/left/image_rect_color", 1);
  7. //发布者
  8. camera_pub=nh.advertise<sensor_msgs::Image>("sync/img",1000);
  9. lidar_pub = nh.advertise<sensor_msgs::PointCloud2>("sync/lidar", 1000);
  10. //回调
  11. sync_.reset(new Sync(syncpolicy(10), camera_sub, lidar_sub));
  12. sync_->registerCallback(boost::bind(&subscriberANDpublisher::callback, this, _1, _2));
  13. }
  14. void subscriberANDpublisher::callback(const sensor_msgs::ImageConstPtr &image, const sensor_msgs::PointCloud2ConstPtr &pointcloud) {
  15. ROS_INFO("done! ");
  16. camera_pub.publish(image);
  17. lidar_pub.publish(pointcloud);
  18. }
  • main.cpp
  1. #include <ros/ros.h>
  2. #include "SubandPub.h"
  3. int main(int argc, char **argv) {
  4. ros::init(argc, argv, "node");
  5. subscriberANDpublisher sp;
  6. ROS_INFO("main done! ");
  7. ros::spin();
  8. }
  • cmakelist

  • package.xml

4.运行结果

5.参考

链接:https://wiki.ros.org/message_filters

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

闽ICP备14008679号