WEBVTT

00:00.640 --> 00:08.050
In this laboratory lesson, we will implement a simple cplusplus node that can be configured with parameters

00:08.050 --> 00:12.550
and change its behavior based on the assigned values to those parameters.

00:13.090 --> 00:19.210
Let's create a new file inside the source folder of the Arduino CPP examples package.

00:19.300 --> 00:25.870
So here where we have already inserted the simple publisher and the simple subscriber node, and let's

00:25.870 --> 00:30.670
call this new file simple parameter dot CPP.

00:31.360 --> 00:35.230
Let's start by importing the RTL CPP library.

00:35.890 --> 00:49.600
So let's include Lclc-rp and let's use it to create a new class called Simple parameter that inherits

00:50.680 --> 00:54.790
from the RTL CPP node class.

00:59.120 --> 01:02.020
Among the public attributes of this class.

01:02.030 --> 01:06.680
Let's begin by defining the constructor so public.

01:07.040 --> 01:08.480
Let's define the constructor.

01:08.480 --> 01:11.330
So the function that has the same name of the class.

01:11.630 --> 01:17.150
So simple parameter and that is executed automatically at the startup.

01:17.360 --> 01:21.860
And so when we create a new instance of the simple parameter class.

01:22.730 --> 01:24.680
Here as our first step.

01:24.680 --> 01:27.640
Let's invoke the constructor of the base class.

01:27.650 --> 01:34.250
So let's call the constructor of the Node class and we need to pass a name of this node.

01:34.250 --> 01:40.100
So for example, let's call it simple parameter.

01:41.200 --> 01:42.700
Within the constructor.

01:42.700 --> 01:50.320
So here in the body of the constructor, we can declare a new node configuration parameter using the

01:51.160 --> 01:56.740
declare parameter function which we have inherited from the node class.

01:57.370 --> 01:59.740
So let's use this function.

01:59.740 --> 02:06.220
This is our template function, and among the angular brackets, we need to declare the type of the

02:06.220 --> 02:08.440
parameter that we want to use for this node.

02:08.770 --> 02:15.670
For example, for this first parameter, this is an integer and between the parentheses of the function.

02:15.940 --> 02:23.320
So here we need to pass also the name of the parameter, which is an arbitrary name and can be chosen

02:23.320 --> 02:24.360
as desired.

02:24.370 --> 02:30.790
For example, let's call it simple int param.

02:31.830 --> 02:38.430
We can also provide a default value for this parameter that will be assigned to the parameter simple

02:38.430 --> 02:39.230
int param.

02:39.240 --> 02:43.170
If at the startup of the node, no one is setting it.

02:43.170 --> 02:46.530
So for example, let's set this 1 to 28.

02:46.890 --> 02:52.890
Similarly, we can also declare a second parameter still with the declare parameter function.

02:53.700 --> 02:57.390
And this time let's declare this parameter as a string.

02:57.390 --> 03:09.810
So first let's include the string module from C plus plus, and then let's use this one to declare that

03:09.810 --> 03:14.070
we are using a string from the C plus plus standard library.

03:14.490 --> 03:22.830
And we want to call this one simple string param and by default.

03:22.830 --> 03:32.490
So if no one sets a value for this parameter, when the node starts, we assign it to be Antonio.

03:33.400 --> 03:40.210
These two instructions are used to declare and initialize the parameters at the node startup.

03:40.420 --> 03:46.750
However, another important feature of parameters is that they can be configured and changed during

03:46.750 --> 03:47.800
the node execution.

03:47.800 --> 03:50.530
So at runtime while the node is running.

03:51.290 --> 04:00.350
To achieve this, we use the add on set parameter callback function that still we are inheriting from

04:00.350 --> 04:01.640
the node class.

04:02.240 --> 04:08.690
And with this function we can define a callback function that will be executed whenever one or more

04:08.690 --> 04:11.600
parameters that are declared in the node are changed.

04:11.870 --> 04:18.650
For example, when the simple int parameter or the simple string parameter are changed, we want a certain

04:18.650 --> 04:22.700
function to be executed in order to handle this change.

04:23.120 --> 04:31.220
To do so, we use the bind function from the standard CPP library and we want to use this one to execute

04:31.220 --> 04:42.590
the param change callback function that we are going to still declare within the simple parameter class.

04:43.370 --> 04:47.120
So let's declare this one to be a member of the simple parameter class.

04:47.150 --> 04:52.440
Let's also use the pointer this to indicate that we want to use the current version.

04:52.440 --> 04:57.150
So the implementation of the parameter change callback function of the current object.

04:57.150 --> 05:03.600
And then let's also declare the usage of the placeholders.

05:03.960 --> 05:12.870
And let's just use the placeholder number one, to indicate that basically here this function will receive

05:13.140 --> 05:14.850
just one input.

