In this lesson you will learn how to create a Cplusplus node.
You will write the node, compile it and run it, and we're going to follow the same structure.
As for the Python node, we're going to first write a very minimal code.
And then in the next lesson you will have the object oriented programming way of writing the code,
plus the full template that you can use for any future node that you create.
And let's get started.
So we are going to go first inside our roster workspace and then the source directory and then the my
CPP package.
So inside this package we have a include and a source folder.
We are going to go inside the source folder of that package.
And here we have nothing.
So let's create a file.
Touch my first node dot cpp.
So we use the CPP extension.
All right.
So that's where you will create your cplusplus files inside the source folder of the package.
And now let's edit that with VSCode.
So I'm going to go back again here to the source folder of the workspace called Dot.
And let's find the file which is going to be right here in the my CP package.
And you see I have my first node CP.
So let's start with the includes.
And we only have one include here.
So we're going to do include and then RCL CP slash RCL CP dot HP.
And you can see here again I have the auto completion.
If you don't have the auto completion you need to go back to the extensions here.
Make sure you have installed the Ros extension by Microsoft and then maybe start the VSCode again.
Okay, so include RCL, CP and then RCL, cp, HP.
At this point I would also suggest to already save the file.
Okay, you see, we have an error and then it's recognized again.
So save the file because then if you use the clcp functionalities later, if you haven't saved the file
maybe you will have some issues with the autocompletion.
So save the file after you include something in Cplusplus.
I would recommend to do that.
And then well, what do we need to do in a Cplusplus program?
We need to create a main function.
So let's just create one with arg c.
So number of arguments and then this for the arguments okay.
So there's nothing special here.
It's just a standard main structure for C plus plus.
And what do we do inside the Ros2 program.
Well first we initialize ros2 communication with our clcp then colon colon init okay.
So inside the clcp library we have the init function just like we had in Python.
And here we can pass the arguments of the main.
So argc and argv.
So that's the first thing we will do every time we initialize ros2 communications.
Then we're going to do some stuff.
And the last thing we will do is our clcp shut down.
Okay.
Just like we did in Python.
But of course with the C plus plus syntax and after shutdown.
Well because you see it's a int.
So we need to return an integer.
Let's return zero.
Okay.
So that's the first thing you will do in a C plus plus program.
And that's the last thing you will do in the middle.
We can create nodes and do all sorts of stuff.
But first you initialize Ros2.
And then at the end you shut down Ros2 and you return.
Now let's create a node.
So what type we will give.
Let's just use the auto type.
So auto node is equal to.
And we are not just going to create a node.
We are going to actually create a pointer to a node.
and in Ros2 you will see that we use pointers for everything and actually not just pointers.
We're going to use smart pointers and in this case a shared pointer.
And if you don't know anything about smart pointers, well maybe just find a quick tutorial online to
get a few explanations.
But it's not that hard.
It's just going to take care of allocating memory and destroying the memory for you, so you don't need
to worry about that.
And in Ros two everything is using those pointers.
So at the beginning it might be a bit complicated to understand, but as you get used to it it's always
the same thing.
So let's do STD make shared to create a pointer of an object and what is going to be the object rcl
cpp node.
Okay, so we get the node class from rcl cpp directly okay.
So here with this we open and close the parentheses.
With this we create a node object.
And actually here we create a shared pointer to a node object.
So you can see now the auto is actually a shared pointer of an lclc-rp node.
Okay.
So that's how you create a node in Ros2 with C plus plus.
And then well we can do the same thing as we did with Python.
We can also make the node print something because this is the minimum code.
But well it's not going to do anything.
So let's do node.
And because this is a pointer we need to use an arrow.
Okay.
If you just use a dot you're going to interact with the shared pointer.
If you use an arrow you interact with the object inside the shared pointer.
And we have a get logger functionality and actually to print something.
So to use a log we are going to use the RCL cpp info like that.
And then we're going to put node get logger as the first argument.
And then the second argument is going to be the log itself.
So hello, world.
And you can construct a string here just like that.
Let's not forget the semicolon.
All right.
So lclc-rp underscore info all uppercase.
And then you do node.
So the node you've created getlogger.
And then the log you want to print okay let's save that.
And now how to test it.
So with Python we could already test it by just making the file executable and running the file as Python
was an interpreted language.
But C plus plus you cannot do that.
You need to build the code anyway.
And how to build the code.
How to create an executable.
Well, this time here we will go to the Cmakelists.txt and well I don't want this.
And you see I have already the syntax highlighting.
If you don't have this, make sure you have the CMake extension here by Twx.
Okay.
That's the one that I'm using here for this course.
So let's go back to Cmakelists.txt.
And we're going to write here the rules to create executables for Cplusplus.
But first let's do a bit of cleanup.
So you see we have here the minimum required version for CMake.
Some stuff for the compiler.
We find some dependencies okay.
So we have a CMake that's going to be well because this is a C plus plus package.
And then we have RCL cpp you see.
So if you add a new dependency in a C plus plus package you will have to add the dependency here.
Okay.
But if you need the dependency to compile some stuff, you will also need to find the package here.
And then we have this.
If build testing with some testing stuff that we actually don't need right now.
So usually if I don't need that I'm just removing it okay.
It's a bit cleaner.
And we finish the CMake list.txt with payment package.
So that should be the last line.
So make sure you don't remove that one.
And now how to add an executable.
Well after you find the dependencies parentheses and before the event package line we will add.
So add executable and you need to choose a name for your executable.
So for example cpp node.
Okay this is whatever you want.
Then we need to provide the path to the file.
Okay.
So here from from there the path is to go to the source folder.
And then my first node cpp.
So we write src my first node dot cpp.
So with this we create an executable.
But we also need to add the dependencies for that executable with the payment target dependencies.
Dependencies like that.
Okay.
So add executable is a command cmake function but that's specific to A-mount and Ros2 okay.
So aim and target dependencies.
The first argument is going to be the executable name.
So cpp node.
And then you will add all the dependencies that you need to link.
So in this case you see in our code we are using rcl cpp.
So I'm going to add Lclc-rp which is also found here we find package rcl cpp.
If you don't add this then you will get an error at compilation saying that Lclc-rp is not found okay
because you haven't used the payment target dependencies.
Great.
And this is going to create an executable, but it's not going to install it.
So we need to add an install block okay.
And in this you can write for example here.
So let's write target like that uppercase.
And then we're going to add I'm just adding two spaces here cpp node.
That's the executable name.
Same here.
And then destination.
All uppercase.
We're going to use lib slash.
And then you use dollar sign curly brackets like that.
And project underscore name.
And make sure you don't make any typo here.
It's dollar sign curly brackets project name okay.
Because I see lots of people making typos here.
And then it doesn't work well because there is a typo okay.
So this is going to create an executable with the dependencies.
And this is going to install the executable in somewhere in the lib folder.
So that's going to be in the install folder of the workspace.
Somewhere in the package there's going to be a lib folder.
And that's where we install the C plus plus executables.
And note that this you just need to do it once because then for any new executable that you want to
add from this package, you basically need to duplicate those two lines.
And then you just need to add the new executable name in a new line just here.
Okay, so let's save that.
Make sure you save everything.
All the files.
Let's go back to the terminal.
Let's go back to the Ros2 workspace once again.
It's super important that you build from that folder and not anywhere else.
And let's do a quick build.
So we could build both packages because we have two packages.
If you want you can just build with packages select.
We can just build my CP pkg and let's use Autocompletion for everything because that makes it faster.
And we don't have errors when we type stuff.
All right.
And you can see well if you have an cplusplus error that's what it's going to look like.
So sometimes it can be explicit or not.
But I need to go back to my file.
And you see well actually I need to give a name here.
So let's do CP test.
All right.
So you can see this kind of error is what you're going to get.
So this is a here this is a typical classic C plus plus error that you see that when you build the package
that's going to build the file.
And that's when you have errors.
Okay.
So in this case we go back to the file here.
When I create a node I need to pass the node name of course as a parameter.
So I save again and then I build again.
And you can see now successful.
So you see this time here we have finished.
Last time we had failed okay.
So that's how you know that the package has been built.
You see this finished line.
Great.
So what happened.
What happened is that we have this file that is used here to create an executable.
This executable is named CP node and is going to be installed somewhere inside the install folder of
our workspace, so you can try to browse inside and find the file if you want.
But it doesn't really matter because we are going to use Ros2 right now.
So let's source the workspace.
Let's do source install setup bash to make sure that we source the workspace, because we have added
something in the terminal that's already open.
And then let's do Ros2 so we can do it from here, from the home directory from anywhere.
So let's do clear and Ros2 run my CP, let's press tab cp package.
And then what was the name CP just in case you press tab is going to be found.
Okay CP node let's press enter.
And you see we have hello world.
So we have the info log.
We have the timestamp.
That's the name of the node okay.
And then the log.
And once again here you see we have.
So the name of the file is my first node DHCP.
Okay, that's one thing.
The second thing is the name of the node here.
It's CP test.
And the third thing is the name of the executable which is CP node.
So you see those are three different things that sometimes you're going to use the same for all three.
But they are three different things.
And you can see it here.
When we do raster run we actually use the executable name that we have created in the Cmakelists.txt.
And then we see CP test in the log, because that corresponds to the node name that is defined from
within the code.
All right.
And now well let's just finish this with a spin because you see once again I run this and that's it.
It's going to start print something and then shut down.
That's exactly what we asked it to do.
But now let's say we want to keep the node alive okay.
So the node can stay alive as long as we want until we press Ctrl C so we can do this right here just
before shutdown.
We do lclc-rp and then spin and we pass the node.
You see, we need to give a shared pointer to a node.
So we pass the node as a parameter.
And that's basically it.
So now I save and I need to build again okay.
So I go back to my Ros2 workspace.
And then you need to remember that every time that we change the code we're going to do build source
and run.
So let's find the command to build.
It's here.
So I have built now I will source and then I will run okay.
And this time you see that we are hanging here because the node is spinning.
Now I press Ctrl C and you see the node is shut down.