WEBVTT

00:00.050 --> 00:05.780
This is an extra lesson I've added here, because I think the parameter callback functionality is quite

00:05.780 --> 00:06.440
interesting.

00:06.440 --> 00:08.630
You will not use that for all your nodes.

00:08.630 --> 00:14.180
This is something that's often optional, and I'm only going to talk about parameter callbacks in this

00:14.180 --> 00:17.300
bonus video, so at least you know how they work.

00:17.330 --> 00:22.370
And let's start with the problem so you can understand why we need them in the first place.

00:22.370 --> 00:31.310
So I'm going to start my number publisher again Ross to run my example the Python one number publisher.

00:31.310 --> 00:35.720
And well I can set some parameters directly or use the default ones.

00:35.750 --> 00:37.490
I'm just going to use the default ones.

00:37.640 --> 00:49.160
So now if I do Ross two param list and then I do Ross two param get number publisher.

00:49.190 --> 00:51.830
Let's just verify what we have.

00:53.360 --> 01:01.200
So it's two and the other one is should be 1.0 as we set in the code.

01:01.200 --> 01:10.290
And then if I do Ros two topic equal number, we should then publish two every one second, which is

01:10.290 --> 01:10.770
the case.

01:10.770 --> 01:12.540
So no surprise until there.

01:12.540 --> 01:20.760
But then what if I do Ros two param I also have a set functionality, which means that I can change

01:20.790 --> 01:24.750
a parameter value after I have started the node.

01:24.780 --> 01:26.280
Okay, so let's try that.

01:26.850 --> 01:33.120
So we will need to provide first the name of the node then the name of the parameter.

01:33.120 --> 01:36.420
So just like for the get functionality.

01:36.420 --> 01:39.810
But then we need to provide another argument which is the new value.

01:39.810 --> 01:41.070
So let's say four.

01:41.070 --> 01:46.890
So with this I want to change the number parameter here inside the number publisher.

01:46.890 --> 01:49.050
And the value is going to be four.

01:49.680 --> 01:51.510
And that's working.

01:51.510 --> 01:56.520
Now if I get the parameter again you see its value is four.

01:56.550 --> 01:57.060
Great.

01:57.090 --> 01:58.260
Now what's the problem.

01:58.260 --> 02:01.190
Well let's equal call the number topic again.

02:01.190 --> 02:03.590
And you see it's still two.

02:03.620 --> 02:04.490
It's not four.

02:04.520 --> 02:05.180
Why is that?

02:05.180 --> 02:08.390
We have changed the parameter but then it doesn't change in the code.

02:08.420 --> 02:10.010
Let's go back to the code.

02:10.880 --> 02:19.040
Let's open the number publisher and let's understand why this new value is not actually used.

02:19.070 --> 02:25.460
So what we do in the constructor when we start the node we declare the parameter and we get the value

02:25.490 --> 02:27.500
when we start the node.

02:27.530 --> 02:27.920
Okay.

02:27.950 --> 02:34.850
So this value is the one that you either get from the default value or when you do rose to run with

02:34.850 --> 02:35.870
the values here.

02:35.870 --> 02:43.790
But then you see that if the parameter value changes, well, there is no way in the code where we reflect

02:43.790 --> 02:48.230
that there is no way in the code where we say, okay, we receive the new value, so we're going to

02:48.260 --> 02:49.190
use that new value.

02:49.190 --> 02:52.190
So you can change the parameter value.

02:52.190 --> 02:54.980
And it's not going to be used in the code.

02:54.980 --> 03:00.720
If you want to be able to do that then you need to add what's called a parameter callback.

03:00.720 --> 03:02.850
So let's add a parameter callback here.

03:02.850 --> 03:08.280
So after we have declared and the parameters and after we get them we can for example do.

03:08.310 --> 03:12.840
Here I'm going to do this here self dot.

03:12.840 --> 03:19.440
And there is a function called add post set parameters callback.

03:19.440 --> 03:23.340
So the name is quite long but well you can use the auto completion.

03:23.340 --> 03:26.790
And then you need to provide a callback function.

03:27.090 --> 03:33.720
So I'm going to create another callback here def um callback.

03:33.750 --> 03:38.640
Actually let's name it parameters callback.

03:39.030 --> 03:40.980
And this is going to receive.

03:40.980 --> 03:44.670
So we have self and is going to receive a list of parameters.

03:44.670 --> 03:45.810
So we have params.

03:45.810 --> 03:51.450
And I'm going to put an explicit type is going to be a list of parameter objects.

