WEBVTT

00:00.080 --> 00:03.950
And let's now improve this node with object oriented programming.

00:03.950 --> 00:07.340
Just as for Python, we're going to improve this.

00:07.340 --> 00:12.380
And then that's going to be the C plus plus template you can use for any future node that you create.

00:12.380 --> 00:18.650
So instead of creating the node here using the node class directly from the main, I'm going to create

00:18.650 --> 00:24.230
my own class for the node that's going to inherit from our Clcp node.

00:24.230 --> 00:28.790
And then in this class I will be able to add as many functionalities as I want.

00:28.820 --> 00:31.370
That's going to make it much easier for the future.

00:31.370 --> 00:35.030
So let's create a C plus plus class class.

00:35.120 --> 00:41.510
Let's call it my node and then how to inherit from Clcp node.

00:41.510 --> 00:43.160
I'm going to do this public.

00:43.160 --> 00:48.920
So that's the public inheritance Clcp node.

00:49.370 --> 00:52.310
That's the declaration of the class.

00:52.310 --> 00:53.960
So I add curly brackets.

00:53.960 --> 01:00.290
And one very important thing is don't forget the semicolon at the end of this curly bracket here okay.

01:00.290 --> 01:05.650
And in this I will have public and then private in public.

01:05.650 --> 01:08.590
I'm going to add the class constructor.

01:08.590 --> 01:11.350
So my node like that.

01:11.350 --> 01:20.290
And we can directly use the node constructor here and pass the node name okay.

01:21.310 --> 01:26.080
So this syntax will if you don't know much about this syntax that's basically how to create a class

01:26.080 --> 01:26.530
in C plus.

01:26.530 --> 01:31.630
Plus you can find also other tutorials if you feel too lost about this.

01:31.630 --> 01:36.550
But be reassured that once we have finished this, we can use this as a template.

01:36.550 --> 01:39.700
And then well, after you get used to it, it's going to be much easier.

01:39.760 --> 01:44.290
So let's open the curly brackets for the function.

01:44.290 --> 01:50.740
And that's basically it for our first class, our first node class okay.

01:50.770 --> 01:53.950
So we need to inherit from our sleep node.

01:53.950 --> 01:57.010
And then we need to call the parent constructor.

01:57.040 --> 01:58.270
That's what we do here.

01:58.270 --> 02:03.750
So my node we call the parent constructor and we need to give it also the node name.

02:03.750 --> 02:05.970
That's basically what we've done here.

02:05.970 --> 02:09.570
Then if we want to add a functionality here, that's what we've done.

02:09.570 --> 02:12.030
We can use Lclc-rp info.

02:12.150 --> 02:16.110
So Lclc-rp info.

02:16.500 --> 02:23.520
And then as we add directly in the class that is a node because of the inheritance, we don't need to

02:23.550 --> 02:24.420
do node something.

02:24.420 --> 02:27.120
We can just do get logger.

02:27.420 --> 02:27.780
Okay.

02:27.810 --> 02:28.830
Just like that.

02:28.830 --> 02:36.000
And usually what I like to do when I use the functionalities from the class here from the node, just

02:36.000 --> 02:39.180
to make it explicit, I use the this keyword.

02:39.180 --> 02:45.840
So you don't need to do you don't need to write this okay, I'm going to use this here for this course,

02:45.870 --> 02:50.250
just so that when you see a function you know that it's coming actually from the node class, but it's

02:50.250 --> 02:52.140
not mandatory, all right?

02:52.170 --> 02:54.750
It's just a kind of a convention I will follow here.

02:54.750 --> 02:56.430
And then you can do whatever you want.

02:56.430 --> 03:05.650
So we first have the logger and then we have the string, which I'm going to just take from here, and

03:05.650 --> 03:07.210
that's basically the same thing.

03:07.510 --> 03:08.800
So we have a constructor.

03:08.800 --> 03:11.620
In the constructor we print something.

03:11.620 --> 03:14.770
So now what I can do I'm just going to remove that line.

03:15.040 --> 03:23.590
And I'm going to modify this line so that instead of creating just a clcp node I'm still going to do

03:23.620 --> 03:25.000
a shared pointer.

03:25.000 --> 03:29.800
But this time it's going to be my node okay.

03:29.830 --> 03:31.270
And open close parentheses.

03:31.300 --> 03:35.110
We don't have anything to pass here because we don't have any parameter.

03:35.110 --> 03:42.190
So what we do is we create a shared pointer of a new object from my node that inherits from node.

03:42.190 --> 03:44.770
And then we can just spin the node.

