Use launch args as variables in ROS2 launch file
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(
package='cpp_pubsub',
executable='talker',
prefix=[debug_prefix],
output='screen'
)
listener_node = Node(
package='cpp_pubsub',
executable='listener',
output='screen'
)
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