How to pass launch args dynamically during launch-time
I have a launch file that launch 2 ROS2 nodes for each vehicle(uav) I have. I would like to specify the number of vehicles I have while running the launch file so that the correct number of nodes is initialized.
My launch file looks like this:
import os
from time import sleep
from launch import LaunchDescription
from launch_ros.actions import Node
from launch.substitutions import LaunchConfiguration
from launch.actions import DeclareLaunchArgument
from ament_index_python.packages import get_package_share_directory
launch_path = os.path.realpath(__file__).replace("demo.launch.py", "")
ros2_ws = os.path.realpath(os.path.relpath(os.path.join(launch_path,"../../../../..")))
avoidance_path = os.path.join(ros2_ws,"src","avoidance")
avoidance_config_path = os.path.join(avoidance_path,"config")
number_of_uavs = int(input("how many uavs do you want to simulate? "))
def generate_launch_description():
ld = LaunchDescription()
for i in range(1, number_of_uavs+1):
namespace="uav_{:s}".format(str(i))
config = os.path.join(
avoidance_config_path,
namespace,
"params.yaml"
)
avoidance_node = Node(
package="avoidance",
namespace=namespace,
executable="avoidance_client_1",
output="screen",
parameters=[config]
)
trajectory_controller_node = Node(
package="offboard_exp",
namespace=namespace,
executable="trajectory_controller",
name="trajectory_controller_{:s}".format(namespace)
)
ld.add_action(trajectory_controller_node)
ld.add_action(avoidance_node)
return ld
Currently, I use number_of_uavs = int(input("how many uavs do you want to simulate? "))
To get the number of vehicles and then change the namespace of the node in each iteration.
Is there a better way to pass this parameter instead of prompting the user to enter it?
IIUC, you're asking for a better option than (statically) passing arg upon execution (e.g. via cmdline). But in the subject you said "how to pass command args", which contradicts to what you're asking in the question body. I modified the subject to try to describe what you really want.