ROS多传感器数据时间戳同步方案——message_filters::TimeSynchronizer

综述

在ROS开发过程中,有一些时候需要处理来自多个传感器的数据,比如我自己做的一些东西,就需要把不同相机的点云数据进行融合,融合的过程中需要判断传感器数据的时间戳,保证融合的数据尽可能是两个传感器的同步数据,一般的实现是用队列来存储传感器的数据,在不断的出队过程中比较传感器队列的时间戳,如果时间戳之间相差的时间在容忍范围内,就可以认为是同步的。

std::queue buf1;
std::queue buf2;
std::thread process;
//typedef M 传感器数据类型,比如sensor_msg::PointCloud2、sensor_msg::Image等等
void callback1(M& msg){
	//数据入队
	buf1.push(msg);
	//其他代码。。。
}
void callback2(M& msg){
	//数据入队
	buf2.push(msg);
	//其他代码。。。
}
void process_comparation(){
	while(ros::ok()){
		M data1=buf1.front();
		M data2=buf2.front();
		if(data1.header.timestamp.toSec()>data1.header.timestamp.toSec()){
			buf2.pop();
		}
		else 
			buf1.pop();
		//其他操作data1和data2的代码
	}
}
int main(int argc,char** argv){
	//initialization
	
}

这个也是VINS里面处理双目相机和IMU数据的方法,这样的方法一个很大的缺点是需要用一些进程锁和条件变量来保证内存访问,因为有一些全局变量例如传感器数据的队列可能同时会被多个进程访问,这个缺点在处理图片和IMU这类小体量数据的时候体现不出来,并且VINS也确实需要这样的队列来完成算法,在处理点云数据的时候,这个缺点对于程序的性能就有很大的影响。幸好ROS提供了message_filters::TimeSynchronizer等同步类来完成多传感器数据同步融合的任务。

message_filters::TimeSynchronizer

参照官文链接: ROS wiki

#include <message_filters/subscriber.h>
#include <message_filters/time_synchronizer.h>
#include <sensor_msgs/PointCloud2.h>

using namespace sensor_msgs;
using namespace message_filters;

void callback(const PointCloud2ConstPtr& image, const PointCloud2ConstPtr& 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<PointCloud2> cloud1(nh, "/camera1/depth/color/points", 1);
  message_filters::Subscriber<PointCloud2> cloud2(nh, "/camera2/depth/color/points", 1);
  TimeSynchronizer<PointCloud2, PointCloud2> sync(cloud1, cloud2, 10);
  sync.registerCallback(boost::bind(&callback, _1, _2));//boost::bind的用法可以参考https://www.cnblogs.com/benxintuzi/p/4862129.html

  ros::spin();

  return 0;
}

注意:回调函数里面的参数一定是const MConstPtr& msg,不能是M& msg。比如点云数据一定是const PointCloud2ConstPtr& msg,别的像const PointCloud2Ptr& msg或者const PointCloud2& msg都不行,这个也是困扰了我一个晚上的小小细节。

这段代码是ROS(机器人操作系统)中使用Python编写的,用于同步接收图像和点云消息,进行数据融合后再发布到一个新的话题中。如果您想在C++中实现相同的功能,可以使用ROS中的C++库进行开发,以下是相应的代码示例: ```c++ #include <ros/ros.h> #include <image_transport/image_transport.h> #include <sensor_msgs/PointCloud2.h> #include <message_filters/subscriber.h> #include <message_filters/time_synchronizer.h> #include <message_filters/sync_policies/approximate_time.h> // Define callback function for synchronized messages void callback(const sensor_msgs::ImageConstPtr& image_msg, const sensor_msgs::PointCloud2ConstPtr& pc_msg) { // TODO: image and point cloud fusion // ... // Publish the fused image // ... } int main(int argc, char** argv) { ros::init(argc, argv, "image_pc_fusion"); ros::NodeHandle nh; // Define message subscribers image_transport::ImageTransport it(nh); image_transport::Subscriber image_sub = it.subscribe("image_color", 1); message_filters::Subscriber<sensor_msgs::PointCloud2> pc_sub(nh, "velodyne_points", 1); // Define message synchronizer typedef message_filters::sync_policies::ApproximateTime<sensor_msgs::Image, sensor_msgs::PointCloud2> MySyncPolicy; message_filters::Synchronizer<MySyncPolicy> sync(MySyncPolicy(5), image_sub, pc_sub); sync.registerCallback(boost::bind(&callback, _1, _2)); // Define message publisher image_transport::Publisher image_pub = it.advertise("camera_lidar", 1); ros::spin(); return 0; } ``` 在这个例子中,我们使用了ROS中的C++库来定义了一个节点(`image_pc_fusion`),并订阅了两个不同的话题:`image_color`和`velodyne_points`。其中,`image_transport`库用于图像消息的传输和处理;`message_filters`库用于消息的同步接收和处理。 通过`sync`对象,我们将图像和点云消息进行同步,设置了一个容差值`slop`,在5个消息中,如果时间戳相差小于0.1秒,就会被认为是同步的。当消息被同步后,就会调用`callback`函数,对图像和点云进行融合处理,并将结果发布到新的话题中`camera_lidar`。 需要注意的是,C++中的ROS编程需要使用一些ROS特有的语法和API,如果您不熟悉ROS的相关知识,请先学习ROS的基本概念和编程方法。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值