WEBVTT

00:00.260 --> 00:06.290
In this laboratory lesson, we are going to develop a simple python node that listens for messages coming

00:06.290 --> 00:13.100
from the Arduino through the serial port and republish each new message received into our Ros2 topic.

00:13.580 --> 00:18.590
If you are only interested in the plus Cplusplus development, then you can skip to the next lesson

00:18.590 --> 00:23.330
in which we are going to implement the same node with the same functionalities in C plus plus.

00:24.000 --> 00:28.650
So let's go back in Visual Studio code and in the Arduino board firmware package.

00:28.650 --> 00:34.350
And as we are inserting a Python node, let's go to the Arduino board firmware folder.

00:34.560 --> 00:40.200
And here let's create a new file called Simple serial

00:43.380 --> 00:45.450
Receiver Dot Pi.

00:45.630 --> 00:54.690
Let's begin by declaring the directory of the python interpreter And so user bin and the environment

00:54.690 --> 00:56.700
is Python three.

00:57.090 --> 01:02.820
Then since in this script we are going to develop a simple python node that publishes messages within

01:02.820 --> 01:03.870
our Ros two topic.

01:03.900 --> 01:09.430
We can copy the code from the simple publisher which we have already implemented in the Arduino board.

01:09.480 --> 01:10.890
Pi examples.

01:10.890 --> 01:12.780
So the simple publisher.

01:13.170 --> 01:16.800
So let's copy all its content and let's paste it here.

01:17.010 --> 01:20.550
Let's just change the name of the class to be the simple.

01:22.480 --> 01:25.600
Serial receiver.

01:26.380 --> 01:28.630
So let's change it also here.

01:28.630 --> 01:35.410
And let's change the name of the variable that is the simple serial receiver.

01:38.950 --> 01:40.810
And let's change it also here.

01:40.810 --> 01:45.820
And then let's finally change also here, the name of the node that we want to start.

01:46.420 --> 01:51.070
That is the simple serial receiver.

01:51.490 --> 01:56.470
Let's also change the name of the topic in which we want to publish messages that are coming from the

01:56.470 --> 01:57.250
Arduino.

01:57.550 --> 02:03.250
And so let's call this one serial receiver.

02:03.250 --> 02:10.060
And here in this topic, we are still going to publish string messages so we can keep the same interface

02:10.210 --> 02:12.070
from the standard messages.

02:12.750 --> 02:18.540
For the scope of this script, of this note, we are not going to need the counter so we can remove

02:18.540 --> 02:23.610
this variable and also we can remove it from here.

02:24.580 --> 02:28.630
So let's remove the entire message here.

02:28.630 --> 02:32.080
And furthermore, let's also change the variable frequency.

02:32.080 --> 02:36.370
So this one to be 0.01.

02:36.370 --> 02:41.110
So this way the Ros two timer executes the timer callback function.

02:41.110 --> 02:45.430
So this one every 0.01 seconds.

02:45.430 --> 02:48.940
So basically with a frequency of 100Hz.

02:49.860 --> 02:54.990
At this point, we can implement the actual logic to publish any new message that is received from the

02:54.990 --> 02:55.740
serial port.

02:55.740 --> 03:01.530
So from the Arduino into the Ros2 topic that is called serial receiver.

03:02.250 --> 03:06.510
So let's remove also this message here.

03:07.110 --> 03:09.180
And let's start by importing.

03:11.330 --> 03:14.090
The serial library.

03:14.090 --> 03:15.680
So the serial library.

03:15.860 --> 03:20.030
And let's use it to create a new object of the serial class.

03:20.480 --> 03:29.420
So here, let's call this one Arduino And this is an instance of the serial class.

03:29.840 --> 03:34.310
And so from the serial library, let's create an instance of the serial class.

03:34.460 --> 03:40.310
And the constructor of this class requires two inputs the port of our PC to which the Arduino board

03:40.310 --> 03:46.280
is connected via the USB cable and also the baud rate that is used for the communication with the Arduino

03:46.280 --> 03:47.360
through its port.

03:47.840 --> 03:53.840
Instead of hard coding these values, as we also did for the serial transmitter dot pi file, let's

03:53.840 --> 04:00.650
use the Ros two parameters so we can exactly copy the same things that we have done for the simple serial

04:00.650 --> 04:01.490
transmitter.

04:01.880 --> 04:08.810
To declare the parameters port and baud rate and then to read them into two variables that are the port

04:08.810 --> 04:11.190
variable and the baud rate variable.

04:11.370 --> 04:14.490
So let's paste it here.

04:15.360 --> 04:17.490
Let's format it a little bit better.

