Introduction to ROS 2
Introduction​
Robot Operating System 2 (ROS 2) is the next generation of ROS, redesigned from the ground up to meet the needs of modern robotics applications. This chapter introduces the fundamental concepts and architecture of ROS 2.
Learning Outcomes​
After completing this chapter, you will be able to:
- Understand ROS 2 architecture and design philosophy
- Compare ROS 2 with ROS 1
- Set up a ROS 2 development environment
What is ROS 2?​
ROS 2 is a set of software libraries and tools for building robot applications. It provides:
- Hardware abstraction: Interface with different robot hardware through standardized interfaces
- Communication: Inter-process communication using topics, services, and actions
- Tools: Development tools for debugging, visualization, and simulation
- Ecosystem: Access to thousands of pre-built packages
Key Design Goals​
ROS 2 was designed with these priorities:
- Real-time support: Deterministic timing for safety-critical applications
- Security: Built-in security features for networked robots
- Production readiness: Suitable for commercial robot deployments
- Flexibility: Support for various robot types and use cases
ROS 1 vs ROS 2​
| Feature | ROS 1 | ROS 2 |
|---|---|---|
| Communication | Master-based | DDS-based |
| Real-time | Limited | Full support |
| Security | None | Built-in |
| Lifecycles | Custom | Standardized |
Why DDS?​
ROS 2 uses the Data Distribution Service (DDS) as its communication layer:
- Discovery: Automatic peer discovery without a master
- QoS: Quality of Service settings for different communication needs
- Security: DDS-Security specification for encrypted communication
- Reliability: Configurable reliability for different scenarios
Setting Up Your Environment​
Prerequisites​
- Ubuntu 22.04 LTS (recommended) or Ubuntu 20.04 LTS
- Python 3.8 or later
- At least 4GB of RAM
Installation​
# Set locale
locale # Check for UTF-8
# Install dependencies
sudo apt update && sudo apt install -y software-properties-common curl
# Add ROS 2 repository
sudo curl -sSL https://raw.githubusercontent.com/ros/rosdistro/master/ros.key -o /usr/share/keyrings/ros-archive-keyring.gpg
echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/ros-archive-keyring.gpg] https://packages.ros.org/ros2/ubuntu $(. /etc/os-release && echo $UBUNTU_CODENAME) main" | sudo tee /etc/apt/sources.list.d/ros2.list > /dev/null
# Install ROS 2
sudo apt update
sudo apt install -y ros-humble-desktop
# Source ROS 2
source /opt/ros/humble/setup.bash
Verification​
# Check ROS 2 installation
ros2 --version
# Run the talker-listener demo
ros2 run demo_nodes_cpp talker &
ros2 run demo_nodes_py listener
ROS 2 Concepts​
Nodes​
A ROS 2 node is a process that performs a specific task:
import rclpy
from rclpy.node import Node
class MyNode(Node):
def __init__(self):
super().__init__('my_node')
self.get_logger().info('Node initialized')
def main(args=None):
rclpy.init(args=args)
node = MyNode()
rclpy.spin(node)
rclpy.shutdown()
Topics​
Topics are named buses for asynchronous communication:
- Publishers: Send messages to a topic
- Subscribers: Receive messages from a topic
- Messages: Typed data structures
Services​
Services provide synchronous request-response communication:
- Server: Provides a service
- Client: Requests the service
Actions​
Actions handle long-running tasks with feedback:
- Goal: The task to accomplish
- Feedback: Progress updates
- Result: The final outcome
Summary​
ROS 2 provides a modern framework for robot application development with improvements in real-time performance, security, and production readiness. The DDS-based communication layer enables flexible and robust inter-process communication.
Assessment​
- What are the key differences between ROS 1 and ROS 2 architecture?
- Why did ROS 2 choose DDS for its communication layer?
- What are the main components of a ROS 2 node?
- How do topics differ from services in ROS 2?