WEBVTT

00:00.080 --> 00:04.850
In this lesson you will learn how to create a Cplusplus service server.

00:04.850 --> 00:08.420
We will do the exact same thing as we did for Python.

00:08.420 --> 00:12.500
So let's start very quickly with the interface we have used.

00:12.500 --> 00:24.650
So Ros2 interface show we have used the example interfaces and then save and then add two ints which

00:24.650 --> 00:28.160
contains a request that's a and b.

00:28.160 --> 00:29.660
So we have two fields.

00:29.690 --> 00:32.480
Each one is an int 64.

00:32.510 --> 00:36.860
Then three dashes to separate the request from the response.

00:36.860 --> 00:40.220
And then we have a some field in the response okay.

00:40.250 --> 00:45.200
So we have two different messages request and then response.

00:45.200 --> 00:50.480
And we are going to use this interface inside our service server and client.

00:50.480 --> 00:53.300
So let's create a node for the service server.

00:53.480 --> 01:01.790
We are going to our C plus plus package this time and in the source folder of that C plus plus package

01:01.790 --> 01:04.670
where we have all our CPP files.

01:04.670 --> 01:06.890
So let's create a new one.

01:06.890 --> 01:11.450
Let's call it add to int server dot cpp.

01:11.900 --> 01:14.450
And then we can open VSCode.

01:14.450 --> 01:20.810
I'm going to open it once again from the same place, which is the source folder of the roster workspace

01:20.810 --> 01:22.580
with code dot.

01:24.200 --> 01:27.230
Okay let's open that file.

01:28.880 --> 01:30.380
And I don't need that.

01:30.380 --> 01:32.540
And we can get the template.

01:33.080 --> 01:33.290
Okay.

01:33.290 --> 01:37.070
So I'm going to use the C plus plus template for nodes.

01:37.250 --> 01:38.420
Let's save.

01:38.420 --> 01:41.840
So we have the auto completion and the include is found.

01:41.870 --> 01:42.350
Okay.

01:42.380 --> 01:48.950
And let's rename that add to int server node.

01:49.790 --> 02:00.720
We replace it here and there and there we can also rename the node add to its server.

02:02.370 --> 02:03.240
Okay.

02:03.270 --> 02:04.980
Just like that.

02:05.520 --> 02:10.590
And then first things first, let's include the interface that we will need for the server.

02:10.650 --> 02:16.620
So include example interfaces slash.

02:16.620 --> 02:18.660
And you see now it's not going to be MSG.

02:18.690 --> 02:20.460
It's going to be S r v.

02:20.550 --> 02:24.720
And then add to hints dot http.

02:25.410 --> 02:25.740
All right.

02:25.770 --> 02:28.770
We import that one and let's save again.

02:28.770 --> 02:30.690
So that is going to be found.

02:30.720 --> 02:33.750
Now we can create our service server.

02:33.750 --> 02:37.770
And first we are going to declare it as a private attribute.

02:37.770 --> 02:40.590
So how to create a service server.

02:40.590 --> 02:42.030
You do Clcp.

02:42.750 --> 02:46.050
And then that's going to be service.

02:46.080 --> 02:52.380
You need to give the type here with a template, just like we did for publishers and subscribers.

02:52.380 --> 02:53.250
It's going to be the same.

02:53.250 --> 02:54.390
So what is the type.

02:54.390 --> 03:03.180
It's example interfaces solve and add to ints.

03:03.300 --> 03:04.770
Okay, I close the bracket.

03:04.770 --> 03:06.510
And actually that's not all.

03:06.510 --> 03:12.660
We are not just going to create a service, we are going to create a guess what a shared pointer to

03:12.690 --> 03:13.530
the service.

03:13.560 --> 03:13.830
Okay.

03:13.860 --> 03:19.740
So we use once again the shared PTR helper here for most things we have in rust.

03:19.740 --> 03:21.420
And let's name that server.

03:21.420 --> 03:23.520
So we have the declaration here.

