-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuser_interface.py
310 lines (160 loc) · 7.54 KB
/
user_interface.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
# importing the required libraries
from tkinter import Button
from PyQt5.QtWidgets import *
from PyQt5 import QtCore
from PyQt5.QtGui import *
from dc_motors import forward, backward, turn_left, turn_right, stop
import calculate
import play_sound
import time
import os
import sys
class Window(QMainWindow):
def __init__(self):
super().__init__()
self.engine_flag = 0
self.setStyleSheet("background-color: white;")
self.setWindowTitle("K4.5")
self.setGeometry(100, 100, 1000, 550)
self.UiComponents()
self.update()
self.show()
def UiComponents(self):
# Title
title = QLabel("K4.5 - The Rover Pet", self)
title.setFont(QFont('Arial', 30))
# Mode
mode = QLabel("Mode", self)
mode.setFont(QFont('Arial', 20))
# Command box
self.command_box = QLineEdit(self)
self.command_box.setPlaceholderText("Enter command")
# Label
self.label = QLabel(self)
status = QPixmap("interface-images/engine_stop.jpg")
self.label.setPixmap(status)
self.label.setGeometry(300,150,50,50)
# Buttons
button_start_stop = QPushButton(self)
button_up = QPushButton(self)
button_down = QPushButton(self)
button_left = QPushButton(self)
button_right = QPushButton(self)
button_stop = QPushButton(self)
command_button = QPushButton(self)
safe_keeping_button = QPushButton("Safe - Keeping", self)
safe_guard_button = QPushButton("Safe - Guard", self)
fall_detection_button = QPushButton("Fall - Detection", self)
spy_mode_button = QPushButton("Spy Mode", self)
# Geometry
button_start_stop.setGeometry(50,150,100,100)
button_up.setGeometry(150, 150, 100, 100)
button_down.setGeometry(150, 350, 100, 100)
button_left.setGeometry(50, 250, 100, 100)
button_right.setGeometry(250, 250, 100, 100)
button_stop.setGeometry(150,250,100,100)
command_button.setGeometry(900,150,50,50)
self.command_box.setGeometry(450,150,400,50)
title.setGeometry(300,0,500,120)
mode.setGeometry(615,200,100,100)
safe_keeping_button.setGeometry(450,300,180,50)
safe_guard_button.setGeometry(670,300,180,50)
fall_detection_button.setGeometry(450,400,180,50)
spy_mode_button.setGeometry(670,400,180,50)
# Connect to function
button_start_stop.clicked.connect(self.start_stop_engine)
button_up.clicked.connect(self.robot_move_forward)
button_down.clicked.connect(self.robot_move_backward)
button_left.clicked.connect(self.robot_turn_left)
button_right.clicked.connect(self.robot_turn_right)
button_stop.clicked.connect(self.robot_stop)
command_button.clicked.connect(self.retrieve_command)
safe_keeping_button.clicked.connect(self.robot_safe_keeping)
safe_guard_button.clicked.connect(self.robot_safe_guard)
fall_detection_button.clicked.connect(self.robot_fall_detect)
spy_mode_button.clicked.connect(self.robot_spy_mode)
# Button image
button_start_stop.setStyleSheet("background-image : url(interface-images/start_stop_button.jpg); border-image: url(interface-images/start_stop_button.jpg)")
button_up.setStyleSheet("background-image : url(interface-images/up-arrow.png); border-image: url(interface-images/up-arrow.png)")
button_down.setStyleSheet("background-image : url(interface-images/down-arrow.png); border-image: url(interface-images/down-arrow.png)")
button_left.setStyleSheet("background-image : url(interface-images/left-arrow.png); border-image: url(interface-images/left-arrow.png)")
button_right.setStyleSheet("background-image : url(interface-images/right-arrow.png); border-image: url(interface-images/right-arrow.png)")
button_stop.setStyleSheet("background-image : url(interface-images/stop_button.jpg); border-image: url(interface-images/stop_button.jpg)")
command_button.setStyleSheet("background-image : url(interface-images/upload.png); border-image: url(interface-images/upload.png)")
def start_stop_engine(self):
if self.engine_flag == 0:
status = QPixmap("interface-images/engine_start.png")
self.engine_flag += 1
self.label.setPixmap(status)
play_sound.play_sound_effect(sound=play_sound.start_engine)
elif self.engine_flag == 1:
self.engine_flag -= 1
status = QPixmap("interface-images/engine_stop.jpg")
self.label.setPixmap(status)
def robot_stop(self):
if self.engine_flag == 1:
print("Stopped moving")
stop()
def robot_move_forward(self):
if self.engine_flag == 1:
print("Moving forward")
forward()
def robot_move_backward(self):
if self.engine_flag == 1:
print("Moving backward")
backward()
def robot_turn_left(self):
if self.engine_flag == 1:
print("Turning left")
turn_left()
def robot_turn_right(self):
if self.engine_flag == 1:
print("Turning right")
turn_right()
def robot_safe_keeping(self):
if self.engine_flag == 1:
os.system('python3 ~/RPI-Project-Rover/interface-safe_keeping.py')
def robot_safe_guard(self):
if self.engine_flag == 1:
os.system('python3 ~/RPI-Project-Rover/interface-safe_guard.py')
def robot_fall_detect(self):
if self.engine_flag == 1:
os.system('python3 ~/RPI-Project-Rover/interface-fall_detection.py')
def robot_spy_mode(self):
if self.engine_flag == 1:
os.system('python3 ~/RPI-Project-Rover/interface-spy_mode.py')
def retrieve_command(self):
if self.engine_flag == 1:
command = self.command_box.text()
self.command_box.setText("")
if "+" in command:
calculate.addition(command)
if "-" in command:
calculate.subtraction(command)
if "*" in command:
calculate.multiplication(command)
if "/" in command:
calculate.division(command)
if any(word in command.lower() for word in ["forward"]):
duration = [int(s) for s in command.split() if s.isdigit()][-1]
forward()
time.sleep(duration)
stop()
if any(word in command.lower() for word in ["backward"]):
duration = [int(s) for s in command.split() if s.isdigit()][-1]
backward()
time.sleep(duration)
stop()
if any(word in command.lower() for word in ["left"]):
duration = [int(s) for s in command.split() if s.isdigit()][-1]
turn_left()
time.sleep(duration)
stop()
if any(word in command.lower() for word in ["right"]):
duration = [int(s) for s in command.split() if s.isdigit()][-1]
turn_right()
time.sleep(duration)
stop()
App = QApplication(sys.argv)
window = Window()
sys.exit(App.exec())