Running Python launch files with arguments
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(
'positions',
default_value="000",
)
ld.add_action(robot_positions)
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.
Please have a look at this answer. Maybe it helps you.