Emergency stop in Python

906 views Asked by At

My students are using the PyParrot library to control drones. However, we need a fail safe if they make an error (i.e. coding a drone in to a wall/elevate to the ceiling etc).

Previously I had used KeyboardInterrupt, however, this year we are using Mu Editor. Mu Editor exits immediately on ctrl+c and does not run anything from my exception.

try:
   # Drone code
   
except KeyboardInterrupt:
    # Landing Code

How else can I ensure that there's an "emergency stop" with Python?

1

There are 1 answers

0
Command-Alt-Escape On

One possibility is to run # Drone code in a subprocess, then leave the main thread open to receive an input.

from multiprocessing import Process

def drone_code():
    # Drone code

if __name__ == '__main__':
    p = Process(target=drone_code)
    p.daemon=True # kill subprocess if main process killed
    p.start()
    input("Press Enter to safely exit...")
    if p.is_alive():
        p.kill()
        #Landing Code