WEBVTT

00:00.080 --> 00:06.080
Let's now create a Cplusplus service client for the service server we have created just before.

00:06.080 --> 00:12.890
And as we did with Python, we will first start with kind of a script with no OOP to see the different

00:12.890 --> 00:20.720
steps of the client, and then we will integrate the client inside an OOP node in the next video.

00:20.720 --> 00:22.400
So let's create.

00:23.150 --> 00:29.210
Let's go to our workspace the C plus plus package and source.

00:29.360 --> 00:39.410
Let's add an add to its client no OOP dot cpp.

00:39.410 --> 00:41.510
So I'm using the same name here.

00:41.510 --> 00:44.180
We are going to create the client in that file.

00:44.210 --> 00:48.410
Let's go back to VS code.

00:48.410 --> 00:55.580
And we have the file here I'm still going to start from the template but I'm going to modify it.

00:55.580 --> 00:57.770
So I take the template code.

00:58.190 --> 01:02.390
And I'm not going to use this class.

01:04.550 --> 01:04.970
Okay.

01:05.000 --> 01:13.400
I'm just going to create a node that's going to be this time an rcl cp node.

01:14.030 --> 01:20.000
Okay I need to provide the name add to hint client.

01:20.030 --> 01:23.240
Let's name it no OP.

01:25.190 --> 01:25.730
Okay.

01:25.730 --> 01:28.070
And I'm going to remove this pin as well.

01:28.070 --> 01:29.930
We're not going to make the speed like that.

01:29.990 --> 01:33.560
And let's actually save to get the Autocompletion for Slcp.

01:34.160 --> 01:34.520
Okay.

01:34.520 --> 01:37.730
And let's start from this and let's create a client.

01:37.730 --> 01:41.810
So we will start by initializing Ros2 communications.

01:41.810 --> 01:44.150
Then we create a node.

01:44.180 --> 01:46.370
It's going to be a shared pointer to a node.

01:46.520 --> 01:47.750
We're going to write our code.

01:47.750 --> 01:50.270
And at the end we shut down Ros2.

01:50.270 --> 01:54.080
So let's create a client I'm going to just use the auto keyword.

01:54.080 --> 01:59.780
Again client is equal to and we're going to use the node object here.

01:59.780 --> 02:05.630
And in the node we have the functionality create client as simple as that.

02:05.630 --> 02:08.780
And we need, of course, to provide an interface.

02:08.780 --> 02:19.430
So I'm also going to include the interface which is example interfaces solve and add to instance HPP.

02:19.430 --> 02:23.060
And let's let's save right now to get the auto completion okay.

02:23.090 --> 02:28.610
So it's important that you use the same interface in the server and in the client of course.

02:28.610 --> 02:32.600
So when we create the client it's going to be a template for the interface.

02:32.600 --> 02:35.210
So we provide an interface here.

02:35.270 --> 02:42.500
Example interfaces solve add to ints like that.

02:42.500 --> 02:45.230
And then we need to open the parentheses.

02:45.230 --> 02:48.950
And in this that's going to be the name of the service.

02:48.950 --> 02:52.040
So if we go back here what was the name of the service.

02:52.070 --> 02:54.740
It's add to int right here.

02:55.310 --> 03:02.780
So I will just use the same name for the client and the server so that they can communicate.

03:02.810 --> 03:03.170
All right.

03:03.170 --> 03:03.920
So two things.

03:03.920 --> 03:08.090
To create the client you need the interface and the a name as always.

03:08.120 --> 03:13.850
Then we are going to also wait for the service to be up, because if you try to send the request and

03:13.850 --> 03:16.670
the server is not up, then you're going to get an error.

03:16.670 --> 03:18.860
So let's do here a while.

03:19.220 --> 03:21.620
Not client.

03:22.220 --> 03:23.060
Wait.

03:23.060 --> 03:27.260
So you see I use an error because of course that's going to be a shared pointer.

03:27.860 --> 03:33.320
So wait for service and we need to provide a timeout.

03:33.320 --> 03:44.840
I'm just going to be using here namespace std chrono literals.

03:45.290 --> 03:48.050
And let's say one second.

03:49.610 --> 03:54.470
So basically wait for service is going to return true if the service is found.

03:54.470 --> 03:57.200
But then if it's not found it's going to wait.

03:57.290 --> 04:00.410
If you provide a timeout it's going to wait until the timeout.

04:00.410 --> 04:06.950
So one second if after one second the service is not found, that's going to return false.

04:06.980 --> 04:10.130
And so with the negation here is going to become true.

