WEBVTT

00:00.080 --> 00:02.180
This is the solution for the activity.

00:02.210 --> 00:06.680
Number three, we will add a new service server inside the number counter.

00:06.710 --> 00:08.300
Note here that we have created.

00:08.300 --> 00:12.680
In the last section I will only provide a video for the Python solution.

00:12.680 --> 00:18.950
But of course you can find the complete C plus plus solution inside the code archive that you can download

00:18.950 --> 00:25.220
at the end of this section, so you can open the number counter dot p y that we've created before.

00:25.220 --> 00:31.580
And actually before we write anything, let's just go to the terminal and let's check the interface

00:31.580 --> 00:33.620
that we will need to use.

00:33.620 --> 00:37.310
So let's do Ros2 interface show.

00:37.310 --> 00:38.540
And I gave you that one.

00:38.570 --> 00:46.880
I gave you example interfaces slash SRV slash set bool.

00:48.680 --> 00:54.260
So you can see we have some commands first saying that this is like something to test.

00:54.260 --> 01:04.440
And then we have a request here that contains only one field name is data and the type is bool for boolean.

01:04.470 --> 01:07.860
Then we have the three dashes and then we have the response.

01:07.860 --> 01:11.640
So that's what we're going to receive from the server to the client.

01:11.640 --> 01:14.940
So a success flag is going to be true or false.

01:14.940 --> 01:16.590
And then a message.

01:16.590 --> 01:21.450
For example if you have an error you could give more information about what went wrong.

01:21.450 --> 01:22.170
So great.

01:22.170 --> 01:23.700
We know the interface.

01:23.700 --> 01:25.260
So that's the package.

01:25.260 --> 01:27.930
And we know what's inside the interface.

01:27.930 --> 01:29.610
So now we can go back to the code.

01:29.640 --> 01:32.130
And in this number counter dot p y.

01:32.160 --> 01:34.650
Let's add this interface.

01:34.800 --> 01:41.670
So after the other imports here I'm going to do from example interfaces dot.

01:41.670 --> 01:49.110
And here it's sorry because we import a service interface import set bool.

01:49.140 --> 01:49.710
All right.

01:49.710 --> 01:54.660
And well we've already added example interfaces into the package dot XML.

01:54.660 --> 01:56.100
So the dependency for that.

01:56.100 --> 01:57.810
So we don't need to do it anymore.

01:57.840 --> 01:59.880
Then I'm going to add a service server.

01:59.880 --> 02:01.890
Where do I add this service?

02:01.920 --> 02:04.500
Well, just in the constructor after.

02:04.530 --> 02:07.170
Well, for example, we have a publisher a subscriber.

02:07.170 --> 02:11.070
I'm going to add it right here before the end log.

02:11.070 --> 02:13.740
So let's name it self dot.

02:14.250 --> 02:17.130
We want to reset the counter.

02:17.130 --> 02:21.660
So reset counter service for example.

02:21.660 --> 02:26.490
And that's going to be self dot create service.

02:26.760 --> 02:30.150
We need to provide the type set bool.

02:30.240 --> 02:32.430
We need to provide the name.

02:32.430 --> 02:33.750
So what's the name.

02:33.750 --> 02:36.600
Well we decided here because we created the service.

02:36.600 --> 02:40.530
When we create the server and we just want to reset the counter.

02:40.530 --> 02:42.810
So let's call it reset counter.

02:43.170 --> 02:44.910
Let's keep things simple here.

02:44.910 --> 02:47.940
And finally we will need to provide a callback.

02:47.970 --> 02:48.450
Okay.

02:48.480 --> 02:52.380
Because when the node is spinning it can receive requests.

02:52.380 --> 02:58.290
When we receive a request, in order to be able to process it we have to write a callback.

02:58.320 --> 03:01.840
And so let's just add a callback inside the class here.

03:01.870 --> 03:03.490
The order doesn't matter.

03:03.520 --> 03:13.270
Make sure that the function is inside the class and let's name it callback reset counter okay with self.

03:13.270 --> 03:18.220
And then we have the request and the response request response.

03:18.280 --> 03:18.730
Okay.

03:18.730 --> 03:21.820
So as you can see for a subscriber we just get the message.