03:44.770 --> 03:46.630
You can see the auto here.

03:46.660 --> 03:48.910
It's a shared pointer to my node.

03:48.910 --> 03:51.280
And my node is an lltp node.

03:51.280 --> 03:52.330
So that's the same thing.

03:52.330 --> 03:52.750
Great.

03:52.750 --> 03:54.490
And that is basically the template.

03:54.520 --> 03:58.000
So the template you don't need this line here.

03:58.030 --> 04:00.420
That's the template I'm going to give to you.

04:00.420 --> 04:03.660
So those 20 lines here, you include the lcbp.

04:03.690 --> 04:08.370
You create the class and then you have the main where you initialize the rows to communication.

04:08.370 --> 04:11.400
You create a node, you make the node spin and then you shut down.

04:11.430 --> 04:11.910
Okay.

04:11.940 --> 04:15.300
Now let's come back to this code.

04:15.300 --> 04:20.700
And I'm just going to add like I did with Python, I'm going to add one more functionality which is

04:20.700 --> 04:21.840
a timer okay.

04:21.870 --> 04:27.990
Because the timer is going to be super useful if you want to have a node that repeats an action, let's

04:27.990 --> 04:30.780
say ten times per second or 20 times per second.

04:30.780 --> 04:35.400
So it could be a sensor reading some data, it could be a controller or it could be anything.

04:35.400 --> 04:39.840
And that's very frequent to use this kind of structure in robotics.

04:39.840 --> 04:43.200
So we're going to see how to create a timer.

04:43.200 --> 04:48.900
And the syntax of course with C plus plus is always a bit more complicated than Python.

04:48.900 --> 04:55.530
So first of all I will create here an so private attribute or clcp.

04:56.430 --> 05:00.170
And we have Timer base.

05:00.290 --> 05:01.610
That's going to be a timer.

05:01.610 --> 05:06.650
And as I told you, everything we're going to do will use shared pointers.

05:06.650 --> 05:15.020
So we have another thing that is called shared as you can see shared Peter okay.

05:15.050 --> 05:16.610
That's kind of a helper.

05:16.640 --> 05:20.690
You see that's so here it's written a shared pointer.

05:20.690 --> 05:26.690
Basically it's going to create an STD shared pointer of this class here of this object okay.

05:26.690 --> 05:31.460
So that's a helper that you can use instead of writing STD shared pointer.

05:31.460 --> 05:37.490
So we create a timer here timer as a private attribute.

05:37.490 --> 05:42.500
And then okay before I create the timer I'm going to create the callback.

05:42.830 --> 05:50.180
So let's do void timer callback I just create a function here a method.

05:50.180 --> 05:59.380
And let's just do Lclc-rp info with this gets longer.

05:59.560 --> 06:02.470
You can see how Autocompletion is very useful.

06:02.500 --> 06:03.610
And then.

06:03.670 --> 06:04.450
Hello.

06:04.540 --> 06:06.580
Okay, so code is valid.

06:06.580 --> 06:08.500
And in the constructor.

06:08.500 --> 06:12.820
So after I create the log I'm also going to create a timer.

06:12.820 --> 06:18.640
So I'm going to use this timer and say that the timer is equal to.

06:18.670 --> 06:22.690
And we have a function inside node that is.

06:22.690 --> 06:24.100
So I'm going to use the this keyword.

06:24.100 --> 06:27.220
Once again you don't have to okay.

06:27.250 --> 06:32.140
It's just for me to make it super explicit that when I use this it's about the node class.

06:32.620 --> 06:36.670
Create wall timer okay.

06:36.700 --> 06:38.950
And you can see it is recognized.

06:39.190 --> 06:41.140
So you can use the Autocompletion.

06:41.140 --> 06:46.630
And so here we need you see we need to give a STD chrono duration.

06:46.630 --> 06:51.550
So for example I'm going to use STD and then Chrono.

06:52.180 --> 06:59.070
And then I can use seconds because I just want one second which is going to be an integer here.

06:59.100 --> 06:59.550
Okay.

06:59.580 --> 07:02.190
So first that's going to be the period.

07:02.190 --> 07:07.380
So every one second and then I need to give the callback.

07:07.680 --> 07:13.140
I'm going to go back to a new line here and align that here.

07:13.950 --> 07:14.580
Okay.

07:14.580 --> 07:17.640
And how to link the callback.

07:17.670 --> 07:21.870
Well we need to use the std bind function.

07:21.870 --> 07:24.390
What do we need to do inside the Stdbind function.

07:24.390 --> 07:29.310
We need to give the reference to the callback with this symbol.

