目录
目录
参考链接:
bag2mat github资源
一、rosbag使用
1、rosbag record使用
在开始记录rostopic之前,方便起见,先创建一个专门的文件夹,存放数据包:
mkdir ~/bagfiles
然后进入文件夹,开始录制数据包
cd ~/bagfiles
rosbag record -a
其中-a
参数等价于--all
,表示将当前发布的所有topic
数据都录制保存到一个.bag
文件中。也可以只记录某些感兴趣的topic
,命令如下:
rosbag record /topic1 /topic2 /topic3
上述命令录制的.bag
文件的名字为日期加时间。如果要指定生成数据包的名字,则用如下命令:
rosbag record -o fileName -a
rosbag record -O fileName.bag -a
-
-o PR
EFIX(小写o)等价于--output-prefix=PREFIX,只需指定文件名的前缀,其中PREFIX是需要自定义的文件名的前缀。
-
-O NAME(大写O)等价于--output-name=NAME,需指定前缀和后缀,其中NAME是需要自定义的包含前后缀的完整的文件名
2、rosbag info查看数据基本信息
1、查看rosbag包的基本信息,可以看到包长,消息个数,类型,topic名,msg名
rosbag info filename.bag
显示类似下面信息:
$ rosbag info 1.bag
path: 1.bag
version: 2.0
duration: 3.3s
start: Aug 24 2019 21:37:53.35 (1566653873.35)
end: Aug 24 2019 21:37:56.65 (1566653876.65)
size: 7.9 KB
messages: 20
compression: none [1/1 chunks]
types: geometry_msgs/Twist [9f195f881246fdfa2798d1d3eebca84a]
topics: /turtle1/cmd_vel 20 msgs : geometry_msgs/Twist
2、读取rosbag包指定 topic 的所有 msg 内容
rostopic echo -b square_cmd_1.bag /turtle1/cmd_vel
rostopic echo -b square_cmd_1.bag /turtle1/pose
3、按照时间片,截断rosbag包,生成一个新的短包
rosbag filter square_cmd_1.bag square_cmd_1_filter.bag "t.to_sec() > 1684977250.80 and t.to_sec() < 1684977450.45"
3、rosbag play
回访数据包中的topic:
rosbag play <bagfile>
开始播放立刻暂停,按空格继续:
rosbag play --pause bag_name.bag
如果只播放感兴趣的 topic
,则用命令:
rosbag play <bagfile> --topic /topic_name1
#或者
rosbag play bag_name.bag --topics /topic_name1 /topic_name2
在上述播放命令执行期间,空格键可以暂停播放
4、用rostopic echo命令转换格式
提取bag文件中的数据并保存为csv格式,提取topic的标准格式为:
rostopic echo -b <BAGFILE> -p <TOPIC> > <output>.csv
# <BAGFILE>是bag文件,<TOPIC>为数据所在的topic
rostopic echo
命令一次只能提取一个topic
。
# save into .csv files
rostopic echo -b rosbag.bag -p /topic/sub_topic1 > topic1.csv
# save into .txt files
rostopic echo -b rosbag.bag -p /topic/sub_topic1 > topic1.txt
5、可视化显示工具:rqt_bag
提供全方位的图形化界面,具有录制,播放,显示,保存,加载文件,暂停等功能:
rqt_bag bag_name.bag
二、bag2mat使用
1、GitHub资源下载
GitHub - neufieldrobotics/rosbag_toolkit
进入编辑工作空间的src子目录下载
cd ~/catkin_ws/src/
git clone https://github.com/neufieldrobotics/rosbag_toolkit.git
根据github bag2mat提示,安装相关python库
rosbag
roslib
scipy
argparse
yaml
utm
logging
命令为pip install scipy
然后退回到工作空间目录,编译
cd catkin_ws/
catkin_make
2、运行bag2mat
将配置文件test_config.yaml和字典文件msg_dict.yaml放入bagfiles文件夹中,其中配置文件中使用自己需要的话题:-[topic name, message type, name for variable in matfile]
运行命令:
rosrun bag2mat bag2matpy -c test_config.yaml -d msg_dict.yaml -i bagname.bag -o matname.mat
具体更多命令可以参照GitHub原文