当前位置:网站首页>【ROS进阶篇】第六讲 ROS中的录制与回放(rosbag)
【ROS进阶篇】第六讲 ROS中的录制与回放(rosbag)
2022-07-03 02:15:00 【生如昭诩】
【ROS进阶篇】第六讲 ROS中的录制与回放(rosbag)

前言
在进阶篇的ROS教程中,我们计划推出对于一些功能组件的介绍与使用,在之前的博客我们重新深入领略了TF2功能包的使用方法,为今后机器人的学习奠定了基础,本篇则相对轻松,重点介绍ROS中完成数据记录与回放的工具:rosbag。
一、工具介绍
- 概念:ROS系统中用于实现录制和回放ROS话题的工具集;
- 本质:rosbag其实本质上就是一个ROS订阅或发布节点;
记录数据:实时订阅话题信息并将数据写入文件,
回放数据:读取文件,发布文件中的数据信息.
作用:数据复用,即采集数据方便延后分析调试,对于未来传感器数据的分析有益;
参考网站:ROS Wiki ROSBAG
二、常用命令行指令
- 根据录制及回放步骤介绍:
- 准备工作环境:
创建录制文件目录:mkdir ./xxx和cd xxx- 开始录制:
rosbag record -a -O 目标文件
- 查看文件:
rosbag info 文件名
- 回放文件:
rosbag play 文件名
三、编程实现
本节内容主要介绍使用C++语言编程实现rosbag的录制与回放过程:
- 录取rosbag文件:
#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;
}
- 读取rosbag文件:
/* 读取 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;
}
总结
- 声明:本节博客部分参考了CSDN用户赵虚左的ROS教程,从下篇博客起ROS进阶篇的教程将会首先转入机器人的一些知识学习,在掌握基本机器人模型的建立方法后,博主一方面将会介绍一些关于四足机器人的知识,另一方面则会从URDF文件和GAZEBO仿真入手,继续完成对于ROS的学习,敬请期待。

边栏推荐
- In the face of difficult SQL requirements, HQL is not afraid
- Current situation and future of Web3 in various countries
- Distributed transaction solution
- Servlet中数据传到JSP页面使用el表达式${}无法显示问题
- 微服务组件Sentinel (Hystrix)详细分析
- [shutter] hero animation (hero realizes radial animation | hero component createrecttween setting)
- RestCloud ETL 跨库数据聚合运算
- Swift开发学习
- Job object of collaboration in kotlin
- Groovy, "try with resources" construction alternative
猜你喜欢

PyTorch 卷积网络正则化 DropBlock

What are the differences between software testers with a monthly salary of 7K and 25K? Leaders look up to you when they master it
![[shutter] top navigation bar implementation (scaffold | defaulttabcontroller | tabbar | tab | tabbarview)](/img/f1/b17631639cb4f0f58007b86476bcc2.gif)
[shutter] top navigation bar implementation (scaffold | defaulttabcontroller | tabbar | tab | tabbarview)

Wechat applet Development Tool Post net:: Err Proxy Connexion Problèmes d'agent défectueux

RestCloud ETL 跨库数据聚合运算

Job object of collaboration in kotlin
![[camera topic] how to save OTP data in user-defined nodes](/img/3e/b76c4d6ef9ab5f5b4326a3a8aa1c4f.png)
[camera topic] how to save OTP data in user-defined nodes

SPI mechanism

详细些介绍如何通过MQTT协议和华为云物联网进行通信

The testing process that software testers should know
随机推荐
Storage basic operation
微信小程序开发工具 POST net::ERR_PROXY_CONNECTION_FAILED 代理问题
Bottleneck period must see: how can testers who have worked for 3-5 years avoid detours and break through smoothly
Deep learning notes (constantly updating...)
What are MySQL locks and classifications
iptables 4层转发
Pytorch convolution network regularization dropblock
力扣(LeetCode)183. 从不订购的客户(2022.07.02)
Cfdiv2 Fixed Point Guessing - (2 points for Interval answer)
Cfdiv2 fixed point guessing- (interval answer two points)
Leetcode(540)——有序数组中的单一元素
CFdiv2-Fixed Point Guessing-(區間答案二分)
My creation anniversary
y54.第三章 Kubernetes从入门到精通 -- ingress(二七)
Wechat applet Development Tool Post net:: Err Proxy Connexion Problèmes d'agent défectueux
转载收录6.5大侠写的部分Qt开发经验
2022 spring "golden three silver four" job hopping prerequisites: Software Test interview questions (with answers)
How do browsers render pages?
What are the key points often asked in the redis interview
Comment le chef de file gère - t - il l'équipe en cas d'épidémie? Contributions communautaires