1
00:00:00,180 --> 00:00:04,680
In this lesson we will be programming the drones to go on a patrol flight.

2
00:00:04,700 --> 00:00:07,820
We have been going through each move that the drone can make.

3
00:00:07,830 --> 00:00:13,140
But in this lesson we will have the drone automatically continue as certain patrol routine.

4
00:00:13,140 --> 00:00:19,130
For example you can set your draw to go into the next room and fly back as your patrol route.

5
00:00:19,140 --> 00:00:20,070
Now let's get started.

6
00:00:21,230 --> 00:00:25,410
First we want to import a library called context lib.

7
00:00:25,430 --> 00:00:30,610
Go to the drill manager in it we set sell petrol event.

8
00:00:30,880 --> 00:00:35,250
We will be setting a threading event eventually but we will set it to none.

9
00:00:35,320 --> 00:00:42,310
In the beginning we will be setting a method to start and stop the patrol so we can set the details

10
00:00:42,310 --> 00:00:43,540
there.

11
00:00:43,540 --> 00:00:47,170
Next we set self is patrol to false.

12
00:00:47,170 --> 00:00:51,180
This will be a flag to see if the drone is on a patrol or not.

13
00:00:51,190 --> 00:00:57,750
Then we set a self patrol semaphore and set threading semaphore followed by one.

14
00:00:57,880 --> 00:01:03,870
This threading semaphore will manage to have only one thread while patrolling.

15
00:01:03,880 --> 00:01:10,110
Next we set thread patrol to none we've completed four attributes.

16
00:01:10,110 --> 00:01:16,670
Now we can go on to creating the methods scroll all the way down under the flip methods.

17
00:01:16,710 --> 00:01:21,930
I'll move the codes away so we have a cleaner view for this lesson.

18
00:01:22,080 --> 00:01:27,120
We start defining the patrol method which will start the patrol.

19
00:01:27,120 --> 00:01:34,840
First we need to check if the drone is currently not armed patrol by setting if not self is patrol.

20
00:01:34,840 --> 00:01:39,250
Next we set the patrol event to a threading event.

21
00:01:39,250 --> 00:01:43,170
Next we set thread patrol to threading thread.

22
00:01:43,300 --> 00:01:51,040
The target will be self underscore patrol and args will be set to self patrols and for and self patrol

23
00:01:51,040 --> 00:01:53,260
event as a tuple type.

24
00:01:53,500 --> 00:01:59,850
Set the underscore thread patrols start to start once the patrol starts.

25
00:01:59,900 --> 00:02:03,550
Self is patrol will become true.

26
00:02:03,590 --> 00:02:07,250
Next we define the under school patrol method.

27
00:02:07,250 --> 00:02:09,980
We need to set the argument to Semaphore and stop.

28
00:02:09,980 --> 00:02:13,430
Event.

29
00:02:13,670 --> 00:02:20,900
Next we'll be setting the semaphore is acquire set to Semaphore with the acquire method followed by

30
00:02:20,900 --> 00:02:25,940
blocking equals false by setting blocking equals false.

31
00:02:25,940 --> 00:02:29,220
The second thread will be blocked.

32
00:02:29,450 --> 00:02:32,480
What happens is that the first thread is acquired.

33
00:02:32,480 --> 00:02:38,000
If a second thread is acquired the process will wait to finish the first thread to go onto the second

34
00:02:39,680 --> 00:02:40,540
in this case.

35
00:02:40,550 --> 00:02:46,310
We set the blocking to false which means the process will immediately finish the second thread if it

36
00:02:46,310 --> 00:02:48,100
were to be acquired.

37
00:02:48,110 --> 00:02:48,970
Now let's move on.

38
00:02:49,820 --> 00:02:53,600
If the thread is acquired we can set a logger here.

39
00:02:53,600 --> 00:02:59,960
Action will be sent to patrol and status to acquire.

40
00:02:59,980 --> 00:03:08,740
Here we set a width statement with context lib using as it stack as stack we said stack with callback

41
00:03:08,770 --> 00:03:13,650
which will be invoked once the statement process finishes.

42
00:03:13,660 --> 00:03:18,450
Here we set the semaphore release method to finish the process.

43
00:03:18,580 --> 00:03:21,700
Initially we set the status to zero.

44
00:03:21,700 --> 00:03:25,140
We will count up the number so that the drone knows what to do.

45
00:03:26,510 --> 00:03:32,270
While not stop event is set the status will count up.

46
00:03:32,270 --> 00:03:35,400
If status is 1 the drone will go up.

47
00:03:35,630 --> 00:03:40,460
If status is to the drone will rotate clockwise 90 degrees.

48
00:03:40,460 --> 00:03:45,160
These settings is free for you to decide if status is 3.

49
00:03:45,170 --> 00:03:53,110
The drone will go down if status is for the status will go back to 0 after each move.

50
00:03:53,120 --> 00:03:59,330
We will make the drone hover for five seconds once the stop event is invoked.

51
00:03:59,380 --> 00:04:03,800
The while loop needs to break once the loop is broke.

