使用ros_readbagfile脚本高效提取bag中的topic数据

特点:高效、不需要运行ros环境甚至是roscore、可选取任意多个关注的topic数据。


1 准备ros_readbagfile

(如果下载链接无效可复制源码

#下载脚本文件
wget https://raw.githubusercontent.com/ElectricRCAircraftGuy/eRCaGuy_dotfiles/master/useful_scripts/ros_readbagfile.py
# 赋予执行权限
chmod +x ros_readbagfile.py
# 创建bin路径
mkdir -p ~/bin
# 将脚本放入bin文件夹中 并更改成ros_readbagfile的脚本名
mv ros_readbagfile.py ~/bin/ros_readbagfile
# resource环境脚本
. ~/.bashrc

执行如果出现bug:
import yaml ImportError: No module named 'yaml'
可以通过
sudo pip3 install pyyaml
解决缺少的依赖


2 使用

获取所需的topic名称

rosbag info ~/subset.bag 
path:        /home/greg/subset.bag
version:     2.0
duration:    1.7s
start:       Apr 28 2021 19:42:52.73 (1619664172.73)
end:         Apr 28 2021 19:42:54.46 (1619664174.46)
size:        7.2 KB
messages:    13
compression: none [1/1 chunks]
types:       geometry_msgs/Twist [9f195f881246fdfa2798d1d3eebca84a]
topics:      /turtle1/cmd_vel   13 msgs    : geometry_msgs/Twist

如上,录制的topic为/turtle1/cmd_vel

如为默认Python版本为Python3 则可以直接使用:
ros_readbagfile <mybagfile.bag> [topic1] [topic2] [topic3] [...]
如默认版本为Python2,则用:
python ~/bin/ros_readbagfile.py ~/subset.bag /turtle1/cmd_vel
获得部分的输出为:

python ~/bin/ros_readbagfile.py ~/subset.bag /turtle1/cmd_vel
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Scanning ROS bag file "/home/greg/subset.bag"
# for the following topics:
#    /turtle1/cmd_vel
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

# =======================================
# topic:           /turtle1/cmd_vel
# msg_count:       1
# timestamp (sec): 1619664172.732249737 # - - -
linear: 
  x: 2.0
  y: 0.0
  z: 0.0
angular: 
  x: 0.0
  y: 0.0
  z: 0.0

# =======================================

如果需要保存成文件可以使用命令:

python ~/bin/ros_readbagfile.py ~/subset.bag /turtle1/cmd_vel |tee out.yaml

其中tee命令是正常屏幕输出并且将屏幕输出保存到文件中。


如果过程中遇到问题可以给我留言


版权声明:本文为weixin_42269667原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。