In this laboratory lesson, we are going to develop a simple cplusplus node that subscribes to our ros2
topic and republishes every new message it receives on the Arduino serial port.
So back in Visual Studio code, in the Arduino board firmware package that we have just created.
Let's insert the new node.
As always within the source folder.
So here let's create a new file called Simple Serial Transmitter dot CP.
Since we are going to develop a simple C plus plus node that subscribes to our ros2 topic, we can copy
the code from the simple subscriber node that we have already created within the Arduino bot CP examples
and we called simple subscriber dot CP.
So let's copy its content and let's paste it here.
Here.
We just need to change the name of the class.
So let's call it simple serial transmitter.
And also, we need to change it.
In the name of the constructor.
And also here when we create the node and finally here, when we declare the message callback function
that now belongs to the simple serial transmitter class.
Let's also change the name of the node.
So here this is the simple serial transmitter and let's change the name of the subscriber.
So this one now is subscribed to the topic serial
transmitter.
Still in this topic, we are going to publish string messages so we can leave the type of the message.
So the interface that is used for the communication within this topic unchanged.
So we can leave this one to be string messages and now we can implement the actual logic that sends
each of the messages received on the serial transmitter topic to the Arduino through the serial port.
For this, we can use the lib serial library that is available in C plus plus and that we have downloaded
in the first setup lessons of this course.
So let's start by including this library.
So let's include the lib serial library.
And from this one, let's take the definition of the serial port.
Then here, among the private variables of the simple serial transmitter class, let's add a new private
member.
So from the lib serial, let's create a new object of type serial port.
And let's call this one Arduino.
Now to start the communication with the serial port.
We can use the open function so we can take this object and here within the constructor we can call
the function open on the serial object.
And this function requires as input the port of our PC to which the Arduino board is connected through
the USB cable instead of hard coding these values in the script, as of course it might change from
user to user and from PC to PC.
We can use the ros2 parameters to dynamically configure these values.
So here, let's start by declaring a new parameter for this node with the declare parameter function
which we are inheriting from the node class.
And this first one is a string.
So we want the type of this parameter to be a string and let's name it port and by default, in my case,
I'm going to set it to the port dev 80 ACM zero.
So to the port ACM zero of my PC.
It did not start up.
So when actually the node starts, we want to read this parameter.
So with this instruction we are declaring this parameter and now we want also to read it in order to
use it to open the communication with the correct port so we can use the function get parameter which
still we are inheriting from the node class.
And let's read the content of the parameter port and let's save it as a string.
Since we have declared this to be a string and let's store it within a new variable.
So within a new string variable that we call port.
And so it contains the output of this get parameter function.
Now we can use this variable here so the port and we can pass it to the open function so that it will
start opening the serial communication with the requested port.
So with the defined port.
After opening and initializing the communication, we also need to define the baud rate at which the
communication should occur.
So again, on the Arduino object, let's use the function set baud rate.
And let's set this one.
So from the lib serial, let's set the baud rate to be this one.
So 1115 200.
Since we opened the serial communication with the Arduino in the constructor of the simple serial transmitter
class, we also need to close it when the nodes ends its execution.
And so this way we need to prevent that.
The connection remains open forever.
To do so, we can create the destructor so the simple serial transmitter destructor.
And within this function, let's simply close the connection.
So if it was open, so if the connection was still open with the Arduino, so with the serial port,
this destructor that is called when we Destroy the node will automatically close the connection so that
other nodes and other application can use the serial communication.
Now at this point in the message callback function.
So here, which is executed upon receiving a new message in the serial transmitter topic, we just want
to send the message that was just received to the Arduino using the serial port.
So let's first change the message that we are going to print in the terminal.
So the informative message here and let's print new message received and then publishing on serial port
and followed by the message data.
So this one is the message that we are going to publish to the serial port.
Now in order to publish this message to the serial port, so in order to write this message to the Arduino,
let's use the function, right?
And so this function takes as input the message that we want to publish so that we want to transmit
through the serial port.
And this is the content of the message that we just received within the topic.
So message data.
Let's just remove the cost statement of this function.
And so now is fine because otherwise it won't be able to modify any object of the class.
With this, our node is completed and so now we can move on to install and launching it to see how it
works.
So let's start by installing it within the file Cmakelists.txt.
And here let's add the dependency from the Rql CPP library and also another one from the standard messages.
From which we have used the definition of the string message.
Then also we need to declare the dependency from the serial library.
So let's use the package.
Config and let's use this one for the instruction package, check modules and check the serial module
and so the lib serial library.
Now to compile and make our node executable, let's add a new instruction.
Add executable.
Let's call our executable simple serial transmitter and the source code is in the source folder and
is in the simple serial transmitter dot cpp.
Then also let's specify the dependencies that this node needs with the aim and target
dependencies.
So the node is the simple serial transmitter.
And it needs the dependency from the CPP library, from the standard messages, and also in order to
add the dependency from the Lib serial library, let's use another instruction of type target include.
A directories.
So still for the simple serial transmitter node.
Let's declare the usage of the library.
So let's declare the serial include dears.
Here is dears.
Finally, we need one more instruction that is target link libraries.
Still, for the simple serial transmitter node, this is public, and for the simple serial transmitter
node, we need to add the serial.
And this goes into the double quotes.
So this one and also this variable here goes into the double quotes.
Now to install the executable, let's add a new install instruction.
So here and let's install the target that we want to install is the simple serial transmitter node.
So this one and the archive destination is the lib folder.
Then the library
destination is again the lib folder and the runtime destination is the bin folder.
And the destination of the executable is the lib folder and the sub folder that has the same name of
the package.
And so we access with the project name variable.
Also let's modify the package dot XML.
Since we added some more dependencies to this email list and here let's add the dependency from the
RCL cpp library.
Then another one from the standard messages library and then another one.
From the lib serial dev library.
With this change, everything is set.
So now we can conclude this lesson by building our workspace in order to see that everything compiles
and so that we have no errors.
So let's go to the workspace and let's build it.
We can see indeed that there is an error in our list as this is runtime space destination.
So let's save and let's build the workspace again.
There is also another similar issue.
So with the archive destination.
Correct.
So let's use this space and let's give it one more try.
Now everything compiles.
And in the next laboratory lesson, we are going to start this node and see how it works and so how
it communicates with the Arduino.