1
00:00:00,390 --> 00:00:05,400
In our previous lesson we were able to send the commands from the laptop to the drone.

2
00:00:05,400 --> 00:00:11,010
In this lesson we will be writing code so we can receive the response from the drone to the laptop.

3
00:00:12,680 --> 00:00:19,070
When we are sending commands the drone we are able to handle when and what to send to the drone although

4
00:00:19,130 --> 00:00:25,550
when receiving a response from the drone we wouldn't be able to manage the timing to be able to receive

5
00:00:25,550 --> 00:00:26,390
the response.

6
00:00:26,390 --> 00:00:29,820
We will use a thread and create a while loop.

7
00:00:30,200 --> 00:00:37,040
Once the laptop receives a response it will receive the data and leave a log to use the thread.

8
00:00:37,040 --> 00:00:41,400
We have to import threading according to the pep 8 format.

9
00:00:41,420 --> 00:00:47,900
It's best that that libraries are imported in alphabetical order so threading will come between sis

10
00:00:48,020 --> 00:00:49,550
and time.

11
00:00:49,550 --> 00:00:55,070
Now let's go on and add codes to receive the response before getting into it.

12
00:00:55,070 --> 00:01:01,760
We've created socket said 2 with a binary command but since we created a set and command method we can

13
00:01:01,760 --> 00:01:05,570
rewrite this with a send command and command.

14
00:01:05,570 --> 00:01:13,820
We can do the same for a stream on now we're ready to set the response codes first we set self response

15
00:01:13,850 --> 00:01:15,330
to a noun.

16
00:01:15,410 --> 00:01:17,390
Here we want to use a thread.

17
00:01:17,630 --> 00:01:20,050
In this case we want to stop the thread.

18
00:01:20,120 --> 00:01:29,540
When an event occurs set self stop event and use threading event once we set the two codes we need to

19
00:01:29,540 --> 00:01:33,740
define a method to receive the response.

20
00:01:33,740 --> 00:01:39,810
The method name will be received response and set the argument as stop event.

21
00:01:39,830 --> 00:01:45,260
Also we said a while loop that will go on until the stop event occurs.

22
00:01:45,260 --> 00:01:49,820
Once the stop event occurs the loop should break.

23
00:01:49,820 --> 00:01:50,660
Next we set.

24
00:01:50,660 --> 00:01:53,090
Try self response.

25
00:01:53,090 --> 00:01:55,210
Currently unknown.

26
00:01:55,220 --> 00:02:03,290
We set the IP as self socket received from and we set a buffer size to three thousand.

27
00:02:03,310 --> 00:02:08,070
This three thousand is the same as a sample code in the Tello document.

28
00:02:08,090 --> 00:02:11,240
You can also set it to a thousand twenty four.

29
00:02:11,330 --> 00:02:16,480
Once we receive a response we want to use the logger info to print it out.

30
00:02:16,640 --> 00:02:26,180
Let's set action to receive response and response to self response in case a socket error occurs.

31
00:02:26,190 --> 00:02:33,930
We also want to log the error so we set an exception to socket error as the ex set the logger to log

32
00:02:33,930 --> 00:02:34,660
the error.

33
00:02:34,890 --> 00:02:41,160
Action will be received response an exception will be x.

34
00:02:41,550 --> 00:02:45,700
In this case if an error occurs let's break the loop.

35
00:02:45,900 --> 00:02:52,400
If an error occurs we may not be able to receive any responses so we should finish the loop.

36
00:02:52,470 --> 00:03:00,360
Next we will run this method with the thread set response thread to threading thread for the target

37
00:03:00,390 --> 00:03:03,170
we set self receive response.

38
00:03:03,570 --> 00:03:07,880
As this is a thread we won't need to follow with the parentheses.

39
00:03:07,950 --> 00:03:13,120
Now we set ARGs and set the argument as self stop event.

40
00:03:13,140 --> 00:03:16,680
Next we set the argument as a tuple type.

41
00:03:16,680 --> 00:03:24,010
At last we set the response thread to start we might be using the response thread later on.

42
00:03:24,060 --> 00:03:29,870
So we will add self-taught underscore before the method name to keep it private.

43
00:03:30,000 --> 00:03:32,100
The timing to invoke the stop event.

44
00:03:32,100 --> 00:03:36,300
We need to set it to under the stop method.

45
00:03:36,750 --> 00:03:45,390
We need to set self stop event except once the drone stops the stop event is set so that the while loop

46
00:03:45,390 --> 00:03:50,400
will end and your laptop won't receive any more responses.

47
00:03:50,400 --> 00:03:56,740
Let's scroll down to the bottom of the script after the drone manager commands to land the drone.

48
00:03:56,760 --> 00:04:01,200
We need to stop the drone manager once the stop is invoked.

49
00:04:01,200 --> 00:04:07,490
Stop underscore event dot set will invoke.

50
00:04:07,610 --> 00:04:13,250
Now I want to take a moment to briefly explain how the thread works especially for those of you who

51
00:04:13,250 --> 00:04:15,190
are not familiar with it.

52
00:04:15,200 --> 00:04:20,820
Here we set threading thread method to process multiple methods at once.

53
00:04:21,020 --> 00:04:23,470
We set the target to the method name.

54
00:04:23,480 --> 00:04:28,040
In this case it'll be received response when we set the args.

55
00:04:28,040 --> 00:04:32,400
We need to add a comma and the end to create a tuple type.

56
00:04:32,510 --> 00:04:39,500
If we don't set a comma this will become a single stop event as we create the thread we set it to start

57
00:04:40,760 --> 00:04:44,650
by running the received response method within a thread.

