WEBVTT

00:00.080 --> 00:06.410
Now that you know what parameters are, let's create one inside a Python node and see how to use it.

00:06.440 --> 00:11.810
So in this lesson we will directly dive into the code, add the parameters, and then see how to provide

00:11.810 --> 00:13.310
them at runtime.

00:13.310 --> 00:15.860
And for this I'm not going to create a new node.

00:15.860 --> 00:22.700
I'm going to come back to an existing node here to the number publisher node that we have created in

00:22.700 --> 00:25.160
an activity for the topics.

00:25.190 --> 00:25.610
All right.

00:25.640 --> 00:27.860
So make sure you have this node in this node.

00:27.860 --> 00:28.700
What do we do.

00:28.730 --> 00:31.010
Well very simply put we create.

00:31.100 --> 00:32.960
So we have a number variable here.

00:32.990 --> 00:34.790
The number was two by default.

00:34.790 --> 00:36.080
And then we have a publisher.

00:36.080 --> 00:39.560
We have a timer and we publish that number every.

00:39.590 --> 00:41.960
Well you see every one second.

00:41.990 --> 00:42.380
Okay.

00:42.410 --> 00:45.590
So by now you should be quite familiar with this code structure.

00:45.620 --> 00:49.700
Now what I'm going to do is you can see that we publish the number two.

00:49.700 --> 00:51.350
And two is hard coded here.

00:51.350 --> 00:55.760
What if I want to start the node and publish a different number?

00:55.790 --> 01:00.260
Or what if I want to start the node and use a different Around frequency or here.

01:00.260 --> 01:02.240
It's a period for the timer.

01:02.270 --> 01:02.780
Okay.

01:02.810 --> 01:03.920
I will need to.

01:03.950 --> 01:09.320
Well I will need to modify those values directly into the code and then maybe build the package again.

01:09.320 --> 01:11.840
So I'm going to use parameters for those two.

01:11.870 --> 01:12.230
All right.

01:12.230 --> 01:13.520
And how to use parameters.

01:13.520 --> 01:16.430
Well we're going to do that in the constructor here.

01:16.430 --> 01:19.250
And usually we do that before the rest.

01:19.250 --> 01:21.770
So before the publishers the timers etc..

01:21.770 --> 01:25.010
And there are two things you need to do for each parameter okay.

01:25.010 --> 01:26.420
It's a two step process.

01:26.420 --> 01:30.140
You need to declare the parameter and then get the parameter value.

01:30.170 --> 01:32.810
Okay I'm going to come back with more details about this.

01:32.810 --> 01:38.030
So first you need to declare all the parameters that you want with self dot.

01:38.030 --> 01:45.500
So I'm using self to get the functionalities from the node class here declare parameter okay.

01:45.530 --> 01:47.570
You need to give it a name.

01:48.170 --> 01:51.110
So let's put number okay.

01:51.110 --> 01:56.780
So we will have a number parameter inside the number publisher node.

01:57.830 --> 02:02.030
And then there are different ways to create a parameter, but I think the best overall is just to give

02:02.060 --> 02:03.890
a default value.

02:03.890 --> 02:07.490
So for example, the default value is going to be two okay.

02:07.520 --> 02:11.960
That's going to be the default value if we don't provide any parameter as we will see when we start

02:11.960 --> 02:12.530
the node.

02:12.560 --> 02:12.920
All right.

02:12.920 --> 02:15.290
So what this will do.

02:15.320 --> 02:18.710
It will create a number parameter for the node.

02:18.740 --> 02:21.140
And the default value will be two.

02:21.170 --> 02:22.220
But that's not all.

02:22.220 --> 02:26.900
It will also set the data type for the parameter and how it's going to do that.

02:26.930 --> 02:31.280
Well it's going to use the data type you provide for the default value.

02:31.280 --> 02:32.870
So this is very important.

02:32.900 --> 02:34.760
You see this is an integer.

