WEBVTT

00:00.080 --> 00:06.830
So they are actually three languages you can use to write large files XML, YAML and Python.

00:06.830 --> 00:11.660
We have just seen XML version in the last video here with this large file.

00:11.690 --> 00:16.190
As for YAML, well first it's not really used by anyone.

00:16.190 --> 00:21.950
And second, I find the XML version much clearer and less prone to errors.

00:21.950 --> 00:23.960
So I'm going to skip YAML anyway.

00:23.960 --> 00:25.220
Nobody uses that.

00:25.220 --> 00:26.540
Then we have Python.

00:26.540 --> 00:33.110
And why I'm talking about Python here is because Python is used quite a lot for large files in ros2.

00:33.140 --> 00:36.590
In fact, that was the first choice at the beginning of ros2.

00:36.590 --> 00:40.940
And now things are slowly switching back to favor XML.

00:40.940 --> 00:44.930
And in this lesson you will understand why we want to use XML.

00:44.960 --> 00:50.870
I can give you a spoiler already, so I like simplicity and I dislike Overengineering.

00:50.870 --> 00:57.710
I am 100% personally for using XML launch files whenever you can instead of Python.

00:57.740 --> 01:01.430
Okay, so this video is going to be a bit opinionated.

01:01.430 --> 01:08.040
With that said, let's get started and let's create a Python launch file that does the exact same thing

01:08.040 --> 01:10.770
as what we did here with this XML one.

01:10.770 --> 01:15.150
So we want to start the number publisher node and the number counter node.

01:15.150 --> 01:21.000
And just a quick detail when I talk about Python here, I'm talking about writing a launch file in Python,

01:21.000 --> 01:24.570
not about the node that is written in Python or C plus.

01:24.570 --> 01:27.300
Plus those are completely different things okay.

01:27.330 --> 01:29.640
So how to create a Python launch file.

01:29.640 --> 01:34.380
Well you first have to create a package like we did my robot bring up.

01:34.380 --> 01:38.820
And to set it up with the install launch file instruction.

01:38.820 --> 01:40.230
But that is done already.

01:40.230 --> 01:42.660
So what we can do is just add a new file.

01:42.660 --> 01:45.900
We can add a new launch file inside our launch folder.

01:45.930 --> 01:46.410
Okay.

01:46.440 --> 01:50.190
You can mix XML and Python files as you want here.

01:50.190 --> 01:51.720
It's not going to be a problem.

01:51.720 --> 01:56.100
And because we're going to do the exact same thing I'm going to also name it number app.

01:56.190 --> 02:01.350
And then it's going to be dot launch and then dot pi okay.

02:01.380 --> 02:07.040
The only difference is that we have a dot pi extension at the end because that's a Python file.

02:07.040 --> 02:09.050
Now let's write this.

02:09.050 --> 02:20.210
And first we will need to create a function that's named generate launch description okay.

02:20.240 --> 02:23.000
You need to write exactly this okay.

02:23.030 --> 02:26.390
Generate launch description exactly like that.

02:26.390 --> 02:31.940
In this launch description we are going to create a launch description object.

02:31.940 --> 02:35.630
So we need to do from launch okay.

02:35.660 --> 02:45.320
There is a ros2 Python module called launch import launch description okay.

02:45.350 --> 02:49.730
If you ever write Python launch files I encourage you to have the auto completion.

02:49.760 --> 02:52.760
Otherwise that's going to be a complete nightmare.

02:52.760 --> 02:54.410
And let's create.

02:54.410 --> 02:58.700
So usually we do LD is equal to launch description.

02:58.700 --> 03:00.830
So we create a launch description object.

03:00.830 --> 03:03.470
And at the end so we could return it directly.

03:03.470 --> 03:05.670
But we're going to add stuff to it at the end.

03:05.700 --> 03:08.880
We're going to do return launch description.

03:09.120 --> 03:09.660
Okay.

03:09.660 --> 03:13.320
So that's basically the minimum code you need for a launch file.

03:13.470 --> 03:17.910
You have this function here generate launch description.

03:17.940 --> 03:21.810
Then you create a launch description object that you return.

03:21.870 --> 03:24.870
Now of course that's going to do nothing.

03:24.870 --> 03:30.120
So we are going to create some nodes and add the nodes inside the launch description.

03:30.120 --> 03:41.160
For that I will need to do from launch underscore Ros dot actions import node.

03:41.190 --> 03:45.060
If it doesn't make any sense well don't even try to make sense of that.

03:45.060 --> 03:47.430
I have completely given up at this point.

03:47.430 --> 03:53.400
And then so let's call it number publisher or number publisher node.

03:53.730 --> 03:55.770
That's going to be a node object.

03:55.800 --> 03:56.070
Okay.

03:56.100 --> 03:57.210
So we create a node object.

03:57.210 --> 04:00.300
But note this is a launch file node object okay.

04:00.330 --> 04:04.950
That's nothing to do with the node object we had in the code for the node itself.

