WEBVTT

00:00.080 --> 00:03.680
Let's now see how to use parameters in your Cplusplus nodes.

00:03.680 --> 00:09.020
So I encourage you to first watch the previous lesson on Python where I detailed everything.

00:09.020 --> 00:09.290
Here.

00:09.290 --> 00:14.330
I'm going to focus more on the syntax because in the end the parameters will are going to work exactly

00:14.330 --> 00:17.090
the same between Python and Cplusplus.

00:17.090 --> 00:19.070
So I'm going to go back to the code.

00:19.070 --> 00:24.290
Well here we have the Python code and the cplusplus code.

00:24.290 --> 00:27.230
I'm going to add it into the number publishers cpp.

00:27.230 --> 00:33.140
So once again this is the code we have written for an activity with the topics section.

00:33.140 --> 00:34.490
And here what do we have.

00:34.490 --> 00:39.470
Well very simply we have a publisher a timer and we publish a number here.

00:39.470 --> 00:45.200
We publish the number two in this method that we call every one second with this timer.

00:45.200 --> 00:51.500
So now let's use parameters for those values so that we can provide a different value at runtime if

00:51.500 --> 00:51.980
we want.

00:52.160 --> 00:55.790
And there are two things you need to do for each parameter.

00:55.790 --> 01:02.840
You need to first declare it just to say that it exists within the node, and also set a default value.

01:02.840 --> 01:06.110
And then you need to get its value so that you can use it.

01:06.110 --> 01:07.520
And we're going to do that.

01:07.550 --> 01:10.580
What I'm going to do that directly inside the constructor.

01:10.610 --> 01:15.620
Usually I declare the parameters and I get them before I do everything else.

01:15.650 --> 01:17.270
How to declare a parameter.

01:17.300 --> 01:19.100
Well I'm going to use this.

01:19.100 --> 01:21.080
So this is not mandatory.

01:21.080 --> 01:24.650
But again it's to make it explicit that it's from the node class.

01:24.650 --> 01:29.810
And you have a declare parameter method.

01:29.810 --> 01:33.980
Here from the Clcp node class you need to give first a name.

01:33.980 --> 01:35.720
So that's going to be number.

01:35.720 --> 01:39.980
And then you can give a default value let's say two.

01:40.730 --> 01:41.480
All right.

01:41.480 --> 01:44.360
And you have declared one parameter in C plus plus.

01:44.360 --> 01:47.270
So after this line the number parameter will exist.

01:47.300 --> 01:48.800
The default value is two.

01:48.800 --> 01:55.130
And because the default value is two like this that's also going to set the data type for the parameter.

01:55.130 --> 01:57.620
And the parameter is going to be an integer.

01:57.680 --> 01:59.660
Let's declare a second one.

02:00.440 --> 02:02.090
Declare parameter.

02:02.120 --> 02:04.250
Let's call it timer period.

02:05.240 --> 02:08.900
And this one is going to be 1.0.

02:10.130 --> 02:10.460
All right.

02:10.490 --> 02:13.940
And after this those two parameters are declared.

02:13.940 --> 02:18.440
But if we just keep the code like this, well nothing is going to happen because the parameters will

02:18.440 --> 02:22.700
exist kind of within the node, but we don't use them within the code.

02:22.700 --> 02:28.190
So for example, instead of initializing the number here, I'm not going to do that.

02:28.190 --> 02:30.920
I'm going to say number.

02:31.610 --> 02:38.990
So that's the private attribute we have here that we're going to use to publish is equal to this get

02:39.020 --> 02:40.160
parameter.

02:40.490 --> 02:41.990
I need to provide the name.

02:41.990 --> 02:43.280
That's number.

02:43.400 --> 02:47.240
And this will give us a parameter object.

02:47.360 --> 02:49.610
You see an Clcp parameter.

02:49.610 --> 02:52.130
From this we can do dot.

02:52.130 --> 02:55.700
And then you can see in C plus plus it's a bit different than in Python.

02:55.700 --> 03:01.040
In C plus plus we have to do as and then the data type that we want.

03:01.040 --> 03:07.280
So this is an integer we do as int and that's a function.

03:07.280 --> 03:09.110
So we use the parentheses.

03:09.140 --> 03:09.710
All right.

03:09.710 --> 03:12.560
So that's going to get the parameter value as an integer.

03:12.560 --> 03:16.610
And then we set that inside the number which is an integer here.

03:16.610 --> 03:22.160
Then I'm going to do the same for the time period I could create a class attribute.

03:22.160 --> 03:30.590
But I'm just going to create a simple variable here double time period.

03:31.760 --> 03:36.350
And that's going to be this get parameter.

03:37.400 --> 03:39.140
We provide the name.

03:39.140 --> 03:44.150
So that's going to be the same as what we've declared here.

03:44.330 --> 03:49.550
And then we do as double because it's a double number.

03:49.580 --> 03:49.940
All right.

03:49.940 --> 03:52.700
So make sure that you match the type here.

