And let's now improve this node with object oriented programming.
Just as for Python, we're going to improve this.
And then that's going to be the C plus plus template you can use for any future node that you create.
So instead of creating the node here using the node class directly from the main, I'm going to create
my own class for the node that's going to inherit from our Clcp node.
And then in this class I will be able to add as many functionalities as I want.
That's going to make it much easier for the future.
So let's create a C plus plus class class.
Let's call it my node and then how to inherit from Clcp node.
I'm going to do this public.
So that's the public inheritance Clcp node.
That's the declaration of the class.
So I add curly brackets.
And one very important thing is don't forget the semicolon at the end of this curly bracket here okay.
And in this I will have public and then private in public.
I'm going to add the class constructor.
So my node like that.
And we can directly use the node constructor here and pass the node name okay.
So this syntax will if you don't know much about this syntax that's basically how to create a class
in C plus.
Plus you can find also other tutorials if you feel too lost about this.
But be reassured that once we have finished this, we can use this as a template.
And then well, after you get used to it, it's going to be much easier.
So let's open the curly brackets for the function.
And that's basically it for our first class, our first node class okay.
So we need to inherit from our sleep node.
And then we need to call the parent constructor.
That's what we do here.
So my node we call the parent constructor and we need to give it also the node name.
That's basically what we've done here.
Then if we want to add a functionality here, that's what we've done.
We can use Lclc-rp info.
So Lclc-rp info.
And then as we add directly in the class that is a node because of the inheritance, we don't need to
do node something.
We can just do get logger.
Okay.
Just like that.
And usually what I like to do when I use the functionalities from the class here from the node, just
to make it explicit, I use the this keyword.
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,
just so that when you see a function you know that it's coming actually from the node class, but it's
not mandatory, all right?
It's just a kind of a convention I will follow here.
And then you can do whatever you want.
So we first have the logger and then we have the string, which I'm going to just take from here, and
that's basically the same thing.
So we have a constructor.
In the constructor we print something.
So now what I can do I'm just going to remove that line.
And I'm going to modify this line so that instead of creating just a clcp node I'm still going to do
a shared pointer.
But this time it's going to be my node okay.
And open close parentheses.
We don't have anything to pass here because we don't have any parameter.
So what we do is we create a shared pointer of a new object from my node that inherits from node.
And then we can just spin the node.
You can see the auto here.
It's a shared pointer to my node.
And my node is an lltp node.
So that's the same thing.
Great.
And that is basically the template.
So the template you don't need this line here.
That's the template I'm going to give to you.
So those 20 lines here, you include the lcbp.
You create the class and then you have the main where you initialize the rows to communication.
You create a node, you make the node spin and then you shut down.
Okay.
Now let's come back to this code.
And I'm just going to add like I did with Python, I'm going to add one more functionality which is
a timer okay.
Because the timer is going to be super useful if you want to have a node that repeats an action, let's
say ten times per second or 20 times per second.
So it could be a sensor reading some data, it could be a controller or it could be anything.
And that's very frequent to use this kind of structure in robotics.
So we're going to see how to create a timer.
And the syntax of course with C plus plus is always a bit more complicated than Python.
So first of all I will create here an so private attribute or clcp.
And we have Timer base.
That's going to be a timer.
And as I told you, everything we're going to do will use shared pointers.
So we have another thing that is called shared as you can see shared Peter okay.
That's kind of a helper.
You see that's so here it's written a shared pointer.
Basically it's going to create an STD shared pointer of this class here of this object okay.
So that's a helper that you can use instead of writing STD shared pointer.
So we create a timer here timer as a private attribute.
And then okay before I create the timer I'm going to create the callback.
So let's do void timer callback I just create a function here a method.
And let's just do Lclc-rp info with this gets longer.
You can see how Autocompletion is very useful.
And then.
Hello.
Okay, so code is valid.
And in the constructor.
So after I create the log I'm also going to create a timer.
So I'm going to use this timer and say that the timer is equal to.
And we have a function inside node that is.
So I'm going to use the this keyword.
Once again you don't have to okay.
It's just for me to make it super explicit that when I use this it's about the node class.
Create wall timer okay.
And you can see it is recognized.
So you can use the Autocompletion.
And so here we need you see we need to give a STD chrono duration.
So for example I'm going to use STD and then Chrono.
And then I can use seconds because I just want one second which is going to be an integer here.
Okay.
So first that's going to be the period.
So every one second and then I need to give the callback.
I'm going to go back to a new line here and align that here.
Okay.
And how to link the callback.
Well we need to use the std bind function.
What do we need to do inside the Stdbind function.
We need to give the reference to the callback with this symbol.
So the ampersand and then my node.
That's the class name plus timer callback.
That's the name of the function without the parentheses.
Okay, we are not calling the function, we are just referencing it.
So I need to do this.
And then I need to bind with this.
So this is this class this object that I'm binding to.
Okay.
And let's not forget the semicolon.
So this stdbind, I know the first time that you use that, it's a bit confusing.
Everything you need to write inside, but well, once again, as you get used to it, it's always the
same thing.
And to complete this example, right now I'm just going to create a counter here.
So let's do encounter same thing.
I use a trailing underscore when I create an attribute in a class.
It's just a convention that I follow.
So I create a counter.
I initialize it here to zero.
And then I can use it in this method.
So hello.
And then percentage D counter.
So you see we just create a string like you would do with printf or something like that.
So percentage d and then colon and you put the variable and let's not forget to do counter plus plus
to add one to the counter every time.
And that looks good.
So let's do a quick recap.
We have created our class here okay.
So when we start our program what's going to happen.
First we're going to initialize Ros2 communications.
Great.
Then we are going to create a shared pointer to my node.
So we're going to create an object my node of the minute class.
When we create the object the constructor is going to be called.
So we call the parent constructor.
We initialize the node with a name.
And then we print hello world.
And we register the timer callback here with a world timer.
So we create a timer.
And the timer says that we need to call this function every one second.
So still nothing is happening with this timer.
Then we exit from the constructor.
We go to this line and we make the node spin.
When the node spins it's going to be.
So the execution is going to be stopped.
Here the node will be kept alive.
And all the callbacks that have been registered can now be called.
And because we have created a timer.
The timer says every one second we need to call the timer callback.
So while the node is spinning, this timer callback will be called every one second.
Then when we press Ctrl C that's going to stop the spinning.
And then we shut down.
We exit and that's it.
So let's save this file.
Make sure that you save this file.
There is nothing else to do in the cmakelists.txt.
Okay.
It's already been done previously.
And let's go back to the terminal and let's do again build.
And then source and then run.
And you see we have hello 01234 etc. and I press Ctrl C and that's it.
Great.
So you have successfully created a C plus plus node with object oriented programming.
And you also have seen how to create your first timer with a callback.