05:14.850 --> 05:17.520
So we will receive just one parameter as input.

05:18.170 --> 05:20.410
Now among the private.

05:20.420 --> 05:28.580
So here among the private members of this function, we need first to create a pointer to an object

05:28.610 --> 05:32.480
of type on set parameter callback endl.

05:32.720 --> 05:37.940
So this is on set parameter callback Endl.

05:38.240 --> 05:48.290
Let's create a shared pointer to this object and let's call param callback endl.

05:50.310 --> 05:58.290
Inside this object, we can store the output of this variable here so we can store the output of the

05:58.290 --> 06:01.200
ad onset parameter change callback.

06:01.200 --> 06:06.810
And now we can finally proceed to declare the param change callback function.

06:06.810 --> 06:13.230
We can do so still among the private functions or the private members of the simple parameter class.

06:13.230 --> 06:20.700
So let's declare this function here and this function receives as input a vector.

06:20.700 --> 06:23.580
So let's start by importing.

06:24.660 --> 06:28.860
So let's include the vector module.

06:29.040 --> 06:40.050
And so let's use this to declare that it receives a standard vector as input and the type of this vector

06:40.050 --> 06:41.400
is RCL.

06:43.310 --> 06:45.790
CP parameter.

06:45.800 --> 06:49.430
So it receives a vector of parameters that have changed.

06:49.610 --> 06:53.360
And let's call this one parameters.

06:53.360 --> 06:55.490
So let's call this variable parameters.

06:55.520 --> 07:00.140
Also, we need to declare the return type of this function and this one.

07:00.140 --> 07:03.680
So the parameter change callback must return a response.

07:03.680 --> 07:07.610
So our result containing the outcome of the change.

07:07.610 --> 07:10.040
So whether it was successful or not.

07:10.590 --> 07:18.890
This response must adhere to a specific two standard interface which is declared in the RCL interface.

07:18.900 --> 07:28.500
So let's include this interface from the RCL interfaces from the messages.

07:28.650 --> 07:33.750
Let's include the set parameter result.

07:34.490 --> 07:38.630
And now let's use this one as our return type of this function.

07:38.630 --> 07:50.360
So from the SQL interfaces, from the messages, let's use the set parameter result so it will return

07:50.390 --> 07:52.100
an object of this type.

07:52.540 --> 07:53.530
In this function.

07:53.530 --> 07:58.030
Let's start just by creating exactly an object of this type.

07:58.030 --> 07:59.770
So let's take this one.

08:00.310 --> 08:04.060
Let's paste it and let's call this one result.

08:04.090 --> 08:07.120
This is also the object that we need to return.

08:07.120 --> 08:11.770
When we finished with the execution of the param change callback function.

08:12.220 --> 08:18.490
Now, in this function for each parameter that has changed, we simply want to print an informative

08:18.490 --> 08:24.220
message in the terminal with the new value that was assigned to those parameters.

08:24.460 --> 08:31.900
So let's create a for loop that is iterating through all the parameters.

08:32.290 --> 08:40.810
So for each parameter within the vector of parameters that we have received as input, what we want

08:40.810 --> 08:44.290
to do is first check, which is the parameter that has changed.

08:44.290 --> 08:48.760
So which are the parameters among the one that we declared that have changed.

08:49.120 --> 09:03.580
So if the parameter dot get name, so it's equal to simple int parameter.

09:03.580 --> 09:10.420
So if someone requested to change the simple int parameter in this case, let's also check that it was

09:10.420 --> 09:13.240
changed with an object of the same type.

09:13.240 --> 09:14.740
So of type integer.

09:14.740 --> 09:24.190
So let's also check that the param type is of type rcl, cp.

09:25.890 --> 09:29.550
Parameter type.

09:29.820 --> 09:33.870
And from this one we take the parameter integer.

09:34.020 --> 09:41.250
So if both these conditions are met, this means that the simple int parameter has been changed with

09:41.250 --> 09:42.850
a new valid value.

09:42.870 --> 09:49.530
So in this case, let's print an informative message in the terminal that says with the function rcl

09:49.920 --> 09:53.400
cp info stream.

09:53.580 --> 10:09.810
So let's use the get logger function and then let's print the message param simple int param changed

10:10.290 --> 10:18.180
and then the new value is and then let's print the new value that is basically param.

10:19.970 --> 10:20.450
Isn't.

10:24.120 --> 10:30.090
And so this way we have in the terminal the new value that was assigned to the symbol int parameter.

10:30.450 --> 10:42.410
Now let's also assign in this case a value for the result variable, and let's set the success to true

10:42.420 --> 10:46.410
since the parameter was correctly changed and displayed in the terminal.

10:46.950 --> 10:52.890
Otherwise, if the parameter that has been changed is the symbol string parameter.

10:53.010 --> 11:08.760
So if the parameter get name is the symbol string param also in this case we need to change this one

