WEBVTT

00:00.230 --> 00:06.130
In this laboratory lesson, we are going to develop a simple python node that subscribes to our ros2

00:06.140 --> 00:11.930
topic and republish every new message it receives on the Arduino serial port.

00:12.320 --> 00:17.330
If you are interested only in the Cplusplus development, then as always, you can skip to the next

00:17.330 --> 00:22.820
lesson in which we are going to implement the same node with the same functionalities in C plus plus.

00:24.540 --> 00:29.400
Making Visual Studio code in the newly created Arduino board firmware package.

00:29.430 --> 00:34.190
Let's create a new folder that is called as the Package itself.

00:34.200 --> 00:38.520
So Arduino boot firmware.

00:39.480 --> 00:47.760
And in order to create a new Python node, let's add a new file within this folder called init dot pi

00:47.760 --> 00:49.050
that we leave empty.

00:49.260 --> 00:54.030
And then let's also add one more file that will actually contain the logic of our node.

00:54.030 --> 01:02.880
And let's call this one simple serial transmitter dot pi.

01:03.330 --> 01:07.410
Here, let's start by declaring the python interpreter directory.

01:07.800 --> 01:17.250
So this is in the user bin and the environment is the Python three that we are going to use.

01:17.490 --> 01:23.430
And then since in this script we are going to develop a simple python node that subscribes to our ros2

01:23.460 --> 01:24.160
topic.

01:24.160 --> 01:31.690
We can copy the content of the simple subscriber python node that we created in the Arduino Pi examples.

01:31.870 --> 01:34.690
So the simple subscriber here.

01:34.870 --> 01:38.710
So let's copy all its contents and let's paste it here.

01:38.710 --> 01:41.830
So we have already a node that implements a subscriber.

01:42.130 --> 01:44.290
Let's just change the name of the class.

01:44.290 --> 01:51.970
So this one to be simple serial transmitter.

01:52.060 --> 01:55.330
And also we need to change it within the main function.

01:55.330 --> 02:03.940
So here let's also change the name of the variable to simple serial transmitter.

02:04.180 --> 02:11.980
So the Pi will spin this node and when we press control C, we are going to destroy this simple serial

02:11.980 --> 02:13.120
transmitter node.

02:13.360 --> 02:15.550
Next, let's also change the name of the node.

02:15.550 --> 02:23.260
So this one, let's call it simple serial transmitter.

02:23.260 --> 02:25.030
And this is subscribed.

02:25.030 --> 02:34.150
So the subscription is to the topic serial transmitter, in which we are still assuming that string

02:34.150 --> 02:35.560
messages are published.

02:35.560 --> 02:40.420
And so we can leave the type of the message of the interface as a string.

02:40.450 --> 02:46.840
Now within the message callback function, which is executed when we receive a new string message within

02:46.840 --> 02:52.750
the serial transmitter topic, we can implement the actual logic that sends each new message that was

02:52.750 --> 02:57.010
received within this topic to the serial port and so to the Arduino.

02:57.490 --> 02:58.240
To do so.

02:58.240 --> 03:03.670
In Python, there is a library that we have already installed in one of the first lessons of this course

03:03.670 --> 03:05.560
that is called Pyserial.

03:05.860 --> 03:13.390
So let's start by importing this serial library and now let's use it to create a new object of this

03:13.390 --> 03:13.720
class.

03:13.720 --> 03:15.550
So of the serial class.

03:16.420 --> 03:24.700
And so from the cereal library, let's create a new instance of the cereal class and let's save it to

03:24.700 --> 03:25.720
a new variable.

03:27.280 --> 03:30.070
That is called Arduino.

03:30.070 --> 03:33.640
And so this is an implementation of the serial class.

03:33.730 --> 03:37.280
And the constructor of this class requires two inputs.

03:37.300 --> 03:43.660
The first one is the port of our PC, to which the Arduino board is connected through the USB cable.

03:43.660 --> 03:49.180
And also the second parameter is the baud rate that we need to use for the communication with the Arduino

03:49.180 --> 03:50.470
through the serial port.

03:51.030 --> 03:54.030
Instead of hard coding these values into the script.

03:54.060 --> 03:58.860
As for example, different users may connect the Arduino to a different port or their PC.

03:59.340 --> 04:04.340
We are going to use the two parameters to configure these serial class.

04:04.380 --> 04:07.320
So let's start by declaring two parameters.

04:07.470 --> 04:14.550
So here, after the initialization of the base class, let's declare two parameters with the function

04:15.720 --> 04:21.510
declare parameter, and the first one is the port.

04:21.750 --> 04:30.420
And by default, I set this one to the port dev ty and in my case is the ACM zero port.

04:30.420 --> 04:40.710
And then the second parameter still with the declared parameter is the boot rate that by default I set

04:40.710 --> 04:43.980
to 115 200.

04:44.620 --> 04:50.770
So, you know the startup, whenever we execute the constructor of the simple serial transmitter class,

