So they are actually three languages you can use to write large files XML, YAML and Python.
We have just seen XML version in the last video here with this large file.
As for YAML, well first it's not really used by anyone.
And second, I find the XML version much clearer and less prone to errors.
So I'm going to skip YAML anyway.
Nobody uses that.
Then we have Python.
And why I'm talking about Python here is because Python is used quite a lot for large files in ros2.
In fact, that was the first choice at the beginning of ros2.
And now things are slowly switching back to favor XML.
And in this lesson you will understand why we want to use XML.
I can give you a spoiler already, so I like simplicity and I dislike Overengineering.
I am 100% personally for using XML launch files whenever you can instead of Python.
Okay, so this video is going to be a bit opinionated.
With that said, let's get started and let's create a Python launch file that does the exact same thing
as what we did here with this XML one.
So we want to start the number publisher node and the number counter node.
And just a quick detail when I talk about Python here, I'm talking about writing a launch file in Python,
not about the node that is written in Python or C plus.
Plus those are completely different things okay.
So how to create a Python launch file.
Well you first have to create a package like we did my robot bring up.
And to set it up with the install launch file instruction.
But that is done already.
So what we can do is just add a new file.
We can add a new launch file inside our launch folder.
Okay.
You can mix XML and Python files as you want here.
It's not going to be a problem.
And because we're going to do the exact same thing I'm going to also name it number app.
And then it's going to be dot launch and then dot pi okay.
The only difference is that we have a dot pi extension at the end because that's a Python file.
Now let's write this.
And first we will need to create a function that's named generate launch description okay.
You need to write exactly this okay.
Generate launch description exactly like that.
In this launch description we are going to create a launch description object.
So we need to do from launch okay.
There is a ros2 Python module called launch import launch description okay.
If you ever write Python launch files I encourage you to have the auto completion.
Otherwise that's going to be a complete nightmare.
And let's create.
So usually we do LD is equal to launch description.
So we create a launch description object.
And at the end so we could return it directly.
But we're going to add stuff to it at the end.
We're going to do return launch description.
Okay.
So that's basically the minimum code you need for a launch file.
You have this function here generate launch description.
Then you create a launch description object that you return.
Now of course that's going to do nothing.
So we are going to create some nodes and add the nodes inside the launch description.
For that I will need to do from launch underscore Ros dot actions import node.
If it doesn't make any sense well don't even try to make sense of that.
I have completely given up at this point.
And then so let's call it number publisher or number publisher node.
That's going to be a node object.
Okay.
So we create a node object.
But note this is a launch file node object okay.
That's nothing to do with the node object we had in the code for the node itself.
And then in this we can provide a lot of different parameters.
For example package okay.
And the package is going to be.
So if I want to start the same thing I'm going to use the my pi pkg.
And then I have executable which is okay.
It's this one executable which is number publisher okay.
And then I can add more stuff if I want.
I'm going to keep it like this for now.
So that's my first node.
Second node is going to be number counter.
So I just create a variable for that.
It's going to be node okay.
And that's going to be the same thing package.
Let's use the C plus plus package and execute table.
That's going to be number counter okay.
So we have created I mean we have our two nodes here.
And then to say that we want to start them, because this is going to do nothing to say that we want
to start them, we need to add them to the launch description.
So ld dot add action, we need to use the add action function.
And we need to provide number publisher.
And then we need to do ld dot add action with the number counter.
All right.
So you create a launch description.
Then you create all your nodes.
You add your nodes to the launch description and then you return the launch description.
So let's save.
And that's basically the same thing as this launch file.
You see here we have four lines.
And here I have 20 lines.
I could reduce the spaces but basically well you see it's much bigger.
And that's just the basic functionality of starting a node.
Okay.
We haven't even tried to import other launch files or import configurations from somewhere else.
In this case, we're going to have many, many more.
Includes.
It's going to become much more verbose.
Well, I guess you see the point here already.
And actually I thought at the beginning.
So at the beginning of Ros2, I thought the Python launch files could be really great features, because
then you can use Python code, you can use for loops.
You can well, you can use just Python logic inside this description.
But in the end what I realized is, well, this is the theory, but then this is what you actually do
in the end and what everybody does for the project.
And I found that we don't really use that logic that much.
And for everything we want to do, even a condition, if you want to do if to start some arguments in
a node, you will have to use some very complicated imports.
Okay.
And in the end, I feel like this Python launch, uh, functionality basically uses a complicated syntax
just to provide a description.
Okay, that's a description we are trying to recreate with Python, but that's basically why XML was
created in the first place is to do a description and it does it pretty well.
So I would recommend that any time you can use XML, just use XML.
Now the thing is some functionality.
So some more advanced functionalities may be only available for Python.
So what if you absolutely need to start one functionality using Python.
Do you then need to write the entire launch file in Python?
Or also lots of existing launch files are written in Python?
What if you need to import one?
Well, the good news is that you can easily import a Python launch file inside an XML launch file.
And I'm going to show you that right now.
So I'm going to create a new launch file here.
New XML launch file named I'm going to call it number app from Python dot launch dot XML okay so the
title is quite long.
But then you see we have the dot launch dot XML.
And I'm going to start with my lounge tag that I open and that I close.
Now let's say that from that XML lounge file, I need to include this Python lounge file here.
What do I do?
Well, I can just use the include tag and file argument with okay.
There is a function here that you can use.
You're going to put dollar sign and parentheses.
This function is named find dash pg dash share.
So it's going to find the share folder inside the package.
And you see the name share.
Here we have the same in Cmakelists.txt.
Okay.
So that's important that you write share here for this folder because this function is going to find
it.
So we put find PG share and then the package name for my robot bring up.
In this case we are including a launch file from the same package.
It could be another one.
Okay.
So then you can do slash launch slash and then the name.
So number app dot launch dot pi.
And then you close the includes.
So you see with one line you can include another file.
And it doesn't have to be an XML file.
You can include a Python launch file inside an XML launch file.
The only thing to know is this syntax.
We find pkg share.
But that's going to be the same every time.
All right so I can save that.
And you can see that if I wanted to create an XML launch file with some stuff and then include that
one, I can just do this.
So then I can just write XML, launch files for everything and just include what I need.
If I need to import another launch file which is written in a different language.
All right.
We haven't tested that already.
So let's just open a terminal and let's go to our workspace and let's build click on build packages
select with my robot bring up.
So we need to build every time because that's going to install the folder.
There's nothing to compile, but it's going to install the folder and the new files.
Then I'm going to well let's open two terminals.
So those are going to be sourced.
And we can do Ros two launch my robot bring up.
And let's start for example the Python launch file that we have created.
So my uh no it's called number app dot launch dot Pi.
And you can see it's starting just like the XML one.
So exactly the same functionalities to stop it I can also press Ctrl C okay.
And I stop the node.
And now what if I want to start.
So let's start this one.
This XML launch file that includes a Python one.
Let's start this one Ros two.
So Rose two launch number app from Python that launch that XML.
And you can see it's the same thing.
Exactly the same thing.
If I do a graph we have the same stuff.
So the application was correctly started.
All right.
So in the following of this course what I'm going to do is I'm going to use XML okay.
And I encourage you to do the same.
This is going to make your life much easier.
Then later on if you encounter some Python launch files well you know the basic structure.
And then you might need to find existing codes on GitHub.
Maybe if you need to modify one Python launch file.
So there is nothing really to understand about all the imports, you just have to find them and basically
copy paste from stuff that you find on GitHub.
So that's if you need to work with Python launch files.
And if you need to include a Python launch file in your application, you know that from an XML launch
file, you can just do that and it's going to work.