ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question

Multiple conditions for IfCondition in ros2 launch script

asked 2022-01-12 22:51:31 -0500

thejeeb gravatar image

I need to launch a node in a ros2 launch script that requires multiple conditions i.e. a logical 'and' expression

DeclareLaunchArgument("input_arg", default_value="arg1")
DeclareLaunchArgument("bool_arg", default_value="false")

input_arg = LaunchConfiguration("input_arg")
bool_arg = LaunchConfiguration("bool_arg")

my_node = Node(
        # condition=IfCondition(input_arg == arg2 and not bool_arg)

I only want to launch the above node if the input_arg launch argument equals a specific value and the bool_arg argument is false. It easy enough to do one or the other condition e.g. with LaunchConfigurationEquals for the input_arg, but how do combine both these into one IfCondition. I'm guessing it has something with using PythonExpression but I can't get anything to work.

edit retag flag offensive close merge delete

3 Answers

Sort by ยป oldest newest most voted

answered 2022-01-14 11:53:41 -0500

thejeeb gravatar image

updated 2022-01-14 12:05:20 -0500

shonigmann gravatar image

Here's what finally worked for me. It is not very readable and the syntax is messy.

                ["'", input_arg, "' == 'arg2' and '", bool_arg, "' == 'false'"]

There doesn't seem to be much documentation on PythonExpression so it would be helpful to add some and possibly add support for multiple conditions.

edit flag offensive delete link more

answered 2022-01-13 13:11:06 -0500

shonigmann gravatar image

Handling multiple conditions using the Launch.Condition wasn't built in from the start (see:, and I haven't seen movement on the repo to add the functionality yet...

A proposed workaround at the time was to use a PythonExpression substitution. Example here

edit flag offensive delete link more


Thanks for the link. I had seen the ros2 launch issue which didn't get any traction and the nav2 example you linked. However, the nav2 example you linked didn't work for me. For example, the following for my example above does not work:

        PythonExpression(["'", input_arg, "' == 'arg2' and not ", bool_arg])

Instead, I found something more explicit that worked. See my answer.

thejeeb gravatar image thejeeb  ( 2022-01-14 11:49:57 -0500 )edit

answered 2023-04-03 09:15:45 -0500

azazdeaz gravatar image

Turns out, there are things like OrSubstitution and NotSubtitution for this, so we can add conditions like:

    rviz_node = Node(
      condition=IfCondition(AndSubstitution(NotSubstitution(run_headless), use_rviz)),

I didn't find any documentation about this, but here is the issue where it was mentioned:, and here are some tests which are mostly explaining the usage:

edit flag offensive delete link more

Question Tools



Asked: 2022-01-12 22:51:31 -0500

Seen: 3,004 times

Last updated: Apr 03 '23