Baike.dev
All toolsOnline toolsAI codingTrendingOpen sourceNewsSubmit
Log in
< Back to tools
S

simple_launch

> 编程语言
Open source

This package provides a Python class to help writing ROS 2 launch files.

99 stars0 likes1 views
WebsiteGitHub

About

This package provides a Python class to help writing ROS 2 launch files.

simple_launch

This package provides a Python class to help writing ROS 2 launch files.

The motivation behind this package is that the ROS 2 launch Python syntax may not be suited for many simple cases such as running basic nodes, spawning a robot_state_publisher, and grouping nodes in namespaces or components.

Thanks: this package is inspired by ros2_launch_util.

The entry point is the SimpleLauncher class, which has several capabilities.

Note as of ROS 2 Humble the XML launch syntax is almost feature-equal to Python. For simple cases it might be enough, and simple_launch can be used for more advanced usage (notably Gazebo interaction).

ROS Packages

Humble

Jazzy

Lyrical

Rolling

Basic syntax

Namespace and argument parser initialization

sl = SimpleLauncher(namespace = '', use_sim_time = None, scope_included_files = False)

  • will initialize all nodes relative to the given namespace
  • if use_sim_time is a Boolean, creates a use_sim_time launch argument with this value as the default and forwards it to all nodes, unless explicitely specified when running the node
  • if use_sim_time is 'auto', then SimpleLauncher will set it to True if the /clock topic is advertized (case of an already running simulation). This may have side effects if the /clock topic is advertized but you want to use this launch file with system clock.
  • if use_sim_time is None (default) then no particular value is forwarded to the nodes
  • if scope_included_files is False (default) then including another launch file that shares the same argument but while passing another value for this argument, will also modify the value of this argument after the inclusion. This is the default behavior of launch but can be undesired. Inside an OpaqueFunction the behavior is to be scoped anyway (the argument value is resolved) and scope_included_files has no effect.

Node registration

sl.node(package, executable, **node_args) where

  • package is the node package
  • executable is the name of the executable
  • node_args are any additional Node arguments

Launch file include

sl.include(package, launch_file, launch_dir = None, launch_arguments=None) where

  • package is the package of the included launch file
  • launch_file is the name of the launch file
  • launch_dir is its directory inside the package share (None to have it found)
  • launch_arguments is a dictionary of arguments to pass to the included launch file

Call a service at launch

This line runs a temporary client that waits for a service and calls it when available:

sl.call_service(server, request = None, verbosity = '') where

  • server is the path to some service (possibly namespaced). The service type is deduced when it becomes available.
  • request is a dictionary representing the service request. If None or incomplete, will use the service request default values.
  • verbosity let the underlying node describe what it is doing: 'req' for request info, 'res' for response info or both with 'reqres'

If any request parameter is __ns it will be changed to the current namespace.

Setting parameters

This line runs a temporary client that waits for a node and changes its parameters when available:

sl.set_parameters(node_name, parameters: dict = {}, verbosity = '') where

  • node_name is the name of the node (possibly namespaced)
  • parameters is a dictionary of (name, value) parameters to be set
  • verbosity let the underlying node describe what it is doing: 'req' for request info, 'res' for response info or both with 'reqres'

This calls the set_parameters service of the node with the passed types. Possible errors may happen if the parameters do not exist or are of a different type.

Robust types for parameters

In the launch API, differents types are expected for:

  • node parameters: a list of dictionaries
  • node remappings: a list of (key, value) pairs
  • included launch arguments: a list of (key, value) pairs
  • xacro arguments: a concatenation of key:=value strings

The sl.include, sl.node and xacro_args calls allow using any type (the simplest being a single dictionary) and will convert to the one expected by the API.

Launch arguments

simple_launch allows declaring launch arguments and getting them in return.

Declare a launch argument

sl.declare_arg(name, default_value, description = None): declare and returns the argument

Contrary to the base API, the default value is a raw Python type.

Retrieve a launch argument

sl.arg(name): returns the argument name as a SimpleSubstitution

Retrieve several arguments as a dictionary

sl.arg_map('robot', 'x', 'y'): returns {'robot': , 'x': , 'y': }

Typical when forwarding some launch arguments to a node or an included launch file.

Groups or scopes

Groups are created through the with sl.group(): syntax and accept, a namespace an if/unless condition and an event.

Actions that are added in a scope inherit from all previous defined groups.

By namespace

python
  with sl.group(ns=sub_namespace):
    sl.node(package, executable)

From a condition

python
  with sl.group(if_condition=True):
    sl.node(package, executable)

  with sl.group(unless_condition=):
    sl.node(package, executable)
  • Only one condition can be set in a group, nested condition must be combined first, or used in nested groups.
  • Combining conditions coming from launch arguments can be done with sl.py_eval as shown below.

From conditional arguments

python
  with sl.group(if_arg='use_gui'):
    sl.node(package, executable)

  with sl.group(unless_arg='dont_do'):
    sl.node(package, executable)

if_arg / unless_arg is expected to be the name of a launch argument. These two lines are equivalent:

python
  with sl.group(if_arg='use_gui'):
  with sl.group(if_condition=sl.arg('use_gui')):

