Robotics StackExchange | Archived questions

ROS2 launch nodes by python script

Hi everybody. I've a question: is there a way to launch nodes without using the "ros2 launch packagename launchfile" command? In particular, I'd like to launch a node directly from another node script written in Python. For example by using LaunchDescription structure with the informations related to the node I want to launch. So is there a python function in ROS2 libraries for accomplishing this? Thank you

Asked by rostyv on 2020-11-18 06:39:41 UTC

Comments

Answers

This can be accomplished using the LaunchService class.

Basic example:

main.py:

from launch import LaunchService

from my_launch_file import generate_launch_description


launch_service = LaunchService()
launch_service.include_launch_description(generate_launch_description())
launch_service.run()

In this case, generate_launch_description() comes from a typical ros2 launch file. For example:

my_launch_file.py:

from launch import LaunchDescription
from launch_ros.actions import Node

def generate_launch_description():
    rviz_node = Node(
        package='rviz2',
        executable='rviz2',
        name='my_rviz_node'
    )
    return LaunchDescription([
        rviz_node,
    ])

Asked by m2-farzan on 2022-05-18 13:38:15 UTC

Comments

I think the launch_service.run() method is blocking. Is there a way to launch the file and continue the script ?

Asked by Sam_Prt on 2022-06-30 07:43:22 UTC

launch_service.run_async()

Asked by buckley.toby on 2023-05-09 12:02:39 UTC