03:51.450 --> 04:04.490
To be able to use parameter I'm going to import here I can do from partial Pi dot parameter import parameter.

04:04.520 --> 04:04.730
Okay.

04:04.760 --> 04:08.810
So that's a different thing than it's not directly from the LCL pi node.

04:08.810 --> 04:11.030
It's from LCL pi parameter.

04:12.530 --> 04:13.160
Okay.

04:13.490 --> 04:16.610
So I provide the type here parameter.

04:17.390 --> 04:19.280
This is just so that it's more explicit.

04:19.280 --> 04:23.420
You don't need to do it, but I like to give explicit types even in Python.

04:24.020 --> 04:25.520
Let's put pass for now.

04:25.520 --> 04:33.830
And we can give the callback function here self dot parameters callback.

04:33.830 --> 04:36.050
So you see in the constructor it's quite easy.

04:36.050 --> 04:42.740
You just add you basically register a new callback that will be triggered whenever something like this

04:42.740 --> 04:43.610
happens.

04:43.640 --> 04:44.180
Okay.

04:44.210 --> 04:47.960
Whenever something like setting a new parameter value happen.

04:47.960 --> 04:53.000
And then you will get to this callback with a list of parameters.

04:53.000 --> 04:57.990
So all the parameters that have been changed because you could also change several parameters at the

04:57.990 --> 04:59.190
same time if you want to.

04:59.220 --> 05:02.610
So what we're going to do here, we are going to use a for loop.

05:02.640 --> 05:06.330
Let's say for param in params.

05:06.960 --> 05:12.390
And then I'm going to do it very basically we can do if param dot.

05:12.390 --> 05:14.670
And you see we have three things.

05:14.670 --> 05:20.010
We have the name, we have the type with an underscore and we have the value.

05:20.010 --> 05:25.830
So if the param name is equal to number okay.

05:25.860 --> 05:28.560
Number is the param name we have used here.

05:29.400 --> 05:37.530
Then we can say self number is equal to the value of the parameter.

05:37.530 --> 05:41.970
So the new value which is param dot value.

05:41.970 --> 05:48.750
And if you want it to do something with the type you also can use param dot and type like this okay.

05:48.780 --> 05:50.370
But we're not going to use it here.

05:51.480 --> 05:54.090
So you can see you do for param in params.

05:54.090 --> 05:58.910
And then if param name is His number and then, well, you can do the same.

05:58.910 --> 06:03.650
You can do if name and you can just do that for every parameter.

06:03.650 --> 06:09.140
So for some parameters like this one, the number that's going to be enough.

06:09.170 --> 06:14.750
You just change the number and then you use the number again in this publisher you might want to validate

06:14.750 --> 06:15.710
the data or not.

06:15.740 --> 06:17.120
It depends here.

06:17.120 --> 06:20.450
We don't here we don't necessarily want to do it.

06:20.480 --> 06:22.670
Well it depends who's going to change the value.

06:22.700 --> 06:24.770
Do you trust the other side for example.

06:24.800 --> 06:27.830
And just to give you an example, I'm not going to add this one here.

06:27.830 --> 06:33.200
But if you were to change the time period here, just changing the time period is not going to be enough.

06:33.200 --> 06:35.570
You would need to stop the timer.

06:35.570 --> 06:44.990
So for example here just to show you if you do self dot and this number timer, you can do cancel for

06:44.990 --> 06:46.070
example to stop it.

06:46.070 --> 06:48.980
Or you can do reset and then you can start it again.

06:48.980 --> 06:51.230
So this you can search on your own.

06:51.230 --> 06:55.980
But just to show you that if you modify a parameter let's say modify the timer period.

06:55.980 --> 06:58.200
I get it inside my parameters callback.

06:58.230 --> 07:01.320
Then it's not just about saving a new variable.

07:01.320 --> 07:07.230
You might need to do more stuff, for example, stopping the timer and creating it again with the new

07:07.260 --> 07:08.430
timer period.

07:08.430 --> 07:14.580
If you were to, let's say you have a camera driver, and then while the camera has disconnected and

07:14.580 --> 07:19.410
you provide the new device name by changing the parameter, maybe you want to restart the node.

07:19.410 --> 07:22.890
Or let's say you want to change the parameter without restarting the node.

07:22.890 --> 07:28.200
Then when you provide the new device name, you will need to do the action of disconnecting the camera

07:28.200 --> 07:29.850
and reconnecting again.

