Bird
Raised Fist0
ROSframework~20 mins

Conditional launching in ROS - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Conditional Launch Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate
2:00remaining
What happens when the condition is false in this ROS launch snippet?
Given this ROS launch snippet using if condition:
<launch>
  <node pkg="demo_pkg" type="demo_node" name="demo" if="launch_demo" />
</launch>

Assuming launch_demo is set to false, what will be the behavior?
ROS
<launch>
  <node pkg="demo_pkg" type="demo_node" name="demo" if="launch_demo" />
</launch>
AThe node 'demo' will launch but immediately shut down.
BThe node 'demo' will launch but with default parameters.
CThe launch file will fail with an error due to the false condition.
DThe node 'demo' will not be launched because the condition is false.
Attempts:
2 left
💡 Hint
Think about what the 'if' attribute controls in ROS launch files.
📝 Syntax
intermediate
2:00remaining
Which option correctly uses a condition to launch a node only if a parameter is true?
You want to launch a node only if the parameter use_sensor is true. Which launch snippet is correct?
A<node pkg="sensor_pkg" type="sensor_node" name="sensor" unless="$(arg use_sensor)" />
B<node pkg="sensor_pkg" type="sensor_node" name="sensor" if="use_sensor" />
C<node pkg="sensor_pkg" type="sensor_node" name="sensor" if="$(arg use_sensor)" />
D<node pkg="sensor_pkg" type="sensor_node" name="sensor" unless="use_sensor" />
Attempts:
2 left
💡 Hint
Remember how to reference arguments inside conditions in ROS launch files.
❓ state_output
advanced
2:00remaining
What is the output when launching with this conditional group?
Consider this launch snippet:
<launch>
  <group if="false">
    <node pkg="pkg1" type="node1" name="node1" />
  </group>
  <node pkg="pkg2" type="node2" name="node2" />
</launch>

Which nodes will be running after launch?
ROS
<launch>
  <group if="false">
    <node pkg="pkg1" type="node1" name="node1" />
  </group>
  <node pkg="pkg2" type="node2" name="node2" />
</launch>
AOnly 'node2' will be running; 'node1' is skipped due to the false group condition.
BBoth 'node1' and 'node2' will be running regardless of the group condition.
CNeither node will run because the group condition disables all nodes.
D'node1' will run but 'node2' will not because it is outside the group.
Attempts:
2 left
💡 Hint
Groups with 'if' conditions control all contained nodes.
🔧 Debug
advanced
2:00remaining
Why does this launch file fail to conditionally launch the node?
This launch snippet is intended to launch camera_node only if enable_camera is true:
<launch>
  <arg name="enable_camera" default="false" />
  <node pkg="camera_pkg" type="camera_node" name="camera" if="enable_camera" />
</launch>

But the node launches even when enable_camera is false. Why?
ROS
<launch>
  <arg name="enable_camera" default="false" />
  <node pkg="camera_pkg" type="camera_node" name="camera" if="enable_camera" />
</launch>
AThe 'if' attribute expects a boolean expression, but 'enable_camera' is a string argument, so it always evaluates true.
BThe 'arg' tag is missing a type declaration, causing the default to be ignored.
CThe node tag is missing a closing slash, causing parsing errors.
DThe launch file must use 'unless' instead of 'if' for boolean arguments.
Attempts:
2 left
💡 Hint
Consider how ROS launch treats argument values in conditions.
🧠 Conceptual
expert
3:00remaining
How does ROS 2 launch handle conditional launching differently than ROS 1?
Which statement best describes the difference in conditional launching between ROS 1 and ROS 2 launch systems?
AROS 2 launch uses YAML files for conditions, ROS 1 uses XML.
BROS 2 launch uses Python scripts with 'IfCondition' objects for conditions, while ROS 1 uses XML 'if' attributes.
CROS 2 launch requires environment variables for conditions, ROS 1 uses XML attributes.
DROS 2 launch does not support conditional launching, unlike ROS 1.
Attempts:
2 left
💡 Hint
Think about the launch file formats and APIs in ROS 1 vs ROS 2.

Practice

(1/5)
1. What is the main purpose of using conditional launching in ROS launch files?
easy
A. To update ROS to the latest version
B. To speed up the robot's movement
C. To change the robot's hardware automatically
D. To start nodes only when certain conditions are met

Solution

  1. Step 1: Understand conditional launching concept

    Conditional launching allows starting nodes only if specific conditions are true, saving resources.
  2. Step 2: Identify the main purpose

    The main goal is to control which nodes run based on conditions, not to affect hardware or speed directly.
  3. Final Answer:

    To start nodes only when certain conditions are met -> Option D
  4. Quick Check:

    Conditional launching = start nodes conditionally [OK]
Hint: Think: conditional launching controls node start based on conditions [OK]
Common Mistakes:
  • Confusing conditional launching with robot speed control
  • Thinking it updates hardware automatically
  • Assuming it updates ROS version
2. Which syntax correctly uses IfCondition in a ROS 2 Python launch file to conditionally start a node?
easy
A. Node(if='launch_arg == true', package='pkg', executable='node')
B. Node(condition=IfCondition(launch_arg == true), package='pkg', executable='node')
C. Node(condition=IfCondition('launch_arg == "true"'), package='pkg', executable='node')
D. Node(condition=IfCondition('launch_arg = true'), package='pkg', executable='node')

