In this lesson, we will examine in detail the working principle of another tool available in Ros2,
which provides an alternative communication protocol between nodes different from the publisher subscriber
protocol we have used so far.
These are called services.
A service in Ros2 is a node that offers some functionalities to other nodes and use a request response
communication protocol instead of a publisher subscriber protocol used within topics.
Let me give you an example to help you understand how this protocol and our services works.
Let's say you are implementing a node, an application in order to that detects faces from a camera
image and moves the robot to follow the detected phase.
This node contains various functionalities, one of which is the face recognition.
It basically takes a camera frame as input and provides the position of the face in the image as output.
Once you have implemented this application, you can continue to develop new nodes and add new functionalities
to your robot.
For example, one of these new functionalities is to make the robot speak when a person is detected
nearby.
Among the other functionalities implemented in this node for making the robot speak, you realize that
you need the face recognition function that you have already implemented in the previous node.
So you decide to simply copy paste the code into the new node.
In the software development in general, not just in robotics.
Copy Pasting the code that performs the same operation in different places is always a bad idea.
If you need to change a few lines of code or to use a faster and more efficient algorithm, or even
to switch to a different library for face detection, you will have to apply these changes in two different
places and ensure that they work in both places.
This is why there are the two services.
They allow you to offer the same functionality to different nodes without rewriting the same code in
multiple nodes.
In practice, instead of having a duplicate of the face recognition function in each node, we create
a new node, specifically a service server which contains this functionality and offers it to other
nodes.
This way, the face recognition function is implemented only once, only in one place, and you only
need to maintain, test and optimize it in one place.
Afterward, you can guarantee that it will work the same way in all the nodes that require this functionality.
To use a function provided by a service in routes to a different communication protocol is used compared
to the topics instead of the publisher Subscriber protocol.
A request response protocol is used in this communication protocol.
The node that needs the functionality offered by the server is called client and sends a request message
to the server requesting its functionalities.
This request message can also contain some arguments, some data that the server needs to use to perform
its functionalities.
For example, in a service server that offers a face recognition function, it is useful to provide
to the server with the frame.
So with the camera image on which the server should perform the face recognition and this is done within
the request message.
Once our request message is received, the service server implements its logic and for example, it
performs the face recognition on the image and then generates an output containing the face position
in the image.
This output.
So the information returned from the execution of the service server is sent back to the client in a
response message which can then be used by the client, for example, to move the robot in the direction
of the detected phase.
Similarly, any other note that needs the same functionality can send a request to the service server.
The server process the request and returns an output with a response message.
In this communication protocol, there are two actors, a service server node that offers a service
and a service client node that requests the functionalities offered by the server.
For example, a service server can also be a node that calculates an integral and instead of letting
every node implementing the logic and the code for the calculation of the integral, they can directly
call the service server and request for such calculation.
The communication begins when the client node requests the server functionality with a request message.
The service server that received the requests start processing it and performs the necessary calculation
to answer to the request.
For example, it starts calculating the requested integral.
While the server processes the request, the client can continue executing other functionalities while
waiting for the server response.
When this happens, when the server finishes its calculation, it sends a response message with the
result of the calculation to the client, which is notified about this event, about the completion
of the service execution and can finally use the server processing results to continue its execution.