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

Convert python launch arg to normal python variable

asked 2022-04-11 10:46:43 -0500

jn42 gravatar image

updated 2022-04-11 12:06:41 -0500

Is there a way to pass an argument to a python launch file and convert it to a normal in-built python type to use within the python launch file?

E.g. ros2 launch my_launch_file.launch.py temp_arg:="some string" and then

# my_launch_file.launch.py
str_arg = LaunchConfiguration("temp_arg")
print(type(str_arg)) # <class 'launch.substitutions.launch_configuration.LaunchConfiguration'>
# Instead, I would like the value of str_arg to be of type <class 'str'>
# NOTE: the code below doesn't work due to the type of str_arg
if (str_arg == "config1"):
  # some logic here
else:
  # some other logic here

The reason being that I want to use the str_arg var to do some other logic in the launch file

edit retag flag offensive close merge delete

2 Answers

Sort by ยป oldest newest most voted
0

answered 2022-04-11 11:25:26 -0500

ljaniec gravatar image

updated 2022-04-11 14:08:56 -0500

You can use e.g.

self.get_parameter("your_parameter").get_parameter_value().string_value

EDIT:

After the updated question - can you maybe use the PythonExpression substitution?

edit flag offensive delete link more

Comments

I see the confusion. I modified the question to make it more clear that I want to use the argument within the python launch file and NOT a python node (I understand how to get the value of a parameter from within a node in ROS2)

jn42 gravatar image jn42  ( 2022-04-11 12:07:30 -0500 )edit

Should I rename the question title to something like "Convert LaunchConfiguration to inbuilt python type for python launch files"?

jn42 gravatar image jn42  ( 2022-04-11 12:10:00 -0500 )edit

Thanks, I updated the answer with a suggestion

ljaniec gravatar image ljaniec  ( 2022-04-11 14:09:15 -0500 )edit
0

answered 2022-04-12 11:55:19 -0500

shonigmann gravatar image

As @Ijaniec suggested, PythonExpressions are great for one-liners.

If you want to use the LaunchConfiguration in a function for some more complicated functionality, you could consider using OpaqueFunction (some discussion/examples here and here)

edit flag offensive delete link more

Question Tools

2 followers

Stats

Asked: 2022-04-11 10:46:43 -0500

Seen: 113 times

Last updated: Apr 12 '22