07:29.850 --> 07:35.070
So you see, it's going to depend a lot on what you want to do in your application, but you could have

07:35.070 --> 07:39.120
to process some stuff when you get a new value for a parameter.

07:39.120 --> 07:42.960
And then it doesn't mean that because you can do it, that you should do it.

07:42.990 --> 07:43.470
Okay.

07:43.500 --> 07:46.470
This is used in some nodes or with more experience.

07:46.470 --> 07:52.410
You will see where it's useful to use it, but by default I would start basically any node and not use

07:52.410 --> 07:53.610
the parameter callback.

07:53.640 --> 07:57.890
Okay, you just set the parameters when you start the node, and then later on you see that.

07:57.920 --> 08:01.130
Is it useful to actually change the value later on or not?

08:01.160 --> 08:01.520
All right.

08:01.520 --> 08:03.530
So let's save that.

08:03.530 --> 08:06.740
And now we should be able to run it.

08:06.770 --> 08:08.510
We're going to do the test again.

08:08.510 --> 08:11.000
So I start the node again.

08:12.050 --> 08:12.710
Okay.

08:12.770 --> 08:19.010
Let's check that the number is two.

08:19.430 --> 08:20.720
And then let's do Ros two.

08:20.750 --> 08:23.840
Topic eco is going to publish two.

08:23.870 --> 08:27.800
And then let's set the new value with four.

08:28.280 --> 08:32.150
And as you can see as soon as we set the new value now we are publishing four.

08:32.150 --> 08:34.820
So it is correctly working in the code.

08:34.820 --> 08:38.690
And also of course the parameter is going to be four.

08:38.720 --> 08:39.440
Okay.

08:39.650 --> 08:45.860
So what happened is that this is going to register a callback when the node is spinning.

08:46.190 --> 08:48.740
If we change the value of the parameter.

08:48.740 --> 08:52.970
So that's one event that will trigger this callback.

08:52.970 --> 08:55.920
And then we check Do we have the number parameter if.

08:55.950 --> 09:00.750
Yes, we change the value and then the next time we publish is going to use the new value.

09:00.780 --> 09:01.320
All right.

09:01.320 --> 09:02.640
So that's the Python code.

09:02.640 --> 09:06.000
I have also written the cplusplus code that you can check.

09:06.000 --> 09:09.780
So you can download it in number publisher.

09:09.960 --> 09:16.410
So for Cplusplus we save here post set parameters callback handle.

09:16.410 --> 09:19.680
Then we initialize it here.

09:19.830 --> 09:21.450
You see it's basically the same thing.

09:21.450 --> 09:23.130
We register a callback.

09:23.250 --> 09:30.750
And in this callback we receive a list of parameters which is actually a const reference of a vector.

09:30.750 --> 09:34.710
And I just go through the list and you can see we do the same thing.

09:34.710 --> 09:38.220
If param getname is number, then we get the parameter.

09:38.220 --> 09:39.690
And we also need to provide.

09:39.690 --> 09:43.530
Here you see as int okay for specifying the data type.

09:43.530 --> 09:47.340
So this you can download at the end of this section to see the complete code.

09:47.370 --> 09:50.730
But at the same thing as what we did with Python.

09:50.730 --> 09:53.750
And while that's it just to finish Here.

09:54.290 --> 10:03.500
You can see that if I come back to my services Ros2 service list for this node, you see I have.

10:03.500 --> 10:05.930
So we have used previously list parameters.

10:05.930 --> 10:09.680
You can get parameters, but you can also set parameters.

10:09.680 --> 10:14.390
So that action we did here of changing a parameter from the terminal.

10:14.390 --> 10:18.560
If you want to do that from a node using ros2 communications.

10:18.560 --> 10:22.730
So from another node you could call this service.

10:22.760 --> 10:25.310
And for example change that parameter.

10:25.310 --> 10:30.830
And then if it was implemented here in the parameter callback then you can change.

10:30.860 --> 10:36.380
Basically you can change a parameter for a node from another node.

10:36.410 --> 10:36.830
All right.

10:36.830 --> 10:40.250
And that's the end of this parentheses on parameter callbacks.

10:40.250 --> 10:43.520
So this is something that is good to know it exists.

10:43.550 --> 10:45.200
It's good to understand how it works.

10:45.200 --> 10:47.720
And you're not necessarily going to need it quite soon.

10:47.720 --> 10:52.850
But if you need it in case in the future, then you know you can come back to this lesson to get a quick

10:52.850 --> 10:53.690
refresher.
