Launch Files, Parameters & Robot Communication
In ROS 2, launch files and parameters are essential for configuring and starting robot systems. They allow you to define complex robot behaviors, manage system configurations, and control how different components communicate. This section covers how to use these tools effectively in Physical AI systems.
Understanding Launch Files
Launch files are Python scripts that define how to start ROS 2 processes and configure them. They replace the older XML-based launch system from ROS 1 with a more flexible Python-based approach.
Basic Launch File Structure
A simple launch file:
# launch/simple_robot.launch.py
from launch import LaunchDescription
from launch_ros.actions import Node
from ament_index_python.packages import get_package_share_directory
import os
def generate_launch_description():
return LaunchDescription([
Node(
package='simple_robot',
executable='robot_controller',
name='robot_controller',
parameters=[
{'wheel_radius': 0.05},
{'wheel_separation': 0.3},
],
remappings=[
('/cmd_vel', '/robot/cmd_vel'),
('/odom', '/robot/odom'),
]
),
Node(
package='simple_robot',
executable='sensor_processor',
name='sensor_processor'
)
])
Launch File Components
The main components of launch files include:
- LaunchDescription: Container for all launch actions
- Node: Defines a ROS 2 node to launch
- Parameters: Configuration values passed to nodes
- Remappings: Redirect topic/service names
- Conditions: Conditional launch of components
- Events: React to system events
Advanced Launch Features
Conditional Launch
Launch components based on conditions:
from launch import LaunchDescription, conditions
from launch.substitutions import LaunchConfiguration
from launch.actions import DeclareLaunchArgument
from launch_ros.actions import Node
def generate_launch_description():
# Declare launch arguments
use_sim_time = LaunchConfiguration('use_sim_time')
use_sim_time_arg = DeclareLaunchArgument(
'use_sim_time',
default_value='false',
description='Use simulation time if true'
)
# Conditional node launch
controller_node = Node(
package='simple_robot',
executable='robot_controller',
name='robot_controller',
parameters=[
{'use_sim_time': use_sim_time}
],
condition=conditions.IfCondition(use_sim_time)
)
return LaunchDescription([
use_sim_time_arg,
controller_node
])
Launch Substitutions
Use substitutions to make launch files more flexible:
from launch import LaunchDescription
from launch.substitutions import TextSubstitution, PathJoinSubstitution
from launch_ros.actions import Node
from ament_index_python.packages import get_package_share_directory
def generate_launch_description():
# Use parameter substitution
robot_name = LaunchConfiguration('robot_name', default='simple_robot')
# Use path substitution for configuration files
config_file_path = PathJoinSubstitution([
get_package_share_directory('simple_robot'),
'config',
[robot_name, '.yaml']
])
return LaunchDescription([
Node(
package='simple_robot',
executable='robot_controller',
name=[robot_name, '_controller'],
parameters=[config_file_path]
)
])
Parameter Management
Parameters provide a way to configure ROS 2 nodes dynamically, which is crucial for Physical AI systems that need to adapt to different environments and tasks.
Parameter Declaration in Nodes
import rclpy
from rclpy.node import Node
class RobotController(Node):
def __init__(self):
super().__init__('robot_controller')
# Declare parameters with default values
self.declare_parameter('robot_name', 'simple_robot')
self.declare_parameter('control_frequency', 50.0)
self.declare_parameter('max_linear_speed', 0.5)
self.declare_parameter('max_angular_speed', 1.0)
self.declare_parameter('safety_distance', 0.3)
# Get parameter values
self.robot_name = self.get_parameter('robot_name').value
self.control_freq = self.get_parameter('control_frequency').value
self.max_linear = self.get_parameter('max_linear_speed').value
self.max_angular = self.get_parameter('max_angular_speed').value
self.safety_distance = self.get_parameter('safety_distance').value
# Create timer based on parameter
self.control_timer = self.create_timer(
1.0 / self.control_freq,
self.control_loop
)
# Setup parameter callback for dynamic reconfiguration
self.add_on_set_parameters_callback(self.parameter_callback)
def parameter_callback(self, params):
"""Callback for parameter changes"""
for param in params:
if param.name == 'max_linear_speed' and param.type_ == Parameter.Type.DOUBLE:
self.max_linear = param.value
self.get_logger().info(f'Updated max_linear_speed to {self.max_linear}')
elif param.name == 'max_angular_speed' and param.type_ == Parameter.Type.DOUBLE:
self.max_angular = param.value
self.get_logger().info(f'Updated max_angular_speed to {self.max_angular}')
return SetParametersResult(successful=True)
YAML Parameter Files
Organize parameters in YAML files for better management:
# config/simple_robot.yaml
/**:
ros__parameters:
robot_name: "simple_robot"
control_frequency: 50.0
max_linear_speed: 0.5
max_angular_speed: 1.0
safety_distance: 0.3
wheel_radius: 0.05
wheel_separation: 0.3
# Controller parameters
controller:
k_p: 1.0
k_i: 0.1
k_d: 0.05
max_integral: 1.0
Load parameters in launch file:
from launch import LaunchDescription
from launch_ros.actions import Node
from ament_index_python.packages import get_package_share_directory
import os
def generate_launch_description():
config = os.path.join(
get_package_share_directory('simple_robot'),
'config',
'simple_robot.yaml'
)
return LaunchDescription([
Node(
package='simple_robot',
executable='robot_controller',
name='robot_controller',
parameters=[config],
output='screen'
)
])