03:23.520 --> 03:26.850
Let's initialize it in the constructor.

03:26.880 --> 03:31.080
Server is equal to and I'm using this.

03:32.190 --> 03:35.220
Okay no need but it's to make this explicit.

03:35.610 --> 03:38.670
Create service.

03:38.820 --> 03:39.030
Okay.

03:39.030 --> 03:44.820
So to create a service we use the create service we need to provide once again the interface.

03:44.850 --> 03:52.020
Example interfaces solve add to ints.

03:52.470 --> 03:53.010
All right.

03:53.010 --> 03:55.090
And then open and close the parentheses.

03:55.120 --> 03:56.110
Semicolon.

03:56.110 --> 03:57.880
And what do we need to provide.

03:57.880 --> 04:01.450
Well we've already provided the interface here.

04:01.450 --> 04:03.610
So now we need to provide the name.

04:03.610 --> 04:07.930
And the name is well we choose the name because we create the service here.

04:07.930 --> 04:09.160
We choose the name.

04:09.160 --> 04:14.590
Let's choose add to insert to make the same as what we did with Python.

04:14.590 --> 04:17.860
And then we need to provide a callback okay.

04:17.890 --> 04:20.020
So let's create the callback now.

04:20.020 --> 04:23.920
And the callback is simply because well once again you create a server.

04:23.950 --> 04:26.530
But the server is not going to do anything on its own.

04:26.530 --> 04:30.910
You need to send a request from the outside of this node to this server.

04:30.910 --> 04:34.480
And when you get a request, you need to be able to process it.

04:34.480 --> 04:41.380
And for that, we just create a callback so that when the node is spinning, we can execute that callback.

04:41.380 --> 04:47.350
And I'm going to add it here in private because we don't want to call this callback directly ourselves.

04:47.380 --> 04:50.680
It's going to be called by the spinning mechanism.

04:50.680 --> 04:52.060
And so that's going to be void.

04:52.100 --> 04:53.930
It's not going to return anything.

04:53.960 --> 04:55.790
Here is the difference with Python.

04:55.820 --> 05:01.490
In Python you had to return the response from the from the function from the callback.

05:01.520 --> 05:02.720
Here it's going to be void.

05:02.720 --> 05:04.550
So we don't return anything.

05:04.580 --> 05:09.170
Let's call it callback add to int.

05:09.470 --> 05:12.320
And here you will receive also two things.

05:12.320 --> 05:16.460
You will receive the request and the response.

05:16.460 --> 05:23.540
And actually you receive a const shared pointer of the request and a const shared pointer of the response.

05:23.570 --> 05:25.130
And so that's going to be const.

05:25.970 --> 05:35.960
Example interfaces solve add to ints and request okay.

05:35.990 --> 05:38.810
So you see we have the request message inside it.

05:38.840 --> 05:39.350
Twins.

05:39.380 --> 05:40.730
This is not a message.

05:40.730 --> 05:41.720
This is a combination.

05:41.720 --> 05:43.310
This is a pair of messages.

05:43.310 --> 05:48.200
And then we have shared pointer plus let's name it request.

05:48.200 --> 05:52.940
So you can see it's quite long but that's the full type you need to provide.

05:52.940 --> 05:54.170
We have the request.

05:54.170 --> 05:57.770
And then comma, I'm going to go back to a new line here.

05:57.890 --> 06:05.030
And let's do const example interfaces salve add to int.

06:05.060 --> 06:13.670
This one is going to be the response and also a shared pointer to the response response okay.

06:13.700 --> 06:19.280
And we can open and close the curly brackets for the method.

06:19.280 --> 06:23.240
So what do we do in this service callback.

06:23.240 --> 06:25.700
Well we are going to process the request.

06:25.730 --> 06:29.090
Maybe you need to validate some data or check stuff.

06:29.270 --> 06:34.670
Maybe you need to do an action like turn on an LED, activate a motor, etc..

06:34.670 --> 06:37.670
Here we just want to return a sum.

