一、使用rostopic記錄數據
$ rostopic list #查看所有topic列表
$ mkdir bagfile #新建一個bagfile文件夾存放生成的rosbag文件
$ cd bagfile
$ rosbag record -a #記錄所有topic數據,生成rosbag數據包
$ rosbag record -O rosbag_name /topic1 /topic2
只記錄指定topic數據,-O指定要保存的rosbag文件名,rosbag_name即爲生成的rosbag文件名;若爲-o,則生成的rosbag文件名後面自動添加年月日時間
(1) 可在bagfile文件中查看生成的.bag數據包;若出現.bag.active文件,則表示正在記錄,使用ctrl+c可終止保存數據;
(2) rosbag在記錄數據時相當於一個subscribe程序
二、查看rosbag數據
$ rostopic echo /topic
可在終端直接打印指定topic的數據信息
$ rosbag info mybagfile.bag #查看rosbag數據包基本信息
三、將rosbag包轉化爲txt文件
$ rostopic echo -b mybagfile.bag -p /topic > mytxtfile.txt
將mybagfile.bag文件中指定/topic的消息轉化爲txt文檔,並存爲mytxtfile.txt。
四、回放rosbag數據包
$ rosbag play mybagfile.bag
其中,Bag Time爲rosbag數據包收到每一則消息時的時間。
(1) rosbag在回放數據時相當於一個publisher程序;
(2) 重新運行訂閱了bag包中發佈的topic程序,即可接收到對應topic的信息。
使用rviz回放rosbag數據包可視化:
$ rosbag play -d 10 mybagfile.bag
-d 10: 指定延時10秒回放;打開rviz訂閱topic即可在場景顯示窗口看到可視化信息。
注:rviz中訂閱rosbag數據包中的topic,需要先發布rosbag數據包,才能在rviz中搜索到topic,一旦rosbag數據包發佈完畢,則topic再次消失
五、.bag.active恢復爲.bag:
$ cd bagfile
$ rosbag reindex mybagfile,bag.active
$ rosbag fix mybagfile.bag.active resultname.bag