evaluate launch argument as python variable
Hi I would like to access a launch argument as a python variable:
Say I type
ros2 launch pkg launch_file my_arg:="foo"
In my launch file I have the following:
DeclareLaunchArgument(name="my_arg", default_value="hello")
now I would like to do some python logic with the value I passed. E.g. in an if statement. Jowever I need to define my_arg somehow...
if(my_arg == "hello"):
# do something
I have not found anything to get the value of the launch argument. What is the best way to access the value?
Asked by MaxDio on 2022-07-28 03:54:24 UTC
Answers
There's no convenient way to do this. I recommend you explain what you're trying to do. There's likely a better way than using if(my_arg == "hello"):
style logic.
Also, I recommend using XML where you can. The Python API was not designed to be used directly, and as a result it is not very ergonomic.
Edit:
You can control when things are actually included using "conditionals", for example (based on your comment):
<launch>
<arg name="a_not_b" default="true" />
<!-- argument a_option is required because it does not have a default value -->
<arg name="a_option" if="$(var a_not_b)" />
<group unless="$(var a_not_b)">
<arg name="b_option1" default="true" />
<arg name="b_option2" default="true" />
</group>
</launch>
In this example, you have a required argument a_option
, which if you don't supply it you get:
% ros2 launch /tmp/example.launch.xml
[INFO] [launch]: All log files can be found below /Users/william/.ros/log/2022-07-28-13-17-55-361895-markook.local-67414
[INFO] [launch]: Default logging verbosity is set to INFO
[ERROR] [launch.actions.declare_launch_argument]: Required launch argument "a_option" (description: "no description given") was not provided
[ERROR] [launch]: Caught exception in launch (see debug for traceback): Required launch argument "a_option" was not provided.
But then, due to the condition "if=" attribute on it, you can avoid that with the option a_not_b
:
% ros2 launch /tmp/example.launch.xml a_not_b:=false
[INFO] [launch]: All log files can be found below /Users/william/.ros/log/2022-07-28-13-18-03-013500-markook.local-67418
[INFO] [launch]: Default logging verbosity is set to INFO
However, since launch cannot know which arguments will be included until you actually run it, when you ask it to show the arguments, every possible argument will be shown, with only a *
for the ones that may be conditionally used:
% ros2 launch /tmp/example.launch.xml --show-arguments
Arguments (pass arguments as '<name>:=<value>'):
'a_not_b':
no description given
(default: 'true')
'a_option':*
no description given
'b_option1':*
no description given
(default: 'true')
'b_option2':*
no description given
(default: 'true')
* argument(s) which are only used if specific conditions occur
Asked by William on 2022-07-28 05:49:39 UTC
Comments
Hmm thats a pity. I would like to call a python function based on an argument and define other launch arguments
Asked by MaxDio on 2022-07-28 07:10:44 UTC
You can create a group to contain the "other launch arguments", then condition that group on the value of my_arg
. I can add a small example in the answer as an edit.
Asked by William on 2022-07-28 15:06:33 UTC
I updated my answer. If you must do this in Python, then that is also possible, though it is much less readable than with XML.
Asked by William on 2022-07-28 15:24:07 UTC
Comments