04:10.130 --> 04:16.070
And so as long as the server is not up every one second, we can do something.

04:16.100 --> 04:18.260
For example, print clcp.

04:18.770 --> 04:35.150
This time I can use Clcp one okay node get logger and we're going to print waiting for the server okay.

04:35.150 --> 04:38.540
You can just add whatever you want okay.

04:38.570 --> 04:42.890
And after this line we know that the server is found.

04:42.890 --> 04:50.960
So if the server is found we can create a request and send a request I'm going to do auto request is

04:50.960 --> 04:51.800
equal to.

04:51.830 --> 04:54.980
And we are going to create a request object.

04:54.980 --> 04:57.440
And actually not just a request object.

04:57.440 --> 05:01.010
But we need to do STD make shared.

05:01.010 --> 05:11.480
So we're going to create a shared pointer to a request object which is example interfaces SRB And two

05:11.510 --> 05:14.570
ints and then we add requests.

05:14.570 --> 05:16.430
You see we have request and response.

05:16.430 --> 05:19.400
So we have two messages inside this interface.

05:19.430 --> 05:22.280
Close the bracket and then we have parentheses.

05:22.310 --> 05:26.900
And so here is a quite big difference when you create a request.

05:26.900 --> 05:32.540
So in C plus plus when you create a request message or if you create if I go back to for example the

05:32.540 --> 05:33.980
number publisher.

05:34.010 --> 05:39.830
You see when you create a message for a topic, you see we don't create a shared pointer.

05:39.830 --> 05:43.250
We just create the object itself and then we publish it.

05:43.250 --> 05:50.630
That's the difference here in a service client we create a shared pointer of the request.

05:50.630 --> 05:54.260
So this is a small inconsistency here in C plus plus okay.

05:54.290 --> 06:00.920
And now we have our request we can request a with an arrow is equal to.

06:00.950 --> 06:03.230
Let's give a number six.

06:04.010 --> 06:10.700
For example request B is equal to two.

06:11.180 --> 06:12.710
So the sum should be eight.

06:12.750 --> 06:18.270
and then we are going to use the client to send the request to the server.

06:18.270 --> 06:22.050
And here we have the async send request.

06:22.050 --> 06:28.020
So we're going to use the asynchronous call so that then we can spin until we get the response.

06:28.020 --> 06:33.120
So async send request I give the shared pointer to the.

06:33.150 --> 06:37.470
So I give the request here which is the shared pointer to the request object.

06:37.470 --> 06:40.320
And that's going to return a future.

06:40.320 --> 06:43.770
And I can use the auto keyword again.

06:43.800 --> 06:46.860
You see that's going to be a future here.

06:47.190 --> 06:54.030
Well that's a that's a client future object something that's specific to Ros2.

06:54.060 --> 07:02.820
And then I can do a clcp spin but not spin spin until future complete.

07:03.300 --> 07:08.220
And I will need to pass first the node because we need to make the node spin.

07:08.220 --> 07:10.140
And for what future?

07:10.140 --> 07:16.650
For the future we have just created here, which is the one we get from the async send request function.

07:16.680 --> 07:17.520
All right.

07:17.520 --> 07:23.490
And then after this returns then we can try to get the response.

07:23.490 --> 07:29.670
So let's do auto response is equal to future dot get okay.

07:29.700 --> 07:32.790
That's how you get the response.

07:32.910 --> 07:36.480
And you can see here we get a shared pointer.

07:36.510 --> 07:42.390
Actually not just the response but a shared pointer to the add to insert response.

07:42.690 --> 07:43.230
All right.

07:43.230 --> 07:53.460
And then if you want you can print a CSP info this get logger.

07:56.700 --> 07:59.280
Oh actually it's not this it's node.

07:59.280 --> 08:00.600
We are not in a class.

08:00.600 --> 08:02.250
We use the node here.

08:02.940 --> 08:06.570
And then I think I've already written that here.

08:06.570 --> 08:10.830
You see we can just do the sum.

08:11.070 --> 08:17.880
I'm just going to take that code and use it there Okay.

08:20.130 --> 08:22.290
So we print the request A plus.

08:22.290 --> 08:28.170
The request b is equal to the response sum that we get after the spinning.

08:28.200 --> 08:28.980
All right.

08:28.980 --> 08:30.900
And the program is complete.

08:30.930 --> 08:31.890
Let's save.

08:31.920 --> 08:33.630
Let's just do a quick recap.

08:34.050 --> 08:35.910
So we initialize rows to communications.

