ROS 2 Launch System Skill
Purpose: Generate accurate, educational ROS 2 Python launch files and multi-node configurations following official ROS 2 Humble patterns for the Physical AI textbook.
When to Use
- Creating lessons in Chapter 6 (Building Robot Systems)
- Writing multi-node deployment exercises
- Generating worked examples for system startup
- Teaching parameter configuration and node composition
- Chapter 7 capstone project integration
Live Documentation Access
CRITICAL: Before generating any code, fetch current ROS 2 documentation using Context7 MCP:
Tool: mcp__context7__resolve-library-id
libraryName: "ros2 launch"
Then:
Tool: mcp__context7__get-library-docs
context7CompatibleLibraryID: [resolved ID]
topic: "launch file python"
This ensures launch patterns match current ROS 2 Humble API.
Launch File Patterns
Basic Launch File (Canonical)
python
1# File: launch/robot_system.launch.py
2
3from launch import LaunchDescription
4from launch_ros.actions import Node
5
6def generate_launch_description():
7 return LaunchDescription([
8 Node(
9 package='my_robot_pkg',
10 executable='publisher_node',
11 name='sensor_publisher',
12 output='screen',
13 ),
14 Node(
15 package='my_robot_pkg',
16 executable='subscriber_node',
17 name='data_processor',
18 output='screen',
19 ),
20 ])
Launch File with Parameters
python
1from launch import LaunchDescription
2from launch_ros.actions import Node
3
4def generate_launch_description():
5 return LaunchDescription([
6 Node(
7 package='my_robot_pkg',
8 executable='configurable_node',
9 name='robot_controller',
10 output='screen',
11 parameters=[{
12 'robot_name': 'my_robot',
13 'update_rate': 10.0,
14 'max_speed': 1.5,
15 'enabled_sensors': ['lidar', 'camera', 'imu'],
16 }],
17 ),
18 ])
Launch File with Parameter File (YAML)
python
1import os
2from ament_index_python.packages import get_package_share_directory
3from launch import LaunchDescription
4from launch_ros.actions import Node
5
6def generate_launch_description():
7 config = os.path.join(
8 get_package_share_directory('my_robot_pkg'),
9 'config',
10 'robot_params.yaml'
11 )
12
13 return LaunchDescription([
14 Node(
15 package='my_robot_pkg',
16 executable='robot_node',
17 name='robot_controller',
18 output='screen',
19 parameters=[config],
20 ),
21 ])
Parameter File (YAML)
yaml
1# File: config/robot_params.yaml
2robot_controller:
3 ros__parameters:
4 robot_name: "physical_ai_bot"
5 update_rate: 20.0
6 max_linear_speed: 2.0
7 max_angular_speed: 1.57
8 sensors:
9 lidar:
10 enabled: true
11 topic: "/scan"
12 camera:
13 enabled: true
14 topic: "/image_raw"
Launch File with Arguments
python
1from launch import LaunchDescription
2from launch.actions import DeclareLaunchArgument
3from launch.substitutions import LaunchConfiguration
4from launch_ros.actions import Node
5
6def generate_launch_description():
7 # Declare launch arguments
8 robot_name_arg = DeclareLaunchArgument(
9 'robot_name',
10 default_value='default_robot',
11 description='Name of the robot'
12 )
13
14 sim_mode_arg = DeclareLaunchArgument(
15 'sim_mode',
16 default_value='true',
17 description='Run in simulation mode'
18 )
19
20 return LaunchDescription([
21 robot_name_arg,
22 sim_mode_arg,
23 Node(
24 package='my_robot_pkg',
25 executable='robot_node',
26 name='robot_controller',
27 output='screen',
28 parameters=[{
29 'robot_name': LaunchConfiguration('robot_name'),
30 'simulation': LaunchConfiguration('sim_mode'),
31 }],
32 ),
33 ])
Package Structure for Launch Files
my_robot_pkg/
├── my_robot_pkg/
│ ├── __init__.py
│ ├── publisher_node.py
│ └── subscriber_node.py
├── launch/
│ ├── robot_system.launch.py
│ └── debug_system.launch.py
├── config/
│ ├── robot_params.yaml
│ └── debug_params.yaml
├── package.xml
├── setup.py
└── setup.cfg
setup.py (Essential Parts for Launch)
python
1from setuptools import setup
2import os
3from glob import glob
4
5package_name = 'my_robot_pkg'
6
7setup(
8 name=package_name,
9 version='0.1.0',
10 packages=[package_name],
11 data_files=[
12 ('share/ament_index/resource_index/packages',
13 ['resource/' + package_name]),
14 ('share/' + package_name, ['package.xml']),
15 # Include launch files
16 (os.path.join('share', package_name, 'launch'),
17 glob('launch/*.launch.py')),
18 # Include config files
19 (os.path.join('share', package_name, 'config'),
20 glob('config/*.yaml')),
21 ],
22 install_requires=['setuptools'],
23 entry_points={
24 'console_scripts': [
25 'publisher_node = my_robot_pkg.publisher_node:main',
26 'subscriber_node = my_robot_pkg.subscriber_node:main',
27 ],
28 },
29)
Debugging Multi-Node Systems
Using ros2doctor
bash
1# Check system health
2ros2 doctor
3
4# Verbose output
5ros2 doctor --report
Using rqt_graph
bash
1# Visualize node graph
2ros2 run rqt_graph rqt_graph
Logging Levels
python
1# In node code
2self.get_logger().debug('Detailed debug info')
3self.get_logger().info('Normal operation')
4self.get_logger().warn('Warning condition')
5self.get_logger().error('Error occurred')
6self.get_logger().fatal('Fatal error')
bash
1# Set log level at runtime
2ros2 run my_pkg my_node --ros-args --log-level debug
Educational Requirements
Layer 1 (Manual Foundation)
- Explain launch file structure and purpose
- Walk through LaunchDescription components
- Demonstrate parameter passing patterns
Layer 2 (AI Collaboration)
- Generate launch files from system requirements
- Debug multi-node communication issues with AI
- Optimize node configuration (Three Roles INVISIBLE)
Layer 3 (Intelligence Design)
- System architecture patterns
- Configuration management strategies
- Reusable launch file components
Layer 4 (Spec-Driven)
- Design system from specification
- Multi-node integration testing
- Capstone project orchestration
Hardware Tier Compatibility
All launch files MUST work on:
- Tier 1: Cloud ROS 2 (TheConstruct) with turtlesim
- Tier 2+: Local ROS 2 Humble with physical robots
Launch commands:
bash
1# Build and source
2cd ~/ros2_ws
3colcon build --packages-select my_robot_pkg
4source install/setup.bash
5
6# Launch
7ros2 launch my_robot_pkg robot_system.launch.py
8
9# Launch with arguments
10ros2 launch my_robot_pkg robot_system.launch.py robot_name:=my_bot sim_mode:=false
Common Mistakes to Prevent
- Missing data_files in setup.py: Launch files won't be installed
- Wrong path in get_package_share_directory: Package name must match
- Forgetting to rebuild: Changes require
colcon build
- Parameter type mismatches: YAML types must match node expectations
Integration with Other Skills
- ros2-publisher-subscriber: Nodes to launch
- ros2-service-pattern: Service nodes in system
- ros2-custom-interfaces: Interface packages as dependencies
- lesson-generator: For full lesson structure
Authoritative Source
All patterns verified against: