In this lesson, you will see how you can save all your parameters for a node or even several nodes
inside only one file that you can load at runtime.
And for this we will use YAML.
So that's why we call the files YAML files.
And first let's understand why you need those.
Well here for example you see I have started my.
So I start my number publisher node with two parameters.
But two parameters is a very small amount of parameters for a node.
You will see later on that you can have dozens of parameters for a node.
So if you need to provide those every time with dash p for each parameter, that's going to be quite
long to do that from the command line.
And also what if you want several configurations for several different nodes, etc.?
Well, it's going to be easier to save those values directly inside one file so that you can just load
the file when you start the node.
All right.
So let's um create I'm going to create a folder here in my home directory.
Let's call it YAML params.
This doesn't really matter at all for now.
It's just you create a folder and we're going to put the YAML files.
And then in the next section on launch files we will see how to actually install those YAML files because
we're going to install them inside your workspace.
Okay.
But for now you just put them anywhere.
It doesn't matter.
And let's create a file called let's say number params dot YAML.
So you make sure you have the YAML extension.
And then it could be number.
So number is because it's the number app kind of and params are number config something like that.
And then we're going to edit this file.
So it's just one file I'm going to use Jedit.
You could also open it with VSCode as you want.
All right.
So what do we write inside this file.
Well first of all let's let's run this just in case.
It's always useful to run the node and see what we have.
So ros2 node list.
You see the node is slash number publisher.
So I'm going to come back to my file and I'm going to write the name of the node number publisher.
Okay.
So I start with this and then I put a colon.
I go back to a new line and I'm going to add an indentation of two spaces.
Then I'm going to write Ross underscore underscore parameters and then another column.
So this is super important.
You write the name of the node the complete name.
And then with an indentation we're going to use two spaces.
You write exactly Ross underscore underscore parameters.
That's going to be the same for every node in your application.
Then you go back to a new line and let's add another indentation of two spaces.
So in total here I should have 1234 spaces.
And then I just provide the values for each parameter one by one one in each line.
So what do we have.
You see we have the number parameter and the timer period.
If I do Ross two parameter list.
Basically those are all the parameters that I can give.
So I could also change the use same time so that the simulation time, for example, if you're using
gazebo or stuff like that for simulation, you could decide to start some nodes using the simulation
time, but it's not really useful for now.
And let's go here.
So you see we can do number and timer period.
So let's put number and then a value.
So we make sure to provide a value that is compatible with the data type we have set in the node.
And let's say timer period of 0.7.
Right.
And then you can just add as many parameters as you want okay.
You will see later on it's not uncommon to see files like this which contains more than 100 lines.
All right.
So let's save that file.
Make sure you save it.
And then what we can do I'm going to stop the node and let's start it again.
But this time I'm not going to provide the parameters value directly.
So I will keep Ross ARGs because I'm going to add arguments, but I'm going to add this one.
So let's put it a bit bigger here.
I'm going to add dash dash and then params file.
And I need to put the path to the file.
So it's in my home directory and then YAML params folder.
And that was number params YAML.
You see I can use the autocompletion here.
And if I run that well first it's working and then let's see what we have simply with the Ross two topic
equal number.
You see we publish five every.
So that was I think 0.7 seconds.
We can do Ross two topic h z with number is going to listen to the topic and you see the rate.
So the rate is 1.4 because the period here is 0.7.
That's exactly what we have specified here in the YAML file.
All right.
So you can see this is quite convenient.
You can put as many values as you want here.
You could even create different YAML files for different configurations that you want to load.
And then the command is the same.
You see if you have 1 or 100 parameters the command is going to be the same.
Now this you can do even more with it because we have just the parameters for one node.
But you can have the parameters for as many nodes as you want.
So let's say we want to run two nodes and we're going to rename them number one and number two.
And the two will have different values for the parameters.
So let's rename that one with one.
And then what I'm going to do is simply I'm just going to take this structure and write the same exact
same structure here with a different name here.
So you see you just add the nodes one by one.
Just make sure that you respect the indentation.
We still have the name of the node Ros parameters.
And then let's change the value here.
Let's put three and the time period is going to be 1.0.
All right let's save that.
And now this YAML file we can load it.
And when we load it the node well the node is going to check.
Do I have one line that corresponds to the node name.
Yes.
If yes I apply the parameters.
So let's try this with an example here.
Let's so clear.
Let's run that node.
But I'm going to also rename it because actually let's run it like this.
And let's see what happens.
You see that now we publish.
So we are back to publishing two every one second.
Why is that.
Because we're going to load the YAML file with this node.
But the node is named number publisher.
We check do we have one node here that is named number publisher.
No.
They each have a different name.
So it's not going to be applied.
So the node name must be exactly one of the nodes that you have in the YAML file.
So that's why I'm going to do here dash r and then node.
And I'm going to name it uh number publisher one for example.
So it's been renamed.
And then you can see we have published five every 0.7 seconds.
Because the number of publisher one node here will get all its parameters from that.
And you can see we load a YAML file with two configurations for two nodes.
But this node is only going to take the configuration for its name okay.
It's going to ignore that one.
So then I can start the second node here.
For example to run my what we can start from the Python package or the C plus plus package.
It doesn't matter number publisher and then ros args.
Let's rename it to Number publisher two okay.
Because we want also to have the parameters here and then params file.
And let's provide the path YAML params and number params dot YAML.
Okay.
You can see the command is the same.
We just changed the name of the node.
Okay it is running.
And now here if I do Ros two topic equal with number.
You see we received three and five at different frequencies.
So both nodes are publishing to the number topic with a different number and a different time period.
So you can see you can put several configurations for several nodes inside one file that you load.
And the file is going to check if the name corresponds to one of the nodes that you've provided here,
and if yes, it's going to load the parameters for that node.