WEBVTT

00:00.140 --> 00:06.110
In this laboratory lesson, we will develop a simple C plus plus node that listen for messages coming

00:06.110 --> 00:11.720
from the Arduino through the serial port and republish each new message that is received from the Arduino

00:11.720 --> 00:13.850
into a ros2 topic.

00:14.420 --> 00:20.510
So let's go back to Visual Studio Code and let's start by creating a new script within the Arduino board

00:20.540 --> 00:21.590
firmware package.

00:21.590 --> 00:23.120
So within the source folder.

00:23.240 --> 00:32.120
And here let's call this new script Simple serial Receiver Dot CP.

00:33.690 --> 00:38.930
As you see in this script, we are going to develop a simple C plus plus node that publish some messages

00:38.930 --> 00:40.430
within a ros2 topic.

00:40.460 --> 00:47.420
We can copy the code from the simple publisher that we have already created within the Arduino CP examples.

00:47.570 --> 00:48.950
So this one.

00:48.950 --> 00:53.510
So let's copy its content and let's paste it here.

00:53.600 --> 01:03.050
Let's just change the name of the class to be the simple serial receiver.

01:03.410 --> 01:07.340
And so let's change it also into the name of the constructor.

01:07.610 --> 01:14.150
Then here, when we define that, the timer callback function now belongs to the simple serial receiver

01:14.150 --> 01:14.960
function.

01:15.110 --> 01:16.620
And also here.

01:16.640 --> 01:22.940
So now the implementation of the node is an implementation of the simple serial receiver class.

01:23.060 --> 01:25.190
Let's also change here the name of the node.

01:26.040 --> 01:34.350
To be the simple serial receiver and the name of the topic in which we want to publish the message.

01:34.350 --> 01:40.890
So this one is the serial receiver topic.

01:41.820 --> 01:42.980
Also in this topic.

01:42.990 --> 01:49.110
So the serial receiver, we are going to publish string messages so we can keep the message interface.

01:49.110 --> 01:53.100
This one so unchanged to be the standard message string.

01:53.780 --> 01:56.570
We are not going to need any counter variable.

01:56.570 --> 02:04.430
So actually we can remove everything from here and also we can remove the counter variable here.

02:05.060 --> 02:08.060
And also let's change the counter from here.

02:08.330 --> 02:11.810
And furthermore, let's change the frequency of the timer.

02:11.810 --> 02:16.250
So this one for the moment, this is set to one second.

02:16.280 --> 02:19.940
Let's set it to 0.01 seconds.

02:19.940 --> 02:26.990
And so basically this means that the timer callback function will be executed every 0.01 seconds.

02:26.990 --> 02:31.250
So namely, it will be executed with a frequency of 100Hz.

02:31.580 --> 02:34.970
So let's remove also this message here.

02:35.950 --> 02:42.130
And now we can implement the actual logic in order to send all the messages that we receive from the

02:42.130 --> 02:46.720
serial port, from the Arduino into the serial receiver topic.

02:46.930 --> 02:52.720
To do so, let's start by importing the Lib serial library to interact with the serial port.

02:53.260 --> 03:02.800
So let's include from the Lib serial, the module serial port, and here among the private variable,

03:02.800 --> 03:10.630
let's create a new variable from the lib serial namespace and the serial port class.

03:10.630 --> 03:15.850
And let's call this one Arduino to start the serial communication.

03:15.850 --> 03:21.640
And so to start the communication with the Arduino here in the constructor, we can use the function

03:21.640 --> 03:22.270
open.

03:22.600 --> 03:27.640
So on the Arduino object, let's use the function open.

03:27.640 --> 03:30.970
And this function requires as input the port.

03:30.970 --> 03:36.410
So which is the port of our PC to which the Arduino board is connected through the USB cable.

03:36.710 --> 03:43.070
As this port can change from computer to computer and from user to user, instead of hard coding this

03:43.070 --> 03:46.340
port within this script, let's use some parameters.

03:46.340 --> 03:52.010
So some Ros two parameters as we also did for the simple serial transmitter.

03:52.010 --> 03:58.220
So we can copy exactly these lines here that declares the parameter port.

03:58.220 --> 04:02.440
And then they are reading the value of the parameter port.

04:02.450 --> 04:09.980
So let's paste it also within the simple serial receiver and then let's use this parameter here.

04:09.980 --> 04:14.930
So let's use the port and let's pass it to the open function.

04:15.170 --> 04:23.420
Now, after opening the port, let's also set the baud rate with the function set baud rate for the

