In this laboratory lesson, we will implement a simple cplusplus node that can be configured with parameters
and change its behavior based on the assigned values to those parameters.
Let's create a new file inside the source folder of the Arduino CPP examples package.
So here where we have already inserted the simple publisher and the simple subscriber node, and let's
call this new file simple parameter dot CPP.
Let's start by importing the RTL CPP library.
So let's include Lclc-rp and let's use it to create a new class called Simple parameter that inherits
from the RTL CPP node class.
Among the public attributes of this class.
Let's begin by defining the constructor so public.
Let's define the constructor.
So the function that has the same name of the class.
So simple parameter and that is executed automatically at the startup.
And so when we create a new instance of the simple parameter class.
Here as our first step.
Let's invoke the constructor of the base class.
So let's call the constructor of the Node class and we need to pass a name of this node.
So for example, let's call it simple parameter.
Within the constructor.
So here in the body of the constructor, we can declare a new node configuration parameter using the
declare parameter function which we have inherited from the node class.
So let's use this function.
This is our template function, and among the angular brackets, we need to declare the type of the
parameter that we want to use for this node.
For example, for this first parameter, this is an integer and between the parentheses of the function.
So here we need to pass also the name of the parameter, which is an arbitrary name and can be chosen
as desired.
For example, let's call it simple int param.
We can also provide a default value for this parameter that will be assigned to the parameter simple
int param.
If at the startup of the node, no one is setting it.
So for example, let's set this 1 to 28.
Similarly, we can also declare a second parameter still with the declare parameter function.
And this time let's declare this parameter as a string.
So first let's include the string module from C plus plus, and then let's use this one to declare that
we are using a string from the C plus plus standard library.
And we want to call this one simple string param and by default.
So if no one sets a value for this parameter, when the node starts, we assign it to be Antonio.
These two instructions are used to declare and initialize the parameters at the node startup.
However, another important feature of parameters is that they can be configured and changed during
the node execution.
So at runtime while the node is running.
To achieve this, we use the add on set parameter callback function that still we are inheriting from
the node class.
And with this function we can define a callback function that will be executed whenever one or more
parameters that are declared in the node are changed.
For example, when the simple int parameter or the simple string parameter are changed, we want a certain
function to be executed in order to handle this change.
To do so, we use the bind function from the standard CPP library and we want to use this one to execute
the param change callback function that we are going to still declare within the simple parameter class.
So let's declare this one to be a member of the simple parameter class.
Let's also use the pointer this to indicate that we want to use the current version.
So the implementation of the parameter change callback function of the current object.
And then let's also declare the usage of the placeholders.
And let's just use the placeholder number one, to indicate that basically here this function will receive
just one input.
So we will receive just one parameter as input.
Now among the private.
So here among the private members of this function, we need first to create a pointer to an object
of type on set parameter callback endl.
So this is on set parameter callback Endl.
Let's create a shared pointer to this object and let's call param callback endl.
Inside this object, we can store the output of this variable here so we can store the output of the
ad onset parameter change callback.
And now we can finally proceed to declare the param change callback function.
We can do so still among the private functions or the private members of the simple parameter class.
So let's declare this function here and this function receives as input a vector.
So let's start by importing.
So let's include the vector module.
And so let's use this to declare that it receives a standard vector as input and the type of this vector
is RCL.
CP parameter.
So it receives a vector of parameters that have changed.
And let's call this one parameters.
So let's call this variable parameters.
Also, we need to declare the return type of this function and this one.
So the parameter change callback must return a response.
So our result containing the outcome of the change.
So whether it was successful or not.
This response must adhere to a specific two standard interface which is declared in the RCL interface.
So let's include this interface from the RCL interfaces from the messages.
Let's include the set parameter result.
And now let's use this one as our return type of this function.
So from the SQL interfaces, from the messages, let's use the set parameter result so it will return
an object of this type.
In this function.
Let's start just by creating exactly an object of this type.
So let's take this one.
Let's paste it and let's call this one result.
This is also the object that we need to return.
When we finished with the execution of the param change callback function.
Now, in this function for each parameter that has changed, we simply want to print an informative
message in the terminal with the new value that was assigned to those parameters.
So let's create a for loop that is iterating through all the parameters.
So for each parameter within the vector of parameters that we have received as input, what we want
to do is first check, which is the parameter that has changed.
So which are the parameters among the one that we declared that have changed.
So if the parameter dot get name, so it's equal to simple int parameter.
So if someone requested to change the simple int parameter in this case, let's also check that it was
changed with an object of the same type.
So of type integer.
So let's also check that the param type is of type rcl, cp.
Parameter type.
And from this one we take the parameter integer.
So if both these conditions are met, this means that the simple int parameter has been changed with
a new valid value.
So in this case, let's print an informative message in the terminal that says with the function rcl
cp info stream.
So let's use the get logger function and then let's print the message param simple int param changed
and then the new value is and then let's print the new value that is basically param.
Isn't.
And so this way we have in the terminal the new value that was assigned to the symbol int parameter.
Now let's also assign in this case a value for the result variable, and let's set the success to true
since the parameter was correctly changed and displayed in the terminal.
Otherwise, if the parameter that has been changed is the symbol string parameter.
So if the parameter get name is the symbol string param also in this case we need to change this one
to simple int param as we called here.
The declared parameter is the simple int param and the second one is the simple string param.
So let's check also that the new value that has been assigned to the simple string param is valid,
so is of type string.
So let's check that the param gets type.
Is an RCL CP parameter type of type parameter string.
And so if both the conditions are met, let's print an informative message in the terminal.
So still with the CP info stream function, let's use the get logger function.
And let's print the message param simple string param changed.
And then let's print also the new value that has been assigned.
And so this is the param as string this time since this is a string message.
And also in this case, let's set the result.
Successful variable to true.
Finally at the end of the parameter change callback function.
So here let's also return the result variable.
So the object of type set parameter result that is the output also of the parameter change callback
function.
Now this is all we need for the definition of the simple parameter class.
And so we can proceed to create the main function, which is the one that is automatically executed
when the node starts.
So int main.
And here, let's start by initializing the communication with Ros two with the init function that receives
as input the argument of the main.
And then let's create a new shared pointer to an object of the simple parameter class.
So let's first include the memory module and let's use it to create a new node.
So a new variable of type node.
And then let's use the make shared function to create a new object of type simple parameter.
And now we can keep this node up and running with the spin function.
So keep spin to keep the node running.
And also if we terminate the execution of the node with control Z, let's shut down Ros.
So lclc-rp shut down.
With this we have completed our node, so let's save this file and now we can proceed to install it
by modifying the file CMake list that is located into the Arduino board CP Examples package.
So as a first thing, let's add a new dependency.
So with the find package instruction from the.
Our SQL interface is a library that we used in our script.
So let's declare the usage of this library, and then let's also add the new executable.
So the simple parameter node to so we can copy the same instruction that we used for the simple publisher
and subscriber.
Let's just change the name of the executable to simple parameter.
And also the name of the source code is the simple parameter dot CPP.
Let's also change the name into the target dependencies instruction and here the libraries that it is
going to need are the CPP.
And instead of the standard messages, this is going to need the RCL Interfaces library.
And then finally, let's also add this executable.
So the simple parameter among the list of install.
So this will be installed alongside with the simple publisher and the simple subscriber.
Let's save also this file.
And finally, before building our workspace and so before running the node, since we used and we declared
the usage of the RCL interfaces inside the Cmakelist, we also need to declare it within the package
dot XML.
So here let's add a new dependency from the RCL interfaces.
We can save also this file.
And to conclude this lesson, let's build our workspace.
So in the terminal, let's go to the workspace and let's build it to verify that actually the build
is successful and there are no errors in the next laboratory lesson, we will start this node and use
the two command line interface to read and change the configuration parameters of a node from the terminal.