What if you wanted to check how long one section in your program took, like how long your drone takes to fly in one square, or if pitching forward for 2 seconds actually takes 2 seconds? There’s a way to do that, and it just involves moving some timers around in your program!
For this exercise, we will pretend that we are programming a movie videography drone. The director wants a 360 degree continuous shot around the two actors for an epic final scene! They need at least 30 seconds of video to cut into the movie. For this program, we will create a loop that lasts 30 seconds using a time and fly in a circle around the actors.
After importing the CoDrone Mini, Sequence and time libraries, creating the drone object, and pairing, have your drone take off.
Next, we need create the variables for the loop. We will create a start_time that will capture the time right before the loop. We will also create an end_time and assign it the start_time variable value + 30 seconds for the required flight time:
from CoDrone_mini import Sequence
drone = CoDrone_mini.CoDrone()
start_time = time.time()
end_time= start_time + 30
Now, we need to create the loop. To do this, we will compare the start_time to the end_time and as long as it is less than the end time, we keep running the circle. Note that we are using the fly_sequence function. You can program your own circular flight path if you choose, but if not, make sure you have the correct imports as shown above.
while start_time < end_time:
start_time = time.time() #update the time once the sequence is done
We have to update the start_time after each sequence or it would end up creating an infinite loop. Can you figure out why?
Now, have your drone land. To check if our flight time was enough, add a line that prints out the difference between start time and end time. Did we fly long enough? Was it exactly 30 seconds? If the time wasn’t exact, why do you think this is the case?