0

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?

enzo
  • 9,861
  • 3
  • 15
  • 38
PlantPot
  • 49
  • 6
  • Without knowing `# Drone code`, how can we tell? – Mad Physicist Aug 10 '21 at 00:30
  • This will only work if you have a loop in your code ([example](https://stackoverflow.com/a/18994932/9997212)). If you do have a loop but Mu Editor doesn't go to the `except` clause, try running the Python file using the command-line. – enzo Aug 10 '21 at 00:36
  • 1
    Maybe don't run the code with/from the editor? Use the editor to edit the code, and `python` to execute it. – chepner Aug 10 '21 at 00:37
  • Drone code could be almost anything, but mostly pretty basic - iteration, branching, inputs etc. It really depends on what the students are doing. Hence why KeyboardInterrupt is great as it gets them out of a jam when they've done something wrong (i.e. don't exit out of a loop and have the drone elevate in to the ceiling, or overshoot the distance and go into a wall). – PlantPot Aug 10 '21 at 01:54

1 Answers1

1

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