WEBVTT

00:00.050 --> 00:05.990
Now that you know what a node is, let's create a Python node in our Python package.

00:05.990 --> 00:08.510
So we will first write a very minimal code.

00:08.510 --> 00:11.030
Here we will install and run the node.

00:11.030 --> 00:16.160
And then in the next lesson we will use object oriented programming to improve the node.

00:16.160 --> 00:21.620
And from that we will have a code template that we can use for any future node that you create.

00:21.620 --> 00:22.940
So let's get started.

00:22.940 --> 00:28.730
First let's go back to our workspace in the source folder here.

00:28.730 --> 00:31.190
And then in the package.

00:31.610 --> 00:35.870
So my Python package okay what do we have here.

00:35.900 --> 00:37.550
Well we have several files and folders.

00:37.550 --> 00:42.380
And if you remember we have a folder that has the same name as the package.

00:42.380 --> 00:44.840
So we go inside that one.

00:45.350 --> 00:56.000
So now you see we are in my pi pkg inside my pi pkg inside the source folder of the ros2 workspace.

00:56.000 --> 00:58.640
And we have here the Init.py.

00:58.670 --> 01:01.530
So we will create a new file right here.

01:01.860 --> 01:02.610
Let's do touch.

01:02.610 --> 01:05.610
And let's name my first node.

01:05.640 --> 01:07.170
Dot p y.

01:07.950 --> 01:08.220
Great.

01:08.220 --> 01:10.350
So now we have the file that we can edit.

01:10.350 --> 01:11.880
And I'm going to use VSCode.

01:11.880 --> 01:19.530
So to open VSCode you see I have closed it previously I'm going to go back to the source folder of my

01:19.530 --> 01:20.280
workspace.

01:20.310 --> 01:20.850
Okay.

01:20.850 --> 01:23.220
And then I'm going to do code dot.

01:23.220 --> 01:26.400
So I'm going to open VSCode from that folder only.

01:28.800 --> 01:29.400
Great.

01:29.400 --> 01:33.660
And we can find here the file is going to be here.

01:33.660 --> 01:35.490
So let's start to write the code.

01:35.490 --> 01:39.180
First we will add the interpreter line for Python.

01:39.210 --> 01:47.460
So like that user bin and and then Python three.

01:47.490 --> 01:49.530
Make sure you don't make any typo here okay.

01:49.560 --> 01:52.950
So this line is going to say that we use Python three just in case.

01:52.980 --> 01:56.100
For example you make an executable and you run it like that.

01:56.130 --> 02:00.270
It's going to use Python three to interpret and execute the code.

02:00.270 --> 02:06.540
And then the first thing we will do is to import the RCL py library.

02:06.570 --> 02:06.780
Okay.

02:06.810 --> 02:10.560
So that's basically our CLP is the Ros2 library for Python.

02:10.560 --> 02:13.080
You will need that in all your Python nodes.

02:13.080 --> 02:17.910
And as you can see here I have the auto completion.

02:18.240 --> 02:20.430
I can press tab for the auto completion.

02:20.430 --> 02:25.350
If you don't have this in VSCode, it's because you haven't correctly set up the extension.

02:25.350 --> 02:30.780
So you go back to the extensions here Ros extension and you make sure it's correctly installed.

02:30.780 --> 02:35.730
So the Ros extension from Microsoft you might need to restart VSCode okay.

02:35.760 --> 02:37.260
Make sure it's installed.

02:37.260 --> 02:38.970
And then you should have the auto completion.

02:38.970 --> 02:39.330
Great.

02:39.330 --> 02:40.980
So we import our CLP.

02:40.980 --> 02:49.170
And then we also import from our CLP dot node import node.

02:49.170 --> 02:50.790
So we import the node class.

02:50.790 --> 02:52.290
So n uppercase.

02:52.290 --> 02:56.550
Here we import the node class from CLP node okay.

02:56.580 --> 02:58.050
That's all the imports that we need.

02:58.080 --> 03:02.680
For now let's create a main function.

03:02.710 --> 03:09.370
So I create a div main and let's put some optional arguments that we can pass to the main function.

03:09.370 --> 03:11.320
And let's name this args.

03:11.500 --> 03:12.280
Okay.

03:12.400 --> 03:14.890
And what do we do inside this main function.

03:14.890 --> 03:17.860
So what do we do inside the Ros2 program.

