系列文章目录
提示:这里可以添加系列文章的所有文章的目录,目录需要自己手动添加
TODO:写完再整理
文章目录
前言
认知有限,望大家多多包涵,有什么问题也希望能够与大家多交流,共同成长!本文先对**rosbag(操作过程录制)**做个简单的介绍,具体内容后续再更,其他模块可以参考去我其他文章
提示:以下是本篇文章正文内容
一、rosbag作用与原理
rosbag本质也是ros的节点
1)当录制时,rosbag是一个订阅节点,可以订阅话题消息并将订阅到的数据写入磁盘文件;
2)当重放时,rosbag是一个发布节点,可以读取磁盘文件,发布文件中的话题消息。
用于录制和回放 ROS 主题的一个工具集。实现了数据的复用,方便调试、测试。
rosbag是采集记录实物机器人运动的话题数据的,所以实机仅仅需要运行一次就好,后面就可以在仿真里面搞了
减少调试的时间和难度(一般是记录传感器发布的数据话题,控制指令话题可以不采集)
二、rosbag命令行实现步骤【一般用指令存放数据】
1.创建目录保存录制的文件
cd ~/bagfiles
2.录制文件–rosbag record
rosbag record -a
-a 选项表示将当前发布的所有 topic 数据都录制保存到一个 rosbag 文件中,录制的数据包名字为日期加时间。
rosbag record /topic_name1 /topic_name2 /topic_name3
只记录某些感兴趣的 topic
rosbag record -O bag_name.bag /topic_name #为文件命名
rosbag record -o bag_name /topic_name #为文件名字做前缀
指定生成数据包的名字,则用-O /-o 参数
rosbag record --duration=30 /topic_name #持续30s录制,还可以指定m,h,和--split类似
rosbag record --split --size=1024 /topic_name1 #空间达到1024M后分文件存储
rosbag record --split --duration=30 /topic_name1 #持续时间到30s后分文件存储
rosbag record --split --duration=5m /topic_name1 #持续时间到5m后分文件存储
rosbag record --split --duration=2h /topic_name1#持续时间到2h后分文件存储
rosbag record -l 1000 /topic_name1 # 录制该主题1000个消息限制
其他录制信息
<node pkg="rosbag" type="record" name="bag_record" args="/topic1 /topic2"/>
如果在 launch 文件中使用 rosbag record 命令
3.查看文件–rosbag info
rosbag info 文件名
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
以yaml形式显示话题信息
rosbag info bag_name.bag -y
#或者
rosbag info bag_name.bag --yaml
$ rosbag info -y 1.bag
path: 1.bag
version: 2.0
duration: 3.295966
start: 1566653873.351150
end: 1566653876.647117
size: 8103
messages: 20
indexed: True
compression: none
types:
- type: geometry_msgs/Twist
md5: 9f195f881246fdfa2798d1d3eebca84a
topics:
- topic: /turtle1/cmd_vel
type: geometry_msgs/Twist
messages: 20
以频率形式显示包中的话题信息:
rosbag info bag_name.bag --freq
4.回放文件–rosbag play
回放数据包中的 topic:
rosbag play 文件名
rosbag play <bagfile>
开始播放立刻暂停,按空格继续:
rosbag play --pause bag_name.bag
如果想改变消息的发布速率,可以用下面的命令,-r 后面的数字对应播放速率:
rosbag play -r 2 <bagfile>
rosbag 循环播放,可以用命令
rosbag play -l <bagfile>
如果只播放感兴趣的 topic ,则用命令
rosbag play <bagfile> --topic /topic_name1
#或者
rosbag play bag_name.bag --topics /topic_name1 /topic_name2
在上述播放命令执行期间,空格键可以暂停播放。
指定播放起始点
rosbag play bag_name.bag -s 5 #从5s的地方开始播放
指定播放时长
rosbag play bag_name.bag -u 250 #播放250s信息
发布时钟
默认/clock话题是没有消息的
rosbag play --clock bag_name.bag #默认/clock的话题频率100hz
rosbag play --clock --hz 200 bag_name.bag #指定/clock的时钟为200hz
5.压缩和解压缩命令:compress&decompress
压缩
使用压缩命令,会将原来的bag备份并重命名为.orig.bag。
#默认bz2格式压缩,特点:压缩数据小,但是慢
rosbag compress bag_name.bag
rosbag compress -j bag_name.bag
#lz4格式压缩,特点:压缩快,但是被压缩后的文件稍大
rosbag compress --lz4 bag_name.bag
解压缩
rosbag decompress bag_name.bag
.
.
三、rosbag编程实现步骤【源码读取数据形式】
1.C++实现
1.写 bag
#include "ros/ros.h"
#include "rosbag/bag.h"
#include "std_msgs/String.h"
int main(int argc, char *argv[])
{
ros::init(argc,argv,"bag_write");
ros::NodeHandle nh;
//创建bag对象
rosbag::Bag bag;
//打开
bag.open("/home/rosdemo/demo/test.bag",rosbag::BagMode::Write);
//写
std_msgs::String msg;
msg.data = "hello world";
bag.write("/chatter",ros::Time::now(),msg);
bag.write("/chatter",ros::Time::now(),msg);
bag.write("/chatter",ros::Time::now(),msg);
bag.write("/chatter",ros::Time::now(),msg);
//关闭
bag.close();
return 0;
}
2.读bag
#include "ros/ros.h"
#include "rosbag/bag.h"
#include "rosbag/view.h"
#include "std_msgs/String.h"
#include "std_msgs/Int32.h"
int main(int argc, char *argv[])
{
setlocale(LC_ALL,"");
ros::init(argc,argv,"bag_read");
ros::NodeHandle nh;
//创建 bag 对象
rosbag::Bag bag;
//打开 bag 文件
bag.open("/home/rosdemo/demo/test.bag",rosbag::BagMode::Read);
//读数据
for (rosbag::MessageInstance const m : rosbag::View(bag))
{
std_msgs::String::ConstPtr p = m.instantiate<std_msgs::String>();
if(p != nullptr){
ROS_INFO("读取的数据:%s",p->data.c_str());
}
}
//关闭文件流
bag.close();
return 0;
}
.
.
2.Python实现
1.写 bag
#! /usr/bin/env python
import rospy
import rosbag
from std_msgs.msg import String
if __name__ == "__main__":
#初始化节点
rospy.init_node("w_bag_p")
# 创建 rosbag 对象
bag = rosbag.Bag("/home/rosdemo/demo/test.bag",'w')
# 写数据
s = String()
s.data= "hahahaha"
bag.write("chatter",s)
bag.write("chatter",s)
bag.write("chatter",s)
# 关闭流
bag.close()
2.读bag
#! /usr/bin/env python
import rospy
import rosbag
from std_msgs.msg import String
if __name__ == "__main__":
#初始化节点
rospy.init_node("w_bag_p")
# 创建 rosbag 对象
bag = rosbag.Bag("/home/rosdemo/demo/test.bag",'r')
# 读数据
bagMessage = bag.read_messages("chatter")
for topic,msg,t in bagMessage:
rospy.loginfo("%s,%s,%s",topic,msg,t)
# 关闭流
bag.close()