In this laboratory lesson, we are going to develop a simple python node that listens for messages coming
from the Arduino through the serial port and republish each new message received into our Ros2 topic.
If you are only interested in the plus Cplusplus development, then you can skip to the next lesson
in which we are going to implement the same node with the same functionalities in C plus plus.
So let's go back in Visual Studio code and in the Arduino board firmware package.
And as we are inserting a Python node, let's go to the Arduino board firmware folder.
And here let's create a new file called Simple serial
Receiver Dot Pi.
Let's begin by declaring the directory of the python interpreter And so user bin and the environment
is Python three.
Then since in this script we are going to develop a simple python node that publishes messages within
our Ros two topic.
We can copy the code from the simple publisher which we have already implemented in the Arduino board.
Pi examples.
So the simple publisher.
So let's copy all its content and let's paste it here.
Let's just change the name of the class to be the simple.
Serial receiver.
So let's change it also here.
And let's change the name of the variable that is the simple serial receiver.
And let's change it also here.
And then let's finally change also here, the name of the node that we want to start.
That is the simple serial receiver.
Let's also change the name of the topic in which we want to publish messages that are coming from the
Arduino.
And so let's call this one serial receiver.
And here in this topic, we are still going to publish string messages so we can keep the same interface
from the standard messages.
For the scope of this script, of this note, we are not going to need the counter so we can remove
this variable and also we can remove it from here.
So let's remove the entire message here.
And furthermore, let's also change the variable frequency.
So this one to be 0.01.
So this way the Ros two timer executes the timer callback function.
So this one every 0.01 seconds.
So basically with a frequency of 100Hz.
At this point, we can implement the actual logic to publish any new message that is received from the
serial port.
So from the Arduino into the Ros2 topic that is called serial receiver.
So let's remove also this message here.
And let's start by importing.
The serial library.
So the serial library.
And let's use it to create a new object of the serial class.
So here, let's call this one Arduino And this is an instance of the serial class.
And so from the serial library, let's create an instance of the serial class.
And the constructor of this class requires two inputs the port of our PC to which the Arduino board
is connected via the USB cable and also the baud rate that is used for the communication with the Arduino
through its port.
Instead of hard coding these values, as we also did for the serial transmitter dot pi file, let's
use the Ros two parameters so we can exactly copy the same things that we have done for the simple serial
transmitter.
To declare the parameters port and baud rate and then to read them into two variables that are the port
variable and the baud rate variable.
So let's paste it here.
Let's format it a little bit better.
Okay, now it's fine.
And so now let's use these two parameters so the port and the baud rate to initialize here the serial
communication.
So let's set the port to be the port and the baud rate to be the variable baud rate.
And let's also set the timeout of 0.1.
Now within the timer callback function.
So this one, the one that is executed at regular time intervals by the Ros two timer.
Let's first verify that the Ros two node is still running, so it's still in execution by using the
Rql library.
And let's check the function.
Okay, so let's check that.
Actually, Ros is still in execution.
And also let's check that the serial communication with the Arduino is still open.
So here let's change this variable to Arduino underscore and here let's verify that the communication
is open in this case.
So if both these conditions are met, we can actually start by reading the messages that are coming
from the Arduino.
And so we can use the still Arduino object.
And on this one, let's call the function read line.
So let's use this function to read an entire message, an entire line, and let's store this one into
a new variable called data.
Next, let's try to decode the content of this data variable.
So let's try.
Data decode to decode this one using the UTF eight format and otherwise.
So if we are not able.
Let's simply terminate the execution.
So let's simply terminate this script.
So here it is.
Except.
This one.
Let's adjust the formatting of this script.
So let's remove one tab character.
And also for this one.
So let's verify also.
This one is fine.
Okay now it's fine.
Now here after the try statement.
So if we reached this point, it means that we were able to decode correctly the message that we received
from the Arduino.
Let's publish a new message within the topic that we named Serial receiver.
So this one.
So let's first create a new message.
So this one goes within the if statement, and then let's set the message data to be equal to the content
of the data variable after converting it to a string.
And then let's publish the message.
So with the function publish, let's save this note.
And with this the simple serial receiver class is completed and also our python node is ready to read
messages from the serial port where the Arduino is connected and to republish them to our ros2 topic.
As a final step for this lesson, let's install this node in the cmakelists.txt.
And so in the install section where we installed the simple serial transmitter dot pi, let's add a
new line in which we install the simple serial receiver dot pi.
We don't need to modify for this time.
The package dot XML of the Arduino bot firmware since we haven't added any new dependencies and we are
still using the same ones of the simple serial transmitter.
So now we can directly proceed to rebuild our workspace to see that we don't have made any mistake.
So let's go to the Arduino bot workspace and let's build it.
In the next laboratory lesson after developing the node with the same functionalities in C plus plus,
we are going to start this node and see how it works and see how it establish the communication with
the Arduino and it starts receiving messages from the Arduino board.