ROS2 之 使用 python Launch

1990阅读 0评论2020-06-18 iibull
分类:其他平台

用python写的launch文件可以启动和停止不同的节点,以及触发和操作各种事件。提供这个框架的包是launch_ros.   sudo apt install ros-foxy-launch-ros

通过 ros2 pkg create  --dependencies [deps] 会自动建立 launch 目录.

在 Python 中, 需要在 setup.py中加入data_files, 然后colcon 识别到 launch文件.

点击(此处)折叠或打开

  1. import os
  2. from glob import glob
  3. from setuptools import setup

  4. package_name = 'my_package'

  5. setup(
  6.     # Other parameters ...
  7.     data_files=[
  8.         # ... Other data files
  9.         # Include all launch files. This is the most important line
  10.         (os.path.join('share', package_name), glob('launch/*.launch.py'))
  11.     ]
  12. )
然后建立 my_script.launch.py 文件. 这种文件和 launch 文件的规则不同, 

点击(此处)折叠或打开  my_script.launch.py

  1. import launch
  2. import launch.actions
  3. import launch.substitutions
  4. import launch_ros.actions

  5. def generate_launch_description():
  6.     return launch.LaunchDescription([
  7.         launch.actions.DeclareLaunchArgument(
  8.             'node_prefix',
  9.             default_value=[launch.substitutions.EnvironmentVariable('USER'), '_'],
  10.             description='Prefix for node names'),
  11.         launch_ros.actions.Node(
  12.             package='demo_nodes_cpp', executable='talker', output='screen',
  13.             name=[launch.substitutions.LaunchConfiguration('node_prefix'), 'talker']),
  14.     ])
然后使用 
    
ros2 launch my_package script.launch.py
上一篇:ROS2 之 ros2doctor
下一篇:ROS2 之 通过命令行传递参数.