WEBVTT

00:00.560 --> 00:07.070
In this laboratory lesson, we're going to develop a code for the Arduino that constantly reads the

00:07.070 --> 00:11.150
serial port, listening for new messages coming from Ros2.

00:11.810 --> 00:18.650
Additionally, upon receiving a new message, it will turn on or off the Led that is integrated on the

00:18.650 --> 00:19.280
board.

00:19.860 --> 00:26.460
To complete this lesson, you will need the Arduino board and a USB cable to connect it to your PC.

00:27.690 --> 00:30.480
All the code we are going to write during this lesson.

00:30.480 --> 00:36.900
Use a programming language that is a variant of the Cplusplus specifically designed for the Arduino.

00:37.320 --> 00:42.690
If you are familiar with Cplusplus, then you will have no difficulties into following along with the

00:42.690 --> 00:46.140
development, even if you never used the Arduino before.

00:46.620 --> 00:52.020
However, if you are not familiar with Cplusplus, it might be a bit more challenging to understand

00:52.020 --> 00:52.800
the code.

00:52.830 --> 00:58.290
Nevertheless, as I always say, this is not a programming course but a robotics course.

00:58.290 --> 01:05.520
So try to grasp the key concepts and the logic rather than the syntax of the language that we are using.

01:05.820 --> 01:13.320
In the following lessons, we are going to develop both Python and Cplusplus node for Ros2 to send messages

01:13.320 --> 01:15.000
to the Arduino serial port.

01:15.000 --> 01:20.520
And with this we are going to enable the communication between the Ros two and the Arduino.

01:20.800 --> 01:26.040
Then also in the following lessons, we are also going to enable the reverse communication.

01:26.040 --> 01:31.210
So the one from Arduino to Ros two enabling Arduino to send messages to Ros two.

01:31.930 --> 01:38.110
Let's start by creating a new rescue package that will contain all the code developed in this section.

01:38.350 --> 01:42.700
So let's go to our workspace and to the source folder.

01:42.700 --> 01:45.270
And here let's create a new package.

01:45.280 --> 01:50.230
So you are already familiar with the rescue package, create command.

01:50.230 --> 01:55.880
And in this case, let's use as build type and.

01:57.570 --> 02:03.630
See make as we are going to insert both Python and C plus plus scripts in this package.

02:03.720 --> 02:10.380
And then let's name this package, Arduino Bot firmware.

02:10.530 --> 02:12.210
So let's press enter.

02:12.210 --> 02:16.500
And this has created our new Arduino Bot firmware package.

02:16.500 --> 02:23.820
Then let's go back to the workspace and let's build it so that the new package is recognized and is

02:23.820 --> 02:25.080
available in Ros two.

02:25.110 --> 02:31.050
So this one since in this lesson we want to develop the code for the Arduino board, which receives

02:31.080 --> 02:35.310
a command from the serial port and turns on or off a Led.

02:35.340 --> 02:42.000
Accordingly, we are going to use the Arduino IDE, which had you download in one of the early setup

02:42.000 --> 02:42.630
lessons.

02:42.630 --> 02:43.950
So this one.

02:44.560 --> 02:47.410
In this ID, so let's make it bigger.

02:47.500 --> 02:53.770
Besides writing and compiling the code for the board, we can also directly upload the code to the board

02:53.770 --> 02:56.500
once we are going to connect it through the USB cable.

02:56.500 --> 03:01.390
So here we can compile it and here we can also upload the code to the board.

03:02.060 --> 03:08.810
By default, when we create a new project within the Arduino IDE, it comes initialized with a script

03:08.810 --> 03:14.180
that contains two functions the setup function and the loop function.

03:14.420 --> 03:15.500
The first one.

03:15.500 --> 03:21.890
So this one, the setup function will contain all the instructions that the Arduino needs to execute

03:21.890 --> 03:25.490
only once, so only at the startup of the board.

03:25.750 --> 03:28.070
Instead, the loop function.

03:28.070 --> 03:34.820
This one will contain all the instructions that the Arduino needs to execute repeatedly so at regular

03:34.820 --> 03:38.300
time intervals defined by the controller's frequency.

03:38.630 --> 03:41.410
So let's start by defining a new global variable.

03:41.420 --> 03:46.010
So let's go here and let's define a new global variable.

03:47.120 --> 03:53.540
And let's call this one let pin and let's set it to 13.

03:53.720 --> 04:00.020
This indicates the number of the pin that is on the Arduino board to which the Led is connected.

04:00.020 --> 04:04.890
And this is the onboard Led that is already integrated with the board itself.

04:05.190 --> 04:11.610
And we want to turn this led on or off based on the message that was received on the serial port.

04:12.000 --> 04:18.300
So now in the setup function, so the one that is executed only once when the controller starts up,

04:18.390 --> 04:20.790
we need to initialize this specific pin.

04:20.790 --> 04:24.570
So the PIN 13 where the Led is connected.

04:24.660 --> 04:37.080
And so let's use the function pin mode to set the Led pin variable and to set this one of type output.

04:37.170 --> 04:44.190
So basically with this instruction we are setting that we want to send an output command to this pin

04:44.190 --> 04:47.550
here in order to turn on or off the Led.

04:48.700 --> 04:49.540
By default.

04:49.540 --> 04:55.360
So at the controller start up, still in the setup function, we want the Led to be off.

