ROS1 rosbag的详细使用,并且使用python来合并bag包

在使用ros的时候经常会用到rosbag来录制或者回放算法,是个非常有用的工具。

rosbag 命令列表

命令作用
record录制一个包,并且指定topic
info总结一个包的详细信息
play回放一个或者多个包,并且可以指定topic
check确定一个包是否可以在当前系统中播放,或者是否可以迁移
fix修复一个包使得它能在当前系统中播放
filter通过python脚本转换包内容
compress压缩包
decompress解压缩包
reindex重新索引一个或者多个损坏的包

record 命令使用

  • record <topic-names> # 指定一个或者多个topic来录制
$ rosbag record body_status gnss_imu
  • record -a, --all # 录制所有topic
$ rosbag record -a
  • record -e, --regex # 录制使用正则表达式匹配的topic
$ rosbag record -e "/(.*)_log/point"
  • rocord -p, -publish # (Melodic 新特性)当开始录制包时,发布一个topic=”begin_write“
$ rosbag record -p
  • record -x, --exclude # 录制时排除正则表达式匹配的topic
$ rosbag record -e "/(.*)_log/point" -x "(.*)/point"
  • record -q, --quiet # 录制时抑制指定topic的log输出
$ rosbag record -q /body_status
  • record -d, --duration # 指定topic录制最大时常
$ rosbag record -d 30 /body_status
$ rosbag record -d 3m /body_status
$ rosbag record -d 3h /body_status
  • record -o, --output-prefix # 输出包名前加上前缀
$ rosbag record -o output /topic_name /topic_name2 
  • record -O, --output-name # 指定输出包名
$ rosbag record -O output.bag /topic_name /topic_name2 
  • record --split # 每隔设定的时常或者大小分割包录制
$ rosbag record --split --size=1024 /topic_name
$ rosbag record --split --duration=30 /topic_name
$ rosbag record --split --duration=5m /topic_name
$ rosbag record --split --duration=2h /topic_name
  • record --max-splits # (Kinetic 新特性)分割包的最大个数
$ rosbag record --split --max-splits 3 --duration=30 /topic_name
  • record -b, --buffsize # 使用内部缓冲区(默认值:256MB,无限为0)
$ rosbag record -b 1024 /topic_name
  • record --chunksize # 开辟缓冲区,比buffsize更为先进(默认值: 768KB,无限为0)
$ rosbag record --chunksize=1024 /topic_name
  • record -l, --limit # 在topic中只录制指定数量的消息
$ rosbag record -l 1000 /topic_name
  • record --node # 记录指定节点订阅的所有消息
$ rosbag record --node=/node
  • record -j, --bz2 # 设置录制以bz2格式压缩
$ rosbag record -j /topic_name
  • record --lz4 # 设置录制以lz4格式压缩
$ rosbag record --lz4 /topic_name
  • record --tcpnodelay # 订阅主题时使用TCP_NODELAY传输 参考资料
  • record --udp # 订阅主题时使用UDP传输

info 指令使用

  • info <bag-files> # 查看一个或者多个包的详细信息
$ rosbag info test.bag
  • info -y, --yaml # 以yaml格式输出一个或者多个包的详细信息
$ rosbag info -y test.bag
  • info -k, --key # 查看以yaml格式输出,包内key值对应的数据
$ rosbag info -y -k topics test.bag

play 指令使用

  • play <bag_name> # 播放一个或者多个数据包
$ rosbag play test.bag
  • play -p, --prefix # 为所有输出主题添加前缀
$ rosbag play -p test test.bag
  • play -q, --quiet # 抑制控制台输出
$ rosbag play -q test.bag
  • play -i, --immediate # 播放包不等待
$ rosbag play -i test.bag
  • play --pause # 开始播放包时暂停
$ rosbag play --pause test.bag
  • play --queue # 播放包时以设置的队列大小播放 默认值为100
$ rosbag play --queue=1000 test.bag
  • play --clock # 自动发布一个clocktopic
$ rosbag play --clock test.bag
  • play --hz # 以指定赫兹发布clock topic
$ rosbag play --hz=1 --clock test.bag
  • play -d, --delay # 指定延迟播放的时间
$ rosbag -d 5 test.bag
  • play -r, --rate # 指定播放速度
$ rosbag -r 10 test.bag
  • play -s, --start # 指定开始播放时间
$ rosbag play -s 5 test.bag 
  • play -u,--duration # 设定播放时间
$ rosbag play -u 240 test.bag
  • play --skip-empty # 设置跳过没有消息的数据的时间
$ rosbag play --skip-empty=1 test.bag 
  • play -l, --loop # 循环播放
$ rosbag play -l test.bag
  • play -k, --keep-alive # 播放时保持活跃状态
$ rosbag play -k test.bag
  • play --try-future-version # 即使不知道版本号也可以打开bag
$ rosbag play --try-future-version test.bag
  • play --topics # 指定对应topics进行播放
$ rosbag play test.bag --topics /topic1 /topic2
  • play --pause-topics # 当回放数据时,暂停指定topics
