[ROS study notes 14] ROS common components
Article directory
Written in the front, this series of notes refers to the tutorial of AutoLabor, the specific project address is here
foreword
The information obtained by the robot sensor, sometimes we may need to process it from time to time, sometimes it may just collect data and analyze it afterwards, such as:
In the implementation of robot navigation, it may be necessary to draw the global map required for navigation. There are two ways to achieve map drawing. Method 1: It can control the movement of the robot, process the data sensed by the robot sensor from time to time, and generate map information. Method 2: It is also to control the movement of the robot, save the data sensed by the robot sensor, and then re-read the data afterwards to generate map information. Comparing the two methods, it is obvious that method 2 is more flexible and convenient to use.
In ROS, a special tool is provided for data retention and reading implementation: rosbag.
concept
is 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 that can subscribe to topic messages and write the subscribed data to disk files; when replaying, rosbag is a publishing node that can read disk files and publish Topic messages in the file.
1. The use of rosbag
1.1 Use of rosbag - command line mode
need:
ROS built-in turtle case and operation, use rosbag to record during the operation, and realize replay after recording
accomplish:
1. Prepare
Create directory to save recorded files
mkdir ./xxx
cd xxx
2. Start recording
rosbag record -a -O 目标文件
Operate the little turtle for a while, end the recording and use ctrl + c, and a bag file will be generated in the created directory.
3. View files
rosbag info 文件名
4. Play back files
rosbag play 文件名
Restart the turtle node, and you will find that the turtle moves according to the track when it was recorded.
Example result:
1.2 Use of rogbag - encoding method
The command implementation is not flexible enough, you can use the encoding method to enhance the flexibility of recording and playback. This section will demonstrate the encoding implementation of rosbag through simple reading and writing.
Solution A: C++ implementation
1. write 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. Read the bag
/*
读取 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;
}
Example effect:
Option B: Python implementation
1. write 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. Read the 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()
Example result:
2. rqt toolbox
Before, some practical tools were used in ROS, such as: ros_bag for recording and playback, tf2_tools can generate TF tree... These tools greatly improve the convenience of development, but there are also some problems: the process of starting and using these tools It involves some command operations, which are not convenient to apply. In ROS, the rqt toolbox is provided, and the command operation is replaced by graphical operation when calling the tool. The application is more convenient, the operation efficiency is improved, and the user experience is optimized.
concept
Based on the QT framework, ROS provides a series of visual tools for robot development. The collection of these tools is rqt
effect
It is convenient to implement ROS visual debugging, and open multiple components in the same window to improve development efficiency and optimize user experience.
composition
The rqt toolbox consists of three parts
- rqt - the core implementation, developers do not need to pay attention
- rqt_common_plugins - a suite of tools commonly used in rqt
- rqt_robot_plugins - plug-ins that interact with robots during operation (eg: rviz)
2.1 rqt installation and basic use
1. Install
-
Generally, as long as you install the desktop-full version, it will come with a toolbox
-
If you need to install, you can install it as follows
$ sudo apt-get install ros-noetic-rqt $ sudo apt-get install ros-noetic-rqt-common-plugins
2. start
rqt
There are two ways to start:
- Method 1:
rqt
- Method 2:
rosrun rqt_gui rqt_gui
Basic use example: Use rqt to publish messages to let the little turtle do circular motion, example results:
2.2 rqt commonly used plug-ins: rqt_graph
rqt has a commonly used plug-in rqt_graph
, which can display the calculation graph in a visual way, which can be used directly
rqt_graph
start up.
Example usage:
The circles are nodes, and the boxes are topics.
2.3 rqt commonly used plug-ins: rqt_console
**Introduction:**rqt_console is a graphical plug-in for displaying and filtering logs in ROS
**Preparation:** Write Node nodes to output log information at various levels
/*
ROS 节点:输出各种级别的日志信息
*/
#include "ros/ros.h"
int main(int argc, char *argv[])
{
ros::init(argc,argv,"log_demo");
ros::NodeHandle nh;
ros::Rate r(0.3);
while (ros::ok())
{
ROS_DEBUG("Debug message d");
ROS_INFO("Info message oooooooooooooo");
ROS_WARN("Warn message wwwww");
ROS_ERROR("Erroe message EEEEEEEEEEEEEEEEEEEE");
ROS_FATAL("Fatal message FFFFFFFFFFFFFFFFFFFFFFFFFFFFF");
r.sleep();
}
return 0;
}
start up:
It can be added in the plugins of rqt, or rqt_console
start with
2.4 rqt commonly used plug-ins: rqt_plot
**Introduction:** Graphic drawing plug-in, which can draw the data published on the topic in the form of 2D drawing
**Preparation:** Start the turtlesim turtle node and keyboard control node, and get the turtle pose through rqt_plot
**Startup:** can be added in the plugins of rqt, or use rqt_plot
startup
Example usage:
2.5 rqt commonly used plug-ins: rqt_bag
**Introduction:** A graphical plug-in for recording and replaying bag files
**Preparation:**Start the turtlesim turtle node and keyboard control node
**Startup:** can be added in the plugins of rqt, or use rqt_bag
startup
recording:
replay:
Example usage: