-
Notifications
You must be signed in to change notification settings - Fork 0
/
vision_module.py
181 lines (144 loc) · 5.32 KB
/
vision_module.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
#!/usr/bin/python
import os, os.path
import time
import math
import cv2
import numpy as np
import drone_ctrl
from drone_ctrl import drone as drn
import position_vector
from position_vector import PositionVector
import cv_fns
from cv_fns import cvfunc as CV
import a_logger
from a_logger import alogger as alog
import droneapi.lib
from droneapi.lib import VehicleMode, Location, Attitude
class VModule(object):
def __init__(self):
# the framerate
self.steps = 10
self.triggered = False
self.targetno =1
def show_frame(self,no):
threshfile = "/home/ardupilot/droneapi-python/example/my_app/stereo/thresh" + str(no) + ".png"
circfile = "/home/ardupilot/droneapi-python/example/my_app/stereo/circled" + str(no) + ".png"
thresh = cv2.imread(threshfile)
circ = cv2.imread(circfile)
cv2.namedWindow('stereo')
cv2.startWindowThread()
cv2.imshow('stereo', np.hstack((thresh,circ)))
cv2.waitKey(1)
return
def location_report(self, tarloc, taralti):
#get target and vehicle locations
self.targetLoc = PositionVector()
self.vehicleLoc = PositionVector()
self.vehicleLoc.set_from_location(drn.get_location())
full_loc = Location(
float(tarloc[0]), float(tarloc[1]),
float(taralti),
is_relative=True
)
self.targetLoc.set_from_location(full_loc)
loc = "Vehicle location: " + "X: " + format(self.vehicleLoc.x) + "Y: " + format(self.vehicleLoc.y) + "Z: " + format(self.vehicleLoc.z)
alog.log.info(loc)
target_loc = "Target location: " + "X: " + format(self.targetLoc.x) + "Y: " + format(self.targetLoc.y) + "Z: " + format(self.targetLoc.z)
alog.log.info(target_loc)
dist = self.vehicleLoc.get_distance_xyz(self.vehicleLoc,self.targetLoc)
reld = "Relative distance: " + format(dist)
alog.log.info(reld)
return dist
#load_target- load an image to simulate the target.
def check_target1(self):
tarloc = [33.775497,-84.396860] #33.775497 -84.396860 33.775632 -84.396806
taralti = 30
#wait for trigger over here
reld = self.location_report(tarloc,taralti)
if not(self.triggered):
while (int(reld)>10):
time.sleep(2)
reld = self.location_report(tarloc,taralti)
target = cv2.imread('/home/ardupilot/droneapi-python/example/my_app/stereo/1left.png')
self.load_target(target,11,0.0,0.0)
#load_target- load an image to simulate the target.
def check_target2(self):
tarloc = [33.776888,-84.396367] #33.775497 -84.396860 33.776888 -84.396367
taralti = 50
self.targetno = 2
#wait for trigger over here
reld = self.location_report(tarloc,taralti)
if not(self.triggered):
while (int(reld)>10):
time.sleep(2)
reld = self.location_report(tarloc,taralti)
target = cv2.imread('/home/ardupilot/droneapi-python/example/my_app/stereo/2left.png')
self.load_target(target,11,0.0,0.0)
def check_target3(self):
tarloc = [33.777845, -84.396523] #33.775497 -84.396860 33.776888 -84.396367 33.777845 -84.396523
taralti = 70
self.targetno = 3
#wait for trigger over here
reld = self.location_report(tarloc,taralti)
if not(self.triggered):
while (int(reld)>10):
time.sleep(2)
reld = self.location_report(tarloc,taralti)
target = cv2.imread('/home/ardupilot/droneapi-python/example/my_app/stereo/3left.png')
self.load_target(target,11,0.0,0.0)
def load_target(self,target,zoom,camera_constX,camera_constY):
frame = CV.get_frame(target,camera_constX,camera_constY,zoom)
self.load_window(target,frame,zoom,camera_constX,camera_constY)
def load_window(self,target,frame,zoom,camera_constX,camera_constY):
self.steps = zoom
cv2.namedWindow('HUD')
cv2.startWindowThread()
while self.steps>1.0:
if (int(self.steps*10) == 91) and (self.targetno == 1): #ANALYZE THE VIDEO MODULE AND GET RESULTS
alog.log.debug("Obstacle detected.")
drn.right_drift(2)
drn.resume()
self.show_frame(self.targetno)
camera_constX=0.02
camera_constY=0.39
if (int(self.steps*10) == 91) and (self.targetno == 2): #ANALYZE THE VIDEO MODULE AND GET RESULTS
alog.log.debug("Obstacle detected.")
drn.left_drift(6)
drn.resume()
self.show_frame(self.targetno)
camera_constX=0.47
camera_constY=0.4
if (int(self.steps*10) == 91) and (self.targetno == 3): #ANALYZE THE VIDEO MODULE AND GET RESULTS
alog.log.debug("Obstacle detected.")
drn.left_drift(2)
drn.resume()
self.show_frame(self.targetno)
camera_constX=0.05
camera_constY=0.2
st = "Steps: " + format(self.steps)
alog.log.debug(st)
cv2.imshow('HUD',frame)
cv2.waitKey(1)
time.sleep(0.1)
camera_constX = camera_constX
camera_constY = camera_constY
self.steps = self.steps - 0.1
self.load_target(target,self.steps,camera_constX,camera_constY)
cv2.waitKey(3)
cv2.destroyAllWindows()
cv2.waitKey(1)
alog.log.info("Exiting vision threads")
vm = VModule()
if __name__ == "__builtin__":
###################################################################
##########################MODULE TESTING###########################
drn.connect(local_connect())
# THE VIDEO MODULE
drn.run()
vm.check_target1()
vm.check_target2()
vm.check_target3()
# time.sleep(3)
# vm.load_target('/home/ardupilot/droneapi-python/example/my_app/target.jpg',15,0.0)
###################################################################
###################################################################