ROS之多个订阅数据同步

2023-05-16

做传感器数据融合时,常常会需要用到多个数据,即需要同时订阅多个话题。那么,如何同步这些传感器数据的时间辍,并将它们放入一个回调函数中进行处理呢?

参考文档:

  1. http://wiki.ros.org/message_filters
  2. ROS自带多传感器时间同步机制Time Synchronizer测试
  3. ros消息时间同步与回调

有些消息类型会带有一个头部数据结构,如下所示。信息中带有时间辍数据,可以通过这个数据进行时间同步。

std_msgs/Header header
  uint32 seq
  time stamp
  string frame_id
  • 以下是一种同步的方式:Time Synchronizer

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.

#include <message_filters/subscriber.h>
#include <message_filters/time_synchronizer.h>
#include <sensor_msgs/Image.h>
#include <sensor_msgs/CameraInfo.h>

using namespace sensor_msgs;
using namespace message_filters;

void callback(const ImageConstPtr& image, const CameraInfoConstPtr& cam_info)
{
  // Solve all of perception here...
}

int main(int argc, char** argv)
{
  ros::init(argc, argv, "vision_node");

  ros::NodeHandle nh;

  message_filters::Subscriber<Image> image_sub(nh, "image", 1);
  message_filters::Subscriber<CameraInfo> info_sub(nh, "camera_info", 1);
  TimeSynchronizer<Image, CameraInfo> sync(image_sub, info_sub, 10);
  sync.registerCallback(boost::bind(&callback, _1, _2));

  ros::spin();

  return 0;
}
  • 另外一种是基于策略的同步方式,也是通过消息头部数据的时间辍进行同步。

Policy-Based Synchronizer [ROS 1.1+]

The Synchronizer 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.

The Synchronizer filter is templated on a policy that determines how to synchronize the channels. There are currently two policies: ExactTime and ApproximateTime.

  1. 当需要同步的所有消息都带有时间辍的头部数据:ExactTime

    The message_filters::sync_policies::ExactTime policy requires messages to have exactly the same timestamp in order to match. Your callback is only called if a message has been received on all specified channels with the same exact timestamp. The timestamp is read from the header field of all messages (which is required for this policy).

#include <message_filters/subscriber.h>
#include <message_filters/synchronizer.h>
#include <message_filters/sync_policies/exact_time.h>
#include <sensor_msgs/Image.h>
#include <sensor_msgs/CameraInfo.h>

using namespace sensor_msgs;
using namespace message_filters;

void callback(const ImageConstPtr& image, const CameraInfoConstPtr& cam_info)
{
  // Solve all of perception here...
}

int main(int argc, char** argv)
{
  ros::init(argc, argv, "vision_node");

  ros::NodeHandle nh;
  message_filters::Subscriber<Image> image_sub(nh, "image", 1);
  message_filters::Subscriber<CameraInfo> info_sub(nh, "camera_info", 1);

  typedef sync_policies::ExactTime<Image, CameraInfo> MySyncPolicy;
  // ExactTime takes a queue size as its constructor argument, hence MySyncPolicy(10)
  Synchronizer<MySyncPolicy> sync(MySyncPolicy(10), image_sub, info_sub);
  sync.registerCallback(boost::bind(&callback, _1, _2));

  ros::spin();

  return 0;
}

由于该同步策略是当所有需同步的话题的时间辍严格相等时,才会触发回调函数。这就会导致以下一些问题:

  • 回调函数的触发频率必然小于等于这些话题中最小的发布频率;
  • 回调函数的触发并不十分稳定,有时候甚至会出现长时间不被触发的情况。如下图所示,某一次的间隔甚至长达10s左右。

在这里插入图片描述

  1. ROS提供了另外一种方法来实现数据的同步:ApproximateTime。与需要时间辍完全相同的ExactTime不同,该方法允许话题之间的时间辍存在一定的偏差。

    The message_filters::sync_policies::ApproximateTime policy uses an adaptive algorithm to match messages based on their timestamp.

#include <message_filters/subscriber.h>
#include <message_filters/synchronizer.h>
#include <message_filters/sync_policies/approximate_time.h>
#include <sensor_msgs/Image.h>

using namespace sensor_msgs;
using namespace message_filters;

