Chapter 1 Summary: ROS 2 Basics
Generated by: summary-generator skill
Time to Read: 3-4 minutes
Covers: Key concepts, mental models, and common mistakes
Key Takeawaysβ
What You Learnedβ
-
ROS 2 is a modular robot framework
- Robots are built from independent nodes
- Nodes communicate via topics and services
- No single point of failure
-
Nodes are the building blocks
- Every program is a node
- Nodes inherit from
rclpy.Node - Nodes run independently
-
The ROS 2 Graph is how systems work
- Nodes (blue boxes)
- Topics (communication channels)
- Messages (data flowing through topics)
-
Timers make nodes do work
create_timer(interval, callback)runs code periodically- Perfect for sensor reading, publishing, or control
-
The ROS 2 Event Loop keeps things running
rclpy.spin()blocks and handles events- Press Ctrl+C to stop gracefully
Mental Modelsβ
The Restaurant Analogyβ
Traditional Robot Code:
βββββββββββββββββββββββββββββββββββββ
β One Chef (Monolithic Program) β
β - Reads sensors β
β - Processes data β
β - Makes decisions β
β - Sends commands β
β Risk: If chef gets sick, robot β
β stops completely β
βββββββββββββββββββββββββββββββββββββ
ROS 2:
ββββββββββββββββ ββββββββββββββββ ββββββββββββββββ
β Prep Cook ββ β Chef ββ β Platter β
β (Sensors) β β (Processing) β β (Control) β
ββββββββββββββββ ββββββββββββββββ ββββββββββββββββ
- Independent
- Can be replaced
- Can be scaled
The ROS 2 Graph as a Data Pipelineβ
Data flows left to right:
Sensors β Process β Decide β Act
Node A Node B Node C Node D
| | | |
ββTopic1ββ¬ββ | |
| | |
Topic2βββββββββ€ |
| |
Topic3ββββ€
|
Output
The Event Loop Conceptβ
rclpy.spin() does this in a loop:
1. Check if any timers are ready
β If yes, run callback
2. Check if any messages arrived
β If yes, run subscriber callback
3. Check if any service calls came in
β If yes, run service callback
4. Go back to step 1
5. (Blocked on Ctrl+C)
Concept Mapβ
ROS 2 System
βββ Nodes
β βββ Inherit from rclpy.Node
β βββ Do one thing well
β βββ Run independently
β
βββ Communication
β βββ Topics (one-to-many)
β βββ Services (request-response)
β βββ Actions (long-running tasks)
β
βββ Graph
β βββ Nodes are vertices
β βββ Topics are edges
β βββ Messages are data
β
βββ Execution
βββ Timers (periodic)
βββ Callbacks (event-driven)
βββ rclpy.spin() (main loop)
Common Mistakes & How to Avoid Themβ
Mistake 1: Forgetting rclpy.init()β
Problem:
# WRONG - will crash
node = HelloNode()
rclpy.spin(node)
Why it's wrong: ROS 2 system not initialized
Fix:
# CORRECT
rclpy.init(args=args)
node = HelloNode()
rclpy.spin(node)
Mistake 2: Blocking Code in Callbacksβ
Problem:
def timer_callback(self):
# WRONG - will freeze the node
time.sleep(5) # Blocks the event loop!
self.get_logger().info('Done')
Why it's wrong: The event loop is blocked; no other work happens
Fix:
def timer_callback(self):
# CORRECT - don't block
self.get_logger().info('Current time:', self.get_clock().now())
# Event loop continues immediately
Mistake 3: Wrong Package Structureβ
Problem:
my_package/
βββ my_package.py # WRONG - file outside package directory
βββ package.xml
Why it's wrong: ROS 2 can't find your code
Fix:
my_package/
βββ my_package/ # Package directory
β βββ __init__.py
β βββ my_node.py
βββ package.xml
βββ setup.py
Mistake 4: Not Sourcing the Environmentβ
Problem:
# WRONG - ROS 2 not in PATH
colcon build
ros2 run my_package my_node # Command not found
Why it's wrong: ROS 2 commands not available
Fix:
# CORRECT
source /opt/ros/humble/setup.bash
colcon build
source install/setup.bash
ros2 run my_package my_node
Quick Referenceβ
Creating a Nodeβ
import rclpy
from rclpy.node import Node
class MyNode(Node):
def __init__(self):
super().__init__('my_node_name')
self.timer = self.create_timer(1.0, self.timer_callback)
def timer_callback(self):
self.get_logger().info('Hello!')
def main(args=None):
rclpy.init(args=args)
node = MyNode()
rclpy.spin(node)
rclpy.shutdown()
if __name__ == '__main__':
main()
Logging Levelsβ
self.get_logger().debug('Detailed debug info')
self.get_logger().info('General information') # Most common
self.get_logger().warning('Something unexpected')
self.get_logger().error('Something went wrong')
self.get_logger().fatal('Critical error')
Useful Commandsβ
ros2 node list # All nodes
ros2 node info /node_name # Node details
ros2 topic list # All topics
ros2 topic echo /topic_name # Watch messages
rqt_graph # Visual ROS 2 graph
What You Can Now Doβ
β Understand what ROS 2 nodes are β Create a simple ROS 2 node in Python β Use timers to make nodes do work β Debug nodes with CLI tools β Understand the ROS 2 graph concept
What Comes Nextβ
In Section 2: Topics, you'll learn:
- How to publish messages to topics
- How to subscribe to messages from topics
- How to build multi-node systems that communicate
- How pub/sub decouples nodes from each other
Then in Section 3: Services, you'll learn:
- Synchronous request/response communication
- When to use services vs. topics
- How to build interactive systems
Practice Problemsβ
Try these without looking at the solution:
- Easy: Modify hello_node to print every 5 seconds
- Medium: Create a node that counts from 1 to 10, then stops
- Hard: Create two nodes that run independently (ignore communication for now)
Resources for Deeper Learningβ
Official ROS 2 Documentationβ
External Guidesβ
Review Checklistβ
Before moving to Section 2, verify you:
- Understand what a node is
- Can create a simple Python node
- Know how to build and run a ROS 2 package
- Can use
ros2 node infoto inspect nodes - Completed all 3 exercises
- Understand the difference between nodes, topics, and services (conceptually)
If you checked all boxes, you're ready for Section 2: Topics!
Next Stepsβ
- Immediate: Complete all Chapter 1 exercises if you haven't
- Review: Go back and modify the code examples
- Practice: Try the practice problems above
- Next Chapter: Section 2: Topics - Publish and Subscribe
Summary Status: Complete β
Skills Used: summary-generator
Last Updated: 2025-11-30