WEBVTT

00:00.110 --> 00:05.360
In this lesson, we will see how to control a servo motor with Arduino and Ros to.

00:06.280 --> 00:11.530
We will use the knowledge acquired in the previous lesson to publish a certain position command for

00:11.530 --> 00:14.530
a servo motor within a ros2 topic.

00:15.210 --> 00:21.600
This command will be received and serialized by the transmitter node implemented in the previous lesson

00:21.600 --> 00:25.140
and then will be sent to the Arduino through the serial port.

00:26.760 --> 00:33.840
Upon receiving this command instead of turning on or off a Led as we did in the previous lesson, Arduino

00:33.840 --> 00:38.250
will instead actuate a servo motor to our desired position.

00:38.610 --> 00:43.890
So let's open the Arduino IDE and here let's create a new sketch.

00:43.890 --> 00:45.360
So a new project.

00:46.500 --> 00:52.350
And within this new sketch still we have the setup function, which is executed once at the startup

00:52.350 --> 00:56.850
of the Arduino and the loop function, which is repeatedly executed.

00:57.630 --> 01:01.440
To interact with the servo motor and to send commands to control it.

01:01.620 --> 01:04.710
Arduino as a nice library called Servo.

01:05.010 --> 01:07.950
So let's start by importing this library.

01:09.990 --> 01:16.530
So let's include the library server dot age.

01:18.000 --> 01:21.330
And let's use this library to create a new variable.

01:21.330 --> 01:24.390
So a new object of type server.

01:24.510 --> 01:27.000
And let's call this one motor.

01:27.790 --> 01:28.630
This class.

01:28.630 --> 01:34.990
So the server class contains a series of functions and tools that make it very easy to send commands

01:34.990 --> 01:37.090
and so to operate a servo motor.

01:37.840 --> 01:40.030
To initialize the communication with the servo motor.

01:40.060 --> 01:41.140
So let's go.

01:41.140 --> 01:46.750
Here in the setup function, we need to use the motor object that we have just created.

01:46.750 --> 01:51.130
And on this one let's use the function attach.

01:52.220 --> 01:58.200
So with this function, we can pass the number of the pin to each servo motor is connected.

01:58.220 --> 02:04.940
For example, let's assume that we want to connect the servo motor to the PIN number eight of our board.

02:05.300 --> 02:11.240
And now still using the servo library, we can move our servo motor to a certain angle.

02:11.240 --> 02:18.350
And this is as simple as calling the function right on the object motor.

02:18.350 --> 02:25.310
And to this function we need to pass the angle in degrees to which we want to rotate the servo motor.

02:25.430 --> 02:33.980
For example, at the start up, let's make the motor to be in the position 90 degrees so in the middle

02:34.010 --> 02:35.150
of its range.

02:35.450 --> 02:41.240
Now still in the setup function after initializing the communication with the motor and after moving

02:41.240 --> 02:43.430
it to the position 90 degrees.

02:43.460 --> 02:50.330
Let's also initialize the serial communication between the Arduino and our PC on which Ros two is running.

02:50.810 --> 02:59.610
So let's use the function begin and then let's set the about rate that we want to use for this communication

02:59.610 --> 03:03.120
to 115 200.

03:03.610 --> 03:08.450
And then let's also, as usual, use a timeout.

03:08.470 --> 03:14.560
So with the function set timeout and let's set a timeout of one.

03:14.770 --> 03:17.250
With this, the setup function is completed.

03:17.260 --> 03:19.720
And now regarding the loop function.

03:19.720 --> 03:24.880
So regarding this function here, in this case, we want the microcontroller.

03:24.880 --> 03:31.870
So at each iteration of the loop function, we want to check if there is any new message available on

03:31.870 --> 03:32.830
the serial port.

03:32.830 --> 03:36.040
So if there is any new message coming from Ros two.

03:36.430 --> 03:43.480
So let's check this one with the serial available function.

03:43.600 --> 03:47.950
So with this one, we are checking if there is any new message.

03:47.950 --> 03:57.010
And in this case, so if there is a new message available, let's read it with the function read String.

03:57.400 --> 04:04.430
And since we know that we are going to receive an integer, let's also convert this one to an integer.

04:04.610 --> 04:10.910
Let's store this integer here within a new variable and let's call this one angle.

04:10.910 --> 04:12.680
And now we can use this variable.

04:12.680 --> 04:17.900
So the angle variable in order to move the servo motor in the desired position.

04:17.900 --> 04:29.390
And so we can do this by using the function motor dot write and so we can write the angle position that

04:29.390 --> 04:30.380
we just set.

04:31.120 --> 04:40.300
Finally, let's also add a delay in the loop function here of 0.1 so that there is so that there is

04:40.300 --> 04:43.960
a small interval between two execution of the loop function.

04:44.720 --> 04:46.520
We can save this code.

04:46.970 --> 04:54.350
So let's save it within the Arduino workspace, within the Arduino board firmware, and within the firmware

04:54.350 --> 04:55.070
folder.

04:55.340 --> 05:03.420
And let's call this one simple servo control.

05:03.440 --> 05:04.820
So let's save it.

