ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | Q&A
Ask Your Question

Running Python launch files with arguments

asked 2021-02-23 10:20:24 -0500

NotARobot gravatar image

updated 2021-02-24 10:28:44 -0500

Greetings. Is there a way to use arguments within launch files written in Python?

I have found the possibilities offered by roslaunch, however I was wondering how to achieve the same effect in Python launch files.

To be more specific I want to create a number of robots for simulation purposes. For this I would like to supply a list of pose coordinates, spawning the corresponding number of robots at the respective poses.

In order to achieve this I plan to iterate in the launch file to create separate namespaces for each robot before spawning them.

Therefore, is there a way to pass some command line arguments like ros2 launch my_sim_bringup multiple_robots.launch -x 0 -y 0 -z 0 ?

Could I define generate_launch_description like this def generate_launch_description(my_arg), as I would do with any Python function to achieve this behavior?

I am very sorry for asking such basic questions, but the documentation regarding the Python launch system is lacking.

EDIT: Doing further research I found: ros2 access current LaunchConfiguration, Access launch argument in LaunchFile ROS2 as well as ROS2: How to pass arguments from one launch file to a child launch file. But to be frank, I don't get it.

Using the method proposed by these links I have implemented in my launch file:

ld = LaunchDescription()    
robot_positions = DeclareLaunchArgument(

as well as

positions = LaunchConfiguration('positions', default="000")

Now I was lead to belief that it was possible to use command line arguments or superordinate launch files to supply the values. Something like this for command line ros2 launch my_sim_bringup multiple_robots.launch positions:=000

However, positions does not hold the string "000", rather a LaunchConfiguration object, which as far as I can see it in has no getter functions for retrieving the value.

edit retag flag offensive close merge delete


Please have a look at this answer. Maybe it helps you.

destogl gravatar image destogl  ( 2021-03-11 10:02:55 -0500 )edit

1 Answer

Sort by ยป oldest newest most voted

answered 2021-05-27 11:28:16 -0500

NotARobot gravatar image

Thank you for your answer destogl. I have to confess the solution I was looking for is very trivial and is more generic to Python.

I have solved the problem by using argv to get the desired effect.

edit flag offensive delete link more

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: 2021-02-23 10:20:24 -0500

Seen: 294 times

Last updated: May 27 '21