03:21.820 --> 03:25.090
But for a service server we get the request.

03:25.090 --> 03:27.670
And already the response object.

03:27.670 --> 03:29.860
I'm going to make it explicit here.

03:29.860 --> 03:35.050
So the request is set bull dot request.

03:35.050 --> 03:40.930
And the response is set bull dot response.

03:42.130 --> 03:47.440
All right let's just put pass for now so we can finish this line here.

03:47.440 --> 03:52.630
So I'm going to put self dot callback reset counter.

03:53.200 --> 03:55.990
And let's go to a new line here to make it a bit cleaner.

03:55.990 --> 04:00.320
So you see we create a service with the interface the name and the callback.

04:00.320 --> 04:05.480
Now let's go to the callback and let's implement the callback.

04:05.480 --> 04:06.770
So what do we want to do?

04:06.800 --> 04:10.520
You see we receive here a wool data.

04:10.520 --> 04:14.150
So let's say that we want to reset the counter if this is true okay.

04:14.150 --> 04:18.020
So if you provide false we are not going to reset the counter.

04:18.710 --> 04:23.450
So I can do if request dot data.

04:23.510 --> 04:28.820
If this is true then we do self dot counter.

04:29.390 --> 04:35.360
So the counter is here is equal to zero just as simple as that.

04:35.360 --> 04:38.900
So you see we initialize the counter to zero when we start the node.

04:38.900 --> 04:46.400
Then whenever we receive a message here on the number topic we're going to add the data to the counter.

04:46.400 --> 04:53.360
But if we receive a request for this service server here reset counter, we also reset the counter to

04:53.390 --> 04:53.990
zero.

04:53.990 --> 04:56.090
And then well that's the action okay.

04:56.120 --> 04:59.960
The action is if we request to reset the counter.

04:59.990 --> 05:02.030
Then we reset the counter to zero.

05:02.060 --> 05:06.740
But then we need to also provide a response for the client.

05:06.740 --> 05:09.200
So response dot.

05:09.200 --> 05:13.640
And we have two things we have success and message.

05:14.240 --> 05:15.980
So let's do response dot.

05:15.980 --> 05:20.810
Success is equal to true and response.

05:20.810 --> 05:22.460
Let's add a message.

05:23.180 --> 05:26.780
You could just leave it empty but let's just say okay.

05:26.870 --> 05:30.860
Or let's say counter has been reset.

05:30.890 --> 05:31.610
All right.

05:31.610 --> 05:33.500
And let's add an else.

05:33.680 --> 05:33.980
Okay.

05:34.010 --> 05:39.830
If the data here is false, then we are not going to reset the counter.

05:39.830 --> 05:44.810
And we are going to put the response dot success for example false.

05:44.840 --> 05:56.840
Just for the example and response dot message let's say counter has not been reset.

05:57.620 --> 06:01.620
Well here it's just because you provide it false in the request.

06:01.620 --> 06:06.750
But if, for example, you would have a service to enable a motor or something like that, then you

06:06.750 --> 06:08.490
would probably need to do some checks.

06:08.490 --> 06:15.030
And if some checks are failing, then you could return a response with a message that explained what

06:15.030 --> 06:15.930
was the issue.

06:15.960 --> 06:17.400
Okay, so that's it.

06:17.400 --> 06:22.320
But actually we need to return the response.

06:22.320 --> 06:23.550
So very important.

06:23.580 --> 06:23.760
Okay.

06:23.790 --> 06:24.660
Don't forget that.

06:24.690 --> 06:28.020
Otherwise you will get an error when you run the service.

06:28.050 --> 06:28.590
Okay.

06:28.620 --> 06:31.770
So you see we just do the action.

06:31.770 --> 06:35.730
We fill the response and we return the response.

06:35.730 --> 06:37.170
And basically that's it.

06:37.170 --> 06:39.150
That's it for this activity, for the code.

06:39.150 --> 06:40.470
It's not much to do.

06:40.530 --> 06:40.830
Okay.

06:40.860 --> 06:44.010
So to recap we have added the interface here.

06:44.340 --> 06:48.690
And then we create a service server inside the constructor.

06:48.690 --> 06:52.380
And we have a callback to handle incoming requests.