06:37.670 --> 06:40.670
So the sum of the two elements we have in the request.

06:40.670 --> 06:42.620
So we are going to do response.

06:42.620 --> 06:56.690
And because this is a pointer we use an arrow response sum is equal to request a plus, request A row

06:56.720 --> 06:57.470
B.

06:58.550 --> 06:58.970
Okay.

06:59.000 --> 07:01.880
And that's basically it for this.

07:01.880 --> 07:03.470
You don't need to return anything.

07:03.470 --> 07:07.010
Now I'm also going to print a log here so we know what's happening.

07:07.220 --> 07:10.640
Club info with.

07:10.670 --> 07:14.900
So this get logger.

07:14.900 --> 07:16.190
And then.

07:16.190 --> 07:18.440
So we're going to have a integer.

07:18.440 --> 07:23.990
So that's percentage D plus integer is equal to integer.

07:23.990 --> 07:27.020
And I provide the three elements here.

07:27.020 --> 07:40.040
So just in case I'm going to cast it to an integer there's going to be request a and then int request

07:40.400 --> 07:50.270
b and then int response some which is already set, and a semicolon.

07:50.660 --> 07:53.360
And maybe let's just go back to a new line here.

07:54.740 --> 07:55.370
Okay.

07:55.370 --> 07:57.590
So that's it for the callback.

07:57.590 --> 08:00.500
Now we need to go back here to the constructor.

08:00.500 --> 08:06.860
So we come back to this create service where we have the the type the name.

08:06.860 --> 08:08.870
And we need to provide the callback.

08:08.870 --> 08:11.720
So I'm going to do that on a new line.

08:11.720 --> 08:14.870
And then this also on a new line.

08:15.230 --> 08:18.410
And we need to do STD bind okay.

08:18.440 --> 08:20.720
So for a callback you will use Stdbind.

08:20.750 --> 08:26.450
Once again the first thing is the reference to the function.

08:26.450 --> 08:31.580
So add two ints seven node that's the class name.

08:31.580 --> 08:34.670
And then callback add two ints.

08:34.670 --> 08:38.720
That's the name of the function here with no parentheses.

08:38.810 --> 08:44.210
Second argument in the stdbind is this to link to that object here.

08:44.210 --> 08:50.490
And then because we have two parameters, we need to place two placeholders.

08:50.490 --> 08:54.660
So placeholder one and placeholder two.

08:55.050 --> 09:04.050
Instead of doing STD placeholders one directly here I'm going to be using the namespace using namespace

09:04.470 --> 09:08.100
std placeholders.

09:08.310 --> 09:08.880
Okay.

09:08.910 --> 09:13.200
So I can just do underscore one and then underscore two.

09:13.230 --> 09:13.530
Okay.

09:13.560 --> 09:15.210
So we have two arguments.

09:15.360 --> 09:18.000
We use underscore one and then underscore two.

09:18.030 --> 09:21.600
So in the STD bind you have four parameters to provide.

09:21.630 --> 09:22.020
Okay.

09:22.050 --> 09:25.980
And let's finish this constructor with a log sleep.

09:27.000 --> 09:34.650
And for this get logger and service.

09:34.650 --> 09:42.330
So add to insert a service has been studied for example.

09:42.360 --> 09:44.580
All right let's do a quick recap here.

09:44.580 --> 09:47.040
So we're going to initialize roster communications.

09:47.040 --> 09:49.710
And then we create a shared pointer to the node.

09:49.710 --> 09:52.830
When we get to the constructor we create a server.

09:52.830 --> 09:58.230
And we're going to register that callback for the add to server.

09:58.260 --> 10:01.800
Then the node is going to spin and when the client.

10:01.800 --> 10:05.730
So when one client sends a request it's going to be processed.

10:05.730 --> 10:08.760
Inside this callback we receive the request.

10:08.790 --> 10:11.880
We also have the response object directly.

10:11.880 --> 10:13.860
And what we need to do is to fill.

