In this laboratory lesson, we will develop a simple C plus plus node that listen for messages coming
from the Arduino through the serial port and republish each new message that is received from the Arduino
into a ros2 topic.
So let's go back to Visual Studio Code and let's start by creating a new script within the Arduino board
firmware package.
So within the source folder.
And here let's call this new script Simple serial Receiver Dot CP.
As you see in this script, we are going to develop a simple C plus plus node that publish some messages
within a ros2 topic.
We can copy the code from the simple publisher that we have already created within the Arduino CP examples.
So this one.
So let's copy its content and let's paste it here.
Let's just change the name of the class to be the simple serial receiver.
And so let's change it also into the name of the constructor.
Then here, when we define that, the timer callback function now belongs to the simple serial receiver
function.
And also here.
So now the implementation of the node is an implementation of the simple serial receiver class.
Let's also change here the name of the node.
To be the simple serial receiver and the name of the topic in which we want to publish the message.
So this one is the serial receiver topic.
Also in this topic.
So the serial receiver, we are going to publish string messages so we can keep the message interface.
This one so unchanged to be the standard message string.
We are not going to need any counter variable.
So actually we can remove everything from here and also we can remove the counter variable here.
And also let's change the counter from here.
And furthermore, let's change the frequency of the timer.
So this one for the moment, this is set to one second.
Let's set it to 0.01 seconds.
And so basically this means that the timer callback function will be executed every 0.01 seconds.
So namely, it will be executed with a frequency of 100Hz.
So let's remove also this message here.
And now we can implement the actual logic in order to send all the messages that we receive from the
serial port, from the Arduino into the serial receiver topic.
To do so, let's start by importing the Lib serial library to interact with the serial port.
So let's include from the Lib serial, the module serial port, and here among the private variable,
let's create a new variable from the lib serial namespace and the serial port class.
And let's call this one Arduino to start the serial communication.
And so to start the communication with the Arduino here in the constructor, we can use the function
open.
So on the Arduino object, let's use the function open.
And this function requires as input the port.
So which is the port of our PC to which the Arduino board is connected through the USB cable.
As this port can change from computer to computer and from user to user, instead of hard coding this
port within this script, let's use some parameters.
So some Ros two parameters as we also did for the simple serial transmitter.
So we can copy exactly these lines here that declares the parameter port.
And then they are reading the value of the parameter port.
So let's paste it also within the simple serial receiver and then let's use this parameter here.
So let's use the port and let's pass it to the open function.
Now, after opening the port, let's also set the baud rate with the function set baud rate for the
communication with the Arduino and from the lib serial from the baud rate.
Let's use the baud rate 115 200.
Now, since in the constructor of the simple serial receiver class, we opened the serial communication
with the Arduino.
So here, when the execution of the node ends, we also need to make sure that the communication with
the serial port is closed so that other nodes and other application can use it.
Therefore, we need to define the destructor.
So the destructor of the simple serial receiver class and within it we need simply to close the connection.
So let's use the serial port and then let's use the function close.
So when the node execution ends, we simply call the close function.
At this point, we can finally implement the logic of this node within the timer callback function.
So the one that is executed at regular time intervals by the Ros two timer and that we want to be executed
with a frequency of 100Hz.
So now in this function, let's first verify that.
Ros So the Ros two node is still executing and so let's use the CP okay, function to verify that Ros
is still executing.
And also let's verify that actually the serial communication with the Arduino is still on by using the
Arduino object.
And then let's check if there is any data available.
And so if in this case there is any new data available, there is any new message available that the
Arduino published on the serial port.
Let's read it.
So let's read those messages with the function read line.
So basically here what we are doing is checking the serial port with a frequency of 100Hz to check if
the Arduino has any new messages for us.
And in this case, we are reading it.
And so let's read its content within this message variable.
So within a new string message.
So let's declare this message above.
So before the if statement.
And then let's read any new messages that the Arduino is publishing within this one.
So within the message data.
And then simply we can publish this message so we can use the publisher to publish the message that
we received from the Arduino within the serial receiver topic.
With this, the simple serial receiver class is completed so we can save our node and now we just miss
to install this script.
So within the cmakelists.txt we need to install it as we installed the simple serial transmitter so
we can just copy the lines that we used for installing the serial transmitter and let's change the name
of the executable to be simple serial receiver.
Then the script, which contains the source code, is the simple cellular receiver.
CPP.
Let's change also here the name of the executable.
And then we need also to list this executable here.
So in the install statement, let's save also this file and we don't need for the moment to modify the
package dot XML within the Arduino board firmware since we haven't added any new dependencies compared
to those that we have already declared and used for the simple serial transmitter.
So all the dependencies are correctly declared.
So now to finish this lesson, let's just build our workspace to check that we have not made any mistake.
So let's go to the Arduino board workspace and let's build it.
And so the build is successful.
And so in the next laboratory lesson, we will start this node and we will see how it works.
And so how it receives messages from the Arduino through the serial port and how it republish those
messages into our ros2 topic.