Rosanswers logo

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 launch_configuration.py has no getter functions for retrieving the value.

Originally posted by NotARobot on ROS Answers with karma: 109 on 2021-02-23

Post score: 1

Original comments

Comment by destogl on 2021-03-11:
Please have a look at this answer. Maybe it helps you.


1 Answer 1


Rosanswers logo

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.

Originally posted by NotARobot with karma: 109 on 2021-05-27

This answer was ACCEPTED on the original site

Post score: 0


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.