跳转至

launch文件

Launch 文件允许您同时启动和配置多个包含 ROS 2 节点的可执行文件。

launch文件编写

  1. 创建文件夹用来存储launch文件
mkdir launch
  1. 创建launch文件
touch launch/turtlesim_mimic_launch.py
  1. 编写launch文件
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'
        )
    ])
  1. 进入launch文件夹启动launch文件
ros2 launch turtlesim_mimic_launch.py

python工程包中launch文件编写

  1. 在python工程包下创建launch文件夹编写launch文件
  2. 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'))
    ]
)