07:29.490 --> 07:33.090
So the ampersand and then my node.

07:33.090 --> 07:39.270
That's the class name plus timer callback.

07:39.270 --> 07:43.050
That's the name of the function without the parentheses.

07:43.080 --> 07:47.490
Okay, we are not calling the function, we are just referencing it.

07:47.490 --> 07:48.990
So I need to do this.

07:48.990 --> 07:51.120
And then I need to bind with this.

07:51.120 --> 07:55.710
So this is this class this object that I'm binding to.

07:55.740 --> 07:56.340
Okay.

07:56.430 --> 07:58.820
And let's not forget the semicolon.

07:59.120 --> 08:03.980
So this stdbind, I know the first time that you use that, it's a bit confusing.

08:03.980 --> 08:08.510
Everything you need to write inside, but well, once again, as you get used to it, it's always the

08:08.510 --> 08:09.290
same thing.

08:09.290 --> 08:13.490
And to complete this example, right now I'm just going to create a counter here.

08:13.490 --> 08:17.030
So let's do encounter same thing.

08:17.030 --> 08:20.960
I use a trailing underscore when I create an attribute in a class.

08:21.110 --> 08:22.970
It's just a convention that I follow.

08:23.120 --> 08:24.650
So I create a counter.

08:24.650 --> 08:29.570
I initialize it here to zero.

08:30.410 --> 08:33.920
And then I can use it in this method.

08:33.920 --> 08:35.060
So hello.

08:35.060 --> 08:39.890
And then percentage D counter.

08:40.010 --> 08:45.500
So you see we just create a string like you would do with printf or something like that.

08:45.500 --> 08:54.320
So percentage d and then colon and you put the variable and let's not forget to do counter plus plus

08:54.320 --> 08:56.420
to add one to the counter every time.

08:56.440 --> 08:57.850
And that looks good.

08:57.850 --> 08:59.560
So let's do a quick recap.

08:59.560 --> 09:02.800
We have created our class here okay.

09:02.920 --> 09:06.370
So when we start our program what's going to happen.

09:06.370 --> 09:09.220
First we're going to initialize Ros2 communications.

09:09.250 --> 09:09.940
Great.

09:09.970 --> 09:14.260
Then we are going to create a shared pointer to my node.

09:14.260 --> 09:18.430
So we're going to create an object my node of the minute class.

09:18.430 --> 09:22.000
When we create the object the constructor is going to be called.

09:22.000 --> 09:23.740
So we call the parent constructor.

09:23.740 --> 09:25.570
We initialize the node with a name.

09:25.660 --> 09:27.670
And then we print hello world.

09:27.670 --> 09:32.590
And we register the timer callback here with a world timer.

09:32.590 --> 09:33.700
So we create a timer.

09:33.700 --> 09:38.260
And the timer says that we need to call this function every one second.

09:38.260 --> 09:41.020
So still nothing is happening with this timer.

09:41.050 --> 09:44.380
Then we exit from the constructor.

09:44.380 --> 09:47.050
We go to this line and we make the node spin.

09:47.050 --> 09:49.180
When the node spins it's going to be.

09:49.180 --> 09:50.680
So the execution is going to be stopped.

09:50.680 --> 09:53.050
Here the node will be kept alive.

09:53.050 --> 09:57.650
And all the callbacks that have been registered can now be called.

09:57.680 --> 09:59.540
And because we have created a timer.

09:59.570 --> 10:03.740
The timer says every one second we need to call the timer callback.

10:03.770 --> 10:09.830
So while the node is spinning, this timer callback will be called every one second.

10:09.860 --> 10:13.190
Then when we press Ctrl C that's going to stop the spinning.

10:13.190 --> 10:14.030
And then we shut down.

10:14.060 --> 10:16.040
We exit and that's it.

10:16.220 --> 10:18.470
So let's save this file.

10:18.500 --> 10:20.720
Make sure that you save this file.

10:20.720 --> 10:23.000
There is nothing else to do in the cmakelists.txt.

10:23.030 --> 10:23.210
Okay.

10:23.240 --> 10:25.520
It's already been done previously.

10:25.520 --> 10:30.350
And let's go back to the terminal and let's do again build.

10:32.870 --> 10:37.640
And then source and then run.

10:37.910 --> 10:45.920
And you see we have hello 01234 etc. and I press Ctrl C and that's it.

10:45.950 --> 10:46.430
Great.

10:46.430 --> 10:51.890
So you have successfully created a C plus plus node with object oriented programming.

10:51.890 --> 10:56.720
And you also have seen how to create your first timer with a callback.
