WEBVTT

00:00.140 --> 00:06.410
In this laboratory lesson, we are going to develop a simple cplusplus node that subscribes to our ros2

00:06.440 --> 00:11.990
topic and republishes every new message it receives on the Arduino serial port.

00:12.590 --> 00:18.110
So back in Visual Studio code, in the Arduino board firmware package that we have just created.

00:18.140 --> 00:20.190
Let's insert the new node.

00:20.210 --> 00:23.200
As always within the source folder.

00:23.210 --> 00:32.630
So here let's create a new file called Simple Serial Transmitter dot CP.

00:33.580 --> 00:39.850
Since we are going to develop a simple C plus plus node that subscribes to our ros2 topic, we can copy

00:39.850 --> 00:46.660
the code from the simple subscriber node that we have already created within the Arduino bot CP examples

00:46.900 --> 00:50.530
and we called simple subscriber dot CP.

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

00:56.000 --> 00:56.510
Here.

00:56.510 --> 00:58.550
We just need to change the name of the class.

00:58.940 --> 01:04.640
So let's call it simple serial transmitter.

01:04.760 --> 01:06.830
And also, we need to change it.

01:07.930 --> 01:09.790
In the name of the constructor.

01:10.450 --> 01:16.600
And also here when we create the node and finally here, when we declare the message callback function

01:16.600 --> 01:20.320
that now belongs to the simple serial transmitter class.

01:20.620 --> 01:22.400
Let's also change the name of the node.

01:22.420 --> 01:31.690
So here this is the simple serial transmitter and let's change the name of the subscriber.

01:31.810 --> 01:36.310
So this one now is subscribed to the topic serial

01:38.830 --> 01:39.850
transmitter.

01:40.420 --> 01:47.590
Still in this topic, we are going to publish string messages so we can leave the type of the message.

01:47.590 --> 01:51.970
So the interface that is used for the communication within this topic unchanged.

01:51.970 --> 01:58.690
So we can leave this one to be string messages and now we can implement the actual logic that sends

01:58.720 --> 02:05.350
each of the messages received on the serial transmitter topic to the Arduino through the serial port.

02:06.050 --> 02:12.380
For this, we can use the lib serial library that is available in C plus plus and that we have downloaded

02:12.380 --> 02:15.110
in the first setup lessons of this course.

02:15.290 --> 02:17.840
So let's start by including this library.

02:18.080 --> 02:22.730
So let's include the lib serial library.

02:22.730 --> 02:26.720
And from this one, let's take the definition of the serial port.

02:27.280 --> 02:33.880
Then here, among the private variables of the simple serial transmitter class, let's add a new private

02:33.880 --> 02:34.600
member.

02:34.600 --> 02:41.080
So from the lib serial, let's create a new object of type serial port.

02:41.080 --> 02:44.410
And let's call this one Arduino.

02:45.850 --> 02:48.730
Now to start the communication with the serial port.

02:48.760 --> 02:56.200
We can use the open function so we can take this object and here within the constructor we can call

02:56.200 --> 03:00.190
the function open on the serial object.

03:00.640 --> 03:06.820
And this function requires as input the port of our PC to which the Arduino board is connected through

03:06.820 --> 03:13.180
the USB cable instead of hard coding these values in the script, as of course it might change from

03:13.180 --> 03:15.400
user to user and from PC to PC.

03:15.820 --> 03:20.530
We can use the ros2 parameters to dynamically configure these values.

03:20.620 --> 03:27.580
So here, let's start by declaring a new parameter for this node with the declare parameter function

03:27.580 --> 03:30.430
which we are inheriting from the node class.

03:30.640 --> 03:33.310
And this first one is a string.

03:34.360 --> 03:43.120
So we want the type of this parameter to be a string and let's name it port and by default, in my case,

03:43.120 --> 03:49.040
I'm going to set it to the port dev 80 ACM zero.

03:49.040 --> 03:51.710
So to the port ACM zero of my PC.