05:06.920 --> 05:12.830
Now before actually uploading this code on the Arduino board, let's verify that it compiles so that

05:12.830 --> 05:14.360
we have no errors.

05:14.360 --> 05:16.060
So it compiles properly.

05:16.070 --> 05:21.410
So now, before uploading the code into the Arduino board, let's first connect the servo motor to the

05:21.410 --> 05:22.070
board.

05:22.190 --> 05:27.530
And we are going to need three jumper wires to connect the servo motor pins to the board.

05:34.870 --> 05:40.330
And let's connect the brown wire to the GMD pin of the Arduino.

05:46.910 --> 05:52.940
Then let's connect the red wire to the board's power, which is the five volt pin.

05:56.780 --> 05:58.060
And the last wire.

05:58.070 --> 06:02.750
So the orange one is the one that transmits the signal to move the robot.

06:02.750 --> 06:08.960
And so we are going to connect it to the PIN eight of the Arduino, as we said in the setup function

06:08.960 --> 06:10.010
of our script.

06:15.080 --> 06:18.530
Now that's the Servomotor is connected to the PIN eight of the Arduino.

06:18.560 --> 06:21.380
We can proceed to connect the Arduino to the PC.

06:29.930 --> 06:37.460
And once we do so, the Arduino IDE should be able to automatically detect the port to which it is connected.

06:37.550 --> 06:44.490
So if we go to the board now, we can see that in my case it is connected to the port ACM zero.

06:44.510 --> 06:48.410
So let's click on this one and let's upload the code on the Arduino.

06:50.100 --> 06:55.830
Now, as soon as we upload the code, we can see that the servo motor moves to its initial position,

06:55.830 --> 06:59.820
which is 90 degrees, as we indicated in the setup function.

07:00.390 --> 07:06.450
Now we can finally send movements to the motor and to do so, let's open a new terminal.

07:07.470 --> 07:10.650
And here let's go to our workspace.

07:10.650 --> 07:11.790
Let's source it.

07:12.390 --> 07:15.570
So let's source the setup dot bash.

07:15.570 --> 07:21.420
And now let's see how we can send command to our servo motor using our ros2 topic.

07:21.840 --> 07:30.000
So as a first thing, let's run a Ros two node that is in the Arduino bot firmware package and that

07:30.000 --> 07:32.070
we have implemented in the previous lesson.

07:32.100 --> 07:37.590
That is the simple serial transmitter here.

07:37.590 --> 07:42.990
I'm using the Cplusplus implementation, but if you want you can use the Pi implementation by adding

07:42.990 --> 07:46.080
the suffix dot pi to this node.

07:46.110 --> 07:51.690
We need to pass a Ros argument, so we need to pass a parameter.

07:52.020 --> 07:55.110
So a value to the parameter port.

07:55.260 --> 07:58.710
And in my case this is connected to the port.

07:59.910 --> 08:02.910
ACM, ACM zero.

08:03.090 --> 08:10.510
So let's press enter and upon starting this node does nothing but initializing the communication with

08:10.510 --> 08:11.020
the Arduino.

08:11.020 --> 08:17.830
So the serial communication with the Arduino and this node is listening on a topic on a Ros two topic

08:17.830 --> 08:23.680
for any new message that is sent through that topic in order to forward it to the Arduino through the

08:23.680 --> 08:24.580
serial port.

08:25.330 --> 08:30.970
So let's open a new terminal and let's use the command Ros two topic list.

08:31.850 --> 08:35.630
To see that actually there is the serial transmitter topic.

08:35.810 --> 08:39.200
So now let's use this topic to move our servo motor.

08:39.530 --> 08:48.090
So with the command Ros2 topic pub and let's publish a message within the serial transmitter.

08:48.110 --> 08:52.130
Here the message type that is accepted is string.

08:52.460 --> 08:55.040
So let's insert a standard message string.

08:55.070 --> 08:56.580
Then let's use double quote.

08:56.600 --> 09:02.680
Let's type D and press tab, and this will insert an empty prototype of the string message.

09:02.690 --> 09:05.420
And for example, let's publish the message zero.

09:05.420 --> 09:06.960
So the angle zero.

09:06.980 --> 09:11.950
So with this, we are asking the Arduino to move the servo motor to the position zero.

09:11.960 --> 09:13.580
So let's press enter.

09:14.420 --> 09:20.330
And indeed we can see that as soon as we publish this message, it is serialized and sent to the Arduino,

09:20.330 --> 09:22.640
which then moves the motor accordingly.

09:22.850 --> 09:25.220
Let's send another message.

09:25.250 --> 09:30.920
This time let's make the servo motor reach the position 180 degrees.

09:30.920 --> 09:32.630
So let's press enter.

09:33.270 --> 09:36.600
And again the servomotor is moved as requested.

09:36.600 --> 09:38.400
So in the desired position.

09:38.610 --> 09:45.120
Building upon this code, in the next lesson, we are going to extend this control to all the four servo

09:45.120 --> 09:46.280
motors of the robot.

09:46.290 --> 09:49.860
And so we are going to control all the servo motors of our robot.
