【ROS】如何将两个rosbag包合并成一个rosbag

参考:

  1. 如何将两个rosbag包合并或者提取rosbag包中某些话题到一个rosbag里
  2. https://github.com/udacity/self-driving-car/blob/master/image-localization/community-code/roboauto/scripts/bagmerge.py
  3. https://answers.ros.org/question/10683/is-there-a-way-to-merge-bag-files/

先上代码 merge_bag.py:

#!/usr/bin/env python
 
import sys
import argparse
from fnmatch import fnmatchcase
 
from rosbag import Bag
 
def main():
 
    parser = argparse.ArgumentParser(description='Merge one or more bag files with the possibilities of filtering topics.')
    parser.add_argument('outputbag',
                        help='output bag file with topics merged')
    parser.add_argument('inputbag', nargs='+',
                        help='input bag files')
    parser.add_argument('-v', '--verbose', action="store_true", default=False,
                        help='verbose output')
    parser.add_argument('-t', '--topics', default="*",
                        help='string interpreted as a list of topics (wildcards \'*\' and \'?\' allowed) to include in the merged bag file')
 
    args = parser.parse_args()
 
    topics = args.topics.split(' ')
 
    total_included_count = 0
    total_skipped_count = 0
 
    if (args.verbose):
        print("Writing bag file: " + args.outputbag)
        print("Matching topics against patters: '%s'" % ' '.join(topics))
 
    with Bag(args.outputbag, 'w') as o: 
        for ifile in args.inputbag:
            matchedtopics = []
            included_count = 0
            skipped_count = 0
            if (args.verbose):
                print("> Reading bag file: " + ifile)
            with Bag(ifile, 'r') as ib:
                for topic, msg, t in ib:
                    if any(fnmatchcase(topic, pattern) for pattern in topics):
                        if not topic in matchedtopics:
                            matchedtopics.append(topic)
                            if (args.verbose):
                                print("Including matched topic '%s'" % topic)
                        o.write(topic, msg, t)
                        included_count += 1
                    else:
                        skipped_count += 1
            total_included_count += included_count
            total_skipped_count += skipped_count
            if (args.verbose):
                print("< Included %d messages and skipped %d" % (included_count, skipped_count))
 
    if (args.verbose):
        print("Total: Included %d messages and skipped %d" % (total_included_count, total_skipped_count))
 
if __name__ == "__main__":
    main()
  • 将两个bag里所有topic合并到一个bag中:
python3 merge_bags.py -v total.bag 2_hq9570_rear_lcr_2022-04-21-20-27-17_filtered.bag 3_hq9570_rear_lcr_2022-04-21-20-38-41_filtered.bag  
# 就是把 2_hq9570_rear_lcr_2022-04-21-20-27-17_filtered.bag 和 3_hq9570_rear_lcr_2022-04-21-20-38-41_filtered.bag 完全合并在一起,时间戳打的都是原来两个bag里原始的时间戳,而不是像一边rosbag play一边rosbag record一样录的是现在这个时间戳

【ROS】如何将两个rosbag包合并成一个rosbag_第1张图片
【ROS】如何将两个rosbag包合并成一个rosbag_第2张图片

  • 将两个bag里某些topic合并到一个bag中:
python3 merge_bags.py -v --topic '/perception/camera/camera_obstacle /perception/lidar/percept_info_rviz /perception/radar/front/radar_obstacle /perception/radar/rear_right/radar_obstacle' total.bag 2_hq9570_rear_lcr_2022-04-21-20-27-17_filtered.bag 3_hq9570_rear_lcr_2022-04-21-20-38-41_filtered.bag 
# 就是把 2_hq9570_rear_lcr_2022-04-21-20-27-17_filtered.bag 里的 /perception/camera/camera_obstacle /perception/lidar/percept_info_rviz 和 3_hq9570_rear_lcr_2022-04-21-20-38-41_filtered.bag 里的 /perception/radar/front/radar_obstacle /perception/radar/rear_right/radar_obstacle 合并在一起

【ROS】如何将两个rosbag包合并成一个rosbag_第3张图片
【ROS】如何将两个rosbag包合并成一个rosbag_第4张图片
注意:运行以上合并bag的脚本之前需要先获取权限:sudo chmod +x merge_bags.py

你可能感兴趣的:(自动驾驶,人工智能,机器学习,ros)