We are now at the last step for this project.
Step number six.
And here we are not going to add any new functionality.
We are just going to make the application more dynamic and scalable with parameters and launch files.
Okay.
So you can start the application with different values for different parameters.
You don't need to change the code at all.
And you can also start everything from just one file.
And let's start with the parameters.
So we are going to go to the code of Total controller and Total Spawner and see what could be a parameter.
And well here for example you see that we have a catch closest total first.
So that's something we can choose.
But it's hard coded in the nodes.
So we will need to modify the code.
Instead of that we can add a parameter.
So I'm going to do self dot declare parameter.
And let's name it simply catch closest total first.
And let's put a default value of true.
Okay.
So then we can just going to reverse the order here.
So this one instead of true we can say self get parameter with the name and then dot value.
And again no parentheses here.
All right.
So you can see it's very easy to create a parameter.
You declare it and then you do get parameter dot value.
Okay let's save that.
And then we can go to turtle spawner.
And here I'm going to add two parameters.
Okay.
We're going to be able to change the turtle prefix I have just used turtle for now.
But let's say you want to use my turtle or any other name.
You can choose it with the parameters.
And also well, that's something I have actually changed in the code to make some different tests.
The frequency at which we're gonna spawn turtles.
Okay, that could be very useful as a parameter.
So let's start here.
Self declare parameter.
So let's name this one turtle.
Name prefix.
The default value.
We're going to use the same one as in the code turtle.
And then self declare parameter with.
So for example spawn frequency.
Here I'm going to use frequency instead of period.
Okay.
It's just the opposite.
To get the frequency you do one divided by the period.
Okay.
So to get the period you will need to do actually one divided by frequency.
All right.
So it's basically the same thing.
We just need to modify something here in the code.
And let's say the default frequency is going to be one hertz.
So then I have my turtle name prefix which is going to be so self get parameter.
The name is here.
Dot value and then self dot.
Here I'm going to create a spawn frequency.
We don't have this variable yet is equal to self get parameter with spawn frequency and dot value.
All right.
So then turtle name prefix is correctly set the spawn frequency I don't use it.
So I'm going to have to use it here.
I'm going to simply do 1.0 divided by self dot spawn frequency.
So if I give ten it means ten hertz.
It means ten turtles per second.
And this is going to be 0.1.
Okay.
That's how the frequency works.
All right.
And that's basically it.
So I can save.
Now you could already.
Well if you want you can start the nodes already here and start with the parameters.
I'm going to go directly to the next thing which is to create the launch file.
And so where do we create launch files.
Well you need a dedicated bringup package for that.
So you can either create one new package for that and set it up and everything.
But I already have my robot bring up in this workspace.
So I'm going to use that.
And in the launch folder I'm going to create a new file named turtle SIM.
Catch them all.
Well you can name it as you want dot launch dot XML.
Let's open and close the launch tag And then what nodes do we need to start?
Well, the first node is actually the turtle sim node.
So if I come back here you see we do restaurant turtle sim.
Turtle sim node.
So node with pig.
Turtle SIM exec.
Turtle sim node.
Okay.
That's the first node we need.
The second node where we could start both.
It doesn't matter the order, but we're going to start the controller from turtle SIM.
Catch them all.
That's the package.
And controller is the executable.
So node, pig turtle sim catch them all.
Exec controller.
And the third node is also going to be from the same package here.
And the executable you can see Ease if I come back is spawner.
All right.
So we've just that we can start what we started in those three terminals here.
And now I'm going to add the parameters.
So you could add the parameters as we saw previously here with param and then name and then value.
But what I'm going to do instead is I'm going to create a YAML file that I will load directly for all
the nodes.
So how to create a YAML file.
I'm going to go to my config folder here.
And I don't know let's name this catch them all config dot YAML for example.
And let's put the parameters for each nodes.
Do we need parameters for the turtle sim node?
No.
You could actually add parameters if you wanted to change the background.
Okay.
You know that you have three colors red blue and green.
You could change the background if you want to, but I'm not going to do that here.
I'm going to add parameters for the controller and actually the node.
So if you run the node I'm going to run that same node data.
Same with the controller and the spawner.
And if I do Ros two uh node list.
You see that's the node here turtle controller and turtle spawner.
Okay.
So that's what we need.
So slash turtle controller and then Ros underscore underscore parameters.
And what parameters we have.
Well we just take it from here.
Catch closest turtle.
And I'm just going to put it to true.
Okay.
Even if it's the default value I'm going to use.
True.
Because yes I want to catch the closest turtle first.
Then I can do.
So what was the name?
Turtle spawner.
And then Ross parameters.
And here we have two parameters.
So we have turtle name prefix.
Let's name this one my turtle to change it.
And we have the spawn frequency.
Okay.
The default is one hertz.
Let's say 1.5Hz.
So it's a bit faster.
And that's all the parameters that I can provide.
Let's save this file.
And then in that one we are going to provide the parameters directly from that YAML file.
So for that node here I'm going to open and close the node tag.
And I'm going to put the param.
And if you don't remember the syntax we had it here.
So from and then find PG share.
So from and then dollar sign parentheses.
Find pg share.
The name of the package is my robot.
Bring up and then slash config slash.
Catch them all.
Config dot YAML.
All right.
And the good thing about that is we can just duplicate the code.
So I also open and close that node like this.
And I can just basically take the same right here.
It's the same file.
Okay.
So you can see our launch file is now nine lines of code long.
And we start three nodes including parameters for two nodes.
All right.
So the first node the second node so the controller node with the params and the spawner node with the
params that are all in this YAML file.
Now we can go back here and we will need to build this time.
Call and build the packages.
Select my robot.
Bring up.
So yeah actually I miss a space here.
And actually before I build one thing we can do because we are going to use, you see, from that,
uh, launch file, we are starting nodes from the turtle package and the turtle sim catch them all package.
So let's go to package dot XML.
And you see we have an exact deployment here that we can add exact depends with turtle sim exact depend.
And then another exact depend.
Turtle SIM him.
Catch them all.
Okay, so we make things a bit cleaner.
So in case those packages were not installed, then we will get an error directly when we try to do
a call on build.
So let's do that.
Okay.
It was very quick.
And then well actually we don't need to do much.
We just source the environment and we do.
Ros two launch.
Turtle.
No actually it's from my robot.
Bring up and launch file is called turtle SIM.
Catch them all.
Dot launch dot XML.
Let's see what happens if I run this and you see.
Well the application is just working.
We have I'm going to do Ctrl C here.
And let's come back here to the beginning.
You see we start three processes.
Okay.
The turtle SIM node the controller and the spawner.
And we have all the logs here that are mixed together in this launch file.
Okay, so let's start again.
All right.
You can see the turtle is chasing the other turtles.
And even here you see it changed to go to the closest one.
All right.
And that's going to continue again and again.
And you can see the turtle name.
So you see my turtle here.
The prefix is my turtle.
Okay.
So that's because we have provided the configuration here my turtle.
And now the cool thing about that is let's say you want to start the application with different parameters.
You just stop.
And we don't even need to go to the code.
So we don't need to go to turtle spawner.
We don't need to go to turtle controller.
We don't even need to go to the launch file.
We just open the config file and we say okay, instead of my turtle I want test And instead of 1.5,
I want let's say very high frequency 5.0.
I'm going to keep this one to true here.
But you see I just for example, changed those settings.
I save then of course I need to build, I need to source and I can run again.
And you see now what is going very fast.
We have five turtles per second.
Okay.
But you see, it just takes a few seconds to change those values.
Save.
And here.
Well, maybe we had too many things.
Okay.
Build and source and run.
All right.
And we start the same application with a complete different set of configs.
And.
Congratulations.
The project is now finished.