ROS 2 Architecture
ROS 2 represents a complete redesign of the Robot Operating System with a focus on production deployment, real-time performance, and security. Understanding its architecture is fundamental to effective robotics development.
Learning Objectives
- Explain the DDS-based architecture of ROS 2
- Understand the role of middleware in ROS 2
- Identify the key components of the ROS 2 framework
- Compare ROS 2 architecture with ROS 1
- Implement basic architectural patterns
DDS Foundation
Data Distribution Service (DDS)
ROS 2's architecture is built on DDS (Data Distribution Service), an OMG (Object Management Group) standard for real-time, distributed data exchange. DDS provides:
- Data-Centricity: Focus on data rather than communication endpoints
- Quality of Service (QoS): Configurable policies for reliability, durability, etc.
- Discovery: Automatic peer discovery in the network
- Language and Platform Independence: Standardized interfaces
DDS vs. ROS 1 Communication
In ROS 1, communication was based on a custom TCP/UDP implementation with a central master node. ROS 2's DDS-based approach provides:
ROS 1 Architecture:
┌─────────────┐ ┌─────────────┐
│ Master │ │ Master │
│ (Central) │ │ (Central) │
└─────┬───────┘ └─────┬───────┘
│ │
┌─────▼──────┐ ┌─────▼──────┐
│ Node A │ │ Node B │
│ │ │ │
└────────────┘ └────────────┘
ROS 2 Architecture:
┌─────────────────────────────────┐
│ DDS Network │
│ ┌─────────┐ ┌─────────┐ │
│ │ Node A │ │ Node B │ │
│ │ │ │ │ │
│ └─────────┘ └─────────┘ │
└──────── ─────────────────────────┘
Core Architecture Components
1. Client Libraries
ROS 2 provides client libraries that implement the ROS 2 API:
- rclcpp: C++ client library
- rclpy: Python client library
- rcl: Reference implementation in C
- rclc: C library optimized for microcontrollers
# Example: Using rclpy to create a node
import rclpy
from rclpy.node import Node
class ArchitectureNode(Node):
def __init__(self):
super().__init__('architecture_node')
self.get_logger().info('Architecture node initialized')
2. RMW (ROS Middleware) Layer
The ROS Middleware Abstraction layer provides a common interface to different DDS implementations:
# The middleware selection happens at runtime
# Supported implementations: Fast DDS, Cyclone DDS, RTI Connext DDS
3. Message and Service Definitions
ROS 2 uses .msg, .srv, and .action files to define message types:
# Example message definition (geometry_msgs/msg/Twist.msg)
# Linear and angular velocities
float64[3] linear
float64[3] angular
Quality of Service (QoS) Profiles
QoS profiles allow fine-tuning communication behavior:
Reliability Policy
- Reliable: All messages are delivered (like TCP)
- Best Effort: Messages may be lost (like UDP)
Durability Policy
- Transient Local: Late-joining subscribers receive last known values
- Volatile: No historical data provided to late joiners
History Policy
- Keep Last: Store only the most recent N messages
- Keep All: Store all messages (limited by memory)
from rclpy.qos import QoSProfile, ReliabilityPolicy, HistoryPolicy
# Example: Configuring QoS for sensor data
sensor_qos = QoSProfile(
depth=10,
reliability=ReliabilityPolicy.BEST_EFFORT,
history=HistoryPolicy.KEEP_LAST
)
# Example: Configuring QoS for critical commands
command_qos = QoSProfile(
depth=1,
reliability=ReliabilityPolicy.RELIABLE,
history=HistoryPolicy.KEEP_LAST
)
Node Architecture
Node Lifecycle
ROS 2 nodes have a well-defined lifecycle with multiple states:
┌─────────────┐
│ Unconfigured │
└──────┬──────┘
│ create()
▼
┌─────────────┐
│ Inactive │ ←─────────────────┐
└──────┬──────┘ │
│ activate() │
▼ │