Introduction to ROS2

Week 10 • CMPSC 304 Robotic Agents

Press Space or → to advance

The Problem: Building Robot Software Is Hard

Without ROS

  • Write your own sensor drivers
  • Write your own motor controllers
  • Write your own communication layer
  • Write your own navigation algorithms
  • Everything is custom, every time

With ROS

  • Standardized sensor interfaces
  • Reusable motor controllers
  • Built-in publish/subscribe messaging
  • Full navigation stack available
  • Write once, reuse everywhere
ROS = Robot Operating System — not actually an OS, but middleware that connects robot software components.

ROS in the Real World

  • Research: Most university and research lab robots run ROS
  • Industry: Warehouse robots, agricultural robots, inspection drones
  • Self-driving cars: Autoware (built on ROS2) powers autonomous vehicles
  • Space: NASA uses ROS for rover prototyping and ISS experiments
Why ROS2? ROS2 is the current generation. Improvements over ROS1: real-time support, better security, no single point of failure, multi-platform (Linux, macOS, Windows).

How ROS Fits Our Course

Arduino
Hardware
→
PLC
Deterministic
→
FANUC
Industrial
→
ROS2
Autonomous
You Already KnowROS2 Equivalent
Arduino sensorsROS topics (sensor data streams)
Arduino motor controlROS nodes (individual processes)
PLC scan cycleROS publish/subscribe loop
FANUC teach pendantROS command-line tools
FANUC program timelineROS launch files

Core Concept: Nodes

A node is a single process that does one job.
Lidar
Driver
Camera
Driver
SLAM
Mapper
Path
Planner
Motor
Controller
  • Each node is independent — it can be started, stopped, or replaced individually
  • Nodes communicate with each other through topics
  • A robot is a graph of connected nodes
ros2 node list          # See all running nodes

Core Concept: Topics

A topic is a named channel for sending messages between nodes.
Lidar
Node
/scan
⟶
SLAM
Node
Teleop
Node
/cmd_vel
⟶
Motor
Node
  • Publishers send data to a topic
  • Subscribers receive data from a topic
  • Multiple nodes can publish or subscribe to the same topic
ros2 topic list         # See all active topics
ros2 topic echo /scan   # View messages on a topic

Core Concept: Messages

A message is structured data sent over a topic. Each topic has a specific message type.

Twist (velocity)

Topic: /cmd_vel

linear:
  x: 0.2    # forward m/s
  y: 0.0
  z: 0.0
angular:
  x: 0.0
  y: 0.0
  z: 0.5    # turn rad/s

LaserScan (lidar)

Topic: /scan

angle_min: -3.14
angle_max: 3.14
ranges:
  - 2.45    # distance (m)
  - 2.41
  - 1.03    # obstacle!
  - 0.52
  ...

The Publish/Subscribe Pattern

Nodes don't talk to each other directly. They communicate through topics — publishers don't know who is listening, and subscribers don't know who is sending.
Why is this useful?
  • Modularity: Swap a real lidar for a simulated one — subscribers don't change
  • Scalability: Add a new node that listens to /scan without modifying existing code
  • Testing: Record and replay topic data for debugging
Think about it: In your Arduino projects, the sensor code and motor code were tightly coupled in one file. ROS separates them into independent nodes connected by topics.

Simulation with Gazebo

What Gazebo Provides

  • Physics engine (gravity, collisions, friction)
  • Simulated sensors (lidar, camera, IMU)
  • 3D environments with obstacles
  • Sensors publish to the same ROS topics as real hardware

Why Simulate First?

  • ✓ No hardware damage from bugs
  • ✓ Everyone gets their own robot
  • ✓ Reset instantly after crashes
  • ✓ Test algorithms before real deployment
Key insight: Code you write for a simulated TurtleBot in Gazebo will also run on a real TurtleBot — because both use the same ROS2 topics and messages.

TurtleBot 4: Our Robot Platform

TurtleBot 4 — built on the iRobot Create 3 base, designed for ROS2 education and research.

Hardware

  • iRobot Create 3 base (differential drive)
  • RPLIDAR A1 — 360° 2D lidar
  • OAK-D stereo camera (depth + RGB)
  • IMU, cliff sensors, bump sensors
  • Raspberry Pi 4

Key ROS2 Topics

  • /scan — lidar distance data
  • /odom — position estimate
  • /cmd_vel — velocity commands
  • /imu — orientation data
  • /oakd/rgb/preview/image_raw — camera
# Launch TurtleBot 4 in Gazebo
ros2 launch turtlebot4_ignition_bringup turtlebot4_ignition.launch.py

Essential ROS2 Commands

Command What It Does
ros2 node list List all running nodes
ros2 topic list List all active topics
ros2 topic echo /scan Print messages from a topic in real time
ros2 topic echo /scan --once Print one message and stop
ros2 run <pkg> <node> Start a single node
ros2 launch <pkg> <file> Start multiple nodes at once
Tip: You only need these 6 commands for the rest of the semester. Everything else builds on them.
Now let's set up the environment! → Activity 5