Skip to main content

Services: Request-Response Communication

Learning Objectives

After completing this chapter, you will be able to:

  • Create custom service definitions for robotic applications
  • Implement service servers that handle requests robustly
  • Build clients that properly communicate with services
  • Design service-based architectures for humanoid robot control

Introduction

Services in ROS 2 provide a request-response communication pattern that is synchronous in nature, contrasting with the asynchronous publish-subscribe pattern of topics. In humanoid robotics, services are essential for operations that require guaranteed delivery and a direct response, such as configuration changes, calibration procedures, or state queries that must be completed before proceeding.

The synchronous nature of services makes them ideal for scenarios where a node must receive a specific response to a request before continuing its operation. This is particularly important in Physical AI applications where precise control and state verification are required for safe robot operation.

Core Concepts

ROS 2 services follow a client-server model where a client sends a request to a server and waits for a response. Services are defined with a specific interface containing both request and response message types. This interface must be known by both the client and server for successful communication.

Service Definition

Service definitions are specified in .srv files that describe both the request and response message structures. The request section defines inputs to the service, and the response section defines the output.

Service Server

A service server implements the business logic for handling requests. When a client sends a request, the server's callback function is executed, processes the request, and returns a response.

Service Client

A service client sends requests to a server and waits for the response. Clients can be synchronous (blocking) or asynchronous (non-blocking), depending on the application requirements.

Use Cases

Services are appropriate for operations that:

  • Require guaranteed delivery and response
  • Have a clear request-response pattern
  • Need to block execution until completion
  • Are infrequently called or time-insensitive

Hands-on Examples

Let's implement custom services for humanoid robot control:

# Service file: SetJointPositions.srv
# Save as srv/SetJointPositions.srv

# Request - joint names and target positions
string[] joint_names
float64[] positions

---
# Response - success status and error message
bool success
string message

Expected Output:

[INFO] [1678882844.123456789] [joint_control_server]: Joint control service server initialized
[INFO] [1678882844.123456789] [joint_control_client]: Connected to joint control service
Success: Successfully set 12 joint positions

Exercises

Complete the following exercises to reinforce your understanding:

  1. Custom Service Creation: Create a service for querying robot state

    • Define a custom service interface for querying robot pose
    • Implement a service server that responds with current robot state
    • Create a client that requests and processes the robot state
    • Test the service with different query parameters
  2. Service Reliability: Implement a service with timeout and retry logic

    • Add timeout handling to the client when calling services
    • Implement retry logic for failed service calls
    • Create a circuit breaker pattern to handle service unavailability
    • Add appropriate logging for service call outcomes

Common Pitfalls and Solutions

  • Pitfall 1: Blocking the main thread - Synchronous service calls blocking node execution
    • Solution: Use asynchronous service calls with proper future handling
  • Pitfall 2: No timeout handling - Clients hanging indefinitely if service is unavailable
    • Solution: Always implement timeout mechanisms and error handling for service calls
  • Pitfall 3: Incompatible service interfaces - Client and server using different service definitions
    • Solution: Ensure both client and server use the same service interface version
  • Pitfall 4: Thread safety issues - Multiple threads accessing service resources simultaneously
    • Solution: Use locks or other synchronization primitives where needed

Summary

  • Services provide synchronous request-response communication in ROS 2
  • Custom service definitions specify request and response message structures
  • Service servers implement the business logic for handling requests
  • Service clients send requests and receive responses from servers
  • Services are appropriate for operations requiring guaranteed delivery and response

Further Reading