[Documentation] [TitleIndex] [WordIndex

The roslaunch package comes with a Python API to assist in the process of launching ROS Nodes.

  Show EOL distros: 

roslaunch API

You can see the source for roslaunch at

Simple usage example

import roslaunch

package = 'rqt_gui'
executable = 'rqt_gui'
node = roslaunch.core.Node(package, executable)

launch = roslaunch.scriptapi.ROSLaunch()
launch.start()

process = launch.launch(node)
print process.is_alive()
process.stop()

This example starts and stops an instance of rqt_gui from package rqt_gui.

Starting a launch file

This example shows you how to refer a .launch file (replace the file path of your choice) and start/shutdown it.

import roslaunch
import rospy

rospy.init_node('en_Mapping', anonymous=True)
uuid = roslaunch.rlutil.get_or_generate_uuid(None, False)
roslaunch.configure_logging(uuid)
launch = roslaunch.parent.ROSLaunchParent(uuid, ["/home/haier/catkin_ws/src/testapi/launch/test_node.launch"])
launch.start()
rospy.loginfo("started")

rospy.sleep(3)
# 3 seconds later
launch.shutdown()

Roslaunch file with command line-style arguments

New in Lunar

Single launchfile with argument list

import roslaunch

cli_args = ['/home/mosaic/catkin_ws/src/robot/launch/id.launch','vel:=2.19']
roslaunch_args = cli_args[1:]
roslaunch_file = [(roslaunch.rlutil.resolve_launch_arguments(cli_args)[0], roslaunch_args)]

parent = roslaunch.parent.ROSLaunchParent(uuid, roslaunch_file)

parent.start()

Multiple launchfiles with and without argument lists

import roslaunch

uuid = roslaunch.rlutil.get_or_generate_uuid(None, False)
roslaunch.configure_logging(uuid)

cli_args1 = ['pkg1', 'file1.launch', 'arg1:=arg1', 'arg2:=arg2']
cli_args2 = ['pkg2', 'file2.launch', 'arg1:=arg1', 'arg2:=arg2']
cli_args3 = ['pkg3', 'file3.launch']
roslaunch_file1 = roslaunch.rlutil.resolve_launch_arguments(cli_args1)
roslaunch_args1 = cli_args1[2:]

roslaunch_file2 = roslaunch.rlutil.resolve_launch_arguments(cli_args2)
roslaunch_args2 = cli_args2[2:]

roslaunch_file3 = roslaunch.rlutil.resolve_launch_arguments(cli_args3)

launch_files = [(roslaunch_file1, roslaunch_args1), (roslaunch_file2, roslaunch_args2), roslaunch_file3]

parent = roslaunch.parent.ROSLaunchParent(uuid, launch_files)

parent.start()

"Inheriting" from a launch file

Sometimes it's handy to start with a "base" launch file and launch additional nodes after running it, while referring to the same object for cleanup:

import roslaunch

roslaunch.configure_logging(uuid)
launch = roslaunch.scriptapi.ROSLaunch()
launch.parent = roslaunch.parent.ROSLaunchParent(uuid, "path/to/base.launch")
launch.start()

# Start another node
node = roslaunch.core.Node(package, executable)
launch.launch(node)

try:
  launch.spin()
finally:
  # After Ctrl+C, stop all nodes from running
  launch.shutdown()

Node Arguments

roslaunch.core.Node(package, node_type, name=None, namespace='/', 
                 machine_name=None, args='', 
                 respawn=False, respawn_delay=0.0, 
                 remap_args=None, env_args=None, output=None, cwd=None, 
                 launch_prefix=None, required=False, filename='<unknown>')

2024-11-16 14:36