Skip to main content

🤖 AI Learning Companion

Agent Skills:

ROS 2 Basics

Focus: Middleware for Robot Control​

In this module, we will explore the Robot Operating System 2 (ROS 2), which serves as the nervous system for our physical AI agents. ROS 2 provides the communication infrastructure that allows different parts of a robot (sensors, actuators, brains) to talk to each other.

The ROS 2 Graph​

ROS 2 systems are structured as a Computation Graph, where processing takes place in nodes that communicate via edges (Topics, Services, Actions).

graph LR
A[Camera Node] -- /image_raw --> B[Computer Vision Node]
B -- /detected_objects --> C[Planner Node]
C -- /cmd_vel --> D[Motor Controller Node]

Key Concepts​

  • Nodes: Individual processes that perform computation. In Physical AI, a node might wrap a neural network model.
  • Topics: Named buses over which nodes exchange messages (Publish/Subscribe pattern). This is used for streaming data like sensor feeds.
  • Services: Request/Reply communication pattern for synchronous transactions (e.g., "Take a picture now").
  • Actions: Long-running tasks with feedback (e.g., "Navigate to the kitchen").
  • DDS (Data Distribution Service): The underlying industry-standard connectivity framework that ROS 2 uses for real-time communication.

Hands-on: Your First Node​

We have provided a simple Python node in examples/module-1/simple_node.py. This node initializes ROS 2, creates a "SimpleNode", and prints a heartbeat message every second.

Code Preview (examples/module-1/simple_node.py):

import rclpy
from rclpy.node import Node

class SimpleNode(Node):
def __init__(self):
super().__init__('simple_node')
self.get_logger().info('Simple Node has been started.')
self.create_timer(1.0, self.timer_callback)

def timer_callback(self):
self.get_logger().info('Hello from ROS 2!')
# ...

Running the Node:

Since we are running this in a purely Python environment (without a full ROS 2 installation on Windows for this demo), you can run it as a standard Python script if you have the libraries installed:

# Ensure you have the necessary libraries (Note: 'rclpy' usually requires a full ROS 2 install)
# For this textbook simulation, we will assume standard Python execution
python examples/module-1/simple_node.py

Learning Objectives​

By the end of this module, you will be able to:

  1. Create ROS 2 nodes in Python using rclpy.
  2. Publish and subscribe to topics to exchange sensor data.
  3. Launch complex systems using .launch.py files.
  4. Debug the system using rqt_graph and ros2 topic echo.