This repository has been archived by the owner on Dec 1, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path2a-ManualControlWithKeyboard.py
101 lines (78 loc) · 2.79 KB
/
2a-ManualControlWithKeyboard.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
import cv2
from djitellopy import Tello
from threading import Thread
import logging
import sys
import keyboard
VELOCITY = 25
manual_command = None
stop = False
def fly(tello):
global manual_command, stop
is_flying = False
frame_read = tello.get_frame_read()
while not stop:
try:
# Show camera
if not frame_read is None:
frame = frame_read.frame
cv2.imshow("Drone", frame)
cv2.waitKey(10)
# Execute manual commands
if not manual_command is None:
if manual_command == "despegar":
tello.takeoff()
is_flying = True
elif manual_command == "aterrizar":
is_flying = False
tello.land()
elif manual_command == "giro horario":
tello.rotate_clockwise(25)
elif manual_command == "giro antihorario":
tello.rotate_anticlockwise(25)
elif manual_command == "salir":
stop = True
break
manual_command = None
# Execute arrow keys
if is_flying:
velocity_fb = velocity_lr = velocity_ud = velocity_yaw = 0
if keyboard.is_pressed(keyboard.KEY_UP): # Up
velocity_fb = velocity_fb + VELOCITY
elif keyboard.is_pressed(keyboard.KEY_DOWN): # Down
velocity_fb = velocity_fb + (VELOCITY * -1)
elif keyboard.is_pressed("left"): # Left
velocity_lr = velocity_lr + VELOCITY
elif keyboard.is_pressed("right"): # Right
velocity_lr = velocity_lr + (VELOCITY * -1)
tello.send_rc_control(velocity_lr, velocity_fb, velocity_ud, velocity_yaw)
except:
stop = True
error = sys.exc_info()[0]
print("Unexpected error:", error)
def main():
global manual_command, stop
thread_fly = None
tello = Tello()
# Remove most of the log lines
logger = logging.getLogger('djitellopy')
logger.setLevel(logging.CRITICAL)
tello.connect()
try:
tello.streamon()
thread_fly = Thread(target=fly, args=[tello])
thread_fly.start()
while not stop:
manual_command = input("Pulsa una tecla de dirección o escribe un comando ('despegar','aterrizar','giro horario', 'giro antihorario', 'salir'): ")
except:
error = sys.exc_info()[0]
print("Unexpected error:", error)
finally:
stop = True
if not thread_fly is None:
thread_fly.join()
tello.land()
tello.streamoff()
tello.end()
if __name__ == '__main__':
main()