Skip to main content

Chapter 1: Understanding the Robotic Nervous System: An Introduction to ROS 2

Overview: The Big Idea​

Every complex robot, from autonomous drones to humanoid assistants, relies on a sophisticated network of software components to perceive, think, and act. This network is the robot's nervous system. In modern robotics, the Robot Operating System (ROS) 2 has become the standard for building this nervous system. This chapter will introduce you to the fundamental concepts of ROS 2 and why it is the backbone of so many robotic systems.

Key Concepts​

At its core, ROS 2 is a set of software libraries and tools that help you build robot applications. It is not an operating system in the traditional sense, but a flexible framework that simplifies the task of creating complex and robust robot behavior across a wide variety of robotic platforms.

The key concepts in ROS 2 are:

  • Nodes: A node is an executable that performs a single, modular purpose, such as controlling a wheel, reading a sensor, or planning a path.
  • Topics: Topics are named buses over which nodes exchange messages. They are the primary way that data is moved between different parts of the system.
  • Messages: Messages are the data structures that are sent on topics. ROS 2 provides a rich set of standard message types, and you can also define your own.
  • Services: Services are a request/response model of communication. One node (the client) sends a request to another node (the server) and waits for a response.
  • Actions: Actions are for long-running tasks. They provide a way to send a request to a node and receive feedback on the progress of the task, as well as a final result.

Real-World Use Cases​

ROS 2 is used in a vast range of robotics applications, from small hobbyist projects to large-scale industrial automation. Here are a few examples:

  • Autonomous Vehicles: ROS 2 is used in self-driving cars to process sensor data, plan paths, and control the vehicle.
  • Warehouse Automation: Many automated warehouses use ROS 2 to manage fleets of robots that move goods and fulfill orders.
  • Research and Education: ROS 2 is the go-to platform for robotics research and education, providing a common framework for students and researchers to build upon.
  • Healthcare: In the medical field, ROS 2 is used in surgical robots, rehabilitation devices, and hospital logistics.

Technical Explanations​

Let's dive a little deeper into the technical workings of ROS 2.

The ROS 2 Graph​

The "graph" is the network of ROS 2 elements processing data together. It consists of all the nodes, topics, services, and actions, and how they are all connected.

Diagram Description: A diagram showing a simple ROS 2 graph. There are three nodes: "Camera," "Image Processor," and "Motor Controller." The "Camera" node publishes an image message to the "/camera/image" topic. The "Image Processor" node subscribes to this topic, processes the image, and then publishes a command message to the "/motor/command" topic. The "Motor Controller" node subscribes to the "/motor/command" topic and controls the robot's motors.

The colcon Build System​

colcon is the recommended build system for ROS 2. It is a command-line tool that allows you to build, test, and install your ROS 2 packages.

Code Samples​

Here is a simple example of a ROS 2 publisher node written in Python. This node publishes a "Hello, World!" message to a topic.

import rclpy
from rclpy.node import Node
from std_msgs.msg import String

class HelloWorldPublisher(Node):

def __init__(self):
super().__init__('hello_world_publisher')
self.publisher_ = self.create_publisher(String, 'hello_world', 10)
timer_period = 0.5 # seconds
self.timer = self.create_timer(timer_period, self.timer_callback)
self.i = 0

def timer_callback(self):
msg = String()
msg.data = 'Hello, World! %d' % self.i
self.publisher_.publish(msg)
self.get_logger().info('Publishing: "%s"' % msg.data)
self.i += 1

def main(args=None):
rclpy.init(args=args)
hello_world_publisher = HelloWorldPublisher()
rclpy.spin(hello_world_publisher)
hello_world_publisher.destroy_node()
rclpy.shutdown()

if __name__ == '__main__':
main()

To run this code, you would save it as a Python file in a ROS 2 package, build the package with colcon, and then run the node with ros2 run.

Summary​

In this chapter, we have taken our first steps into the world of ROS 2. We have learned that ROS 2 is a flexible framework for building robot applications, and we have been introduced to its core concepts: nodes, topics, messages, services, and actions. We have also seen how ROS 2 is used in a variety of real-world applications and have taken a look at a simple code example.

In the next chapter, we will take a deeper dive into ROS 2 topics and learn how to create our own custom messages.