赞
踩
ROS 2中的启动系统负责帮助用户描述其系统的配置,然后按描述执行。系统的配置包括运行什么程序,在哪里运行,传递什么参数,以及ROS特定的约定,这些约定通过为每个组件提供不同的配置,使其易于在整个系统中重用组件。它还负责监测启动过程的状态,并报告和/或对这些过程的状态变化作出反应。
用Python、XML或YAML编写的启动文件可以启动和停止不同的节点,也可以触发和处理各种事件。请参阅将Python、XML和YAML用于ROS 2启动文件,以了解不同格式的描述。提供该框架的包是launch_ros,它使用非ros特定的启动框架作为基础。
设计文档详细说明了ROS 2 launch系统的设计目标(目前并非所有功能都可用)。
我们还是拿小海龟来说事。
创建一个launch文件夹,我们可以用python/xml/yaml这几种方式写就,如turtlesim_mimic_launch.py:
- from launch import LaunchDescription
- from launch_ros.actions import Node
-
- def generate_launch_description():
- return LaunchDescription([
- Node(
- package='turtlesim',
- namespace='turtlesim1',
- executable='turtlesim_node',
- name='sim'
- ),
- Node(
- package='turtlesim',
- namespace='turtlesim2',
- executable='turtlesim_node',
- name='sim'
- ),
- Node(
- package='turtlesim',
- executable='mimic',
- name='mimic',
- remappings=[
- ('/input/pose', '/turtlesim1/turtle1/pose'),
- ('/output/cmd_vel', '/turtlesim2/turtle1/cmd_vel'),
- ]
- )
- ])
turtlesim_mimic_launch.xml:
- <launch>
- <node pkg="turtlesim" exec="turtlesim_node" name="sim" namespace="turtlesim1"/>
- <node pkg="turtlesim" exec="turtlesim_node" name="sim" namespace="turtlesim2"/>
- <node pkg="turtlesim" exec="mimic" name="mimic">
- <remap from="/input/pose" to="/turtlesim1/turtle1/pose"/>
- <remap from="/output/cmd_vel" to="/turtlesim2/turtle1/cmd_vel"/>
- </node>
- </launch>
turtlesim_mimic_launch.yaml:
launch: - node: pkg: "turtlesim" exec: "turtlesim_node" name: "sim" namespace: "turtlesim1" - node: pkg: "turtlesim" exec: "turtlesim_node" name: "sim" namespace: "turtlesim2" - node: pkg: "turtlesim" exec: "mimic" name: "mimic" remap: - from: "/input/pose" to: "/turtlesim1/turtle1/pose" - from: "/output/cmd_vel" to: "/turtlesim2/turtle1/cmd_vel"
上面的所有启动文件都是从turtlesim包启动一个由三个节点组成的系统。该系统的目标是启动两个海龟仿真窗口,让一只海龟模仿另一只海龟的动作。
当启动两个turtlesim节点时,它们之间唯一的区别是名称空间值。唯一的命名空间允许系统启动两个节点,而不会出现节点名称或主题名称冲突。该系统中的两只乌龟都会接收同一主题的命令,并在同一主题上发布姿势。通过独特的名称空间,可以区分针对不同海龟的消息。
最后一个节点也来自turtlesim包,但有一个不同的可执行文件:mimic。此节点以重新映射的形式添加了配置详细信息。mimic的/input/spose主题被重新映射到/turtlesim1/turtle1/pose,而它的/output/cmd_vel主题被映射到/turnlesim2/turtle1/cmd_vel。这意味着mimic将订阅/turtlesim1/sim的姿势主题,并将其重新发布给/turtlesim2/sim的速度命令主题进行订阅。换句话说,turtlesim2将模仿turtlesim1的动作。
python
导入相关python模块:
- from launch import LaunchDescription
- from launch_ros.actions import Node
launch描述函数:
- def generate_launch_description():
- return LaunchDescription([
-
- ])
两个海龟节点的启动相关描述:
- Node(
- package='turtlesim',
- namespace='turtlesim1',
- executable='turtlesim_node',
- name='sim'
- ),
- Node(
- package='turtlesim',
- namespace='turtlesim2',
- executable='turtlesim_node',
- name='sim'
- ),
mimic节点的启动相关描述,其中重映射了主题:
- Node(
- package='turtlesim',
- executable='mimic',
- name='mimic',
- remappings=[
- ('/input/pose', '/turtlesim1/turtle1/pose'),
- ('/output/cmd_vel', '/turtlesim2/turtle1/cmd_vel'),
- ]
- )
XML
两个海龟节点的启动说明:
- <node pkg="turtlesim" exec="turtlesim_node" name="sim" namespace="turtlesim1"/>
- <node pkg="turtlesim" exec="turtlesim_node" name="sim" namespace="turtlesim2"/>
mimic节点的启动说明(重映射):
- <node pkg="turtlesim" exec="mimic" name="mimic">
- <remap from="/input/pose" to="/turtlesim1/turtle1/pose"/>
- <remap from="/output/cmd_vel" to="/turtlesim2/turtle1/cmd_vel"/>
- </node>
YAML
两个海龟节点的启动说明:
- - node:
- pkg: "turtlesim"
- exec: "turtlesim_node"
- name: "sim"
- namespace: "turtlesim1"
-
- - node:
- pkg: "turtlesim"
- exec: "turtlesim_node"
- name: "sim"
- namespace: "turtlesim2"
mimic节点启动说明(重映射):
- - node:
- pkg: "turtlesim"
- exec: "mimic"
- name: "mimic"
- remap:
- -
- from: "/input/pose"
- to: "/turtlesim1/turtle1/pose"
- -
- from: "/output/cmd_vel"
- to: "/turtlesim2/turtle1/cmd_vel"
三种格式的launch文件都可以用ros2 launch <launch_file>命令执行,我们进入launch文件夹,执行:
$ros2 launch turtlesim_mimic_launch.py
需要注意的是,如果是功能包路径里面的launch文件,我们则需要按ros2 launch <package_name> <launch_file>命令执行。另外还有一点,我们最好在功能包的package.xml里面增加<exec_depend>对launch支持的依赖项,这样就能确保功能包在构建完成之后可以支持launch运行:
<exec_depend>ros2launch</exec_depend>
我们来看看终端返回什么信息:
我们再来手动发布主题让小海龟动起来,看看另外一只是否也跟着跑:
$ros2 topic pub -r 1 /turtlesim1/turtle1/cmd_vel geometry_msgs/msg/Twist "{linear: {x: 2.0, y: 0.0, z: 0.0}, angular: {x: 0.0, y: 0.0, z: -1.8}}"
然也。
另起一个终端,执行下面命令看看通过launch文件启动的系统各节点之间的关系:
$rqt_graph
从上图可以看到,一个隐藏的节点(运行的ros2 topic pub命令)正在将数据发布到左侧的/turtlesim1/turtle1/cmd_vel主题,/turtlesim1/sim节点订阅了该主题。而mimic订阅了/turtlesim1/sim的姿势主题,并发布到/turtlesim2/sim的cmd_vel主题。注意:如果没有上面的内容,首先确认节点是否都在运行,其次点击左上角的刷新按钮刷新一下。
本篇完。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。