By the end of this lesson, you should be able to:
Understand how to use the takeoff(), land(), and hover(int) functions to navigate with your drone
Describe how trim affects the flight of the drone and how to use the set_trim(int, int) function to improve the drone’s hovering and flight
Complete the challenges using the skills you have learned so far.
In this lesson, you will complete the following steps. Make sure that you demonstrate your completed code to your instructor after each step:
Back on solid ground! (Landing your drone) 1.
Using the set_trim(int,int) function 2.
Challenges: Applying What You Have Learned 3.
Now that we know how to takeoff and land, let’s add some more functionality to our program by making the drone hover!
Hovering simply means that the drone will stay in a single place for a time that we specify. We can tell the drone to hover using the hover(int) command. You will see that the word int is inside the parenthesis. Int is short for integer which is a whole number. This is one of many data types that
we will learn about.
Do the following to get your file ready:
Open the takeoff.py file from the previous step.
1.
Click on the file menu and choose “Save As”
2.
In the window that appears, enter land.py 3.
Hit enter or click “Ok”
4.
You just created a new file called land.py that has all the code from your takeoff.py file! You should see that a new tab is opened in pycharm with the name land.py. This is a great way to reuse code from a previous file while also making sure that you do not mess up the code in that previous file, especially if we know it works! This is a simple way of doing version control. In later lessons, you will learn more about version control and how this is an important tool to help programmers develop larger and more complex programs!
Next, program your drone to hover longer in the air. Remember when you put a phrase inside the parentheses when you wrote a print statement? Values that go in between the parentheses are called parameters, which provide any data that a function might need in order to run. The print() function accepts a string (text) as a parameter. The drone.hover()
function parameter is the time in seconds that you would like your CoDrone Mini to hover in the air. We have ours set to three seconds, but you can edit this number to whatever you like. Note that we don’t tell the drone to land, but it does anyway. This is a built-in safety feature so that the drone doesn’t just run until the battery dies.
drone.hover(3) print("Hovering")
Your final program should look like this:
import CoDrone_mini
print("Creating drone object") drone = CoDrone_mini.CoDrone() print("Getting ready to pair") drone.pair()
print("Paired!") drone.takeoff() print("taking off") drone.hover(3)
print("Hovering") drone.land()
print("landing") drone.close()
Drones don’t always fly in a perfectly straight line, especially if there’s an air current, the battery is running low, or the drone has crashed one too many times. If you notice your drone drifting, you can use the set_trim() function at the beginning of your program (underneath pairing and before takeoff) to fix its roll, its sideway movement, or pitch, its forward and backward movement. For example, if your CoDrone Mini is drifting to the right, you can set the roll, or its sideways movement, to a negative number. This is what it would look like:
set_trim(-5, 0)
This trim will remain saved, even after powering off, until you’ve changed the trim either in the program or reset with the remote. NOTE: If you’re setting the trim right before a takeoff, make sure to add a time.sleep(1) before the takeoff() . Otherwise, the take off might be skipped.
This is what your program would look like with the trim function:
import CoDrone_mini
drone = CoDrone_mini.CoDrone() drone.pair()
drone.set_trim(-1, 0). # example: drone is drifting right, so trim to roll left a little bit
time.sleep(1) # Add a time.sleep(1) before takeoff if you're planning to set the trim before takeoff
drone.takeoff() drone.hover(3) drone.land() drone.close()
Trim refers to modifying the controller inputs to counter any aerodynamic flaws in the drone itself. Since it is nearly impossible to have a drone completely balanced perfectly in its center, they will often have to be trimmed in order to hover properly. A correctly trimmed drone should hover in one place without input from the user.
In essence, what is happening is we are compensating for the imbalance in the drone by modifying the power values of the motors. For example, if the drone is floating to the right, I will trim down the left motor values so they don’t “push” the drone to the right. Ideally, all values would be exactly the same for motor power settings, but this is rarely the case.
When drones crash, it can cause slight imperfections in their aerodynamics that are not visible to the human eye but can cause drift. This makes the drone difficult to control, which is why it is important to trim the drone before flying.
Challenge 1: CoDrone pushups
Try to make your CoDrone Mini take off and land 5 times in a row with one code! Currently, your CoDrone Mini can do one pushup with the code you have.
Rules:
The CoDrone Mini must completely leave the ground on 1.
takeoff.
The CoDrone Mini must be completely back on the ground 2.
on landing.
There is no height requirement for how high the CoDrone 3.
Mini flies.
S e e w h o c a n d o t h e m o s t C o D r o n e M i n i p u s h u p s i n 1 0 seconds!Challenge 2: Floating Away
Using your knowledge of the trim function, make the CoDrone Mini takeoff and float to the left, then land. Don’t forget to set the trim back to normal once you have completed this
The CoDrone Mini must be completely back on the ground 2.
on landing.
The CoDrone Mini must float only to the left.
3.
See who can make the drone float fastest before landing! Below are examples of what student code might look like when they are done with the challenges:
Challenge 1: CoDrone pushups
drone.hover(1) drone.land() drone.close()
Challenge 2: Floating Away
import CoDrone_mini
drone = CoDrone_mini.CoDrone() drone.pair()
drone.trim(0,-5) time.sleep(1) drone.takeoff() drone.hover(3) drone.land() drone.close()