using-conditionals-in-roslaunch.md

Author: Lisandro Mayancela

This tutorial assumes the reader knows how to use/access args and parameters in a launch file.

When making launch files you may sometimes want aspects of your launch (Such as the urdf file that is used) to be dependent on certain conditions

In order to have this functionality you can use the group tag with an if parameter like so:

<group if="condition goes here">
    If statement body
</group>

Examples

For a better example let's look at a launch file which spawns a robot into a gazebo world:

<launch>
    <arg name="robot_name"/>
    <arg name="init_pose"/>
    <arg name="team"/>
    <arg name="type"/>

    <group if="$(eval team == 'Red')">
        <group if="$(eval type == 'painter')">
            <param name="robot_description" 
                command="$(find xacro)/xacro.py $(find robopaint)/urdf/red/painterbot_red_burger.urdf.xacro" />
        </group>
        <group if="$(eval type == 'attacker')">
            <param name="robot_description" 
                command="$(find xacro)/xacro.py $(find robopaint)/urdf/red/attackerbot_red_burger.urdf.xacro" />
        </group>
    </group>

    <group if="$(eval team == 'Blue')">
        <group if="$(eval type == 'painter')">
            <param name="robot_description" 
                command="$(find xacro)/xacro.py $(find robopaint)/urdf/blue/painterbot_blue_burger.urdf.xacro" />
        </group>
        <group if="$(eval type == 'attacker')">
            <param name="robot_description" 
                command="$(find xacro)/xacro.py $(find robopaint)/urdf/blue/attackerbot_blue_burger.urdf.xacro" />
        </group>
    </group>

    node name="spawn_minibot_model" pkg="gazebo_ros" type="spawn_model"
     args="$(arg init_pose) -urdf -param robot_description -model $(arg robot_name)"
     respawn="false" output="screen" />

    <node pkg="robot_state_publisher" type="robot_state_publisher" 
          name="robot_state_publisher" output="screen"/>
</launch>

In lines 7-27 we see a chain of these if statements which then end up deciding one out of four urdf files to load as the robot_description which then gets passed into the spawn_minibot_model node.

Last updated

Copyright (c) Brandeis University