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!