Skip to main content

4. ROS 2 Fundamentals

The Robot Operating System (ROS) is a flexible framework for writing robot software. ROS 2 is the latest iteration, offering improved real-time capabilities, security, and multi-robot support. This chapter introduces the core concepts of ROS 2.

What is ROS 2?

ROS 2 provides libraries, tools, and conventions for developing complex robot applications. It's not an operating system in the traditional sense, but a meta-operating system that sits on top of a conventional OS (like Linux, Windows, or macOS).

Key Concepts

  • Nodes: Executables that perform computation.
  • Topics: Named buses over which nodes exchange messages (publishers and subscribers).
  • Services: Request/reply communication patterns between nodes.
  • Actions: Long-running goal-oriented communication for complex tasks (e.g., moving to a target location).
  • ROS 2 Graph: The network of nodes communicating via topics, services, and actions.

Example: Simple ROS 2 Publisher (Conceptual)

This is a conceptual representation, as a full ROS 2 example requires a ROS 2 environment setup.

# Conceptual Python code for a ROS 2 publisher
import rclpy
from rclpy.node import Node
from std_msgs.msg import String

class MinimalPublisher(Node):
def __init__(self):
super().__init__('minimal_publisher')
self.publisher_ = self.create_publisher(String, 'topic', 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 = f'Hello ROS 2! {self.i}'
self.publisher_.publish(msg)
self.get_logger().info(f'Publishing: "{msg.data}"')
self.i += 1

def main(args=None):
rclpy.init(args=args)
minimal_publisher = MinimalPublisher()
rclpy.spin(minimal_publisher)
minimal_publisher.destroy_node()
rclpy.shutdown()

if __name__ == '__main__':
main()