02:34.760 --> 02:39.890
So we have declared a number parameter of type integer.

02:39.890 --> 02:41.840
And the default value is two.

02:41.870 --> 02:43.400
Now let's create another one.

02:43.400 --> 02:48.890
So actually let's declare another one self dot declare parameter.

02:48.890 --> 02:52.940
And let's call it timer period.

02:52.970 --> 02:54.890
That's what we're going to use here.

02:55.040 --> 02:57.860
And this one you see we have a float number.

02:57.890 --> 02:59.690
I'm going to use 1.0.

02:59.690 --> 03:03.290
So we create a time period parameter.

03:03.320 --> 03:06.110
Its default value is 1.0.

03:06.110 --> 03:09.740
And thus this is a float number.

03:09.830 --> 03:10.040
Okay.

03:10.070 --> 03:13.070
So those two parameters have a different data type.

03:13.100 --> 03:13.490
All right.

03:13.490 --> 03:15.740
So we have declared two parameters.

03:15.770 --> 03:16.370
That's great.

03:16.370 --> 03:18.260
But now it's not going to do anything.

03:18.260 --> 03:22.130
So those parameters are going to be created kind of inside the node.

03:22.130 --> 03:27.050
But we don't use them in the code and we don't get the value in the code.

03:27.050 --> 03:30.560
So after you declare a parameter you need to get its value.

03:30.560 --> 03:36.050
And to get the value, we can use the get parameter method from the node class.

03:36.050 --> 03:37.550
And where we do this.

03:37.580 --> 03:47.060
Well for example here I have self number instead of two I will do self dot get parameter like this.

03:47.060 --> 03:48.920
And I will need to provide the name.

03:49.130 --> 03:51.620
And the name is number okay.

03:51.650 --> 03:53.300
That's the exact same name here.

03:53.300 --> 03:57.840
So I declare it and then I get its value, then that's quite important.

03:57.870 --> 04:00.390
This is actually not the value.

04:00.420 --> 04:06.780
This is a parameter object and you need to do dot value okay to get the property.

04:06.780 --> 04:14.400
So to get the value property inside the parameter object from the CLP parameter class.

04:14.400 --> 04:19.560
And very important here you might be tempted to add parentheses but there is no parentheses.

04:19.560 --> 04:20.940
This is not a method.

04:20.940 --> 04:23.070
This is a property okay.

04:23.100 --> 04:24.210
So this is quite tricky.

04:24.210 --> 04:26.250
And this is a source of errors.

04:26.250 --> 04:28.170
Make sure you don't put any parentheses.

04:28.740 --> 04:29.040
Okay.

04:29.070 --> 04:31.380
So we self get parameter.

04:31.470 --> 04:35.850
And then the name of the parameter dot value you get the parameter value.

04:35.850 --> 04:38.580
And I will explain a bit more about this when we run the node.

04:38.610 --> 04:43.260
Now let's let's actually create a new attribute here.

04:43.260 --> 04:46.440
Let's call it timer period.

04:46.440 --> 04:55.320
You could name it as you want self get parameter with timer period Okay.

04:55.500 --> 04:57.810
And then dot value.

04:58.020 --> 04:59.220
As simple as that.

04:59.250 --> 05:01.800
And where do we use that.

05:01.830 --> 05:05.640
Well we use that in here in the create timer function.

05:05.670 --> 05:09.390
So self dot timer period.

05:09.660 --> 05:11.730
So now the code is complete.

05:11.730 --> 05:14.970
As you can see for each parameter we declare it.

05:14.970 --> 05:16.350
And then we get it.

05:16.380 --> 05:19.140
And then well we save it inside an attribute.

05:19.140 --> 05:23.700
And for example we use the self number here to publish the number with the publisher.

05:23.700 --> 05:30.000
And we use the timer period to create the timer period for the timer here to publish the number.

05:30.000 --> 05:32.010
So I'm going to save that.

05:32.010 --> 05:34.410
And let's see let's run it.

