-
Notifications
You must be signed in to change notification settings - Fork 0
/
ControlConsoleV2.2.py
399 lines (353 loc) · 14.3 KB
/
ControlConsoleV2.2.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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
from tkinter import *
import serial, time, math, random
from PIL import Image, ImageTk
from VideoCapture import Device
import threading
CAMERA = True
class Console(Frame):
def __init__(self):
#self.port = serial.Serial("COM3", 57600)
self.cam = Device(1)
self.fwState = True
self.bwState = True
self.rtState = True
self.ltState = True
self.firstPress = True
self.pressCount = 0
self.commands = []
Frame.__init__(self)
self.master.title("Rover Control")
self.grid()
self._viewVar = StringVar()
self._viewVar.set("")
self._viewEntry = Entry(self, textvariable = self._viewVar)
self._viewEntry.grid(row = 5, columnspan = 3)
self._randomVar = IntVar()
self._randomToggle = Checkbutton(self, text = "Random Toggle", \
variable = self._randomVar, \
command = self._randomControl)
self._forwardButton = Button(self, text = "Forward", \
command = self._forward)
self._backwardButton = Button(self, text = "Backward", \
command = self._backward)
self._rightButton = Button(self, text = "Right", \
command = self._right)
self._leftButton = Button(self, text = "Left", \
command = self._left)
self._stopButton = Button(self, text = "Abort", \
command = self._stop)
self._beginButton = Button(self, text = "Begin Mission", \
command = self._begin)
self._undoButton = Button(self, text = "Undo", \
command = self._undo)
self._restartButton = Button(self, text = "Start Over", \
command = self._restart)
self._pictureButton = Button(self, text = "Take Picture", \
command = self._takePicture)
self._forwardButton.grid(row = 0, column = 1)
self._backwardButton.grid(row = 2, column = 1)
self._rightButton.grid(row = 1, column = 2)
self._leftButton.grid(row = 1, column = 0)
self._randomToggle.grid(row = 2, column = 0)
self._stopButton.grid(row = 1, column = 1)
self._beginButton.grid(row = 3, column = 2)
self._undoButton.grid(row = 3, column = 0)
self._restartButton.grid(row = 3, column = 1)
self._pictureButton.grid(row = 2, column = 2)
def _forward(self):
self.bwState = True
self.ltState = True
self.rtState = True
if self.fwState:
if not self.firstPress:
self.commands.append(self.pressCount.to_bytes(1, "big"))
self.commands.append(b"\x31")
self._viewVar.set("Forward cm")
self.pressCount = 1
self.fwState = False
self.firstPress = False
else:
self.pressCount+=1
def _backward(self):
self.fwState = True
self.ltState = True
self.rtState = True
if self.bwState:
if not self.firstPress:
self.commands.append(self.pressCount.to_bytes(1, "big"))
self.commands.append(b"\x32")
self._viewVar.set("Backward cm")
self.pressCount = 1
self.bwState = False
self.firstPress = False
else:
self.pressCount += 1
def _right(self):
self.fwState = True
self.bwState = True
self.ltState = True
if self.rtState:
if not self.firstPress:
self.commands.append(self.pressCount.to_bytes(1, "big"))
self.commands.append(b"\x33")
self._viewVar.set("Right 15 degrees")
self.pressCount = 15
self.rtState = False
self.firstPress = False
else:
self.pressCount += 15
def _left(self):
self.fwState = True
self.bwState = True
self.rtState = True
if self.ltState:
if not self.firstPress:
self.commands.append(self.pressCount.to_bytes(1, "big"))
self.commands.append(b"\x34")
self._viewVar.set("Left 15 degrees")
self.pressCount = 15
self.ltState = False
self.firstPress = False
else:
self.pressCount += 15
def _takePicture(self):
if not self.firstPress:
self.commands.append(self.pressCount.to_bytes(1, "big"))
self.commands.append(b"\x35")
self._viewVar.set("Take Picture")
def _stop(self):
self.port.write(b"\x36")
self._viewVar.set("Aborting")
self.commands.clear()
def _random(self):
while self._randomVar.get():
print("Random Mode Enabled")
while self.firstImage()[0] + self.firstImage()[1] < 100:
print("Rover not found!")
while self._randomVar.get():
if self.port.read() == b"\x53":
self.command = random.randint(49,52)
self.distance = random.randint(15,90)
self.port.write(self.command.to_bytes(1,"big"))
self.port.write(self.distance.to_bytes(1,"big"))
if self.getCenter(center) == [0,0]:
print("Rover Lost!")
while self.firstImage() == [0,0]:
pass
print("Rover Found!")
print(center)
print("Random Mode Disabled")
return
def _randomControl(self):
self.locationFinder = threading.Thread(target = self._random)
self.locationFinder.start()
def firstImage(self):
while True:
try:
self.img = self.cam.getImage()
self.pixels = self.img.load()
break
except:
print("Nothing yet")
self.redRatio = 0
self.maxRed = 0
self.redLoc = [0,0]
self.blueRatio = 0
self.maxBlue = 0
self.blueLoc = [0,0]
for x in range(1600):
for y in range(1200):
self.dot = self.pixels[x,y]
if self.dot[0]+self.dot[1]+self.dot[2] > 100:
self.redRatio = self.dot[0]/(self.dot[1]+self.dot[2]+1)
if self.redRatio > self.maxRed:
self.maxRed = self.redRatio
self.redLoc = [x,y]
self.blueRatio = self.dot[2]/(self.dot[0]+self.dot[1]+1)
if self.blueRatio > self.maxBlue:
self.maxBlue = self.blueRatio
self.blueLoc = [x,y]
global center
center = [int((self.blueLoc[0] + self.redLoc[0])/2), int((self.blueLoc[1] + self.redLoc[1])/2)]
print(center)
return center
def getCenter(self, ct):
while True:
try:
self.img = self.cam.getImage()
self.pixels = self.img.load()
break
except:
pass
self.redRatio = 0
self.maxRed = 0
self.redLoc = [0,0]
self.blueRatio = 0
self.maxBlue = 0
self.blueLoc = [0,0]
for x in range(ct[0]-125, ct[0]+125):
for y in range(ct[1]-125, ct[1]+125):
self.dot = self.pixels[x,y]
if self.dot[0]+self.dot[1]+self.dot[2] > 100:
self.redRatio = self.dot[0]/(self.dot[1]+self.dot[2]+1)
if self.redRatio > self.maxRed:
self.maxRed = self.redRatio
self.redLoc = [x,y]
self.blueRatio = self.dot[2]/(self.dot[0]+self.dot[1]+1)
if self.blueRatio > self.maxBlue:
self.maxBlue = self.blueRatio
self.blueLoc = [x,y]
global center
center = [int((self.blueLoc[0] + self.redLoc[0])/2), int((self.blueLoc[1] + self.redLoc[1])/2)]
print(center)
if center != [0,0]:
self.boundCheck(center)
return center
def direction(self, initial, final):
self.angle = [0,0]
self.x = initial[0] - final[0]
self.y = (initial[1]- final[1])
self.angle = math.degrees(math.atan2(self.x,self.y))
return self.angle
def boundCheck(self, ct):
self.boundary = Image.open(r"C:\Users\dklebe\Desktop\Rover\Navigation\boundary.png")
self.bound = self.boundary.load() #todo: draw actual boundary case
if self.bound[ct[0], ct[1]] == (255, 255, 255, 255):
print("Out of Bounds!")
self.port.write(b"\x36")
self.time1 = time.time()
print(self.time1)
while self.port.read() != b"\x53":
self.time2 = time.time()
if self.time2 - self.time1 > 10:
print("No feedback received; reattempting connection")
return
print("Rover sucessfully stoped")
print("Calculating angle 1")
self.theta1 = self.direction(self.firstImage(), [800,600])
#print(self.theta1)
self.theta2 = self.direction(self.firstImage(), self.getYellow(self.firstImage()))
#print(self.theta2)
self.theta_1st = int(self.theta1 - self.theta2)
print(self.theta_1st)
print("Calculating angle 2")
self.theta3 = self.direction(self.firstImage(), [800,600])
#print(self.theta3)
self.theta4 = self.direction(self.firstImage(), self.getYellow(self.firstImage()))
#print(self.theta4)
self.theta_2nd = int(self.theta3 - self.theta4)
print(self.theta_2nd)
self.theta = int((self.theta_1st + self.theta_2nd) / 2)
if self.theta < -180:
self.theta = self.theta + 360
elif self.theta > 180:
self.theta = self.theta - 360
if self.theta < 0:
self.port.write(b"\x34")
else:
self.port.write(b"\x33")
self.theta = abs(self.theta)
print("Final Angle: ", self.theta)
self.port.write(self.theta.to_bytes(1, "big"))
self.time1 = time.time()
while self.port.read() != b"\x53":
self.time2 = time.time()
if self.time2 - self.time1 > 60:
print("No feedback received; reattempting connection")
return
print("Turn executed")
self.firstImage()
self.dist = int(math.sqrt(math.pow((ct[0] - 800), 2) + math.pow((ct[1] - 600), 2)) / 5)
print(self.dist)
self.port.write(b"\x31")
self.port.write(self.dist.to_bytes(1, "big"))
self.time1 = time.time()
while self.port.read() != b"\x53":
self.time2 = time.time()
if self.time2 - self.time1 > 60:
print("No feedback received; reattempting connection")
return
print("Resuming normal operations")
self.firstImage()
def getYellow(self, ct):
while True:
try:
self.img = self.cam.getImage()
print("Image oppened")
self.pixels = self.img.load()
break
except:
print("Error reading image file")
self.yellowRatio = 0
self.maxYellow = 0
self.yellowLoc = [0,0]
for x in range(ct[0]-125, ct[0]+125):
for y in range(ct[1]-125, ct[1]+125):
self.dot = self.pixels[x,y]
if self.dot[0]+self.dot[1]+self.dot[2] > 100:
self.yellowRatio = (self.dot[0]+self.dot[1])/(self.dot[2]+1)
if self.yellowRatio > self.maxYellow:
self.maxYellow = self.yellowRatio
self.yellowLoc = [x,y]
return self.yellowLoc
def _begin(self):
self.fwState = True
self.bwState = True
self.rtState = True
self.ltState = True
self.firstPress = True
self._viewVar.set("Sending mission data...")
for i in range(len(self.commands)):
self.port.write(self.commands[i])
print(self.commands[i])
self.port.write(self.pressCount.to_bytes(1, "big"))
print(self.pressCount.to_bytes(1, "big"))
self._viewVar.set("Commands sent")
self.commands.clear()
print()
if CAMERA:
self._imageWait()
def _undo(self):
if len(self.commands) > 0:
self._viewVar.set("Striking Last Entry")
self.commands.pop()
self.pressCount = 0
def _restart(self):
self._viewVar.set("")
self.commands.clear()
self.pressCount = 0
def _getImage(self):
print("displaying image")
image = Image.open("imageTest.jpg")
photo = ImageTk.PhotoImage(image)
top = Toplevel()
top.title("Collected Image")
label = Label(top, image = photo)
label.image = photo
label.grid()
def _imageWait(self):
imageLoader = threading.Thread(target = self._imageReceiver)
imageLoader.start()
def _imageReceiver(self):
while True:
command = self.port.read()
if command == b"\x97":
print("opening file")
file = open("imageTest.jpg", "wb")
time1 = time.time()
while True:
if self.port.inWaiting():
file.write(self.port.read())
time1 = time.time()
else:
time2 = time.time()
if (time2 - time1) > 1:
print("file transfer complete")
file.close()
self._getImage()
return
elif command == b"\x90":
return
def main():
Console().mainloop()
main()