04:04.950 --> 04:08.960
And then in this we can provide a lot of different parameters.

04:08.960 --> 04:12.740
For example package okay.

04:12.770 --> 04:14.630
And the package is going to be.

04:14.630 --> 04:20.690
So if I want to start the same thing I'm going to use the my pi pkg.

04:20.990 --> 04:26.660
And then I have executable which is okay.

04:26.690 --> 04:34.100
It's this one executable which is number publisher okay.

04:34.160 --> 04:36.230
And then I can add more stuff if I want.

04:36.260 --> 04:38.270
I'm going to keep it like this for now.

04:38.270 --> 04:40.010
So that's my first node.

04:40.010 --> 04:43.160
Second node is going to be number counter.

04:43.160 --> 04:45.260
So I just create a variable for that.

04:46.130 --> 04:48.230
It's going to be node okay.

04:48.230 --> 04:51.230
And that's going to be the same thing package.

04:51.830 --> 04:57.920
Let's use the C plus plus package and execute table.

04:58.550 --> 05:04.040
That's going to be number counter okay.

05:04.040 --> 05:07.700
So we have created I mean we have our two nodes here.

05:07.700 --> 05:13.410
And then to say that we want to start them, because this is going to do nothing to say that we want

05:13.410 --> 05:16.920
to start them, we need to add them to the launch description.

05:16.920 --> 05:23.370
So ld dot add action, we need to use the add action function.

05:23.370 --> 05:27.810
And we need to provide number publisher.

05:27.810 --> 05:34.560
And then we need to do ld dot add action with the number counter.

05:35.310 --> 05:35.850
All right.

05:35.850 --> 05:38.130
So you create a launch description.

05:38.130 --> 05:39.900
Then you create all your nodes.

05:39.900 --> 05:45.540
You add your nodes to the launch description and then you return the launch description.

05:45.690 --> 05:46.770
So let's save.

05:46.770 --> 05:50.550
And that's basically the same thing as this launch file.

05:50.670 --> 05:52.380
You see here we have four lines.

05:52.530 --> 05:54.480
And here I have 20 lines.

05:54.480 --> 05:58.800
I could reduce the spaces but basically well you see it's much bigger.

05:58.800 --> 06:02.430
And that's just the basic functionality of starting a node.

06:02.460 --> 06:02.640
Okay.

06:02.670 --> 06:09.650
We haven't even tried to import other launch files or import configurations from somewhere else.

06:09.650 --> 06:12.050
In this case, we're going to have many, many more.

06:12.050 --> 06:12.710
Includes.

06:12.710 --> 06:14.510
It's going to become much more verbose.

06:14.510 --> 06:17.690
Well, I guess you see the point here already.

06:17.690 --> 06:19.550
And actually I thought at the beginning.

06:19.550 --> 06:24.680
So at the beginning of Ros2, I thought the Python launch files could be really great features, because

06:24.680 --> 06:27.530
then you can use Python code, you can use for loops.

06:27.530 --> 06:33.380
You can well, you can use just Python logic inside this description.

06:33.380 --> 06:39.020
But in the end what I realized is, well, this is the theory, but then this is what you actually do

06:39.050 --> 06:41.630
in the end and what everybody does for the project.

06:41.630 --> 06:44.990
And I found that we don't really use that logic that much.

06:44.990 --> 06:51.470
And for everything we want to do, even a condition, if you want to do if to start some arguments in

06:51.500 --> 06:55.370
a node, you will have to use some very complicated imports.

06:55.400 --> 06:55.670
Okay.

06:55.700 --> 07:03.200
And in the end, I feel like this Python launch, uh, functionality basically uses a complicated syntax

07:03.200 --> 07:05.870
just to provide a description.

07:05.900 --> 07:12.060
Okay, that's a description we are trying to recreate with Python, but that's basically why XML was

07:12.060 --> 07:16.710
created in the first place is to do a description and it does it pretty well.

07:16.710 --> 07:22.110
So I would recommend that any time you can use XML, just use XML.

07:22.140 --> 07:24.570
Now the thing is some functionality.

07:24.600 --> 07:30.240
So some more advanced functionalities may be only available for Python.

07:30.240 --> 07:34.650
So what if you absolutely need to start one functionality using Python.

07:34.680 --> 07:37.860
Do you then need to write the entire launch file in Python?

07:37.860 --> 07:41.520
Or also lots of existing launch files are written in Python?

07:41.520 --> 07:42.900
What if you need to import one?

07:42.900 --> 07:50.490
Well, the good news is that you can easily import a Python launch file inside an XML launch file.

07:50.580 --> 07:52.410
And I'm going to show you that right now.

07:52.410 --> 07:54.990
So I'm going to create a new launch file here.

07:55.020 --> 08:06.210
New XML launch file named I'm going to call it number app from Python dot launch dot XML okay so the

08:06.210 --> 08:07.680
title is quite long.

08:07.680 --> 08:10.410
But then you see we have the dot launch dot XML.