04:55.570 --> 05:08.320
So let's use the digital write function to write on the Led pin and let's write a low tension.

05:08.320 --> 05:12.580
And so this basically will turn off the Led if it was on.

05:13.330 --> 05:15.910
Next, still in the setup function.

05:15.910 --> 05:18.790
Let's also initialize the serial communication.

05:18.790 --> 05:28.660
So the serial communication with the our PC and so with the Ros two world and let's use the serial begin.

05:28.900 --> 05:32.500
And within this function we have to specify the bound of the connection.

05:32.500 --> 05:41.440
So let's set it to 115 200 and additionally let's set the timeout for the serial communication.

05:41.440 --> 05:47.920
So serial set timeout and let's set a timeout of one.

05:48.710 --> 05:54.710
With this, the board initialization is completed, so the setup function is completed and now we can

05:54.710 --> 05:57.590
move on to define the behavior of the loop function.

05:57.590 --> 06:02.150
So the one that is executed repeatedly after the setup function.

06:02.420 --> 06:10.940
So here, let's check if we have any new messages on the serial port with the instruction serial available.

06:12.530 --> 06:17.120
So if there is any new message available on the serial port.

06:17.120 --> 06:20.960
So this means that our Ros two node sent a new message to the Arduino.

06:20.990 --> 06:24.350
Now we want to read this message within the Arduino.

06:24.530 --> 06:28.820
So to read this message, let's use the serial read.

06:32.030 --> 06:35.870
String function and let's convert this one to an integer.

06:36.110 --> 06:42.410
Since we know that all the messages that we are going to receive from our ros2 node are going to be

06:42.410 --> 06:43.190
integers.

06:43.400 --> 06:48.890
And let's store the output of this function into a new variable called X.

06:49.310 --> 06:52.850
And now let's check which is this message that we have just received.

06:52.880 --> 06:55.790
So basically, let's check which is the message.

06:55.790 --> 06:59.360
So the content of the message that we have just received.

06:59.570 --> 07:05.720
So if the value of X is equal to zero.

07:06.050 --> 07:09.170
Now, in this case, we want to turn off the Led.

07:09.290 --> 07:11.330
So let's use again the digital.

07:13.630 --> 07:18.910
Write function to write on the pin of delete.

07:18.910 --> 07:21.610
And we want to write again a low tension.

07:21.610 --> 07:30.640
So in order to turn off the light, otherwise so else if we receive any other number, so any other

07:30.640 --> 07:33.430
command we want to turn on delete.

07:33.430 --> 07:42.310
So again, let's use the digital write function to write on the Led pin.

07:42.310 --> 07:45.490
And in this case we want to write an high voltage.

07:45.490 --> 07:51.070
And so basically this will turn on the Led finally at the end of the if statement.

07:51.070 --> 07:59.770
So here in order to avoid this loop to be executed too rapidly, let's add a small delay of 0.1.

08:01.240 --> 08:07.330
With this, we have completed our very simple script for the Arduino that receives a message from the

08:07.330 --> 08:13.570
serial port and turns off or on the Led based on the message that we have received.

08:14.080 --> 08:16.450
Now let's save this file.

08:16.450 --> 08:25.210
So with Ctrl s and let's save it within our workspace and so within the new package that we have created.

08:25.210 --> 08:34.030
So the Arduino bot firmware and here let's create a new folder called firmware and this will contain

08:34.030 --> 08:38.370
all the code for the Arduino that we are going to develop in this section.

08:38.380 --> 08:39.910
And let's call this one.

08:39.910 --> 08:46.270
So let's call this script simple serial receiver.

08:46.480 --> 08:48.970
So let's save it under this name.

08:50.640 --> 08:57.510
Let's conclude this lesson by compiling and loading this code into the Arduino board so we can compile

08:57.510 --> 09:00.240
it to verify that there are no no errors.

09:00.240 --> 09:03.810
And actually we can see that there is an error indeed.

09:04.640 --> 09:06.380
Because here there is a typo.

09:06.500 --> 09:08.810
Time out.

09:09.290 --> 09:11.870
Let's save and let's compile again.

09:12.290 --> 09:16.940
And now we can see that it compiled properly so there are no more errors.

09:17.750 --> 09:19.910
And now let's save it one more time.

09:20.630 --> 09:26.660
And now we can connect our Arduino board to our PC using a USB cable.

09:34.920 --> 09:40.500
And before uploading the code to the board, we need to configure the Arduino ID to indicate the type

09:40.500 --> 09:44.970
of the Arduino board that we are using and also the port to which it is connected.

09:45.270 --> 09:51.440
So in the tools section, let's change the board to the board that you are using.

09:51.450 --> 09:56.610
In my case, I'm using an Arduino Uno and then still in the tools section.

09:56.640 --> 09:58.170
Let's set the port.

09:58.170 --> 10:04.350
And here, as you can see, the Arduino IDE was able to automatically detect that at the port.

10:04.380 --> 10:08.480
ACM zero in my case is connected an Arduino Uno.

10:08.550 --> 10:14.160
So let's click on this port and be aware that in your case it can be connected, for example, to a

10:14.160 --> 10:16.650
different port with a different name.

10:17.040 --> 10:22.170
So once you select the port correctly, now we can click on upload.

10:23.020 --> 10:28.810
And this command will first compile the code to check for any errors, and then we load it into the

10:28.810 --> 10:32.470
Arduino boards and also it will automatically start its execution.