03:17.890 --> 03:27.730
The first thing that you will do the first line is going to be rcl py dot init.

03:27.970 --> 03:29.890
And you can pass arguments.

03:29.890 --> 03:32.590
So args is equal to x.

03:32.590 --> 03:34.870
So it has a args argument okay.

03:34.900 --> 03:37.000
And you can give the arguments that you get from the main.

03:37.000 --> 03:43.120
So that's the first thing you will do in any ros2 programs you need to initialize Ros2.

03:43.150 --> 03:47.620
So it's going to initialize Ros2 communications and all the stuff that you need.

03:47.650 --> 03:50.350
Then to create and use nodes.

03:50.350 --> 03:52.030
Then we're going to do some stuff.

03:52.030 --> 03:59.230
And the last line that you will write is RCL pi dot shutdown okay.

03:59.260 --> 04:02.990
And you see here again I use the auto completion.

04:02.990 --> 04:05.690
So use the auto completion as much as possible.

04:05.690 --> 04:08.000
I really recommend you to use something.

04:08.000 --> 04:12.710
So if you don't use VSCode, use something else that has auto completion for Ros2.

04:12.740 --> 04:14.360
That's going to be super useful.

04:14.390 --> 04:14.720
Great.

04:14.720 --> 04:19.460
So CLP init and then at the end CLP dot shutdown.

04:19.460 --> 04:22.940
Now that we have this we can create a node.

04:22.940 --> 04:26.240
So let's do node is equal to node.

04:26.240 --> 04:29.420
So we use the node class here okay.

04:29.450 --> 04:31.250
And we need to give it a name.

04:31.250 --> 04:33.800
So for example let's name it Pi test.

04:33.800 --> 04:35.270
That's going to be our first node.

04:35.300 --> 04:42.170
Now you can note one thing is that we actually create a node inside the Python file okay.

04:42.170 --> 04:45.290
So the Python file itself is not really the node.

04:45.290 --> 04:47.870
The node is inside the file.

04:47.900 --> 04:48.410
All right.

04:48.410 --> 04:52.280
And that's the minimum code for a node.

04:52.280 --> 04:53.660
So you just create a node.

04:53.660 --> 04:55.070
You initialize Ros2 communication.

04:55.070 --> 04:56.600
You create a node and you shut down.

04:56.600 --> 04:59.060
Now that's not going to do anything if we test it.

04:59.060 --> 05:01.820
So let's add a log here.

05:01.850 --> 05:03.620
Let's do node dot.

05:03.650 --> 05:05.810
So you see that inside the node.

05:05.840 --> 05:13.580
Thanks to the all the functionalities from the node class from our CLP we can do node dot get logger.

05:13.580 --> 05:22.430
So we get the logger functionality and then.info for example with let's say hello world.

05:22.460 --> 05:24.710
That's going to be our Hello World program.

05:24.710 --> 05:31.910
And now we have a node that will well be initialized and then print something on the terminal.

05:32.210 --> 05:36.260
So that's it I'm just going to add here if name.

05:36.260 --> 05:44.480
So double underscore name is equal to and then double underscore main double underscore.

05:44.660 --> 05:47.030
And let's call the main function.

05:47.060 --> 05:47.270
Okay.

05:47.300 --> 05:48.590
That's just useful.

05:48.590 --> 05:53.420
If you want to run the program directly from the terminal then that's going to call the main function

05:53.420 --> 05:54.920
automatically for you.

05:54.950 --> 05:55.310
Okay.

05:55.340 --> 05:56.690
And that's what we're going to do right now.

05:56.690 --> 05:58.700
So I add this bit of code.

05:58.740 --> 06:02.940
Now it's very important that you save the files.

06:02.970 --> 06:08.520
Okay, I see lots of errors of people starting with Ros2 and they forget to save the file.

06:08.520 --> 06:12.300
As you can see here we have a dot, a big dot.

06:12.330 --> 06:15.480
If I save with Ctrl s now it's a cross.

06:15.510 --> 06:20.430
Okay, so that's an easy way to see if you have saved the files or not on VSCode.

06:20.430 --> 06:22.470
So make sure you save that file.

06:22.470 --> 06:24.120
Then let's go back to the terminal.

06:24.120 --> 06:27.390
And let's go back to where the file was.

06:28.440 --> 06:31.020
My Python package okay.

