Article directory
Five, ROS common components
2.rosbag
In ROS, a special tool is provided for data retention and reading implementation: rosbag;
concept:
A toolset for recording and playback of ROS topics.
effect:
Data reuse is realized, which is convenient for debugging and testing
Nature:
The essence of rosbag is also a node of ros. When recording, rosbag is a subscription node, which can subscribe topic messages and write the subscribed data to disk files; when playing back, rosbag is a publishing node, which can read disk files and publish files topic news;
2.1 rosbag use - command line
Requirements: ROS built-in turtle, use rosbag to record the operation process, and realize replay after recording.
accomplish:
1. Create a directory to save the recorded files
mkdir ./bags
cd bags
2. Start recording
roscore
rosrun turtlesim turtlesim_node
rosrun turtlesim turtle_teleop_key
Start the turtle GUI and the keyboard control node;
rosbag record -a -o bags/hello
Operate the little turtle to run for a period of time, and use ctrl+c to end the recording, and a bag suffix file will be generated in the created directory;
3. View files
rosbag info bags/hello_2022-11-16-21-32-41.bag
path: bags/hello_2022-11-16-21-32-41.bag
version: 2.0
duration: 21.3s
start: Nov 16 2022 21:32:41.60 (1668605561.60)
end: Nov 16 2022 21:33:02.89 (1668605582.89)
size: 216.7 KB
messages: 2902
compression: none [1/1 chunks]
types: geometry_msgs/Twist [9f195f881246fdfa2798d1d3eebca84a]
rosgraph_msgs/Log [acffd30cd6b6de30f120938c17c593fb]
turtlesim/Color [353891e354491c51aabe32df673fb446]
turtlesim/Pose [863b248d5016ca62ea2e895ae5265cf9]
topics: /rosout 3 msgs : rosgraph_msgs/Log
/turtle1/cmd_vel 265 msgs : geometry_msgs/Twist
/turtle1/color_sensor 1317 msgs : turtlesim/Color
/turtle1/pose 1317 msgs : turtlesim/Pose
4. Play back files
rosbag play bags/hello_2022-11-16-21-32-41.bag
Restart the tortoise node, and you can see that the tortoise runs according to the track recorded in the bag file
Note: rosbag is also a node, so rosrun can also be used.
2.2 rosbag use - encoding
The command line is not flexible enough, so use the encoding mode to enhance flexibility
1. Write bag
Implementation process:
Initialization;
create rosbag object;
open file stream;
write data;
close file stream;
code:
#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,"bag_write");
ros::NodeHandle nh;
// 3、创建rosbag对象
rosbag::Bag bag;
// 4、打开文件流
bag.open("hello.bag",rosbag::BagMode::Write);
// 5、写数据
std_msgs::String msg;
msg.data="hello xxxx";
bag.write("", ros::Time::now(), msg);
bag.write("", ros::Time::now(), msg);
bag.write("", ros::Time::now(), msg);
bag.write("", ros::Time::now(), msg);
// 6、关闭文件流
bag.close();
return 0;
}
2. Read bag
Implementation process:
Similar, essentially operating disk files
code:
#include "ros/ros.h"
#include "rosbag/bag.h"
#include "rosbag/view.h"
#include "std_msgs/String.h"
/*
需求:使用rosbag 读取磁盘上的bag文件
流程:
1、导包
2、初始化;
3、创建rosbag对象
4、打开文件流(以读的方式打开)
5、读数据
6、关闭文件流
*/
int main(int argc, char *argv[])
{
// 2、初始化;
setlocale(LC_ALL,"");
ros::init(argc,argv,"bag_read");
ros::NodeHandle nh;
// 3、创建rosbag对象
rosbag::Bag bag;
// 4、打开文件流(以读的方式打开)
bag.open("hello.bag", rosbag::BagMode::Read);
// 5、读数据
//因为磁盘文件保存数据类型读也要读出来
//取出时间戳,话题和消息内容
//先获取消息的集合,再迭代取出消息的字段具体
for (auto &&m : rosbag::View(bag))
{
//解析
std::string topic = m.getTopic();
ros::Time time = m.getTime();
std_msgs::StringPtr p = m.instantiate<std_msgs::String>();
ROS_INFO("解析的内容,话题:%s,时间戳:%.2f,消息值:%s",topic.c_str(),time.toSec(),p->data.c_str());
}
// 6、关闭文件流
bag.close();
return 0;
}
An error occurred:
terminate called after throwing an instance of 'ros::InvalidNameException'
what(): Character [ ] at element [7] is not valid in Graph Resource Name [dwadawd 1]. Valid characters are a-z, A-Z, 0-9, / and _.
已放弃 (核心已转储)
Cause Analysis: There is a naming error in the cpp file, delete the space or change it to an underscore
Solve the problem: After inspection, it is found that during the initialization process, the name of the topic is arbitrarily typed into dwadawd 1, and there are spaces. After changing it to bag_read, recompile and run;