Concept Flow - Conditional launching
Start launch file
Evaluate condition
Launch Node
Continue launch
End
The launch file starts, checks a condition, then launches a node only if the condition is true, otherwise it skips it and continues.
from launch import LaunchDescription from launch.actions import DeclareLaunchArgument, LogInfo from launch.conditions import IfCondition from launch_ros.actions import Node from launch.substitutions import LaunchConfiguration, PythonExpression def generate_launch_description(): return LaunchDescription([ DeclareLaunchArgument('use_node', default_value='true'), Node( package='demo_pkg', executable='demo_node', condition=IfCondition(LaunchConfiguration('use_node')) ), LogInfo(condition=IfCondition(LaunchConfiguration('use_node')), text='Node launched'), LogInfo(condition=IfCondition(PythonExpression(["'", LaunchConfiguration('use_node'), "' == 'false'"])), text='Node skipped') ])
| Step | Action | Condition Evaluated | Result | Node Launched | Log Message |
|---|---|---|---|---|---|
| 1 | Start launch file | N/A | N/A | No | No |
| 2 | Evaluate 'use_node' argument | use_node == 'true' | True | No | No |
| 3 | Check IfCondition for Node | True | True | Yes | No |
| 4 | Launch Node 'demo_node' | N/A | N/A | Yes | No |
| 5 | Check IfCondition for LogInfo 'Node launched' | True | True | Yes | Node launched |
| 6 | Check IfCondition for LogInfo 'Node skipped' | False | False | No | No |
| 7 | End launch file | N/A | N/A | Yes | Node launched |
| Variable | Start | After Step 2 | After Step 3 | After Step 5 | Final |
|---|---|---|---|---|---|
| use_node | undefined | 'true' | 'true' | 'true' | 'true' |
| Node Launched | No | No | Yes | Yes | Yes |
| Log Message | None | None | None | 'Node launched' | 'Node launched' |
Conditional launching in ROS launch files: - Use DeclareLaunchArgument to define a condition variable. - Use IfCondition with Node or other actions. - Node launches only if condition is true. - Use LogInfo with conditions to log messages. - Helps control which nodes start based on parameters.
IfCondition in a ROS 2 Python launch file to conditionally start a node?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'
)
])Node(
package='my_pkg',
executable='my_node',
condition=IfCondition(use_sensor == 'true')
)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?Pass the test and this pattern moves unknown