06:31.020 --> 06:37.470
And what you can run it with the Python three command directly or make it executable.

06:37.470 --> 06:47.910
For example with chmod plus x my first node you see now it is executable and I can run my first node

06:47.910 --> 06:48.630
directly.

06:48.630 --> 06:50.130
So let's see what we have.

06:50.160 --> 06:53.430
I press enter and you see we have hello world.

06:53.430 --> 06:54.840
So it correctly worked.

06:55.530 --> 06:59.440
You see we have info that corresponds to the info log level.

06:59.470 --> 07:04.750
Here then we have a timestamp and then we have the name of the node.

07:04.750 --> 07:06.250
So pi test.

07:06.280 --> 07:07.180
Why pi test?

07:07.180 --> 07:09.460
Because that's what we have defined here.

07:09.490 --> 07:10.120
Okay.

07:10.210 --> 07:12.970
When we create the node we give it a name.

07:13.000 --> 07:13.330
All right.

07:13.360 --> 07:14.410
So that's what you see.

07:14.410 --> 07:21.190
But what you see also is if I run it again we print the log and then we exit from the program.

07:21.190 --> 07:25.210
And that's quite normal because here we just initialize Ross to communication.

07:25.300 --> 07:29.350
We create a node, we print something and then we shut down everything.

07:29.350 --> 07:35.890
Now a quite common thing to do with a node is to actually keep it alive so it can process more stuff.

07:35.920 --> 07:36.160
Okay.

07:36.190 --> 07:39.940
And you just want to keep it alive and stop it when you want.

07:39.970 --> 07:40.960
How to do this?

07:40.960 --> 07:50.140
Well, after we create the node before we shutdown, of course we are going to do RCL pi dot spin.

07:50.170 --> 07:56.260
And as a parameter we are going to provide the node that we have just created okay.

07:56.260 --> 08:01.760
So we create the node and then we pass it to the spin functionality and the spin.

08:01.760 --> 08:04.400
So spin is very important mechanism in Ros.

08:04.400 --> 08:06.950
And we're going to come back to this a lot during this course.

08:06.950 --> 08:12.770
But for now you just have to know that the spin is going to keep the node alive until you press Ctrl

08:12.800 --> 08:13.040
C.

08:13.070 --> 08:17.480
So let's save that and let's start again.

08:18.140 --> 08:20.030
And you can see that now we have Hello world.

08:20.030 --> 08:23.870
But the node is still hanging there okay.

08:23.900 --> 08:26.780
If I press enter the node is still hanging there.

08:26.780 --> 08:28.040
If I press Ctrl c.

08:28.070 --> 08:31.040
Now we exit from the node.

08:31.040 --> 08:32.750
So everything is going to be shut down.

08:32.750 --> 08:35.930
The node is going to be shut down and we exit from the program.

08:35.960 --> 08:36.470
All right.

08:36.470 --> 08:38.960
So I'm going to keep this like this here.

08:38.960 --> 08:41.210
And I'm going to explain more about it a bit later.

08:41.240 --> 08:44.840
Now what we have done so far is we have a file that's a valid file.

08:44.840 --> 08:49.190
We have run the file from where we created it directly.

08:49.190 --> 08:54.470
But now what we want to do actually when we create a new file like this is we want to install it, we

08:54.470 --> 08:59.510
want to install it and create an executable that we can start then with the ros2.

08:59.540 --> 09:00.320
Command line.

09:00.320 --> 09:01.820
And that's going to be super useful.

09:01.820 --> 09:05.810
For example, later we can also start it from a launch file, etc..

09:05.810 --> 09:11.480
So even if you can run the Python program like this directly from the source folder here of the workspace,

09:11.480 --> 09:15.200
we're going to install the node and how to install the node.

09:15.200 --> 09:21.980
Well you will just go inside your Python package to the setup.py inside this file.

09:21.980 --> 09:23.720
Here you see lots of stuff.

09:23.720 --> 09:28.100
But at the end you see entrypoints with console scripts.

09:28.100 --> 09:29.630
And that's an array.

09:29.660 --> 09:30.650
That's a list here.

09:30.650 --> 09:36.410
So you go inside this, you open double quotes and we are going to give the instructions to create a

09:36.410 --> 09:38.030
new executable.

09:38.210 --> 09:39.350
So I'm going to create it.

09:39.380 --> 09:40.250
We're going to run it.

