Robotics and Middleware · ROS 2 concept
ROS 2 Service
Also known as ROS service, service server, service client, .srv
A ROS 2 request-response interface, similar to a remote procedure call: a client sends a typed request to a named service, and a single service server performs a short computation and returns a typed response.
Explanation
Service types are defined in .srv files, with a request part and a response part separated by three dashes. A service has one server and any number of clients using the service name, which lives in a separate namespace from topic names. If several servers use the same name, it is undefined which one receives a request, so service names must be unique.
Services are meant for quick operations such as querying state, triggering a reset or setting a mode, because the client is usually waiting for the result. Long-running or cancellable work should use actions instead. Clients can call services synchronously or asynchronously, but the ROS documentation warns that synchronous calls can easily cause deadlock and recommends the asynchronous pattern.
By default, services use reliable QoS with volatile durability, so a restarted server does not receive outdated requests. The client is protected from receiving multiple responses, but the server is not protected from the side effects of stale requests, so idempotent service design helps.
How it works
The server node registers a service name and type together with a callback. A client node creates a client for that name, waits until the service is available and sends a request. The middleware delivers the request to the server, the callback fills in the response, and the response is returned to the client, which receives it asynchronously.
Automotive applications
- Resetting odometry or clearing a map
- Querying calibration or configuration
- Switching operating modes
- Triggering one-off computations such as path validation
Example
A test tool calls a /localisation/set_initial_pose service with a pose. The localisation node reinitialises its filter and returns a success flag, after which the tool continues the test scenario.
An illustrative example: any figures in it are worked numbers, not measurements.
References
- Services (ROS 2 documentation source) (opens in a new tab)
“In ROS 2, a service refers to a remote procedure call.”
- Synchronous vs. asynchronous service clients (ROS 2 documentation source) (opens in a new tab)
- Quality of Service settings (ROS 2 documentation source) (opens in a new tab)
Definitions are original summaries. Standards text is not reproduced; follow the links for the official publications.