Now that you know what a node is, let's create a Python node in our Python package.
So we will first write a very minimal code.
Here we will install and run the node.
And then in the next lesson we will use object oriented programming to improve the node.
And from that we will have a code template that we can use for any future node that you create.
So let's get started.
First let's go back to our workspace in the source folder here.
And then in the package.
So my Python package okay what do we have here.
Well we have several files and folders.
And if you remember we have a folder that has the same name as the package.
So we go inside that one.
So now you see we are in my pi pkg inside my pi pkg inside the source folder of the ros2 workspace.
And we have here the Init.py.
So we will create a new file right here.
Let's do touch.
And let's name my first node.
Dot p y.
Great.
So now we have the file that we can edit.
And I'm going to use VSCode.
So to open VSCode you see I have closed it previously I'm going to go back to the source folder of my
workspace.
Okay.
And then I'm going to do code dot.
So I'm going to open VSCode from that folder only.
Great.
And we can find here the file is going to be here.
So let's start to write the code.
First we will add the interpreter line for Python.
So like that user bin and and then Python three.
Make sure you don't make any typo here okay.
So this line is going to say that we use Python three just in case.
For example you make an executable and you run it like that.
It's going to use Python three to interpret and execute the code.
And then the first thing we will do is to import the RCL py library.
Okay.
So that's basically our CLP is the Ros2 library for Python.
You will need that in all your Python nodes.
And as you can see here I have the auto completion.
I can press tab for the auto completion.
If you don't have this in VSCode, it's because you haven't correctly set up the extension.
So you go back to the extensions here Ros extension and you make sure it's correctly installed.
So the Ros extension from Microsoft you might need to restart VSCode okay.
Make sure it's installed.
And then you should have the auto completion.
Great.
So we import our CLP.
And then we also import from our CLP dot node import node.
So we import the node class.
So n uppercase.
Here we import the node class from CLP node okay.
That's all the imports that we need.
For now let's create a main function.
So I create a div main and let's put some optional arguments that we can pass to the main function.
And let's name this args.
Okay.
And what do we do inside this main function.
So what do we do inside the Ros2 program.
The first thing that you will do the first line is going to be rcl py dot init.
And you can pass arguments.
So args is equal to x.
So it has a args argument okay.
And you can give the arguments that you get from the main.
So that's the first thing you will do in any ros2 programs you need to initialize Ros2.
So it's going to initialize Ros2 communications and all the stuff that you need.
Then to create and use nodes.
Then we're going to do some stuff.
And the last line that you will write is RCL pi dot shutdown okay.
And you see here again I use the auto completion.
So use the auto completion as much as possible.
I really recommend you to use something.
So if you don't use VSCode, use something else that has auto completion for Ros2.
That's going to be super useful.
Great.
So CLP init and then at the end CLP dot shutdown.
Now that we have this we can create a node.
So let's do node is equal to node.
So we use the node class here okay.
And we need to give it a name.
So for example let's name it Pi test.
That's going to be our first node.
Now you can note one thing is that we actually create a node inside the Python file okay.
So the Python file itself is not really the node.
The node is inside the file.
All right.
And that's the minimum code for a node.
So you just create a node.
You initialize Ros2 communication.
You create a node and you shut down.
Now that's not going to do anything if we test it.
So let's add a log here.
Let's do node dot.
So you see that inside the node.
Thanks to the all the functionalities from the node class from our CLP we can do node dot get logger.
So we get the logger functionality and then.info for example with let's say hello world.
That's going to be our Hello World program.
And now we have a node that will well be initialized and then print something on the terminal.
So that's it I'm just going to add here if name.
So double underscore name is equal to and then double underscore main double underscore.
And let's call the main function.
Okay.
That's just useful.
If you want to run the program directly from the terminal then that's going to call the main function
automatically for you.
Okay.
And that's what we're going to do right now.
So I add this bit of code.
Now it's very important that you save the files.
Okay, I see lots of errors of people starting with Ros2 and they forget to save the file.
As you can see here we have a dot, a big dot.
If I save with Ctrl s now it's a cross.
Okay, so that's an easy way to see if you have saved the files or not on VSCode.
So make sure you save that file.
Then let's go back to the terminal.
And let's go back to where the file was.
My Python package okay.
And what you can run it with the Python three command directly or make it executable.
For example with chmod plus x my first node you see now it is executable and I can run my first node
directly.
So let's see what we have.
I press enter and you see we have hello world.
So it correctly worked.
You see we have info that corresponds to the info log level.
Here then we have a timestamp and then we have the name of the node.
So pi test.
Why pi test?
Because that's what we have defined here.
Okay.
When we create the node we give it a name.
All right.
So that's what you see.
But what you see also is if I run it again we print the log and then we exit from the program.
And that's quite normal because here we just initialize Ross to communication.
We create a node, we print something and then we shut down everything.
Now a quite common thing to do with a node is to actually keep it alive so it can process more stuff.
Okay.
And you just want to keep it alive and stop it when you want.
How to do this?
Well, after we create the node before we shutdown, of course we are going to do RCL pi dot spin.
And as a parameter we are going to provide the node that we have just created okay.
So we create the node and then we pass it to the spin functionality and the spin.
So spin is very important mechanism in Ros.
And we're going to come back to this a lot during this course.
But for now you just have to know that the spin is going to keep the node alive until you press Ctrl
C.
So let's save that and let's start again.
And you can see that now we have Hello world.
But the node is still hanging there okay.
If I press enter the node is still hanging there.
If I press Ctrl c.
Now we exit from the node.
So everything is going to be shut down.
The node is going to be shut down and we exit from the program.
All right.
So I'm going to keep this like this here.
And I'm going to explain more about it a bit later.
Now what we have done so far is we have a file that's a valid file.
We have run the file from where we created it directly.
But now what we want to do actually when we create a new file like this is we want to install it, we
want to install it and create an executable that we can start then with the ros2.
Command line.
And that's going to be super useful.
For example, later we can also start it from a launch file, etc..
So even if you can run the Python program like this directly from the source folder here of the workspace,
we're going to install the node and how to install the node.
Well you will just go inside your Python package to the setup.py inside this file.
Here you see lots of stuff.
But at the end you see entrypoints with console scripts.
And that's an array.
That's a list here.
So you go inside this, you open double quotes and we are going to give the instructions to create a
new executable.
So I'm going to create it.
We're going to run it.
And then I'm going to come back and explain a bit more about it.
So let's create an executable which name is py node.
So that is something that I choose.
Then I will put a space equal space.
And then I need to provide basically the path.
So the relative path to the file here.
So we need to go to my pi pkg.
So usually there's going to be the package name.
So package name and then dot the file name here it's my first node dot pi my first node.
But I don't provide the pi extension okay.
So just my first node okay.
Package dot file without the extension.
And then we have a colon.
And we need to provide the name of the function that we want to call.
So when we start this we are going to call a function from that program what function we want to call.
Well that's going to be the main function.
So we write main just like that.
No parentheses nothing okay.
So you choose a name for the executable and then package dot file name colon function to call.
And that's it.
If you want to add more executables you will need to add a comma.
Go to a new line and then do the same thing again.
Okay, but for now we only have one.
So let's save this file.
Very important.
You save all the files.
Let's go back to the terminal and let's go to.
So let's go to the Ros2 workspace.
Once again.
Very important.
You don't build just anywhere.
You only built in the Ros2 workspace folder.
That's super, super important.
So we are here I can do.
So let's do clear here.
And I can do call on build now because I have two packages and I only want to build the Python one.
I'm going to use packages select.
So it's not really important.
You could build the two packages.
But here let's do things in a clean way.
So packages select and then my pi pkg okay.
So we build the Python package.
And now what's going to happen is that you just gave an instruction to create an executable.
So inside the install folder here.
Well you can try to browse inside and maybe find the executable.
It doesn't really matter.
But basically you know that this executable here pi node is going to be created inside the install folder.
And it doesn't really matter where because we don't need to go inside this.
We can use the Ros2 command line tool directly to run the executable.
Okay.
So for example let's say I'm in my home directory.
Well you could do that from wherever.
And I'm still going to here.
I'm still going to source the bashrc.
So you could do complete path with tilde slash dot bash RC.
Why is that?
Because well, I have just created something new in my workspace and because I have created something
new, I'm going to source my workspace.
Okay.
Just to be sure that I have the latest updates and how to source the workspace.
Well, you could source the setup bash script inside the install folder, but you can also source the
bashrc because, well, we already have the line to do this inside the bashrc.
Okay.
So just make sure that you choose the workspace if you have created something new.
Okay.
In a terminal.
Or you could open a new terminal and then everything is done for you automatically.
So for example we source that and then I can start the node with Ros2 run.
I will need to first provide the package name.
The package name is my pi pkg.
You see I can press the auto completion.
It should be found.
If it's not found here then you have a problem already and you need to go back and make sure that you've
done everything and then well.
So I do roster package name.
And then I need to provide the executable name.
What is the executable name.
Well once again the executable name is what we have created here in the setup.py.
So it's called here pi node.
So pi node.
And I don't even need to type everything.
I can press Tab here.
Okay.
It's going to be found.
I press enter and you can see it is currently working.
So now I have created a node.
I have installed it and I can run it from anywhere using the Ros to run command line tool.
And now I can do ctrl C to stop.
So we have a keyboard interrupt.
But you don't need to really worry about that.
Okay.
And I'm just going to come back to one very important thing that you need to make sure to understand
is that.
So we have the executable name here.
The executable name is what's created after you install the node here.
So let's pick node here.
But then we also have pytest.
Pytest is the node name.
And the node name is what you have defined here inside the code.
Okay.
So that's two different things.
The executable name here and the node name are different things.
And then also we have my first node.py.
That's the file name.
Okay.
So we have three different things here.
We have the node name that we create inside the file.
We have the file name.
And then we have the executable name that we create so that we can use with Ros to run.
Okay three different things.
And here I used different names for all three just to show you that there are different things.
But sometimes you will see the same.
For example, if you create a temperature sensor file, you might name it temperature sensor dot p y
and then the node name might be temperature sensor.
And the executable name also might be temperature sensor.
But you need to be aware that those are three different things okay.
And I'm going to come back to this also later.
But for now well, great.
You've made a lot of progress already because you can create a file here.
You can create a file to write a node.
You can write a basic version of a node.
You can install that node and run that node from the terminal.