使用可替换变量(substitutions)

目标: 学习 ROS 2 启动文件中可替换变量的使用。

教程等级: 中级

预计时长: 15 分钟

背景

启动文件用于启动节点、服务和运行进程。 这些操作可能需要参数,这些参数会影响它们的行为。 可替换变量(substitutions)可以用于参数,以提供更大的灵活性,描述可重用的启动文件。 可替换变量是只在启动文件执行期间才会计算的变量,可以用于获取特定信息,如启动配置、环境变量,或计算一些 Python 表达式。

本教程展示了 ROS 2 启动文件中可替换变量的使用。

前提条件

本教程用到了 turtlesim 包。 本教程还假设你已经熟悉了 创建包

记得要在 每次打开新终端时 中 source ROS 2。

使用可替换变量

1 创建并配置包

首先,创建一个名为 launch_tutorial 的新包:

创建一个构建类型为 ament_python 的新包:

ros2 pkg create --build-type ament_python --license Apache-2.0 launch_tutorial

接着,在包内创建一个名为 launch 的目录:

mkdir launch_tutorial/launch

最后确保安装文件能正确安装:

添加以下更改到包的 setup.py 文件:

import os
from glob import glob
from setuptools import find_packages, setup

package_name = 'launch_tutorial'

setup(
    # Other parameters ...
    data_files=[
        # ... Other data files
        # Include all launch files.
        (os.path.join('share', package_name, 'launch'), glob('launch/*'))
    ]
)

2 外层启动文件

让我们创建一个可以调用并传递参数给另一个启动文件的启动文件。 这个启动文件可以是 Python 或 YAML 的类型。

为此,在 launch_tutorial 包的 launch 文件夹中创建以下文件。

Copy and paste the complete code into the launch/example_main.launch.yaml file:

launch:
  - let:
      name: 'background_r'
      value: '200'
  - include:
      file: '$(find-pkg-share launch_tutorial)/launch/example_substitutions.launch.yaml'
      arg:
        - name: 'turtlesim_ns'
          value: 'turtlesim2'
        - name: 'use_provided_red'
          value: 'True'
        - name: 'new_background_r'
          value: '$(var background_r)'

The $(find-pkg-share launch_tutorial) substitution is used to find the path to the launch_tutorial package. The path substitution is then joined with the example_substitutions.launch.yaml file name.

file: '$(find-pkg-share launch_tutorial)/launch/example_substitutions.launch.yaml'

The background_r variable with turtlesim_ns and use_provided_red arguments is passed to the include action. The $(var background_r) substitution is used to define the new_background_r argument with the value of the background_r variable.

arg:
  - name: 'turtlesim_ns'
    value: 'turtlesim2'
  - name: 'use_provided_red'
    value: 'True'
  - name: 'new_background_r'
    value: '$(var background_r)'

3 用到可替换变量的启动文件样例

现在在同一个文件夹中创建一个带有可替换变量的启动文件:

Create the file launch/example_substitutions.launch.yaml and insert the following code:

launch:
  - arg:
      name: 'turtlesim_ns'
      default: 'turtlesim1'
  - arg:
      name: 'use_provided_red'
      default: 'False'
  - arg:
      name: 'new_background_r'
      default: '200'

  - node:
      pkg: 'turtlesim'
      namespace: '$(var turtlesim_ns)'
      exec: 'turtlesim_node'
      name: 'sim'
  - executable:
      cmd: 'ros2 service call $(var turtlesim_ns)/spawn turtlesim/srv/Spawn "{x: 5, y: 2, theta: 0.2}"'
  - executable:
      cmd: 'ros2 param set $(var turtlesim_ns)/sim background_r 120'
  - timer:
      period: 2.0
      children:
        - executable:
            cmd: 'ros2 param set $(var turtlesim_ns)/sim background_r $(var new_background_r)'
            if: '$(eval "$(var new_background_r) == 200 and $(var use_provided_red)")'

The turtlesim_ns, use_provided_red, and new_background_r launch configurations are defined. They are used to store values of launch arguments in the above variables and to pass them to required actions. The launch configuration arguments can later be used with the $(var <name>) substitution to acquire the value of the launch argument in any part of the launch description.

The arg tag is used to define the launch argument that can be passed from the above launch file or from the console.

- arg:
    name: 'turtlesim_ns'
    default: 'turtlesim1'
- arg:
    name: 'use_provided_red'
    default: 'False'
- arg:
    name: 'new_background_r'
    default: '200'

The turtlesim_node node with the namespace set to the turtlesim_ns launch configuration value using the $(var <name>) substitution is defined.

- node:
    pkg: 'turtlesim'
    namespace: '$(var turtlesim_ns)'
    exec: 'turtlesim_node'
    name: 'sim'

Afterwards, an executable action is defined with the corresponding cmd tag. This command makes a call to the spawn service of the turtlesim node.

Additionally, the $(var <name>) substitution is used to get the value of the turtlesim_ns launch argument to construct a command string.

- executable:
    cmd: 'ros2 service call $(var turtlesim_ns)/spawn turtlesim/srv/Spawn "{x: 5, y: 2, theta: 0.2}"'

The same approach is used for the ros2 param executable actions that change the turtlesim background’s red color parameter. The difference is that the second action inside of the timer is only executed if the provided new_background_r argument equals 200 and the use_provided_red launch argument is set to True. The evaluation of the if predicate is done using the $(eval <python-expression>) substitution.

- executable:
    cmd: 'ros2 param set $(var turtlesim_ns)/sim background_r 120'
- timer:
    period: 2.0
    children:
      - executable:
          cmd: 'ros2 param set $(var turtlesim_ns)/sim background_r $(var new_background_r)'
          if: '$(eval "$(var new_background_r) == 200 and $(var use_provided_red)")'

4 构建包

回到工作空间的根目录,构建包:

colcon build

构建后别忘了 source 工作空间。

运行示例

现在可以使用 ros2 launch 命令来启动。

ros2 launch launch_tutorial example_main.launch.yaml

这将执行以下操作:

  1. 启动一个带有蓝色背景的 turtlesim 节点

  2. 生成第二只乌龟

  3. 将背景颜色改为紫色

  4. 如果提供的 background_r 参数为 200use_provided_red 参数为 True,则两秒后将颜色改为粉色

修改启动参数

If you want to change the provided launch arguments, you can either update the background_r variable in the example_main.launch.yaml or launch the example_substitutions.launch.yaml with preferred arguments. To see arguments that may be given to the launch file, run the following command:

ros2 launch launch_tutorial example_substitutions.launch.yaml --show-args

这将显示可以传递给启动文件的参数及其默认值。

Arguments (pass arguments as '<name>:=<value>'):

    'turtlesim_ns':
        no description given
        (default: 'turtlesim1')

    'use_provided_red':
        no description given
        (default: 'False')

    'new_background_r':
        no description given
        (default: '200')

现在可以通过以下方式将所需的参数传递给启动文件:

ros2 launch launch_tutorial example_substitutions.launch.yaml turtlesim_ns:='turtlesim3' use_provided_red:='True' new_background_r:=200

文档

The launch documentation 提供了有关可用替换变量的详细信息。

总结

在本教程中,你学习了如何在启动文件中使用可替换变量。 你学习了用它们创建带有可修改的启动变量的启动文件。

现在可以学习 在启动文件中使用事件处理程序,它们用于定义一组复杂的规则,可以用于动态修改启动文件。