void callback(const ImageConstPtr& image1, const ImageConstPtr& image2)
{
  // Solve all of perception here...
}

int main(int argc, char** argv)
{
  ros::init(argc, argv, "vision_node");

  ros::NodeHandle nh;
  message_filters::Subscriber<Image> image1_sub(nh, "image1", 1);
  message_filters::Subscriber<Image> image2_sub(nh, "image2", 1);

  typedef sync_policies::ApproximateTime<Image, Image> MySyncPolicy;
  // ApproximateTime takes a queue size as its constructor argument, hence MySyncPolicy(10)
  Synchronizer<MySyncPolicy> sync(MySyncPolicy(10), image1_sub, image2_sub);
  sync.registerCallback(boost::bind(&callback, _1, _2));

  ros::spin();

  return 0;
}

从下图可以看出,虽然该方法允许时间之间存在偏差,但实际上偏差并不大。而且比起上一种方法,这个方法的回调函数的触发频率快多了。

在这里插入图片描述
关于ApproximateTime,我还有一个不解的地方,这里做一下记录:

If not all messages have a header field from which the timestamp could be determined, see below for a workaround.
If some messages are of a type that doesn’t contain the header field, ApproximateTimeSynchronizer refuses by default adding such messages.

以上这两句话,似乎自相矛盾。不知道是不是我理解的问题。。。从时间同步的角度看,话题消息内容中应该必须要带上时间辍信息才能进行同步,但第一句话却说可以允许一些消息不带时间辍?

[补充于2021.2.11: 今天在使用ApproximateTime时同步了一个自定义的消息类型,发生了如下图所示的错误。后来查阅资料才发现是没有加header的原因,即没有时间辍,程序就无法根据时间进行同步。换句话说,该方法也是必须需要时间辍信息的。加上header后错误就没有了。]

在这里插入图片描述

另外需要注意的是,使用message_filters时,需要在CMakeLists.txtpackage.xml中添加相关依赖:

# CMakeLists.txt
find_package( catkin REQUIRED COMPONENTS
	 ...
	message_filters
)
# package.xml
find_package( catkin REQUIRED COMPONENTS
	<build_depend>message_filters</build_depend>
	<build_export_depend>message_filters</build_export_depend>
	<exec_depend>message_filters</exec_depend>
)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

ROS之多个订阅数据同步 的相关文章

