[ROS2 Foxy] Passing argument from one launch file to another
I have compartmentalized my launch files into different purposes, I currently have a main launch file from which I would like to call one of the others. I have partly done this to be able to separate some of the functionalities, but also so the main launch doesn't become to verbose.
The main launch file gets passed an argument for the vehicle platform architecture, such as to find a specific config file highlighting some necessary parameters. This argument that is to be passed is platform
.
How can I pass this argument from the main launch file to its children.
I included an example of the main.launch.py
so one can see how the platform
arg is being used.
import os
from ament_index_python.packages import get_package_share_directory
from launch import LaunchDescription
from launch.actions.declare_launch_argument import DeclareLaunchArgument
from launch.actions.include_launch_description import IncludeLaunchDescription
from launch.launch_description_sources import PythonLaunchDescriptionSource
from launch_ros.actions import Node
def generate_launch_description():
# defining platform
# select either newholland_t7 or fort_rd25
platforms = ["newholland_t7", "fort_rd25"]
for id, plat in enumerate(platforms):
print(f"\t{id} for {plat}")
platform_id = int(input("Select a platform: "))
try:
platform = platforms[platform_id]
except IndexError as ex:
print(f"[ERROR]: {ex}, please select valid index")
return LaunchDescription()
print(f"Selected {platform}")
urdf_path = os.path.join(
get_package_share_directory("main_pkg"), "urdf", platform + ".urdf.xml"
)
with open(urdf_path, "r") as infp:
robot_description = infp.read()
urdf_publisher = Node(
package="robot_state_publisher",
executable="robot_state_publisher",
name="robot_state_publisher",
parameters=[{"robot_description": robot_description}],
arguments=[urdf_path],
output="screen",
)
danger_zone_config = os.path.join(
get_package_share_directory("agrirobot"), "config", platform + ".yaml"
)
visualize_markers = Node(
package="visualize_markers",
executable="visualize_markers_node",
name="visualize_markers",
parameters=[danger_zone_config],
output="screen",
)
# some other things
ld = LaunchDescription()
ld.add_action(urdf_publisher)
ld.add_action(visualize_markers)
return ld
I just realized that you are giving your
robot_state_publisher
both the URDF path AND the contents of the URDF file asrobot_description
. You only need one or the other, and the former is deprecated, so the latter is a better bet.I also just realized that, for the use case outlined above, you can get away without the additional LaunchArguments. I will append a secondary answer that matches what you're going for a bit more directly (but is perhaps a bit less generalizable)