-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMiniEduBotExplorer.py
More file actions
347 lines (282 loc) · 15.4 KB
/
MiniEduBotExplorer.py
File metadata and controls
347 lines (282 loc) · 15.4 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
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
# -*- coding: utf-8 -*-
import sys, random, math
from PyQt5.QtWidgets import (
QApplication, QWidget, QPushButton, QLabel, QVBoxLayout, QHBoxLayout,
QTextEdit, QProgressBar, QGraphicsView, QGraphicsScene, QGraphicsEllipseItem, QFrame,
QGraphicsRectItem, QGraphicsLineItem, QGroupBox, QCheckBox, QGridLayout
)
from PyQt5.QtCore import Qt, QTimer, QPointF
from PyQt5.QtGui import QBrush, QColor, QPen, QFont
class EduBotExplorer(QWidget):
def __init__(self):
super().__init__()
self.setWindowTitle("EduBot Explorer")
self.setGeometry(50, 50, 600, 500)
self.robot_x, self.robot_y = 70, 70
self.start_x, self.start_y = 70, 70
self.robot_trail = []
self.trail_lines = []
self.obstacles = []
self.autonomous_mode = False
self.target_x, self.target_y = None, None
self.target_item = None
self.target_selection_mode = False
self.autonomous_timer = QTimer()
self.setup_ui()
self.setup_connections()
self.setup_timer()
self.draw_map()
def setup_ui(self):
self.controls = {
"Forward": QPushButton("▲"),
"Backward": QPushButton("▼"),
"Left": QPushButton("◄"),
"Right": QPushButton("►"),
"Stop": QPushButton("Stop"),
"Clear": QPushButton("Clear"),
"Home": QPushButton("Home"),
"Auto": QPushButton("Auto")
}
button_style = "QPushButton { font-weight: bold; border-radius: 3px; padding: 1px; font-size: 9px; margin: 1px; } QPushButton:hover { background-color: rgba(255,255,255,0.2); }"
self.controls["Forward"].setStyleSheet(button_style + "background-color: #2E7D32; color: white;")
self.controls["Backward"].setStyleSheet(button_style + "background-color: #C62828; color: white;")
self.controls["Left"].setStyleSheet(button_style + "background-color: #1565C0; color: white;")
self.controls["Right"].setStyleSheet(button_style + "background-color: #1565C0; color: white;")
self.controls["Stop"].setStyleSheet(button_style + "background-color: #EF6C00; color: white;")
self.controls["Clear"].setStyleSheet(button_style + "background-color: #7B1FA2; color: white;")
self.controls["Home"].setStyleSheet(button_style + "background-color: #455A64; color: white;")
self.controls["Auto"].setStyleSheet(button_style + "background-color: #7B1FA2; color: white;")
for btn in self.controls.values():
btn.setFixedSize(35, 22)
movement_layout = QGridLayout()
movement_layout.addWidget(self.controls["Forward"], 0, 1)
movement_layout.addWidget(self.controls["Left"], 1, 0)
movement_layout.addWidget(self.controls["Stop"], 1, 1)
movement_layout.addWidget(self.controls["Right"], 1, 2)
movement_layout.addWidget(self.controls["Backward"], 2, 1)
movement_layout.setSpacing(2)
movement_layout.setContentsMargins(0, 0, 0, 0)
control_buttons_layout = QHBoxLayout()
control_buttons_layout.addWidget(self.controls["Clear"])
control_buttons_layout.addWidget(self.controls["Home"])
control_buttons_layout.addWidget(self.controls["Auto"])
control_buttons_layout.setSpacing(2)
control_buttons_layout.setContentsMargins(0, 0, 0, 0)
autonomous_group = QGroupBox("Auto")
autonomous_group.setMaximumHeight(65)
autonomous_group.setStyleSheet("QGroupBox { font-weight: bold; color: #37474F; font-size: 9px; border: 1px solid #BDBDBD; margin-top: 10px; }")
autonomous_layout = QVBoxLayout()
autonomous_layout.setSpacing(1)
self.autonomous_check = QCheckBox("Enable")
self.autonomous_check.setStyleSheet("font-weight: normal; color: #37474F; font-size: 8px;")
self.target_btn = QPushButton("Set Target")
self.target_btn.setStyleSheet("QPushButton { background-color: #AD1457; color: white; border-radius: 2px; font-size: 8px; padding: 0px; } QPushButton:hover { background-color: #880E4F; }")
self.target_btn.setFixedHeight(18)
self.target_label = QLabel("Target: Not set")
self.target_label.setStyleSheet("font-size: 8px; background-color: #FFF9C4; padding: 1px; border-radius: 1px;")
autonomous_layout.addWidget(self.autonomous_check)
autonomous_layout.addWidget(self.target_btn)
autonomous_layout.addWidget(self.target_label)
autonomous_group.setLayout(autonomous_layout)
self.sensor_label = QLabel("Dist: -- cm | Temp: -- °C")
self.sensor_label.setStyleSheet("font-weight: bold; font-size: 9px; background-color: #E1F5FE; padding: 2px; border-radius: 2px; border: 1px solid #B3E5FC;")
self.sensor_label.setMaximumHeight(18)
self.battery_bar = QProgressBar()
self.battery_bar.setValue(100)
self.battery_bar.setFormat("Battery: %p%")
self.battery_bar.setStyleSheet("QProgressBar { border: 1px solid #B0BEC5; border-radius: 2px; text-align: center; height: 14px; background-color: #ECEFF1; font-size: 8px; } QProgressBar::chunk { background-color: #4CAF50; border-radius: 1px; }")
self.battery_bar.setMaximumHeight(16)
self.scene = QGraphicsScene(0, 0, 280, 180)
self.map_view = QGraphicsView(self.scene)
self.map_view.setFixedHeight(140)
self.map_view.setFrameShape(QFrame.Box)
self.map_view.setStyleSheet("background-color: #F5F5F5; border: 1px solid #BDBDBD; border-radius: 2px;")
self.map_view.setSceneRect(0, 0, 280, 180)
self.map_view.mousePressEvent = self.map_clicked
self.robot_item = QGraphicsEllipseItem(0, 0, 12, 12)
self.robot_item.setBrush(QBrush(QColor("#FF5722")))
self.robot_item.setPen(QPen(Qt.black, 1))
self.scene.addItem(self.robot_item)
self.robot_item.setPos(self.robot_x, self.robot_y)
self.position_label = QLabel("Position: (70, 70)")
self.position_label.setStyleSheet("font-size: 9px; background-color: #FFF9C4; padding: 1px; border-radius: 1px; border: 1px solid #FFEB3B;")
self.position_label.setMaximumHeight(16)
self.log = QTextEdit()
self.log.setReadOnly(True)
self.log.setPlaceholderText("Log...")
self.log.setStyleSheet("background-color: #FAFAFA; border: 1px solid #E0E0E0; border-radius: 2px; font-size: 8px;")
self.log.setMaximumHeight(60)
main_layout = QVBoxLayout()
main_layout.addLayout(movement_layout)
main_layout.addLayout(control_buttons_layout)
info_layout = QHBoxLayout()
info_layout.addWidget(autonomous_group)
info_layout.addWidget(self.sensor_label)
info_layout.setSpacing(3)
main_layout.addLayout(info_layout)
main_layout.addWidget(self.position_label)
main_layout.addWidget(self.battery_bar)
map_label = QLabel("Robot Map")
map_label.setStyleSheet("font-size: 9px; font-weight: bold; color: #37474F;")
main_layout.addWidget(map_label)
main_layout.addWidget(self.map_view)
log_label = QLabel("Command Log")
log_label.setStyleSheet("font-size: 9px; font-weight: bold; color: #37474F;")
main_layout.addWidget(log_label)
main_layout.addWidget(self.log)
main_layout.setSpacing(3)
main_layout.setContentsMargins(5, 5, 5, 5)
self.setLayout(main_layout)
self.setStyleSheet("background-color: #F5F5F5; font-family: Arial;")
def draw_map(self):
for x in range(0, 280, 15):
line = QGraphicsLineItem(x, 0, x, 180)
line.setPen(QPen(QColor("#E0E0E0"), 0.5))
self.scene.addItem(line)
for y in range(0, 180, 15):
line = QGraphicsLineItem(0, y, 280, y)
line.setPen(QPen(QColor("#E0E0E0"), 0.5))
self.scene.addItem(line)
border = QGraphicsRectItem(0, 0, 280, 180)
border.setPen(QPen(Qt.black, 2))
self.scene.addItem(border)
obstacle_positions = [
(40, 40, 25, 25), (170, 60, 30, 15),
(120, 110, 20, 30), (220, 90, 25, 25)
]
for x, y, w, h in obstacle_positions:
obstacle = QGraphicsRectItem(x, y, w, h)
obstacle.setBrush(QBrush(QColor("#9E9E9E")))
obstacle.setPen(QPen(Qt.black, 1))
self.scene.addItem(obstacle)
self.obstacles.append(obstacle)
def setup_connections(self):
self.controls["Forward"].clicked.connect(lambda: self.move_robot(0, -8, "Forward"))
self.controls["Backward"].clicked.connect(lambda: self.move_robot(0, 8, "Backward"))
self.controls["Left"].clicked.connect(lambda: self.move_robot(-8, 0, "Left"))
self.controls["Right"].clicked.connect(lambda: self.move_robot(8, 0, "Right"))
self.controls["Stop"].clicked.connect(self.emergency_stop)
self.controls["Clear"].clicked.connect(self.clear_map)
self.controls["Home"].clicked.connect(self.return_to_start)
self.controls["Auto"].clicked.connect(self.auto_move_random)
self.autonomous_check.stateChanged.connect(self.toggle_autonomous_mode)
self.target_btn.clicked.connect(self.enable_target_selection)
self.autonomous_timer.timeout.connect(self.autonomous_move)
def map_clicked(self, event):
if self.target_selection_mode:
pos = self.map_view.mapToScene(event.pos())
self.set_target(pos.x(), pos.y())
event.accept()
def set_target(self, x, y):
if self.target_item:
self.scene.removeItem(self.target_item)
self.target_x, self.target_y = x, y
self.target_item = QGraphicsEllipseItem(x-8, y-8, 16, 16)
self.target_item.setBrush(QBrush(QColor("#FF0000")))
self.target_item.setPen(QPen(Qt.black, 2))
self.target_item.setZValue(10)
self.scene.addItem(self.target_item)
self.target_label.setText(f"Target: ({int(x)}, {int(y)})")
self.log.append(f"Target set at: ({int(x)}, {int(y)})")
self.target_selection_mode = False
self.target_btn.setStyleSheet("QPushButton { background-color: #AD1457; color: white; border-radius: 2px; font-size: 8px; padding: 0px; }")
if self.autonomous_mode:
self.autonomous_timer.start(100)
def enable_target_selection(self):
self.target_selection_mode = True
self.log.append("Click on the map to set target")
self.target_btn.setStyleSheet("QPushButton { background-color: #C2185B; color: white; border-radius: 2px; font-size: 8px; padding: 0px; font-weight: bold; }")
def toggle_autonomous_mode(self, state):
self.autonomous_mode = state == Qt.Checked
if self.autonomous_mode:
self.log.append("Autonomous mode enabled")
if self.target_x is not None:
self.autonomous_timer.start(100)
else:
self.log.append("Please set a target first")
self.autonomous_check.setChecked(False)
self.autonomous_mode = False
else:
self.log.append("Autonomous mode disabled")
self.autonomous_timer.stop()
def autonomous_move(self):
if self.target_x is None or not self.autonomous_mode:
self.autonomous_timer.stop()
return
dx = self.target_x - (self.robot_x + 6)
dy = self.target_y - (self.robot_y + 6)
distance = math.sqrt(dx*dx + dy*dy)
if distance < 8:
self.log.append("Target reached!")
self.autonomous_timer.stop()
return
dx = dx / distance * 4
dy = dy / distance * 4
self.move_robot(dx, dy, "Autonomous move")
def auto_move_random(self):
random_x = random.randint(15, 265)
random_y = random.randint(15, 165)
self.set_target(random_x, random_y)
if not self.autonomous_mode:
self.autonomous_check.setChecked(True)
self.autonomous_mode = True
self.autonomous_timer.start(100)
def emergency_stop(self):
self.autonomous_mode = False
self.autonomous_check.setChecked(False)
self.autonomous_timer.stop()
self.send_command("Emergency Stop")
def clear_map(self):
for line in self.trail_lines:
self.scene.removeItem(line)
self.trail_lines.clear()
self.robot_trail.clear()
if self.target_item:
self.scene.removeItem(self.target_item)
self.target_item = None
self.target_x, self.target_y = None, None
self.target_label.setText("Target: Not set")
self.log.append("Map cleared")
def return_to_start(self):
self.robot_x, self.robot_y = self.start_x, self.start_y
self.robot_item.setPos(self.robot_x, self.robot_y)
self.position_label.setText(f"Position: ({self.robot_x}, {self.robot_y})")
self.log.append("Returned to start position")
def setup_timer(self):
self.timer = QTimer()
self.timer.timeout.connect(self.update_sensors)
self.timer.start(1000)
def move_robot(self, dx, dy, command):
self.robot_trail.append((self.robot_x, self.robot_y))
new_x = max(8, min(self.robot_x + dx, 268))
new_y = max(8, min(self.robot_y + dy, 168))
self.robot_x = new_x
self.robot_y = new_y
self.robot_item.setPos(self.robot_x, self.robot_y)
if len(self.robot_trail) > 1:
prev_x, prev_y = self.robot_trail[-1]
trail_line = QGraphicsLineItem(prev_x + 6, prev_y + 6, self.robot_x + 6, self.robot_y + 6)
trail_line.setPen(QPen(QColor("#2196F3"), 1.5))
self.scene.addItem(trail_line)
self.trail_lines.append(trail_line)
self.position_label.setText(f"Position: ({int(self.robot_x)}, {int(self.robot_y)})")
self.send_command(command)
def send_command(self, command):
self.log.append(f"Command: {command}")
def update_sensors(self):
distance = random.randint(10, 100)
temperature = random.randint(20, 35)
battery = max(0, self.battery_bar.value() - random.randint(0, 2))
self.sensor_label.setText(f"Dist: {distance} cm | Temp: {temperature} °C")
self.battery_bar.setValue(battery)
if battery < 20:
self.battery_bar.setStyleSheet("QProgressBar { border: 1px solid #B0BEC5; border-radius: 2px; text-align: center; height: 14px; background-color: #ECEFF1; font-size: 8px; } QProgressBar::chunk { background-color: #F44336; border-radius: 1px; }")
elif battery < 50:
self.battery_bar.setStyleSheet("QProgressBar { border: 1px solid #B0BEC5; border-radius: 2px; text-align: center; height: 14px; background-color: #ECEFF1; font-size: 8px; } QProgressBar::chunk { background-color: #FFC107; border-radius: 1px; }")
else:
self.battery_bar.setStyleSheet("QProgressBar { border: 1px solid #B0BEC5; border-radius: 2px; text-align: center; height: 14px; background-color: #ECEFF1; font-size: 8px; } QProgressBar::chunk { background-color: #4CAF50; border-radius: 1px; }")
if __name__ == "__main__":
app = QApplication(sys.argv)
window = EduBotExplorer()
window.show()
sys.exit(app.exec_())