Launching/monitoring multiple nodes with Launch 


ROS 2 launch system ROS 2 launch启动系统

The launch system in ROS 2 is responsible for helping the user describe the configuration of their system and then execute it as described. The configuration of the system includes what programs to run, where to run them, what arguments to pass them, and ROS specific conventions which make it easy to reuse components throughout the system by giving them each different configurations. It is also responsible for monitoring the state of the processes launched, and reporting and/or reacting to changes in the state of those processes. 

ROS 2中的启动系统负责协助用户描述其系统的配置,然后按照描述执行。系统的配置包括运行哪些程序运行它们的位置传递它们的参数以及ROS特定约定,这些约定使得通过为每个不同的配置提供组件,可以轻松地在整个系统中重用组件。它还负责监控已启动的流程的状态,并报告和/或响应这些流程的状态变化。

The ROS 2 Bouncy release includes a framework in which launch files, written in Python, can start and stop different nodes as well as trigger and act on various events. The package providing this framework is launch_ros, which uses the non-ROS-specific launch framework underneath. 

ROS 2 Bouncy及之后版本包含一个框架,其中用Python编写的launch文件可以启动和停止不同的节点,以及触发和处理各种事件。提供此框架的launch_ros包使用下面的非ROS特定launch框架。


The design document (in review) details the goal of the design of ROS 2’s launch system (not all functionality is currently available). 

这个设计文件(综述)详细描述ROS 2的launch启动系统的设计目标(不是所有的功能是目前可用)。

Example of ROS 2 launch concepts 

ROS 2 launch启动概念的例子

The launch file in this example launches two nodes, one of which is a node with a managed lifecycle (a “lifecycle node”). Lifecycle nodes launched through launch_ros automatically emit events when they transition between states. The events can then be acted on through the launch framework, e.g. by emitting other events (such as requesting another state transition, which lifecycle nodes launched through launch_ros automatically have event handlers for) or triggering other actions (e.g. starting another node). 


In the aforementioned example, various transition requests are requested of the talker lifecycle node, and its transition events are reacted to by, for example, launching a listener node when the lifecycle talker reaches the appropriate state. 


Usage 用法

While launch files can be written as standalone scripts, the typical usage in ROS is to have launch files invoked by ROS 2 tools. 

虽然启动文件可以作为独立脚本编写,但ROS中的典型用法是使用ROS 2工具调用启动文件。

For example, this launch file has been designed such that it can be invoked by ros2 launch: 

例如,此启动文件的设计使其可以通过以下方式调用:ros2 launch

ros2 launch demo_nodes_cpp


"""Launch a add_two_ints_server and a (synchronous) add_two_ints_client."""


import launch

import launch_ros.actions


