浅谈Ros中使用launch启动文件的方法(一):launch文件语法介绍

首先附上古月居老师的教程地址:古月居
古月居老师《Ros入门21讲》的全部代码在资源链接中分享给大家共同学习

主要内容
浅谈Ros中使用launch启动文件的方法(一):launch文件语法介绍
1.学习目标
2.launch文件初览
3.launch文件基本语法
4.参数设置语法
5.remap重映射


1.学习目标
在使用Ros过程中,经常遇到使用多个Terminal同时启动多个节点的情况,为简化Ros开发操作,使用Launch文件进行RosMaster启动、节点运行等操作是Ros学习和日常开发中十分重要的技能,以下根据古月居老师《Ros入门21讲》中第19讲的内容,特总结了launch文件使用的一些基本方法。

2.launch文件初览
launch文件采用XML语法,使用标签化定义,旨在向服务器发送当前操作请求以及关联的相关功能包、可执行文件等信息,下面是一个完整的launch文件内容:

<launch>
    <arg name="config_path" default = "$(find feature_tracker)/../config/euroc/euroc_config.yaml" />
	  <arg name="vins_path" default = "$(find feature_tracker)/../config/../" />
    
    <node name="feature_tracker" pkg="feature_tracker" type="feature_tracker" output="log">
        <param name="config_file" type="string" value="$(arg config_path)" />
        <param name="vins_folder" type="string" value="$(arg vins_path)" />
    </node>
 
    <node name="vins_estimator" pkg="vins_estimator" type="vins_estimator" output="screen">
       <param name="config_file" type="string" value="$(arg config_path)" />
       <param name="vins_folder" type="string" value="$(arg vins_path)" />
    </node>
 
    <node name="pose_graph" pkg="pose_graph" type="pose_graph" output="screen">
        <param name="config_file" type="string" value="$(arg config_path)" />
        <param name="visualization_shift_x" type="int" value="0" />
        <param name="visualization_shift_y" type="int" value="0" />
        <param name="skip_cnt" type="int" value="0" />
        <param name="skip_dis" type="double" value="0" />
    </node>
 
</launch>

3.launch文件基本语法

<launch>
        <node pkg="turtlesim" name = "sim1" type="turtlesim_nade"/>
        <node pkg="turtlesim" name = "sim2" type="turtlesim_node"/>
</launch>

以上面这个简单的launch文件举例,在< launch >标签中,描述了使用roslaunch命令运行节点所需的标签。其中< node >描述了roslaunch运行的节点,选项包括pkg、type和name。一个完整的launch语句可以包含以下选项内容:

基础选项:

pkg: 节点依赖的功能包名称。
type: 节点使用的可执行文件名称。
name: 节点运行时的名称(用户定义)。

除以上3个基础选项外,还可添加其它选项:

output: 选择是否需要将节点输出信息打印到终端上。
respawn: 如果节点启动时down掉选择是否需要重启节点。
required: 当前node在运行期间是否必须启动成功。
ns: =namespace可以为每个待启动节点自定义一个命名空间,来避免使用时命名冲突的问题。
args: 可在启动节点时为每个节点添加参数,如在新建Ros工作空间时可以添加roscpp、rospy、tf等参数表示新建工作空间的依赖项。
machine: 可以设置运行该节点的PC的名称、address、ros-root和ros-package-path。
remap: 可以更改节点名称、话题名称等等,在节点中用到的ROS变量的名称。例如:。
include: 可以加载属于同一个功能包或不同的功能包的另一个launch,并将其作为一个launch文件来运行。
group: 用于分组正在运行的节点。
test: 用于测试节点。类似于,但是有可以用于测试的选项。

以上是目前个人使用中比较常用的选项介绍,后续会持续更新,全部介绍可查看参考官方文档:roslaunch/XML

4.参数设置语法
下面用几个较概括性的语句进行举例说明:

<param name="output_frame" value="odom"/>

eg-1:< param >
实现功能:
设置Ros系统中正在运行的一个参数并存储到Ros参数服务器中,其中:

name:参数名
value:参数值
<rosparam file="params.yaml" command="load" ns=“params"/>

eg-2:< rosparam >
实现功能:
从参数文件params.yaml中加载所有参数,上传到Ros的参数服务器中,定义命名空间为params,其中:

file:参数文件名称
command:当前操作
ns:命名空间名称
<arg name="arg-name" default="arg-value" />

eg-3:< arg >标签
实现功能:
arg标签中的参数仅限于在launch文件之间进行参数传递,该语句表示在当前launch文件下定义一个名为“arg-name”的参数,其默认参数值为“arg-value”,其中:

name:在当前launch文件中定义一个参数的参数名称
default:该参数的默认参数值
<param name="foo" value="$(arg arg-name)" />

eg-4 :< arg >定义参数调用方法1
实现功能:
launc文件中找到名为arg-name的参数,取出参数值,将参数值赋值给名为foo的参数,其中:

name:取出参数值后赋值给参数的参数名
value="$(arg xxx)":在launch文件内部找到名为 "xxx"的参数,取出参数值,注意不能漏掉 $和前面的arg前缀
<node name="node" pkg="package" type="type " args="$(arg arg-name)" />

eg-5:< arg >定义参数调用方法2
实现功能:在启动节点时,取出launch文件中的arg-name参数作为节点的输入参数参与节点运行。

5.remap重映射
作用:将Ros中某些计算图的资源进行重新命名,如:

<remap from="/turtlebot/cmd_vel" to="/cmd_vel"/>

实现功能:
将Ros中的Topic:"/turtlebot/cmd_vel"进行映射,重命名为"/cmd_vel",其中:

from:原命名
to:映射之后的命名
同时,remap重映射机制不仅可以对Topic资源进行映射,对Server与Client等相关资源同样可以实现相关功能。