58
00:04:44,660 --> 00:04:48,580
This method will process what other methods are processed.

59
00:04:48,980 --> 00:04:56,000
The while loop will go on until this stop event occurs and while the loop is going on we will continue

60
00:04:56,000 --> 00:05:03,890
receiving the responses from the drone and set the data into response which will be logged by the logger.

61
00:05:04,130 --> 00:05:05,620
Let's go up the script a bit.

62
00:05:05,630 --> 00:05:12,310
We need to set the command and stream on after the thread starts so let's move the two methods here.

63
00:05:12,320 --> 00:05:15,480
Now let's run the code once we run the code.

64
00:05:15,500 --> 00:05:18,260
You can see that we are getting the log data.

65
00:05:18,350 --> 00:05:27,600
The log is showing the take off command which was received and responded OK.

66
00:05:27,640 --> 00:05:35,280
Now we see the drone land but we also see an error the error says bad file descriptor.

67
00:05:35,350 --> 00:05:38,630
Let's go back to the script to see why we got an error.

68
00:05:38,950 --> 00:05:43,430
The stop event method was invoked and broke the while loop.

69
00:05:43,750 --> 00:05:49,570
The drone was trying to send back a response but at the same time since the method is running on a thread

70
00:05:49,900 --> 00:05:53,290
the socket close method was also invoked.

71
00:05:53,290 --> 00:05:58,010
This means the drone was trying to send a response to a closed socket.

72
00:05:58,060 --> 00:06:01,330
This became the reason for the error to occur.

73
00:06:01,330 --> 00:06:07,600
To solve this problem once the stop event method is invoked we need to wait until the loop comes to

74
00:06:07,600 --> 00:06:08,280
an end.

75
00:06:08,410 --> 00:06:10,420
Then close the socket.

76
00:06:10,420 --> 00:06:15,940
We can set the system to sleep for a few seconds but in this case we want to be sure that the thread

77
00:06:15,940 --> 00:06:19,210
has finished before we close the socket.

78
00:06:19,210 --> 00:06:26,530
We set while self response thread is alive we will set the time sleep with point 3 seconds

79
00:06:29,530 --> 00:06:32,060
if the retrial goes up to 30.

80
00:06:32,140 --> 00:06:36,160
We understand it as a time up and break the loop.

81
00:06:36,160 --> 00:06:44,900
If not we can't count up one now we've completed our code so let's try running the code again once we

82
00:06:44,910 --> 00:06:51,020
run this code you will see that the command being sent and also the response from the drone is OK.

83
00:06:55,970 --> 00:07:01,850
Now the Land Command has been set and the drone lands sending the response as OK.

84
00:07:02,150 --> 00:07:09,900
This time there are no errors as we are running the process on thread the log data is showing three

85
00:07:09,900 --> 00:07:11,560
okays altogether.

86
00:07:11,870 --> 00:07:15,140
The responses should be paired with each commands.

87
00:07:15,290 --> 00:07:18,590
Let's rewrite the code so the log comes out cleaner.

88
00:07:19,040 --> 00:07:24,030
Go to the send command method set retry to zero.

89
00:07:24,080 --> 00:07:31,370
This will be similar to what we did with the self socket clothes we set while self response is none.

90
00:07:31,370 --> 00:07:39,570
We will set the time sleep with point three seconds if the retrial goes up to three we break the loop.

91
00:07:39,740 --> 00:07:49,090
If not we can count up one Next of self response is none the above was when the retrial became more

92
00:07:49,090 --> 00:07:50,230
than three.

93
00:07:50,500 --> 00:07:57,520
But this next if statement will be set for when the retrial is less than three response will be set

94
00:07:57,520 --> 00:07:59,720
temporarily to none.

95
00:07:59,770 --> 00:08:10,890
If the response is received else response will be set to self response decode UTF 8 for the self response.

96
00:08:10,890 --> 00:08:13,240
We want to initialize it back to none.

97
00:08:14,600 --> 00:08:20,150
We set to return the response that is set and the temporary response.

98
00:08:20,200 --> 00:08:21,610
Now the code is ready.

99
00:08:21,610 --> 00:08:24,040
So let's go through it once more.

100
00:08:24,100 --> 00:08:26,120
First the send command is invoked.

101
00:08:26,380 --> 00:08:31,390
Let's say it's to take off the response for the take off comes back.

102
00:08:31,390 --> 00:08:34,960
If there is no response the system will wait for it.

103
00:08:34,960 --> 00:08:38,470
If there is no response the loop will break.

104
00:08:38,470 --> 00:08:44,410
If the response does come back the value will be set to self response.

105
00:08:44,680 --> 00:08:51,990
Then the response OK will be decoded and set to be returned as we set the response to be return.

106
00:08:51,990 --> 00:08:55,880
We can rewrite the takeoff and land command to return as well.

107
00:08:57,150 --> 00:09:03,220
We finish cleaning the code so let's run the code again and see how the results come out.

108
00:09:03,810 --> 00:09:09,960
As I run the code you can see that the command and the response are paired from the top it's command

109
00:09:10,620 --> 00:09:13,940
okay stream on OK.

110
00:09:14,130 --> 00:09:20,760
The response to take off may take a bit of time but here you see the response come out as OK.

111
00:09:20,760 --> 00:09:24,630
Now the Land Command has been sent and the response comes back.

112
00:09:24,630 --> 00:09:31,690
OK here we've successfully programmed our drone with a very clean log.

113
00:09:31,730 --> 00:09:32,810
Great job.

114
00:09:32,810 --> 00:09:34,490
I'll see you in our next lesson.