04:17.940 --> 04:19.260
Okay, now it's fine.

04:19.260 --> 04:26.070
And so now let's use these two parameters so the port and the baud rate to initialize here the serial

04:26.070 --> 04:26.940
communication.

04:27.270 --> 04:37.470
So let's set the port to be the port and the baud rate to be the variable baud rate.

04:38.170 --> 04:43.660
And let's also set the timeout of 0.1.

04:44.810 --> 04:47.140
Now within the timer callback function.

04:47.140 --> 04:52.750
So this one, the one that is executed at regular time intervals by the Ros two timer.

04:52.780 --> 04:59.830
Let's first verify that the Ros two node is still running, so it's still in execution by using the

04:59.990 --> 05:01.330
Rql library.

05:01.450 --> 05:03.450
And let's check the function.

05:03.460 --> 05:05.470
Okay, so let's check that.

05:05.470 --> 05:07.840
Actually, Ros is still in execution.

05:07.840 --> 05:14.140
And also let's check that the serial communication with the Arduino is still open.

05:14.230 --> 05:21.940
So here let's change this variable to Arduino underscore and here let's verify that the communication

05:21.940 --> 05:25.210
is open in this case.

05:25.210 --> 05:31.450
So if both these conditions are met, we can actually start by reading the messages that are coming

05:31.450 --> 05:32.440
from the Arduino.

05:32.560 --> 05:36.820
And so we can use the still Arduino object.

05:36.940 --> 05:41.350
And on this one, let's call the function read line.

05:41.650 --> 05:48.290
So let's use this function to read an entire message, an entire line, and let's store this one into

05:48.290 --> 05:50.840
a new variable called data.

05:52.360 --> 05:56.810
Next, let's try to decode the content of this data variable.

05:56.830 --> 05:58.630
So let's try.

05:59.140 --> 06:08.860
Data decode to decode this one using the UTF eight format and otherwise.

06:08.860 --> 06:10.450
So if we are not able.

06:11.500 --> 06:14.140
Let's simply terminate the execution.

06:14.140 --> 06:16.450
So let's simply terminate this script.

06:16.450 --> 06:17.590
So here it is.

06:17.620 --> 06:18.610
Except.

06:20.530 --> 06:21.220
This one.

06:21.860 --> 06:24.470
Let's adjust the formatting of this script.

06:25.850 --> 06:27.950
So let's remove one tab character.

06:28.490 --> 06:29.900
And also for this one.

06:31.160 --> 06:33.260
So let's verify also.

06:33.260 --> 06:33.980
This one is fine.

06:38.060 --> 06:39.200
Okay now it's fine.

06:40.560 --> 06:43.190
Now here after the try statement.

06:43.190 --> 06:49.190
So if we reached this point, it means that we were able to decode correctly the message that we received

06:49.190 --> 06:50.240
from the Arduino.

06:50.540 --> 06:56.570
Let's publish a new message within the topic that we named Serial receiver.

06:56.570 --> 06:57.620
So this one.

06:57.950 --> 07:00.380
So let's first create a new message.

07:00.380 --> 07:12.620
So this one goes within the if statement, and then let's set the message data to be equal to the content

07:12.650 --> 07:15.830
of the data variable after converting it to a string.

07:15.830 --> 07:17.900
And then let's publish the message.

07:17.900 --> 07:22.010
So with the function publish, let's save this note.

07:22.190 --> 07:29.090
And with this the simple serial receiver class is completed and also our python node is ready to read

07:29.090 --> 07:35.690
messages from the serial port where the Arduino is connected and to republish them to our ros2 topic.

07:36.140 --> 07:41.990
As a final step for this lesson, let's install this node in the cmakelists.txt.

07:42.360 --> 07:49.080
And so in the install section where we installed the simple serial transmitter dot pi, let's add a

07:49.080 --> 07:57.330
new line in which we install the simple serial receiver dot pi.

07:58.140 --> 08:00.120
We don't need to modify for this time.

08:00.120 --> 08:06.150
The package dot XML of the Arduino bot firmware since we haven't added any new dependencies and we are

08:06.150 --> 08:10.140
still using the same ones of the simple serial transmitter.

08:10.320 --> 08:16.920
So now we can directly proceed to rebuild our workspace to see that we don't have made any mistake.

08:17.250 --> 08:22.020
So let's go to the Arduino bot workspace and let's build it.

08:23.740 --> 08:29.440
In the next laboratory lesson after developing the node with the same functionalities in C plus plus,

08:29.440 --> 08:34.750
we are going to start this node and see how it works and see how it establish the communication with

08:34.750 --> 08:38.740
the Arduino and it starts receiving messages from the Arduino board.