03:52.700 --> 03:59.270
And then well once we have declared the parameters and once we have the values in the code we can use

03:59.270 --> 03:59.480
them.

03:59.480 --> 04:01.940
So number is already used here.

04:01.940 --> 04:03.620
And in the timer here.

04:03.620 --> 04:05.240
Instead of doing that.

04:05.240 --> 04:15.110
I'm gonna want a different ways to write that, but I'm going to do STD Chrono duration and use this

04:15.110 --> 04:18.530
syntax with double type.

04:18.860 --> 04:19.250
Okay.

04:19.280 --> 04:25.970
And I'm going to provide the timer period like that.

04:26.360 --> 04:26.600
Right.

04:26.600 --> 04:31.700
So I create a duration with the timer period that I got as a double from the parameters.

04:31.700 --> 04:32.810
And that's it for the code.

04:32.810 --> 04:37.730
So you see you're going to have a bunch of lines to declare and get the parameters at the beginning

04:37.760 --> 04:39.530
of your node constructor.

04:39.560 --> 04:44.390
Then you just use those values just like normal variables and attributes.

04:44.450 --> 04:46.070
Let's save.

04:46.070 --> 04:48.680
And we're going to go back to the terminal.

04:49.040 --> 04:53.930
Let's build in the raster workspace packages.

04:53.930 --> 04:59.720
Select my CP package.

05:02.420 --> 05:03.050
All right.

05:03.050 --> 05:17.060
And let's source here Source bash RC and let's do a Ros to run my Python package and number publisher.

05:17.570 --> 05:24.890
Actually, if I do the C plus plus code, better to launch the C plus plus code.

05:24.890 --> 05:30.800
So I have just run the number publisher from the C plus plus package, and we can see if I do Ros two

05:30.830 --> 05:34.880
topic equal with the number topic.

05:37.400 --> 05:44.510
We have two published every one second, simply because I haven't provided any value for the parameters

05:44.510 --> 05:47.450
so that the default value is going to be used.

05:47.480 --> 05:48.050
Okay.

05:48.080 --> 05:52.640
So once again when you declare a parameter the parameter just exists.

05:52.640 --> 05:59.210
So it means that when you do Ros two param list you will find the parameter here.

05:59.780 --> 06:01.460
Now you see we have more stuff here.

06:01.460 --> 06:04.280
But you can see we have the number and the timer period.

06:04.280 --> 06:07.670
So they exist and they have a default value.

06:07.790 --> 06:09.320
And then you can get them.

06:09.320 --> 06:13.820
And if you don't provide them at runtime the default value is going to be used.

06:13.850 --> 06:15.800
So now I can do that.

06:15.800 --> 06:16.880
And I can do.

06:16.910 --> 06:27.110
So actually let's clear and then Ross args dash p number let's say 20.

06:27.110 --> 06:37.160
And then another dash p time period which is going to be let's say 3.4 because that's the float number.

06:38.450 --> 06:44.120
And you can see for example here I have an error because I have forgotten a colon here.

06:44.330 --> 06:44.990
Okay.

06:46.070 --> 06:47.660
And now it's working.

06:47.660 --> 06:54.350
If I do Rostopic echo it's going to publish the number 20 every 3.4 seconds.

06:54.350 --> 06:56.960
And you can see we have the number here.

06:57.020 --> 06:58.160
So it's working.

06:58.160 --> 07:02.810
And one more thing we can test is what if you provide a parameter that doesn't exist.

07:02.840 --> 07:07.370
So let's say you have a typo or maybe you provide a parameter that.

07:07.370 --> 07:13.550
So this param doesn't exist with a value?

07:13.580 --> 07:14.030
I don't know.

07:14.060 --> 07:14.720
Three.

07:14.750 --> 07:16.580
Let's say you want to provide a string.

07:17.000 --> 07:19.430
So that's also not a type.

07:19.730 --> 07:21.020
What's going to happen.

07:21.260 --> 07:24.470
You see that it's still working.

07:24.500 --> 07:30.770
And if I do Rosetta param list, the parameter that we have provided here doesn't appear.

07:30.800 --> 07:31.340
Okay.

07:31.340 --> 07:37.940
So basically you can provide any parameter that you want even if it's not declared in this case it's

07:37.940 --> 07:39.320
just going to be ignored.

07:39.350 --> 07:39.890
Okay.

07:39.920 --> 07:45.590
If the parameter is not declared in the code and if you don't provide the exact same value, then you

07:45.590 --> 07:48.920
see it's not taken into account anyway.

07:48.920 --> 07:51.710
It doesn't appear in the parameter list.

07:51.740 --> 07:52.130
All right.

07:52.130 --> 07:59.870
So make sure also that when you provide the parameter name and value that this is exactly the same name

07:59.870 --> 08:03.590
that you have set here when you declare the parameter.

08:04.250 --> 08:04.640
All right.

08:04.640 --> 08:07.310
And that's it for parameters for C plus plus.

08:07.310 --> 08:09.710
So now you know how to use them in your nodes.
