1

4.2: Battery

Goals:

By the end of this lesson, I will be able to:

  1. Explain the importance of measuring battery level
  2. Describe how the battery level is reported by your CoDrone Mini
  3. Use the get_battery_percentage function to measure the drone’s battery level
  4. Store and print the drone’s battery level
  5. Use battery level to restrict flight movements
  6. Use the LED to indicate battery level

Steps to Success:

  1. Battery Indicator
  2. Printing the Battery Level
  3. Before and After
  4. Limiting Takeoff
  5. Adding the LED
  6. Add Conditionals to Your LED Program
  7. Adding elif Statements
  8. Adding an else Statement
  9. Final LED Code
  10. Challenge 1: Battery Calculator
  11. Challenge 2: Battery Charge Reporter

Think of all the electronics that you use and how you know that their battery is low or dead on each of them. If you are on a laptop or phone, you will most likely see a battery icon on the top or bottom of your screen. If you used Blockly to program your CoDrone Mini, you probably saw the battery level on the bottom lefthand corner of the screen.

How can you tell if your CoDrone Mini is running out of battery in Python though? There are three methods that you can try out: printing out the battery level, limiting when your drone can take off, and programming the LED as an indicator.You used the print function in previous lessons, and you can use it to print your CoDrone Mini’s battery level too! After pairing with your drone and importing the library, call the function get_battery_percentage() and store its value in a variable called battery. Finally, print the battery level. We added the words “Battery Level: ” to our print statement to label it for you! If you want to do the same, make sure you add a comma in between all of the print parameters.

import CoDrone_mini

drone = CoDrone_mini.CoDrone()

drone.pair()


battery = drone.get_battery_percentage()

print("Battery Level: ", battery)

drone.close()

Now run that code again! Did it change? Go up? Go down? Do not be alarmed if you run the code a few times and it seems to change a bit, especially after doing certain flight commands. The battery percentage is an approximation from reading the battery voltage. Don’t worry too much about voltage for now, just be aware that the battery percentage may fluctuate a bit.You probably won’t notice a big difference in the percentage yet, so add some flying!

Print the battery percentage before and after hovering for 10 seconds.

import CoDrone_mini

drone = CoDrone_mini.CoDrone()

drone.pair()


battery = drone.get_battery_percentage()
print("Before: ", battery)

drone.takeoff()
drone.hover(10)
drone.land()


battery = drone.get_battery_percentage()
print("After: ", battery)

drone.close()

What was the difference? Explore with various flight commands to see which one consumes the most energy.In this exercise, you will write a program that will either takeoff or do nothing depending on the battery level.

First, include the program that you wrote in the last step which will print out your drone’s battery level. Next, write a conditional so that if the battery level is greater than 50, it will take off, hover for 2 seconds, and land. Else, it will print “Not enough power!” and stay grounded.

import CoDrone_mini

drone = CoDrone_mini.CoDrone()

drone.pair()

battery = drone.get_battery_percentage()

print("Battery Level: ", battery)


if battery > 50:

    drone.takeoff()

    drone.hover(2)

    drone.land()


else:

    print("Not enough power!")

drone.close()

Try changing the battery level in the conditional to a different number. At what percentage does your drone really not have enough power to take off, land, and hover?The LED on your CoDrone Mini already acts as a battery indicator — when your battery is low, it flashes red. In this program, you will be adding more colors to indicate different battery percentages.

First, include the code from the last two steps that will import libraries, pair with CoDrone Mini, and print the battery percentage. You will also need to import the time library to include the  time.sleep() function in your program.

import CoDrone_mini

import time

drone = CoDrone_mini.CoDrone()

drone.pair()


battery = drone.get_battery_percentage()

print("Battery Level: ", battery)

time.sleep(1)

drone.close()

You are going to use conditionals to check your battery’s percentage. Depending on your battery level, your CoDrone Mini’s LED will turn a certain color. You can change them to whatever you want, but we recommend you make them easy to tell apart so you know when you need to charge your battery. This is what we’re going to use for this lesson:

  • 100-76: solid green LED
  • 75-51: solid yellow LED
  • 50-26: solid red LED
  • 25-0: will not fly

Next, write an if statement that will change your CoDrone Mini’s LED to green when the battery level is above 75%. Include a delay so flight movements after the LED change aren’t skipped, and then add a takeoff command.

if battery > 75:

    drone.LED_color(0, 255, 0, 100)  # green

    time.sleep(0.05)  # delay so takeoff doesn't skip

    drone.takeoff()

Do the same for 51-75% battery and then 26-50% battery, but with a few changes:

  • Instead of if statements, use else if statements.
  • Instead of the battery being greater than 75%, it should be greater than 50% and then 25%.
elif battery > 50:

    drone.LED_color(255,255,0,100) # yellow

    time.sleep(0.05)  # delay so takeoff doesn't skip

    drone.takeoff()

elif battery > 25:

    drone.LED_color(255,0,0, 100) # red

    time.sleep(0.05)  # delay so takeoff doesn't skip

    drone.takeoff()

Add an else statement to indicate that if none of the above statements are true and your battery is less than 25%, your drone does not have enough power to fly!

else:

   print("Not enough power!")

All of the conditionals above 25% have a takeoff command included, so include any flight commands you would like in your program after the else statement. Don’t forget to land and close your drone!Your final code should look like this:

import CoDrone_mini

import time

drone = CoDrone_mini.CoDrone()

drone.pair()


battery = drone.get_battery_percentage()

print("Battery Level: ", battery)

time.sleep(1)


if battery > 75:

    drone.LED_color(0, 255, 0, 100)  # green

    time.sleep(0.05)  # delay so takeoff doesn't skip

    drone.takeoff()

elif battery > 50:

    drone.LED_color(255,255,0,100) # yellow

    time.sleep(0.05)  # delay so takeoff doesn't skip

    drone.takeoff()

elif battery > 25:

    drone.LED_color(255,0,0, 100) # red

    time.sleep(0.05)  # delay so takeoff doesn't skip

    drone.takeoff()

else:

    print("Not enough power!")


# Insert your own flight pattern here

drone.set_pitch(100)

drone.move(1)

drone.land()

drone.close()

For this challenge, we want to predict whether or not our drone will be able to fly the flight path we have programmed for it. To do this, repeat the step where we checked the difference in battery power after a takeoff, wait, and land. Figure out how much battery is lost per 5 seconds of flight time:

  1. Takeoff, hover 5 seconds, land -> take before and after battery measurements
  2. Takeoff, hover 10 seconds, land -> take before and after battery measurements
  3. Take the difference of the battery usage between steps 1 and 2. This is your battery drain per 5 seconds of stationary flight.

Now, write a flight plan that has at least 5 movements and at least 30 seconds of flight time. Store the beginning and ending battery percentage. Also store the beginning and ending flight time. Print both of these values at the end of the flight, after landing.

When you run the program, time it and make a prediction as to how much battery life you will lose. Check your drone’s output versus your prediction. How close was your prediction? What could be causing any differences?For this challenge, we want to find out what the most battery and heat intensive actions are so that we don’t tax our drone too much when flying. To complete this challenge, complete the following steps:

  1. Create a list that contains the names of the maneuvers your drone will perform. (For example, flip, circle, square, hover).
  2. Create 2 empty lists, one for temperatures and one for battery levels.
  3. Call the functions in the list you created in step 1. At the end of each step, append to the list the current temperature and time.
  4. At the end of the program, print the list of maneuvers with their battery and temperature levels. You must use a loop to do this.

BONUS Challenge: If you dare, try to figure out the maneuver that uses the most battery and the one that creates the most heat and print these as well.