04:23.420 --> 04:31.280
communication with the Arduino and from the lib serial from the baud rate.

04:31.310 --> 04:35.570
Let's use the baud rate 115 200.

04:36.140 --> 04:42.620
Now, since in the constructor of the simple serial receiver class, we opened the serial communication

04:42.620 --> 04:43.640
with the Arduino.

04:43.790 --> 04:50.780
So here, when the execution of the node ends, we also need to make sure that the communication with

04:50.780 --> 04:55.580
the serial port is closed so that other nodes and other application can use it.

04:56.030 --> 04:58.830
Therefore, we need to define the destructor.

04:58.850 --> 05:08.240
So the destructor of the simple serial receiver class and within it we need simply to close the connection.

05:08.480 --> 05:12.860
So let's use the serial port and then let's use the function close.

05:13.490 --> 05:18.410
So when the node execution ends, we simply call the close function.

05:18.980 --> 05:24.680
At this point, we can finally implement the logic of this node within the timer callback function.

05:24.680 --> 05:30.950
So the one that is executed at regular time intervals by the Ros two timer and that we want to be executed

05:30.950 --> 05:33.350
with a frequency of 100Hz.

05:33.770 --> 05:36.660
So now in this function, let's first verify that.

05:36.660 --> 05:45.120
Ros So the Ros two node is still executing and so let's use the CP okay, function to verify that Ros

05:45.120 --> 05:46.200
is still executing.

05:46.200 --> 05:52.770
And also let's verify that actually the serial communication with the Arduino is still on by using the

05:52.770 --> 05:53.820
Arduino object.

05:53.820 --> 05:58.200
And then let's check if there is any data available.

05:58.560 --> 06:04.740
And so if in this case there is any new data available, there is any new message available that the

06:04.740 --> 06:06.720
Arduino published on the serial port.

06:06.750 --> 06:07.710
Let's read it.

06:07.710 --> 06:13.050
So let's read those messages with the function read line.

06:13.860 --> 06:20.970
So basically here what we are doing is checking the serial port with a frequency of 100Hz to check if

06:20.970 --> 06:23.400
the Arduino has any new messages for us.

06:23.400 --> 06:25.830
And in this case, we are reading it.

06:26.820 --> 06:30.210
And so let's read its content within this message variable.

06:30.210 --> 06:32.370
So within a new string message.

06:32.520 --> 06:35.730
So let's declare this message above.

06:35.760 --> 06:37.830
So before the if statement.

06:37.830 --> 06:42.960
And then let's read any new messages that the Arduino is publishing within this one.

06:42.960 --> 06:46.410
So within the message data.

06:48.070 --> 06:54.100
And then simply we can publish this message so we can use the publisher to publish the message that

06:54.100 --> 06:58.180
we received from the Arduino within the serial receiver topic.

06:58.930 --> 07:05.500
With this, the simple serial receiver class is completed so we can save our node and now we just miss

07:05.500 --> 07:07.200
to install this script.

07:07.210 --> 07:14.680
So within the cmakelists.txt we need to install it as we installed the simple serial transmitter so

07:14.680 --> 07:21.250
we can just copy the lines that we used for installing the serial transmitter and let's change the name

07:21.250 --> 07:25.150
of the executable to be simple serial receiver.

07:26.050 --> 07:30.880
Then the script, which contains the source code, is the simple cellular receiver.

07:31.060 --> 07:31.480
CPP.

07:31.630 --> 07:35.500
Let's change also here the name of the executable.

07:36.160 --> 07:39.220
And then we need also to list this executable here.

07:39.220 --> 07:45.550
So in the install statement, let's save also this file and we don't need for the moment to modify the

07:45.550 --> 07:52.210
package dot XML within the Arduino board firmware since we haven't added any new dependencies compared

07:52.210 --> 07:56.590
to those that we have already declared and used for the simple serial transmitter.

07:56.590 --> 07:59.110
So all the dependencies are correctly declared.

07:59.230 --> 08:05.350
So now to finish this lesson, let's just build our workspace to check that we have not made any mistake.

08:06.100 --> 08:10.480
So let's go to the Arduino board workspace and let's build it.

08:13.870 --> 08:15.810
And so the build is successful.

08:15.820 --> 08:21.010
And so in the next laboratory lesson, we will start this node and we will see how it works.

08:21.010 --> 08:26.560
And so how it receives messages from the Arduino through the serial port and how it republish those

08:26.560 --> 08:29.080
messages into our ros2 topic.
