Skip to main content

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​

  1. ROS 2 is a modular robot framework

    • Robots are built from independent nodes
    • Nodes communicate via topics and services
    • No single point of failure
  2. Nodes are the building blocks

    • Every program is a node
    • Nodes inherit from rclpy.Node
    • Nodes run independently
  3. The ROS 2 Graph is how systems work

    • Nodes (blue boxes)
    • Topics (communication channels)
    • Messages (data flowing through topics)
  4. Timers make nodes do work

    • create_timer(interval, callback) runs code periodically
    • Perfect for sensor reading, publishing, or control
  5. 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:

  1. Easy: Modify hello_node to print every 5 seconds
  2. Medium: Create a node that counts from 1 to 10, then stops
  3. 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 info to 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​

  1. Immediate: Complete all Chapter 1 exercises if you haven't
  2. Review: Go back and modify the code examples
  3. Practice: Try the practice problems above
  4. Next Chapter: Section 2: Topics - Publish and Subscribe

Summary Status: Complete βœ… Skills Used: summary-generator Last Updated: 2025-11-30