ROS2編程基礎課程--Launch

  • 2019 年 10 月 5 日
  • 筆記

版權聲明:本文為部落客原創文章,遵循 CC 4.0 BY-SA 版權協議,轉載請附上原文出處鏈接和本聲明。

本文鏈接:https://blog.csdn.net/ZhangRelay/article/details/100773599

Launching/monitoring multiple nodes with Launch

使用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).

此示例中的啟動文件啟動兩個節點,其中一個節點是具有託管生命周期的節點(「生命周期節點」)。生命周期節點通過launch_ros在狀態之間轉換時自動發出事件來啟動。然後可以通過啟動框架對事件進行操作,例如通過發出其他事件(例如請求另一個狀態轉換,生命周期節點通過launch_ros自動啟動事件處理程式)或觸發其他操作(例如,啟動另一個節點)。

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.

在前述示例中,請求talker生命周期節點的各種轉換請求,並且通過例如listener在生命周期發布器達到適當狀態時啟動節點來響應其轉換事件。

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 add_two_ints.launch.py

"""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([

server,

client,

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

# Shutdown launch when client exits.

launch.actions.RegisterEventHandler(

event_handler=launch.event_handlers.OnProcessExit(

target_action=client,

on_exit=[launch.actions.EmitEvent(event=launch.events.Shutdown())],

)),

])

Documentation 文檔

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

啟動文檔提供了有關其中使用的概念的更多詳細資訊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

從ROS1遷移標籤ROS2

launch 啟動

可用於ROS 1。

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

launch 是任何ROS 2啟動XML文件的根元素。

node 節點

ROS1中提供。

  • Launches a new node.

啟動一個新節點。

Differences from ROS 1: 與ROS 1的區別:

  • type attribute is now executable.

type屬性現在executable。

  • The following attributes aren』t available: machine, respawn, respawn_delay, clear_params.

下面的屬性是不可用:machine,respawn,respawn_delay,clear_params。

Example 示例

<launch>

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

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

</launch>

from launch import LaunchDescription

import launch_ros.actions

def generate_launch_description():

return LaunchDescription([

launch_ros.actions.Node(

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

launch_ros.actions.Node(

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

])

param 參數

ROS1中提供。

  • 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: type, textfile, binfile, executable, command.

ROS 2中沒有全局參數概念。因此,它只能嵌套在node標籤中。某些屬性不支援ROS 2:type、textfile、 binfile、executablecommand。

Example 示例

<launch>

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

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

</node>

</launch>

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="//"/>

</node>

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>

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

</param>

</node>

That will create two parameters:

這將創建兩個參數:

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

一個group1.group2.my_param的值1,由節點/an_absolute_ns/my_node託管。

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

一個group1.another_param的值2,由節點/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"/>

</node>

rosparam

ROS1中提供。

  • Loads parameters from a yaml file.

從yaml文件載入參數。

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

它已從from屬性替換param標籤。

Example 示例

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

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

</node>

remap 重映射

可用於ROS 1。

  • Used to pass remapping rules to a node.

用於將重映射規則傳遞給節點。

  • It can only be used within node tags.

它只能在node標籤中使用。

Example 示例

<launch>

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

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

</node>

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

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

</node>

</launch>

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.

ns屬性不受支援。有關push_ros_namespace標記替換方法,請參考示例。

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

arg標記嵌套在include標記中,不支援條件(if或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.

這兩個clear_params和pass_all_args屬性不被支援。

Examples 示例

See Replacing an include tag. 請參考替換包含標記。

arg

可用於ROS 1。

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

arg用於聲明啟動參數,或在使用include標記時傳遞參數。

Differences from ROS 1: 與ROS 1的區別:

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

value屬性是不允許的。使用let標籤。

  • doc is now description.

doc現在是description。

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

當嵌套在一個include標籤的範圍內,if和unless屬性是不允許的。

Example 示例

<launch>

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

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

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

</node>

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

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

</node>

</launch>

Passing an argument via the command line 通過命令行傳遞參數

See ROS 2 launch tutorial. 請參考ROS 2 launch啟動教程。

env

可用於ROS 1。

  • Sets an environment variable.

設置環境變數。

  • It has been replaced with env, set_env and unset_env:

它已被替換為env,set_env和unset_env:

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

env只能嵌套在一個node或executable標籤中使用。 不支援if和unless標籤。

    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.

set_env可以嵌套在根標籤launch或group標籤中。它接受與env,if以及unless標籤相同的屬性。

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

unset_env取消設置環境變數。它接受name屬性和條件。

Example 示例

<launch>

<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"/>

</node>

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

<node pkg="ANOTHER_PACKAGE" exec="ANOTHER_EXECUTABLE" name="ANOTHER_NODE"/>

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

<unset_env name="SOME_ENV_VAR"/>

</launch>

group

可用於ROS 1。

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

允許限制啟動配置的範圍。通常與let,include和push_ros_namespace標籤一起使用。

Differences from ROS 1: 與ROS 1的區別:

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

沒有ns屬性。將新push_ros_namespace標記視為替代方法。

  • clear_params attribute isn』t available.

clear_params 屬性不可用。

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

它不接受remap和param標籤作子標籤。

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.

launch-prefix配置會影響標記executable和node標記的操作。此示例將time用作use_time_prefix_in_talker參數的前綴1,僅用於發布器。

<launch>

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

<group>

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

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

</group>

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

</launch>

machine

It is not supported at the moment. 暫不支援。

test

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標籤說明。

push_ros_namespace

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

include和group標籤不接受ns屬性。此操作可用作解決方法:

<!-Other tags-->

<group>

<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-->

let

It』s a replacement of arg tag with a value attribute. 它是使用value屬性arg標記的替換。

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

executable

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/some.so"/>

</executable>

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標記中。

<group>

<include file="another_launch_file"/>

</group>

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

要替換ns屬性,必須使用push_ros_namespace操作:

<group>

<push_ros_namespace namespace="my_ns"/>

<include file="another_launch_file"/>

</group>

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>).

env和optenv標籤已被標籤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.

find已被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.

有一個新的find-exec替代品。

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

arg已被var取代。它查看之前使用arg或let標記定義的配置。

  • eval and dirname substitutions haven』t changed.

eval和dirname替換沒有改變。

  • 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)。

Tags: