1
00:00:06,070 --> 00:00:11,050
Now we will learn about how we can control the servo motor using ros2.

2
00:00:11,110 --> 00:00:18,460
And for that we have to include a library which is esp32 servo library, and this will be dealing with

3
00:00:18,460 --> 00:00:23,590
the servo stuff and we will click sketch include library manage libraries.

4
00:00:25,720 --> 00:00:31,180
And from here we can search our library, which is Esp32 and Servo.

5
00:00:35,400 --> 00:00:42,630
This is the library Esp32 server and you can click install and this will be installed.

6
00:00:44,040 --> 00:00:53,130
And now we will go to the file example we will look for that included library which is here Esp32 server

7
00:00:53,130 --> 00:00:55,890
and it has the example code.

8
00:00:56,280 --> 00:01:04,800
We are going to select these three and we will take this code as a reference and we will be editing

9
00:01:04,830 --> 00:01:10,320
and modifying our code and first place we will include this library.

10
00:01:11,310 --> 00:01:13,610
There is an object of desire.

11
00:01:15,770 --> 00:01:16,070
Okay.

12
00:01:16,070 --> 00:01:18,680
Then we have to mention the server pin.

13
00:01:19,040 --> 00:01:22,220
This server pin in our case is.

14
00:01:24,040 --> 00:01:26,920
So is connected to the PIN number 12.

15
00:01:27,100 --> 00:01:28,210
Here you can see.

16
00:01:28,390 --> 00:01:31,240
So we will try to 12.

17
00:01:31,900 --> 00:01:38,860
So in the word setup, we have to mention all these conventions that is provided by the example code.

18
00:01:39,970 --> 00:01:46,540
We will copy this simply and go to the word setup.

19
00:01:46,540 --> 00:01:52,030
And here we will mention this, verify this if everything goes well.

20
00:01:52,920 --> 00:01:57,810
So our library is included successfully and it's also configured.

21
00:01:57,840 --> 00:02:01,830
Now we have to create a subscriber for the server.

22
00:02:01,860 --> 00:02:05,580
Here we will create the object of the server.

23
00:02:12,640 --> 00:02:15,310
And we can now close this in here.

24
00:02:15,310 --> 00:02:17,370
We have to create the subscriber for this.

25
00:02:17,380 --> 00:02:24,190
The message will be again and add because in the server we have to operate the server from angle zero

26
00:02:24,190 --> 00:02:29,500
to angle 45 at the maximum and we can easily handle in in it.

27
00:02:29,530 --> 00:02:39,820
Let's mention a server subscriber here, subscriber and here we will also add this line for a new subscriber.

28
00:02:40,630 --> 00:02:48,220
We are feeding the subscriber to the executor and here we have mentioned the server subscriber server

29
00:02:48,220 --> 00:02:52,510
message and here it is server callback that we have to create right now.

30
00:02:52,510 --> 00:02:58,020
And here we will write the server callback function and and there will be a position of this server.

31
00:02:58,030 --> 00:03:04,810
Let's create a variable that is in server position that will be keeping track of the server position

32
00:03:04,810 --> 00:03:09,760
as we have to limit the server position to 45 degree angle.

33
00:03:09,760 --> 00:03:18,460
So we will be creating a function and limit to maximum value and it takes the input value and the maximum

34
00:03:18,460 --> 00:03:19,060
value.

35
00:03:19,090 --> 00:03:24,400
If the value exceeds the maximum limit, it will then give us the maximum allowed limit.

36
00:03:24,430 --> 00:03:31,150
It means whenever a user inputs 180 degree angle, it will not feed the 180 degree.

37
00:03:31,150 --> 00:03:31,570
Exactly.

38
00:03:31,570 --> 00:03:36,490
Because if it feeds 180 degree angle, the server will be pushing its boundary.

39
00:03:36,520 --> 00:03:39,460
For that reason, we will limit here the server position.

40
00:03:39,460 --> 00:03:48,490
Variable server position is limited to the maximum value and then the angle that is coming from here,

41
00:03:48,490 --> 00:03:55,660
we will give it to the maximum of 14 degree angle just so we save and then write this server position

42
00:03:55,660 --> 00:03:58,390
to the my server.

43
00:03:58,570 --> 00:04:06,390
So this position will be given to the server motor and the server motor will reach that angle.

44
00:04:06,400 --> 00:04:09,880
Let's save this and verify checks.

45
00:04:09,880 --> 00:04:13,670
This is compiled now we have to upload this.

46
00:04:17,220 --> 00:04:18,980
And now this is uploaded.

47
00:04:18,990 --> 00:04:26,460
Now remember that in our circuit, this servo motor will be powered when we turn on the switch.

48
00:04:26,460 --> 00:04:33,030
By using just the power, we cannot operate the servo motor because it draws a lot of current and it

49
00:04:33,030 --> 00:04:37,260
is directly powered by the battery, not from the voltage booster.

50
00:04:37,260 --> 00:04:41,820
So you have to disconnect the wire of the esp32 and then we will turn the switch on.

51
00:04:41,820 --> 00:04:45,760
In order to operate, we will connect via Wi-Fi.

52
00:04:45,810 --> 00:04:49,320
Disconnect from here, turn on the switch.

53
00:04:50,310 --> 00:04:54,300
Now we will be connecting it via micro Ross Agent.

54
00:05:00,270 --> 00:05:02,720
And now we have run the command.

55
00:05:02,730 --> 00:05:05,130
But this is not connected yet.

56
00:05:05,160 --> 00:05:07,530
Now we will do a restart.

57
00:05:18,190 --> 00:05:19,990
And just now this is connected.

58
00:05:24,350 --> 00:05:25,970
There is the topic.

59
00:05:25,970 --> 00:05:31,820
So now you have to publish value over this topic in order to operate this server.

60
00:05:37,610 --> 00:05:38,780
It is not moving.

61
00:05:38,780 --> 00:05:43,440
So we have one thing that we have made a mistake.

62
00:05:43,460 --> 00:05:47,520
We do not allow it to execute two threads.

63
00:05:47,550 --> 00:05:51,050
We have to update this number and change it to two.

64
00:05:51,080 --> 00:05:52,040
Save it.

65
00:05:52,340 --> 00:05:59,630
And now we have to turn off the robot and plug in the esp32.

66
00:05:59,900 --> 00:06:02,690
And now upload it again.

67
00:06:07,220 --> 00:06:12,800
Okay, now disconnect the wire and turn on the button.

68
00:06:14,150 --> 00:06:17,510
Reconnect with the micro station.

69
00:06:17,510 --> 00:06:18,500
This is connected.

70
00:06:18,500 --> 00:06:20,540
And now we will publish.

71
00:06:23,400 --> 00:06:24,510
The published zero.

72
00:06:27,220 --> 00:06:28,770
Yes, this goes to zero.

73
00:06:28,770 --> 00:06:30,840
And if we publish ten.

74
00:06:33,790 --> 00:06:38,200
Justices giving this response, they will give it again.

75
00:06:39,220 --> 00:06:40,180
40.

76
00:06:45,440 --> 00:06:48,440
Yes, this is absolutely working.

77
00:06:48,470 --> 00:06:49,700
That's it for now.
