rosbag 录制命令:
rosbag record -a -o turtle.bag
rosbag 查看命令
rosbag info turtle_2021-08-27-17-24-19.bag
rosbag 回放命令
rosbag play turtle_2021-08-27-17-24-19.bag
rosbag 写的C++编码实现
#include "ros/ros.h"
#include "rosbag/bag.h"
#include "std_msgs/String.h"
/*
需求:使用 rosbag 向磁盘文件写出数据(话题+消息)
流程:
1.导包
2,初始化
3.创建 rosbag 对象
4.打开文件流
5.写数据
6.关闭文件流
*/
int main(int argc, char *argv[])
{
//2,初始化
setlocale(LC_ALL,"");
ros::init(argc,argv,"bagW");
ros::NodeHandle nh;
// 3.创建 rosbag 对象
rosbag::Bag bag;
// 4.打开文件流
bag.open("hello.bag",rosbag::bagmode::Write);
// 5.写数据
std_msgs::String msg ;
msg.data = "hello xxxxxxx";
//参数1 话题 参数2 时间辍 参数3 消息
bag.write("/chatter",ros::Time::now(),msg);
// 6.关闭文件流
bag.close();
return 0;
}
rosbag 读的C++编码实现
#include "ros/ros.h"
#include "rosbag/bag.h"
#include "std_msgs/String.h"
#include "rosbag/view.h"
int main(int argc, char *argv[])
{
setlocale(LC_ALL,"");
ros::init(argc,argv,"bagR");
ros::NodeHandle nh;
rosbag::Bag bag;
bag.open("hello.bag",rosbag::BagMode::Read);
//取出话题时间戳消息和内容
//先获取消息的集合,再迭代取出消息
for (auto && m : rosbag::View(bag))
{
//解析
std::string topic = m.getTopic();
ros::Time time = m.getTime();
std_msgs::StringConstPtr info = m.instantiate<std_msgs::String>();
ROS_INFO("topic: %s,时间戳:%.2f,消息:%s",
topic.c_str(),
time.toSec(),
info->data.c_str()
);
}
bag.close();
return 0;
}
rosbag 写的python编码实现
import rospy
import rosbag
from std_msgs.msg import String
if __name__ == "__main__":
msg = String()
msg.data = "hellpython"
rospy.init_node("bagW")
bag = rosbag.Bag("hello1.bag",mode='w')
bag.write("/mywrite",msg)
bag.write("/mywrite",msg)
bag.write("/mywrite",msg)
bag.close()
pass
rosbag 读的python编码实现
import rospy
import rosbag
from rospy import topics
from std_msgs.msg import String
if __name__ =="__main__":
rospy.init_node("bagR")
bag = rosbag.Bag("hello1.bag",mode='r')
msgs = bag.read_messages()
for top,index,tim in msgs:
rospy.loginfo("话题:{},内容{},时间{}".format(top,index,tim))
bag.close()
pass
|