📖 Scenario: You are working on a robot project using ROS (Robot Operating System). You want to create a launch file that starts different nodes based on a condition, such as whether the robot is in simulation mode or real hardware mode.
🎯 Goal: Create a ROS launch file that conditionally launches either a simulator_node or a hardware_node based on a boolean launch argument called use_simulator.
📋 What You'll Learn
Create a launch argument named use_simulator with a default value of true.
Define two nodes: simulator_node and hardware_node.
Use a conditional statement to launch simulator_node only if use_simulator is true.
Use a conditional statement to launch hardware_node only if use_simulator is false.
💡 Why This Matters
🌍 Real World
Robots often need to run different software depending on whether they are in simulation or real hardware. Conditional launching helps manage this easily.
💼 Career
ROS developers use conditional launching to create flexible and reusable launch files for various robot configurations and environments.
Progress0 / 4 steps
1
Create the launch argument
Create a launch argument called use_simulator with a default value of true in the launch file.
ROS
Hint
Use DeclareLaunchArgument to create the argument with the name use_simulator and set default_value='true'.
2
Define the simulator and hardware nodes
Define two nodes named simulator_node and hardware_node using Node from launch_ros.actions. Use package my_robot and executable names simulator_node and hardware_node respectively.
ROS
Hint
Import Node from launch_ros.actions and create two nodes with the exact package and executable names.
3
Add conditional logic to launch nodes
Use launch.conditions.IfCondition to add simulator_node to the launch description only if use_simulator is true, and add hardware_node only if use_simulator is false. Use LaunchConfiguration('use_simulator') to access the argument value.
ROS
Hint
Use IfCondition with LaunchConfiguration('use_simulator') to conditionally launch nodes. Remember to add both nodes to the launch description.
4
Complete the launch description function
Wrap the launch description in a function called generate_launch_description() that returns the LaunchDescription object ld.
ROS
Hint
Define a function named generate_launch_description that returns the launch description ld.
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
Step 1: Understand conditional launching concept
Conditional launching allows starting nodes only if specific conditions are true, saving resources.
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.
Final Answer:
To start nodes only when certain conditions are met -> Option D
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?