Now that we know how the life cycle nodes work, let's see how to use them to create the interface.
The driver that connects our robot.
And so the Arduino and the servo motors to the Ros two environment.
To do this, we are going to use the Ros two Control library and we are going to develop an Arduino
interface to control the real robot and the real motors so that all the other applications that use
the Ros to control, to move the simulated robot, like, for example, move it can continue using the
same logic and the same files to move the real robot in the same way as they were actuating on the simulated
robot.
At the time of creating this lesson in Ros2, it is possible to develop an hardware interface and integrate
it with the Ros2 Control library only with C plus plus.
Therefore, there won't be a similar lesson for the creation of the hardware interface in Python.
Anyway, as soon as this functionality becomes available also in Python I will take care of extending
this lesson in Python as well.
And since we are about to create the hardware interface for controlling the robot, let's add this functionality
within the Arduino bot controller package.
And here, let's start by creating a new script, a new header file within the include folder and within
the Arduino bot controller sub folder.
And let's call this file Arduino bot interface dot PHP.
If this is the first time you encounter a file of this type.
So with extension h p, you just need to know that this is a cplusplus header file used to separate
the declaration of a class and so to declaration of its methods and attributes from its definition.
So where the logic is, the separation is very useful, especially in large projects, and improves
the code readability and the reusability of the code.
In this other file.
Let's start by importing the CPP library.
So the CPP.
And then let's also import the system interface module from the hardware interface library.
So let's include from the hardware interface, the system interface module.
And we are going to use it to create a new hardware interface for our robot.
So for our manipulator, let's create a new class called Arduino Bot Interface, which inherits from
the system interface class that is defined in the Arduino interface.
And so from the system interface class.
And this indicates that the Arduino bot interface class that we have just created implements an hardware
interface of the Ros2 Control library.
So to integrate it with the Ros to control library.
Now let's continue defining this class.
And before declaring this class, let's first use the Cplusplus include guards.
So if is not defined the Arduino bot interface H let's define this one and at the end of the file.
So here let's just put and if.
This include guards, prevent this class from being redefined multiple times in the same project or
in the same application.
If, for example, several different cplusplus scripts include this header file in their in their definition.
Let's also place the declaration of this class.
So if the Arduino board interface class within a namespace and let's call this one Arduino board controller.
So let's define these namespace and let's define the Arduino board interface class within this namespace.
Now let's start by adding functionalities to the Arduino board interface class.
Let's start by clearing the public attributes of this class.
And so let's start with this constructor.
So with the Arduino board interface class and then also let's declare the virtual destructor of this
class.
So still Arduino board interface.
And then since this node is inheriting from the system interface class, from the interface library
and since this class.
So the system interface class implements a lifecycle node, we need to redefine the behavior of some
functions that are the on activate and on the activate functions.
So still here, let's redefine the behavior on the on.
Activate function.
So let's use the override to say that we are redefining the behavior respect to the one that was defined
here and also the on the activate function.
Let's override also this here.
The return type of this function is callback return that is defined in the CPP lifecycle library.
So first we need to import this library.
So let's include CPP lifecycle and from this one from the node interfaces, let's import the lifecycle
node interface.
And now here within the namespace, instead of retyping every time the type callback return.
So as we did for example here, as we did in the simple lifecycle node, instead of retyping every time
all of these return type, we can use an using statement.
So using callback return and so let's use just this one instead of using the CPP lifecycle node interfaces.
And then from this one, the lifecycle node interface.
And from this one the callback return.
So from now on, instead of rewriting all of this here, we can just refer to it just as callback return.
And so this one is the return type that now we can use on the on activate function and on the on the
activate function about the input of this function.
So we can see that still there is an error since the input is not correct.
And so about the input they receive an object of type state from the CPP interface package.
So let's include this class.
So include still from the CPP lifecycle.
Let's take the definition of the state class and now let's use this class that we have imported.
So the state class to define that the on activate function receive as input an object of type rql cpp
lifecycle of type state and let's call this one for example, previous state.
And also the on the activate function receives the same input.
With this, we have redefined only the two functions that we need to configure and start the behavior
of the life cycle node.
Now we need to redefine some other functions that belongs to the system Interface class of the hardware
interface library from which we are inheriting.
Redefining the behavior of this function will allow us to configure the drivers behavior and the communication
with the Arduino and the motors.
Among these, we need to redefine the function that is called on init.
So let's override the behavior of this function.
And also this one returns an object of type callback return and instead it receives as input an object
of type.
Hardware info and let's call this one hardware info.
Next, we need to redefine another function that is the export state interface and the return type of
this function is a vector.
So let's import the vector module from the C plus plus standard library and let's use it to declare
that here the return type of this function is a vector of type hardware interface and of type state
interface.
Let's redefine one more function, and this is the export command interface.
This time.
So let's override this function and it also returns a vector this time of type hardware interface command
interface.
Finally, we need to redefine two more functions.
So here and these are the read function.
And the write function.
And also these two functions are inherited from the system interface class.
Both of them return an object of type hardware interface return type.
So this is the return type of both these functions and both receive as input an object of type time
from the CPP library.
So from the CPP they both receive an object of type time and let's call this variable time.
And this works for the read and also for the write function.
And also they receive another input, both of them.
That is another object of type rql CPP duration.
And let's call this one period.
So let's copy this one also for the write function.
So here.
With this, we have concluded the declaration of the private functions of the Arduino interface class,
and basically we have redefined the behavior of some of the functions that we have inherited from the
System interface class.
Now let's proceed to declare some private variables of this class.
And since the hardware interface needs to communicate directly with the Arduino and this communication
happen through the serial port, we are going to use the serial library in this script here, which
we have already used in the previous lesson to create some simple examples, some templates for the
communication between Ros two and the Arduino.
So let's include from the Lib serial, the serial port class, and let's use it here.
So from the Lib serial, let's create a new serial port object which we call Arduino.
Next, let's also add a new private member of the Arduino Bot interface class, which is a string.
So first let's include the string module.
Then let's create a new string and let's call this one port, which is going to indicate the port to
which the Arduino is connected to our PC.
And finally, let's add also three vectors of decimal numbers.
So of doubles.
And let's call the first one position commands.
Then let's copy this one and let's paste it two more times.
The second one is called Previous Position commands and the last one.
So this one is called Position states.
With this, the Arduino interface dot PHP file is completed and this, as you can see, just declares
the elements that belongs to the Arduino bot interface class.
In the next lesson we will proceed to define all the functions.
And so basically to add the logic and define the behavior of all these functions and also to initialize
all these variables here.