09:40.250 --> 09:42.860
And then I'm going to come back and explain a bit more about it.

09:42.860 --> 09:47.660
So let's create an executable which name is py node.

09:47.660 --> 09:50.000
So that is something that I choose.

09:50.000 --> 09:53.390
Then I will put a space equal space.

09:53.390 --> 09:56.420
And then I need to provide basically the path.

09:56.450 --> 10:00.000
So the relative path to the file here.

10:00.270 --> 10:02.730
So we need to go to my pi pkg.

10:02.730 --> 10:05.160
So usually there's going to be the package name.

10:05.730 --> 10:15.240
So package name and then dot the file name here it's my first node dot pi my first node.

10:15.240 --> 10:18.270
But I don't provide the pi extension okay.

10:18.300 --> 10:21.240
So just my first node okay.

10:21.270 --> 10:24.750
Package dot file without the extension.

10:24.870 --> 10:26.310
And then we have a colon.

10:26.310 --> 10:30.960
And we need to provide the name of the function that we want to call.

10:30.960 --> 10:37.200
So when we start this we are going to call a function from that program what function we want to call.

10:37.200 --> 10:38.940
Well that's going to be the main function.

10:38.940 --> 10:42.120
So we write main just like that.

10:42.120 --> 10:44.160
No parentheses nothing okay.

10:44.190 --> 10:52.140
So you choose a name for the executable and then package dot file name colon function to call.

10:52.200 --> 10:53.130
And that's it.

10:53.160 --> 10:56.640
If you want to add more executables you will need to add a comma.

10:56.670 --> 10:59.530
Go to a new line and then do the same thing again.

10:59.560 --> 11:01.900
Okay, but for now we only have one.

11:01.900 --> 11:04.180
So let's save this file.

11:04.180 --> 11:05.200
Very important.

11:05.230 --> 11:06.700
You save all the files.

11:06.910 --> 11:11.200
Let's go back to the terminal and let's go to.

11:11.230 --> 11:14.980
So let's go to the Ros2 workspace.

11:15.010 --> 11:16.030
Once again.

11:16.030 --> 11:17.230
Very important.

11:17.230 --> 11:20.380
You don't build just anywhere.

11:20.380 --> 11:24.220
You only built in the Ros2 workspace folder.

11:24.220 --> 11:26.200
That's super, super important.

11:26.200 --> 11:27.940
So we are here I can do.

11:27.970 --> 11:29.110
So let's do clear here.

11:29.110 --> 11:35.650
And I can do call on build now because I have two packages and I only want to build the Python one.

11:35.650 --> 11:39.760
I'm going to use packages select.

11:39.760 --> 11:41.020
So it's not really important.

11:41.020 --> 11:42.220
You could build the two packages.

11:42.220 --> 11:44.380
But here let's do things in a clean way.

11:44.380 --> 11:51.040
So packages select and then my pi pkg okay.

11:51.040 --> 11:53.680
So we build the Python package.

11:53.680 --> 11:58.210
And now what's going to happen is that you just gave an instruction to create an executable.

11:58.210 --> 12:03.070
So inside the install folder here.

12:03.220 --> 12:06.490
Well you can try to browse inside and maybe find the executable.

12:06.490 --> 12:07.390
It doesn't really matter.

12:07.390 --> 12:13.720
But basically you know that this executable here pi node is going to be created inside the install folder.

12:13.750 --> 12:17.530
And it doesn't really matter where because we don't need to go inside this.

12:17.530 --> 12:21.880
We can use the Ros2 command line tool directly to run the executable.

12:21.970 --> 12:22.600
Okay.

12:23.380 --> 12:26.290
So for example let's say I'm in my home directory.

12:26.290 --> 12:28.150
Well you could do that from wherever.

12:28.150 --> 12:29.350
And I'm still going to here.

12:29.350 --> 12:31.660
I'm still going to source the bashrc.

12:31.660 --> 12:37.060
So you could do complete path with tilde slash dot bash RC.

12:37.090 --> 12:37.990
Why is that?

12:37.990 --> 12:44.260
Because well, I have just created something new in my workspace and because I have created something

12:44.260 --> 12:47.350
new, I'm going to source my workspace.

12:47.380 --> 12:47.530
Okay.

12:47.560 --> 12:52.660
Just to be sure that I have the latest updates and how to source the workspace.

