This is an extra lesson I've added here, because I think the parameter callback functionality is quite
interesting.
You will not use that for all your nodes.
This is something that's often optional, and I'm only going to talk about parameter callbacks in this
bonus video, so at least you know how they work.
And let's start with the problem so you can understand why we need them in the first place.
So I'm going to start my number publisher again Ross to run my example the Python one number publisher.
And well I can set some parameters directly or use the default ones.
I'm just going to use the default ones.
So now if I do Ross two param list and then I do Ross two param get number publisher.
Let's just verify what we have.
So it's two and the other one is should be 1.0 as we set in the code.
And then if I do Ros two topic equal number, we should then publish two every one second, which is
the case.
So no surprise until there.
But then what if I do Ros two param I also have a set functionality, which means that I can change
a parameter value after I have started the node.
Okay, so let's try that.
So we will need to provide first the name of the node then the name of the parameter.
So just like for the get functionality.
But then we need to provide another argument which is the new value.
So let's say four.
So with this I want to change the number parameter here inside the number publisher.
And the value is going to be four.
And that's working.
Now if I get the parameter again you see its value is four.
Great.
Now what's the problem.
Well let's equal call the number topic again.
And you see it's still two.
It's not four.
Why is that?
We have changed the parameter but then it doesn't change in the code.
Let's go back to the code.
Let's open the number publisher and let's understand why this new value is not actually used.
So what we do in the constructor when we start the node we declare the parameter and we get the value
when we start the node.
Okay.
So this value is the one that you either get from the default value or when you do rose to run with
the values here.
But then you see that if the parameter value changes, well, there is no way in the code where we reflect
that there is no way in the code where we say, okay, we receive the new value, so we're going to
use that new value.
So you can change the parameter value.
And it's not going to be used in the code.
If you want to be able to do that then you need to add what's called a parameter callback.
So let's add a parameter callback here.
So after we have declared and the parameters and after we get them we can for example do.
Here I'm going to do this here self dot.
And there is a function called add post set parameters callback.
So the name is quite long but well you can use the auto completion.
And then you need to provide a callback function.
So I'm going to create another callback here def um callback.
Actually let's name it parameters callback.
And this is going to receive.
So we have self and is going to receive a list of parameters.
So we have params.
And I'm going to put an explicit type is going to be a list of parameter objects.
To be able to use parameter I'm going to import here I can do from partial Pi dot parameter import parameter.
Okay.
So that's a different thing than it's not directly from the LCL pi node.
It's from LCL pi parameter.
Okay.
So I provide the type here parameter.
This is just so that it's more explicit.
You don't need to do it, but I like to give explicit types even in Python.
Let's put pass for now.
And we can give the callback function here self dot parameters callback.
So you see in the constructor it's quite easy.
You just add you basically register a new callback that will be triggered whenever something like this
happens.
Okay.
Whenever something like setting a new parameter value happen.
And then you will get to this callback with a list of parameters.
So all the parameters that have been changed because you could also change several parameters at the
same time if you want to.
So what we're going to do here, we are going to use a for loop.
Let's say for param in params.
And then I'm going to do it very basically we can do if param dot.
And you see we have three things.
We have the name, we have the type with an underscore and we have the value.
So if the param name is equal to number okay.
Number is the param name we have used here.
Then we can say self number is equal to the value of the parameter.
So the new value which is param dot value.
And if you want it to do something with the type you also can use param dot and type like this okay.
But we're not going to use it here.
So you can see you do for param in params.
And then if param name is His number and then, well, you can do the same.
You can do if name and you can just do that for every parameter.
So for some parameters like this one, the number that's going to be enough.
You just change the number and then you use the number again in this publisher you might want to validate
the data or not.
It depends here.
We don't here we don't necessarily want to do it.
Well it depends who's going to change the value.
Do you trust the other side for example.
And just to give you an example, I'm not going to add this one here.
But if you were to change the time period here, just changing the time period is not going to be enough.
You would need to stop the timer.
So for example here just to show you if you do self dot and this number timer, you can do cancel for
example to stop it.
Or you can do reset and then you can start it again.
So this you can search on your own.
But just to show you that if you modify a parameter let's say modify the timer period.
I get it inside my parameters callback.
Then it's not just about saving a new variable.
You might need to do more stuff, for example, stopping the timer and creating it again with the new
timer period.
If you were to, let's say you have a camera driver, and then while the camera has disconnected and
you provide the new device name by changing the parameter, maybe you want to restart the node.
Or let's say you want to change the parameter without restarting the node.
Then when you provide the new device name, you will need to do the action of disconnecting the camera
and reconnecting again.
So you see, it's going to depend a lot on what you want to do in your application, but you could have
to process some stuff when you get a new value for a parameter.
And then it doesn't mean that because you can do it, that you should do it.
Okay.
This is used in some nodes or with more experience.
You will see where it's useful to use it, but by default I would start basically any node and not use
the parameter callback.
Okay, you just set the parameters when you start the node, and then later on you see that.
Is it useful to actually change the value later on or not?
All right.
So let's save that.
And now we should be able to run it.
We're going to do the test again.
So I start the node again.
Okay.
Let's check that the number is two.
And then let's do Ros two.
Topic eco is going to publish two.
And then let's set the new value with four.
And as you can see as soon as we set the new value now we are publishing four.
So it is correctly working in the code.
And also of course the parameter is going to be four.
Okay.
So what happened is that this is going to register a callback when the node is spinning.
If we change the value of the parameter.
So that's one event that will trigger this callback.
And then we check Do we have the number parameter if.
Yes, we change the value and then the next time we publish is going to use the new value.
All right.
So that's the Python code.
I have also written the cplusplus code that you can check.
So you can download it in number publisher.
So for Cplusplus we save here post set parameters callback handle.
Then we initialize it here.
You see it's basically the same thing.
We register a callback.
And in this callback we receive a list of parameters which is actually a const reference of a vector.
And I just go through the list and you can see we do the same thing.
If param getname is number, then we get the parameter.
And we also need to provide.
Here you see as int okay for specifying the data type.
So this you can download at the end of this section to see the complete code.
But at the same thing as what we did with Python.
And while that's it just to finish Here.
You can see that if I come back to my services Ros2 service list for this node, you see I have.
So we have used previously list parameters.
You can get parameters, but you can also set parameters.
So that action we did here of changing a parameter from the terminal.
If you want to do that from a node using ros2 communications.
So from another node you could call this service.
And for example change that parameter.
And then if it was implemented here in the parameter callback then you can change.
Basically you can change a parameter for a node from another node.
All right.
And that's the end of this parentheses on parameter callbacks.
So this is something that is good to know it exists.
It's good to understand how it works.
And you're not necessarily going to need it quite soon.
But if you need it in case in the future, then you know you can come back to this lesson to get a quick
refresher.