Ask Your Question

Use launch args as variables in ROS2 launch file

asked 2020-07-16 17:12:02 -0600

APettinger gravatar image

I would like to start a launch file with ros2 launch and pass it arguments that are used in the launch file, like for starting a node in debug mode or starting extra nodes if running in simulation. I hope to achieve something like this from ROS1:

<arg name="debug" default="false" />
<arg unless="$(arg debug)" name="launch_prefix" value="" />
<arg if="$(arg debug)" name="launch_prefix" value="gdb --ex run --args" />

I have seen good examples for passing launch arguments through to node parameters using launch.actions.DeclareLaunchArgument but can't figure out using the launch args inside of the launch file itself. I am using Foxy on Ubuntu 20. This is the minimal launch file I am trying to get working

import os
import yaml
from launch import LaunchDescription
from launch_ros.actions import Node
from launch.actions import DeclareLaunchArgument
from launch.substitutions import LaunchConfiguration

def generate_launch_description():
    debug_arg = DeclareLaunchArgument('debug', default_value='false')
    debug_prefix = 'xterm -e gdb --args' if LaunchConfiguration('debug')=='true' else ''

    talker_node = Node(

    listener_node = Node(

    return LaunchDescription([ debug_arg, talker_node, listener_node ])

What I hope to be able to do eventually is ros2 launch package file debug:='true' or similiar

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted

answered 2020-07-29 09:27:26 -0600

screen33 gravatar image

It cannot work as is because parsing is not yet done when the description is generated.

You can split your talker_node in 2 nodes:

  • 1 with condition=launch.conditions.IfCondition(launch.LaunchConfiguration("debug")) having the additional debug_prefix
  • 1 with condition=launch.conditions.UnlessCondition(launch.LaunchConfiguration("debug")) having no additional prefix

This is equivalent to the <node if="$(arg debug) .../>" and <node unless="$(arg debug) .../>" existing with the XML syntax of launch files.

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

1 follower


Asked: 2020-07-16 17:12:02 -0600

Seen: 687 times

Last updated: Jul 29 '20