赞
踩
用Python
、XML
或YAML
编写的启动文件可以启动和停止不同的节点,以及触发和处理各种事件
提供这个框架的包是launch_ros
,它在下面使用了非ROS特定的launch
框架
那么就创建一个独立的py_launch_example
启动功能包
cd ~/ros2_ws/src
ros2 pkg create py_launch_example --build-type ament_python
在功能包中新建目录launch
cd ~/ros2_ws/src/py_launch_example
mkdir launch
在package.xml
清单文件中,添加对依赖项的声明
<exec_depend>ros2launch</exec_depend>
打开 setup.py
文件,添加所有launch
启动文件:
import os # Add
from glob import glob # Add
from setuptools import setup
package_name = 'py_launch_example'
setup(
# Other parameters ...
data_files=[
# ... Other data files
# Include all launch files.
(os.path.join('share', package_name), glob('launch/*launch.[pxy][yma]*'))
]
)
进入launch
文件夹,创建启动文件,启动两只海龟,并设置模仿运动
cd ~/ros2_ws/src/py_launch_example/launch
可以选择多种方式
创建 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'),
]
)
])
启动文件应该定义generate_launch_description()
函数
该函数返回一个launch. launchdescription()
,以供ros2 launch
使用
创建 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"
进入工作空间根目录
cd ~/ros2_ws
编译:
colcon build --packages-select py_launch_example
现在可以打开一个新终端,启动海龟模仿运动启动文件,可以启动了两只海龟:
ros2 launch py_launch_example turtlesim_mimic_launch.py
再打开一个新终端,并在/turtlesim1/turtle1/cmd_vel
主题上运行ros2 topic pub
命令,让第一只海龟移动:
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}}"
会看到两只乌龟走相同路径
打开一个新终端并运行rqt_graph
,以更好地了解启动文件中节点之间的关系
rqt_graph
终端命令ros2 topic pub
将数据发布到左侧的/turtlesim1/turtle1/cmd_vel
话题,/turtlesim1/sim
节点订阅了该话题
模仿节点mimic
订阅到/turtlesim1/turtle1/pose
话题,并发布/turtlesim2/turtle1/cmd_vel
话题,/turtlesim2/sim
节点订阅了该话题
所以就呈现了两只乌龟走相同路径
谢谢
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。