03:52.880 --> 03:54.200
It did not start up.

03:54.200 --> 03:58.700
So when actually the node starts, we want to read this parameter.

03:58.700 --> 04:04.310
So with this instruction we are declaring this parameter and now we want also to read it in order to

04:04.310 --> 04:12.710
use it to open the communication with the correct port so we can use the function get parameter which

04:12.710 --> 04:15.470
still we are inheriting from the node class.

04:15.650 --> 04:24.290
And let's read the content of the parameter port and let's save it as a string.

04:24.320 --> 04:29.600
Since we have declared this to be a string and let's store it within a new variable.

04:30.950 --> 04:34.430
So within a new string variable that we call port.

04:34.580 --> 04:38.810
And so it contains the output of this get parameter function.

04:39.650 --> 04:45.860
Now we can use this variable here so the port and we can pass it to the open function so that it will

04:45.860 --> 04:49.460
start opening the serial communication with the requested port.

04:49.460 --> 04:51.110
So with the defined port.

04:52.010 --> 04:58.370
After opening and initializing the communication, we also need to define the baud rate at which the

04:58.370 --> 04:59.930
communication should occur.

05:00.380 --> 05:06.230
So again, on the Arduino object, let's use the function set baud rate.

05:06.500 --> 05:08.100
And let's set this one.

05:08.120 --> 05:14.330
So from the lib serial, let's set the baud rate to be this one.

05:14.330 --> 05:17.990
So 1115 200.

05:18.230 --> 05:24.140
Since we opened the serial communication with the Arduino in the constructor of the simple serial transmitter

05:24.140 --> 05:28.640
class, we also need to close it when the nodes ends its execution.

05:28.640 --> 05:31.100
And so this way we need to prevent that.

05:31.100 --> 05:33.350
The connection remains open forever.

05:33.980 --> 05:41.960
To do so, we can create the destructor so the simple serial transmitter destructor.

05:41.960 --> 05:47.060
And within this function, let's simply close the connection.

05:47.060 --> 05:53.760
So if it was open, so if the connection was still open with the Arduino, so with the serial port,

05:53.790 --> 06:00.120
this destructor that is called when we Destroy the node will automatically close the connection so that

06:00.120 --> 06:04.050
other nodes and other application can use the serial communication.

06:04.940 --> 06:07.970
Now at this point in the message callback function.

06:07.970 --> 06:14.810
So here, which is executed upon receiving a new message in the serial transmitter topic, we just want

06:14.810 --> 06:20.060
to send the message that was just received to the Arduino using the serial port.

06:20.330 --> 06:24.590
So let's first change the message that we are going to print in the terminal.

06:24.590 --> 06:38.690
So the informative message here and let's print new message received and then publishing on serial port

06:38.870 --> 06:42.530
and followed by the message data.

06:42.860 --> 06:48.020
So this one is the message that we are going to publish to the serial port.

06:48.470 --> 06:55.910
Now in order to publish this message to the serial port, so in order to write this message to the Arduino,

06:56.390 --> 06:59.870
let's use the function, right?

07:00.590 --> 07:06.390
And so this function takes as input the message that we want to publish so that we want to transmit

07:06.390 --> 07:07.680
through the serial port.

07:07.860 --> 07:13.050
And this is the content of the message that we just received within the topic.

07:13.050 --> 07:14.780
So message data.

07:15.470 --> 07:20.690
Let's just remove the cost statement of this function.

07:20.690 --> 07:27.500
And so now is fine because otherwise it won't be able to modify any object of the class.

07:27.830 --> 07:33.710
With this, our node is completed and so now we can move on to install and launching it to see how it

07:33.710 --> 07:34.330
works.

07:34.340 --> 07:39.170
So let's start by installing it within the file Cmakelists.txt.

07:39.890 --> 07:51.530
And here let's add the dependency from the Rql CPP library and also another one from the standard messages.