05:34.410 --> 05:38.430
So let's see how this works because maybe it's still a bit confusing for you.

05:38.460 --> 05:41.370
When we run it it's going to make much more sense.

05:41.370 --> 05:50.490
So let's go back to our workspace and let's do a call on build just in case with packages.

05:50.490 --> 05:56.430
Select let's build a Python package with Simulink install.

05:58.290 --> 05:58.800
Okay.

05:58.800 --> 06:00.570
And here let's source.

06:01.800 --> 06:11.880
So let's source the workspace and let's run that node Ros to run my pi PG with number publisher.

06:12.150 --> 06:17.100
So I just add it like this Ros to run package executable as always.

06:17.100 --> 06:18.780
And well it's working.

06:18.990 --> 06:19.980
It's the same thing.

06:19.980 --> 06:22.440
So nothing really changed here.

06:22.440 --> 06:28.980
And now let's let's do this here Ros two param list.

06:30.360 --> 06:30.600
Okay.

06:30.630 --> 06:34.980
So you can list all the parameters that exist for a node with Ros two param list.

06:35.010 --> 06:39.240
As you can see we had Ros two service Ros two topics for each functionality.

06:39.240 --> 06:42.150
Basically we have a new Ros2 command line.

06:42.150 --> 06:44.490
And you see we have our number publisher node.

06:44.490 --> 06:46.740
And we have a number parameter.

06:46.740 --> 06:48.870
And also the timer period parameter.

06:48.870 --> 06:50.280
We also have two more parameters.

06:50.280 --> 06:55.290
I'm going to come back to those later, those are going to be started for every node that you create.

06:55.320 --> 06:59.130
Okay, so you can see we already had two parameters and we can add more parameters.

06:59.130 --> 07:06.180
So every time we declare a parameter then you see we have a parameter kind of registered inside the

07:06.180 --> 07:06.780
node.

07:07.050 --> 07:15.840
And now if I do ros2 if I do Ros two topic equal with the number topic, you see that we are going to

07:15.870 --> 07:18.570
publish two every one second.

07:18.600 --> 07:22.590
You could verify the frequency also, but it's one second here.

07:22.590 --> 07:24.120
So what happened.

07:24.270 --> 07:29.520
Well you declare a parameter with a default value okay.

07:29.550 --> 07:31.110
That's going to create the parameter.

07:31.110 --> 07:35.250
So that's why you see the parameter here when you do Ros two paramlist for that node here.

07:35.250 --> 07:41.310
But then we get the parameter value because we haven't provided anything here.

07:41.340 --> 07:46.950
Then we are going to use the default value which is two and one here.

07:46.980 --> 07:52.350
Now let's stop that node And let's run it again.

07:52.350 --> 07:54.480
But this time I'm going to add.

07:54.480 --> 07:57.000
So let's put it a bit bigger here.

07:57.780 --> 08:01.740
I'm going to add dash dash rows args okay.

08:01.770 --> 08:06.780
Just like when we wanted to rename a node, remap a topic or add parameters.

08:06.780 --> 08:10.680
Here you need to add ros args and this only once.

08:10.710 --> 08:16.560
Then if you want to provide a parameter value, you can do dash p and then you provide the name of the

08:16.560 --> 08:22.380
parameter which is number colon equal the value.

08:22.380 --> 08:24.780
So let's say three.

08:24.810 --> 08:25.260
All right.

08:25.260 --> 08:30.630
So what we do here is we start the node and we set the parameter value to three.

08:30.630 --> 08:34.920
So in this case the default value here is not going to be used.

08:34.950 --> 08:37.830
What we're going to use is the value we provide here.

08:37.830 --> 08:41.400
And I can add as many parameters as I want with another dash p.

08:41.580 --> 08:46.290
And once again only once ros args and then as many p as you want.

08:46.320 --> 08:50.850
And the other one is timer Period.

