Chapter 1: ROS 2 Basics (Nodes, Topics, Services)
Learning Time: 25-30 minutes Level: Beginner Prerequisites: Basic Python, Linux terminal comfort
Introduction: What is ROS 2 and Why Should You Care?β
Robot Operating System 2 (ROS 2) is the middleware that powers modern robotics. Instead of writing a monolithic robot control program, ROS 2 lets you build robots as a collection of independent, communicating processes called nodes.
Think of a robot as a team of specialists:
- One specialist reads sensor data (camera, lidar, IMU)
- Another processes that data (perception)
- A third makes decisions (planning)
- A fourth sends commands to motors (control)
ROS 2 is the communication system that lets these specialists talk to each other reliably and efficiently.
Key Benefitsβ
- Modularity: Write small, focused programs instead of monolithic code
- Reusability: Use existing ROS 2 packages instead of reinventing the wheel
- Scalability: Add more nodes and features without rewriting everything
- Debugging: Inspect messages flowing through the system
- Hardware Independence: Same code runs on different robots (simulator or real)
Connection to Physical AIβ
In the Physical AI curriculum, ROS 2 is your foundation:
- Chapter 1 (this chapter): How nodes communicate
- Chapter 2: How to describe robot structure (URDF)
- Chapter 3: How to control robots using these tools
Chapter Structureβ
This chapter has 4 sections plus exercises:
-
Nodes and the Graph (Section 1)
- What are nodes?
- How do they form a computation graph?
- Your first ROS 2 node
-
Topics: Publish and Subscribe (Section 2)
- One-to-many communication
- Decoupled systems
- Real-world use cases
-
Services: Request and Response (Section 3)
- Synchronous communication
- When to use services vs. topics
- Building interactive systems
-
Putting It Together (Section 4)
- Multi-node systems
- Debugging with ROS 2 tools
- Best practices
Learning Objectivesβ
By the end of this chapter, you will be able to:
Remember:
- Define what a ROS 2 node is
- List ROS 2 communication patterns
- Recall command-line tools (ros2 run, ros2 topic, ros2 service)
Understand:
- Explain how the ROS 2 graph works
- Describe pub/sub vs. service patterns
- Understand message flow and decoupling
Apply:
- Create a simple publisher node
- Create a simple subscriber node
- Create a service client and server
- Debug a running system with CLI tools
See Learning Objectives for detailed breakdown by Bloom's taxonomy level.
Key Concepts at a Glanceβ
| Concept | What It Does | When to Use |
|---|---|---|
| Node | Independent ROS 2 program | Always - everything is a node |
| Topic | One-way communication channel | Sensor data, state updates (oneβmany) |
| Publisher | Sends data to a topic | Sensor drivers, data producers |
| Subscriber | Receives data from a topic | Data consumers, processing |
| Service | Request/response call | Commands, queries (synchronous) |
| Message | Data structure on a topic | Sensor readings, commands |
Tools You'll Useβ
Command Lineβ
ros2 run [package] [node] # Run a node
ros2 topic list # See all topics
ros2 topic echo /topic_name # Watch messages
ros2 service list # See all services
ros2 service call /srv AddNumbers '{a: 5, b: 3}' # Call service
rqt_graph # Visualize the ROS 2 graph
Python Code (rclpy)β
import rclpy
from rclpy.node import Node
class MyNode(Node):
def __init__(self):
super().__init__('my_node')
# Create publishers, subscribers, services
def main():
rclpy.init()
node = MyNode()
rclpy.spin(node) # Event loop
rclpy.shutdown()
Chapter Contentsβ
- Nodes and the Graph - 7 min
- Topics: Publish and Subscribe - 8 min
- Services: Request and Response - 7 min
- Exercises - 10-15 min
- Summary - 3 min
Total Time: 35-40 minutes (including hands-on)
How to Use This Chapterβ
For Self-Studyβ
- Read each section in order
- Run the code examples yourself
- Modify examples and experiment
- Complete all exercises
- Check your understanding with the summary
For Instructorsβ
- Each section is ~7-8 minutes of lecture
- Code examples are ready-to-run in any ROS 2 Humble environment
- Exercises have solutions and common pitfalls documented
- Activities scale from beginner to intermediate difficulty
For Learners with Different Stylesβ
Visual Learners:
- Focus on the ROS 2 graph diagrams
- Use
rqt_graphto visualize running systems - Study the node/topic/service relationship diagrams
Hands-On Learners:
- Run code examples immediately
- Modify and experiment with code
- Complete exercises as you learn
Theoretical Learners:
- Read detailed explanations in each section
- Study the mental models section in the summary
- Understand the "why" behind design patterns
Prerequisites Checklistβ
Before starting, make sure you have:
- ROS 2 Humble installed on Ubuntu 22.04
- Python 3.10+ working with rclpy
- Basic understanding of Python (functions, classes)
- Linux terminal comfort level (mkdir, cd, ls)
- Ability to run
ros2 --versionand see version info
Not set up yet? See ROS 2 Humble Installation Guide.
What You'll Buildβ
By the end of this chapter, you'll have created:
- hello_node.py: A simple publisher that prints "Hello, ROS 2!"
- publisher.py + subscriber.py: A working pub/sub system
- service_server.py + service_client.py: A request/response system
- 3+ working exercises: Custom nodes you build from scratch
All code examples are:
- β Tested and working in ROS 2 Humble
- β Fully commented with explanations
- β
Available in
code-examples/ros2_packages/ - β Ready to extend and modify
Common Questions Answeredβ
Q: Do I need to understand C++? A: No! This chapter uses Python exclusively. C++ comes later if you need it.
Q: Do I need a physical robot? A: No! All examples run in simulation. Real hardware comes in Chapter 3.
Q: How long will this take? A: 30-40 minutes for reading + exercises. Faster if you skip exercises, longer if you explore.
Q: Can I skip this chapter? A: Not recommended. This chapter is the foundation for everything that follows.
Q: What if I get stuck? A: Each exercise has hints (collapsed sections). See Troubleshooting.
Next Stepsβ
After completing this chapter:
- Immediately: Do the exercises and verify all code runs
- Next Chapter: Chapter 2 will teach you how to describe robot structure (URDF)
- Practical Project: Build a simple robot that uses pub/sub communication
Additional Resourcesβ
Official Documentationβ
External Tutorialsβ
Chapter Statisticsβ
| Metric | Value |
|---|---|
| Reading Time | 15-20 min |
| Hands-On Time | 10-15 min |
| Code Examples | 3+ (publisher, subscriber, service) |
| Exercises | 3 (beginner to intermediate) |
| Diagrams | 4+ (ROS 2 graph, message flow, architecture) |
| Key Concepts | 6 (node, topic, publisher, subscriber, service, message) |
Ready to Start?β
π Next: Section 1: Nodes and the Graph
Or jump to a specific section:
Chapter Status: Ready for learning β
Skills Used: learning-objectives
Created: 2025-11-30
For Questions: See chapter summary or troubleshooting section