Overview
ROS2 (Robot Operating System 2) is a flexible framework for writing robot software. It provides tools, libraries, and conventions that aim to simplify creating complex and robust robot behavior across platforms. Understanding ROS2’s core architecture is essential for building autonomous navigation systems.Distributed System
Multiple programs (nodes) communicate over standardized interfaces
Middleware Layer
DDS (Data Distribution Service) handles inter-process communication
Core Concepts
Nodes
Nodes are individual processes that perform specific computations. A robot system is composed of many nodes working together.simple_node.py
motor_controller_node: Controls wheel motors via ESP32lidar_driver_node: Publishes laser scan data from RPLIDARimu_driver_node: Publishes IMU sensor dataekf_localization_node: Fuses odometry and IMU for robot poseslam_toolbox_node: Creates map using LiDAR datanav2_controller_node: Autonomous path following
- Single, focused responsibility (one node = one job)
- Can be started/stopped independently
- Communicate via topics, services, or actions
- Written in Python or C++ (this course uses Python)
Topics
Topics implement publish-subscribe messaging. Nodes publish messages to topics, and other nodes subscribe to receive those messages.- Many-to-many: Multiple publishers and subscribers allowed
- Asynchronous: Publishers don’t wait for subscribers
- Decoupled: Nodes don’t know about each other, only topics
- Typed: Each topic has a specific message type
publisher_example.py
subscriber_example.py
Services
Services implement request-response communication. A client sends a request and waits for a response from the server.- One-time requests (e.g., “reset odometry”)
- Configuration changes (e.g., “set PID gains”)
- Queries (e.g., “get current position”)
- Topics: Continuous data streams (sensor readings, velocities)
- Services: Occasional requests (commands, queries)
service_server.py
service_client.py
Actions
Actions are for long-running tasks that provide feedback. Combines goal request, continuous feedback, and final result.- Long-running tasks (navigation, mapping)
- Tasks requiring feedback (progress updates)
- Cancellable operations (stop navigation mid-route)
navigation_action_client.py
ROS2 Communication Patterns
Comparison Table
| Feature | Topics | Services | Actions |
|---|---|---|---|
| Pattern | Pub-Sub | Request-Response | Goal-Feedback-Result |
| Direction | One-way | Two-way | Two-way + continuous |
| Blocking | No (async) | Yes (waits for response) | Yes (can cancel) |
| Feedback | No | No | Yes (during execution) |
| Use Cases | Sensor data, velocities | Commands, queries | Long tasks, navigation |
| Examples | /scan, /odom, /cmd_vel | reset_odom, get_pose | navigate_to_pose |
When to Use Each
- Topics
- Services
- Actions
Use topics for:
- Continuous data streams (sensor readings)
- Real-time control signals (motor commands)
- Status updates (battery level, warnings)
- Broadcasting to multiple receivers
/scan(LaserScan): LiDAR data at 5.5 Hz/imu/data(Imu): IMU readings at 50 Hz/cmd_vel(Twist): Velocity commands at 10 Hz/odom(Odometry): Position estimates at 20 Hz
ROS2 Graph Architecture
Visualize how nodes communicate in the mecanum robot system:- Sensors publish data to topics (one-way flow)
- EKF fuses multiple sensors (subscribes to multiple topics)
- Navigation stack orchestrates multiple nodes
- Final control commands go to motor controller
- Each node is independent and can be replaced
ROS2 Quality of Service (QoS)
QoS policies control how messages are delivered between publishers and subscribers.Common QoS Profiles
- Reliability:
BEST_EFFORT(fast, lossy) vs.RELIABLE(slow, guaranteed) - History:
KEEP_LAST(only recent) vs.KEEP_ALL(all messages) - Depth: Queue size (1 = only latest, 10+ = buffer messages)
- Durability:
VOLATILE(RAM only) vs.TRANSIENT_LOCAL(persist for late joiners)
- Sensor data:
BEST_EFFORT,KEEP_LAST(1)→ Speed over reliability - Commands:
RELIABLE,KEEP_LAST(10)→ No message loss - Diagnostics:
TRANSIENT_LOCAL→ Late joiners get status updates
ROS2 Parameters
Parameters allow configuring nodes at runtime without recompiling code.node_with_parameters.py
robot_params.yaml
Best Practices
Node Design
- One node = one responsibility
- Keep nodes small and focused
- Use descriptive node names (
lidar_driver, notnode1) - Initialize in
__init__, run inspin()
Topic Naming
- Use lowercase with underscores:
/cmd_vel,/scan - Namespace related topics:
/imu/data,/imu/raw - Follow ROS conventions (REP-144)
- Avoid generic names (
/data,/output)
Message Types
- Use standard ROS2 messages when possible
- geometry_msgs/Twist for velocities
- sensor_msgs/LaserScan for LiDAR
- nav_msgs/Odometry for position
- Create custom messages only if necessary
Error Handling
- Log errors with
self.get_logger().error() - Validate message content before use
- Handle missing topics/services gracefully
- Use try-except for critical operations
CLI Commands Reference
Next Steps
ROS2 Packages
Learn to organize code into ROS2 packages
Launch Files
Start multiple nodes with single command
URDF/Xacro
Model robot physical structure
TF Transforms
Manage coordinate frames and transformations