ROS从.bag文件中读取消息

回放消息并查看输出

要先知道想从bag文件中读取的准确话题名。先看看.bag里有什么。终端用这个命令,手动检查所有已发布的话题,以及向每个话题发布了多少消息:

#不知道话题名
time rosbag info demo. Bag
#知道话题名
time rosbag info mybag.bag | grep -E "(topic1|topic2|topic3)"

打开一个终端,运行

roscore

打开另一个终端,订阅/obs1/gps/fix话题并复读该话题上发布的所有内容,同时用tee命令转储到一个yaml格式的文件中以便之后查看:

rostopic echo /obs1/gps/fix | tee topic1.yaml

再打开一个新终端,订阅另一个话题/diagnostics_agg。每个话题必须有自己的终端。

rostopic echo /diagnostics_agg | tee topic2.yaml

再打开另一个新终端来回放bag文件。尽可能快地回放bag文件(使用--immediate选项),只会发布感兴趣的话题。格式如下:

time rosbag play --immediate demo.bag --topics /topic1 /topic2 /topic3 /topicN

本例中,命令如下:
time rosbag play --immediate demo.bag --topics /obs1/gps/fix /diagnostics_agg

使用ros_readbagfile脚本轻松地提取感兴趣的话题

下载安装ros_readbagfile脚本

# Download the file
wget https://raw.githubusercontent.com/ElectricRCAircraftGuy/eRCaGuy_dotfiles/master/useful_scripts/ros_readbagfile.py
# Make it executable
chmod +x ros_readbagfile.py
# Ensure you have the ~/bin directory for personal binaries
mkdir -p ~/bin
# Move this executable script into that directory as `ros_readbagfile`, so that it will
# be available as that command
mv ros_readbagfile.py ~/bin/ros_readbagfile
# Re-source your ~/.bashrc file to ensure ~/bin is in your PATH, so you can use this
# new `ros_readbagfile` command you just installed
. ~/.bashrc

要先知道想从bag文件中读取的准确话题名。先看看.bag里有什么。终端用这个命令,手动检查所有已发布的话题,以及向每个话题发布了多少消息:

#不知道话题名
time rosbag info demo. Bag
#知道话题名
time rosbag info mybag.bag | grep -E "(topic1|topic2|topic3)"

然后使用ros_readbagfile,格式如下:

ros_readbagfile <mybagfile.bag> [topic1] [topic2] [topic3] [...]

要阅读上面方法1中显示的相同消息,请使用:

time ros_readbagfile demo.bag /obs1/gps/fix /diagnostics_agg | tee topics.yaml

注意

使用

rostopic echo -b

也可以读取.bag文件,但是速度感人,而且只能订阅一个消息,所以我们不选择

posted @ 2023-11-05 11:04  不想要名字  阅读(191)  评论(0编辑  收藏  举报