WEBVTT

00:00.360 --> 00:06.990
In this laboratory lesson, we will implement a simple python node that can be configured with parameters

00:06.990 --> 00:11.880
and change its behavior based on the assigned values to these parameters.

00:13.320 --> 00:19.710
If you are only interested in the C plus plus development, then you can directly skip to the next lesson

00:19.710 --> 00:23.040
where we will implement the same node in C plus plus.

00:23.460 --> 00:31.110
So let's start by creating a new file inside the Arduino board Pi example in the corresponding folder.

00:31.110 --> 00:37.080
So this sub folder Arduino Pi example where we have already created the code for the simple publisher

00:37.080 --> 00:38.610
and the simple subscriber.

00:38.820 --> 00:46.560
So let's call this new file simple parameter dot pi.

00:46.770 --> 00:57.090
And let's start as always, this node by importing the Pi library and also still from the Pi library

00:57.120 --> 00:58.950
from the node modules.

01:00.300 --> 01:05.580
Let's import the node class and let's use this one to create a new class.

01:06.090 --> 01:08.940
So let's call this one simple

01:11.820 --> 01:20.710
parameter that indeed inherits from the node class that we just imported from the Pi module.

01:21.040 --> 01:25.360
Let's start by defining the constructor of the simple parameter class.

01:25.360 --> 01:29.680
So let's define the init function.

01:31.990 --> 01:37.360
That is the function that is automatically executed when we create an instance of the simple parameter

01:37.360 --> 01:38.040
class.

01:38.050 --> 01:42.520
And here as a first thing, let's call the constructor of the base class.

01:42.520 --> 01:44.680
So the constructor of the node class.

01:45.520 --> 01:51.310
So let's call its init function and let's assign a name to this node.

01:51.340 --> 01:56.560
For example, let's call it simple parameter.

01:57.270 --> 02:00.300
Within the constructor of the simple parameter class.

02:00.300 --> 02:09.780
So here we can declare a new node configuration parameter using the function declare parameter that

02:09.780 --> 02:12.480
we inherited from the node class.

02:12.990 --> 02:18.630
This function takes as input the parameter name, which is an arbitrary name.

02:18.750 --> 02:26.130
And for example, let's call this one simple int param.

02:26.280 --> 02:33.060
And also we can decide to provide a default value which will be assigned to this configuration parameter.

02:33.060 --> 02:36.960
If, for example, no one change it when we started the node.

02:37.050 --> 02:40.110
For example, let's set this 1 to 28.

02:40.890 --> 02:48.030
Similarly, let's also declare a second parameter still with the declare parameter function.

02:48.030 --> 02:58.530
And instead let's call this second one simple string param, and also let's assign a default value.

02:58.560 --> 03:05.490
Let's say this time we want to assign a string and this string says Antonio.

03:06.870 --> 03:13.170
These two instructions are used to declare and initialize a parameter when the node starts up.

03:13.590 --> 03:20.040
However, another important feature of the parameter is that they can also be configured and changed

03:20.040 --> 03:21.450
while the node is running.

03:21.450 --> 03:23.100
So on runtime.

03:23.760 --> 03:30.540
To achieve this we need to use another function still one that we inherited from the node class.

03:30.690 --> 03:32.880
That is the add on.

03:33.090 --> 03:36.660
Set parameters callback.

03:37.590 --> 03:44.820
So with this function we can define a callback function that will be executed whenever one or more of

03:44.820 --> 03:46.290
the parameters that we have declared.

03:46.290 --> 03:49.380
So one of these two basically is changed.

03:50.080 --> 03:53.530
And we want in this case, for example, the function.

03:55.350 --> 03:55.830
Param.

03:57.030 --> 04:01.650
Change callback to be executed.

04:01.650 --> 04:04.500
So when any of these two parameter is changed.

04:04.800 --> 04:11.010
So let's proceed to define this function still as a member of the simple parameter class.

04:11.130 --> 04:13.920
So let's define this function.

04:14.670 --> 04:18.690
And this is still a member of the simple parameter class.

04:19.430 --> 04:22.130
As this is a callback function that is executed.

04:22.130 --> 04:28.610
When a node parameter is changed, it receives exactly the new values of the parameter that have been

04:28.610 --> 04:29.690
just set.

04:29.990 --> 04:34.220
So it receives a new variable called params.

04:35.160 --> 04:42.990
This function also needs to return a response, a result containing the outcome of the parameter change,

04:43.080 --> 04:51.210
and the response must follow a particular standard Ros2 interface, which is declared in the RCL Interface

04:51.210 --> 04:51.840
library.

04:52.020 --> 05:02.640
So from this library, from the RCL interfaces, from the messages of this library, let's import the

05:02.640 --> 05:04.740
set parameter result.

05:05.580 --> 05:12.750
So let's create a new variable called Result, and this is an instance of the set parameter result.

05:13.830 --> 05:17.490
Now within this function for each parameter.

05:17.490 --> 05:23.460
So for which parameter that we requested to change, we simply can print an informative message in the

05:23.460 --> 05:27.440
terminal with the new value that was requested for that parameter.

05:27.450 --> 05:37.650
So let's loop through all the parameters that are in the parameters vector, and now let's check which

05:37.650 --> 05:39.710
was the parameter that changed.

05:39.720 --> 05:50.160
So if the parameter name is equal to simple int param.

05:50.160 --> 05:54.900
So in this case it means that someone is trying to change this parameter of the node.

05:54.900 --> 06:02.130
And so in this case we also expect the new parameter that has been changed to be of type integers since

