0.5: Flying CoDrone EDU with Python
This lesson will go over the following:
How to run Python code with CoDrone EDU
Troubleshooting Python programming and connection issues If you have any trouble completing these steps throughout the lesson, please reach out to us at [email protected] so that we can help you get started!The first step is to open a new project or file and write your Python code. If you don’t remember how to do that, watch our PyCharm setup video in the previous lesson.
For this test, we wrote some code for you so you can practice!
Don’t worry, the drone will not take off just yet. You will do that in the next lesson.
Copy and paste this code into a new Python file.I am text block. Click edit button to change this text. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Ut elit tellus, luctus nec ullamcorper mattis, pulvinar dapibus leo.
from time import sleep
from codrone_edu.drone import *
drone = Drone() drone.pair()
drone.set_drone_LED(255,255,255,255) drone.drone_buzzer(Note.C4, 1000) drone.drone_buzzer(Note.D4, 1000) drone.drone_buzzer(Note.E4, 1000) drone.close()
Once your controller is in “Link” state and your code is ready, press the green triangle to run your code! You can also
right-click anywhere in the code and select “Run”.
If you have any errors, make sure your drone and controller are properly connected.
1.02: Hover and Trim
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 EDU 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:
from codrone_edu.drone import * print("Creating drone object")
drone = Drone()
print("Getting ready to pair") drone.pair()
print("Paired!") drone.takeoff() print("taking off") drone.hover(3)
print("Hovering") print("landing") drone.land() 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 EDU 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 controller. 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:
from codrone_edu.drone import * drone = Drone()
drone.pair()
drone.set_trim(-5, 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. 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 EDU 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 controller. 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:
from codrone_edu.drone import * drone = Drone()
drone.pair()
drone.set_trim(-5, 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. Now that we know how to set our trim, how do we get our current trim value? It is important when coding the CoDrone EDU to use the drone.get_trim() function to see your trim levels. If you want to see your trim values printed onto the screen use print(drone.get_trim())
If you switch the CoDrone EDU controller from the “link” state to the “remote flying” state you will need to unplug your controller and drone completely and reconnect them before setting your trim values again.Challenge 1: CoDrone pushups Try to make your CoDrone EDU take off and land 5 times in a row with one code! Currently, your CoDrone EDU can do one pushup with the code you have.
Rules:
The CoDrone EDU must completely leave the ground on 1.
takeoff.
The CoDrone EDU must be completely back on the ground on 2.
landing.
There is no height requirement for how high the CoDrone 3.
EDU 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 E D U 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 EDU takeoff and float to the left, then land. Don’t forget to set the trim back to normal once you have completed this challenge.
Rules:
The CoDrone EDU must completely leave the ground on 1.
takeoff.
The CoDrone EDU must be completely back on the ground on 2.
landing.
The CoDrone EDU 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 from codrone_edu.drone import * drone = Drone()
drone.pair() drone.takeoff() drone.hover(1) drone.land() drone.takeoff() drone.hover(1) drone.land() drone.takeoff() drone.hover(1) drone.land() drone.takeoff() drone.hover(1) drone.land() drone.takeoff() drone.hover(1) drone.land() drone.close()
Challenge 2: Floating Away
from codrone_edu.drone import * drone = Drone()
drone.pair() drone.trim(0,-5) time.sleep(1) drone.takeoff() drone.hover(3) drone.land()
drone.close()
In this lesson you learned how to make your drone hover and how to write code to fix drifting. In the next lessons, you will learn more about roll, pitch, yaw, and throttle.
1.01: First Flight
By the end of this lesson, you should be able to:
Use the print function and describe its value in debugging (finding and fixing errors) in your code
Add comments to your code and understand the importance of well documented code
Pair with your drone using the pair() function
Understand how to use the takeoff() and land() functions In this lesson, you will complete the following steps. Make sure that you demonstrate your completed code to your instructor after each step:
Hello World! (Using the print() function) 1.
Using Comments 2.
Pairing and Printing 3.
Takeoff! (Your First Flight Test) 4.
It’s time to write your first Python program! This program will use the print() function. You’re not printing with ink and paper, but you’ll print to the screen so you can see any output from the program. Programmers use print statements to determine where the program is when they run it (since you can’t tell once you run the code) which helps them debug (find errors in) the code. They also use the print() function to check the values of variables (containers that store values) as the program progresses.
Make a new Python file called helloworld.py and run this line of code:
print("Hello, world!")
Print statements are sent to the Run console. The console is a tool in most IDEs (Integrated Development Environments) that allows you to see specific commands and information about the program as it runs. In pycharm, the console is just called Run and appears at the bottom of your screen.
Click “Run” The “Hello World” program has been used to introduce beginner programmers to a new programming language since the 1970s. Welcome to Python!Comments are ignored by the computer when running your code, so you can write words and sentences as you would in a normal document. You can add comments to your code by beginning the line with a hash symbol (#). These are for single-line only comments, which means that when you start a new line, the program goes back to normal code.
Try it out with the code below!
# print("The computer is ignoring this") print("The computer is printing this!") What do you see?
If you want to write a longer comment, insert the block of code between two sets of triple quotation marks or triple apostrophes. The first set of triple apostrophes is the beginning of the comment and the second set is the end of the comment. Everything in between will be ignored by the computer. Try it out with the code below!
'''
This is a longer comment that the computer will not read:
Robots Play Yellow
Trombones '''
Comments can also be used by programmers to debug by “turning off” parts of the code by commenting them so they are ignored.
This way they can test specific parts of a program to narrow down what the problem may be. Now that we know how to use print statements, we will connect to the CoDrone EDU and debug our program using print statements. Before we get started, please complete the following steps:
Make sure you have imported the CoDrone EDU library in 1.
project interpreter.
Click on the “File” menu and choose “New”
2.
Choose Python File 3.
Give the new file the name takeoff.py 4.
Click “Ok” or press Enter 5.
A new tab in Pycharm should be created that has our 6.
program name takeoff.py as the tab name
Now that you have those steps completed, you will always need to tell python you want to use the CoDrone EDU library. Think of the library like a recipe book that your program needs to be able to code your drone! Inside the library there are all sorts of functions, or “recipes”, that you will use. You will learn more about functions later. To do this you will use the import statement. This tells python that you want to use the tools in a specific library. In this case, you want to use the tools from the CoDrone EDU’s library so you can program it.
Import the CoDrone EDU library into your program with the following line of code:
from codrone_edu.drone import *
IMPORTANT NOTE: In future programs, if you forget to include the import statement, Pycharm will give you an error which looks like a red squiggly line whenever you try to create the
drone in the program (as shown in the image below), so make sure you always remember to include this line!
After you import the library, you need to create an object, which is a collection of variables and functions that act on data. Create an object called “drone” like this:
drone = Drone()
In this example, CoDrone EDU is a class. You do not have to understand the details now, but just know that a class is like a factory that can make CoDrone EDU objects. All the objects have the same characteristics and the same accessible functions.
Now you need to pair your drone with this command line:
drone.pair()
The pair function will find your controller automatically.
Finally, add one more line that will close the connection.
This is an important step because if the communication channel with the drone is not closed, the next time you try to upload code, it will still be looking for the old connection. So make sure you close the connection at the end of each program.
drone.close()
So far your program should look like this:
from codrone_edu.drone import * drone = Drone()
drone.pair() drone.close()
Now, try to add print statements to let you know the following:
When the drone object has been created 1.
When the drone is ready to pair 2.
When the drone has successfully paired.
3.
As we discussed before, using print statements can help us make sure that all parts of our program successfully execute.
If you are having trouble, see the example code below:
from codrone_edu.drone import * print("Creating drone object") drone = Drone()
print("Getting ready to pair") drone.pair()
print("Paired!") drone.close()
In this step, we will learn how to make the drone take off and land. Since the drone will be flying, please make sure you follow all safety precautions and make sure you place your CoDrone EDU away from other objects on the floor.
The takeoff() commandFollow the steps below to prepare a new file for this step:
Create a file called flight_test.py (note we use an 1.
underscore, you could also use a dash-)
In this new file, add a drone object (you can call it 2.
drone or give it a name of your choosing) Tell the drone object to pair
3.
We use an underscore or dash to make the file name readable.
It is possible to use a space, but is not good programming practice as not all interpreters will be able to understand or use file names with spaces. If you are having trouble with these steps, please go back and review the previous step and code.
Now we are ready to write our flight test! Write the drone.takeoff() command, which will allow your CoDrone EDU will take off and hover. Note that the takeoff command does
not take in any parameters, so the parenthesis should be empty. This is because drone.takeoff() goes through the steps of starting the motors and flying the drone to a preset height. Add the following code to your flight_test.py:
drone.takeoff() print("taking off") The land() function
Much like the takeoff() function, the land() function follows a preset routine that will land the drone safely on the ground.
There are two ways a drone can land. The first is with the drone.land()command, which will gently bring your CoDrone EDU to the ground.
drone.land()
The second is with the drone.emergency_stop()command. This immediately turns off all the motors, which will crash your CoDrone EDU to the ground. Ideally, this should only be used in emergency situations, and you can program your keyboard’s spacebar to act as an emergency stop if it’s hit. For now, we will stick with a simple land command. Below you will find an example of the code with print statements included.
from codrone_edu.drone import * drone = Drone()
drone.pair() print("Paired!") drone.takeoff()
print("In the air!") print("Landing") drone.land() drone.close()
print("Program complete")
In this lesson, you learned about how to takeoff and land your
drone just by using code! In the next lesson, learn how to use the hover() function to fly in place.
0.5: Setting the Trim
This lesson will go over the following:
How to adjust the trim values
If you have any trouble completing these steps throughout the lesson, please reach out to us on the forum so that we can help you get started!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 value 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, you can 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.
1.1: First Flight
By the end of this lesson, you should be able to:
Use the print function and describe its value in debugging (finding and fixing errors) in your code
Add comments to your code and understand the importance of well documented code
Pair with your drone using the pair() function
Understand how to use the takeoff() and land() functions In this lesson, you will complete the following steps. Make sure that you demonstrate your completed code to your instructor after each step:
Hello World! (Using the print() function) 1.
Using Comments 2.
Pairing and Printing 3.
Takeoff! (Your First Flight Test) 4.
It’s time to write your first Python program! This program will use the print() function. You’re not printing with ink and paper, but you’ll print to the screen so you can see any output from the program. Programmers use print statements to determine where the program is when they run it (since you can’t tell once you run the code) which helps them debug (find errors in) the code. They also use the print() function to check the values of variables (containers that store values) as the program progresses.
Make a new Python file called helloworld.py and run this line of code:
print("Hello, world!")
Print statements are sent to the Run console. The console is a tool in most IDEs (Integrated Development Environments) that allows you to see specific commands and information about the program as it runs. In pycharm, the console is just called Run
and appears at the bottom of your screen.
Click “Run” The “Hello World” program has been used to introduce beginner programmers to a new programming language since the 1970s. Welcome to Python!Comments are ignored by the computer when running your code, so you can write words and sentences as you would in a normal document. You can add comments to your code by beginning the line with a hash symbol (#). These are for single-line only comments, which means that when you start a new line, the program goes back to normal code.
Try it out with the code below!
# print("The computer is ignoring this") print("The computer is printing this!") What do you see?
If you want to write a longer comment, insert the block of code between two sets of triple quotation marks or triple apostrophes. The first set of triple apostrophes is the beginning of the comment and the second set is the end of the comment. Everything in between will be ignored by the computer. Try it out with the code below!
'''
This is a longer comment that the computer will not read:
Robots Play Yellow Trombones '''
Comments can also be used by programmers to debug by “turning off” parts of the code by commenting them so they are ignored.
This way they can test specific parts of a program to narrow down what the problem may be. Now that we know how to use
print statements, we will connect to the CoDrone Mini and debug our program using print statements. Before we get started, please complete the following steps:
Make sure you have imported the CoDrone Mini library in 1.
project interpreter (please click HERE to learn how to do this)
Click on the “File” menu and choose “New”
2.
Choose Python File 3.
Give the new file the name takeoff.py 4.
Click “Ok” or press Enter 5.
A new tab in Pycharm should be created that has our 6.
program name takeoff.py as the tab name
Now that you have those steps completed, you will always need to tell python you want to use the CoDrone_mini library. Think of the library like a recipe book that your program needs to be able to code your drone! Inside the library there are all sorts of functions, or “recipes”, that you will use. You will learn more about functions later. To do this you will use the import statement. This tells python that you want to use the tools in a specific library. In this case, you want to use the tools from the CoDrone Mini’s library so you can program it.
Import the CoDrone Mini library into your program with the following line of code:
import CoDrone_mini
IMPORTANT NOTE: In future programs, if you forget to include the import statement, Pycharm will give you an error which looks like a red light bulb whenever you try to create the drone in the program (as shown in the image below), so make sure you always remember to include this line!
After you import the library, you need to create an object, which is a collection of variables and functions that act on data. Create an object called “drone” like this:
drone = CoDrone_mini.CoDrone()
In this example, CoDrone Mini is a class. You do not have to understand the details now, but just know that a class is like a factory that can make CoDrone Mini objects. All the objects have the same characteristics and the same accessible functions.
Now you need to pair your drone with this command line:
drone.pair()
The pair function will find your remote automatically. If you are having trouble, set up CDM with Blockly and copy the port found in your pair list into the pair function. Here is an example of the Mac port below:
drone.pair("/dev/tty.usbserial-14530")
Finally, add one more line that will close CoDrone’s Mini connection. This is an important step because if the communication channel with the drone is not closed, the next time you try to upload code, it will still be looking for the old connection. So make sure you close the connection at the end of each program.
drone.close()
So far your program should look like this:
import CoDrone_mini
drone = CoDrone_mini.CoDrone()
drone.pair() drone.close()
Now, try to add print statements to let you know the following:
When the drone object has been created 1.
When the drone is ready to pair 2.
When the drone has successfully paired.
3.
As we discussed before, using print statements can help us make sure that all parts of our program successfully execute.
If you are having trouble, see the example code below:
import CoDrone_mini
print("Creating drone object") drone = CoDrone_mini.CoDrone() print("Getting ready to pair") drone.pair()
print("Paired!") drone.close()
Now you can run your code. When paired, your remote’s LED should blink red and your CoDrone Mini’s LED should be solid red.In this step, we will learn how to make the drone take off and land. Since the drone will be flying, please make sure you follow all safety precautions and make sure you place your CoDrone Mini away from other objects on the floor.
The takeoff() command
Follow the steps below to prepare a new file for this step:
Create a file called flight_test.py (note we use an 1.
underscore, you could also use a dash-)
In this new file, add a drone object (you can call it 2.
drone or give it a name of your choosing) Tell the drone object to pair
3.
We use an underscore or dash to make the file name readable.
It is possible to use a space, but is not good programming
practice as not all interpreters will be able to understand or use file names with spaces. If you are having trouble with these steps, please go back and review the previous step and code.
Now we are ready to write our flight test! Write the drone.takeoff() command, which will allow your CoDrone Mini will take off and hover. Note that the takeoff command does not take in any parameters, so the parenthesis should be empty. This is because drone.takeoff() goes through the steps of starting the motors and flying the drone to a preset height. Add the following code to your flight_test.py:
drone.takeoff() print("taking off") The land() function
Much like the takeoff() function, the land() function follows a preset routine that will land the drone safely on the ground.
There are two ways a drone can land. The first is with the drone.land()command, which will gently bring your CoDrone Mini to the ground.
drone.land()
The second is with the drone.emergency_stop()command. This immediately turns off all the motors, which will crash your CoDrone Mini to the ground. Ideally, this should only be used in emergency situations, and you can program your keyboard’s spacebar to act as an emergency stop if it’s hit. For now, we will stick with a simple land command. Below you will find an example of the code with print statements included.
import CoDrone_mini
drone = CoDrone_mini.CoDrone() drone.pair()
print("Paired!") drone.takeoff()
print("In the air!") drone.land()
print("Landing") drone.close()
print("Program complete")
1.2: Hover and Trim
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 challenge.
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.
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 import CoDrone_mini
drone = CoDrone_mini.CoDrone() drone.pair()
drone.takeoff() drone.hover(1) drone.land() drone.takeoff() drone.hover(1) drone.land() drone.takeoff() drone.hover(1) drone.land() drone.takeoff() drone.hover(1) drone.land() drone.takeoff()
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()
Flight Movements
Air vehicles move a little differently than land vehicles.
While land vehicles can move forward, backward, and sometimes side-to-side, air vehicles can do all of that and move up and down. To keep track of how CoDrone moves in a three- dimensional space, you can use the terms below:
Roll controls the CoDrone’s horizontal, or side to side, movement. Positive roll will make the CoDrone move to the right, and negative roll will make the drone move to the left.
drone.set_roll(power) # power represents power out of 100%
# power can be between -100 and 100
Pitch is the CoDrone’s forward and backward tilt. Positive pitch will make the CoDrone tilt and move forward, and
negative pitch will make the CoDrone tilt and move backwards.
drone.set_pitch(power) # power represents power out of 100%
# power can be between -100 and 100
Yaw is the CoDrone’s left and right rotation. Positive yaw will make the CoDrone turn to the right, and negative yaw will make the CoDrone turn to the left.
drone.set_yaw(power) # power represents power out of 100%
# power can be between -100 and 100
Throttle controls the CoDrone’s vertical, or up and down, movement. Positive throttle will make the CoDrone fly higher, and negative throttle will make the CoDrone fly lower.
drone.set_throttle(power) # power represents power out of 100%
# power can be between -100 and 100 Be careful with yaw and roll! Yaw will make your CoDrone turn left or right, while roll will move your CoDrone left or right.One of the best ways to learn new words is to see them in action, so instead of you just memorizing all of the flight movements, you’re going to do an obstacle course! Have a friend sit down on a chair and then blindfold them (or just have them keep their eyes closed). Next, guide them through an obstacle course to have them sit down on another chair at the end using only flight movement words. Make sure that anything dangerous is out of their way first!
Positive throttle: stand up Negative throttle: sit down Positive pitch: move forward Negative pitch: move backward Positive yaw: turn right
Negative yaw: turn left Positive roll: move right Negative roll: move left
For example, if you wanted to tell your friend to move
forward, you could say, “Positive pitch 2 steps.” That would let your friend know that you want them to move forward by 2 steps. Once your friend successfully completes the obstacle course, switch roles so they can give you directions!Your first challenge: have your CoDrone take off, go forward, then turn left, and land. First, you need to import the CoDrone library and pair your drone:
import CoDrone
drone = CoDrone.CoDrone() drone.pair(drone.Nearest)
# drone.pair() if paired to CoDrone before drone.takeoff()
Next, you can add the new functions that you just learned!
import CoDrone
drone = CoDrone.CoDrone() drone.pair(drone.Nearest)
# drone.pair() if paired to CoDrone before
drone.takeoff() # takeoff for 2 seconds
drone.set_pitch(30) # Set positive pitch to 30% power drone.move(2) # forward for 2 seconds
drone.set_yaw(-50) # Set negative yaw to 30% power drone.move(2) # turn left for 2 seconds
drone.land() # lands the CoDrone drone.close() # disconnects CoDrone
Wondering why you need two lines of code for one movement?
drone.set()will prepare the CoDrone to move in a certain direction and speed, while drone.move()will actually move the CoDrone in the air.
You might also notice that CoDrone was still moving forward while turning left. We will explain a bit more why this
happens in the next step.Anytime you use a flight command, it sets the value for that direction permanently! Well, at least until you change it back to 0 or another value. This is how your CoDrone can fly in multiple directions at once. We know this sounds crazy but it just means you will be able to do stuff like fly diagonally or spin while flying in another direction.
Run this code and see what happens!
import CoDrone
drone = CoDrone.CoDrone() drone.pair(drone.Nearest)
# drone.pair() if paired to CoDrone before
drone.takeoff() # takeoff for 2 seconds
drone.set_pitch(30) # Set positive pitch to 30% power drone.set_roll(-30) # Set negative roll to 30% power drone.move(2) # forward and right for 2 seconds drone.land() # lands the CoDrone
drone.close() # disconnects CoDrone
Flight commands can be confusing. Try drone.go() instead! This is what the function looks like:
drone.go(direction, duration, power) This is what each part means:
Direction: where the drone will fly. You can use FORWARD, BACKWARD, LEFT, RIGHT, UP, and DOWN, but make sure you type in all caps like you see here.
Duration: how long the movement will happen in seconds.
You can leave it as blank, zero, or any positive number.
If duration is zero, it runs indefinitely. If duration has no number, it automatically sets to 1. If you put any positive number, duration is equal to that value.
Power: the speed of the drone. It can by any number
between 0-100, with 0 being no power and 100 being full power. If power has no number, it will default to 50.
Here’s some examples:
drone.go(UP, 3, 75) # go up for 3 seconds at 75% power drone.go(FORWARD) # go forward for 1 second at 50% power drone.go(LEFT, 6) # go left for 6 seconds at 50% power If you want to use drone.go() in your programs, you’ll need to include this line at the top, right underneath import CoDrone:
from CoDrone import Direction
After that, you’re good to go! Try using drone.go()in the program you wrote for Step 3 in place of drone.set()and drone.move(). What happened?Program your CoDrone to fly in a square using the CoDrone directions you recently learned. You can use drone.set() and drone.move() or drone.go().
Using drone.set() and drone.move() will require you to keep resetting flight directions back to 0 after each movement in the square. See the example below:
import CoDrone
drone = CoDrone.CoDrone()
drone.pair() # pairs to previous drone drone.takeoff() # takeoff for 2 seconds drone.hover(3) # hover for 3 seconds
drone.set_pitch(30) # set positive pitch at 30% power drone.move(2) # forward for 2 seconds
drone.set_pitch(0) # reset pitch to 0 to stop moving forward
drone.set_roll(-30) # set negative roll at 30% power drone.move(2) # left for 2 seconds
drone.set_roll(0) # reset roll to 0 to stop moving sideways
drone.set_pitch(-30) # set negative pitch at 30% power drone.move(2) # backward for 2 seconds
drone.set_pitch(0) # reset pitch to 0 to stop moving backward
drone.set_roll(30) # set positive roll at 30% power
drone.move(2) # right for 2 seconds
drone.set_roll(0) # reset roll to 0 to stop moving sideways drone.land() # lands the CoDrone
drone.close() # disconnects CoDrone Using drone.go():
import CoDrone
from CoDrone import Direction
drone = CoDrone.CoDrone() # easier to type drone drone.pair(drone.Nearest) # pairs to nearest drone drone.takeoff() # takeoff for 2 seconds drone.hover(3) # hover for 3 seconds
drone.go(FORWARD, 2, 30) # moves the drone forward for 2 seconds at 30% power
drone.go(LEFT, 2, -30) # moves the drone left for 2 seconds at 30 power
drone.go(BACKWARD, 2, -30) # moves the drone backward for 2 seconds a 30% power
drone.go(RIGHT, 2, 30) # moves the drone right for 2 seconds at 30% power
drone.land() # lands the CoDrone drone.close() # disconnects CoDrone Challenge Jumper
Have two tables or chairs 2 feet apart. The goal is to have the CoDrone take off from one chair, land on the other, turn around, fly back, and then land at the starting point. It should look like this:
A -> B then B -> A Rules:
The CoDrone should always fly forward between chairs.
If you succeed, move the tables or chairs an additional
2 feet apart. Continue adding 2 feet for every success until you reach the ends of the room.
Hint: You can use drone.land()in the middle of the code and take off again.Once you have written your code to have the CoDrone fly from chair to chair, take a look at the example solution code to compare your program!
import CoDrone
drone = CoDrone.CoDrone() drone.pair()
drone.takeoff() drone.hover()
drone.set_pitch(30)
drone.move(3) # drone flies forward toward chair drone.set_pitch(0) # reset pitch
drone.land() # land on chair
drone.takeoff() # takeoff from chair drone.set_yaw(40)
drone.move(3) # drone turns right towards starting chair drone.set_yaw(0) # reset yaw
drone.set_pitch(30)
drone.move(3) # drone flies forward toward chair drone.land()
Pair and Fly!
It’s time to write your first Python program! This program will use the print() function. You’re not printing with ink and paper, but you’ll print to the screen so you can see any output from the program. It’s handy for keeping track of variable values and finding errors in your code!
Make a new Python file in PyCharm and add the following code to the new file:
print("Hello world!")
Click “Run” and watch the screen print your phrase.
The “Hello World” program has been used to introduce beginner programmers to a new programming language since the 1970s.
Welcome to Python!Try printing different phrases on your screen. You can even try adding another print statement on another line! Just remember to keep your phrases within quotation marks, since these indicate the beginning and the end of the string you want to print. A string is a collection of characters, which can include letters, punctuation, numbers, and even spaces!
Try this example:
print("Hello, CoDrone!") print("Hi, CoDrone!") print("Hey, CoDrone!")
What order will the strings be printed in? Run the code to find out!You can use \t anywhere in your string where you would like to tab, or indent.
To see how much space a tab gives, you should test it! Copy the text below into a new line and then click “Run”.
print("\t This is how much space a tab gives")
If you want to add a new line, like you’re pressing the enter key, use \n where you need to break the line:
print("This is how you \n break phrases \n into parts")
Sometimes you may want to make comments, or memos to yourself or others that the computer will ignore. Comments are a great way to describe your code. You can add comments to your code by beginning the line with a hash symbol (#). If you want to write a longer comment, insert the block of code between two sets of triple quotation marks or triple apostrophes. Try it out with the code below!
# print("The computer is ignoring this") print("The computer is printing this!") '''
This is a longer comment that the computer will not read:
Robots Play Yellow Trombones '''
Now you are going to combine print statements with CoDrone! In a new file, import the CoDrone library:
import CoDrone
After you import the library, you need to create an object, which is a collection of variables and functions that act on data. Create an object called “drone” like this:
drone = CoDrone.CoDrone()
In this example, CoDrone is a class. You do not have to understand the details now, but just know that a class is like a factory that can make CoDrone() objects. All the objects have the same characteristics and the same accessible functions.
Now you need to connect your drone to the BLE (Bluetooth Low Energy) board.
drone.pair(drone.Nearest)
I t i s s t r o n g l y s u g g e s t e d t h a t y o u u s e drone.pair(drone.Nearest) the first time when pairing to a CoDrone and then use drone.pair() after pairing to the CoDrone at least once. This way your board will only pair to your CoDrone when pairing in the future. This is especially helpful in the classroom, or anywhere else with a large number of drones and people.
Add some unique print statements to your code to practice!
import CoDrone
print("Creating drone object") drone = CoDrone.CoDrone()
print("Getting ready to pair") drone.pair(drone.Nearest)
print("Paired!")
Run the code following the same steps you learned about in Step 4 of the Library and Driver Installation lesson. When paired, your BLE LED will be blinking red and the CoDrone tail LED should be solid green.For this code, make sure you place your CoDrone away from other objects on the floor.
Next, you will be writing code for a flight test! Write the drone.takeoff() command, which will allow your CoDrone will take off and hover. If you want, include a print statement:
drone.takeoff() print("taking off")
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 as a parameter. The drone.hover() function parameter is the time in seconds that you would like your CoDrone to hover in the air.
drone.hover(3) print("Hovering") Don’t forget to land!
drone.land() print("landing")
Before running the completed code:
Make sure the BLE board is blinking red. If it’s not, press the reset button below the LED.
Make sure the CoDrone is in pairing mode. You may need to disconnect the battery and press the reset button on your Bluetooth board.
If you have the CoDrone pro remote, make sure that the controller cable connecting to the BLE board is disconnected before running the code.
If you find that you need to rewrite the code and test it again, you only need to edit the lines that need to be adjusted. Do not disconnect your drone or battery!
For example, let’s say you want to hover for 5 seconds instead of 3. Edit your code like so:
drone.takeoff() # takeoff
print("taking off") # prints the phrase drone.hover(5) # hover for 5 seconds print("hovering") # prints the phrase drone.land() # land the CoDrone print("landing") # prints the phrase
Your code will run immediately with your new changes. However, the CoDrone will have to pair each time the code is run. Keep this in mind for any code your write in the upcoming lessons. From now on the pairing process will be omitted from e x a m p l e s , b u t i t i s r e q u i r e d f o r t h e c o d e t o r u n properly.Challenge: CoDrone pushups
Try to make your CoDrone take off and land 5 times in a row with one code! Currently your CoDrone can do one pushup with the code you have.
Rules:
The CoDrone must completely leave the ground on takeoff.
1.
The CoDrone must be completely back on the ground on 2.
landing.
There is no height requirement for how high the CoDrone 3.
flies.
See who can do the most CoDrone pushups in 10 seconds!Finished writing the code to make your CoDrone do pushups? Try comparing it to the example solution code below!
import CoDrone
drone = CoDrone.CoDrone() drone.pair()
drone.takeoff() drone.hover() drone.land() drone.takeoff() drone.hover() drone.land() drone.takeoff() drone.hover() drone.land() drone.takeoff() drone.hover() drone.land() drone.takeoff() drone.hover() drone.land() drone.close()
1B: Flight Directions
Air vehicles move a little differently than land vehicles.
While land vehicles can move forward, backward, and sometimes side-to-side, air vehicles can do all of that and move up and down. To keep track of how a CoDrone Mini moves in a three- dimensional space, you can use the terms below:
Roll controls the CoDrone Mini’s horizontal, or side to side, movement. Positive roll will make the CoDrone Mini move to the
right, and negative roll will make the drone move to the left.
drone.set_roll(power) # power represents power out of 100%
# power can be between -100 and 100
Pitch is the CoDrone Mini’s forward and backward tilt.
Positive pitch will make the CoDrone Mini tilt and move forward, and negative pitch will make the CoDrone Mini tilt and move backwards.
drone.set_pitch(power) # power represents power out of 100%
# power can be between -100 and 100
Yaw is the CoDrone Mini’s left and right rotation. Positive yaw will make the CoDrone Mini turn to the right, and negative yaw will make the CoDrone Mini turn to the left.
drone.set_yaw(power) # power represents power out of 100%
# power can be between -100 and 100
Throttle controls the CoDrone Mini’s vertical, or up and down, movement. Positive throttle will make the CoDrone Mini fly higher, and negative throttle will make the CoDrone Mini fly lower.
drone.set_throttle(power) # power represents power out of 100%
# power can be between -100 and 100 Be careful with yaw and roll! Yaw will make your CoDrone Mini turn left or right, while roll will move your CoDrone Mini left or right.One of the best ways to learn new words is to see them in action, so instead of you just memorizing all of the flight movements, you’re going to do an obstacle course!
Have a friend sit down on a chair and then blindfold them (or just have them keep their eyes closed). Next, guide them
through an obstacle course to have them sit down on another chair at the end using only flight movement words. Make sure that anything dangerous is out of their way first!
Positive throttle: stand up Negative throttle: sit down Positive pitch: move forward Negative pitch: move backward Positive yaw: turn right
Negative yaw: turn left Positive roll: move right Negative roll: turn left
For example, if you wanted to tell your friend to move forward, you could say, “Positive pitch 2 steps.” That would let your friend know that you want them to move forward by 2 steps. Once your friend successfully completes the obstacle course, switch roles so they can give you directions!Your first challenge: have your CoDrone Mini take off, go forward, then turn left, and land. First, you need to import the CoDrone Mini library and pair your drone:
import CoDrone_mini
drone = CoDrone_mini.CoDrone() drone.pair()
Next, you can add the takeoff and pitch!
drone.takeoff() # takeoff for 2 seconds
drone.set_pitch(30) # Set positive pitch to 30% power drone.move(2) # forward for 2 seconds
Wondering why you need two lines of code for one movement?
drone.set_pitch() will prepare the CoDrone Mini to move in a certain direction and speed, while drone.move() will actually move the CoDrone Mini in the air for the amount of seconds in
the parentheses. Your code will not run with the move() command.Next, add your yaw and landing:
drone.set_pitch(0) # Resets pitch to 0 before yaw drone.set_yaw(-50) # Set negative yaw to 30% power drone.move(2) # turn left for 2 seconds
drone.land() # lands the CoDrone drone.close() # disconnects CoDrone
Wondering why the pitch is reset to 0? Anytime you set a flight movement, it will use those values for the rest of your program unless you reset it back to 0. Not doing so will have the drone move forward and turn to the left at the same time.
Since the challenge was to go forward and then turn left, resetting the pitch to 0 will help the CoDrone Mini perform those two movements separately.In the last step, each flight direction was used separately. You probably saw your CoDrone Mini go straight up, fly straight forward, turn to the left, and then land. However, you can also have your CoDrone Mini fly in multiple directions at once. Which two commands would you combine to perform the following flight pattern (as seen from a bird’s eye view)?If you guessed positive pitch and negative roll, you are correct! If you want the drone to fly equally forward and sideways, use the same number value for both the roll and pitch. Here is an example solution. Try adjusting one or two numbers to see how the flight changes.
import CoDrone_mini
drone = CoDrone_mini.CoDrone() drone.pair()
drone.takeoff() # takeoff for 2 seconds
drone.set_pitch(30) # Set positive pitch to 30% power drone.set_roll(-30) # Set negative roll to 30% power drone.move(2) # forward and left for 2 seconds drone.land() # lands the CoDrone
If you find it difficult to remember all of the flight directions, try drone.go() instead! This is what the function looks like:
drone.go(direction, duration, power) This is what each part means:
Direction: where the drone will fly. You can use FORWARD, BACKWARD, LEFT, RIGHT, UP, and DOWN, but make sure you type in all caps like you see here. You will also need to type Direction before it.
Duration: how long the movement will happen in seconds.
You can leave it as blank, zero, or any positive number.
If duration is zero, it runs indefinitely. If duration has no number, it automatically sets to 1. If you put any positive number, duration is equal to that value.
Power: the speed of the drone. It can be any number between 0-100, with 0 being no power and 100 being full power. If power has no number, it will default to 50.
Here are some examples:
drone.go(Direction.UP, 3, 75) # go up for 3 seconds at 75%
power
drone.go(Direction.FORWARD) # go forward for 1 second at 50%
power
drone.go(Direction.LEFT, 6) # go left for 6 seconds at 50%
power
If you want to use drone.go() in your programs, you’ll need to include this line at the top, right underneath import CoDrone_mini :
from CoDrone_mini import Direction
After that, you’re good to go! Try using drone.go() to make some patterns!Program your CoDrone Mini to fly in a square using the CoDrone Mini directions you recently learned. You can use drone.set() and drone.move() or drone.go() . Remember, if you use the set pitch and roll commands you will need to reset some values to zero to prevent your drone from flying diagonally!
Take a look at the diagram and think about what your code will look like.Example 1:
import CoDrone_mini
drone = CoDrone_mini.CoDrone() drone.pair()
drone.takeoff() # takeoff for 2 seconds drone.hover(3) # hover for 3 seconds
drone.set_pitch(30) # set positive pitch at 30% power drone.move(2) # forward for 2 seconds
drone.set_pitch(0) # reset pitch
drone.set_roll(-30) # set negative roll at 30% power drone.move(2) # left for 2 seconds
drone.set_roll(0) # reset roll
drone.set_pitch(-30) # set negative pitch at 30% power drone.move(2) # backward for 2 seconds
drone.set_pitch(0) # reset pitch
drone.set_roll(30) # set positive roll at 30% power drone.move(2) # right for 2 seconds
drone.land() # lands the CoDrone
Example 2:
import CoDrone_mini
from CoDrone_mini import Direction drone = CoDrone_mini.CoDrone()
drone.pair()
drone.takeoff() # takeoff for 2 seconds drone.hover(3) # hover for 3 seconds
drone.go(Direction.FORWARD, 2, 30) # moves the drone forward for 2 seconds at 30% power
drone.go(Direction.LEFT, 2, -30) # moves the drone left for 2 seconds at 30% power
drone.go(Direction.BACKWARD, 2, -30) # moves the drone backward for 2 seconds at 30% power
drone.go(Direction.RIGHT, 2, 30) # moves the drone right for 2 seconds at 30% power
drone.land() # lands the CoDrone
Have two tables or chairs 2 feet apart. The goal is to have the CoDrone Mini take off from one chair, land on the other, turn around, fly back, and then land at the starting point. It should look like this:
A -> B then B -> A Rules:
The CoDrone Mini should always fly forward between chairs.
If you succeed, move the tables or chairs an additional 2 feet apart. Continue adding 2 feet for every success until you reach the ends of the room.
Hint: You can use drone.land() in the middle of the code and take off again.
1A: Flight Events
It’s time to write your first Python program! This program will use the print() function. You’re not printing with ink and paper, but you’ll print to the screen so you can see any output from the program. It’s great for keeping track of variable values and finding errors in your code!
Make a new Python file called helloworld.py and run this line of code:
print("Hello, world!")
Any code that is printed will appear in the console. The console window will be at the bottom of the screen.The “Hello World” program has been used to introduce beginner programmers to a new programming language since the 1970s. Welcome to Python!Sometimes you may want to make comments, or memos to yourself or others that the computer will ignore. Comments are a great way to describe your code. You can add comments to your code by beginning the line with a hash symbol (#). If you want to write a longer comment, insert the block of code between two sets of triple quotation marks or triple apostrophes. Try it out with the code below!
# print("The computer is ignoring this") print("The computer is printing this!") '''
This is a longer comment that the computer will not read:
Robots Play Yellow Trombones '''
Now you are going to combine print statements with CoDrone Mini! Open a new file takeoff.py. First, you will always need to import the CoDrone_mini library. Think of the library like
a recipe book that your program needs to be able to code your drone! Inside the library there are all sorts of functions, or
“recipes”, that you will use. You will learn more about functions later.
import CoDrone_mini
After you import the library, you need to create a drone object. You will learn about objects and classes in more advanced Python courses.
drone = CoDrone_mini.CoDrone()
Now you need to pair your drone with this command:
drone.pair()
The pair function will find your remote automatically. If you are having trouble, set up CDM with Blockly and copy the port found in your pair list into the pair function. Here is an example of the Mac port below:
drone.pair("/dev/tty.usbserial-14530")
Finally, add some unique print statements to your code to practice!
import CoDrone_mini
print("Creating drone object") drone = CoDrone_mini.CoDrone() print("Getting ready to pair") drone.pair()
print("Paired!")
Now you can run your code. When paired, your remote’s LED should blink red and your CoDrone Mini’s LED should be solid red.For this code, make sure you place your CoDrone Mini away from other objects on the floor.
Add the following command to make your drone take off and hover. Optional: Include a print statement!
drone.takeoff() print("taking off")
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 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. You can also use decimal values!
drone.hover(3) print("Hovering")
There are two ways a drone can land. The first is with the drone.land() command, which will gently bring your CoDrone Mini to the ground.
drone.land()
The second is with the drone.emergency_stop() command. This immediately turns off all the motors, which will crash your CoDrone Mini to the ground. Ideally, this should only be used in emergency situations, and you can program your keyboard’s spacebar to act as an emergency stop if it’s hit. However, you can try it out here to see what it looks like.
drone.emergency_stop()
Choose either a land or an emergency stop. Finally, add a landing print statement!
print("landing")
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")
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) 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()
Challenge: 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.
See who can do the most CoDrone Mini pushups in 10 seconds!