07:54.950 --> 07:58.430
From which we have used the definition of the string message.

07:58.730 --> 08:02.660
Then also we need to declare the dependency from the serial library.

08:03.320 --> 08:07.070
So let's use the package.

08:09.000 --> 08:21.990
Config and let's use this one for the instruction package, check modules and check the serial module

08:22.770 --> 08:26.340
and so the lib serial library.

08:26.550 --> 08:32.910
Now to compile and make our node executable, let's add a new instruction.

08:33.180 --> 08:36.830
Add executable.

08:36.840 --> 08:48.390
Let's call our executable simple serial transmitter and the source code is in the source folder and

08:48.390 --> 08:54.570
is in the simple serial transmitter dot cpp.

08:55.460 --> 09:02.780
Then also let's specify the dependencies that this node needs with the aim and target

09:05.000 --> 09:06.090
dependencies.

09:06.140 --> 09:09.950
So the node is the simple serial transmitter.

09:11.360 --> 09:19.490
And it needs the dependency from the CPP library, from the standard messages, and also in order to

09:19.490 --> 09:29.030
add the dependency from the Lib serial library, let's use another instruction of type target include.

09:31.340 --> 09:32.890
A directories.

09:33.070 --> 09:36.760
So still for the simple serial transmitter node.

09:37.000 --> 09:39.760
Let's declare the usage of the library.

09:40.450 --> 09:49.550
So let's declare the serial include dears.

09:50.230 --> 09:51.610
Here is dears.

09:51.970 --> 09:59.460
Finally, we need one more instruction that is target link libraries.

09:59.470 --> 10:07.690
Still, for the simple serial transmitter node, this is public, and for the simple serial transmitter

10:07.690 --> 10:12.880
node, we need to add the serial.

10:14.560 --> 10:17.340
And this goes into the double quotes.

10:17.350 --> 10:22.870
So this one and also this variable here goes into the double quotes.

10:23.500 --> 10:28.450
Now to install the executable, let's add a new install instruction.

10:30.070 --> 10:40.930
So here and let's install the target that we want to install is the simple serial transmitter node.

10:41.440 --> 10:49.660
So this one and the archive destination is the lib folder.

10:49.930 --> 10:52.420
Then the library

10:55.360 --> 11:06.160
destination is again the lib folder and the runtime destination is the bin folder.

11:06.580 --> 11:15.250
And the destination of the executable is the lib folder and the sub folder that has the same name of

11:15.250 --> 11:16.150
the package.

11:16.270 --> 11:21.400
And so we access with the project name variable.

11:22.300 --> 11:24.980
Also let's modify the package dot XML.

11:25.000 --> 11:32.860
Since we added some more dependencies to this email list and here let's add the dependency from the

11:34.600 --> 11:36.730
RCL cpp library.

11:37.090 --> 11:45.100
Then another one from the standard messages library and then another one.

11:45.930 --> 11:51.480
From the lib serial dev library.

11:52.260 --> 11:54.750
With this change, everything is set.

11:54.780 --> 12:00.390
So now we can conclude this lesson by building our workspace in order to see that everything compiles

12:00.390 --> 12:02.220
and so that we have no errors.

12:02.610 --> 12:06.210
So let's go to the workspace and let's build it.

12:08.920 --> 12:16.560
We can see indeed that there is an error in our list as this is runtime space destination.

12:16.570 --> 12:21.280
So let's save and let's build the workspace again.

12:21.460 --> 12:24.460
There is also another similar issue.

12:25.180 --> 12:27.130
So with the archive destination.

12:27.130 --> 12:27.940
Correct.

12:28.060 --> 12:32.350
So let's use this space and let's give it one more try.

12:36.140 --> 12:37.940
Now everything compiles.

12:37.940 --> 12:43.130
And in the next laboratory lesson, we are going to start this node and see how it works and so how

12:43.130 --> 12:45.020
it communicates with the Arduino.
