In this laboratory lesson, we are going to develop a simple python node that subscribes to our ros2
topic and republish every new message it receives on the Arduino serial port.
If you are interested only in the Cplusplus development, then as always, 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.
Making Visual Studio code in the newly created Arduino board firmware package.
Let's create a new folder that is called as the Package itself.
So Arduino boot firmware.
And in order to create a new Python node, let's add a new file within this folder called init dot pi
that we leave empty.
And then let's also add one more file that will actually contain the logic of our node.
And let's call this one simple serial transmitter dot pi.
Here, let's start by declaring the python interpreter directory.
So this is in the user bin and the environment is the Python three that we are going to use.
And then since in this script we are going to develop a simple python node that subscribes to our ros2
topic.
We can copy the content of the simple subscriber python node that we created in the Arduino Pi examples.
So the simple subscriber here.
So let's copy all its contents and let's paste it here.
So we have already a node that implements a subscriber.
Let's just change the name of the class.
So this one to be simple serial transmitter.
And also we need to change it within the main function.
So here let's also change the name of the variable to simple serial transmitter.
So the Pi will spin this node and when we press control C, we are going to destroy this simple serial
transmitter node.
Next, let's also change the name of the node.
So this one, let's call it simple serial transmitter.
And this is subscribed.
So the subscription is to the topic serial transmitter, in which we are still assuming that string
messages are published.
And so we can leave the type of the message of the interface as a string.
Now within the message callback function, which is executed when we receive a new string message within
the serial transmitter topic, we can implement the actual logic that sends each new message that was
received within this topic to the serial port and so to the Arduino.
To do so.
In Python, there is a library that we have already installed in one of the first lessons of this course
that is called Pyserial.
So let's start by importing this serial library and now let's use it to create a new object of this
class.
So of the serial class.
And so from the cereal library, let's create a new instance of the cereal class and let's save it to
a new variable.
That is called Arduino.
And so this is an implementation of the serial class.
And the constructor of this class requires two inputs.
The first one is the port of our PC, to which the Arduino board is connected through the USB cable.
And also the second parameter is the baud rate that we need to use for the communication with the Arduino
through the serial port.
Instead of hard coding these values into the script.
As for example, different users may connect the Arduino to a different port or their PC.
We are going to use the two parameters to configure these serial class.
So let's start by declaring two parameters.
So here, after the initialization of the base class, let's declare two parameters with the function
declare parameter, and the first one is the port.
And by default, I set this one to the port dev ty and in my case is the ACM zero port.
And then the second parameter still with the declared parameter is the boot rate that by default I set
to 115 200.
So, you know the startup, whenever we execute the constructor of the simple serial transmitter class,
these two parameters will be declared.
And then also we are going to read the values that are assigned to these parameters, and we can do
so with the get parameter function.
So still from the node class, let's use the get parameter function that we have inherited from the
node class and let's read the content of the port value.
So let's read this content and let's store this one into a new variable called, for example, port.
Let's do the same.
Also for the second parameter that we have declared for this node.
So the.
Boundary parameter and let's read the content of the boundary parameter.
Now we can use these two variables so the port and the baud rate to initialize the serial communication.
So to initialize this serial object.
And so we need to provide a port that is in the variable port and also we need to provide a baud rate
that is in the variable baud rate.
Finally, let's also set a timeout for this communication to be equal to zero 0.1.
At this point in the message callback function, which is executed upon receiving a new message in the
serial transmitter topic, we can send the received message to the Arduino using the serial object.
So this one that we have just created.
So let's first change here the message that we are going to print in the terminal and let's print the
message.
New message received.
And then publishing on Serial and then followed by the port.
So let's take the port from the serial object.
And let's take the name.
Now, in order to send this message through the serial port, let's use the Arduino object and let's
use the write function.
And so we can write the content of the ros2 message that we have just received.
And let's ensure that it is encoded properly following the UTF eight encoding.
With this, our node is completed, so now we can move on to installing and launching it.
To see how it works.
So in the file cmake list dot txt.
Let's remove all the commented rows to have a better visualization of this file.
And now let's add a new dependency from the amend cmake python in order to compile our python node.
And then let's also add another one from the PY library which we used in our node.
Then let's also use the instruction almond python install package to install the python script within
the current package.
So within the Arduino board firmware package.
Then let's also use an install instruction to install our python node.
And so to install a new program.
And this program that we want to install is within the Arduino board firmware folder.
So the folder that has the same name of the package.
So we refer to this one with the variable project name and then the script that contains the source
code is the simple serial transmitter dot pi and the destination.
So we want to install this one within the lib folder and within the sub folder that has the same name
as the package.
And so again, let's use the project name variable.
Next.
We also need to modify the package dot XML.
And so here we need to add a new build tool dependency from the CMake Python in order to compile our
node.
And then also we need a dependency
from the SQL Pi and also we need an execution dependency from the Python three
serial library that we have used for the communication with the Arduino.
That's it.
Now everything is done and so we can build our workspace and see how it works.
So let's open a new terminal.
Let's go to the workspace and let's build it.
In the next laboratory lesson after developing the node with the same functionalities in Cplusplus,
we are going to start this node and so we are going to see how it works and how it communicates with
the Arduino board.