12:52.660 --> 12:58.730
Well, you could source the setup bash script inside the install folder, but you can also source the

12:58.730 --> 13:03.020
bashrc because, well, we already have the line to do this inside the bashrc.

13:03.050 --> 13:03.680
Okay.

13:03.680 --> 13:09.140
So just make sure that you choose the workspace if you have created something new.

13:09.170 --> 13:09.410
Okay.

13:09.440 --> 13:10.370
In a terminal.

13:10.400 --> 13:14.840
Or you could open a new terminal and then everything is done for you automatically.

13:14.840 --> 13:20.120
So for example we source that and then I can start the node with Ros2 run.

13:20.150 --> 13:23.810
I will need to first provide the package name.

13:24.020 --> 13:26.660
The package name is my pi pkg.

13:26.720 --> 13:28.400
You see I can press the auto completion.

13:28.430 --> 13:29.210
It should be found.

13:29.210 --> 13:34.460
If it's not found here then you have a problem already and you need to go back and make sure that you've

13:34.460 --> 13:36.800
done everything and then well.

13:36.800 --> 13:38.900
So I do roster package name.

13:38.900 --> 13:42.170
And then I need to provide the executable name.

13:42.170 --> 13:44.030
What is the executable name.

13:44.030 --> 13:49.130
Well once again the executable name is what we have created here in the setup.py.

13:49.130 --> 13:51.800
So it's called here pi node.

13:52.640 --> 13:54.800
So pi node.

13:54.800 --> 13:56.770
And I don't even need to type everything.

13:56.800 --> 13:58.180
I can press Tab here.

13:58.210 --> 13:58.630
Okay.

13:58.660 --> 13:59.800
It's going to be found.

13:59.830 --> 14:04.960
I press enter and you can see it is currently working.

14:04.960 --> 14:06.760
So now I have created a node.

14:06.790 --> 14:12.940
I have installed it and I can run it from anywhere using the Ros to run command line tool.

14:12.940 --> 14:15.070
And now I can do ctrl C to stop.

14:15.070 --> 14:16.480
So we have a keyboard interrupt.

14:16.480 --> 14:18.790
But you don't need to really worry about that.

14:18.820 --> 14:19.330
Okay.

14:19.330 --> 14:24.220
And I'm just going to come back to one very important thing that you need to make sure to understand

14:24.250 --> 14:25.030
is that.

14:25.030 --> 14:27.070
So we have the executable name here.

14:27.100 --> 14:31.300
The executable name is what's created after you install the node here.

14:31.330 --> 14:32.890
So let's pick node here.

14:33.190 --> 14:36.280
But then we also have pytest.

14:36.310 --> 14:38.920
Pytest is the node name.

14:38.920 --> 14:43.870
And the node name is what you have defined here inside the code.

14:43.900 --> 14:44.170
Okay.

14:44.200 --> 14:45.850
So that's two different things.

14:45.850 --> 14:51.610
The executable name here and the node name are different things.

14:51.610 --> 14:54.850
And then also we have my first node.py.

14:54.880 --> 14:56.730
That's the file name.

14:56.760 --> 14:56.910
Okay.

14:56.940 --> 14:58.560
So we have three different things here.

14:58.560 --> 15:02.040
We have the node name that we create inside the file.

15:02.070 --> 15:03.630
We have the file name.

15:03.630 --> 15:09.930
And then we have the executable name that we create so that we can use with Ros to run.

15:09.960 --> 15:11.730
Okay three different things.

15:11.730 --> 15:16.770
And here I used different names for all three just to show you that there are different things.

15:16.770 --> 15:18.630
But sometimes you will see the same.

15:18.630 --> 15:24.810
For example, if you create a temperature sensor file, you might name it temperature sensor dot p y

15:24.810 --> 15:28.500
and then the node name might be temperature sensor.

15:28.500 --> 15:32.460
And the executable name also might be temperature sensor.

15:32.460 --> 15:37.200
But you need to be aware that those are three different things okay.

15:37.230 --> 15:39.570
And I'm going to come back to this also later.

15:39.570 --> 15:40.800
But for now well, great.

15:40.800 --> 15:45.240
You've made a lot of progress already because you can create a file here.

15:45.240 --> 15:48.240
You can create a file to write a node.

15:48.240 --> 15:51.390
You can write a basic version of a node.

15:51.390 --> 15:56.160
You can install that node and run that node from the terminal.
