1
00:00:00,330 --> 00:00:02,970
Now let's get started on coding our drones.

2
00:00:02,970 --> 00:00:09,300
First I want to talk about how we can connect the drone and your computer on the right side is an image

3
00:00:09,300 --> 00:00:15,600
of how your drone and computer will be connected on the left side you will see the Tello document to

4
00:00:15,600 --> 00:00:17,870
send commands and receive responses.

5
00:00:17,870 --> 00:00:25,290
The IP address will be one ninety two point one sixty eight point ten point one with the port number

6
00:00:25,380 --> 00:00:27,320
eight eight eight nine.

7
00:00:27,510 --> 00:00:30,460
There are three ways to connect with the drone.

8
00:00:30,810 --> 00:00:34,220
The drone IP address is a fixed IP.

9
00:00:34,440 --> 00:00:41,910
Once your laptop is connected on Wi-Fi the IP will be a one ninety two point one sixty eight point ten

10
00:00:42,000 --> 00:00:46,110
point two to make your drone take off or land.

11
00:00:46,170 --> 00:00:51,040
You will need to use the port number eight eight eight nine.

12
00:00:51,330 --> 00:00:58,170
Once the drone accepts the commands it will respond using the same port number eight eight nine.

13
00:00:58,170 --> 00:01:03,400
This first two way connection is written in the top part of the document.

14
00:01:03,540 --> 00:01:07,430
The next connection here is where the drone sends you its status.

15
00:01:07,470 --> 00:01:13,230
The drone will be sending you its steps constantly using the port number 8 8 9 0.

16
00:01:13,260 --> 00:01:16,160
The third connection is for video streaming.

17
00:01:16,200 --> 00:01:24,280
The drone will be sending the video binary using the UDP port number 1 1 1 1 we will mainly be using

18
00:01:24,280 --> 00:01:29,220
the first connection to send commands and to receive response.

19
00:01:29,290 --> 00:01:31,680
We will not be using the second connection.

20
00:01:31,930 --> 00:01:37,180
The drone will be sending life stats which will be massive information.

21
00:01:37,180 --> 00:01:43,030
These information can be used for debugging or when you want to operate your drone with more detail.

22
00:01:43,390 --> 00:01:49,570
When it comes to simple programming your own drone you wouldn't need to use it much.

23
00:01:49,680 --> 00:01:54,090
We will definitely be using the third connection for face recognition.

24
00:01:54,120 --> 00:01:58,010
So in this course we'll be using the first and third connection.

25
00:01:58,080 --> 00:02:03,990
It's always good to have an image in your head before you go into coding it clear your mind.

26
00:02:03,990 --> 00:02:09,080
Now it's time to get into the programming in our previous lesson.

27
00:02:09,100 --> 00:02:12,470
We were using a file we created in the tools directory.

28
00:02:12,850 --> 00:02:14,860
We want to start with a fresh new project.

29
00:02:15,370 --> 00:02:22,820
So right click on the director early P Y Tello G is new and click on python file.

30
00:02:22,870 --> 00:02:29,140
Let's set the file name to drone manager and hit OK as we've created a new file.

31
00:02:29,140 --> 00:02:30,740
We need to go to the go button.

32
00:02:30,820 --> 00:02:35,350
Open edit configurations and change the path to the file name.

33
00:02:35,440 --> 00:02:43,170
We just created drone manager I'll minimize the project tab here.

34
00:02:43,170 --> 00:02:46,180
Let's start by importing the libraries we need.

35
00:02:46,320 --> 00:02:48,230
First we import logging.

36
00:02:48,420 --> 00:02:50,670
We need socket to connect.

37
00:02:50,670 --> 00:02:53,160
We'll be using a C.D. out when logging.

38
00:02:53,160 --> 00:02:59,440
So we need to import sis also we will be using time.

39
00:02:59,610 --> 00:03:06,030
We will start setting longings before we start I want you to understand that I may not be following

40
00:03:06,030 --> 00:03:11,100
the PIP 8 format or falling out of the Python code style sometimes.

