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

Or you can look at the rendered API doc (ROS Melodic).

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()

NOTE: ROSLaunch() call may fail or even hang when there's no running ROS Master node available.

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>')
  • package: node package name, str
  • node_type: node type, str
  • name: node name, str
  • namespace: namespace for node, str
  • machine_name: name of machine to run node on, str
  • args: argument string to pass to node executable, str
  • respawn: if True, respawn node if it dies, bool
  • respawn_delay: if respawn is True, respawn node after delay, float
  • remap_args: list of [(from, to)] remapping arguments, [(str, str)]
  • env_args: list of [(key, value)] of additional environment vars to set for node, [(str, str)]
  • output: where to log output to, either Node, 'screen' or 'log', str
  • cwd: current working directory of node, either 'node', 'ROS_HOME'. Default: ROS_HOME, str
  • launch_prefix: launch command/arguments to prepend to node executable arguments, str
  • required: node is required to stay running (launch fails if node dies), bool
  • filename: name of file Node was parsed from, str

Wiki: roslaunch/API Usage (last edited 2022-11-16 10:53:22 by GavrielKrauss)