• ROS 2边学边练(31)-- 管理大工程


    前言

            往往现实中的工程都是会包含很多节点很多参数很多主题的那种,如果单独通过各种ros2 run命令进行启动管理,恐怕难以招架,主要还是通过launch文件的方式进行管理,而launch文件也可以像节点那样按功能的不同模块化,最终利用一个(或多个,根据实际需要)所谓顶级launch文件去调用管理这些模块化的子launch文件(还有其他ROS中提供的如参数、YAML文件、重映射、命名空间、RViz参数等项),化零为整,化繁为简。

    动动手

            我们还是在launch_tutorial这个功能包内进行练习。

    编写launch文件

    编写顶级launch文件

            在编写launch文件的过程中,目标之一应该是使它们尽可能地可重复使用。这可以通过将相关节点和配置集群到单独的launch文件中来实现。之后,可以编写专用于特定配置的顶级launch文件。这将允许在完全不改变launch文件(或很少改动)的情况下在相同的机器人之间通用。

            我们接下来要在launch_tutorial包的launch文件夹下编写launch_turtlesim_launch.py(即是顶级launch文件)用来调用管理其他单独的launch文件(随后我们会逐一编写里面调用的其他的launch文件),内容如下:

    1. import os
    2. from ament_index_python.packages import get_package_share_directory
    3. from launch import LaunchDescription
    4. from launch.actions import IncludeLaunchDescription
    5. from launch.launch_description_sources import PythonLaunchDescriptionSource
    6. def generate_launch_description():
    7. turtlesim_world_1 = IncludeLaunchDescription(
    8. PythonLaunchDescriptionSource([os.path.join(
    9. get_package_share_directory('launch_tutorial'), 'launch'),
    10. '/turtlesim_world_1_launch.py'])
    11. )
    12. turtlesim_world_2 = IncludeLaunchDescription(
    13. PythonLaunchDescriptionSource([os.path.join(
    14. get_package_share_directory('launch_tutorial'), 'launch'),
    15. '/turtlesim_world_2_launch.py'])
    16. )
    17. broadcaster_listener_nodes = IncludeLaunchDescription(
    18. PythonLaunchDescriptionSource([os.path.join(
    19. get_package_share_directory('launch_tutorial'), 'launch'),
    20. '/broadcaster_listener_launch.py']),
    21. launch_arguments={'target_frame': 'carrot1'}.items(),
    22. )
    23. mimic_node = IncludeLaunchDescription(
    24. PythonLaunchDescriptionSource([os.path.join(
    25. get_package_share_directory('launch_tutorial'), 'launch'),
    26. '/mimic_launch.py'])
    27. )
    28. fixed_frame_node = IncludeLaunchDescription(
    29. PythonLaunchDescriptionSource([os.path.join(
    30. get_package_share_directory('launch_tutorial'), 'launch'),
    31. '/fixed_broadcaster_launch.py'])
    32. )
    33. rviz_node = IncludeLaunchDescription(
    34. PythonLaunchDescriptionSource([os.path.join(
    35. get_package_share_directory('launch_tutorial'), 'launch'),
    36. '/turtlesim_rviz_launch.py'])
    37. )
    38. return LaunchDescription([
    39. turtlesim_world_1,
    40. turtlesim_world_2,
    41. broadcaster_listener_nodes,
    42. mimic_node,
    43. fixed_frame_node,
    44. rviz_node
    45. ])

            此启动文件包括一组其他启动文件。这些包含的启动文件中的每一个都包含节点、参数,可能还有嵌套的包含,它们属于系统的一个部分。确切地说,我们推出了两个turtlesim模拟世界,TF广播器(TF broadcaster)、TF侦听器(TF listener)、仿态(mimic)、固定帧广播器(fixed frame broadcaster)和RViz节点(TF、RViz这些后续内容会有介绍)。

            需要注意是,顶级启动文件应该能短则短,包括与应用程序的子组件相对应的其他文件的包含,以及通常更改的参数。

            按照以下方式编写启动文件可以很容易地交换系统的一部分,我们稍后将看到这一点。但是,有时由于性能和使用原因,某些节点或启动文件必须单独启动。

            在决定应用程序需要多少顶级启动文件时,要注意权衡。

    参数

            关于参数相关的详细信息,可以参见官方说明或前期相关文章。

    启动文件中设置参数

            我们创建一个名为turtlesim_world_1_launch.py的启动文件来实现启动第一个海龟仿真模拟,内容如下:

    1. from launch import LaunchDescription
    2. from launch.actions import DeclareLaunchArgument
    3. from launch.substitutions import LaunchConfiguration, TextSubstitution
    4. from launch_ros.actions import Node
    5. def generate_launch_description():
    6. background_r_launch_arg = DeclareLaunchArgument(
    7. 'background_r', default_value=TextSubstitution(text='0')
    8. )
    9. background_g_launch_arg = DeclareLaunchArgument(
    10. 'background_g', default_value=TextSubstitution(text='84')
    11. )
    12. background_b_launch_arg = DeclareLaunchArgument(
    13. 'background_b', default_value=TextSubstitution(text='122')
    14. )
    15. return LaunchDescription([
    16. background_r_launch_arg,
    17. background_g_launch_arg,
    18. background_b_launch_arg,
    19. Node(
    20. package='turtlesim',
    21. executable='turtlesim_node',
    22. name='sim',
    23. parameters=[{
    24. 'background_r': LaunchConfiguration('background_r'),
    25. 'background_g': LaunchConfiguration('background_g'),
    26. 'background_b': LaunchConfiguration('background_b'),
    27. }]
    28. ),
    29. ])

            此启动文件启动turtlesim_node节点,该节点启动turtlesim模拟,并定义模拟配置参数并将其传递给节点。

    从YAML文件加载参数

            我们再来创建第二个海龟仿真模拟节点,只不过不像第一只海龟提供在启动文件中设置了一些背景颜色参数值来改变背景,而第二只则是通过加载读取配置文件的方式来修改背景颜色参数值来改变背景,turtlesim_world_2_launch.py,内容如下:

    1. import os
    2. from ament_index_python.packages import get_package_share_directory
    3. from launch import LaunchDescription
    4. from launch_ros.actions import Node
    5. def generate_launch_description():
    6. config = os.path.join(
    7. get_package_share_directory('launch_tutorial'),
    8. 'config',
    9. 'turtlesim.yaml'
    10. )
    11. return LaunchDescription([
    12. Node(
    13. package='turtlesim',
    14. executable='turtlesim_node',
    15. namespace='turtlesim2',
    16. name='sim',
    17. parameters=[config]
    18. )
    19. ])

            从上可以看到该启动文件加载了launch/config下的turtlesim.yaml文件,该文件保存了相关参数(YAML文件可以很方便地从当前的ros2 parm 中导出,详情见文档5 ros2 param dump),我们再来创建这个turtlesim.yaml文件,内容如下:

    1. /turtlesim:
    2. ros__parameters:
    3. background_b: 255
    4. background_g: 86
    5. background_r: 150
    6. qos_overrides:
    7. /parameter_events:
    8. publisher:
    9. depth: 1000
    10. durability: volatile
    11. history: keep_last
    12. reliability: reliable
    13. use_sim_time: false
    在YAML文件中使用通配符

            在某些情况下,我们希望在多个节点中设置相同的参数。这些节点可以具有不同的名称空间或名称,但仍然具有相同的参数。定义单独的YAML文件来显式定义名称空间和节点名称是低效的。一种解决方案是使用通配符,作为文本值中未知字符的替换,将参数应用于几个不同的节点。

            我们像turtlesim_world_2_launch.py那样创建第三个海龟仿真节点启动文件turtlesim_world_3_launch.py,只不过呢,要包含更多的turtlesim_node节点,内容如下:

    1. import os
    2. from ament_index_python.packages import get_package_share_directory
    3. from launch import LaunchDescription
    4. from launch_ros.actions import Node
    5. def generate_launch_description():
    6. config = os.path.join(
    7. get_package_share_directory('launch_tutorial'),
    8. 'config',
    9. 'turtlesim.yaml'
    10. )
    11. return LaunchDescription([
    12. Node(
    13. package='turtlesim',
    14. executable='turtlesim_node',
    15. namespace='turtlesim3',
    16. name='sim',
    17. parameters=[config]
    18. )
    19. ])

            也是同样从同一个YAML文件中读取参数启动,如果有多个节点读取同一份YAML文件时,会根据里面的命名空间来找到匹配自己的参数,比如针对turtlesim3,正常情况我们需要在之前节点的参数后面加上:

    1. /turtlesim3/sim:
    2. background_b
    3. background_g
    4. background_r

    功能是做到了,但是是不是显得有点啰嗦了而且还占用了空间,这时我们的通配符就可以出场一显身手啦,对于那些共用的参数内容,我们不需再另起一行复制一遍那些参数内容,只需将这些参数最上面的“/namespace/nodename:”替换为“/**:”即可,省了空间也精简了代码内容。修改后的turtlesim.yaml文件内容如下:

    1. /**:
    2. ros__parameters:
    3. background_b: 255
    4. background_g: 86
    5. background_r: 150
    6. qos_overrides:
    7. /parameter_events:
    8. publisher:
    9. depth: 1000
    10. durability: volatile
    11. history: keep_last
    12. reliability: reliable
    13. use_sim_time: false
    命名空间

            不同命名空间下的节点可以拥有相同的节点名字、主题名称,不会发生冲突,比如我们上面创建的/turtlesim2/sim和/turtlesim3/sim,这就是两个不同命名空间下有相同名字的节点。在每个节点的启动文件里面都有显示填写了所属的命名空间,比如turtlesim_world_2_launch.py里面的namespace='turtlesim2',如果我们有很多很多(可能恒河沙数)的节点(名字可能会相同)launch文件,都要在每个launch文件里定义各自的命名空间,有没有感觉到有点无趣呢?有没有一种办法能够让我们省去这套乏味的操作呢?为了解决这个问题,PushROSNamespace操作可以用于为每个启动文件描述定义全局命名空间。每个嵌套节点都将自动继承该名称空间。

            如何做呢,首先,我们需要从turtlesim_world_2_launch.py文件中删除namespace='turtlesim2'行(turtlesim_world_3_launch.py也同样操作)。之后,我们需要更新launch_turtlesim_launch.py以包括以下行:

    1. from launch.actions import GroupAction
    2. from launch_ros.actions import PushROSNamespace
    3. ...
    4. turtlesim_world_2 = IncludeLaunchDescription(
    5. PythonLaunchDescriptionSource([os.path.join(
    6. get_package_share_directory('launch_tutorial'), 'launch'),
    7. '/turtlesim_world_2_launch.py'])
    8. )
    9. turtlesim_world_2_with_namespace = GroupAction(
    10. actions=[
    11. PushROSNamespace('turtlesim2'),
    12. turtlesim_world_2,
    13. ]
    14. )

    最后将return LaunchDescription里面的turtlesim_world_2替换为turtlesim_world_2_with_namespace,最终的launch_turtlesim_launch.py的内容如下:

    1. import os
    2. from ament_index_python.packages import get_package_share_directory
    3. from launch import LaunchDescription
    4. from launch.actions import IncludeLaunchDescription
    5. from launch.launch_description_sources import PythonLaunchDescriptionSource
    6. def generate_launch_description():
    7. turtlesim_world_1 = IncludeLaunchDescription(
    8. PythonLaunchDescriptionSource([os.path.join(
    9. get_package_share_directory('launch_tutorial'), 'launch'),
    10. '/turtlesim_world_1_launch.py'])
    11. )
    12. broadcaster_listener_nodes = IncludeLaunchDescription(
    13. PythonLaunchDescriptionSource([os.path.join(
    14. get_package_share_directory('launch_tutorial'), 'launch'),
    15. '/broadcaster_listener_launch.py']),
    16. launch_arguments={'target_frame': 'carrot1'}.items(),
    17. )
    18. mimic_node = IncludeLaunchDescription(
    19. PythonLaunchDescriptionSource([os.path.join(
    20. get_package_share_directory('launch_tutorial'), 'launch'),
    21. '/mimic_launch.py'])
    22. )
    23. fixed_frame_node = IncludeLaunchDescription(
    24. PythonLaunchDescriptionSource([os.path.join(
    25. get_package_share_directory('launch_tutorial'), 'launch'),
    26. '/fixed_broadcaster_launch.py'])
    27. )
    28. rviz_node = IncludeLaunchDescription(
    29. PythonLaunchDescriptionSource([os.path.join(
    30. get_package_share_directory('launch_tutorial'), 'launch'),
    31. '/turtlesim_rviz_launch.py'])
    32. )
    33. turtlesim_world_2 = IncludeLaunchDescription(
    34. PythonLaunchDescriptionSource([os.path.join(
    35. get_package_share_directory('launch_tutorial'), 'launch'),
    36. '/turtlesim_world_2_launch.py'])
    37. )
    38. turtlesim_world_2_with_namespace = GroupAction(
    39. actions=[
    40. PushROSNamespace('turtlesim2'),
    41. turtlesim_world_2,
    42. ]
    43. )
    44. turtlesim_world_3 = IncludeLaunchDescription(
    45. PythonLaunchDescriptionSource([os.path.join(
    46. get_package_share_directory('launch_tutorial'), 'launch'),
    47. '/turtlesim_world_3_launch.py'])
    48. )
    49. turtlesim_world_3_with_namespace = GroupAction(
    50. actions=[
    51. PushROSNamespace('turtlesim3'),
    52. turtlesim_world_3,
    53. ]
    54. )
    55. return LaunchDescription([
    56. turtlesim_world_1,
    57. turtlesim_world_2_with_namespace,
    58. turtlesim_world_3_with_namespace,
    59. broadcaster_listener_nodes,
    60. mimic_node,
    61. fixed_frame_node,
    62. rviz_node
    63. ])

            最终,turtlesim_world_2_launch.py启动描述中的每个节点都将具有一个turtlesim2名称空间(同样turtlesim_world_3_launch.py也一样)。

    重用节点

            创建一个broadcaster_listener_launch.py文件,内容如下:

    1. from launch import LaunchDescription
    2. from launch.actions import DeclareLaunchArgument
    3. from launch.substitutions import LaunchConfiguration
    4. from launch_ros.actions import Node
    5. def generate_launch_description():
    6. return LaunchDescription([
    7. DeclareLaunchArgument(
    8. 'target_frame', default_value='turtle1',
    9. description='Target frame name.'
    10. ),
    11. Node(
    12. package='turtle_tf2_py',
    13. executable='turtle_tf2_broadcaster',
    14. name='broadcaster1',
    15. parameters=[
    16. {'turtlename': 'turtle1'}
    17. ]
    18. ),
    19. Node(
    20. package='turtle_tf2_py',
    21. executable='turtle_tf2_broadcaster',
    22. name='broadcaster2',
    23. parameters=[
    24. {'turtlename': 'turtle2'}
    25. ]
    26. ),
    27. Node(
    28. package='turtle_tf2_py',
    29. executable='turtle_tf2_listener',
    30. name='listener',
    31. parameters=[
    32. {'target_frame': LaunchConfiguration('target_frame')}
    33. ]
    34. ),
    35. ])

            在这个文件中,我们声明了target_frame启动参数,默认值为turtle1。默认值意味着启动文件可以接收一个参数以转发到其节点,或者如果未提供该参数,则会将默认值传递给其节点。

            之后,我们在启动时使用两个不同的名称和参数来使用turtle_tf2_broadcaster节点两次。这允许我们复制相同的节点而不会发生冲突。

            我们还启动了一个turtle_tf2_listener节点,并设置其target_frame参数,该参数是我们在上面声明和获取的。

    参数重载

            回想一下,我们在顶级启动文件中调用了broadcaster_listener_launch.py文件。除此之外,我们还为其传递了target_frame启动参数,如下所示:

    1. broadcaster_listener_nodes = IncludeLaunchDescription(
    2. PythonLaunchDescriptionSource([os.path.join(
    3. get_package_share_directory('launch_tutorial'), 'launch'),
    4. '/broadcaster_listener_launch.py']),
    5. launch_arguments={'target_frame': 'carrot1'}.items(),
    6. )

            如果删除launch_arguments这行,此节点就会使用默认的名字turtle1作为其target_frame的参数值。

    重映射

            我们来创建mimic_launch.py文件来启动mimic节点,mimic的意思就是模仿,比如之前的文章里面提到的,第二只海龟的运动轨迹完全跟随着第一只海龟,这里面就利用了重映射的功能。内容如下:

    1. from launch import LaunchDescription
    2. from launch_ros.actions import Node
    3. def generate_launch_description():
    4. return LaunchDescription([
    5. Node(
    6. package='turtlesim',
    7. executable='mimic',
    8. name='mimic',
    9. remappings=[
    10. ('/input/pose', '/turtle2/pose'),
    11. ('/output/cmd_vel', '/turtlesim2/turtle1/cmd_vel'),
    12. ]
    13. )
    14. ])

            这个启动文件将启动mimic节点,该节点将向一个turtlesim发送命令,使其跟随另一个turtlesim。该节点被设计为接收主题/input/pose上的目标姿态。在我们的案例中,我们想要将目标姿态从/turtle2/pose主题重新映射。最后,我们将/output/cmd_vel主题重新映射到/turtlesim2/turtle1/cmd_vel。这样,在我们turtlesim2模拟世界中的turtle1就会跟随我们初始turtlesim世界中的turtle2。

    配置文件

            我们再来创建一个启动文件turtlesim_rviz_launch.py:

    1. import os
    2. from ament_index_python.packages import get_package_share_directory
    3. from launch import LaunchDescription
    4. from launch_ros.actions import Node
    5. def generate_launch_description():
    6. rviz_config = os.path.join(
    7. get_package_share_directory('turtle_tf2_py'),
    8. 'rviz',
    9. 'turtle_rviz.rviz'
    10. )
    11. return LaunchDescription([
    12. Node(
    13. package='rviz2',
    14. executable='rviz2',
    15. name='rviz2',
    16. arguments=['-d', rviz_config]
    17. )
    18. ])

            这个启动文件将使用turtle_tf2_py包中定义的配置文件来启动RViz。这个RViz配置将设置世界坐标系,启用TF可视化,并以从上往下的视角启动RViz。

    环境变量

            创建fixed_broadcaster_launch.py:

    1. from launch import LaunchDescription
    2. from launch.actions import DeclareLaunchArgument
    3. from launch.substitutions import EnvironmentVariable, LaunchConfiguration
    4. from launch_ros.actions import Node
    5. def generate_launch_description():
    6. return LaunchDescription([
    7. DeclareLaunchArgument(
    8. 'node_prefix',
    9. default_value=[EnvironmentVariable('USER'), '_'],
    10. description='prefix for node name'
    11. ),
    12. Node(
    13. package='turtle_tf2_py',
    14. executable='fixed_frame_tf2_broadcaster',
    15. name=[LaunchConfiguration('node_prefix'), 'fixed_broadcaster'],
    16. ),
    17. ])

            这个启动文件展示了如何在启动文件中调用环境变量的方法。环境变量可以用来定义或推送命名空间,以便区分不同计算机或机器人上的节点。

    运行launch文件

    更新setup.py

            setup.py文件是在功能包launch_tutorial根路径下,如果你发现没有,那么原因之一就是这个功能包之前是ament_cmake生成的,我们得重新生成python的包launch_tutorial,然后将之前包里面的launch文件夹整个拷贝到新包根路径下即可,再来修改自动生成的setup.py,需要添加的内容如下,以便安装launch/文件夹中的启动文件和config/文件夹中的配置文件。:

    1. import os
    2. from glob import glob
    3. from setuptools import setup
    4. ...
    5. data_files=[
    6. ...
    7. (os.path.join('share', package_name, 'launch'),
    8. glob(os.path.join('launch', '*launch.[pxy][yma]*'))),
    9. (os.path.join('share', package_name, 'config'),
    10. glob(os.path.join('config', '*.yaml'))),
    11. ],

    完整的setup.py文件内容为:

    1. import os
    2. from glob import glob
    3. from setuptools import find_packages, setup
    4. package_name = 'launch_tutorial'
    5. setup(
    6. name=package_name,
    7. version='0.0.0',
    8. packages=find_packages(exclude=['test']),
    9. data_files=[
    10. ('share/ament_index/resource_index/packages',
    11. ['resource/' + package_name]),
    12. ('share/' + package_name, ['package.xml']),
    13. (os.path.join('share', package_name, 'launch'),
    14. glob(os.path.join('launch', '*launch.[pxy][yma]*'))),
    15. (os.path.join('share', package_name, 'config'),
    16. glob(os.path.join('config', '*.yaml'))),
    17. ],
    18. install_requires=['setuptools'],
    19. zip_safe=True,
    20. maintainer='mike',
    21. maintainer_email='mike@todo.todo',
    22. description='TODO: Package description',
    23. license='Apache-2.0',
    24. tests_require=['pytest'],
    25. entry_points={
    26. 'console_scripts': [
    27. ],
    28. },
    29. )
    构建运行

            我们返回到工作空间根路径下执行colcon build,构建完成后,先source install/setup.bash,然后启动下顶级launch文件:

    $ros2 launch launch_tutorial launch_turtlesim_launch.py

    如果不凑巧,出现如下错误:

    那就是python文件里面内容的缩进问题了,python对这个很严苛,我们可以根据提示找到对应的行,对照上下文进行缩进即可。

    如果出现如下错误:

    找不到turtle_tf2_py,可通过如下命令进行下载安装:

    $sudo apt-get install ros-iron-rviz2 ros-iron-turtle-tf2-py ros-iron-tf2-ros ros-iron-tf2-tools ros-iron-turtlesim

    (tf2相关知识点从下篇文章会介绍)

    都准备好了之后再次启动顶级launch文件,看看效果:

    左边的是第一个海龟仿真,有两只海龟,开始无轨迹的是turtle1,右边的是第二个海龟仿真,有一只海龟,它是模仿turtle1的,中间的是rviz窗口,刚启动的时候,小海龟已经跑了一点路径了,为了进一步确定效果,我们可以再开一个终端,运行之前的turtle_teletop_key来控制小海龟turtle1的移动:

    $ros2 run turtlesim turtle_teleop_key

    通过rqt_graph来查看这些节点之间的关系:

    今天的内容有点超标,有些后面的内容提前放了上来,还是那句话,先大概了解即可。

    本篇完。 

  • 相关阅读:
    2.9 GBDT模型(下篇)
    [LeetCode解题报告] 30. 串联所有单词的子串
    Vite和Webpack的区别是什么,你站队谁?
    Session会话追踪的实现机制
    kafka + Springboot 实战测试操作完整文档记录
    html form拼凑表单触发后端api
    java169-DatagramPacket 数据报包类
    OpenCV 相机相关函数
    使用 Vue3 + ts 开发一个ProTable
    十三、前端开发知识快速入门
  • 原文地址:https://blog.csdn.net/DIANZI520SUA/article/details/138026928