From 73f424cefc1984b933d1d69639cc5233e819ddb2 Mon Sep 17 00:00:00 2001 From: Nikita Kiselov Date: Tue, 16 Mar 2021 14:08:11 +0200 Subject: [PATCH] Land/Take-off on Space key --- README.md | 3 +-- main.py | 23 ++++++++++++++++------- 2 files changed, 17 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index 22afef5..9d5756e 100644 --- a/README.md +++ b/README.md @@ -110,8 +110,7 @@ The following is a list of keys and action description - * `k` -> Toggle Keyboard controls * `g` -> Toggle Gesture controls -* `Left Shift` -> Take off drone #TODO -* `Space` -> Land drone +* `Space` -> Take off drone(if landed) **OR** Land drone(if in flight) * `w` -> Move forward * `s` -> Move back * `a` -> Move left diff --git a/main.py b/main.py index f729a19..bb48ab2 100644 --- a/main.py +++ b/main.py @@ -37,6 +37,7 @@ def get_args(): return args + def select_mode(key, mode): number = -1 if 48 <= key <= 57: # 0 ~ 9 @@ -49,6 +50,7 @@ def select_mode(key, mode): mode = 2 return number, mode + def main(): # init global vars global gesture_buffer @@ -59,16 +61,13 @@ def main(): args = get_args() KEYBOARD_CONTROL = args.is_keyboard WRITE_CONTROL = False + in_flight = False # Camera preparation tello = Tello() tello.connect() tello.streamon() - - # Take-off drone - tello.takeoff() - cap = tello.get_frame_read() # Init Tello Controllers @@ -107,15 +106,26 @@ def tello_battery(tello): key = cv.waitKey(1) & 0xff if key == 27: # ESC break + elif key == 32: # Space + if not in_flight: + # Take-off drone + tello.takeoff() + in_flight = True + + elif in_flight: + # Land tello + tello.land() + in_flight = False + elif key == ord('k'): - mode=0 + mode = 0 KEYBOARD_CONTROL = True WRITE_CONTROL = False tello.send_rc_control(0, 0, 0, 0) # Stop moving elif key == ord('g'): KEYBOARD_CONTROL = False elif key == ord('n'): - mode=1 + mode = 1 WRITE_CONTROL = True KEYBOARD_CONTROL = True @@ -124,7 +134,6 @@ def tello_battery(tello): if 48 <= key <= 57: # 0 ~ 9 number = key - 48 - # Camera capture image = cap.frame