06:02.130 --> 06:04.290
we set this one to be of type integer.

06:04.380 --> 06:12.450
So in order to do this check, let's import the definition of an integer parameter by importing from

06:12.450 --> 06:20.820
the SQL Pi Library and from the parameter module, let's import the parameter class.

06:20.820 --> 06:27.180
And now let's use this class to verify actually that the parameter type.

06:27.180 --> 06:30.300
Let's take care of adding the underscore.

06:30.300 --> 06:33.810
So the name of the variable is type underscore.

06:34.110 --> 06:37.980
Let's verify that the type is parameter.

06:40.440 --> 06:45.140
Then type integer.

06:45.180 --> 06:51.840
And so in this case, we use the get logger function of the node class to print an informative message.

06:55.390 --> 06:57.100
And let's print.

06:59.750 --> 07:03.140
The message param.

07:04.010 --> 07:09.770
Simple int param changed.

07:10.040 --> 07:13.820
And now let's also print the new value is.

07:14.300 --> 07:19.010
And then let's print the new number that was assigned to the simple int param.

07:20.390 --> 07:25.970
And this is the one that is in the param value.

07:26.860 --> 07:32.890
In this case, let's also set the successful variable of the result message to true.

07:32.920 --> 07:42.940
So let's set the result dot successful and let's set this one to true.

07:44.250 --> 07:45.140
Otherwise.

07:45.150 --> 07:57.840
So if the parameter that was requested to change is the simple string param, in this case, we expect

07:57.840 --> 08:01.790
that the new value that was assigned to this parameter is of type string.

08:01.800 --> 08:03.190
So let's also verify.

08:03.210 --> 08:04.320
So let's double check that.

08:04.320 --> 08:07.620
Actually the new value that was assigned is a string.

08:07.770 --> 08:15.120
So let's check that the param type is of parameter.

08:17.700 --> 08:20.460
Type string.

08:20.640 --> 08:26.520
And so in this case, if both these conditions are met, let's print an informative message in the terminal

08:27.630 --> 08:30.210
with the get logger function.

08:31.350 --> 08:38.850
And then the message says param, simple string param.

08:39.810 --> 08:47.160
Changed and then let's print new value is.

08:49.210 --> 08:57.550
And so let's print the new value that is in the value variable of the parameter object.

08:57.820 --> 08:59.230
So let's print this one.

08:59.230 --> 09:02.860
And also in this case, let's set the result.

09:03.790 --> 09:07.030
Successful variable to true.

09:08.670 --> 09:12.450
Finally at the end of the parameter change callback function.

09:12.450 --> 09:17.640
So here after the for loop, let's return the result message.

09:18.120 --> 09:23.760
So return the result message with the result of the parameter change.

09:23.760 --> 09:25.890
So the one that we set here.

09:27.020 --> 09:31.220
This is all we need for the definition of the simple parameter class.

09:31.220 --> 09:36.830
And now we can proceed to create the main function, which is the one that is automatically called when

09:36.830 --> 09:38.030
the node starts.

09:47.990 --> 09:56.300
So this calls the main function and let's define the main function here.

09:56.300 --> 10:05.500
And let's start the main function by initializing the Ros to communication with our Pi dot init function.

10:05.510 --> 10:08.960
Then let's create a new instance of this simple parameter class.

10:12.640 --> 10:16.800
Let's call this variable simple parameter and let's keep the node up and running.

10:16.810 --> 10:20.830
Use the RCMP spin function.

10:21.010 --> 10:24.520
And so let's spin the simple parameter node.

10:24.820 --> 10:29.920
And finally, if we terminate the execution of the node with Control Z, let's destroy the node.

10:30.190 --> 10:32.020
So simple parameter.

10:34.130 --> 10:40.010
Let's destroy the node and then let's also shut down the Eros two subscriber.

10:40.010 --> 10:44.330
So with our CLP shut down.

10:45.420 --> 10:51.660
With this, we have completed our node, so let's save this script and now we can proceed to install

10:51.660 --> 10:58.230
it by modifying the Setup.py file that is located in the Arduino board Pi Examples package.

10:58.230 --> 10:59.340
So this one.

11:00.080 --> 11:10.340
So let's add a new line here where we can install the simple parameter node, which again is in the

11:10.490 --> 11:11.600
Arduino board.

11:11.760 --> 11:20.690
PY examples folder this time in the simple parameter file.

11:20.690 --> 11:23.480
And again we want to start the main function.

11:23.480 --> 11:28.070
So the entry point where the node will start is the main function.

11:28.900 --> 11:35.560
Finally, before actually building the workspace and running the node, since we have used the RCL interface

11:35.560 --> 11:36.700
library in the script.

11:36.700 --> 11:42.660
So this one we also need to declare this dependency in the package dot XML file.

11:42.670 --> 11:53.170
So here we need to add a new exec dependency from the RCL interfaces.

11:53.320 --> 11:59.420
Now we can save also this file and we can conclude this lesson by building the workspace.

11:59.440 --> 12:01.330
So let's open a new terminal.

12:02.350 --> 12:05.260
Let's go to the workspace and let's run the command.

12:05.260 --> 12:09.430
Click on Build to verify that everything is working fine.

12:09.430 --> 12:16.480
And as there are no errors in the next laboratory lesson after developing the same functionalities in

12:16.480 --> 12:22.630
C plus plus we will start this node and use the Ross to command line interface to read and change the

12:22.630 --> 12:25.450
configuration parameter of a node from the terminal.
