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

Access launch argument in LaunchFile ROS2

asked 2019-12-26 12:40:47 -0600

relffok gravatar image

updated 2020-06-04 03:23:17 -0600

KenYN gravatar image

Hi, I've been trying to Use my Launch Arguments inside of my Launch File for further purposes. So I can declare it by using launch.actions.DeclareLaunchArgument( 'param1', default_value="defaultString") and access it later using launch.substitutions.LaunchConfiguration( 'param1') to pass it to a node or whatever. The type of this element is a launch.substitutions.launch_configuration.LaunchConfiguration object.

Now I want to use the content of this argument as a string for building filepaths for example. I know several functions are able to interpret the LaunchConfiguration object but I specifically to read it as a string. I have not found a way to do so. As I need to access multiple arguments several times, I am looking for a simple implementation.

Any ideas? Thanks!

(I am working with ROS2 Dashing)

edit retag flag offensive close merge delete


Hi @relffok, have you figured out how to use a launch argument as a string? I'm trying to pass an argument (file path) to open(yaml_file, 'r').read() function and it gives me the following error when I launch it:

TypeError: expected str, bytes or os.PathLike object, not LaunchConfiguration
kwang gravatar image kwang  ( 2021-02-17 12:48:06 -0600 )edit

2 Answers

Sort by ยป oldest newest most voted

answered 2019-12-31 10:27:41 -0600

guru_florida gravatar image

updated 2019-12-31 10:29:17 -0600

I don't believe you can access the argument during building of the description. I think you can still do what you want though as far as building paths based on an argument by passing in an array and have the path concat'd at run time.

For example, I want to specify the underlying hardware launch file as an argument. If I give the argument hardware:=rpi1 then my main launch file will include launch file instead of the default in it's launch description:

Import required modules:

from launch.launch_description_sources import PythonLaunchDescriptionSource
from launch.substitutions import ThisLaunchFileDir, LaunchConfiguration

Declare the launch argument, then use the launch argument in a build-path statement to include my parameterized launch file:

launch_desc.add_action( launch.actions.DeclareLaunchArgument('hardware', default_value='hardware') )
launch_desc.add_action( IncludeLaunchDescription(PythonLaunchDescriptionSource([ThisLaunchFileDir(),'/',LaunchConfiguration('hardware'), ''])) )
edit flag offensive delete link more



Thanks for your answer. I did know the possibility using an array. Unfortunately I need to access the arguments as a string type because the LaunchConfiguration element can only be read by launch action functions and I want to use them with other functions as well.

I can't believe that this is not possible yet because I feel its a very basic and handy action to use.

relffok gravatar image relffok  ( 2020-01-02 11:48:04 -0600 )edit

It appears that Foxy has had the feature added.

KenYN gravatar image KenYN  ( 2020-06-04 04:27:52 -0600 )edit

Is there a good way to get the argument value? Thank you!

Yanray47 gravatar image Yanray47  ( 2021-04-07 02:15:03 -0600 )edit

@relffok I know this is an old issue, but someone bumped it from another place, and it might help others for me to clarify. Using a substitution is the only way to access the value of the launch argument. You can't just convert it to a string, as it requires the context to be evaluated. If this is confusing, then you might want to stick with xml based launch files as it is more natural to adhere to this restriction there. The Python API is really meant to help implement the markup based frontends like YAML and XML, and so it is declarative rather than imperative.

Hope this helps, and sorry that it's confusing. Others have proposed a more imperative version of the Python API for launch, but that doesn't exist at the moment.

William gravatar image William  ( 2022-03-14 14:10:22 -0600 )edit

answered 2020-08-24 14:26:50 -0600

I was able to evaluate launch arguments in a custom function outside of the LaunchDescription by using the OpaqueFunction launch action feature which takes a context as an argument, see this link:

edit flag offensive delete link more

Question Tools



Asked: 2019-12-26 12:40:47 -0600

Seen: 3,853 times

Last updated: Aug 24 '20