Now that you understand the purpose of launch files, let's see how to actually create one for your
Ros2 application.
And the first question is where do we create our launch files.
So if I go to my workspace in the source folder you see I have three packages.
So where do I create the launch file.
Well technically you could add a launch file in any package.
But as we did for our interfaces, we are going to create a dedicated package for the launch files.
We will add all the launch files for our application and also configuration files for example the YAML
files.
This will make it much easier for you to handle dependencies in your application, and also later on
if you check some other projects.
If you find other robots for which the code is open source and on GitHub, you will find this organization
all the time.
So we're going to create a new package.
And well let's just first you go to the source folder of your workspace and let's do Ros2 pkg Create.
And I'm going to start with my robot.
Okay.
So if your robot is named ABC, you're going to write ABC.
Just use the name of your robot or your application.
And then I'm going to add the bring up suffix.
Okay.
So for interfaces we have interfaces for launch file we have bring up.
This is really common okay.
You will find this kind of bring up package in a lot of different GitHub projects if you browse online
for different robots okay.
So it's always the same thing.
And I recommend that you use also the name bring up.
Then what build type do we need to provide?
Well we can provide payment CMake if we want, but we can also skip it which is going to create anyway,
even CMake by default.
And for the dependencies I'm not going to provide any dependency.
Now we're going to add them as we add more stuff in our launch files.
So basically just Ros2 pkg PG.
Create my robot.
Bring up.
We have a new package.
You see some logs?
The warning for the license.
Okay.
And let's go inside.
So RLS, we have the new package here.
Let's go inside my robot bring up.
And what do we have.
Well, classic stuff with Cmakelists.txt that we will keep package dot XML.
Very important.
And then we have include and source.
We don't need those folders so let's remove them.
Let's remove include and let's remove source.
Now I'm going to create a new folder right now called launch okay.
So now in my package I have this launch folder and those two files.
And in this launch folder I will write all the launch files for my application.
Great.
So now we can open uh let's just open the workspace with VSCode here from the source folder.
And we can go to our bring up package.
And what I'm going to do is I'm going to set up the package for installing the launch files.
Okay?
Just like when we created the interfaces package, we had a bit of setup here.
We also have a bit of setup specific for launch files is going to be much quicker.
First of all, we don't have to do anything in the package dot XML, so you can just keep it like this.
You see we have build type A1, CMake.
That's what we want.
And then we're going to go to the Cmakelists.txt.
And in that one I'm going to simplify the code here as well.
I'm going to remove the build testing.
We don't need that for now.
And I'm going to remove those comments.
And now basically the only instruction that we are going to provide here is to say that we want to install
the launch folder, okay.
And everything that's inside the launch folder.
So I'm going to write install and then uppercase Directory.
Okay.
Let's go to New Line.
I'm going to put launch.
Launch is because that folder is named launch and then destination all uppercase.
Make sure you don't make any typo.
And then we are going to write share.
Okay.
So we are going to install this inside a share folder.
Okay.
That's going to be inside the install folder of your workspace.
And it's important that you write share here slash.
And then we're going to put the project name.
So basically the package name with dollar sign and curly brackets.
Make sure you don't put parentheses so often.
This is a common mistake.
But parentheses now it's curly brackets and project name.
And you can add a slash here if you want.
So okay make sure you write exactly this.
Okay.
So between find package and payment package you add this install instruction and that's basically it.
Everything that we will write inside the launch folder here is going to be installed.
So we only need to write this once.
That's it.
Let's make sure we save.
And while the package is correctly configured.
So that's basically it.
You create a Myrobot package.
Then you create a launch folder and you install this launch folder.
Now let's create our first launch file.
For this we are going to add it inside the launch folder.
So you could go to the terminal.
I can also just create it here and let's name it number app dot launch dot XML okay so what I'm going
to start the number of application from this launch file.
What I want to do is start different nodes.
So for this example I'm going to start the number publisher and the number counter that we have created
previously in this course.
So I'm just going to name this application number app.
Then you have dot launch and then dot XML because we write an XML file.
Okay.
And you can see this is inside the launch folder inside the package.
So you can do that from the terminal, from VSCode from anywhere you want.
And then we are going to initialize our launch file.
And for that it's very easy.
So we're going to write XML code.
And we need a launch tag.
So you open a launch tag like this and you close the launch tag and that's it.
Your launch file is initialized.
So for every launch file you create in rust with XML, you need to add those launch tags.
And everything you write is going to be inside this.
Then you don't need to provide any indentation okay, it's XML, but still I'm going to add an indentation
for readability.
And in this launch file let's say we want to start two nodes I want to start the number publisher node
and the number counter node.
To start a node you can provide the node tag just like that.
So you open it and inside the node tag you have a pkg argument that's going to be the package where
the node.
So actually where the executable is.
And for example let's start the one from my pi pkg.
And then we have exec okay.
So we have pkg and exec exec is the executable.
So number publisher actually you could just check that.
So here I can do Ros to run package name okay.
Executable name.
So to construct a so number publisher to construct a launch file you can just run the commands here
one by one all the nodes inside terminals.
And then you can just take the names that you have written here just to make sure that this is correct.
You see my pi pkg.
That's the package and number publisher.
That's the executable.
And I'm going to close.
It's just going to be one line for now.
So I'm going to close like this.
Okay.
With a forward slash and a bracket.
And so what will happen if I launch if I start this launch file.
Well it's going to start that executable.
Just like if you would run that node in the terminal.
Same thing.
Now let's say that I want to start the number counter.
And this time let's change.
Let's use the C plus plus package number counter.
So you see package executable name.
So I'm adding another node tag pkg.
My cp pkg and exec is going to be number counter.
Okay.
Just like if you would run that on the terminal.
Okay.
And with this simple launch file we have four lines.
Then we have started two nodes.
Okay.
No configuration nothing.
We're going to add that in the following lessons.
But you see with just this we start two nodes.
I'm going to save this.
And also because you see that from my robot bring up, we are going to execute stuff from two other
packages.
So because of that I'm going to go inside my package dot XML of the My Robot Bringer package.
And after here I'm going to add dependencies.
Okay.
So usually I have written the depend tag.
But for large files you could also use the exec depend tag because well, exec depend is a bit less
strict than depend.
And it just means that you need the dependencies for execution.
Okay.
Not for compiling, not for building anything, just for execution.
All right.
We're not going to compile any code here.
We just say that we need this package when we execute the launch file.
So we can use exec depend.
And my pi pkg okay.
Here execute depend and then exec depend with my cp pkg exec depend.
So as a best practice, whenever you see you start to use a new package inside a launch file in this
package here, then you add the package with exec, depend in the package dot XML file and let's save
everything.
Now we can go back to the terminal.
Let's let's go back to our workspace.
And let's build on build.
Let's use packages select to just build my robot.
Bring up and use the Autocompletion once again okay.
So this one is always going to be quite quick.
There is nothing really to compile.
It's just going to install the the file somewhere in the install folder.
And now to start it.
Well to start it is very easy.
I'm just going to keep this terminal here.
I'm going to make sure that I source My workspace here with the setup that bash.
And then well, let's do this to have more space then we can do Ros2 launch.
Okay.
So for node you do Ros to run.
If you want to start a launch file, you do Ros to launch.
And then you need to provide what the package where the launch file is.
So that's my robot.
Bring up my robot.
Bring up using Autocompletion.
And if I press tab twice here.
Well you see I have some options and stuff.
But you see I have my number app dot launch dot XML.
So if I just start number app you see I can find it with Autocompletion.
So Ros2 launch package name and then launch file name.
Let's start it and you can see what do we have here.
We have a few logs.
So stuff where you can find logs here logging verbosity.
And then you see that for each executable that we start in the launch file, we have a log saying that
we have started the process.
It even gives you the PID.
So the process ID here.
And you see now we have started an executable named number publisher one.
Number counter two.
Okay.
So that's the executable that has been started.
And then you see the logs.
Those are the logs that we directly print from the nodes.
So we have the number counter node and we have the number publisher node.
Now well actually I'm going to start some terminals here.
If I do ros2 node list you see I have my two nodes okay.
I didn't need to start them from two terminals.
I just started them from the same launch file.
Okay.
If I do ros2 topic list, you see I have my number topic and my number count topic.
If I do ros2 topic equal number counts just to check that it works.
And you see I have my data here so it's correctly working I can do RCT graph.
And well you might refresh.
You put nodes topics all.
You also uncheck those boxes here and you will have also that view okay.
So everything is correctly working.
Our application has been launched.
We've just you see this command line and that's how you create an XML launch file.
So if you want to create a new one you just add a new file here in the launch folder you start with
a launch tag, and then you add one node tag for each node that you want to start.
And then that's it.
You just make sure that you build the package again and that you source the environment.
And then you can just do Ros2 launch package and launch file name.