41
00:03:11,190 --> 00:03:17,480
For example when we start setting logging we need to set two blank lines after the import.

42
00:03:17,820 --> 00:03:23,670
But if I use up two lines it may be hard to fit all the codes in the screen for you to understand the

43
00:03:23,670 --> 00:03:25,040
whole script.

44
00:03:25,080 --> 00:03:30,870
So due to these reasons I may be cutting out some blank lines or spaces.

45
00:03:30,870 --> 00:03:38,250
Now back to the code we set the logging level to info and stream will be set to this DUCs as G.D. out

46
00:03:38,580 --> 00:03:43,820
so we can have the log data shown on the console.

47
00:03:43,860 --> 00:03:49,950
This will be a single file so we wouldn't be needing a logger but we will be moving this file to a different

48
00:03:49,950 --> 00:03:51,800
folder to use in the framework.

49
00:03:51,900 --> 00:03:58,480
So we'll set the logger and use get logger with double underscored me.

50
00:03:58,500 --> 00:04:00,510
Next we create a class.

51
00:04:00,510 --> 00:04:07,950
This will be used to manage your drone from your laptop so we want to name the class drone manager and

52
00:04:07,960 --> 00:04:11,770
set object for that in it.

53
00:04:11,770 --> 00:04:18,130
We need to set the argument with the laptop IP which was one ninety two point one sixty eight point

54
00:04:18,130 --> 00:04:23,030
ten point two and the port number eight nine.

55
00:04:23,080 --> 00:04:30,460
The drone IP is a fixed IP which was one ninety two point one sixty eight point ten point one and the

56
00:04:30,460 --> 00:04:33,160
port number eighty nine.

57
00:04:33,250 --> 00:04:36,960
Let's set the host IP in the class so we can use it later on.

58
00:04:37,900 --> 00:04:47,890
Self host IP HOST PORT grown IP and drone port we will be using the drone address a lot.

59
00:04:47,920 --> 00:04:55,470
So let's set the drone address with drone IP and drone port as a tuple type when using send to.

60
00:04:55,480 --> 00:04:56,440
With sockets.

61
00:04:56,470 --> 00:04:59,940
It's easier to have the drone address in a tuple type.

62
00:04:59,950 --> 00:05:05,930
Now we create a socket self socket equals socket socket.

63
00:05:06,040 --> 00:05:12,920
The first argument will be socket a f in it for the second argument.

64
00:05:13,020 --> 00:05:21,730
Since we're using UDP we'll set SOC data room for those of you who want to know more about socket.

65
00:05:21,780 --> 00:05:30,150
You can copy paste SOC diagram and google search for more information once we create a self socket.

66
00:05:30,150 --> 00:05:39,670
We need to bind it we'll set saw self host IP and self host port to bind it in a tuple type now we'll

67
00:05:39,670 --> 00:05:42,290
be sending commands before that.

68
00:05:42,290 --> 00:05:44,990
Let's go back to the drone document.

69
00:05:45,030 --> 00:05:49,390
I want you to look at the Sen command and receive response part.

70
00:05:49,500 --> 00:05:56,070
It says that we need to send a text command to the drone to start the communication between your laptop

71
00:05:56,160 --> 00:05:57,990
and your drone.

72
00:05:57,990 --> 00:06:03,630
Again we need to send command to the drone to initialize.

73
00:06:03,650 --> 00:06:06,740
Now let's get the second part and go onto the third part.

74
00:06:06,740 --> 00:06:12,110
We also need to send the text stream on to start receiving the video.

75
00:06:12,110 --> 00:06:20,090
Let's go back and send the two texts to the drone in a binary through UDP connection to send the two

76
00:06:20,090 --> 00:06:26,210
words reset socket send to and B which stands for binary.

77
00:06:26,210 --> 00:06:28,260
And the text command.

78
00:06:28,320 --> 00:06:31,550
Next we need to set where we send this text.

79
00:06:31,600 --> 00:06:34,070
It'll be the self drone address.

80
00:06:34,440 --> 00:06:38,520
We repeat the code so we can also send stream on.

