-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
executable file
·276 lines (256 loc) · 8.47 KB
/
main.py
File metadata and controls
executable file
·276 lines (256 loc) · 8.47 KB
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
from tkinter import *
from tkinter.ttk import *
import os
import smbus
import RPi.GPIO as GPIO
import time
from threading import Thread
from recognize import *
GPIO.setmode(GPIO.BCM)
GPIO.setup(16,GPIO.OUT) # buzzer
GPIO.setup(22,GPIO.OUT) # relay switch
GPIO.setup(18,GPIO.IN,pull_up_down=GPIO.PUD_UP) # IR sensor signal
GPIO.output(16,False)
GPIO.output(22,False)
bus=smbus.SMBus(1)
address=0x04
cmd={'b':0,'w':1,'s':2,'d':3,'a':4,'x':-1}
win=Tk()
win.title("Car GUI")
win.geometry("645x650")
Style().configure("CB.TButton",font=("Sans","12","bold"),foreground="blue")
Style().configure("EB.TButton",font=("Sans","12","bold"),foreground="red")
btn_forward=Button(win,text="^",width=2,style="CB.TButton")
btn_backward=Button(win,text="v",width=2,style="CB.TButton")
btn_right=Button(win,text=">",width=2,style="CB.TButton")
btn_left=Button(win,text="<",width=2,style="CB.TButton")
btn_end=Button(win,text="X",width=2,style="EB.TButton")
btn_forward.place(x=45,y=550)
btn_backward.place(x=45,y=600)
btn_left.place(x=20,y=575)
btn_right.place(x=70,y=575)
btn_end.place(x=45,y=575)
class route:
def __init__(self):
self.window=win
self.button_forward=btn_forward
self.button_backward=btn_backward
self.button_right=btn_right
self.button_left=btn_left
self.button_end=btn_end
self.cmdValue='b'
self.delay=1
def forward(self):
self.disableButtons()
self.cmdValue='w'
bus.write_byte(address,cmd[self.cmdValue])
self.file.write(self.cmdValue+',')
time.sleep(self.delay)
self.enableButtons()
def backward(self):
self.disableButtons()
self.cmdValue='s'
bus.write_byte(address,cmd[self.cmdValue])
self.file.write(self.cmdValue+',')
time.sleep(self.delay)
self.enableButtons()
def right(self):
self.disableButtons()
self.cmdValue='d'
bus.write_byte(address,cmd[self.cmdValue])
self.file.write(self.cmdValue+',')
time.sleep(self.delay)
self.enableButtons()
def left(self):
self.disableButtons()
self.cmdValue='a'
bus.write_byte(address,cmd[self.cmdValue])
self.file.write(self.cmdValue+',')
time.sleep(self.delay)
self.enableButtons()
def end(self):
enable_GO()
enable_STOP()
self.disableButtons()
self.cmdValue='x'
bus.write_byte(address,cmd['b'])
self.file.write(self.cmdValue)
self.button_forward.config(command=None)
self.button_backward.config(command=None)
self.button_right.config(command=None)
self.button_left.config(command=None)
self.button_end.config(command=None)
self.file.close()
def disableButtons(self):
self.button_forward.state(["disabled"])
self.button_backward.state(["disabled"])
self.button_right.state(["disabled"])
self.button_left.state(["disabled"])
self.button_end.state(["disabled"])
def enableButtons(self):
self.button_forward.state(["!disabled"])
self.button_backward.state(["!disabled"])
self.button_right.state(["!disabled"])
self.button_left.state(["!disabled"])
self.button_end.state(["!disabled"])
def start(self):
self.file=open("cmd.txt",'w')
self.button_forward.config(command=self.forward)
self.button_backward.config(command=self.backward)
self.button_right.config(command=self.right)
self.button_left.config(command=self.left)
self.button_end.config(command=self.end)
self.enableButtons()
Route=route()
def keepMoving():
Status.deactivateKeepMoving()
Status.KEEPMOVING=True
Status.changeMessage("","black")
class status:
def __init__(self):
self.win=win
self.child_process=init()
self.CAMERA=False
self.MONITOR=False
self.MOVE=0
self.STOP=False
self.KEEPMOVING=False
self.image=PhotoImage(file="./data/cover.png")
self.label=Label(image=self.image)
self.message=Label(self.win,text=" READY",foreground="black",font=("Helvetica",36))
self.btn_keepmoving=Button(self.win,text="Keep Moving",width=12,style="EB.TButton")
def interruptHandler_IR(self):
self.CAMERA=True
def changeImage(self,path):
self.image=PhotoImage(file=path)
self.label.configure(image=self.image)
def changeMessage(self,text,color):
self.message.configure(text=text)
self.message.configure(foreground=color)
def activateKeepMoving(self):
self.btn_keepmoving.configure(command=keepMoving)
self.btn_keepmoving.place(x=360,y=600)
def deactivateKeepMoving(self):
self.btn_keepmoving.configure(command=None)
Status=status()
class navigationThread(Thread):
def __init__(self):
Thread.__init__(self)
def run(self):
try:
f=open("cmd.txt",'r')
except:
Status.changeMessage("No Route!","red")
return
GPIO.add_event_detect(18,GPIO.RISING,callback=IR_callback)
line=f.readline()
currentline=line.split(",")
while True:
for key in currentline:
while(Status.CAMERA):
a=1 #do nothing
if Status.STOP:
GPIO.remove_event_detect(18)
f.close()
return
bus.write_byte(address,cmd[key])
Status.MOVE+=1
time.sleep(1)
class monitorThread(Thread):
def __init__(self):
Thread.__init__(self)
def run(self):
Stream=stream()
while True:
if Stream.poll()==0:
Status.MONITOR=False
return
def IR_callback(channel):
if Status.MONITOR:
return
if(Status.MOVE>2):
Status.KEEPMOVING=False
Status.interruptHandler_IR()
GPIO.output(22,True)
time.sleep(1)
shot()
GPIO.output(22,False)
if recognize(Status.child_process):
time.sleep(1.5)
Status.changeImage("./predictions.png")
Status.changeMessage("WARNING","red")
Status.activateKeepMoving()
while True:
if(Status.STOP or Status.KEEPMOVING):
break
GPIO.output(16,True)
GPIO.output(22,True)
time.sleep(0.1)
GPIO.output(22,False)
time.sleep(0.1)
else:
time.sleep(1.5)
Status.changeImage("./predictions.png")
Status.changeMessage(" OK","green")
GPIO.output(16,False)
GPIO.output(22,False)
Status.MOVE=0
Status.CAMERA=False
def setupRoute():
Status.STOP=True
disable_STOP()
disable_GO()
Route.start()
def startNavigation():
Status.STOP=False
Status.CAMERA=False
Status.MOVE=0
disable_SetupandGO()
Route.disableButtons()
Status.changeMessage("","black")
OBJ=navigationThread()
OBJ.start()
Route.enableButtons()
enable_SetupandGO()
def stopNavigation():
Status.STOP=True
enable_SetupandGO()
Status.changeMessage(" READY","black")
Status.deactivateKeepMoving()
Route.enableButtons()
GPIO.output(16,False)
GPIO.output(22,False)
def monitor():
if Status.CAMERA:
return
Status.MONITOR=True
OBJ=monitorThread()
OBJ.start()
Style().configure("Bold.TButton", font = ('Sans','10','bold'),foreground='black')
btn_setupRoute=Button(win,text="Set up Route",command=setupRoute,style="Bold.TButton")
btn_setupRoute.place(x=15,y=510)
btn_GO=Button(win,text="GO !",command=startNavigation,style="Bold.TButton")
btn_GO.place(x=130,y=560)
btn_stop=Button(win,text="STOP",command=stopNavigation,style="Bold.TButton")
btn_stop.place(x=130,y=600)
Style().configure("VB.TButton",font=('Sans','12','bold'),foreground="green",background="black")
btn_monitor=Button(win,text="Monitor",command=monitor,style="VB.TButton")
btn_monitor.place(x=120,y=510)
def disable_SetupandGO():
btn_setupRoute.state(["disabled"])
btn_GO.state(["disabled"])
def enable_SetupandGO():
btn_setupRoute.state(["!disabled"])
btn_GO.state(["!disabled"])
def disable_GO():
btn_GO.state(["disabled"])
def enable_GO():
btn_GO.state(["!disabled"])
def disable_STOP():
btn_stop.state(["disabled"])
def enable_STOP():
btn_stop.state(["!disabled"])
if __name__=="__main__":
Status.label.place(x=0,y=0)
Status.message.place(x=300,y=520)
win.mainloop()