04:50.800 --> 04:53.110
these two parameters will be declared.

04:53.110 --> 04:58.510
And then also we are going to read the values that are assigned to these parameters, and we can do

04:58.510 --> 05:01.270
so with the get parameter function.

05:01.900 --> 05:11.560
So still from the node class, let's use the get parameter function that we have inherited from the

05:11.560 --> 05:16.990
node class and let's read the content of the port value.

05:17.170 --> 05:25.450
So let's read this content and let's store this one into a new variable called, for example, port.

05:25.810 --> 05:27.280
Let's do the same.

05:27.940 --> 05:31.060
Also for the second parameter that we have declared for this node.

05:31.330 --> 05:32.440
So the.

05:34.500 --> 05:41.160
Boundary parameter and let's read the content of the boundary parameter.

05:41.790 --> 05:48.780
Now we can use these two variables so the port and the baud rate to initialize the serial communication.

05:48.780 --> 05:52.020
So to initialize this serial object.

05:52.020 --> 06:01.320
And so we need to provide a port that is in the variable port and also we need to provide a baud rate

06:01.650 --> 06:04.770
that is in the variable baud rate.

06:04.890 --> 06:11.220
Finally, let's also set a timeout for this communication to be equal to zero 0.1.

06:12.110 --> 06:18.200
At this point in the message callback function, which is executed upon receiving a new message in the

06:18.200 --> 06:24.980
serial transmitter topic, we can send the received message to the Arduino using the serial object.

06:24.980 --> 06:27.470
So this one that we have just created.

06:27.590 --> 06:33.980
So let's first change here the message that we are going to print in the terminal and let's print the

06:33.980 --> 06:34.800
message.

06:34.820 --> 06:38.690
New message received.

06:39.760 --> 06:47.650
And then publishing on Serial and then followed by the port.

06:47.800 --> 06:52.720
So let's take the port from the serial object.

06:53.290 --> 06:54.830
And let's take the name.

06:54.850 --> 07:02.680
Now, in order to send this message through the serial port, let's use the Arduino object and let's

07:02.680 --> 07:05.260
use the write function.

07:05.260 --> 07:11.490
And so we can write the content of the ros2 message that we have just received.

07:11.500 --> 07:19.600
And let's ensure that it is encoded properly following the UTF eight encoding.

07:20.500 --> 07:25.870
With this, our node is completed, so now we can move on to installing and launching it.

07:25.900 --> 07:27.190
To see how it works.

07:27.640 --> 07:30.850
So in the file cmake list dot txt.

07:31.510 --> 07:37.720
Let's remove all the commented rows to have a better visualization of this file.

07:38.500 --> 07:47.560
And now let's add a new dependency from the amend cmake python in order to compile our python node.

07:47.570 --> 07:54.420
And then let's also add another one from the PY library which we used in our node.

07:54.440 --> 08:09.890
Then let's also use the instruction almond python install package to install the python script within

08:09.890 --> 08:10.940
the current package.

08:10.940 --> 08:13.580
So within the Arduino board firmware package.

08:13.610 --> 08:19.400
Then let's also use an install instruction to install our python node.

08:19.400 --> 08:21.110
And so to install a new program.

08:22.400 --> 08:28.310
And this program that we want to install is within the Arduino board firmware folder.

08:28.490 --> 08:31.160
So the folder that has the same name of the package.

08:31.460 --> 08:38.450
So we refer to this one with the variable project name and then the script that contains the source

08:38.450 --> 08:47.500
code is the simple serial transmitter dot pi and the destination.

08:47.500 --> 08:55.600
So we want to install this one within the lib folder and within the sub folder that has the same name

08:55.600 --> 08:56.680
as the package.

08:56.950 --> 09:01.930
And so again, let's use the project name variable.

09:02.780 --> 09:03.350
Next.

09:03.350 --> 09:07.220
We also need to modify the package dot XML.

09:07.340 --> 09:16.070
And so here we need to add a new build tool dependency from the CMake Python in order to compile our

09:16.080 --> 09:16.610
node.

09:16.700 --> 09:18.860
And then also we need a dependency

09:22.250 --> 09:33.200
from the SQL Pi and also we need an execution dependency from the Python three

09:35.900 --> 09:40.310
serial library that we have used for the communication with the Arduino.

09:40.340 --> 09:41.090
That's it.

09:41.120 --> 09:46.220
Now everything is done and so we can build our workspace and see how it works.

09:46.790 --> 09:48.980
So let's open a new terminal.

09:50.270 --> 09:55.280
Let's go to the workspace and let's build it.

09:55.830 --> 10:01.260
In the next laboratory lesson after developing the node with the same functionalities in Cplusplus,

10:01.260 --> 10:06.540
we are going to start this node and so we are going to see how it works and how it communicates with

10:06.540 --> 10:07.650
the Arduino board.
