Glide vs stop and a better startup-sequence

In this section, the term “thrust” is also used for speed; because, basically, speed is caused by thrust.

The following example shows the difference between active stopping and holding position on the one hand; and reducing the thrust to 0.0 on the other. Active stopping will cause the drone to hold position, while giving no thrust will make the drone to glide on; it will just get slower due to air drag. If you want to experience the difference between the two yourself, start the program, push the flying drone and see what happens.

##### Suggested clean drone startup sequence #####
import time, sys
import ps_drone                                    # Import PS-Drone-API

drone = ps_drone.Drone()                           # Start using drone
drone.startup()                                    # Connects to drone and starts subprocesses

drone.reset()                                      # Sets drone's status to good
while (drone.getBattery()[0]==-1): time.sleep(0.1) # Wait until drone has done its reset
print "Battery: "+str(drone.getBattery()[0])+"% "+str(drone.getBattery()[1]) # Battery-status
if drone.getBattery()[1]=="empty":  sys.exit()     # Give it up if battery is empty

drone.useDemoMode(True)                            # Set 15 basic dataset/sec (default anyway)
drone.getNDpackage(["demo"])                       # Packets, which shall be decoded
time.sleep(0.5)                                    # Give it some time to awake fully

drone.takeoff()                                    # Fly, drone, fly !
while drone.NavData["demo"][0][2]: time.sleep(0.1) # Wait until drone is completely flying

##### Mainprogram begin #####
print "The Drone is flying now, land it with any key but "

gliding = False
print "The Drone is holding its position, toggle \"hold position\" and \"glide\" with -key."

end = False
while not end:
    key = drone.getKey()                           # Get a pressed key
    if key == " ":
    if gliding:
        gliding = False
        drone.stop()                               # Stop and hold position
        print "Drone is now holding its position"
        gliding = True
        drone.moveForward(0)                       # Just do not fly actively in any direction
        print "Drone is now gliding"
    elif key:  end = True                          # End this loop, sys.exit() is also ok
    else:      time.sleep(0.1)                     # Wait until next looping
Download sample

For a clean start up some additional code has been added, because a just time-based controlling is rarely not a good solution. As a suggestion just copy and paste this start up-sequence to your own code.

The first lines remain more or less the same. Resetting the drone makes it work again, if the drone’s firmware blocks flying, indicated by red LEDs. Resetting takes a while and the drone does not send data in the meantime. When sending data again (the battery status will be checked here), the drone has almost finished, but still needs a little time.

The commands useDemoMode(val) and getNDpackage(val) will be explained later, the values True and ["demo"] are standard-settings anyway.
However, it could happen that you require more data and faster. Therefore, a brief explanation beforehand: The drone sends its sensor-/NavData in a large bunch. This includes several packets called "demo", "time", "altitude" and so on, with specified data. In demo-mode, there are only the packages "demo" and "vision_detect" available, both 15 times per second; most of the time, this is enough and perfect for learning or debugging. Switching off the demo-mode means all NavData-packages are available, 200 times per second.
Available NavData-values are always stored in an array called "NavData". First dimension is the name of the small package the values are stored in. Position [0][2] stores the drone’s landed-status-tag. If not set, the drone is ready for action.

It has already been mentioned, that a take-off takes around seven seconds. However, this example uses the drone’s flight-status. There will be a further explanation later.

The command getKey() has been added for convenience and has technically nothing to do with the drone. Use it to get the value of a pressed key, without worrying about timing, deadlocks and other annoying effects.
Notice that there is no command to land the drone, PS-Drone does it for you at the end of your program. The example stops when a key was pressed that is not <space>.

It is recommended to implement an emergency stop in your code which will land the drone by pressing a key. Also a powerpoint-remote control is suggested in order to be more mobile; a pressed key on that remote control is like pressing a key or a key chain on the keyboard and simple to implement.