Skip to main content

ROS 2 Nodes, Topics, Services, and Actions

Learning Objectives

  • Understand the fundamental concepts of ROS 2 architecture
  • Learn how to create and manage ROS 2 nodes
  • Master the use of topics for asynchronous communication
  • Differentiate between services and actions for synchronous communication

Overview

The ROS 2 architecture is built around the concept of nodes, which are individual processes that perform computation. Nodes are designed to be modular, allowing for distributed computation across multiple devices. Communication between nodes happens through topics, services, and actions.

Nodes

A node is an executable that uses ROS 2 to communicate with other nodes. Nodes are the fundamental building blocks of a ROS 2 system. They are typically organized to perform specific tasks such as sensor data processing, actuator control, or planning.

Creating a Node

To create a node in ROS 2, you need to:

  1. Initialize the ROS 2 client library
  2. Create a node instance
  3. Add functionality to the node (publishers, subscribers, services, etc.)
  4. Spin the node to process callbacks
  5. Shutdown the node

Topics and Publishers/Subscribers

Topics enable asynchronous communication between nodes using a publish/subscribe model. A publisher node sends messages to a topic, and subscriber nodes receive messages from that topic.

Characteristics of Topics:

  • Asynchronous communication
  • Many-to-many relationship (multiple publishers and subscribers)
  • Data flow is unidirectional from publishers to subscribers
  • Message types are strongly typed

Example Use Cases:

  • Sensor data distribution
  • Robot state broadcasting
  • Log message collection

Services

Services provide synchronous request/response communication between nodes. A client sends a request to a service, and the service returns a response.

Characteristics of Services:

  • Synchronous communication
  • One-to-one relationship (one client, one service)
  • Request/response pattern
  • Blocking until response is received

Example Use Cases:

  • Parameter configuration
  • One-time data requests
  • System activation/deactivation

Actions

Actions are for long-running tasks that require feedback during execution. They combine features of both topics and services.

Characteristics of Actions:

  • Asynchronous communication
  • Support for goals, feedback, and results
  • Ability to cancel ongoing tasks
  • Suitable for long-running operations

Example Use Cases:

  • Navigation goals with progress feedback
  • Complex manipulation tasks
  • Calibration procedures

Communication Patterns Comparison

FeatureTopicsServicesActions
Communication TypeAsynchronousSynchronousAsynchronous
Data FlowUnidirectionalBidirectionalBidirectional
Use CaseContinuous dataRequest/responseLong-running tasks
FeedbackNoNoYes

Best Practices

  1. Node Design: Keep nodes focused on a single responsibility
  2. Topic Naming: Use descriptive, consistent naming conventions
  3. Message Types: Use appropriate message types for the data being transmitted
  4. Resource Management: Properly manage publishers, subscribers, and other resources
  5. Error Handling: Implement appropriate error handling and recovery mechanisms

Exercises

Exercise 1: Publisher Node Implementation

Create a publisher node that broadcasts a custom message to a topic:

  • Define a custom message type with at least two fields
  • Create a publisher that sends messages at 10Hz
  • Include timestamp and sequence number in the message
  • Verify the messages are being published correctly
Exercise 2: Subscriber Node Implementation

Create a subscriber node that processes incoming messages:

  • Subscribe to the topic from Exercise 1
  • Calculate and display statistics about received messages
  • Implement message filtering based on certain criteria
  • Log message arrival times for performance analysis
Exercise 3: Service Implementation

Implement a service that performs calculations:

  • Create a service definition file with request and response fields
  • Implement a service server that performs mathematical operations
  • Create a service client that sends requests and displays results
  • Handle error cases and timeouts appropriately
Exercise 4: Action Server Design

Design an action server for navigation:

  • Define goal, feedback, and result message types
  • Implement an action server that simulates navigation progress
  • Create an action client that sends goals and monitors progress
  • Add capability to cancel ongoing navigation tasks

Summary

This section covered the core communication mechanisms in ROS 2: nodes, topics, services, and actions. Understanding these concepts is crucial for developing distributed robotic applications. Each communication pattern serves specific purposes and choosing the right one is important for system design.