Now let's get started on coding our drones.
First I want to talk about how we can connect the drone and your computer on the right side is an image
of how your drone and computer will be connected on the left side you will see the Tello document to
send commands and receive responses.
The IP address will be one ninety two point one sixty eight point ten point one with the port number
eight eight eight nine.
There are three ways to connect with the drone.
The drone IP address is a fixed IP.
Once your laptop is connected on Wi-Fi the IP will be a one ninety two point one sixty eight point ten
point two to make your drone take off or land.
You will need to use the port number eight eight eight nine.
Once the drone accepts the commands it will respond using the same port number eight eight nine.
This first two way connection is written in the top part of the document.
The next connection here is where the drone sends you its status.
The drone will be sending you its steps constantly using the port number 8 8 9 0.
The third connection is for video streaming.
The drone will be sending the video binary using the UDP port number 1 1 1 1 we will mainly be using
the first connection to send commands and to receive response.
We will not be using the second connection.
The drone will be sending life stats which will be massive information.
These information can be used for debugging or when you want to operate your drone with more detail.
When it comes to simple programming your own drone you wouldn't need to use it much.
We will definitely be using the third connection for face recognition.
So in this course we'll be using the first and third connection.
It's always good to have an image in your head before you go into coding it clear your mind.
Now it's time to get into the programming in our previous lesson.
We were using a file we created in the tools directory.
We want to start with a fresh new project.
So right click on the director early P Y Tello G is new and click on python file.
Let's set the file name to drone manager and hit OK as we've created a new file.
We need to go to the go button.
Open edit configurations and change the path to the file name.
We just created drone manager I'll minimize the project tab here.
Let's start by importing the libraries we need.
First we import logging.
We need socket to connect.
We'll be using a C.D. out when logging.
So we need to import sis also we will be using time.
We will start setting longings before we start I want you to understand that I may not be following
the PIP 8 format or falling out of the Python code style sometimes.
For example when we start setting logging we need to set two blank lines after the import.
But if I use up two lines it may be hard to fit all the codes in the screen for you to understand the
whole script.
So due to these reasons I may be cutting out some blank lines or spaces.
Now back to the code we set the logging level to info and stream will be set to this DUCs as G.D. out
so we can have the log data shown on the console.
This will be a single file so we wouldn't be needing a logger but we will be moving this file to a different
folder to use in the framework.
So we'll set the logger and use get logger with double underscored me.
Next we create a class.
This will be used to manage your drone from your laptop so we want to name the class drone manager and
set object for that in it.
We need to set the argument with the laptop IP which was one ninety two point one sixty eight point
ten point two and the port number eight nine.
The drone IP is a fixed IP which was one ninety two point one sixty eight point ten point one and the
port number eighty nine.
Let's set the host IP in the class so we can use it later on.
Self host IP HOST PORT grown IP and drone port we will be using the drone address a lot.
So let's set the drone address with drone IP and drone port as a tuple type when using send to.
With sockets.
It's easier to have the drone address in a tuple type.
Now we create a socket self socket equals socket socket.
The first argument will be socket a f in it for the second argument.
Since we're using UDP we'll set SOC data room for those of you who want to know more about socket.
You can copy paste SOC diagram and google search for more information once we create a self socket.
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
be sending commands before that.
Let's go back to the drone document.
I want you to look at the Sen command and receive response part.
It says that we need to send a text command to the drone to start the communication between your laptop
and your drone.
Again we need to send command to the drone to initialize.
Now let's get the second part and go onto the third part.
We also need to send the text stream on to start receiving the video.
Let's go back and send the two texts to the drone in a binary through UDP connection to send the two
words reset socket send to and B which stands for binary.
And the text command.
Next we need to set where we send this text.
It'll be the self drone address.
We repeat the code so we can also send stream on.
Now we've completed the initialization.
Let's go on and create as destructor define double underscored Dell and self when the classes deleted
from the memory or if we forget to delete this self stop method will be invoked and the self stop method
will set the self socket to close
to make things easier I want to create a method that can receive the take off or land commands let's
name the self command method and set argument to command take off or land commands will be set in this
command argument as a string type for all the commands sent.
We want to leave a log so let's set a logger followed by a dictionary type action will be send command
and command will be the name that commands.
Now we set self socket send to.
We want to have the command and string type.
So we need to encode UTF 8 and we send this to the drone.
So we set the self drone address here we've completed a method to send the commands to the drone as
a binary.
Next we create a takeoff method we set to send command takeoff as written in the document.
Let's do the same for landing command was land
here we've programmed the drone to initialize and the basic commands as take off and land.
Lastly we need to set an if statement.
Once this python code is invoked we want to run this code.
If underscore name is made now we create a drone manager and initialize then set the drone to take off.
We'll set a time manager time sleep to 10 so that the drone will be in the air for 10 seconds.
Then we set the drone to land.
Now it's the moment we all waited for.
Let's run this code and watch the drone fly push the power button on your drone.
Once you see the light flashing on your drone.
Place it in a safe area.
Now we need to connect the drone to Wi-Fi click on the Wi-Fi icon and choose your telco to connect to
Wi-Fi.
Now we are ready to run the code as we run the code.
You can see the drone take off on your right side of the screen you will see the log data the log starts
with the takeoff command.
Once 10 seconds passes you will see the land command and the drone lands.
So how did your drone do.
Were you able to fly your drone.
If an error occurred and your drone didn't fly your drone may automatically shut down while you are
searching for the air.
In that case remember to reconnect the drone onto your Wi-Fi before you run the code.
OK.
Congratulations on your first flight.
I'll see you in our next lesson for more drone programming.