Now that we know how to use the different movement commands separately, lets see what happens when we use them together! Complete the steps below for our first combined program:
- Make a copy of yaw.py or any of your exercise files to this point.
- Enter a command to make the drone move forward for 1 second at 20% power
- Enter a command to make the drone move to the left for 2 seconds at 20% power
Once you are done, run your code and see what happens! If you are having issues with your code running, here is some example code below:
from codrone_edu.drone import *
drone = Drone()
drone.pair()
drone.takeoff()
drone.set_pitch(20)
drone.move(1)
drone.set_roll(-20)
drone.move(2)
drone.land()
drone.close()
What did you observe when you ran the code? You may have noticed that the drone moved forward correctly, but then it moved forward and left at the same time. This is because we never set the pitch back to 0. If you remember from earlier, we learned that these set movement commands were changing the value of a variable. If we don’t set the value back to 0, then it stays whatever value we set it as last. This can actually be valuable if we want to move in two directions at once, like forward and to the right, but was not what we expected.
Try to think how you would reset the value of the pitch so that our drone only moves to the left. To do this, we just set the pitch to 0 after the drone has moved forward.
drone.set_pitch(0)
Add this new line of code to your program and test it again. This time the drone should move forward and then left only. Below is what the code should look like with the reset value (minus the importing, takeoff, etc…):
from codrone_edu.drone import *
drone = Drone()
drone.pair()
drone.takeoff()
drone.set_pitch(20) # Moves the drone forward at 20% power
drone.move(1) # Moves for 1 second
drone.set_pitch(0) # Resets the pitch back to 0
drone.set_roll(-20) # Moves the drone to the left at 20% power
drone.move(2) # Moves for 2 seconds
drone.land()
drone.close()