Introduction to ROS 2
What you’ll learn
- What ROS 2 is (and isn’t)
- Nodes, topics, services and actions
- What a tiny ROS 2 program looks like, and how to start
What is ROS 2?
ROS 2 (Robot Operating System 2) isn’t an operating system: it’s a free, open-source framework of tools and libraries that runs on Linux (and others), used by universities and companies to build complex robots. Its big idea is splitting a robot’s software into many small programs that talk to each other.
The building blocks
| Concept | What it is | Example |
|---|---|---|
| Node | One small program with one job | camera_node, motor_driver, planner |
| Topic | A named channel; nodes publish messages to it and others subscribe | /scan (laser data), /cmd_vel (speed commands) |
| Message | A typed data structure | Twist = linear and angular speed |
| Service | A request with a reply | “Reset the map” → “done” |
| Action | A long task with progress updates | “Drive to the kitchen” → “50%… arrived” |
| Launch file | Starts many nodes together | Start the whole robot with one command |
[lidar node] --/scan--> [SLAM node] --/map--> [planner] --/cmd_vel--> [motor node]Because every node uses the same message types, you can swap a real robot for a simulator, or one lidar for another, without rewriting everything.
Getting started
- Install the current long-term support (LTS) release on Ubuntu Linux, following the official docs at docs.ros.org (a Raspberry Pi 4/5 can run it).
- Try turtlesim (a 2D turtle robot) and the beginner tutorials.
- Use the command-line tools:
ros2 topic list,ros2 topic echo /topic,ros2 run.
A minimal Python publisher (rclpy)
import rclpyfrom rclpy.node import Nodefrom geometry_msgs.msg import Twist
class Driver(Node): def __init__(self): super().__init__('driver') self.pub = self.create_publisher(Twist, '/cmd_vel', 10) self.create_timer(0.1, self.tick) # 10 times a second
def tick(self): msg = Twist() msg.linear.x = 0.2 # metres per second forward msg.angular.z = 0.5 # radians per second turning self.pub.publish(msg)
def main(): rclpy.init() rclpy.spin(Driver())
if __name__ == '__main__': main()With turtlesim running, publishing to /turtle1/cmd_vel instead makes the turtle drive in a circle.
Where Arduinos fit
A common design: ROS 2 runs on a Raspberry Pi or laptop, and a microcontroller handles motors and encoders, connected over USB (for example with micro-ROS or a simple serial bridge).
Draw the graphEasy
Draw the nodes and topics for a robot with a camera, a “find the ball” node, and a motor controller.
[camera] → /image → [ball_finder] → /cmd_vel → [motor_controller].
Topic or service?Medium
Should each of these be a topic, a service or an action: a 30 Hz stream of laser scans; “take one photo now”; “drive to the charging dock”? Topic; service; action.
TurtlesimHard
If you have Linux (or a Pi) available, install ROS 2, run turtlesim, and drive the turtle in a square using ros2 topic pub commands.
Quick check
Finished this lesson?