If if_arg / unless_arg is not a string then it is considered as a if_condition / unless_condition.

From events

The when argument wraps events from the launch.event_handlers module. It combines an event and a delay (0 by default)

…

Creating containers

This syntax adds the composition/composition::Talker as a ComposableNode

python
  with sl.container(name='my_container', output='screen'):
    sl.node(package='composition', plugin='Talker', name='talker')

Use the executable and package parameters if you want to use executors other than rclcpp_components's component_container:

python
  with sl.container(name='my_container', output='screen', executable='component_container_isolated'):

It is currently impossible to have group blocks within a container block, as containers can only accept ComposableNodes. A GroupAction containing e.g. PushRosNamespace and a ComposableNode is not itself a ComposableNode.

use_sim_time

The current use_sim_time setting can be retrieved through sl.sim_time that may be:

  • None, if use_sim_time was not set in the SimpleLauncher constructor
  • a raw Boolean, if use_sim_time was set to 'auto', depending on the /clock topic being advertized
  • a Boolean launch argument, if use_sim_time was set to True or False

In all cases, if the use_sim_time parameter is explicitely given to a node, it will be used instead of the SimpleLauncher instance one.

OpaqueFunction with implicit .perform(context)

Most of the use cases can be dealt with substitutions and with sl.group blocks. In order to design more imperative launch files, the OpaqueFunction approach can be used. The main drawback is that potential errors are harder to track.

To do this with simple_launch:

  • the SimpleLauncher instance and the argument declaration should be done in the main body of your launch file.
  • then, define a function (e.g. launch_setup) that takes no argument, where the logic of the launch file resides.
  • all arguments obtained through sl.arg will be basic Python types, obtained from performing the substitutions.
  • finally just export generate_launch_description = sl.launch_description(opaque_function = launch_setup).

Compare example_launch.py and example_opaque_launch.py to see the two approaches on the same logic.

Note that inside an OpaqueFunction the if/unless idiom reduces to a basic if/else:

python
# with substitutions
with sl.group(if_arg='some_condition'):
  # do stuff
with sl.group(unless_arg='some_condition'):
  # do other stuff

# with opaque function
if sl.arg('some_condition'):
  # do stuff
else:
  # do other stuff

Other one-liners

Methods listed below return instances of SimpleSubstitution that represent any Substitution, but that provides concatenation (+) and path concatenation (/) operators. It is still a Substitution, not a raw Python type. If run from an OpaqueFunction the underlying Python variable is returned.

String / substitution concatenation

The following syntax builds the SimpleSubstitution corresponding to .xacro:

file_name = sl.arg('robot') + '.xacro'

deprecated: sl.name_join(sl.arg('robot'), '.xacro')

Path concatenation

The following syntax builds the SimpleSubstitution corresponding to /urdf/.xacro:

python
file_name = sl.arg('robot') + '.xacro'
urdf_file = os.path.join(get_package_share_directory(package),'urdf')/file_name

Obviously if all the path elements are raw strings, you should use os.path.join all along.

deprecated: sl.path_join(get_package_share_directory(package), sl.arg('robot'), '.xacro')

Force evaluation of a Substitution

When inside an OpaqueFunction, some functions might still return a Substitution. They can be forced to be performed with the following syntax:

python
cmd = Command('ros2 topic list'.split())  # returns a Substitution
sl.as_raw_type(cmd)  # evaluates the Substitution as a string, if inside an OpaqueFunction

Find a share file

path = sl.find(package, file_name = None, file_dir = None) where:

  • package is the name of the package or None if file_name is already an absolute path
  • file_name is the name of the file to find
  • file_dir is the path inside the package

If file_dir is None but package and file_name are raw strings then the find function will actually look for the file inside the package share, using os.walk.

If file_name is None then the function just returns the path to the package share directory (e.g. get_package_share_directory(package))

Robot state publisher

It is quite common to run a robot_state_publisher from a urdf or xacro file. The line below runs it at the current namespace / condition level:

sl.robot_state_publisher(package, description_file, description_dir=None, xacro_args=None, **node_args) where

  • description_file is a URDF or xacro file
  • description_dir is the sub-directory of the file. If omitted, let the script search for the file assuming it is a raw string
  • xacro_args is a dictionary of arguments to forward to xacro
  • node_args are any additional arguments for robot_state_publisher (remappings / parameters)

Python expressions

sl.py_eval will evaluate the given arguments as a Python expression, possibly performed if in an Opaque Function.

python
# RGB color as a list of [0-255] integers
sl.declare_arg('color', [255,0,0])
# same color as a string of [0-1] numbers (URDF format), note the padding commas to get a string
xacro_color = "'" + sl.py

Issues· 0 open

View all issuesOpen on GitHub

No open issues yet, or sync has not completed.

> Tags

Python

No comments yet. Be the first to share.

> Details

PublishedAug 1, 2026
UpdatedSep 18, 2026
Category编程语言
PricingOpen source

> Related tools

T
TypeScript
JavaScript 的超集,为前端与全栈提供静态类型
P
Python
通用编程语言,广泛用于 Web、数据与 AI
G
Go
Google 推出的简洁高效系统语言