launch file for ROS advanced components
1 data
Starting from this article, we learn ros advanced courses. Before, we completed the ros introductory course, see ROS efficient introductory series . The focus of introductory learning is the basic component learning of ros itself, without any algorithm involved. Starting from this article, we will learn robot modeling and simulation, as well as various algorithms related to robots, based on ros. Finally, in the simulation environment, the construction and testing of the entire robot system can be completed.
This article is the first section of the first chapter of the ros advanced series, explaining complex launch files. Anyone who engages in robots knows that many algorithms and models are already very mature, and they don't write much code, but mainly write launch files.
The reference materials for this article are as follows:
(1) ROS Efficient Introduction Series Blog: ROS Efficient Introduction Chapter 2 – Basic Concepts and Common Command Learning, Based on the Little Turtle Sample
(2) Gu Yueju: ROS Exploration Summary-54.launch file
(3 ) "Analysis of Robot Operating System (ROS)" [US] Jason M. O'Kane Translated by Xiao Junhao Chapter 6
(4) ros Tutorials: Using rqt_console and roslaunch
(5) ros Tutorials: Tips for using Roslaunch in large-scale projects
2 text
In the ROS Efficient Getting Started series blog, I wrote the launch startup file for each sample. This startup method does not need to start the node manager separately, and can pull up multiple nodes at once, saving time and effort.
2.1 Simpler launch
(1) Create the learning_launch package and related files
cd ~/catkin_ws/src
catkin_create_pkg learning_launch rospy roscpp
cd beginner_tutorials
mkdir config
touch config/param.yaml
mkdir launch
touch launch/mimic_draw_cycle.launch launch/mimic_draw_square.launch launch/double_mimic_draw_cycle_square.launch
(2) mimic_draw_cycle.launch
This sample is based on turtlesim, creating two turtlesim_nodes and making them draw a circle together. This example also uses turtlesim's mimic, which makes one turtlesim_node behave like another.
The concept of the source name of the calculation graph is mentioned in the comments. For details, please refer to: ROS Efficient Introduction Chapter 3 – Customizing ROS Messages, Writing C++ pub+sub Samples, Understanding the Source Name of the Calculation Graph
<launch> // <launch>和</launch>标签是launch文件的起止
// <group>是组标签,group使用命名空间 ns 区分
// 两个 turtlesim_node 分为位于各自的group内,其名称会使用命名空间进行区分
<group ns="turtlesim1">
// <node> 用于启动节点
// pkg:节点所在的软件包
// name: 节点运行时名称
// type: 节点的可执行文件名称
<node pkg="turtlesim" name="sim" type="turtlesim_node"/>
</group>
<group ns="turtlesim2">
<node pkg="turtlesim" name="sim" type="turtlesim_node"/>
</group>
<node pkg="turtlesim" name="mimic" type="mimic">
// <remap>重映射ros的计算图源名称
// 这句是把 mimic 的带 input 的计算图源,一律改为带 turtlesim1/turtle1,其实主要是几个 topic 名的改动
// 这说明<remap>不一定需要写全计算图源名称
<remap from="input" to="turtlesim1/turtle1"/>
<remap from="output" to="turtlesim2/turtle1"/>
</node>
<node
// required 表示这个节点对于整个系统是必不可少的,一旦挂了,系统将整体推出
// args 用于向节点传递参数
pkg="rostopic" type="rostopic" name="rostopic" required="true"
args="pub /turtlesim1/turtle1/cmd_vel geometry_msgs/Twist -r 1 -- '[2.0, 0.0, 0.0]' '[0.0, 0.0, -1.8]'"
/>
</launch>
Running results
(3) mimic_draw_square.launch
This sample is based on turtlesim, create two turtlesim_nodes, and let them draw a square together. This example also uses turtlesim's mimic.
<launch>
<group ns="turtlesim1">
<node pkg="turtlesim" name="sim" type="turtlesim_node"/>
</group>
<group ns="turtlesim2">
<node pkg="turtlesim" name="sim" type="turtlesim_node"/>
</group>
<node pkg="turtlesim" name="mimic" type="mimic">
<remap from="input" to="turtlesim1/turtle1"/>
<remap from="output" to="turtlesim2/turtle1"/>
</node>
<node pkg="turtlesim" type="draw_square" name="draw_square_cmd" required="true">
// 这里把topic进行了重映射,不然下游的 turtlesim_node 和 mimic 收不到
<remap from="/turtle1/cmd_vel" to="/turtlesim1/turtle1/cmd_vel"/>
<remap from="/turtle1/pose" to="/turtlesim1/turtle1/pose"/>
</node>
</launch>
operation result
2.2 Complicated launch
double_mimic_draw_cycle_square is to combine the two examples of 2.1 above into one, and use a launch file to pull them up. This example mainly uses the include and parameter setting functions of launch, which are necessary for complex launches.
(1) double_mimic_draw_cycle_square.launch
<launch>
<group ns="turtlesim1">
// <include>标签的功能就跟C语言的一样,将子文件原地展开
// $(find learning_launch)是在ros的全局路径中,找到learning_launch包
<include file="$(find learning_launch)/launch/two_turtlesim.launch"/>
</group>
<group ns="turtlesim2">
<include file="$(find learning_launch)/launch/two_turtlesim.launch"/>
</group>
<node
pkg="rostopic" type="rostopic" name="rostopic" output="screen" required="true"
args="pub /turtlesim1/sim_node1/cmd_vel geometry_msgs/Twist -r 1 -- '[2.0, 0.0, 0.0]' '[0.0, 0.0, -1.8]'"
/>
<node pkg="turtlesim" type="draw_square" name="draw_square_cmd" output="screen" required="true">
<remap from="/turtle1/cmd_vel" to="/turtlesim2/sim_node1/cmd_vel"/>
<remap from="/turtle1/pose" to="/turtlesim2/sim_node1/pose"/>
// <rosparam>标签,支持通过yaml文件,批量设置参数,这里是给当前node设置
<rosparam file="$(find learning_launch)/config/param.yaml" command="load"/>
</node>
</launch>
(2)two_turtlesim.launch
<launch>
// <param>标签用于设置单个参数
<param name="turtle_number" value="2"/>
// <arg>标签用于定于launch中的变量,供后面使用,与<node>的args区分!
<arg name="TurtlesimName1" default="Tom"/>
<arg name="TurtlesimName2" default="Jerry"/>
// <node>的respawn表示当前节点如果挂了,会被自动拉起来,即可重启
<node pkg="turtlesim" type="turtlesim_node" name="sim_node1" respawn="true">
<remap from="turtle1" to="sim_node1"/>
// 使用 arg 变量,设置节点参数
<param name="turtle_name" value="$(arg TurtlesimName1)"/>
</node>
<node pkg="turtlesim" type="turtlesim_node" name="sim_node2" respawn="true">
<remap from="turtle1" to="sim_node2"/>
<param name="turtle_name" value="$(arg TurtlesimName2)"/>
</node>
<node pkg="turtlesim" name="mimic" type="mimic" output="screen" required="true" >
<remap from="input" to="sim_node1"/>
<remap from="output" to="sim_node2"/>
</node>
</launch>
(3)param.yaml
param_a: 123
param_b: "ycao"
test_group:
param_c: 456
param_d: "miao"
(4) Compile and run
3 Summary
Ros Tutorials gives several tips for writing large launch files: Tips for using Roslaunch in large-scale projects , which will be experienced gradually later.
The samples for this article are hosted on my github: learning_launch