Skip to content

[Development] ROS2 launch files

Hajdu Csaba edited this page Oct 6, 2021 · 1 revision

ROS2 launch files

ROS2 introduces a brand new Python-based launch file semantics.

Basic semantics

from launch import LaunchDescription
from launch.actions import IncludeLaunchDescription
from launch.substitutions import EnvironmentVariable
from launch.launch_description_sources import PythonLaunchDescriptionSource
from launch.substitutions import ThisLaunchFileDir
from ament_index_python.packages import get_package_share_directory

import os
import launch_ros.actions
import pathlib

def generate_launch_description():
    return LaunchDescription([])

Launching node

The following code snippet launches a node from a predefined package:

from launch import LaunchDescription
from launch.actions import IncludeLaunchDescription
from launch.substitutions import EnvironmentVariable
from launch.launch_description_sources import PythonLaunchDescriptionSource
from launch.substitutions import ThisLaunchFileDir
from ament_index_python.packages import get_package_share_directory

import os
import launch_ros.actions
import pathlib

parameters_file_name = 'gripp3r.yaml'

def generate_launch_description():
    #parameters_file_path = str(pathlib.Path(__file__).parents[1]) # get current path and go one level up
    #sick_parameters_file_path = str(pathlib.Path(__file__).parents[1]) # get current path and go one level up
    state_publisher_dir = get_package_share_directory('neo_relayboard_v1') + '/launch'
    config_dir = get_package_share_directory('neo_relayboard_v1')
    parameters_file_path = config_dir
    sick_parameters_file_path = config_dir
    parameters_file_path += '/configuration/' + parameters_file_name
    return LaunchDescription([
        launch_ros.actions.Node(
            package='my_package',
            node_executable='my_node',
            output='screen',
            parameters=[
                parameters_file_path
            ],
         ),
    ])
Clone this wiki locally