In this lesson, you will learn how to create and build your own custom services.
If you already know how to create custom messages, well, it's going to be very easy to do the same
for a service.
And where will we build our service definition?
Well, we are going to do that inside our.
I'm going to close that.
We're going to do that inside our my robot interfaces okay.
And this package is already fully configured.
So if you were creating the service for the first time with no other interfaces, you would need to
configure the package with.
You see the three lines here in the package dot XML and then those few additional lines also in Cmakelists.txt.
But now let's reuse what we had before.
So let's go to our package My Robot interfaces.
And here I have an MSG folder for messages.
But because I'm going to create services I'm going to create a SRV folder.
And in this SVG folder I will put all the services I have for this application.
Now let's say that I want to create a service to compute a rectangle area, for example.
So I could create also an interface named compute rectangle.
Area dot serve.
And so here are a few tips for the name of the service definitions you're going to use.
So first of all, as we did for messages you will start with an uppercase.
And then each word is going to be separated by an uppercase here okay.
No underscore no dashes no spaces just an uppercase between each word.
Then you also don't say it's a compute rectangle area service or something like that.
You just say compute rectangle area and then dot serve.
And also you see, we use a verb here because as I told you, services are going to be used to do some
actions.
So either we do an action like set something, enable motor or we do a computation.
In this case you see we have compute.
Great.
So let's create that file here.
And now let's edit it in VS code.
Now we have an MSG and an SV folders.
And here.
So what do we expect as the requests.
And let's use float numbers.
So float 64 we expect the length of the rectangle.
And then another float with the width.
And with this we can compute the rectangle area.
And now you see I add three dashes.
Actually the three dashes is the first thing you add okay this is a valid service definition.
It's simply empty.
So we have no request and no response.
But this is valid and you need to put three dashes anyway for any service definition.
And this, for example, is also a valid service definition.
You could have a request, but no response.
Of course, here we are expecting to return something.
So let's add a response.
And the response is also going to be a float number.
Let's name it area because we want to compute the rectangle area.
Great.
Now let's save that.
And the only thing we need to do for that service is to add it to the cmakelists.txt we have here already.
One line for the message I'm going to go back to a new line.
And very important, don't add any comma or anything here.
It's just one definition per line.
And this time we need to go to the SRV.
So SRV slash compute rectangle.
Area dot service.
So that's the path for the file I will save that.
And then we can build it so we can build the package.
Let's do a call on build with packages.
Select my robot interfaces.
Okay.
And you might have some logs like that sometimes.
Well you can ignore it okay.
Because this time well it says that you have built some messages in one or more underlying workspaces.
But what he is not really the case.
So I guess that sometimes they detect some false positives for this warning.
And you have this warning.
But here.
Well if you make sure you always build in the same place, you will not have any problem.
So this is just a warning.
It's nothing.
You see it's still working great.
And now for example, in this terminal let's source the environment and we can do Ros2 interface show
with my robot interfaces.
You see now I have the choice.
If I press tab twice.
I see that I have those two interfaces.
So let's see this one.
And that's the one I have created.
And if you see this, it means that your interface is correctly built.
And you can use your service in your code.
So I'm not going to do the code here.
But it's very simple okay.
In Python you will just just like we did here.
You will do from my robot interfaces dot solve import compute rectangle area.
And then for Cplusplus you will do include my robot interfaces slash service slash compute rectangle
area dot HTTP.
And you will have two underscores.
So you will have one underscore between each word.
Because we have added a uppercase here.
And that's it.
So you can see that creating new interfaces from now on is quite easy and quick.