def generate_launch_description():

    server = launch_ros.actions.Node(

        package='demo_nodes_cpp', node_executable='add_two_ints_server', output='screen')

    client = launch_ros.actions.Node(

        package='demo_nodes_cpp', node_executable='add_two_ints_client', output='screen')

    return launch.LaunchDescription([



        # TODO(wjwwood): replace this with a `required=True|False` option on ExecuteProcess().

        # Shutdown launch when client exits.










Documentation 文档

The launch documentation provides more details on concepts that are also used in launch_ros. 


Additional documentation/examples of capabilities are forthcoming. See the source code in the meantime. 



Migrating launch files from ROS 1 to ROS 2 

将启动文件从ROS 1迁移到ROS2

This tutorial describes how to write XML launch files for an easy migration from ROS 1. 

本教程描述了如何编写XML启动文件以便从ROS 1轻松迁移到ROS 2

(仅用于ROS 1复习)

Background 背景

A description of the ROS 2 launch system and its Python API can be found in Launch System tutorial. 

可以在Launch System教程中找到有关ROS 2启动系统及其Python API的说明。


Migrating tags from ROS1 to ROS2 


launch 启动

可用于ROS 1

  • launch is the root element of any ROS 2 launch XML file. 

launch 是任何ROS 2启动XML文件的根元素。

node 节点


  • Launches a new node. 


Differences from ROS 1: 与ROS 1的区别:

  • type attribute is now executable. 


  • The following attributes aren’t available: machinerespawnrespawn_delayclear_params. 


Example 示例


   <node pkg="demo_nodes_cpp" exec="talker"/>

   <node pkg="demo_nodes_cpp" exec="listener"/>



from launch import LaunchDescription

import launch_ros.actions


def generate_launch_description():

    return LaunchDescription([


            node_namespace= "ros2", package='demo_nodes_cpp', node_executable='talker', output='screen'),


            node_namespace= "ros2", package='demo_nodes_cpp', node_executable='listener', output='screen'),



param 参数


  • Used for passing a parameter to a node. 


There’s no global parameter concept in ROS 2. For that reason, it can only be used nested in a node tag. Some attributes aren’t supported in ROS 2: typetextfilebinfileexecutablecommand. 

ROS 2中没有全局参数概念。因此,它只能嵌套在node标签中。某些属性不支持ROS 2:typetextfile binfileexecutablecommand

Example 示例


   <node pkg="demo_nodes_cpp" exec="parameter_event">

      <param name="foo" value="5"/>




Type inference rules 类型推断规则

Here are some examples of how to write parameters: 


<node pkg="my_package" exec="my_executable" name="my_node">

   <!--A string parameter with value "1"-->

   <param name="a_string" value="'1'"/>

   <!--A integer parameter with value 1-->

   <param name="an_int" value="1"/>

   <!--A float parameter with value 1.0-->

   <param name="a_float" value="1.0"/>

   <!--A string parameter with value "asd"-->

   <param name="another_string" value="asd"/>

   <!--Another string parameter, with value "asd"-->

   <param name="string_with_same_value_as_above" value="'asd'"/>

   <!--Another string parameter, with value "'asd'"-->

   <param name="quoted_string" value="\'asd\'"/>

   <!--A list of strings, with value ["asd", "bsd", "csd"]-->

   <param name="list_of_strings" value="asd, bsd, csd" value-sep=", "/>

   <!--A list of ints, with value [1, 2, 3]-->

   <param name="list_of_ints" value="1,2,3" value-sep=","/>

   <!--Another list of strings, with value ["1", "2", "3"]-->

   <param name="another_list_of_strings" value="'1';'2';'3'" value-sep=";"/>

   <!--A list of strings using an strange separator, with value ["1", "2", "3"]-->

   <param name="strange_separator" value="'1'//'2'//'3'" value-sep="//"/>



Parameter grouping 参数分组

In ROS 2, param tags are allowed to be nested. For example: 

在ROS 2中,param标签允许嵌套。例如:


<node pkg="my_package" exec="my_executable" name="my_node" ns="/an_absoulute_ns">

   <param name="group1">

      <param name="group2">

         <param name="my_param" value="1"/>


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




That will create two parameters: 


  • group1.group2.my_param of value 1, hosted by node /an_absolute_ns/my_node. 


  • group1.another_param of value 2 hosted by node /an_absolute_ns/my_node. 


It’s also possible to use full parameter names: 


<node pkg="my_package" exec="my_executable" name="my_node" ns="/an_absoulute_ns">

   <param name="group1.group2.my_param" value="1"/>

   <param name="group1.another_param" value="2"/>





  • Loads parameters from a yaml file. 


  • It has been replaced with a from atribute in param tags. 


Example 示例

<node pkg="my_package" exec="my_executable" name="my_node" ns="/an_absoulute_ns">

   <param from="/path/to/file"/>



remap 重映射

可用于ROS 1

  • Used to pass remapping rules to a node. 


  • It can only be used within node tags. 


Example 示例


   <node pkg="demo_nodes_cpp" exec="talker">

      <remap from="chatter" to="my_topic"/>


   <node pkg="demo_nodes_cpp" exec="listener">

      <remap from="chatter" to="my_topic"/>




include 包括

可用于ROS 1

  • Allows including another launch file. 


Differences from ROS 1: 与ROS 1的区别:

  • Available in ROS 1, included content was scoped. In ROS 2, it’s not. Nest includes in group tags to scope them. 

在ROS 1中可用,包含的内容是作用域。在ROS 2中,它不是。Nest包含group标记以限定它们。

  • ns attribute is not supported. See example of push_ros_namespace tag for a workaround. 


  • arg tags nested in an include tag don’t support conditionals (if or unless). 


  • There is no support for nested env tags. set_env and unset_env can be used instead. 

不支持嵌套env标记。 set_env并且unset_env可以替代使用。

  • Both clear_params and pass_all_args attributes aren’t supported. 


Examples 示例

See Replacing an include tag. 参考替换包含标记


可用于ROS 1

  • arg is used for declaring a launch argument, or to pass an argument when using include tags. 


Differences from ROS 1: 与ROS 1的区别:

  • value attribute is not allowed. Use let tag for this. 


  • doc is now description. 


  • When nested within an include tag, if and unless attributes aren’t allowed. 


Example 示例


   <arg name="topic_name" default="chatter"/>

   <node pkg="demo_nodes_cpp" exec="talker">

      <remap from="chatter" to="$(var topic_name)"/>


   <node pkg="demo_nodes_cpp" exec="listener">

      <remap from="chatter" to="$(var topic_name)"/>



Passing an argument via the command line 通过命令行传递参数

See ROS 2 launch tutorial. 参考ROS 2 launch启动教程


可用于ROS 1

  • Sets an environment variable. 


  • It has been replaced with envset_env and unset_env: 


  1. env can only be used nested in a node or executable tag. if and unless tags aren’t supported. 

env只能嵌套在一个nodeexecutable标签中使用。 不支持ifunless标签。

    1. set_env can be nested within the root tag launch or in group tags. It accepts the same attributes as env, and also if and unless tags. 


  1. unset_env unsets an environment variable. It accepts a name attribute and conditionals. 


Example 示例


   <set_env name="MY_ENV_VAR" value="MY_VALUE" if="CONDITION_A"/>

   <set_env name="ANOTHER_ENV_VAR" value="ANOTHER_VALUE" unless="CONDITION_B"/>

   <set_env name="SOME_ENV_VAR" value="SOME_VALUE"/>

   <node pkg="MY_PACKAGE" exec="MY_EXECUTABLE" name="MY_NODE">

      <env name="NODE_ENV_VAR" value="SOME_VALUE"/>


   <unset_env name="MY_ENV_VAR" if="CONDITION_A"/>


   <unset_env name="ANOTHER_ENV_VAR" unless="CONDITION_B"/>

   <unset_env name="SOME_ENV_VAR"/>



可用于ROS 1

  • Allows limiting the scope of launch configurations. Usually used together with let,  include and push_ros_namespace tags. 


Differences from ROS 1: 与ROS 1的区别:

  • There is no ns attribute. See the new push_ros_namespace tag as a workaround. 


  • clear_params attribute isn’t available. 

clear_params 属性不可用。

  • It doesn’t accept remap nor param tags as children. 


Example 示例

launch-prefix configuration affects both executable and node tags’ actions. This example will use time as a prefix if use_time_prefix_in_talker argument is 1, only for the talker. 



   <arg name="use_time_prefix_in_talker" default="0"/>


      <let name="launch-prefix" value="time" if="$(var use_time_prefix_in_talker)"/>

      <node pkg="demo_nodes_cpp" exec="talker"/>


   <node pkg="demo_nodes_cpp" exec="listener"/>



It is not supported at the moment. 暂不支持。


It is not supported at the moment. 暂不支持。

New tags in ROS 2 ROS 2中的新标签

set_env and unset_env set_env和unset_env

See env tag decription. 参考env标签说明。


include and group tags don’t accept an ns attribute. This action can be used as a workaround: 


<!-Other tags-->


   <push_ros_namespace namespace="my_ns"/>

   <!--Nodes here are namespaced with "my_ns".-->

   <!--If there is an include action here, its nodes will also be namespaced.-->

   <push_ros_namespace namespace="another_ns"/>

   <!--Nodes here are namespaced with "another_ns/my_ns".-->

   <push_ros_namespace namespace="/absolute_ns"/>

   <!--Nodes here are namespaced with "/absolute_ns".-->

   <!--The following node receives an absolute namespace, so it will ignore the others previously pushed.-->

   <!--The full path of the node will be /asd/my_node.-->

   <node pkg="my_pkg" exec="my_executable" name="my_node" ns="/asd"/>

</group><!--Nodes outside the group action won't be namespaced.--><!-Other tags-->



It’s a replacement of arg tag with a value attribute. 它是使用value属性arg标记替换。

<let var="foo" value="asd"/>



It allows running any executable. 它允许运行任何可执行文件

Example 示例

<executable cmd="ls -las" cwd="/var/log" name="my_exec" launch-prefix="something" output="screen" shell="true">

   <env name="LD_LIBRARY" value="/lib/"/>


Replacing an include tag 替换包含标记

To have exactly the same behavior as Available in ROS 1, include tags must be nested in a group tag. 

要与ROS 1中的“可用”具有完全相同的行为,include标记必须嵌套在group标记中。


   <include file="another_launch_file"/>



To replace the ns attribute, push_ros_namespace action must be used: 



   <push_ros_namespace namespace="my_ns"/>

   <include file="another_launch_file"/>



Substitutions 替换

Documentation about ROS 1’s substitutions can be found in roslaunch XML wiki. Substitutions syntax hasn’t changed, i.e. it still follows the $(substitution-name arg1 arg2 ...) pattern. There are, however, some changes w.r.t. ROS 1: 

有关ROS 1替换的文档可以在roslaunch XML wiki中找到。替换语法没有改变,即它仍然遵循$(substitution-name arg1 arg2 ...)模式。但是,ROS 1有一些变化:

  • env and optenv tags have been replaced by the env tag. $(env <NAME>) will fail if the environment variable doesn’t exist. $(env <NAME> '') does the same as ROS 1’s $(optenv <NAME>). $(env <NAME> <DEFAULT>) does the same as ROS 1’s $(env <NAME> <DEFAULT>) or $(optenv <NAME> <DEFAULT>). 

envoptenv标签已被标签env取代。 如果环境变量$(env <NAME>)不存在,则会失败。 $(env <NAME> '') 与ROS 1$(optenv <NAME>)相同。 $(env <NAME> <DEFAULT>)与ROS 1$(env <NAME> <DEFAULT>) $(optenv <NAME> <DEFAULT>)相同。

  • find has been replaced with find-pkg. 


  • There is a new exec-in-pkg substitution. e.g.: $(exec-in-pkg <package_name> <exec_name>). 

有一个新的exec-in-pkg替代品。例如:$(exec-in-pkg <package_name> <exec_name>)

  • There is a new find-exec substitution. 


  • arg has been replaced with var. It looks at configurations defined either with arg or let tag. 


  • eval and dirname substitutions haven’t changed. 


  • anon substitution is not supported. 

anon 不支持替换。


Type inference rules 类型推断规则

The rules that were shown in Type inference rules subsection of param tag applies to any attribute. For example: 

标签param子部分Type inference rules中显示的规则适用于任何属性。例如:


<!--Setting a string value to an attribute expecting an int will raise an error.-->

<tag1 attr-expecting-an-int="'1'"/>

<!--Correct version.-->

<tag1 attr-expecting-an-int="1"/>

<!--Setting an integer in an attribute expecting a string will raise an error.-->

<tag2 attr-expecting-a-str="1"/>

<!--Correct version.-->

<tag2 attr-expecting-a-str="'1'"/>

<!--Setting a list of strings in an attribute expecting a string will raise an error.-->

<tag3 attr-expecting-a-str="asd, bsd" str-attr-sep=", "/>

<!--Correct version.-->

<tag3 attr-expecting-a-str="don't use a separator"/>

Some attributes accept more than a single type, for example value attribute of param tag. It’s usual that parameters that are of type int (or float) also accept an str, that will be later substituted and tried to convert to an int (or float) by the action. 

某些属性接受多个类型,例如param标记的value属性。通常,int类型(或float类型)的参数也接受 str,稍后将替换并尝试通过操作转换为int(或float)。

zhangrelay CSDN认证博客专家 不合格高校讲师
已标记关键词 清除标记
ROS2编程基础课程文档 ROS 2(机器人操作系统2)是用于机器人应用的开源开发套件。ROS 2之目的是为各行各业的开发人员提供标准的软件平台,从研究和原型设计再到部署和生产。 ROS 2建立在ROS 1的成功基础之上,ROS 1目前已在世界各地的无数机器人应用中得到应用。 特色 缩短上市时间 ROS 2提供了开发应用程序所需的机器人工具,库和功能,可以将时间花在对业务非常重要的工作上。因为它 是开源的,所以可以灵活地决定在何处以及如何使用ROS 2,以及根据实际的需求自由定制,使用ROS 2 可以大幅度提升产品和算法研发速度! 专为生产而设计 凭借在建立ROS 1作为机器人研发的事实上的全球标准方面的十年经验,ROS 2从一开始就被建立在工业级 基础上并可用于生产,包括高可靠性和安全关键系统。 ROS 2的设计选择、开发实践和项目管理基于行业利 益相关者的要求。 多平台支持 ROS 2在Linux,Windows和macOS上得到支持和测试,允许无缝开发和部署机器人自动化,后端管理和 用户界面。分层支持模型允许端口到新平台,例如实时和嵌入式操作系统,以便在获得兴趣和投资时引入和推 广。 丰富的应用领域 与之前的ROS 1一样,ROS 2可用于各种机器人应用,从室内到室外、从家庭到汽车、水下到太空、从消费 到工业。 没有供应商锁定 ROS 2建立在一个抽象层上,使机器人库和应用程序与通信技术隔离开来。抽象底层是通信代码的多种实现, 包括开源和专有解决方案。在抽象顶层,核心库和用户应用程序是可移植的。 建立在开放标准之上 ROS 2中的默认通信方法使用IDL、DDS和DDS-I RTPS等行业标准,这些标准已广泛应用于从工厂到航空 航天的各种工业应用中。 开源许可证 ROS 2代码在Apache 2.0许可下获得许可,在3条款(或“新”)BSD许可下使用移植的ROS 1代码。这两个 许可证允许允许使用软件,而不会影响用户的知识产权。 全球社区 超过10年的ROS项目通过发展一个由数十万开发人员和用户组成的全球社区,为机器人技术创建了一个庞大 的生态系统,他们为这些软件做出贡献并进行了改进。 ROS 2由该社区开发并为该社区开发,他们将成为未 来的管理者。 行业支持 正如ROS 2技术指导委员会成员所证明的那样,对ROS 2的行业支持很强。除了开发顶级产品外,来自世界 各地的大大小小公司都在投入资源为ROS 2做出开源贡献。 与ROS1的互操作性 ROS 2包括到ROS 1的桥接器,处理两个系统之间的双向通信。如果有一个现有的ROS 1应用程序, 可 以通过桥接器开始尝试使用ROS 2,并根据要求和可用资源逐步移植应用程序。
©️2020 CSDN 皮肤主题: Age of Ai 设计师:meimeiellie 返回首页