10:13.890 --> 10:19.590
So to do an action and to fill the response object and that's it.

10:19.620 --> 10:26.310
After this function will exit, then the response is going to be sent back automatically to the client.

10:26.340 --> 10:26.730
All right.

10:26.730 --> 10:28.230
So let's save this file.

10:28.230 --> 10:34.290
And let's go to the Cmakelists.txt to build and install an executable.

10:35.250 --> 10:36.330
So let's do that here.

10:36.360 --> 10:38.310
Add executable.

10:38.790 --> 10:48.190
Add to INS severe from ad to int server dot cp.

10:48.430 --> 10:48.820
All right.

10:48.850 --> 10:50.110
That's the file here.

10:50.110 --> 10:52.390
I'm going a bit faster now because we've done already.

10:52.390 --> 10:52.960
You see that.

10:52.960 --> 11:01.810
We've done already that five times payment target dependencies with the executable name.

11:01.810 --> 11:06.880
And what do we need Lclc-rp and example interfaces.

11:06.910 --> 11:12.520
As you can see in the code we import the two packages and then we have the executable.

11:12.520 --> 11:16.480
I'm going to add it here add two int server.

11:16.510 --> 11:19.210
Now I can save and let's build that.

11:19.210 --> 11:25.570
So let's go back one directory and let's do a call on build packages.

11:25.570 --> 11:26.560
Select.

11:27.070 --> 11:31.000
And let's only build the my CPP package.

11:31.000 --> 11:33.550
So it's quite useful to use this packages select here.

11:33.550 --> 11:39.190
Because if you remember we had built the Python package with Simulink install.

11:39.190 --> 11:44.470
So if you build all the packages again without Simulink install, then you're going to lose the Simulink

11:44.470 --> 11:46.360
install option for the Python package.

11:46.360 --> 11:52.570
So that's why here we just build the Cplusplus one and we don't lose that benefit for the other packages.

11:52.600 --> 11:54.820
All right so I will just a quick parenthesis here.

11:54.820 --> 11:56.470
So let's build.

11:58.960 --> 12:03.910
And then let's solve the terminal or open new ones.

12:03.910 --> 12:12.940
And let's do a Ros to run my CPP package with add to in server okay.

12:12.970 --> 12:15.190
Add to in service has been started.

12:15.190 --> 12:16.690
I can do Ros two.

12:16.720 --> 12:18.880
Well I can do Ros two node list.

12:18.910 --> 12:29.350
Once again I find the node Ros two node info with that name in the service servers I see my add to int

12:29.350 --> 12:31.570
with the example interfaces.

12:31.690 --> 12:33.280
So we add to int.

12:33.310 --> 12:34.120
Great.

12:34.120 --> 12:41.180
And now what I could do is I could do Ros two service Call to test this service from the terminal.

12:41.420 --> 12:48.200
But if you remember, what we just did in the previous videos was to create a Python service client.

12:48.200 --> 12:52.100
So we already have a service client for that service and it's the same name.

12:52.100 --> 12:55.970
So why not just use the client that we already have.

12:56.000 --> 12:59.780
Let's do Ros to run my Python package.

12:59.780 --> 13:02.990
We've had two ints client.

13:02.990 --> 13:08.780
We can use the one with object oriented programming or the one with.

13:08.810 --> 13:11.210
So the script one with no op.

13:11.300 --> 13:13.340
And let's see if that works.

13:14.030 --> 13:15.770
And you can see it's working.

13:15.770 --> 13:20.780
So the Python client here sent a request that was processed by the server here.

13:20.780 --> 13:22.220
And we got the response.

13:22.220 --> 13:28.880
So you can see once again you can have a for example a Python client in one node and a C plus plus server

13:28.880 --> 13:29.630
in another node.

13:29.630 --> 13:30.650
And that's no problem.

13:30.650 --> 13:31.670
It's going to work the same.

13:31.670 --> 13:36.800
And with this we can also validate that our service server is correctly working.
