Ask Your Question

Access launch argument in LaunchFile ROS2

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

relffok gravatar image

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

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 -0500 )edit

2 Answers

Sort by ยป oldest newest most voted

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

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

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

guru_florida gravatar image

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

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 -0500 )edit

It appears that Foxy has had the feature added.

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

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

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

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools



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

Seen: 819 times

Last updated: Aug 24 '20