81
00:06:38,630 --> 00:06:41,870
Now we've completed the initialization.

82
00:06:41,970 --> 00:06:51,030
Let's go on and create as destructor define double underscored Dell and self when the classes deleted

83
00:06:51,030 --> 00:06:59,310
from the memory or if we forget to delete this self stop method will be invoked and the self stop method

84
00:06:59,580 --> 00:07:01,490
will set the self socket to close

85
00:07:04,250 --> 00:07:10,430
to make things easier I want to create a method that can receive the take off or land commands let's

86
00:07:10,430 --> 00:07:17,540
name the self command method and set argument to command take off or land commands will be set in this

87
00:07:17,540 --> 00:07:22,100
command argument as a string type for all the commands sent.

88
00:07:22,100 --> 00:07:30,370
We want to leave a log so let's set a logger followed by a dictionary type action will be send command

89
00:07:30,700 --> 00:07:35,930
and command will be the name that commands.

90
00:07:35,980 --> 00:07:39,400
Now we set self socket send to.

91
00:07:39,460 --> 00:07:41,520
We want to have the command and string type.

92
00:07:41,520 --> 00:07:46,630
So we need to encode UTF 8 and we send this to the drone.

93
00:07:46,690 --> 00:07:53,560
So we set the self drone address here we've completed a method to send the commands to the drone as

94
00:07:53,560 --> 00:07:56,030
a binary.

95
00:07:56,110 --> 00:08:03,340
Next we create a takeoff method we set to send command takeoff as written in the document.

96
00:08:03,340 --> 00:08:06,670
Let's do the same for landing command was land

97
00:08:12,350 --> 00:08:19,220
here we've programmed the drone to initialize and the basic commands as take off and land.

98
00:08:19,220 --> 00:08:22,520
Lastly we need to set an if statement.

99
00:08:22,970 --> 00:08:28,130
Once this python code is invoked we want to run this code.

100
00:08:28,130 --> 00:08:38,090
If underscore name is made now we create a drone manager and initialize then set the drone to take off.

101
00:08:39,520 --> 00:08:46,120
We'll set a time manager time sleep to 10 so that the drone will be in the air for 10 seconds.

102
00:08:46,120 --> 00:08:48,790
Then we set the drone to land.

103
00:08:48,820 --> 00:08:51,820
Now it's the moment we all waited for.

104
00:08:51,910 --> 00:08:57,610
Let's run this code and watch the drone fly push the power button on your drone.

105
00:08:57,750 --> 00:09:00,350
Once you see the light flashing on your drone.

106
00:09:00,450 --> 00:09:03,330
Place it in a safe area.

107
00:09:03,360 --> 00:09:10,080
Now we need to connect the drone to Wi-Fi click on the Wi-Fi icon and choose your telco to connect to

108
00:09:10,080 --> 00:09:11,900
Wi-Fi.

109
00:09:11,900 --> 00:09:15,620
Now we are ready to run the code as we run the code.

110
00:09:15,620 --> 00:09:23,690
You can see the drone take off on your right side of the screen you will see the log data the log starts

111
00:09:23,690 --> 00:09:25,260
with the takeoff command.

112
00:09:25,520 --> 00:09:32,600
Once 10 seconds passes you will see the land command and the drone lands.

113
00:09:32,770 --> 00:09:35,690
So how did your drone do.

114
00:09:35,710 --> 00:09:37,820
Were you able to fly your drone.

115
00:09:38,410 --> 00:09:44,380
If an error occurred and your drone didn't fly your drone may automatically shut down while you are

116
00:09:44,380 --> 00:09:46,250
searching for the air.

117
00:09:46,330 --> 00:09:52,240
In that case remember to reconnect the drone onto your Wi-Fi before you run the code.

118
00:09:53,460 --> 00:09:54,390
OK.

119
00:09:54,670 --> 00:09:57,380
Congratulations on your first flight.

120
00:09:57,460 --> 00:10:00,730
I'll see you in our next lesson for more drone programming.
