In this lesson we will be sending commands to fly the drones in different directions.
We'll be using commands such as Up down left right forward and backwards looking at the API.
You'll find some X's after the command.
These X's can be set with numbers from 20 to 500 which will be the length in centimeters.
This is based on SDK 2.0.
So maybe in the near future there may be changes in the numbers depending on the drone version.
Please revise your numbers in case there are any changes in the SDK for some of you.
You may not be familiar with centimeters in our code.
We will set the drone to measurements and feats also just to give you an idea and measurements one feat
is thirty point four eight centimeters.
Now let's get started with our codes first.
Let's start by setting a default value default distance will be set to zero point three zero.
For those of you who are familiar with centimeters.
This means 30 centimetres.
If I change to 4 0 to a 1 that will mean one meter and 30 centimetres.
I do want to warn you if you are thinking of changing this value you will be flying your drone inside
so you don't want to set the default to a big number.
For those of you who wish to use the centimeter metric you should add a code to the initial settings
you will set is imperial false.
Also we need to set the length as an argument.
We will need to set self is imperial.
Now scroll down.
Let's add this after land.
Define a method named move and set the first argument as direction in this direction argument will be
set to change to command names such as up or left the direction will be followed by distance.
If the distance is set to 30 centimetres the argument will be placed with zero point three zero for
the distance will set it as a float.
If the distance is imperial meaning it's set to FT metrics we need to rounded and multiply by thirty
eight point four eight.
If the distance is not imperial that will mean it set to centimeter metrics.
So we need to rounded up and multiply by hundred.
Now we will return self then command here we will be using f strings in the parentheses we set f followed
by a curly braces with the direction and another curly braces.
With distance Next we need to set the directions in the move method.
Let's take up as an example def up and we set the distance of the default distance we set to return
to self move with the command up and the distance once we make this object we can set the drone manager
up with the distance of 30 centimeters then self move will be returned with the up command and the distance
as zero point three.
These numbers will be set to the move method arguments.
The distance will be calculated as float.
If the distance is set to fit metrics we will need to change the number and centimeters by multiplying
with thirty point four eight
if the distance is set to centimeter metrics.
It will be multiplied by a hundred as we are using round.
The number will be round off and set as an integer.
Then the process will return.
Self command as this is at f string direction will be set up and distance will be set to 30.
If you're not familiar with F strings I'll rewrite this in a format style with curly braces 0 come a
one point format
parenthesis direction and distance.
For those of you who are familiar with Python 2 we used to use them.
Percentage marks like this.
All three codes mean the same thing.
F string started from Python three point six so I'll be using the first code style.
Feel free to use what you are familiar with.
We've completed the up method.
The rest will be very easy.
Copy the up method and paste 5 times
we need to set down.
Remember to change the command name also.
Then left and left right and right.
Forward and forward back and back.
We've completed all the methods.
Let's prepare to fly the drone.
The drone will take off and sleep for 10 seconds.
Let's use the drone.
Manage your forward method to move forward.
The default is set to 30 centimetres.
Make sure you have plenty of room and not set the numbers too high.
Now let's sleep for another 5 seconds.
Next fly to the right.
Then we fly back
flied to the left and go up last.
We go down and land.
We're all set so let's run this code.
Here you see the drone received the takeoff command.
Here we are waiting for the 10 seconds.
Now it flies forward for 30 centimetres hovers for five seconds.
Then it flies right hovers five seconds again not flies back and hovers.
Five seconds
then it flies left and hovers five seconds.
Next we go up
and down and now the drone lands.
I hope you were able to fly your drone and all the different directions.
I'll be looking forward to seeing you in our next lesson.