WEBVTT

00:00.080 --> 00:04.700
In this lesson, you will learn how to create and build your own custom services.

00:04.700 --> 00:10.430
If you already know how to create custom messages, well, it's going to be very easy to do the same

00:10.430 --> 00:11.660
for a service.

00:11.660 --> 00:14.420
And where will we build our service definition?

00:14.420 --> 00:17.930
Well, we are going to do that inside our.

00:17.930 --> 00:19.010
I'm going to close that.

00:19.310 --> 00:23.450
We're going to do that inside our my robot interfaces okay.

00:23.480 --> 00:25.970
And this package is already fully configured.

00:25.970 --> 00:30.980
So if you were creating the service for the first time with no other interfaces, you would need to

00:31.010 --> 00:32.960
configure the package with.

00:32.990 --> 00:40.100
You see the three lines here in the package dot XML and then those few additional lines also in Cmakelists.txt.

00:40.130 --> 00:43.490
But now let's reuse what we had before.

00:43.490 --> 00:49.550
So let's go to our package My Robot interfaces.

00:49.550 --> 00:52.850
And here I have an MSG folder for messages.

00:52.850 --> 00:58.370
But because I'm going to create services I'm going to create a SRV folder.

00:58.370 --> 01:03.830
And in this SVG folder I will put all the services I have for this application.

01:03.860 --> 01:09.650
Now let's say that I want to create a service to compute a rectangle area, for example.

01:09.650 --> 01:16.580
So I could create also an interface named compute rectangle.

01:18.920 --> 01:21.800
Area dot serve.

01:24.080 --> 01:28.820
And so here are a few tips for the name of the service definitions you're going to use.

01:28.820 --> 01:33.470
So first of all, as we did for messages you will start with an uppercase.

01:33.470 --> 01:38.780
And then each word is going to be separated by an uppercase here okay.

01:38.810 --> 01:44.420
No underscore no dashes no spaces just an uppercase between each word.

01:44.720 --> 01:50.810
Then you also don't say it's a compute rectangle area service or something like that.

01:50.840 --> 01:56.120
You just say compute rectangle area and then dot serve.

01:56.360 --> 02:02.150
And also you see, we use a verb here because as I told you, services are going to be used to do some

02:02.180 --> 02:02.840
actions.

02:02.840 --> 02:09.350
So either we do an action like set something, enable motor or we do a computation.

02:09.350 --> 02:12.200
In this case you see we have compute.

02:12.290 --> 02:12.740
Great.

02:12.740 --> 02:14.120
So let's create that file here.

02:14.120 --> 02:16.580
And now let's edit it in VS code.

02:16.610 --> 02:19.730
Now we have an MSG and an SV folders.

02:19.730 --> 02:20.600
And here.

02:20.600 --> 02:23.090
So what do we expect as the requests.

02:23.720 --> 02:25.490
And let's use float numbers.

02:25.490 --> 02:31.100
So float 64 we expect the length of the rectangle.

02:31.100 --> 02:34.610
And then another float with the width.

02:34.610 --> 02:38.270
And with this we can compute the rectangle area.

02:38.270 --> 02:40.820
And now you see I add three dashes.

02:40.820 --> 02:47.420
Actually the three dashes is the first thing you add okay this is a valid service definition.

02:47.420 --> 02:48.560
It's simply empty.

02:48.560 --> 02:51.200
So we have no request and no response.

02:51.200 --> 02:56.550
But this is valid and you need to put three dashes anyway for any service definition.

02:57.000 --> 03:00.330
And this, for example, is also a valid service definition.

03:00.330 --> 03:03.360
You could have a request, but no response.

03:03.390 --> 03:06.120
Of course, here we are expecting to return something.

03:06.120 --> 03:08.250
So let's add a response.

03:08.250 --> 03:11.040
And the response is also going to be a float number.

03:11.070 --> 03:16.440
Let's name it area because we want to compute the rectangle area.

03:16.470 --> 03:16.950
Great.

03:16.980 --> 03:18.660
Now let's save that.

03:18.660 --> 03:25.770
And the only thing we need to do for that service is to add it to the cmakelists.txt we have here already.

03:25.770 --> 03:29.040
One line for the message I'm going to go back to a new line.

03:29.040 --> 03:32.760
And very important, don't add any comma or anything here.

03:32.760 --> 03:35.670
It's just one definition per line.

03:36.060 --> 03:38.280
And this time we need to go to the SRV.

03:38.280 --> 03:44.040
So SRV slash compute rectangle.

03:46.260 --> 03:48.180
Area dot service.

03:48.210 --> 03:52.110
So that's the path for the file I will save that.

03:52.560 --> 03:55.890
And then we can build it so we can build the package.

03:56.820 --> 04:02.100
Let's do a call on build with packages.

04:02.100 --> 04:05.610
Select my robot interfaces.

04:06.390 --> 04:06.810
Okay.

04:06.840 --> 04:10.470
And you might have some logs like that sometimes.

04:10.470 --> 04:13.770
Well you can ignore it okay.

04:13.770 --> 04:20.400
Because this time well it says that you have built some messages in one or more underlying workspaces.

04:20.400 --> 04:22.350
But what he is not really the case.

04:22.350 --> 04:25.980
So I guess that sometimes they detect some false positives for this warning.

04:25.980 --> 04:26.880
And you have this warning.

04:26.880 --> 04:27.600
But here.

04:27.630 --> 04:32.610
Well if you make sure you always build in the same place, you will not have any problem.

04:32.610 --> 04:34.020
So this is just a warning.

04:34.020 --> 04:34.890
It's nothing.

04:34.890 --> 04:37.380
You see it's still working great.

04:37.380 --> 04:46.680
And now for example, in this terminal let's source the environment and we can do Ros2 interface show

04:46.710 --> 04:50.700
with my robot interfaces.

04:50.700 --> 04:51.960
You see now I have the choice.

04:51.960 --> 04:53.730
If I press tab twice.

04:54.090 --> 04:57.330
I see that I have those two interfaces.

04:57.780 --> 05:00.900
So let's see this one.

05:00.900 --> 05:02.730
And that's the one I have created.

05:02.760 --> 05:06.030
And if you see this, it means that your interface is correctly built.

05:06.030 --> 05:08.640
And you can use your service in your code.

05:08.640 --> 05:10.320
So I'm not going to do the code here.

05:10.320 --> 05:12.480
But it's very simple okay.

05:12.480 --> 05:16.950
In Python you will just just like we did here.

05:16.950 --> 05:23.940
You will do from my robot interfaces dot solve import compute rectangle area.

05:23.940 --> 05:34.470
And then for Cplusplus you will do include my robot interfaces slash service slash compute rectangle

05:34.470 --> 05:36.210
area dot HTTP.

05:36.330 --> 05:38.100
And you will have two underscores.

05:38.100 --> 05:40.920
So you will have one underscore between each word.

05:41.550 --> 05:43.980
Because we have added a uppercase here.

05:43.980 --> 05:44.790
And that's it.

05:44.790 --> 05:50.130
So you can see that creating new interfaces from now on is quite easy and quick.
