如何将两个rosbag包合并或者提取rosbag包中某些话题到一个rosbag里

代码叫做merge_bag.py

运行的时候

python merge_bag.py -v 1028msf.bag msf.bag  vinReNoOutlier.bag  就把msf.bag和vinReNoOutlier.bag完全合并在一起了,时间戳打的都是原来两个bag里原始的时间戳,而不是像一边rosbag play一边rosbag record一样录的是现在这个时间戳

python merge_bag.py -v --topics '/aft_mapped_to_init /gnss/data /imu/data vinReNoOutlier' msf.bag 2019-10-28-14-40-45.bag vinReNoOutlier.bag 就把2019-10-28-14-40-45.bag里的/aft_mapped_to_init /gnss/data /imu/data和vinReNoOutlier.bag里的vinReNoOutlier合并在了一起,注意一定不能在vinReNoOutlier在/,因为本身vinReNoOutlier.bag里的话题就是vinReNoOutlier,没有/

最后附上代码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()

 

  • 7
    点赞
  • 48
    收藏
    觉得还不错? 一键收藏
  • 16
    评论
评论 16
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值