Now that we know how to read our analog stick values, let’s use them for flying! You may remember from previous lessons that our flight movement commands (set_pitch, set_roll, etc.) take values between -100 and 100. This works perfectly for our analog stick functions, as they give values between -100 and 100. We simply need to assign our flight movement commands to the values our analog sticks are currently reading!
For this custom controller activity, we will be switching up the default configuration that you are familiar with. We will make the left analog stick control pitch and roll, while the right analog stick will control throttle and yaw. Let’s start with throttle so that we have a way to land the drone. If necessary, the controller’s emergency stop functionality will still work!
from codrone_edu.drone import *
drone = Drone()
drone.pair()
drone.takeoff()
while True:
throttle = drone.get_right_joystick_y()
drone.set_throttle(throttle)
drone.move()
In this code, we are creating an infinite while loop that will continuously check our right analog stick’s Y value and assign its value to our throttle flight movement. If you move the left analog stick while in flight, you will notice that it doesn’t affect the drone at all. That’s only because we haven’t coded it yet!
See if you can code the rest of the flight movements on your own. The solution will be posted below:
from codrone_edu.drone import *
drone = Drone()
drone.pair()
drone.takeoff()
while True:
pitch = drone.get_left_joystick_y()
roll = drone.get_left_joystick_x()
throttle = drone.get_right_joystick_y()
yaw = drone.get_right_joystick_x()
drone.set_pitch(pitch)
drone.set_roll(roll)
drone.set_throttle(throttle)
drone.set_yaw(yaw)
drone.move()