Introduction to ROS 2 Architecture
Robot Operating System 2 (ROS 2) is the middleware that enables robot components to communicate effectively. Unlike traditional robotic systems that tightly couple hardware and software components, ROS 2 provides a flexible, distributed architecture that scales from simple robots to complex multi-robot systems.
What is ROS 2?
ROS 2 is not an operating system in the traditional sense, but rather a collection of libraries, tools, and conventions that work together to provide a framework for robot development. It's designed to address the challenges of building complex, distributed robotic systems.
Key Characteristics
- Distributed: Components can run on different machines
- Language agnostic: Supports C++, Python, and other languages
- Real-time capable: Designed to meet real-time requirements
- Secure: Built-in security features for commercial deployment
- Real-world tested: Used in production systems across multiple industries
Why ROS 2 Matters for Physical AI
ROS 2 serves as the "nervous system" of physical AI systems by:
- Enabling modular robot design where components can be developed and tested independently
- Providing standardized interfaces that allow different components to work together
- Handling low-level communication details so developers can focus on intelligence
- Supporting simulation and real-world operation with the same interfaces
Core Concepts
Nodes
Nodes are the basic units of computation in ROS 2. Each node performs a specific function, such as sensor processing, control, or planning. A robot system typically consists of multiple nodes that communicate with each other.
# Example of a simple ROS 2 node
import rclpy
from rclpy.node import Node
class MinimalPublisher(Node):
def __init__(self):
super().__init__('minimal_publisher')
self.publisher = self.create_publisher(String, 'topic', 10)
timer_period = 0.5 # seconds
self.timer = self.create_timer(timer_period, self.timer_callback)
def timer_callback(self):
msg = String()
msg.data = 'Hello World: %d' % self.get_clock().now().nanoseconds
self.publisher.publish(msg)
Topics and Messages
Topics are named buses over which nodes exchange messages. Messages are the data structures that nodes send to each other. ROS 2 provides many standard message types, but users can also define custom message types.
Common message types include:
std_msgs: Basic data types (integers, floats, strings, etc.)sensor_msgs: Sensor data (images, point clouds, laser scans, etc.)geometry_msgs: 3D geometric primitives (points, poses, twists, etc.)nav_msgs: Navigation-related messages (paths, occupancy grids, etc.)
Services
While topics enable asynchronous, many-to-many communication, services provide synchronous, request-response communication. A service has a request message and a response message.
Actions
Actions are for long-running tasks that provide feedback and can be canceled. They're useful for navigation, manipulation planning, or other tasks that take significant time to complete.
ROS 2 Architecture
DDS-Based Communication
ROS 2 uses Data Distribution Service (DDS) as its communication middleware. DDS provides:
- Reliable message delivery
- Quality of Service (QoS) settings for different communication needs
- Discovery of nodes on the network
- Built-in security features
Client Libraries
ROS 2 provides client libraries for multiple programming languages:
- rclcpp: C++ implementation
- rclpy: Python implementation
- rcl: C implementation (underlying layer)
- Other languages through ROS 1 bridge tools
Package and Build System
ROS 2 uses the following tools for organizing and building code:
- ament: Build system that supports CMake, Python setuptools, and other build tools
- packages: Organizational units containing source code, launch files, and configuration
- colcon: Multi-package build tool that can build entire workspaces
Physical AI Considerations
Real-Time Performance
Physical AI systems often have strict timing requirements. ROS 2 provides several mechanisms for real-time operation:
- Real-time scheduling: Support for real-time operating systems
- Deterministic communication: Predictable message delivery within QoS constraints
- Minimal latency: Optimized communication paths between nodes
Safety and Reliability
Physical AI systems must be reliable and safe:
- Isolation: Faults in one node don't necessarily affect others
- Monitoring: Tools to monitor node status and communication
- Recovery: Mechanisms to handle and recover from failures
Scalability
ROS 2 architecture scales from single robots to multi-robot systems:
- Same interfaces: Simulation and real robots use identical APIs
- Network transparency: Communication works the same across network boundaries
- Resource management: Support for different computational capabilities
ROS 2 vs. ROS 1
ROS 2 is a complete rewrite that addresses the limitations of ROS 1:
| Feature | ROS 1 | ROS 2 |
|---|---|---|
| Communication | Custom | DDS-based |
| Platform support | Linux | Linux, Windows, macOS |
| Real-time support | Limited | Full support |
| Security | None | Built-in security |
| Multi-robot | Difficult | Native support |
| Communication model | Master-based | Peer-to-peer |
Quality of Service (QoS)
ROS 2 includes QoS profiles that allow nodes to specify their communication requirements:
- Reliability: Reliable (all messages delivered) or best-effort (some messages may be dropped)
- Durability: Keep-all (save messages for late joiners) or volatile (don't save messages for late joiners)
- History: Size of message queue to maintain
- Deadline: Maximum time between messages
- Lifespan: Maximum time a message is valid
These QoS settings are crucial for physical AI systems where different components may have different requirements for message delivery.
Learning Objectives
After completing this module, you will be able to:
- Explain the core concepts of ROS 2 architecture (nodes, topics, services, actions)
- Understand how ROS 2 enables distributed robot systems
- Describe the role of DDS in ROS 2 communication
- Identify the key differences between ROS 1 and ROS 2
- Understand Quality of Service settings and their importance
Hands-On Exercise
Create a simple ROS 2 workspace with a publisher node and a subscriber node. Create a custom message type and use it to communicate between the nodes. Test the system in both simulation and a simple real-world scenario using dummy sensors.
Next Steps
In the next section, we'll explore how nodes communicate through topics, services, and actions, with practical examples of each communication pattern.