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

ROS2 launch nodes by python script

asked 2020-11-18 05:39:41 -0600

rostyv gravatar image

Hi everybody. I've a question: is there a way to launch nodes without using the "ros2 launch package_name launch_file" 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

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
1

answered 2022-05-18 13:38:15 -0600

m2-farzan gravatar image

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,
    ])
edit flag offensive delete link more

Comments

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

Sam_Prt gravatar image Sam_Prt  ( 2022-06-30 07:43:22 -0600 )edit
1

launch_service.run_async()

buckley.toby gravatar image buckley.toby  ( 2023-05-09 12:02:39 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2020-11-18 05:39:41 -0600

Seen: 1,133 times

Last updated: May 18 '22