浅谈Ros中使用launch启动文件的方法(二)

1.Simple.launch
实现功能:
这个launch文件实现功能较为简单,使用node标签启动person_subscriber和person_publisher两个节点(以上两个Topic的编程实实现在古月居老师《Ros入门21讲》第10-11讲有相关介绍),并且在启动节点之前自启动RosMaster,意味着使用该launch文件进行节点启动前,不用先在Terminal中执行roscore命令,下面以这个简单的launch文件为例,完整介绍launch文件的生成到执行的完整流程。
调用步骤:

1.在Ros工作空间catkin_ws下新建功能包learning_launch

cd catkin_ws/src
catkin_create_pkg learning_launch

在这里插入图片描述

2.创建文件夹launch

mkdir launch

3.新建simple.launch文件并写入以下内容

touch simple.launch
<launch>
    <node pkg="learning_topic" type="person_subscriber" name="talker" output="screen" />
    <node pkg="learning_topic" type="person_publisher" name="listener" output="screen" /> 
</launch>

4.Terminal执行命令,运行launch文件

 roslaunch learning_launch simple.launch 

命令结构:roslaunch +launch所在功能包名 + launch文件名

5.实现流程回顾:

在这里插入图片描述

在这里插入图片描述

浅谈Ros中使用launch启动文件的方法(三)

1.turtlesim_parameter_config.launch(对应《Ros入门21讲》第16讲有关内容

<launch>

	<param name="/turtle_number"   value="2"/>

    <node pkg="turtlesim" type="turtlesim_node" name="turtlesim_node">
		<param name="turtle_name1"   value="Tom"/>
		<param name="turtle_name2"   value="Jerry"/>

		<rosparam file="$(find learning_launch)/config/param.yaml" command="load"/>
	</node>

    <node pkg="turtlesim" type="turtle_teleop_key" name="turtle_teleop_key" output="screen"/>

</launch>

实现功能:

设置名为"/turtle_number"的参数,赋值为2,将参数名与参数值以一个字典值的形式存入Ros的参数服务器中;
启动Ros中的"小乌龟"turtlesim_node节点,在节点运行后设置两个参数并赋值;
利用rosparam标签加载参数文件param.yaml中的所有参数并上传到参数服务器;
启动小海龟的键盘控制节点用于控制其运动。
附说明:
1.param.yaml参数文件内容(可直接在功能包内新建config/param.yaml复制使用),其中,A、B参数无命名空间,C、D设置在命名空间group中,用以避免资源冲突:

A: 123
B: "hello"

group:
  C: 456
  D: "hello"

2.通过launch文件存入参数服务器中的参数,可运行以下命令全部显示出来。

rosparam list

可以通过以下命令输出参数值:

rosparam get + 参数名称

3.在node标签内外使用param标签定义参数的不同之处在于参数前是否会带加上节点名的前缀,如下图中的turtle_name1(标签内默认加上)和turtle_number(标签外无前缀),类似命名空间的实现效果。
在这里插入图片描述

4.执行效果:
在这里插入图片描述

在这里插入图片描述

2.turtlesim_parameter_config.launch(对应《Ros入门21讲》第18讲有关内容)

 <launch>

    <!-- Turtlesim Node-->
    <node pkg="turtlesim" type="turtlesim_node" name="sim"/>
    <node pkg="turtlesim" type="turtle_teleop_key" name="teleop" output="screen"/>

    <node pkg="learning_tf" type="turtle_tf_broadcaster" args="/turtle1" name="turtle1_tf_broadcaster" />
    <node pkg="learning_tf" type="turtle_tf_broadcaster" args="/turtle2" name="turtle2_tf_broadcaster" />

    <node pkg="learning_tf" type="turtle_tf_listener" name="listener" />

  </launch>
 <launch>

    <!-- Turtlesim Node-->
    <node pkg="turtlesim" type="turtlesim_node" name="sim"/>
    <node pkg="turtlesim" type="turtle_teleop_key" name="teleop" output="screen"/>

    <node pkg="learning_tf" type="turtle_tf_broadcaster" args="/turtle1" name="turtle1_tf_broadcaster" />
    <node pkg="learning_tf" type="turtle_tf_broadcaster" args="/turtle2" name="turtle2_tf_broadcaster" />

    <node pkg="learning_tf" type="turtle_tf_listener" name="listener" />

  </launch>

实现功能:
记得在第18讲中,古月居老师利用一个简单的小乌龟跟随移动案例,来介绍在Ros中常用的坐标变换及坐标变换tf功能包的编程和使用方法,相信学过这部分案例的同学都记得当时为了运行相关代码,实现海龟跟踪的效果,打开了5个独立的Terminal才实现相关功能,而在下面的介绍中,仅使用一个launch文件即可实现全部步骤流程。
该launch文件实现了以下功能:

启动海龟仿真器;
启动海龟仿真器的键盘控制节点;
运行learning_tf功能包下的两个广播器turtle1_tf_broadcaster和turtle2_tf_broadcaster(args:为程序主函数输入的参数);
运行learning_tf功能包下的监听器listener。
执行效果:

在这里插入图片描述

2.turtlesim_remap.launch(remap重映射操作)

<launch>

	<include file="$(find learning_launch)/launch/simple.launch" />

    <node pkg="turtlesim" type="turtlesim_node" name="turtlesim_node">
		<remap from="/turtle1/cmd_vel" to="/cmd_vel"/>
	</node>

</launch>

实现功能:

  1. 利用include标签导入另一个launch文件(在第二节中有simple.launch文件介绍)
  2. 启动仿真器节点
  3. 将仿真器中代表海龟运行速度的参数/turtle1/cmd_vel去掉前缀重命名为/cmd_vel