随机推荐

  • qt: error: C2001: 常量中有换行符

    PS 这两天搞工程系统移植 xff0c 搞得疯掉了 xff0c 代码复用还不如重写呢 如下一句带有中文的程序 xff0c mingw 43 linux 运行没有任何问题 xff0c window下msvc 运行就报错C2001 time s
  • Eigen内存分配器aligned_allocator

    在使用Eigen的时候 xff0c 如果STL容器中的元素是Eigen数据库结构 xff0c 比如下面用vector容器存储Eigen Matrix4f类型或用map存储Eigen Vector4f数据类型时 xff1a vector lt
  • Ubuntu 升级cmake 版本

    PS 在编译一些包时需要更高的版本 xff0c 需要升级 cmake 千万别执行下面的命令 xff0c 这样会把之前用 cmake 编译好的包都给卸载掉 xff0c 包括ros sudo apt get autoremove cmake 比
  • 视觉slam十四讲(ch6) Ubuntu18.04安装 g2o库 报错error: FixedArray ... has no member named ‘fill’

    ps 再学习14讲第二版的时候 xff0c 运行g2o 报错 error FixedArray aka class ceres internal FixedArray lt double 6 gt has no member named f
  • 无人驾驶学习笔记-NDT 配准

    目录 1 NDT 的算法处理流程 2 NDT 公式推导 3 NDT 实例 3 1 常规NDT的位姿估计 3 2 front end node 1 ROS常规初始化 2 初始化操作 xff1a 读取传感器数据 获取lidar to imu变换
  • KD 树原理以及在三维激光点云中的应用

    目录 1 介绍 2 原理 2 1 数据结构 2 2 构建KD树 2 3 实例 3 程序示例 4 参考链接 1 介绍 kd tree简称k维树 xff0c 是一种空间划分的数据结构 常被用于高维空间中的搜索 xff0c 比如范围搜索和最近邻搜
  • slam 基础知识整理之- 最小二乘问题的引出与求解方法

    目录 1 最小二乘引出 2 线性最小二乘 及 求解方法 3 非线性最小二乘 编辑 3 1 求解思路 3 2 常用四种方法 3 3 四种方法总结 4 参考链接 在SLAM的过程中 xff0c 我们可以构建机器人状态过程 通过对其概率的计算 x
  • 无人驾驶学习笔记 - LOAM 算法论文核心关键点总结

    目录 1 框架 2 特征点提取 3 点云去畸变 4 帧间匹配 特征关联与损失函数计算 a 线特征 b 面特征 5 运动估计 6 建图 7 姿态融合 8 LOAM 优劣势 9 参考连接 1 框架 loam框架核心是两部分 xff0c 高频率的
  • 动态窗口法的理解和一些细节

    机器人局部路径规划 动态窗口法 动态窗口法 xff08 Dynamic Window Approach xff0c DWA xff09 是一类经典的机器人局部路径规划算法 它的过程主要分为两部分 xff1a 速度空间 v
  • 无人驾驶学习笔记 - A-LOAM 算法代码解析总结

    目录 1 概述 2 scanRegistration cpp 2 1 代码注释 2 1 1 主函数 2 1 2 removeClosedPointCloud xff08 雷达周边过近点移除 xff09 2 1 3 laserCloudHan
  • 无人驾驶学习笔记-LeGO-LOAM 算法源码学习总结

    目录 1 概述 2 lego loam的贡献 3 系统框图 4 ros graph中的节点关系表 5 lego loam 的文件系统架构 6 各部分方法原理及代码注释 6 1 点云投影与目标分割 1 总结概述 2 代码注释 2 1 copy
  • Boost 中 signal2 用法

    boost 函数与回调 xff08 三 xff09 signals2
  • 树莓派学习笔记

    文章目录 树莓派基础入门笔记无显示屏使用方式基础教程5 树莓派文件传输 配置编译环境使用U盘直接传输使用vnc传输文件FTP文件传输协议Python配置编译环境C C 43 43 配置编译环境Linux常用终端命令nano和vi编辑器的使用
  • 22.IO与显示器

    README 1 本文内容总结自 B站 操作系统 哈工大李治军老师 xff0c 内容非常棒 xff0c 墙裂推荐 xff1b 2 显示器是输入型外设 xff1b 3 本章主要内容是讲 显示器是如何被驱动的 xff1b 或操作系统是如何让用户
  • BGP协议基础配置—学习

    BGP重要概念 IGP是运行在AS内部的路由协议 xff0c 主要有RIP OSPF及IS IS xff0c 着重于发现和计算路由 EGP是运行在AS之间的路由协议 xff0c 通常是BGP xff0c 它是实现路由控制和选择最好的路由协议
  • STM32 Not a genuine ST Device! Abort connection 错误解决方案

    STM32 Not a genuine ST Device Abort connection 错误解决方案 网上解决方案晶振设置不匹配导致Connect setting to with Pre reset降低MAX Clock 我自己的解决
  • VMware的.vmdk文件只赠不减的处理方法

    VMware虚拟机的虚拟磁盘的大小会随着使用时间不断变大 xff0c 而且只赠不减 即使在虚拟系统中删除了磁盘中的文件 xff0c 虚拟磁盘的大小仍然不会变小 释放空闲磁盘的方法如下 xff1a VMWare Tools中的 Shrink功
  • 使用码云(Gitee)进行代码管理,以及VsCode关联Git

    一 安装git Git的下载 安装与配置 git 简明指南 二 注册码云 1 xff09 注册码云账号 xff1a 码云官网 2 xff09 绑定邮箱 xff1a 右上角 头像 设置 邮箱管理 三 本地项目与码云关联 1 本地项目上传至码云
  • 删掉带页眉的空白页结果把所有页眉都删掉解决办法

    点击视图 大纲 会发现在之前的操作中有两个分节符 xff0c 把上面那个删掉即可 参考链接https zhidao baidu com question 105591450 html 小问题也蛮耗时的
  • ROS之多个订阅数据同步

    做传感器数据融合时 xff0c 常常会需要用到多个数据 xff0c 即需要同时订阅多个话题 那么 xff0c 如何同步这些传感器数据的时间辍 xff0c 并将它们放入一个回调函数中进行处理呢 xff1f 参考文档 xff1a http wi