Now that you know what parameters are, let's create one inside a Python node and see how to use it.
So in this lesson we will directly dive into the code, add the parameters, and then see how to provide
them at runtime.
And for this I'm not going to create a new node.
I'm going to come back to an existing node here to the number publisher node that we have created in
an activity for the topics.
All right.
So make sure you have this node in this node.
What do we do.
Well very simply put we create.
So we have a number variable here.
The number was two by default.
And then we have a publisher.
We have a timer and we publish that number every.
Well you see every one second.
Okay.
So by now you should be quite familiar with this code structure.
Now what I'm going to do is you can see that we publish the number two.
And two is hard coded here.
What if I want to start the node and publish a different number?
Or what if I want to start the node and use a different Around frequency or here.
It's a period for the timer.
Okay.
I will need to.
Well I will need to modify those values directly into the code and then maybe build the package again.
So I'm going to use parameters for those two.
All right.
And how to use parameters.
Well we're going to do that in the constructor here.
And usually we do that before the rest.
So before the publishers the timers etc..
And there are two things you need to do for each parameter okay.
It's a two step process.
You need to declare the parameter and then get the parameter value.
Okay I'm going to come back with more details about this.
So first you need to declare all the parameters that you want with self dot.
So I'm using self to get the functionalities from the node class here declare parameter okay.
You need to give it a name.
So let's put number okay.
So we will have a number parameter inside the number publisher node.
And then there are different ways to create a parameter, but I think the best overall is just to give
a default value.
So for example, the default value is going to be two okay.
That's going to be the default value if we don't provide any parameter as we will see when we start
the node.
All right.
So what this will do.
It will create a number parameter for the node.
And the default value will be two.
But that's not all.
It will also set the data type for the parameter and how it's going to do that.
Well it's going to use the data type you provide for the default value.
So this is very important.
You see this is an integer.
So we have declared a number parameter of type integer.
And the default value is two.
Now let's create another one.
So actually let's declare another one self dot declare parameter.
And let's call it timer period.
That's what we're going to use here.
And this one you see we have a float number.
I'm going to use 1.0.
So we create a time period parameter.
Its default value is 1.0.
And thus this is a float number.
Okay.
So those two parameters have a different data type.
All right.
So we have declared two parameters.
That's great.
But now it's not going to do anything.
So those parameters are going to be created kind of inside the node.
But we don't use them in the code and we don't get the value in the code.
So after you declare a parameter you need to get its value.
And to get the value, we can use the get parameter method from the node class.
And where we do this.
Well for example here I have self number instead of two I will do self dot get parameter like this.
And I will need to provide the name.
And the name is number okay.
That's the exact same name here.
So I declare it and then I get its value, then that's quite important.
This is actually not the value.
This is a parameter object and you need to do dot value okay to get the property.
So to get the value property inside the parameter object from the CLP parameter class.
And very important here you might be tempted to add parentheses but there is no parentheses.
This is not a method.
This is a property okay.
So this is quite tricky.
And this is a source of errors.
Make sure you don't put any parentheses.
Okay.
So we self get parameter.
And then the name of the parameter dot value you get the parameter value.
And I will explain a bit more about this when we run the node.
Now let's let's actually create a new attribute here.
Let's call it timer period.
You could name it as you want self get parameter with timer period Okay.
And then dot value.
As simple as that.
And where do we use that.
Well we use that in here in the create timer function.
So self dot timer period.
So now the code is complete.
As you can see for each parameter we declare it.
And then we get it.
And then well we save it inside an attribute.
And for example we use the self number here to publish the number with the publisher.
And we use the timer period to create the timer period for the timer here to publish the number.
So I'm going to save that.
And let's see let's run it.
So let's see how this works because maybe it's still a bit confusing for you.
When we run it it's going to make much more sense.
So let's go back to our workspace and let's do a call on build just in case with packages.
Select let's build a Python package with Simulink install.
Okay.
And here let's source.
So let's source the workspace and let's run that node Ros to run my pi PG with number publisher.
So I just add it like this Ros to run package executable as always.
And well it's working.
It's the same thing.
So nothing really changed here.
And now let's let's do this here Ros two param list.
Okay.
So you can list all the parameters that exist for a node with Ros two param list.
As you can see we had Ros two service Ros two topics for each functionality.
Basically we have a new Ros2 command line.
And you see we have our number publisher node.
And we have a number parameter.
And also the timer period parameter.
We also have two more parameters.
I'm going to come back to those later, those are going to be started for every node that you create.
Okay, so you can see we already had two parameters and we can add more parameters.
So every time we declare a parameter then you see we have a parameter kind of registered inside the
node.
And now if I do ros2 if I do Ros two topic equal with the number topic, you see that we are going to
publish two every one second.
You could verify the frequency also, but it's one second here.
So what happened.
Well you declare a parameter with a default value okay.
That's going to create the parameter.
So that's why you see the parameter here when you do Ros two paramlist for that node here.
But then we get the parameter value because we haven't provided anything here.
Then we are going to use the default value which is two and one here.
Now let's stop that node And let's run it again.
But this time I'm going to add.
So let's put it a bit bigger here.
I'm going to add dash dash rows args okay.
Just like when we wanted to rename a node, remap a topic or add parameters.
Here you need to add ros args and this only once.
Then if you want to provide a parameter value, you can do dash p and then you provide the name of the
parameter which is number colon equal the value.
So let's say three.
All right.
So what we do here is we start the node and we set the parameter value to three.
So in this case the default value here is not going to be used.
What we're going to use is the value we provide here.
And I can add as many parameters as I want with another dash p.
And once again only once ros args and then as many p as you want.
And the other one is timer Period.
And this is a float number let's say 0.5.
So we want to publish the number three every 0.5 seconds.
Let's press enter.
Okay.
Still working the same.
And let's just verify that with Ros2.
Topic equal number.
And you can see now we publish three every 0.5 seconds.
All right.
Because what happened.
We first declare the parameters so that the number parameter and time period parameter exist.
And then we get the value that we provide at runtime.
You see with this p argument.
So we get the value from there.
And we save that value inside the code.
So then we can use that value.
Okay.
So two step.
Once again you declare a parameter with a default value.
And then you get the parameter value.
And then if you provide the parameter value here that's going to be set in the code.
If you don't, we're going to use the default value so we can.
I'm going to stop that.
And I can run the command again.
But for example just provide one parameter.
If I just provide one then let's see what happens.
We're going to publish the number three.
But you see every one second.
Because if I go back here what's going to happen.
We declare those two parameters.
Then we get this one.
We have provided it from the terminal.
So the number is going to be three.
But then the timer period we haven't provided it in the terminal.
So we just get the default value which is one okay.
So you can see it's quite modular.
And the node now is more dynamic because you can start the node again.
You don't need to compile anything.
You can just start it and provide any value you want.
Then just make sure that you respect the data type.
For example, if I want to put 3.4 for the number, Then you see we get an error because the number
parameter here, as I told you, the type is going to be defined by the default value.
So the type is integer.
And we provide a float number or a double number.
And you can see the error we get is here number of type double expecting type integer.
So make sure that when you start a node and you provide parameters, make sure that you provide a value
that is valid.
So with a correct data type that corresponds to the one you have set in the code.
And now it's working.
And now you can see I'm publishing the number seven.
Great.
And that's basically it for how to use parameters in your code.
You just add one line to declare parameter with a default value.
And then you get the parameter and you can just use it.
And then to provide the value at runtime you just add the value like that.