08:10.500 --> 08:18.010
And I'm going to start with my lounge tag that I open and that I close.

08:18.460 --> 08:26.170
Now let's say that from that XML lounge file, I need to include this Python lounge file here.

08:26.260 --> 08:27.340
What do I do?

08:27.370 --> 08:34.660
Well, I can just use the include tag and file argument with okay.

08:34.690 --> 08:37.180
There is a function here that you can use.

08:37.180 --> 08:40.270
You're going to put dollar sign and parentheses.

08:40.300 --> 08:46.420
This function is named find dash pg dash share.

08:46.420 --> 08:49.510
So it's going to find the share folder inside the package.

08:49.510 --> 08:50.830
And you see the name share.

08:50.860 --> 08:53.860
Here we have the same in Cmakelists.txt.

08:54.280 --> 08:54.790
Okay.

08:54.790 --> 09:00.730
So that's important that you write share here for this folder because this function is going to find

09:00.730 --> 09:01.030
it.

09:01.030 --> 09:07.900
So we put find PG share and then the package name for my robot bring up.

09:07.930 --> 09:11.530
In this case we are including a launch file from the same package.

09:11.710 --> 09:13.060
It could be another one.

09:13.110 --> 09:13.620
Okay.

09:13.620 --> 09:19.380
So then you can do slash launch slash and then the name.

09:19.380 --> 09:24.630
So number app dot launch dot pi.

09:24.900 --> 09:27.210
And then you close the includes.

09:27.210 --> 09:31.650
So you see with one line you can include another file.

09:31.650 --> 09:33.780
And it doesn't have to be an XML file.

09:33.780 --> 09:37.950
You can include a Python launch file inside an XML launch file.

09:37.950 --> 09:40.860
The only thing to know is this syntax.

09:40.860 --> 09:42.330
We find pkg share.

09:42.330 --> 09:43.980
But that's going to be the same every time.

09:44.010 --> 09:45.720
All right so I can save that.

09:45.720 --> 09:51.690
And you can see that if I wanted to create an XML launch file with some stuff and then include that

09:51.690 --> 09:54.330
one, I can just do this.

09:54.330 --> 10:00.300
So then I can just write XML, launch files for everything and just include what I need.

10:00.300 --> 10:04.320
If I need to import another launch file which is written in a different language.

10:04.350 --> 10:04.740
All right.

10:04.740 --> 10:06.690
We haven't tested that already.

10:06.690 --> 10:15.610
So let's just open a terminal and let's go to our workspace and let's build click on build packages

10:15.610 --> 10:20.980
select with my robot bring up.

10:21.430 --> 10:25.270
So we need to build every time because that's going to install the folder.

10:25.270 --> 10:29.080
There's nothing to compile, but it's going to install the folder and the new files.

10:29.080 --> 10:33.040
Then I'm going to well let's open two terminals.

10:33.040 --> 10:35.260
So those are going to be sourced.

10:35.500 --> 10:41.380
And we can do Ros two launch my robot bring up.

10:41.380 --> 10:45.280
And let's start for example the Python launch file that we have created.

10:45.280 --> 10:53.770
So my uh no it's called number app dot launch dot Pi.

10:55.000 --> 10:59.410
And you can see it's starting just like the XML one.

10:59.410 --> 11:05.530
So exactly the same functionalities to stop it I can also press Ctrl C okay.

11:05.560 --> 11:06.670
And I stop the node.

11:06.670 --> 11:08.530
And now what if I want to start.

11:08.530 --> 11:10.180
So let's start this one.

11:10.270 --> 11:13.840
This XML launch file that includes a Python one.

11:14.230 --> 11:15.880
Let's start this one Ros two.

11:15.900 --> 11:23.130
So Rose two launch number app from Python that launch that XML.

11:24.630 --> 11:26.490
And you can see it's the same thing.

11:26.880 --> 11:27.960
Exactly the same thing.

11:27.960 --> 11:32.730
If I do a graph we have the same stuff.

11:32.730 --> 11:35.490
So the application was correctly started.

11:36.060 --> 11:36.510
All right.

11:36.510 --> 11:42.990
So in the following of this course what I'm going to do is I'm going to use XML okay.

11:43.890 --> 11:45.480
And I encourage you to do the same.

11:45.480 --> 11:47.580
This is going to make your life much easier.

11:47.580 --> 11:52.890
Then later on if you encounter some Python launch files well you know the basic structure.

11:52.890 --> 11:57.030
And then you might need to find existing codes on GitHub.

11:57.030 --> 11:59.970
Maybe if you need to modify one Python launch file.

11:59.970 --> 12:05.940
So there is nothing really to understand about all the imports, you just have to find them and basically

12:05.940 --> 12:08.460
copy paste from stuff that you find on GitHub.

12:08.460 --> 12:10.740
So that's if you need to work with Python launch files.

12:10.740 --> 12:15.090
And if you need to include a Python launch file in your application, you know that from an XML launch

12:15.090 --> 12:18.150
file, you can just do that and it's going to work.