06:52.410 --> 06:54.750
Now I'm going to save that.

06:54.840 --> 07:00.720
We don't need to do anything else in the setup.py because we already have the number counter executable

07:00.750 --> 07:03.180
here, so nothing to do.

07:03.210 --> 07:09.990
Let's go back to the terminal and well let's build it again because I'm not sure if last time I built

07:10.020 --> 07:19.830
was with Simulink install called build packages select and the Python package.

07:21.780 --> 07:22.260
Okay.

07:22.260 --> 07:29.550
And I'm just going to start new terminals so that everything is sourced correctly.

07:29.550 --> 07:34.860
And to test this well I'm gonna start first the number publisher.

07:34.860 --> 07:45.960
So Ros to run number um that's my Pi PG with number publisher here I'm going to start the number counter

07:45.990 --> 07:47.700
here Ros to run.

07:49.860 --> 07:58.500
With number counter I start with two because well the counter is only going to be increased if we receive

07:58.530 --> 07:59.980
data on the number topic.

07:59.980 --> 08:01.810
So we also need the number publisher.

08:01.810 --> 08:07.630
So let's see what we have here Ros2 service list.

08:08.950 --> 08:13.960
So you see we have lots of services because we have those that are automatically generated.

08:13.960 --> 08:24.400
But then we find the reset counter okay Ros2 service type with reset counter.

08:25.480 --> 08:29.680
And we find set bool interface okay.

08:29.710 --> 08:31.900
So let's make a simple test here.

08:31.930 --> 08:38.050
Let's clear that and we can do Ros2 service call with.

08:38.080 --> 08:41.110
So that's reset counter.

08:41.200 --> 08:44.620
And then example interfaces set bool.

08:44.620 --> 08:50.620
And we had a field named data which is supposed to be a boolean.

08:50.620 --> 08:52.180
So let's put true.

08:53.560 --> 09:03.170
And you see we have here success true Message counter has been reset, but we don't see anything because,

09:03.200 --> 09:14.870
well, to see something we need to do a rostopic echo with the number count and you can see the counter

09:14.900 --> 09:15.890
going up.

09:15.890 --> 09:23.630
And then if I call the service again, you will see that we start at zero again.

09:23.660 --> 09:25.820
You see we went back to zero okay.

09:25.850 --> 09:32.150
Every time I run the service, every time I make a request we start at zero.

09:32.180 --> 09:37.370
Now let's say I make a request, but I put data false.

09:38.330 --> 09:41.180
In this case, the service is still working correctly.

09:41.180 --> 09:43.400
But you see, because we have provided false.

09:43.400 --> 09:48.200
And then in the code we said that if it's false we don't reset the counter.

09:48.230 --> 09:50.420
Then you see here we receive success.

09:50.420 --> 09:56.360
False counter has not been reset and you can see it also here the counter is not reset.

09:56.390 --> 09:56.990
All right.

09:56.990 --> 10:04.340
So with those two cases, you can see the application is working.

10:06.470 --> 10:06.890
All right.

10:06.920 --> 10:10.880
And if you want you can also create some code for the client.

10:10.880 --> 10:15.800
So you could use the for example add to in clients no OOP.

10:15.980 --> 10:21.470
And modify this to create a simple script to call this new service.

10:21.500 --> 10:24.980
Or you could also use that one with object oriented programming.

10:24.980 --> 10:27.050
So you can do that if you want to practice a bit more.

10:27.050 --> 10:29.480
And the activity is now finished.

10:29.480 --> 10:33.140
Once again, you can download all the code at the end of this section.

10:33.170 --> 10:33.530
Okay.

10:33.560 --> 10:37.100
The C plus plus solution is going to be exact same stuff okay.

10:37.100 --> 10:38.810
Just using the C plus plus syntax.

10:38.810 --> 10:40.460
And just a final note here.

10:40.460 --> 10:46.220
If I come back to this number counter, you can see that in a node you can have many different things.

10:46.220 --> 10:52.100
Here we have one publisher one subscriber one service server okay.

10:52.130 --> 10:54.350
So you can have any combination of those.

10:54.350 --> 10:56.750
You can create as many as you want.

10:56.780 --> 10:59.210
Just make sure that they all have a different name.
