The CoDrone EDU is equipped with a front range sensor that can detect physical objects in front of it. The CoDrone EDU library has built in functions that allow you to detect an object, avoid an object, or keep the same distance from an object. In this lesson we are going to start by learning how to detect objects in front of our drone. The front range sensor can detect an object between 0-1000 millimeters (0 – 100 centimeters)
Let’s start by printing our our front range sensor value. We will do that by using the drone.get_front_range()
function. This function will return a value in centimeters. So for example, if you print your front range sensor value and it is 50, that means that it detects an object 50 centimeters away. Let’s do an example
For this exercise we won’t be flying our drone. Instead we will run a program that prints out our front range sensor value once every second. Let’s use a for loop and print our sensor value 10 times! First we will import the libraries we will need and get our drone connected.
from codrone_edu.drone import *
import time
drone = Drone()
drone.pair()
Next, we need to use a for loop to print our front range sensor 10 times. We will also time.sleep(1)
to make sure we’re only printing 1 value per second.
for i in range(10):
print(drone.get_front_range())
time.sleep(1)
Try running this program while slowly moving your drone toward or away from an object. This object can be your hand, a wall, a table, or anything big enough for your front range sensor to detect. The limit for the front range sensor is about 150 centimeters, or 1.5 meters.
Here is the full code for the program:
from codrone_edu.drone import *
import time
drone = Drone()
drone.pair()
for i in range(10):
print(drone.get_front_range())
time.sleep(1)
drone.close()