11:09.000 --> 11:12.180
to simple int param as we called here.

11:12.180 --> 11:17.150
The declared parameter is the simple int param and the second one is the simple string param.

11:17.160 --> 11:23.700
So let's check also that the new value that has been assigned to the simple string param is valid,

11:23.710 --> 11:25.830
so is of type string.

11:25.840 --> 11:30.040
So let's check that the param gets type.

11:32.840 --> 11:44.840
Is an RCL CP parameter type of type parameter string.

11:44.960 --> 11:50.540
And so if both the conditions are met, let's print an informative message in the terminal.

11:50.810 --> 12:01.040
So still with the CP info stream function, let's use the get logger function.

12:01.370 --> 12:09.470
And let's print the message param simple string param changed.

12:09.650 --> 12:13.610
And then let's print also the new value that has been assigned.

12:14.180 --> 12:23.030
And so this is the param as string this time since this is a string message.

12:23.360 --> 12:26.480
And also in this case, let's set the result.

12:27.470 --> 12:30.500
Successful variable to true.

12:31.070 --> 12:35.120
Finally at the end of the parameter change callback function.

12:35.390 --> 12:41.420
So here let's also return the result variable.

12:41.420 --> 12:48.770
So the object of type set parameter result that is the output also of the parameter change callback

12:48.770 --> 12:49.430
function.

12:50.120 --> 12:54.330
Now this is all we need for the definition of the simple parameter class.

12:54.350 --> 13:00.110
And so we can proceed to create the main function, which is the one that is automatically executed

13:00.110 --> 13:01.670
when the node starts.

13:01.670 --> 13:03.860
So int main.

13:15.540 --> 13:23.910
And here, let's start by initializing the communication with Ros two with the init function that receives

13:23.910 --> 13:26.010
as input the argument of the main.

13:27.810 --> 13:33.060
And then let's create a new shared pointer to an object of the simple parameter class.

13:33.060 --> 13:42.660
So let's first include the memory module and let's use it to create a new node.

13:42.930 --> 13:45.360
So a new variable of type node.

13:45.360 --> 13:56.970
And then let's use the make shared function to create a new object of type simple parameter.

13:56.970 --> 14:01.110
And now we can keep this node up and running with the spin function.

14:01.110 --> 14:06.960
So keep spin to keep the node running.

14:06.960 --> 14:12.480
And also if we terminate the execution of the node with control Z, let's shut down Ros.

14:12.480 --> 14:16.310
So lclc-rp shut down.

14:17.690 --> 14:24.140
With this we have completed our node, so let's save this file and now we can proceed to install it

14:24.140 --> 14:31.250
by modifying the file CMake list that is located into the Arduino board CP Examples package.

14:31.490 --> 14:35.240
So as a first thing, let's add a new dependency.

14:35.240 --> 14:40.220
So with the find package instruction from the.

14:41.020 --> 14:44.640
Our SQL interface is a library that we used in our script.

14:44.970 --> 14:50.610
So let's declare the usage of this library, and then let's also add the new executable.

14:50.610 --> 14:57.660
So the simple parameter node to so we can copy the same instruction that we used for the simple publisher

14:57.660 --> 14:58.670
and subscriber.

14:58.680 --> 15:04.260
Let's just change the name of the executable to simple parameter.

15:04.890 --> 15:09.930
And also the name of the source code is the simple parameter dot CPP.

15:10.710 --> 15:17.730
Let's also change the name into the target dependencies instruction and here the libraries that it is

15:17.730 --> 15:20.010
going to need are the CPP.

15:20.550 --> 15:26.430
And instead of the standard messages, this is going to need the RCL Interfaces library.

15:26.880 --> 15:30.060
And then finally, let's also add this executable.

15:30.060 --> 15:34.440
So the simple parameter among the list of install.

15:34.440 --> 15:39.630
So this will be installed alongside with the simple publisher and the simple subscriber.

15:40.050 --> 15:42.070
Let's save also this file.

15:42.070 --> 15:48.840
And finally, before building our workspace and so before running the node, since we used and we declared

15:48.840 --> 15:55.420
the usage of the RCL interfaces inside the Cmakelist, we also need to declare it within the package

15:55.420 --> 15:56.650
dot XML.

15:56.680 --> 16:03.400
So here let's add a new dependency from the RCL interfaces.

16:03.400 --> 16:05.170
We can save also this file.

16:05.170 --> 16:08.820
And to conclude this lesson, let's build our workspace.

16:08.830 --> 16:16.240
So in the terminal, let's go to the workspace and let's build it to verify that actually the build

16:16.240 --> 16:23.800
is successful and there are no errors in the next laboratory lesson, we will start this node and use

16:23.800 --> 16:30.460
the two command line interface to read and change the configuration parameters of a node from the terminal.
