< >
Home » ROS2与launch入门教程 » ROS2与launch入门教程-使用Launch启动和监控多个节点

ROS2与launch入门教程-使用Launch启动和监控多个节点

ROS2与launch入门教程-使用Launch启动和监控多个节点

说明:

  • 介绍使用Launch启动和监控多个节点

launch系统:

  • ROS 2中的launch(启动)系统负责帮助用户描述他们系统的配置,然后按照描述执行它。

  • 系统的配置包括运行什么程序、在哪里运行它们、传递它们的参数以及 ROS 特定的约定,这些约定通过给它们不同的配置,使得在整个系统中重用组件变得容易。

  • 它还负责监视启动的进程的状态,并报告和/或对这些进程的状态变化做出反应。

  • 用 Python 编写的启动文件可以启动和停止不同的节点以及触发和作用于各种事件。

  • 提供这个框架的包是launch_ros,它使用了底层非ROS特定的启动框架。

  • 设计文档详细说明了 ROS 2 launch系统的设计目标(目前并非所有功能都可用)。

步骤: 

  • 1.首先要让ROS包知道那里找到launch文件
  • python包主要修改setup.py
  • 具体内容如
import os
from glob import glob
from setuptools import setup

package_name = 'my_package'

setup(
    # Other parameters ...
    data_files=[
        # ... Other data files
        # Include all launch files. This is the most important line here!
        (os.path.join('share', package_name), glob('launch/*launch.py'))
    ]
)
  • 增加setup的参数data_files内容,指定使用的launch文件格式
  • C++包修改CMakeLists.txt
  • 具体内容如
# Install launch files.
install(DIRECTORY
  launch
  DESTINATION share/${PROJECT_NAME}/
)
  • ament_package()前增加上面代码
  • 2.编写launch文件
  • launch的文件应该以*launch.py的格式结束,如my_script_launch.py或my_script.launch.py
  • 定义generate_launch_description(),返回launch.LaunchDescription(),以便ros2 launch能调用
  • 代码如: 
import platform

import launch
import launch.actions
import launch.substitutions
import launch_ros.actions

env_var_name = 'USER' if platform.system() != 'Windows' else 'USERNAME'

def generate_launch_description():
    return launch.LaunchDescription([
        launch.actions.DeclareLaunchArgument(
            'node_prefix',
            default_value=[launch.substitutions.EnvironmentVariable(env_var_name), '_'],
            description='Prefix for node names'),
        launch_ros.actions.Node(
            package='demo_nodes_cpp', executable='talker', output='screen',
            name=[launch.substitutions.LaunchConfiguration('node_prefix'), 'talker']),
    ])
  • launch文件可以作为独立文件来启动
  • 更常用是使用ros2工具来启动,如
ros2 launch my_package my_script_launch.py
  • 或者作为独立文件启动 
ros2 launch my_script_launch.py

ROS2的python包launch文件测试:

  • 创建工作空间和包
mkdir -p ~/launch_ws/src
cd ~/launch_ws/src
ros2 pkg create --build-type ament_python test_launch_py
  • 创建luanch目录
mkdir -p ~/launch_ws/src/test_launch_py/launch
  • 拷贝turtlesim_mimic_launch.py
cp ~/launch_ws/turtlesim_mimic.launch.py ~/launch_ws/src/test_launch_py/launch/turtlesim_mimic.launch.py
  • 修改setup.py
vim ~/launch_ws/src/test_launch_py/setup.py
  • 内容修改为
from setuptools import setup
import os
from glob import glob

package_name = 'test_launch_py'

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': [
        ],
    },
)
  • 指定构建test_launch_py包,并使用symlink的方式
cd ~/launch_ws/
colcon build --symlink-install --packages-select test_launch_py
  • 加载工作空间
. ~/launch_ws/install/local_setup.bash
  • 启动launch文件 
ros2 launch test_launch_py turtlesim_mimic.launch.py
  • 控制海龟1运行
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}}"

ROS2的c++包launch文件测试:

  • 创建ros2包test_launch_cpp
cd -p ~/launch_ws/src/
ros2 pkg create --build-type ament_cmake  test_launch_cpp
  • 创建luanch目录
mkdir -p ~/launch_ws/src/test_launch_cpp/launch
  • 拷贝之前建立的launch文件
cp ~/launch_ws/turtlesim_mimic.launch.py ~/launch_ws/src/test_launch_cpp/launch/
  • CMakelist.txt增加内容
install(
  DIRECTORY   launch  
  DESTINATION share/${PROJECT_NAME}
)
  • package.xml增加内容
<exec_depend>launch</exec_depend>
<exec_depend>launch_ros</exec_depend>  
  • 指定构建test_launch_cpp包,并使用symlink的方式
cd ~/launch_ws/
colcon build --symlink-install --packages-select test_launch_cpp
  • 加载工作空间
. ~/launch_ws/install/local_setup.bash
  • 启动launch文件 
ros2 launch test_launch_cpp turtlesim_mimic.launch.py
  • 控制海龟1运行
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}}"

纠错,疑问,交流: 请进入讨论区点击加入Q群

获取最新文章: 扫一扫右上角的二维码加入“创客智造”公众号


标签: ros2与launch入门教程