$ rosbag play test.bag --topics /topic1 /topic2 --pause-topics
  • play --bags # 指定多个bags进行回放
$ rosbag play --bags=test.bag 
  • play --wait-for-subscribers # 再发布之前,等待每个主题至少有一个订阅者
  • play --rate-control-topic= # 观看给定的主题,如果上一次发布时间超过了<速率控制最大延迟>,请等待该主题再次发布后再继续播放
  • play --rate-control-max-delay= # 暂停前与<速率控制主题>的最大时间差

check 指令使用

  • check <file> # 确定一个包在当前系统中是否可以播放。
$ rosbag check test.bag
  • check -g, --genrules # 生成一个名为RULEFILE的迁移规则文件
$ rosbag check -g diagnostics.bmr test.bag
  • check -a, --append # 加载后附加到现有数据迁移规则文件的末尾。
$ rosbag check -a -g diagnostics.bmr test.bag
  • check -n, --noplugins # 不加载规则文件
$ rosbag check -n test.bag

fix 指令使用

  • fix <in-bag> <out-bag> [rules.bmr] # 使用规则文件修复数据包
$ rosbag fix old.bag repaired.bag myrules.bmr
  • fix -n, --noplugins # 修复数据包不带规则文件
$ rosbag fix -n old.bag repaired.bag

filter 指令使用

  • filter <in-bag> <out-bag> <expression> # 使用给定的Python表达式转换数据文件。
$ rosbag filter my.bag only-tf.bag "topic == '/tf'"

compress 指令使用

  • compress <bag-files> # 使用BZ2格式压缩包
$ rosbag compress *.bag
  • compress --output-dir=DIR # 指定路径输出文件
$ rosbag compress *.bag --output-dir=compressed
  • compress -f, --force # 如果已经存在包,则强制覆盖
$ rosbag compress -f *.bag
  • compress -q, --quiet # 抑制非关键信息
$ rosbag compress -q *.bag
  • compress -j, --bz2 # 使用bz2格式压缩包
$ rosbag compress -j *.bag
  • compress --lz4 # 使用lz4格式压缩包
$ rosbag compress --lz4 *.bag

decompress 指令使用

  • decompress <bag-files> # 解压缩包
$ rosbag decompress *.bag
  • decompress --output-dir=DIR # 指定解压缩的路径
$ rosbag decompress --output-dir=uncompressed *.bag
  • decompress -f, --force # 如果已经存在包,则强制覆盖
$ rosbag decompress -f *.bag
  • decompress -q, --quiet # 抑制非关键信息
$ rosbag decompress -q *.bag

reindex 指令使用

  • reindex <bag-files> # 修复坏包
$ rosbag reindex *.bag
  • reindex --output-dir=DIR # 指定路径
$ rosbag reindex --output-dir=reindexed *.bag
  • reindex -f, --force # 如果已经存在包,则强制覆盖
$ rosbag reindex -f *.bag
  • reindex -q, --quiet # 抑制非关键信息
$ rosbag reindex -q *.bag

2. 使用Python合并包代码

#! /usr/bin/env python3
import os
import time
import argparse
from rosbag import Bag, Compression

def parse_compression(compression):
    if compression == "none" or compression == "NONE":
        compression = Compression.NONE
    elif compression == "bz2":
        compression = Compression.BZ2
    elif compression == "lz4":
        compression = Compression.LZ4
    return compression

def get_files_list(arg_input, arg_select):
    files = None
    if " " in arg_input:
        files = arg_input.split(" ")
    elif "," in arg_input:
        files = arg_input.split(",")
    elif os.path.exists(args.input) or "*" in args.input:
        rfind_index = arg_input.rfind("/")
        dir_path = arg_input[:rfind_index]
        files = os.listdir(dir_path)
        files.sort() # 名称排序
        files = [dir_path + "/" + file for file in files]
    
    if arg_select:
        files = select_bags(files)
    
    print("bags list:")
    for i in range(len(files)):
        print(str(i) + "." + files[i])

    return files

def select_bags(files):
    for i in range(len(files)):
        print(str(i) + "." + files[i])
    print("Please input bag numbers to merge. split by , or space", end=":")
    s_b = input()
    s_b_list = []
    if "," in s_b:
        s_b_list = s_b.split(",")
    elif " " in s_b:
        s_b_list = s_b.split(" ")
    files = [files[int(x)] for x in s_b_list]
    return files


def show_process_bar(total, i, start):
    a = "*" * i
    b = "." * (total - i)
    c = (i / total) * 100
    dur = time.perf_counter() - start
    print("\r{:^3.0f}%[{}->{}]{:.2f}s".format(c,a,b,dur), end="")

def merge_bags(args):
    print("start merge bags.")
    compression = parse_compression(args.compression)
    print(f"bag's compression mode is {compression}.")
    files = get_files_list(args.input, args.select)
    with Bag(args.output, "w",  compression=compression) as o:
        start = time.perf_counter()
        for i in range(len(files)):
            show_process_bar(len(files), i+1, start)
            with Bag(files[i], "r") as ib:
                for topic, msg, t in ib:
                    o.write(topic, msg, t)
            show_process_bar(len(files), i+1, start)
            