08:35.910 --> 08:37.920
We start a node.

08:37.920 --> 08:40.050
From the node we create a client.

08:40.200 --> 08:42.630
We wait for the service to be up.

08:42.690 --> 08:46.770
After the service is up we can create a request.

08:46.800 --> 08:49.020
We send the request asynchronously.

08:49.020 --> 08:55.440
And then we're going to make the node spin until that future is complete okay.

08:55.470 --> 09:00.780
So the future we get after we send the request then we get the request from the future.

09:00.780 --> 09:03.960
And well then you just do whatever you want with the response.

09:03.960 --> 09:07.380
Let's add an executable to test that.

09:07.710 --> 09:15.630
So another one here add executable add to its client.

09:16.290 --> 09:21.810
No, OOP is quite a long name from SK.

09:22.170 --> 09:26.730
Add two ints client no oop cpp.

09:26.760 --> 09:38.700
That's the file name and an amount target dependencies with this executable name and what dependencies

09:38.700 --> 09:43.620
we need are clcp of course and example interfaces.

09:44.640 --> 09:47.700
Let's not forget to install the executable.

09:47.730 --> 09:50.160
Add two ints client.

09:50.190 --> 09:51.570
No OOP here.

09:51.780 --> 09:56.130
Okay so once again we add those two lines and that one here.

09:56.430 --> 09:57.810
Let's save everything.

09:57.810 --> 10:04.620
And let's go back to the terminal to build the package.

10:05.490 --> 10:11.430
So let's do a call on build I should have the the command with packages.

10:11.430 --> 10:14.010
Select my C plus plus package.

10:15.750 --> 10:16.680
Okay.

10:16.680 --> 10:21.700
And let's well let's use those terminals I'm just going to use that one.

10:23.530 --> 10:28.900
Actually the both terminals, because we have just built and we have a new node.

10:28.900 --> 10:30.220
And let's start here.

10:30.250 --> 10:38.440
Ros to run my CPP package with add two ints client no OOP.

10:38.620 --> 10:40.480
So that's the C plus plus one right.

10:40.480 --> 10:42.100
That's the one we've just written.

10:42.100 --> 10:48.250
And this we should see a log a warning log waiting for the server because there is no server.

10:48.280 --> 10:50.350
Now I'm going to start the server here.

10:50.530 --> 10:52.720
So the C plus plus server for example.

10:53.470 --> 10:57.580
And you see as soon as I start the server it's been found on the client.

10:57.610 --> 11:00.370
The client will send a request.

11:00.400 --> 11:06.160
It's received by the server which processes the request, sends back the response, and then we get

11:06.160 --> 11:07.720
the response on the client side.

11:07.750 --> 11:08.110
Great.

11:08.110 --> 11:14.590
So you have seen the mechanism for the C plus plus client, which is exactly the same as the Python

11:14.590 --> 11:14.830
one.

11:14.860 --> 11:14.980
Okay.

11:15.010 --> 11:18.910
That's the same behavior as we did for the Python client.

11:18.910 --> 11:21.160
And to recap here this code.

11:21.160 --> 11:27.900
So that's not a normal OOP code for rust, but this code is very useful to test services very fast.

11:27.930 --> 11:33.000
Okay, if you have a service server and a request is a bit too complicated to use in the terminal,

11:33.000 --> 11:36.750
then you just use this as a template and you can just test your server.

11:36.750 --> 11:41.100
In fact, for just that script, you could even just use the Python script.

11:41.130 --> 11:42.690
Okay, you don't even need this C plus.

11:42.690 --> 11:44.160
Plus that was just for the example.

11:44.160 --> 11:50.640
But you could use the where is it that script here in Python if you have written that okay.

11:50.640 --> 11:55.200
Because it's just for testing purposes, you're not going to use that in your application in your end

11:55.230 --> 11:56.010
application.

11:56.010 --> 12:00.120
So for just testing purposes you could just use the Python one.

12:00.120 --> 12:00.930
And why is that?

12:00.930 --> 12:05.760
Because if you use Simulink install then you can just modify the code and make different tests.

12:05.760 --> 12:08.010
And you don't need to build every time.

12:08.010 --> 12:09.690
So it's going to be much faster.

12:09.720 --> 12:09.960
Okay.

12:09.990 --> 12:14.850
So for this example the C plus plus one is actually not really needed.

12:14.850 --> 12:19.140
But now you know how to write a C plus plus client without OOP.

12:19.170 --> 12:25.980
Let's see the OOP version which is what you will use in the nodes that you write for your future applications.
