WEBVTT

00:00.140 --> 00:07.940
In the previous laboratory lesson, we saw how to use our ros2 topic to turn on or off a lead that is

00:07.940 --> 00:09.620
embedded on the Arduino board.

00:09.620 --> 00:15.620
And we also enable the communication between Ros two and Arduino using the serial port.

00:16.880 --> 00:22.910
In this lesson, we are going to develop a simple Arduino script that instead sends messages over the

00:22.910 --> 00:23.870
serial port.

00:24.780 --> 00:31.740
In the next lesson, we will develop our Ros two node in both Cplusplus and Python that reads these

00:31.740 --> 00:38.130
comments that are received from the serial port by the Arduino and republish them on a Ros two topic.

00:38.340 --> 00:44.100
And this way we will have enabled also the reverse communication that is the communication between Arduino

00:44.100 --> 00:45.030
and Ros two.

00:45.030 --> 00:50.700
And so basically Arduino will be able to publish messages within our Ros two topic.

00:51.720 --> 00:57.780
Let's start by opening the Arduino ID, and here let's create a new sketch.

00:57.780 --> 00:59.100
So a new script.

00:59.400 --> 01:01.530
So let's close the old one.

01:02.590 --> 01:08.260
And here in the setup function, which is called only once when the Arduino boots up.

01:08.290 --> 01:14.290
Let's initialize the communication with the serial port as we did in the previous lesson by using the

01:14.290 --> 01:23.200
function, begin from the serial library and let's begin this communication by using about rate of 115

01:23.830 --> 01:24.790
200.

01:25.890 --> 01:28.680
And also in this case, let's use a timeout.

01:28.680 --> 01:30.090
So let's set a timeout.

01:34.410 --> 01:35.340
Of one.

01:36.570 --> 01:43.290
In the loop function instead, which is the one that is executed repeatedly in each cycle of the microcontroller.

01:43.290 --> 01:45.000
We want to send a new message.

01:45.000 --> 01:48.370
So a new integer variable called X.

01:48.390 --> 01:55.650
So let's create this X variable as a global variable and let's initialize this 1 to 0.

01:55.650 --> 01:57.540
And so we want to publish this variable.

01:57.540 --> 02:01.260
So the this integer value in this serial port.

02:02.000 --> 02:02.660
To do so.

02:02.840 --> 02:07.700
In Arduino, we can use serial print line.

02:07.700 --> 02:12.800
So the print line function and let's publish the X variable.

02:12.800 --> 02:17.810
And then at the end of each loop, let's also increase this variable.

02:17.810 --> 02:20.210
So let's increment its value.

02:20.950 --> 02:30.220
Then in order to avoid executing this loop function too fast, let's also insert a small delay of 0.1.

02:31.120 --> 02:36.430
With this, we have completed our very simple script for Arduino that publish a message.

02:36.430 --> 02:38.770
So a counter on the serial port.

02:39.660 --> 02:41.900
At this point, we can save this script.

02:41.910 --> 02:43.740
So let's save it.

02:43.890 --> 02:51.570
And again, let's save it within our workspace and within the Arduino boot firmware and within the firmware

02:51.570 --> 02:52.530
sub folder.

02:52.530 --> 03:00.840
And let's call this one simple serial transmitter.

03:00.840 --> 03:03.120
And so let's save this script.

03:05.460 --> 03:11.880
Let's conclude this lesson by compiling this file in order to see that actually there are no errors.

03:11.880 --> 03:15.300
And so let's upload it into our Arduino board.

03:15.420 --> 03:19.440
So let's connect the board to the PC using an USB cable.

03:21.790 --> 03:24.550
And let's go to the tools here.

03:24.550 --> 03:25.180
The port.

03:26.040 --> 03:32.130
Let's select the port of your PC to which the Arduino board is connected and then click on upload.

03:32.520 --> 03:36.480
So this will compile again and upload the code into the Arduino.
