Now that you have seen renaming and remappings in this lesson, you will learn how to set parameters
values to your nodes inside a launch file.
We will first set the parameters one by one, and then load them directly from a YAML file that we will
also install inside the Myrobot bringup package.
Let's get started.
So if you remember from the command line so I can stop all that from the command line when we start
a node with Ros to run, we have also Ros args.
And then we can do dash p param name.
And that's going to be value okay.
So how do we add this inside a launch file.
Well let's go back and well where do we have parameters.
Actually we don't have any parameter in my number counter.
So I'm going to well we have the default ones but we haven't added ones in the code.
I'm going to leave this one like that.
But in number publisher we have two parameters okay.
So if I come back to the code number publisher?
You see, we have the number parameter and the timer period.
So let's add those parameters.
Let's add values for those.
Where do we do this.
Well you do this inside the node tag.
So you need to open and close the node tag if it hasn't been done yet.
And then we can add a param tag.
Okay.
So we have the remappings and then we have the param tags.
And for each param tag you have name and value arguments.
So quite easy here name is going to be number.
And the value let's say six.
And that's going to set the parameter number with the value six.
When you start the node with the launch file okay.
And if you want to add more params you add another param tag name.
That one is timer period.
Make sure it's the correct name value.
Let's say 1.5 seconds and make sure you provide the correct data type as well.
All right.
And that's it for the parameters here.
Let's save and then let's build again.
So let's do a click on build let's source.
And let's start the number app launch dot XML.
So you see to start and to test launch files is quite easy.
It's quite convenient to have one terminal here where you just build source and launch in that order.
So okay it's working.
And now we can just check actually what we have on the number topic.
Ross two topic equal number.
Actually it's not a number.
It's it's my number because if you remember, we have remapped the topic.
So what do we have on my number?
We have six published every word.
You can check the frequency but it's going to be every 1.5 second.
Great.
And we can also do rows to param list and see that we have plus two param get with my number publisher
and number.
We have six.
And for the time period we have 1.5 which is exactly what we have set in the launch file.
Okay so it's quite easy now.
Well that's good if you have only a few parameters.
But let's say you have many parameters.
And let's say you've already created a YAML file or you want to create a new one.
Where are you going to put your YAML files?
Because for now, if you remember, we have just added I have a YAML params folder here in my home directory,
and we have the two YAML files that we have created here number params and led config.
Well, now that we have this bringup package here, we're not just going to create the YAML files anywhere
in our computer, we're going to add them inside the package.
And for that I'm going to go back to.
So let's go to our My Robot Bringup package.
And we have a launch folder.
I'm going to create a new one that will usually we name it config.
It could be params.
It could be whatever you want.
But usually config is a quite common name.
And now you have a launch and a config folder.
Okay it's here.
And in this config folder you are going to add your YAML files.
And there's one thing we need to do to set up this config folder.
We go back to the cmakelists.txt and you see here we say to install the launch folder inside this directory.
After launch you're going to put a space and then config.
So now we say to install the launch folder and the config folder.
So any new folder that you add in the Myrobot bringup that you want to install, you just add a space
and then you put the name of the folder.
So make sure you add the config folder here.
Very important.
And then let's add a new YAML file.
So let's call it number app dot YAML for example.
And now let's provide the parameters.
So we want to add the number parameter and the timer parameter for the my number publisher okay.
You see the name must match.
So I'm going to write my number publisher.
And then two spaces Ross underscore underscore parameters and then more spaces here number.
Well let's use the same values and timer period 1.5 okay I'm going to save that file.
So this is what we've seen on the previous section on parameters.
If you have some doubt you can come back to the to the lesson on YAML files.
And now that we have this YAML file here inside the config folder, we can load it directly here inside
this launch file.
And how do we do this.
Well first I'm going to actually comment those lines.
So here I use control shift and slash.
I'm going to comment those lines so that I don't write the parameters manually.
But I still leave the code so that you can see it.
How to say that we want to load those parameters from this launch file.
Well you are going to add a param tag as well, but instead of name and value you are going to write
from, and that's going to be the path to the YAML file.
And how do we find this?
Well, we're going to use the function.
We have used the same function in the in this XML file to include another launch file we are going to
use.
So dollar sign parentheses bind pkg share and the name of the package my robot bring up.
So it could be from this package.
It could be from another package.
And then slash where is the file.
Well in config slash number app dot YAML okay.
And then we finish.
We close the tag.
So once again find pkg share with the package name in parentheses with a dollar sign.
And then slash the path.
So the relative path here to the YAML file.
And that's going to load the YAML file.
And in this YAML file we have you see parameters for my number publisher which is going to work with
this.
And so you see if you have 200 parameters then you add your 200 parameters here.
And your launch file is still super small.
Let's save that.
And let's uh, let's just test it.
So we build again, we source again and we run again and it's working I guess.
No surprise.
Time period and number, you see still the same.
But this time we load them from the YAML file.
And here is what you have to do in case you have to add parameters inside the Python launch file you
see inside the node.
Here we have another parameters argument, which is a list, but this time we use curly brackets.
Okay, so make sure that you don't make the confusion here.
So this is like a dictionary but this is a tuple for remappings.
Okay.
So not the same thing.
And then you can just put one parameter on each line just like that.
So not too complicated.
And that's the code.
If you want to load a YAML file inside a Python launch file.
So we are still using the same YAML file.
Okay from this package.
But you can see you need to import get package shared directory from element index python packages and
then import OS.
You can have the path here for the parameter config.
And then you can add the parameter here just like that.
So just in case that's how you do it in Python.
But of course I recommend you do that in XML.
You just have to remember find pkg share and that's it.