52
00:04:03,850 --> 00:04:10,720
The stack callback will be invoked using the context lib exit stack.

53
00:04:10,720 --> 00:04:18,560
We are able to create a stack of release methods but here will only set one if you are used to Python

54
00:04:18,560 --> 00:04:18,980
2.

55
00:04:18,980 --> 00:04:23,930
You may be using try except finally with Python 3.

56
00:04:23,930 --> 00:04:28,580
This will be the new code style so remember how to use this context.

57
00:04:28,580 --> 00:04:30,190
Lib exit stack

58
00:04:33,640 --> 00:04:37,900
in case the threat is not acquired the process will finish.

59
00:04:37,900 --> 00:04:43,370
We can also set a logger to see if there were any cases where the thread was an acquired.

60
00:04:43,570 --> 00:04:51,160
We set logger warning with action underscore patrol and status not acquired.

61
00:04:51,160 --> 00:04:58,750
Once we have finished underscore patrol we will go on to set the stop patrol method defined stop patrol

62
00:04:58,750 --> 00:04:59,910
method.

63
00:05:00,040 --> 00:05:03,570
If the drone is in patrol we want to stop it.

64
00:05:03,850 --> 00:05:13,990
Set self patrol event set let's set retried to 0 while thread patrol is alive.

65
00:05:14,020 --> 00:05:17,190
We want to wait four point three seconds.

66
00:05:17,260 --> 00:05:18,580
Then we try again.

67
00:05:18,580 --> 00:05:22,280
Up to 300 if it goes up to 300.

68
00:05:22,300 --> 00:05:27,380
The Loop will break retrial needs to count up.

69
00:05:27,560 --> 00:05:31,010
Lastly we need to set is patrol as follows.

70
00:05:32,180 --> 00:05:38,810
Once the patrol event is set this while loop will be broken then the semaphore will be released and

71
00:05:38,810 --> 00:05:41,670
we'll be able to start a new thread.

72
00:05:41,690 --> 00:05:45,120
Some of you may think that the semaphore is unnecessary.

73
00:05:45,380 --> 00:05:49,310
So I'll explain it to you why we set it here.

74
00:05:49,320 --> 00:05:55,100
This patrol method will be invoked if is patrol is not true.

75
00:05:55,410 --> 00:06:02,610
The thread will run and right after the thread starts the drone will start the patrol in this case there

76
00:06:02,610 --> 00:06:05,820
is a high chance that the second thread will not run.

77
00:06:05,820 --> 00:06:06,920
Based on this code.

78
00:06:07,590 --> 00:06:13,860
But if you were to be using a web application and we invoke the patrol method twice at the same time

79
00:06:14,430 --> 00:06:17,190
then a thread may run twice at the same time.

80
00:06:19,390 --> 00:06:25,700
The chances that this happens may be very low but to avoid it to happen we need to set the semaphore

81
00:06:25,720 --> 00:06:27,280
to be sure.

82
00:06:27,280 --> 00:06:32,920
I hope you understand that this is an advanced way of programming with python and a way to minimize

83
00:06:32,920 --> 00:06:34,660
errors to occur.

84
00:06:34,810 --> 00:06:37,940
You just learned how to set a semaphore and use it.

85
00:06:37,950 --> 00:06:42,430
Context lib that are advanced Python coding while you program your drone.

86
00:06:43,150 --> 00:06:46,610
What a great way to strike two birds with a stone.

87
00:06:46,630 --> 00:06:51,370
Now we are ready to set the commands after the drone takes off.

88
00:06:51,370 --> 00:06:58,780
We will set to go on a patrol using the patrol method we'll set time sleep to 45 seconds before the

89
00:06:58,780 --> 00:07:00,300
patrol stops.

90
00:07:00,520 --> 00:07:04,550
Then the drone will land and complete the process.

91
00:07:04,560 --> 00:07:08,070
Now let's round the code.

92
00:07:08,170 --> 00:07:10,130
You can see the drone take off.

93
00:07:10,420 --> 00:07:13,270
Now it waits for the patrol method.

94
00:07:13,480 --> 00:07:20,890
You'll see that the patrol has started when the word acquire shows as a log first command will go up.

95
00:07:21,070 --> 00:07:27,760
Then make a clockwise turn 90 degrees and goes down after the drone goes down.

96
00:07:27,780 --> 00:07:34,360
The status will go back to zero and the drone will go onto its second patrol routine again.

97
00:07:34,590 --> 00:07:35,520
It flies up

98
00:07:38,900 --> 00:07:40,310
turns 90 degrees

99
00:07:44,000 --> 00:07:51,350
and goes down once you decide on a patrol routine the drone will automatically continue the routine

100
00:07:51,410 --> 00:07:53,140
until the patrol comes to a stop.

101
00:08:02,100 --> 00:08:09,610
Once the stop method invokes the patrol loop will be broken and the drone will land and finish its process.

102
00:08:10,080 --> 00:08:12,450
So that's all for our patrol lesson.

103
00:08:13,500 --> 00:08:14,760
See you in the next lesson.
