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 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.

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.

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

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>`.

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.

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.




![[2025.08.01]](https://i.pinimg.com/originals/72/81/1e/72811e81d1c13610becb7ee24cfae30f.jpg)













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!