forked from celik02/stm_yolo_son_kod
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pid_cam.py
185 lines (150 loc) · 4.67 KB
/
pid_cam.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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
from multiprocessing import Manager
from multiprocessing import Process
from ctypes import c_char_p
from utils import gstreamer_pipeline
from utils import send_ned_velocity
from utils import proper_detection
from utils import YOLO_Detector
from utils import is_centered
from utils import ihamibagla
from utils import armolveuc
from utils import position
from utils import quarter
from utils import decide
from utils import PID
from utils import big_enough
import imutils
import time
import cv2
import glob
import signal
import sys
def signal_handler(sig, frame):
print("[INFO] Exiting...")
sys.exit()
def flight(pidx, pidy, satisfactory, phase, centered, logo, pos_dict, is_it_big, seen):
signal.signal(signal.SIGINT, signal_handler)
iha = ihamibagla()
armolveuc(iha, 6)
saf=time.time()
phase.value = 2
logo.value = "odtu"
inis = 0
while iha.mode == 'GUIDED':
#if lost.value == 1:
# position(iha, 0, 0, 6, 0)
if (seen.value == 1) and not centered.value:
inis = 1
if centered.value:
time.sleep(0.1)
print("Merkezleme basarili")
"""iha.mode="LAND"
while iha.armed==True:
if not centered.value:
#iha.mode = 'GUIDED'
#time.sleep(0.3)
#inis = 1
break
if not centered.value:
continue"""
elif inis:
send_ned_velocity(iha, pidx.value, pidy.value, 0)
if time.time()-saf>=1:
print(inis, pidx.value,pidy.value)
saf=time.time()
if iha.armed == 0:
time.sleep(0.5)
break
def cv(up_left_x, up_left_y, bottom_right_x, bottom_right_y,
logo, centered, phase, pos_dict, satisfactory, lost, is_it_big, seen):
signal.signal(signal.SIGINT, signal_handler)
weights = "Tiny-YOLO/logo_final.weights"
cfg = "Tiny-YOLO/logo.cfg"
detector = YOLO_Detector(weights, cfg)
counter_for_mapping = 1
local_logo = 0
logo_list = ["stm", "odtu", "ort", "helikopter_inis"]
pos_dict["turk_bayragi"] = 5
lost_list = [False for i in range(4)]
centered_list = [False for i in range(5)]
order_of_positions = [2, 4, 3, 1]
camera = cv2.VideoCapture(gstreamer_pipeline(), cv2.CAP_GSTREAMER)
time.sleep(0.2)
#time.sleep(2)
while True:
grabbed, frame = camera.read()
rects, confidences, classIDs = detector.detect(frame)
frame, cond, (x1, x2, y1, y2), c = proper_detection(frame, rects, logo.value,
confidences, classIDs)
if cond:
seen.value = 1
centered_list[:-1] = centered_list[1:]
centered_list[-1] = c
lost_list[:-1] = lost_list[1:]
lost_list[-1] = True
up_left_x.value = x1
up_left_y.value = y1
bottom_right_x.value = x2
bottom_right_y.value = y2
is_it_big.value = big_enough(x2-x1, y2-y1)
else:
seen.value = 0
centered_list[:-1] = centered_list[1:]
centered_list[-1] = False
lost_list[:-1] = lost_list[1:]
lost_list[-1] = False
lost.value = 0 if any(lost_list) else 1
centered.value = is_centered(centered_list)
cv2.imshow("test", frame)
cv2.waitKey(1)
def pid(x1, y1, x2, y2, pidx, pidy, lost, logo):
signal.signal(signal.SIGINT, signal_handler)
local_logo = 0
_pid = PID()
while True:
if (local_logo != logo.value) or lost.value == 1:
_pid = PID()
local_logo = logo.value
pidx.value = 0
pidy.value = 0
pidx.value, pidy.value = _pid.update(x1.value, y1.value, x2.value, y2.value, local_logo)
time.sleep(0.015)
if __name__ == "__main__":
with Manager() as manager:
# PID <-----> CV
up_left_x = manager.Value("i", 0)
up_left_y = manager.Value("i", 0)
bottom_right_x = manager.Value("i", 0)
bottom_right_y = manager.Value("i", 0)
lost = manager.Value("i", 1)
# AUTOPILOT <-----< PID
pidx = manager.Value("f", 0.0)
pidy = manager.Value("f", 0.0)
# CV <-------> AUTOPILOT
logo = manager.Value(c_char_p, "stm")
centered = manager.Value("i", 0)
phase = manager.Value("i", 1)
satisfactory = manager.Value("i", 1)
pos_dict = manager.dict()
is_it_big = manager.Value("i", 0)
seen = manager.Value("i", 0)
# we have 3 independent processes
# 1. AUTOPILOT - controls the drone
# 2. CV - finds/localizes the object
# 3. PID - PID control loop determines x, y velocity
processPID = Process(target=pid,
args=(up_left_x, up_left_y, bottom_right_x, bottom_right_y,
pidx, pidy, lost, logo))
processCV = Process(target=cv,
args=(up_left_x, up_left_y, bottom_right_x, bottom_right_y,
logo, centered, phase, pos_dict, satisfactory, lost, is_it_big, seen))
processAUTOPILOT = Process(target=flight,
args=(pidx, pidy, satisfactory, phase, centered, logo, pos_dict, is_it_big, seen))
# start all 3 processes
processAUTOPILOT.start()
processCV.start()
processPID.start()
# join all 3 processes
processAUTOPILOT.join()
processCV.join()
processPID.join()