Introduction
Conditional launching lets you start parts of your robot system only when certain conditions are true. This saves resources and makes your robot smarter.
Conditional launching lets you start parts of your robot system only when certain conditions are true. This saves resources and makes your robot smarter.
launch_ros.actions.Node(
package='my_package',
executable='my_node',
condition=IfCondition(LaunchConfiguration('my_condition'))
)IfCondition to check if a launch argument or configuration is true.sensor_node only if the use_sensor argument is set to true.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_sensor', default_value='false'), Node( package='sensor_package', executable='sensor_node', condition=IfCondition(LaunchConfiguration('use_sensor')) ) ])
enable_camera is true.Node(
package='camera_package',
executable='camera_node',
condition=IfCondition(LaunchConfiguration('enable_camera'))
)This launch file declares an argument start_navigation. The navigation_node runs only if start_navigation is set to true when launching.
from launch import LaunchDescription from launch.actions import DeclareLaunchArgument from launch.conditions import IfCondition from launch.substitutions import LaunchConfiguration from launch_ros.actions import Node def generate_launch_description(): return LaunchDescription([ DeclareLaunchArgument('start_navigation', default_value='false', description='Start navigation node'), Node( package='nav_package', executable='navigation_node', condition=IfCondition(LaunchConfiguration('start_navigation')) ) ])
Always declare launch arguments before using them in conditions.
Conditions help keep your launch files flexible and efficient.
Conditional launching controls which nodes start based on settings.
Use IfCondition with launch arguments to enable or disable nodes.
This helps save resources and customize robot behavior easily.
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