ros 同步两个消息

由于最近经常用到 相机和激光雷达来采集数据,因此在这里就需要对两个不同的消息进行同步处理。

由于是之前录好的包,因此需要在播放的时候  ,需要进行 设置一个参数。

 

rosparam set use_sim_time true

 

降低播放速度:

 

 rosbag play -r 0.1  stereo_camera.bag --clock  --pause 

 

 

 

此外由于我们读取的图像时 经过压缩的 ,如果直接接收 compressed 的数据的话,会出现一些问题。假设我们录包里面的消息是

 

 

 

 

/camera_17023549/pg_17023549/image_raw/compressed

 

 

 

 

 

可以先进行数据的TOPIC 转发一下

 

rosrun image_transport republish compressed in:=/camera_17023549/pg_17023549/image_raw raw out:=/camera_17023549/pg_17023549/image_raw


这样的话我们只需要在 cpp 里面接收消息:

 

 

/camera_17023549/pg_17023549/image_raw

 

 


就可以了,其次的话还有另外一种方法,在 ros wiki 上面进行介绍的

rosparam set /compressed_listener/image_transport compressed

 

rosrun beginner_tutorials compress __name:=compressed 

 

 

 

此外还有小的技巧比如怎么看一个消息的 fixframe

 

 rostopic echo /horizontal_hokuyo/scan | grep frame_id


当你录得包里面含有很多消息,你只想拿出来其中的一部分时可以这么做

 

 

rosbag filter sensor_data.bag only-tf.bag "topic == '/camera_17023549/pg_17023549/image_raw/compressed' or topic == '/left_velodyne/velodyne_points'"

在这里只保留俩消息。
 

 

 

/camera_17023549/pg_17023549/image_raw/compressed 和 /left_velodyne/velodyne_points

 

 

 

 

 

好了言归正传 我们在这里开始进行同步两个消息

 

 

 

 

 

#include <ros/ros.h>
#include <message_filters/subscriber.h>
#include <message_filters/time_synchronizer.h>
#include <message_filters/sync_policies/approximate_time.h>
#include <sensor_msgs/Image.h>
#include <sensor_msgs/CameraInfo.h>
#include <geometry_msgs/PoseStamped.h>
#include <iostream>
#include <beginner_tutorials/myNum.h>

using namespace std;
using namespace sensor_msgs;
using namespace message_filters;
using namespace geometry_msgs;

// global variable

typedef message_filters::sync_policies::ApproximateTime<Image, Image> sync_policy_classification;

void callback(const ImageConstPtr& cam_info, const ImageConstPtr& pos)
{

    cout << "I should record the pose: " << endl;
}



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

    ros::NodeHandle nh;

    message_filters::Subscriber<Image> info_sub(nh, "/camera_17082012/pg_17082012/image_raw", 1);
    message_filters::Subscriber<Image> pose_sub(nh, "/camera_17082034/pg_17082034/image_raw", 1);
    message_filters::Synchronizer<sync_policy_classification> sync(sync_policy_classification(100), info_sub, pose_sub);
    //TimeSynchronizer<CameraInfo, PoseStamped> sync(info_sub, pose_sub, 10);
    sync.registerCallback(boost::bind(&callback, _1, _2));

    ros::spin();

    return 0;
}

 

 

 

 

 

### 实现两个ROS Bag文件的时间同步 为了实现在ROS中对两个Bag文件进行时间同步,可以采用多种策略。一种常见做法是利用`rosbag`工具结合Python脚本来处理和调整消息的时间戳。 #### 方法一:通过自定义Python脚本实现时间同步 可以通过编写Python脚本来读取两个Bag文件中的消息,并根据所需逻辑重新设置每条消息的时间戳。下面是一个简单的例子: ```python import rosbag from datetime import timedelta, datetime def sync_bags(bag_file_1, bag_file_2, output_bag): """ 同步来自两个不同Bag文件的消息到一个新的Bag文件中。 参数: bag_file_1 (str): 输入的第一个Bag文件路径。 bag_file_2 (str): 输入的第二个Bag文件路径。 output_bag (str): 输出的新Bag文件路径。 """ with rosbag.Bag(output_bag, 'w') as outbag: for topic, msg, t in rosbag.Bag(bag_file_1).read_messages(): # 假设第一个Bag作为基准,保持其原始时间不变 outbag.write(topic, msg, t) start_time = None for topic, msg, t in rosbag.Bag(bag_file_2).read_messages(): if not start_time: start_time = t adjusted_timestamp = t - start_time + rospy.Time(0) # 调整第二组数据的时间戳使其与第一组一致 outbag.write(topic, msg, adjusted_timestamp) sync_bags('path_to_first_bag.bag', 'path_to_second_bag.bag', 'output_synced.bag') ``` 此代码片段展示了如何遍历两个输入Bag文件并将它们写入新的已同步Bag文件中。对于第二个Bag文件中的每一则消息,计算相对于该Bag起始时刻的时间差并应用这个差异来更新消息头中的时间戳[^1]。 #### 方法二:使用第三方库或工具辅助完成更复杂场景下的时间同步 如果遇到更加复杂的同步需求,则可能需要借助专门设计用于解决此类问题的软件包或工具链。例如,针对特定传感器的数据流(如激光雷达扫描仪),可能会有专用算法来进行精确校准;而对于视觉惯导融合系统来说,像TUM RGB-D这样的公开数据集中提供的关联脚本也可以作为一个很好的起点[^3]。 此外,还可以考虑使用`rosbag play`命令行选项配合其他参数以达到更好的效果。比如,当播放多个Bag文件时,可以通过指定相同的开始时间和频率因子(`--clock`)使这些记录能够按照实际发生顺序重播出来[^2]。
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值