Solution

  1. Step 1: Recall correct IfCondition usage

    IfCondition expects a string expression comparing launch arguments, e.g., 'launch_arg == "true"'.
  2. Step 2: Check each option's syntax

    Node(condition=IfCondition('launch_arg == "true"'), package='pkg', executable='node') uses correct string comparison and quotes. Others have syntax errors or wrong operators.
  3. Final Answer:

    Node(condition=IfCondition('launch_arg == "true"'), package='pkg', executable='node') -> Option C
  4. Quick Check:

    IfCondition needs string expression with == [OK]
Hint: Use quotes around condition string and double equals for comparison [OK]
Common Mistakes:
  • Using single equals (=) instead of double equals (==)
  • Not quoting the condition expression
  • Passing boolean directly instead of string
3. Given this launch snippet, what nodes will start if the launch argument use_camera is set to false?
from launch import LaunchDescription
from launch.actions import DeclareLaunchArgument
from launch.conditions import IfCondition
from launch_ros.actions import Node

def generate_launch_description():
    return LaunchDescription([
        DeclareLaunchArgument('use_camera', default_value='false'),
        Node(
            package='camera_pkg',
            executable='camera_node',
            condition=IfCondition('use_camera == "true"')
        ),
        Node(
            package='lidar_pkg',
            executable='lidar_node'
        )
    ])
medium
A. Only lidar_node starts
B. Both camera_node and lidar_node start
C. Only camera_node starts
D. No nodes start

Solution

  1. Step 1: Analyze the condition for camera_node

    camera_node starts only if 'use_camera' equals 'true'. Here, 'use_camera' is 'false', so it won't start.
  2. Step 2: Check lidar_node launch condition

    lidar_node has no condition, so it always starts.
  3. Final Answer:

    Only lidar_node starts -> Option A
  4. Quick Check:

    Condition false skips camera_node, lidar_node always runs [OK]
Hint: Nodes without conditions always start; check condition expressions carefully [OK]
Common Mistakes:
  • Assuming camera_node starts when use_camera is false
  • Thinking no nodes start if condition fails
  • Ignoring unconditional nodes
4. Identify the error in this ROS 2 launch file snippet that prevents conditional launching from working:
Node(
    package='my_pkg',
    executable='my_node',
    condition=IfCondition(use_sensor == 'true')
)
medium
A. IfCondition cannot be used with Node
B. use_sensor should be a string inside quotes
C. Missing package name
D. Executable name must be capitalized

Solution

  1. Step 1: Check IfCondition argument type

    IfCondition expects a string expression, but here use_sensor is used as a variable without quotes.
  2. Step 2: Confirm correct syntax

    The condition should be written as IfCondition('use_sensor == "true"') with quotes around the expression.
  3. Final Answer:

    use_sensor should be a string inside quotes -> Option B
  4. Quick Check:

    IfCondition needs string condition expression [OK]
Hint: Always quote condition expressions in IfCondition [OK]
Common Mistakes:
  • Passing variable without quotes to IfCondition
  • Thinking IfCondition is invalid with Node
  • Ignoring case sensitivity in executable
5. You want to launch either sim_node or real_node based on a launch argument use_sim. Which launch snippet correctly implements this conditional launching in ROS 2 Python launch file?
hard
A. Node(package='robot_pkg', executable='sim_node', condition=IfCondition('use_sim == "true"')) Node(package='robot_pkg', executable='real_node', condition=IfCondition('use_sim == "false"'))
B. Node(package='robot_pkg', executable='sim_node', condition=IfCondition(use_sim == true)) Node(package='robot_pkg', executable='real_node', condition=IfCondition(use_sim == false))
C. Node(package='robot_pkg', executable='sim_node') Node(package='robot_pkg', executable='real_node')
D. Node(package='robot_pkg', executable='sim_node', condition=IfCondition('use_sim = true')) Node(package='robot_pkg', executable='real_node', condition=IfCondition('use_sim = false'))

Solution

  1. Step 1: Check condition syntax for sim_node

    Node(package='robot_pkg', executable='sim_node', condition=IfCondition('use_sim == "true"')) Node(package='robot_pkg', executable='real_node', condition=IfCondition('use_sim == "false"')) uses correct string expressions with double equals and quotes for 'use_sim == "true"'.
  2. Step 2: Check condition syntax for real_node

    Node(package='robot_pkg', executable='sim_node', condition=IfCondition('use_sim == "true"')) Node(package='robot_pkg', executable='real_node', condition=IfCondition('use_sim == "false"')) also correctly uses 'use_sim == "false"' for real_node, ensuring only one node launches.
  3. Step 3: Verify other options

    Node(package='robot_pkg', executable='sim_node', condition=IfCondition(use_sim == true)) Node(package='robot_pkg', executable='real_node', condition=IfCondition(use_sim == false)) lacks quotes and uses booleans incorrectly; Node(package='robot_pkg', executable='sim_node') Node(package='robot_pkg', executable='real_node') launches both nodes unconditionally; Node(package='robot_pkg', executable='sim_node', condition=IfCondition('use_sim = true')) Node(package='robot_pkg', executable='real_node', condition=IfCondition('use_sim = false')) uses single equals (=) which is invalid.
  4. Final Answer:

    Option A correctly uses IfCondition with string expressions and double equals -> Option A
  5. Quick Check:

    Use quotes and == for conditions to launch nodes selectively [OK]
Hint: Use '==' inside quotes for conditions; avoid single '=' [OK]
Common Mistakes:
  • Using single equals (=) instead of double equals (==)
  • Not quoting the condition expression
  • Launching both nodes without conditions
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard