ROS2 service vs library call - when to use

Hi,

“library call”: I mean, a function that can be imported and called without any network call.

It hink, that services are very similar to library function calls. Also services seem to me overkill for such tasks like “generating Fibonacchi number” or “AddTwoInts” like in the documentation: Writing a simple service and client (Python) — ROS 2 Documentation: Foxy documentation.

Do you know some rule-of-thumbs to decide which one to use for a task?

I definitely not use ROS2 Service for a purely computational task. SO my “rule of thumb” can be:
If a function is fast and does not use live robotic information, I may create a librabry function.
If a function is fast but uses some sensory information which is hidden from the caller (or the caller is unaware of it), I definitely use ROS2 Service.

But there is a caveate here:
Lets say, that our service uses LaserScan data (It is frequently changing and big). But we are calling this service rarely.
In this case I see only three possibility:
if I write this service to store the latest LaserScan data but it uses this latest data only when the service is called, the storing consume resources unnecessarily most of the time.
If I write this service that it only starts to listen to LaserScan data only when the service is called, it probably will be slow.
If I call this service with the LaserScan data as a parameter, it will be fast, but now it is overkill, because a library function would be much faster and lightweight.

Based on these, I never choose ROS2 Service, I rather write simple functions and call them.

What do you think?
Thank you in advance
Péter

I think you are right.

Bear in mind that those examples mere teach how to use ROS2 services, and not necessarily best practices with respect to performance and optimization. In the real world, those things should be considered.

In the end, what counts is that the problem is solved in a sustainable and optimal way.

1 Like

Hi @peterborkuti,
The service allows to simplify your architecture for many type of operations. The first use case that come in my mind are status check for example. Services have a real use if your system is composed of multiple nodes and you need to check status or get specific information from another node without having to create a subscriber and a flag system to ensure the reception of the data.

@EnzoGhisoni

Interesting idea.

Thank you for your answer.
Lets keep this topic open for a while, maybe others can add more ideas/use cases for services.
Péter

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.