08:51.600 --> 08:55.500
And this is a float number let's say 0.5.

08:55.500 --> 09:00.090
So we want to publish the number three every 0.5 seconds.

09:00.120 --> 09:02.010
Let's press enter.

09:03.030 --> 09:03.480
Okay.

09:03.600 --> 09:05.250
Still working the same.

09:05.250 --> 09:07.470
And let's just verify that with Ros2.

09:07.500 --> 09:09.000
Topic equal number.

09:10.140 --> 09:14.730
And you can see now we publish three every 0.5 seconds.

09:15.090 --> 09:15.600
All right.

09:15.630 --> 09:17.070
Because what happened.

09:17.490 --> 09:23.280
We first declare the parameters so that the number parameter and time period parameter exist.

09:23.280 --> 09:28.440
And then we get the value that we provide at runtime.

09:28.440 --> 09:30.420
You see with this p argument.

09:30.420 --> 09:32.280
So we get the value from there.

09:32.310 --> 09:35.160
And we save that value inside the code.

09:35.160 --> 09:37.050
So then we can use that value.

09:37.080 --> 09:37.470
Okay.

09:37.500 --> 09:38.520
So two step.

09:38.520 --> 09:41.580
Once again you declare a parameter with a default value.

09:41.580 --> 09:43.650
And then you get the parameter value.

09:43.650 --> 09:48.310
And then if you provide the parameter value here that's going to be set in the code.

09:48.310 --> 09:52.810
If you don't, we're going to use the default value so we can.

09:52.840 --> 09:54.190
I'm going to stop that.

09:54.370 --> 09:55.990
And I can run the command again.

09:55.990 --> 09:58.840
But for example just provide one parameter.

09:58.840 --> 10:03.310
If I just provide one then let's see what happens.

10:03.970 --> 10:05.230
We're going to publish the number three.

10:05.230 --> 10:08.620
But you see every one second.

10:09.400 --> 10:11.380
Because if I go back here what's going to happen.

10:11.380 --> 10:12.940
We declare those two parameters.

10:12.940 --> 10:14.590
Then we get this one.

10:14.590 --> 10:16.330
We have provided it from the terminal.

10:16.330 --> 10:18.880
So the number is going to be three.

10:19.000 --> 10:22.750
But then the timer period we haven't provided it in the terminal.

10:22.750 --> 10:26.320
So we just get the default value which is one okay.

10:26.350 --> 10:28.030
So you can see it's quite modular.

10:28.030 --> 10:32.080
And the node now is more dynamic because you can start the node again.

10:32.110 --> 10:33.610
You don't need to compile anything.

10:33.610 --> 10:37.600
You can just start it and provide any value you want.

10:37.630 --> 10:41.410
Then just make sure that you respect the data type.

10:41.440 --> 10:50.980
For example, if I want to put 3.4 for the number, Then you see we get an error because the number

10:51.010 --> 10:56.440
parameter here, as I told you, the type is going to be defined by the default value.

10:56.440 --> 10:58.270
So the type is integer.

10:58.420 --> 11:01.990
And we provide a float number or a double number.

11:01.990 --> 11:09.670
And you can see the error we get is here number of type double expecting type integer.

11:09.670 --> 11:15.850
So make sure that when you start a node and you provide parameters, make sure that you provide a value

11:15.880 --> 11:17.650
that is valid.

11:17.650 --> 11:22.480
So with a correct data type that corresponds to the one you have set in the code.

11:22.480 --> 11:23.830
And now it's working.

11:23.830 --> 11:27.850
And now you can see I'm publishing the number seven.

11:29.290 --> 11:29.740
Great.

11:29.740 --> 11:34.510
And that's basically it for how to use parameters in your code.

11:34.510 --> 11:38.140
You just add one line to declare parameter with a default value.

11:38.140 --> 11:41.080
And then you get the parameter and you can just use it.

11:41.080 --> 11:46.300
And then to provide the value at runtime you just add the value like that.
