此包提供了一个 Python 类,用于帮助编写 ROS 2 启动文件。
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).
Humble
Jazzy
Lyrical
Rolling
sl = SimpleLauncher(namespace = '', use_sim_time = None, scope_included_files = False)
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 nodeuse_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.use_sim_time is None (default) then no particular value is forwarded to the nodesscope_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.sl.node(package, executable, **node_args) where
package is the node packageexecutable is the name of the executablenode_args are any additional Node argumentssl.include(package, launch_file, launch_dir = None, launch_arguments=None) where
package is the package of the included launch filelaunch_file is the name of the launch filelaunch_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 fileThis 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.
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 setverbosity 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.
In the launch API, differents types are expected for:
key, value) pairskey, value) pairskey:=value stringsThe 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.
simple_launch allows declaring launch arguments and getting them in return.
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.
sl.arg(name): returns the argument name as a SimpleSubstitution
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 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.
with sl.group(ns=sub_namespace):
sl.node(package, executable) with sl.group(if_condition=True):
sl.node(package, executable)
with sl.group(unless_condition=):
sl.node(package, executable)sl.py_eval as shown below. 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:
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.
The when argument wraps events from the launch.event_handlers module. It combines an event and a delay (0 by default)
…This syntax adds the composition/composition::Talker as a ComposableNode
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:
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_timeThe 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 constructoruse_sim_time was set to 'auto', depending on the /clock topic being advertizeduse_sim_time was set to True or FalseIn 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:
SimpleLauncher instance and the argument declaration should be done in the main body of your launch file.launch_setup) that takes no argument, where the logic of the launch file resides.sl.arg will be basic Python types, obtained from performing the substitutions.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:
# 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 stuffMethods 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.
The following syntax builds the SimpleSubstitution corresponding to .xacro:
file_name = sl.arg('robot') + '.xacro'
deprecated: sl.name_join(sl.arg('robot'), '.xacro')
The following syntax builds the SimpleSubstitution corresponding to /urdf/.xacro:
file_name = sl.arg('robot') + '.xacro'
urdf_file = os.path.join(get_package_share_directory(package),'urdf')/file_nameObviously 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')
SubstitutionWhen inside an OpaqueFunction, some functions might still return a Substitution. They can be forced to be performed with the following syntax:
cmd = Command('ros2 topic list'.split()) # returns a Substitution
sl.as_raw_type(cmd) # evaluates the Substitution as a string, if inside an OpaqueFunctionpath = 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 pathfile_name is the name of the file to findfile_dir is the path inside the packageIf 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))
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 filedescription_dir is the sub-directory of the file. If omitted, let the script search for the file assuming it is a raw stringxacro_args is a dictionary of arguments to forward to xacronode_args are any additional arguments for robot_state_publisher (remappings / parameters)sl.py_eval will evaluate the given arguments as a Python expression, possibly performed if in an Opaque Function.
# 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,或尚未同步最近议题。