Now that we know the basics of the front range sensor, let’s do something a bit more complicated. In our next program we will make an autonomous flight program using our front range sensor!
Warning: Remember, your drone can only see directly in front of itself in this program. Your CoDrone EDU is going to depend on you to make sure it doesn’t get too close to anything above, below, or to the side of it. If your drone gets into a weird spot press your power button to switch to remote flight mode and fly the drone away with your remote. Also be prepared to use your emergency stop if necessary!
First, we need our import statements, drone object creation, and drone pairing as usual. We will also go ahead and put a takeoff command in.
from codrone_edu.drone import *
import time
drone = Drone()
drone.pair()
drone.takeoff()
Next, in order to make sure our drone lands, we need to use timers. If you don’t remember how timers work then take a look back at the timers lesson!
We will start by making two variables time_elapsed
and time_start
. time_elapsed will hold how much time has elapsed since our program started. Since this is the beginning of our program we can set that to 0 for now.
time_elapsed = 0
Next is time_start. Since this variable will hold the time that our program begins we will set it as
time_start = time.time()
Now we want to use a while loop. Our while loop will continue to loop while our elapsed time is less than a certain amount of time we want our program to run for. In this example my code will run for 30 seconds.
while time_elapsed < 30:
While the total time elapsed is less than 30 seconds, our loop will continue. Remember how we initialized time_elapsed to 0? We will want that variable to continuously update inside of our while loop. Right now it would stay at 0 forever and we would have an infinite while loop.
time_elapsed = time.time() - time_start
This line of code is setting our elapsed time to be the difference between the current time and the starting time of our program. After 30 seconds have passed our loop will end and we will land! Now that we have that we need to get data from our front range sensor like we did in the previous lesson.
front_sensor = drone.get_front_range()
print("front_sensor", front_sensor)
This is getting our front range sensor data and printing it like we did in a previous lesson. Since this is inside of our while loop we will continuously print out our sensor data while the program is running.
Now all that is left is setting up our conditionals. First we will check if our front range sensor value is greater than 500 millimeters. If it is, then we want to fly forward!
if front_sensor > 50:
print("Moving forward!")
drone.set_yaw(0) # yaw is 0 when we want to go forward
drone.set_pitch(30)
drone.move()
time.sleep(0.01)
Since we will be using set_yaw to turn away from obstacles in the future, we want to make sure we reset it to 0 whenever we are flying forward. We also use drone.move()
without a parameter so that our drone will continuously move until we hit another conditional check.
In our next conditional check, if our front range sensor detects an object between 50 centimeters and 30 centimeters then we will decrease the speed of our CoDrone EDU so that we don’t accidently crash into anything.
elif front_sensor < 50 and front_sensor > 30:
print("Something is getting close.. Slowing down!")
drone.set_yaw(0) # yaw is 0 when we want to go forward
drone.set_pitch(20)
drone.move()
time.sleep(0.01)
This condition is very similar to our last condition. We still want to make sure our yaw is 0 since we will be using that in the future. We also want to make sure that our pitch is slower than in our last condition. The idea here is to slow down as we are approaching something. We will also make sure to change our print statement so that python can print to us correct information!
Now that we have coded both of those conditions we have one last condition to check for. Our last condition will be when our drone is within 300 millimeters or 30 centimeters from an obstacle. In this condition we will need to make sure to stop our pitch movement and use yaw to turn away from the obstacle.
else:
print("Object detected! Turning!")
drone.set_pitch(0) # pitch is 0 when we want to turn
drone.set_yaw(40)
drone.move()
time.sleep(0.01)
Since we are no longer moving forward, we will change our print statement to reflect that. We also make sure to set our pitch to 0 so that all forward movement stops. This time we will use yaw to turn away from the obstacle that our drone is seeing. Once our drone no longer sees an obstacle it will jump out of this condition and check for the next one!
Lastly, all we need to add is a land command outside of our while loop. Once 30 seconds has passed our while loop will end and we will land to finish our program!
drone.land()
Here is the full code for this lesson:
from codrone_edu.drone import *
import time
drone = Drone()
drone.pair()
drone.takeoff()
time_elapsed = 0
time_start = time.time()
while time_elapsed < 30:
time_elapsed = time.time() - time_start
front_sensor = drone.get_front_range()
print("front_sensor", front_sensor)
if front_sensor > 50:
print("Moving forward!")
drone.set_yaw(0) # yaw is 0 when we want to go forward
drone.set_pitch(30)
drone.move()
time.sleep(0.01)
elif front_sensor < 50 and front_sensor > 30:
print("Something is getting close.. Slowing down!")
drone.set_yaw(0) # yaw is 0 when we want to go forward
drone.set_pitch(20)
drone.move()
time.sleep(0.01)
else:
print("Object detected! Turning!")
drone.set_pitch(0) # pitch is 0 when we want to turn
drone.set_yaw(40)
drone.move()
time.sleep(0.01)
drone.land()
drone.close()