Master ROS2 Examples: Boost Your Robotics Skills Fast

Logan Jul 13, 2026

ROS 2, also known as ROS2, is the next generation of the Robot Operating System, widely used in robotics research and development. It offers a diverse range of features and improvements over its predecessor, ROS1, including a scalable, real-time, and lightweight framework. Here, we will explore ROS2 through various examples, ensuring a comprehensive understanding of its capabilities.

ROS2 Actions vs Services vs Topics
ROS2 Actions vs Services vs Topics

ROS2 is built on top of DDS (Data Distribution Service) and offers a robust, high-performance inter-process communication (IPC) with built-in quality of service (QoS) management. It also provides a rich ecosystem of libraries and tools, fostering a collaborative robotic community. Let's delve into some practical ROS2 examples to appreciate its power and flexibility.

Robot Programming Made Easy with ROS 2 🤖🌐
Robot Programming Made Easy with ROS 2 🤖🌐

ROS2 Nodes: The Building Blocks

Nodes are the fundamental components of a ROS2 system, performing specific tasks and exchanging data. They can publish (send) and subscribe (receive) to topics, which are named streams of data. Understanding ROS2 nodes is crucial for building complex robotic systems.

Robot Programming with ROS2: A Beginner's Concise Guide 🤖
Robot Programming with ROS2: A Beginner's Concise Guide 🤖

Let's consider a simple example: a hello world node in Python.

Hello World Node in Python

the cover of mastering ros2 services for synconoous communication
the cover of mastering ros2 services for synconoous communication

First, ensure you have ROS2 installed and initialized. Then, create a new Python script and import the necessary ROS2 modules:

import rclpy
from rclpy.node import Node

Next, define your node class, inheriting from `Node`, and implement the node lifecycle methods:

class HelloWorld(Node):
    def __init__(self):
        super().__init__('hello_world')

    def on_init(self):
        self.get_logger().info('Hello, World!')

def main(args=None):
    rclpy.init(args=args)
    node = HelloWorld()
    node.on_init()
    node.destroy_node()
    rclpy.shutdown()

Start your node using `ros2 run <package_name> <node_name>`.

Confused about ROS? Try These 5 Online Courses - Learn Robotics
Confused about ROS? Try These 5 Online Courses - Learn Robotics

Latching Subscriptions

ROS2 supports latching subscriptions, where a new subscriber immediately receives all previously published messages on a topic. This is particularly useful in scenarios where a node needs to catch up on missed data. Here's an example of a latching subscription in Python:

def on_connect(self, msg):
    self.get_logger().info(f'Latched message: {msg.data}')

When this subscriber starts, it will receive the latest published message, providing a vital data buffer for recovering nodes.

two roses with water droplets on them are in the foreground, one is pink and the other is red
two roses with water droplets on them are in the foreground, one is pink and the other is red

ROS2 Services: Request-Response Communication

Services in ROS2 enable a request-response style of communication, ideal for scenarios where a node needs to query or control another. Understanding and implementing services are essential for building complex, cooperative robotic systems.

the code example server is shown in this screenshote, it shows an image of a
the code example server is shown in this screenshote, it shows an image of a
Rose Wallpaper / Rose pictures / Rose Warzone / Rose COD / Old Rose
Rose Wallpaper / Rose pictures / Rose Warzone / Rose COD / Old Rose
a woman holding a microphone in her right hand and smiling at the camera with both hands
a woman holding a microphone in her right hand and smiling at the camera with both hands
RX65N Microcontrollers Now Support DDS-XRCE Communication Protocol for ROS 2
RX65N Microcontrollers Now Support DDS-XRCE Communication Protocol for ROS 2
[2025.08.01]
[2025.08.01]
a pink and yellow rose with green leaves in the background, taken from above on a sunny day
a pink and yellow rose with green leaves in the background, taken from above on a sunny day
two pink roses are sitting next to each other
two pink roses are sitting next to each other
💗 ROSÉ 💗
💗 ROSÉ 💗
ROSIÉ 🤎✨|| YSL BEAUTY | ROSIÉ FOR YSL
ROSIÉ 🤎✨|| YSL BEAUTY | ROSIÉ FOR YSL
an info sheet with flowers on it and the words rose peti in different languages
an info sheet with flowers on it and the words rose peti in different languages
photography by artist sagar prajapati
photography by artist sagar prajapati
a pink and black rose with the words infl flame rose on it's label
a pink and black rose with the words infl flame rose on it's label
Learn Robot Operating System - ROS Cheat Sheet
Learn Robot Operating System - ROS Cheat Sheet
ROSÉ || INSTAGRAM POST
ROSÉ || INSTAGRAM POST
Run Multiple Regressions in for-Loop in R (Example) | Linear Models
Run Multiple Regressions in for-Loop in R (Example) | Linear Models
冬花 : winter flower
冬花 : winter flower
blackpink rose summer layout تنسيق روزي صيفي
blackpink rose summer layout تنسيق روزي صيفي
ROSÉ || INSTAGRAM POÁT
ROSÉ || INSTAGRAM POÁT

Let's explore an example of a service for setting a robot's speed.

Speed Service: Setting Robot Speed

Define a service with a request and response message structure using the `srv` files:

# speed_service.srv
int32 velocity

---
bool result
string msg

Implement the server in Python:

import rclpy
from robot_interfaces.srv import Speed
from rclpy.node import Node

class SpeedServer(Node):
    def __init__(self):
        super().__init__('speed_server')
        self.srv = self.create_service(Speed, 'set_velocity', self.set_velocity_callback)

    def set_velocity_callback(self, request, response):
        # Implement speed control logic here
        response.result = True
        response.msg = f'Speed set to {request.velocity}'
        return response

And the client:

def set_velocity_client(node):
    clt = node.create_client(Speed, 'set_velocity')
    while not clt.wait_for_service(timeout_sec=1.0):
        node.get_logger().info('Service not available, waiting again...')
    req = Speed.Request()
    req.velocity = 50
    future = clt.call_async(req)
    node.get_logger().info('Sent velocity request')

ROS2 offers a wide range of tools for creating, testing, and deploying robust robotic systems. Whether you're a seasoned robotics researcher or just starting out, ROS2 examples like these provide an invaluable starting point. Don't hesitate to explore its vast ecosystem, contribute to its ever-growing community, and build the next generation of robots!