if __name__ == "__main__":
    parser = argparse.ArgumentParser(description="Merge one or more bag files to one file.")
    parser.add_argument("-o", "--output", help="Output bag's file path.",
                        default="output.bag", required=True)

    parser.add_argument("-i", "--input", help="Input files bags name or path, split by , .",
                        required=True)
    
    parser.add_argument("-c", "--compression", help="Compress the bag by bz2 or lz4",
                        default="lz4", choices=["none", "lz4", "bz2"])
    
    parser.add_argument("-s", "--select", help="Select bags to merge.",
                        default=False)

    parser.add_argument("-v", "--verbose", help="Show the verbose msg.")

    args = parser.parse_args()
    merge_bags(args)        


Reference:

http://wiki.ros.org/rosbag/Commandline

  • 0
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
ROS机器⼈技术 机器⼈技术-rosbag详细使⽤教程 详细使⽤教程! 在 ROS 系统中,可以使⽤ bag ⽂件来保存和恢复系统的运⾏状态,⽐如录制雷达和相机话题的 bag ,然后回放⽤来进⾏联合外参标定。 这⾥记录下我学习官⽅的 rosbag 教程的笔记: 我常⽤的⼏个操作 我常⽤的⼏个操作 虽然命令很多,但是我⽬前在⼯作中常⽤的命令就如下⼏个: 1. 录 录制所有话题: rosbag record -a 录制指定话题,设置 bag 名: rosbag record -O bag_name.bag /topic1_name /topic2_name /xxx 有时候我录制不设置名称,默认按照录制结束时间命名: rosbag record /topic1_name /topic2_name /xxx 2. 回放 回放 我常⽤的是以暂停的⽅式启动,防⽌跑掉数据: rosbag play --pause record.bag 你也可以直接回放: rosbag play record.bag 我建图过程中有时会设置以 0.5 倍速回放,也就是以录制频率的⼀半回放: rosbag play -r 0.5 record.bag 回放完后,我⼀般会⽤ rostopic list 查看下发布的主题,确保是我需要的: rostopic list 3. 修复 修复 之前在我们⼩车的 Intel NUC 上录拷到台式机上回放会报错,提⽰需要 reindex,执⾏⼀下即可,不过数据好像会少⼀些: rosbag reindex xxx.bag 下⾯是完整的 rosbag ⽤法,需要的可以查找下。 ⼀、 ⼀、rosbag 基本作⽤ 基本作⽤ rosbag ⼯具可以录制⼀个、从⼀个或多个中重新发布消息、查看⼀个的基本信息、检查⼀个的消息定义,基于 Python 表达式过滤 ⼀个的消息,压缩和解压缩⼀个以及重建⼀个的索引。 rosbag ⽬前常⽤的命令如下(fix 和 filter 暂时没有⽤到): record:⽤指定的话题录制⼀个 bag info:显⽰⼀个 bag 的基本信息,⽐如含哪些话题 play:回放⼀个或者多个 bag check:检查⼀个 bag 在当前的系统中是否可以回放和迁移 compress:压缩⼀个或多个 bag decompress:解压缩⼀个或多个 bag reindex:重新索引⼀个或多个损坏 bag 我在⽬前项⽬的使⽤中,⽤的最多的就是 record、info 以及 play 功能,先录制想要的话题录制完毕检查下的信息,最后再回放作为 算法的输⼊,下⾯就详细学习下上⾯命令的⽤法。 ⼆、 ⼆、rosbag record 使⽤ record 订阅指定主题并⽣成⼀个 bag ⽂件,其中含有关这些主题的所有消息,常见⽤法如下。 ⽤指定的话题 topic_names 来录制 bag rosbag record <topic_names> ⽐如录制 rosout、tf、cmd_vel 3 个话题,录制bag 以时间命名: rosbag recoed rosout tf cmd_vel 录制完保存 bag 名称为 session1 + 时间戳.bag 格式: rosbag record -o session1 /chatter 录制完保存为指定⽂件名 session2_090210.bagrosbag record -O session2_090210.bag /chatter 录制系统中所有的话题: rosbag record -a 使⽤ -h 查看 record 使⽤⽅法,很多命令都可以⽤这个: rosbag record -h 三、 三、rosbag info rosbag info 显⽰⽂件内容的可读摘要,括开始和结束时间,主题及其类型,消息计数、频率以及压缩统计信息,常见⽤法如下: 显⽰⼀个 bag 的信息: rosbag info name.bag $ rosbag info foo.bag path: foo.bag version: 2.0 duration: 1.2s start: Jun 17 2010 14:24:58.83 (1276809898.83) end: Jun 17 2010 14:25:00.01 (1276809900.01) size: 14.2 KB messages: 119 compression: none [1/1 chunks] types: geometry_msgs/Point [4a842b65f413084dc2b10fb484ea7f17] topics: /points 119 msg

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

锡城筱凯

你的鼓励是我创造的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值