To create a raster node, you first need a package.
Packages will allow you to separate your code into reusable blocks.
Each package is an independent unit.
For example, in one application you could have one package that will handle a camera, another package
that will run the wheels of your robot, and yet another one that will handle motion planning for the
robot in the environment.
So we will first create a simple Python package here, and you will learn more about packages and best
practices all along this course.
Note that in Ros2, Python packages and Cplusplus packages have a complete different architecture.
So where will we create our Python package?
Well, that's going to be inside our Ros2 workspace.
So you go inside the Ros2 workspace and you go also in the source folder okay.
Make sure that you create all the files inside this src folder.
It's very important.
So now this is empty of course.
And to create a package we are going to use the Ros two command line.
Okay.
I'm going to come back to this a lot.
During this course we're going to explore a lot of different commands you can use with Ros two.
So Ros two and then space.
And we can use the PKG command to create a package with the create keyword.
So Ros two pkg create.
Then we need to give a name for the package.
So for example if your package would be monitoring a temperature sensor it could be a temperature sensor
package.
Here I'm just going to name it my pi pkg.
So my Python package okay.
As a first example for this course, make sure that you use underscores to separate words.
If you want inside the name of the package after the name, you will need to precise.
If the package is a Python package or a C plus plus package for example.
So you can do this with build type okay.
Like this.
So dash dash build dash type.
And you can use Autocompletion.
As you can see it should work.
And then you provide the build type.
And we're going to write element underscore Python.
So what is element.
Element is the build system.
So we have call con that is the build tool.
And element is the build system.
You're going to see quite a few times in different files.
And to say that the package is a Python package you need to write element underscore Python.
And that's pretty much it for the package.
Now what you can do is you can also add dependencies.
So dependencies like this which are basically other packages that this package will depend on.
So of course we will be able to add all the dependencies later directly inside the package, but you
can also provide them already from the start.
So here I'm going to add one rcl py and rcl.
Py is the Python library.
So you can write Python code to use the ros2 features.
So of course if we're going to write Python nodes We want to use the Python ros2 library.
So now that we have all that let's press enter.
That's going to create a package.
You can see we have lots of logs with creating folders, creating files etc. and we also have.
So you might have this warning.
If you have this warning it's really not important.
It's just to say that you don't have a license file.
So having a license file is going to be important.
If you want to publish your code with an open source license somewhere on GitHub, for example, if
you want other people to use, etc..
But for now, for learning and for most projects you will do, you don't really need to publish your
code online, so we don't really need a license.
So you can just ignore completely this warning.
So let's do a clear.
And now if I do ls you see I have my pi pkg.
So that's my new package.
And let's see what we have inside.
To do this I'm going to open Visual Studio Code and to open Visual Studio Code, I recommend that you
always do the following.
So first you navigate inside your source folder here of the roster workspace, not anywhere else.
And then you do code.
That's the command to launch VSCode and then dot to say you want to open VSCode from the source folder.
I recommend you do that because if you open VSCode from different parts every time, that might be a
problem, especially for Autocompletion.
Okay, because VSCode might not find the includes and the imports, etc. so better to open it from the
source folder.
That's what I'm doing here.
Code space dot.
Okay.
And you can see that I have my source folder.
So we have a new VSCode folder.
We're going to come back to this later.
That's just stuff for VSCode.
And then we have well this package that we have created my Python package.
And what do we have inside?
You see, we have quite a lot of stuff.
First of all, you will find another folder that has exactly the same name as the package.
So if your package was named ABC, that folder would be named ABC.
And inside you have an empty init.py.
Okay, so this folder here is where you will write your Python code.
Then we have a resource folder.
Don't really need to worry about that.
A test folder with test stuff.
But also we don't need to worry about that right now.
And then we have three more files.
So we have a package dot XML.
This file is quite important and it's mandatory for any ros2 package okay.
And what do you have inside.
Well as you can see we have stuff like name version description.
You can put a maintenance email or license.
So if you're going to publish your code somewhere on GitHub for example, you will need to fill this
with accurate information.
Then you see we have a depend tag with RCL.
Py so that's because we have provided the CLP dependency in the command line when we created the package.
Now if you want to add more dependencies.
So if this package here that you've created depends on more other packages, you will need to add other
dependency tag.
So one tag per line we have more stuff here.
And in the end you also see the build type here amount Python.
All right.
So that's about it for the package dot XML for now.
And then you have a setup.cfg and setup.py that's going to be used when we want to install the nodes.
We're going to come back to that when we create a node okay.
So that's what you can find in a Python package.
Now let's go back to the terminal.
And we can build this Python package.
Even if it has no node we can build it.
So let's go back to Ros2 workspace okay.
You always go back to this Ros2 workspace.
You don't build inside the source folder you build inside the Ros2 workspace.
Once we are here, we can do a call on build and you can see we have starting my Python package and
then finished my Python package.
So that package has been built.
And I'm just going to show you one option here.
So you can use call build with packages select and you can press tab.
Here you should have the Autocompletion and then my Python package with Autocompletion okay.
So if I do this well you will see the result is going to be the same because we only have one package.
But that option is quite convenient to use.
It's only going to build the packages that you actually select.
So the packages that you write after this option.
All right.
So if you just want to build that package, if you have let's say 12 packages and you just want to build
that one, you can use this option as well.
All right.
And now your Python package is ready to host a Python node.