• No results found

Pair and Fly!

In document 0.5: Flying CoDrone EDU with Python (Page 34-39)

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()

In document 0.5: Flying CoDrone EDU with Python (Page 34-39)

Related documents