-
Notifications
You must be signed in to change notification settings - Fork 1
/
integration.py
159 lines (122 loc) · 5.18 KB
/
integration.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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
from djitellopy import Tello
import cv2
import pygame
import numpy as np
import os
import time
from imutils.video import fps
from yolo_detection import YoloTracker
from bangbang_motion_control import MotionController as NewMotionController
from pid_motion_control import MotionController as NewMotionController
from old_motion_control import MotionController as OldMotionController
from configparser import ConfigParser
config = ConfigParser()
config.read('config.ini')
# Frames per second of the pygame window display
# A low number also results in input lag, as input information is processed once per frame.
PYGAME_FPS = int(config['control']['PYGAME_FPS'])
LOG = config.getboolean('control', 'LOG')
CONTROL_METHOD = config['control']['CONTROL_METHOD'] # 'yolo' or 'hsv'
SAVE_FRAMES = False
MAX_CSV_LENGTH = int(config['data_collection']['MAX_CSV_LENGTH'])
SAVE_DIR = config['data_collection']['SAVE_DIR']
class FrontEnd(object):
""" Maintains the Tello display and moves it through the keyboard keys.
Press escape key to quit.
The controls are:
- RETURN: Takeoff
- SPACE: Land
"""
handControl = True
def __init__(self):
# Init pygame
pygame.init()
# Create pygame window
pygame.display.set_caption("Tello video stream")
self.screen = pygame.display.set_mode([int(960 * 0.9), int(720 * 0.9)])
# Init Tello object that interacts with the Tello drone
self.tello = Tello()
# Drone velocities between -100~100
self.vz = 0
self.vx = 0
self.vy = 0
self.vyaw = 0
self.v = 0.0, 0.0, 0.0 # yaw, up/down, forward/backward
self.speed = 10 # do not change this
self.recording = False
self.send_rc_control = False
# create update timer
pygame.time.set_timer(pygame.USEREVENT + 1, 1000 // PYGAME_FPS)
self.notecard_tracker = YoloTracker(config['yolo_tracker'])
self.new_motion_controller = NewMotionController()
self.old_motion_controller = OldMotionController()
def process_frame(self, frame):
# Displaying battery
battery_text = "Battery: {}%".format(self.tello.get_battery())
frame = cv2.putText(frame, battery_text, (5, 720 - 5), cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 255, 255), 2)
frame = cv2.putText(frame, "velocities: " + str(self.v), (5, 45), cv2.FONT_HERSHEY_SIMPLEX, 1,
(255, 255, 255), 2)
frame = cv2.putText(frame, f'saving frames: {SAVE_FRAMES}', (5, 720-45), cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 255, 255), 2)
frame = np.rot90(frame)
frame = np.flipud(frame)
frame = cv2.resize(frame, (int(frame.shape[1] * 0.9), int(frame.shape[0] * 0.9)))
frame = pygame.surfarray.make_surface(frame)
return frame
def run(self):
self.tello.connect()
self.tello.set_speed(self.speed)
# In case streaming is on. This happens when we quit this program without the escape key.
self.tello.streamoff()
self.tello.streamon()
# self.tello.send_keepalive()
frame_read = self.tello.get_frame_read()
should_stop = False
while not should_stop:
last_time = time.time()
if frame_read.stopped:
break
for event in pygame.event.get():
if event.type == pygame.USEREVENT + 1:
self.update()
elif event.type == pygame.QUIT:
break
elif event.type == pygame.KEYUP:
if event.key == pygame.K_RETURN:
self.tello.takeoff()
self.motion_controller.clear_data()
self.send_rc_control = True
elif event.key == pygame.K_SPACE:
should_stop = True
self.screen.fill([0, 0, 0])
frame = frame_read.frame
frame, rect = self.notecard_tracker.get_rect(frame)
print(f"obj pixels: x:{rect[0]}\t y:{rect[1]}\t size:{rect[2]})")
frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB) # Commented out to color correct
dtime = time.time() - last_time
v = self.new_motion_controller.instruct(rect, dtime)
old_v = self.old_motion_controller.instruct(rect, time)
for i in range(len(v)):
if v[i] is None:
v[i] = old_v[i]
print(f"left/right: {v[0]}\t up/down: {v[1]}\t forward/back: {v[2]}")
if self.tello.is_flying:
v = list(map(int, v))
self.vx, self.vy, self.vz = v
frame = self.process_frame(frame)
self.screen.blit(frame, (0, 0))
pygame.display.update()
time.sleep((1 / PYGAME_FPS) - (time.time() - last_time))
self.tello.land()
self.send_rc_control = False
self.tello.end()
def update(self):
if not self.send_rc_control:
return
self.tello.send_rc_control(self.vx, self.vz,
self.vy, self.vyaw)
def main():
frontend = FrontEnd()
# run frontend
frontend.run()
if __name__ == '__main__':
main()