In this laboratory lesson, we will implement a simple python node that can be configured with parameters
and change its behavior based on the assigned values to these parameters.
If you are only interested in the C plus plus development, then you can directly skip to the next lesson
where we will implement the same node in C plus plus.
So let's start by creating a new file inside the Arduino board Pi example in the corresponding folder.
So this sub folder Arduino Pi example where we have already created the code for the simple publisher
and the simple subscriber.
So let's call this new file simple parameter dot pi.
And let's start as always, this node by importing the Pi library and also still from the Pi library
from the node modules.
Let's import the node class and let's use this one to create a new class.
So let's call this one simple
parameter that indeed inherits from the node class that we just imported from the Pi module.
Let's start by defining the constructor of the simple parameter class.
So let's define the init function.
That is the function that is automatically executed when we create an instance of the simple parameter
class.
And here as a first thing, let's call the constructor of the base class.
So the constructor of the node class.
So let's call its init function and let's assign a name to this node.
For example, let's call it simple parameter.
Within the constructor of the simple parameter class.
So here we can declare a new node configuration parameter using the function declare parameter that
we inherited from the node class.
This function takes as input the parameter name, which is an arbitrary name.
And for example, let's call this one simple int param.
And also we can decide to provide a default value which will be assigned to this configuration parameter.
If, for example, no one change it when we started the node.
For example, let's set this 1 to 28.
Similarly, let's also declare a second parameter still with the declare parameter function.
And instead let's call this second one simple string param, and also let's assign a default value.
Let's say this time we want to assign a string and this string says Antonio.
These two instructions are used to declare and initialize a parameter when the node starts up.
However, another important feature of the parameter is that they can also be configured and changed
while the node is running.
So on runtime.
To achieve this we need to use another function still one that we inherited from the node class.
That is the add on.
Set parameters callback.
So with this function we can define a callback function that will be executed whenever one or more of
the parameters that we have declared.
So one of these two basically is changed.
And we want in this case, for example, the function.
Param.
Change callback to be executed.
So when any of these two parameter is changed.
So let's proceed to define this function still as a member of the simple parameter class.
So let's define this function.
And this is still a member of the simple parameter class.
As this is a callback function that is executed.
When a node parameter is changed, it receives exactly the new values of the parameter that have been
just set.
So it receives a new variable called params.
This function also needs to return a response, a result containing the outcome of the parameter change,
and the response must follow a particular standard Ros2 interface, which is declared in the RCL Interface
library.
So from this library, from the RCL interfaces, from the messages of this library, let's import the
set parameter result.
So let's create a new variable called Result, and this is an instance of the set parameter result.
Now within this function for each parameter.
So for which parameter that we requested to change, we simply can print an informative message in the
terminal with the new value that was requested for that parameter.
So let's loop through all the parameters that are in the parameters vector, and now let's check which
was the parameter that changed.
So if the parameter name is equal to simple int param.
So in this case it means that someone is trying to change this parameter of the node.
And so in this case we also expect the new parameter that has been changed to be of type integers since
we set this one to be of type integer.
So in order to do this check, let's import the definition of an integer parameter by importing from
the SQL Pi Library and from the parameter module, let's import the parameter class.
And now let's use this class to verify actually that the parameter type.
Let's take care of adding the underscore.
So the name of the variable is type underscore.
Let's verify that the type is parameter.
Then type integer.
And so in this case, we use the get logger function of the node class to print an informative message.
And let's print.
The message param.
Simple int param changed.
And now let's also print the new value is.
And then let's print the new number that was assigned to the simple int param.
And this is the one that is in the param value.
In this case, let's also set the successful variable of the result message to true.
So let's set the result dot successful and let's set this one to true.
Otherwise.
So if the parameter that was requested to change is the simple string param, in this case, we expect
that the new value that was assigned to this parameter is of type string.
So let's also verify.
So let's double check that.
Actually the new value that was assigned is a string.
So let's check that the param type is of parameter.
Type string.
And so in this case, if both these conditions are met, let's print an informative message in the terminal
with the get logger function.
And then the message says param, simple string param.
Changed and then let's print new value is.
And so let's print the new value that is in the value variable of the parameter object.
So let's print this one.
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 after the for loop, let's return the result message.
So return the result message with the result of the parameter change.
So the one that we set here.
This is all we need for the definition of the simple parameter class.
And now we can proceed to create the main function, which is the one that is automatically called when
the node starts.
So this calls the main function and let's define the main function here.
And let's start the main function by initializing the Ros to communication with our Pi dot init function.
Then let's create a new instance of this simple parameter class.
Let's call this variable simple parameter and let's keep the node up and running.
Use the RCMP spin function.
And so let's spin the simple parameter node.
And finally, if we terminate the execution of the node with Control Z, let's destroy the node.
So simple parameter.
Let's destroy the node and then let's also shut down the Eros two subscriber.
So with our CLP shut down.
With this, we have completed our node, so let's save this script and now we can proceed to install
it by modifying the Setup.py file that is located in the Arduino board Pi Examples package.
So this one.
So let's add a new line here where we can install the simple parameter node, which again is in the
Arduino board.
PY examples folder this time in the simple parameter file.
And again we want to start the main function.
So the entry point where the node will start is the main function.
Finally, before actually building the workspace and running the node, since we have used the RCL interface
library in the script.
So this one we also need to declare this dependency in the package dot XML file.
So here we need to add a new exec dependency from the RCL interfaces.
Now we can save also this file and we can conclude this lesson by building the workspace.
So let's open a new terminal.
Let's go to the workspace and let's run the command.
Click on Build to verify that everything is working fine.
And as there are no errors in the next laboratory lesson after developing the same functionalities in
C plus plus we will start this node and use the Ross to command line interface to read and change the
configuration parameter of a node from the terminal.