ROS2与launch入门教程-使用substitutions
ROS2与launch入门教程-使用substitutions
说明:
- 介绍如何在launch中使用substitutions
背景:
- launch文件用于启动节点、服务和执行进程。
- 这组动作可能有参数,这些参数会影响它们的行为。
- 在描述可重用的启动文件时,可以在参数中使用替换以提供更大的灵活性。
- 替换是仅在启动描述执行期间评估的变量,可用于获取特定信息,如启动配置、环境变量或评估任意 Python 表达式。
准备
- 创建名为launch_tutorial的ros2的python包
cd ~/launch_ws/src
ros2 pkg create --build-type ament_python launch_tutorial
- 创建luanch目录
mkdir -p ~/launch_ws/src/launch_tutorial/launch
- 修改setup.py
vim ~/launch_ws/src/launch_tutorial/setup.py
- 内容修改为
from setuptools import setup
import os
from glob import glob
package_name = 'launch_tutorial'
setup(
name=package_name,
version='0.0.0',
packages=[package_name],
data_files=[
('share/ament_index/resource_index/packages',
['resource/' + package_name]),
('share/' + package_name, ['package.xml']),
(os.path.join('share', package_name, 'launch'), glob(os.path.join('launch', '*launch.py'))),
],
install_requires=['setuptools'],
zip_safe=True,
maintainer='ncnynl',
maintainer_email='1043931@qq.com',
description='TODO: Package description',
license='TODO: License declaration',
tests_require=['pytest'],
entry_points={
'console_scripts': [
],
},
)
- 指定构建launch_tutorial包,并使用symlink的方式
cd ~/launch_ws/
colcon build --symlink-install --packages-select launch_tutorial
- 加载工作空间
. ~/launch_ws/install/local_setup.bash
父launch文件:
- 首先,我们将创建一个launch文件,该文件将调用并将参数传递给另一个launch文件。
- 在 launch_tutorial包的/launch文件夹中创建一个example_main.launch.py文件。
cd ~/launch_ws/src/launch_tutorial/launch
vim example_main.launch.py
- 内容如下
from launch_ros.substitutions import FindPackageShare
from launch import LaunchDescription
from launch.actions import IncludeLaunchDescription
from launch.launch_description_sources import PythonLaunchDescriptionSource
from launch.substitutions import PathJoinSubstitution, TextSubstitution
def generate_launch_description():
colors = {
'background_r': '200'
}
return LaunchDescription([
IncludeLaunchDescription(
PythonLaunchDescriptionSource([
PathJoinSubstitution([
FindPackageShare('launch_tutorial'),
'launch',
'example_substitutions.launch.py'
])
]),
launch_arguments={
'turtlesim_ns': 'turtlesim2',
'use_provided_red': 'True',
'new_background_r': TextSubstitution(text=str(colors['background_r']))
}.items()
)
])
- 在 example_main.launch.py 文件中,FindPackageShare 替换用于查找 launch_tutorial包的路径。
- 然后使用 PathJoinSubstitution 替换将路径与 example_substitutions.launch.py 文件名连接到该包路径。
- 代码块:
PathJoinSubstitution([
FindPackageShare('launch_tutorial'),
'launch',
'example_substitutions.launch.py'
])
- 带有turtlesim_ns 和use_provided_red 参数的launch_arguments 字典被传递给IncludeLaunchDescription 操作。
- TextSubstitution 替换用于使用颜色字典中 background_r 键的值定义 new_background_r 参数。
- 代码块:
launch_arguments={
'turtlesim_ns': 'turtlesim2',
'use_provided_red': 'True',
'new_background_r': TextSubstitution(text=str(colors['background_r']))
}.items()
- 创建example_substitutions.launch.py
cd ~/launch_ws/src/launch_tutorial/launch
vim example_substitutions.launch.py
- 内容如下
from launch_ros.actions import Node
from launch import LaunchDescription
from launch.actions import DeclareLaunchArgument, ExecuteProcess, TimerAction
from launch.conditions import IfCondition
from launch.substitutions import LaunchConfiguration, PythonExpression
def generate_launch_description():
turtlesim_ns = LaunchConfiguration('turtlesim_ns')
use_provided_red = LaunchConfiguration('use_provided_red')
new_background_r = LaunchConfiguration('new_background_r')
turtlesim_ns_launch_arg = DeclareLaunchArgument(
'turtlesim_ns',
default_value='turtlesim1'
)
use_provided_red_launch_arg = DeclareLaunchArgument(
'use_provided_red',
default_value='False'
)
new_background_r_launch_arg = DeclareLaunchArgument(
'new_background_r',
default_value='200'
)
turtlesim_node = Node(
package='turtlesim',
namespace=turtlesim_ns,
executable='turtlesim_node',
name='sim'
)
spawn_turtle = ExecuteProcess(
cmd=[[
'ros2 service call ',
turtlesim_ns,
'/spawn ',
'turtlesim/srv/Spawn ',
'"{x: 2, y: 2, theta: 0.2}"'
]],
shell=True
)
change_background_r = ExecuteProcess(
cmd=[[
'ros2 param set ',
turtlesim_ns,
'/sim background_r ',
'120'
]],
shell=True
)
change_background_r_conditioned = ExecuteProcess(
condition=IfCondition(
PythonExpression([
new_background_r,
' == 200',
' and ',
use_provided_red
])
),
cmd=[[
'ros2 param set ',
turtlesim_ns,
'/sim background_r ',
new_background_r
]],
shell=True
)
return LaunchDescription([
turtlesim_ns_launch_arg,
use_provided_red_launch_arg,
new_background_r_launch_arg,
turtlesim_node,
spawn_turtle,
change_background_r,
TimerAction(
period=2.0,
actions=[change_background_r_conditioned],
)
])
- 在 example_substitutions.launch.py 文件中,定义了 turtlesim_ns、use_provided_red 和 new_background_r 启动配置。
- 它们用于在上述变量中存储启动参数的值并将它们传递给所需的操作。
- 这些 LaunchConfiguration 替换允许我们在启动描述的任何部分获取启动参数的值。
- DeclareLaunchArgument 用于定义可以从上述启动文件或控制台传递的启动参数。
- 代码块:
turtlesim_ns = LaunchConfiguration('turtlesim_ns')
use_provided_red = LaunchConfiguration('use_provided_red')
new_background_r = LaunchConfiguration('new_background_r')
turtlesim_ns_launch_arg = DeclareLaunchArgument(
'turtlesim_ns',
default_value='turtlesim1'
)
use_provided_red_launch_arg = DeclareLaunchArgument(
'use_provided_red',
default_value='False'
)
new_background_r_launch_arg = DeclareLaunchArgument(
'new_background_r',
default_value='200'
)
- 定义了命名空间设置为 turtlesim_ns LaunchConfiguration 替换的 turtlesim_node 节点。
- 代码块:
turtlesim_node = Node(
package='turtlesim',
namespace=turtlesim_ns,
executable='turtlesim_node',
name='sim'
)
- 之后,使用相应的 cmd 参数定义名为 spawn_turtle 的 ExecuteProcess 操作。
- 此命令调用 turtlesim 节点的 spawn 服务。
- 此外,LaunchConfiguration 替换用于获取 turtlesim_ns 启动参数的值以构造命令字符串。
- 代码块:
spawn_turtle = ExecuteProcess(
cmd=[[
'ros2 service call ',
turtlesim_ns,
'/spawn ',
'turtlesim/srv/Spawn ',
'"{x: 2, y: 2, theta: 0.2}"'
]],
shell=True
)
- 相同的方法用于更改 turtlesim 背景的红色参数的 change_background_r 和 change_background_r_condition 操作。
- 不同之处在于,仅当提供的 new_background_r 参数等于 200 并且 use_provided_red 启动参数设置为 True 时才会执行 change_background_r_condition 操作。
- IfCondition 内部的评估是使用 PythonExpression 替换完成的。
- 代码块:
change_background_r = ExecuteProcess(
cmd=[[
'ros2 param set ',
turtlesim_ns,
'/sim background_r ',
'120'
]],
shell=True
)
change_background_r_conditioned = ExecuteProcess(
condition=IfCondition(
PythonExpression([
new_background_r,
' == 200',
' and ',
use_provided_red
])
),
cmd=[[
'ros2 param set ',
turtlesim_ns,
'/sim background_r ',
new_background_r
]],
shell=True
)
- 编译包
cd ~/launch_ws/
colcon build --symlink-install --packages-select=launch_tutorial
- 启动launch文件
. ~/launch_ws/install/local_setup.bash
ros2 launch launch_tutorial example_main.launch.py
这将执行以下操作:
- 启动一个蓝色背景的turtlesim节点
- 生成第二只海龟
- 将颜色更改为紫色
- 如果提供的 background_r 参数为 200 且 use_provided_red 参数为 True,则在两秒后将颜色更改为粉红色
修改启动参数
- 如果要更改提供的启动参数,可以在 example_main.launch.py 的 launch_arguments 字典中更新它们
- 或者使用首选参数启动 example_substitutions.launch.py。
- 要查看可能提供给启动文件的参数,请运行以下命令:
ros2 launch launch_tutorial example_substitutions.launch.py --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.py turtlesim_ns:='turtlesim3' use_provided_red:='True' new_background_r:=200
获取最新文章: 扫一扫右上角的二维码加入“创客智造”公众号