ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

In ROS 2 you need to follow these steps:

  1. Create a LaunchDescription object. If you have seen a regular python launch file, this is what generate_launch_description() returns. This object contains information about nodes to be launched, etc.

  2. Create a LaunchService object from launch python module.

  3. Pass the LaunchDescription to LaunchService using include_launch_description method of the LaunchService instance.

  4. Use run method of the LaunchService. (Use more advanced functions if you need async, etc. See more info here.)

Here's an example:

import launch
from launch.substitutions import Command
import launch_ros


def generate_launch_description():
    model_path = '/path/to/my/robot.urdf'

    robot_state_publisher_node = launch_ros.actions.Node(
        package='robot_state_publisher',
        executable='robot_state_publisher',
        parameters=[{'robot_description': Command(['xacro ', model_path])}]
    )

    spawn_entity = launch_ros.actions.Node(
        package='gazebo_ros', 
        executable='spawn_entity.py',
        arguments=['-entity', 'sam_bot', '-topic', 'robot_description'],
        output='screen'
    )

    return launch.LaunchDescription([
        launch.actions.ExecuteProcess(cmd=['gazebo', '--verbose', '-s', 'libgazebo_ros_factory.so'], output='screen'),
        robot_state_publisher_node,
        spawn_entity,
    ])


def main():
    launch_description = generate_launch_description()
    launch_service = launch.LaunchService()
    launch_service.include_launch_description(launch_description)
    launch_service.run()


if __name__ == '__main__':
    main()