-
Notifications
You must be signed in to change notification settings - Fork 0
/
init.py
329 lines (256 loc) · 12.5 KB
/
init.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
import sys
from PyQt5.QtWidgets import QApplication, QMainWindow, QWidget, QPushButton, QLineEdit
from PyQt5.uic import loadUi
from PyQt5.QtCore import pyqtSignal, QObject, QEventLoop, QTimer, Qt
import serial
import serial.tools.list_ports
import time
class Communicate(QObject):
buttonClicked = pyqtSignal()
class MainWindow(QMainWindow): # The main window class of the application that manages the process of graphene coating
def __init__(self, ui_file, parent=None):
super().__init__(parent)
self.setWindowFlags(Qt.WindowMinimizeButtonHint | Qt.CustomizeWindowHint)
self.ui_file = ui_file
self.communicate = Communicate()
self.setup_ui()
self.setFixedSize(800, 600)
self.show()
self.action = 0
def setup_ui(self):
self.load_ui()
self.add_button_handler()
def load_ui(self):
loadUi(self.ui_file, self)
def add_button_handler(self):
button = self.findChild(QPushButton, "pushButton_2")
button_2 = self.findChild(QPushButton, "pushButton")
button_3 = self.findChild(QPushButton, "pushButton_5")
button_4 = self.findChild(QPushButton, "pushButton_4")
button_5 = self.findChild(QPushButton, "pushButton_3")
if button_2:
button_2.clicked.connect(self.handle_button_click_2)
if button:
button.clicked.connect(self.handle_button_click)
if button_3:
button_3.clicked.connect(self.handle_button_click_3)
if button_4:
button_4.clicked.connect(self.handle_button_click_4)
if button_5:
button_5.clicked.connect(self.handle_button_click_5)
def handle_button_click(self): # Handler of starting graphene coating proces
self.communicate.buttonClicked.emit()
self.action = 1
def handle_button_click_2(self): # Handler of closing app button
self.communicate.buttonClicked.emit()
sys.exit(0)
def handle_button_click_3(self): # Handler of closing app button
self.communicate.buttonClicked.emit()
self.action = 2
def handle_button_click_4(self): # Handler of closing app button
self.communicate.buttonClicked.emit()
self.action = 3
def handle_button_click_5(self): # Handler of closing app button
self.communicate.buttonClicked.emit()
self.action = 4
class PromptWindow(QWidget): # The prompt window class with button to continue the process
def __init__(self, ui_file, parent=None):
super().__init__(parent)
self.setWindowFlags(Qt.WindowMinimizeButtonHint | Qt.CustomizeWindowHint)
self.ui_file = ui_file
self.communicate = Communicate()
self.setup_ui()
self.setFixedSize(410, 120)
self.show()
def setup_ui(self):
self.load_ui()
self.add_button_handler()
def load_ui(self):
loadUi(self.ui_file, self)
def add_button_handler(self):
button = self.findChild(QPushButton, "pushButton")
if button:
button.clicked.connect(self.handle_button_click)
def handle_button_click(self):
self.communicate.buttonClicked.emit()
class ProcessWindow(QWidget): # Class of processing window
def __init__(self, ui_file, parent=None):
super().__init__(parent)
self.setWindowFlags(Qt.WindowMinimizeButtonHint | Qt.CustomizeWindowHint)
self.ui_file = ui_file
self.window = QWidget()
loadUi(self.ui_file, self)
self.setFixedSize(410, 120)
self.show()
def signal_check(): # Check every available port to find connection with Arduino UNO
available_ports = list(serial.tools.list_ports.comports())
for port, desc, hwid in available_ports:
print(desc)
if "CH340" in desc:
return port # If found - return the name of port
return 0 # If not - return 0
def read_data(ser): # Function that reads sent data from arduino (from Serial.println())
buffer = b'' # Init of empty buffer
timeout = time.time() + 1 # Max wait time for data (2 seconds)
while time.time() < timeout:
if ser.in_waiting > 0:
buffer += ser.read(ser.in_waiting) # Read of available data
if b'\n' in buffer:
line, buffer = buffer.split(b'\n', 1)
return line.decode('utf-8').strip()
return None # If the data haven't been read in time
def detection(): # Function for towrope detection
try:
ser = serial.Serial(signal_check(), 9600)
start_time = time.time()
duration = 6
counter = 0
func_num = "s:"
while time.time() - start_time < duration:
ser.write(func_num.encode())
time.sleep(0.1)
val = read_data(ser) # Use a function for non-blocking data flow
if val is not None:
val = int(val)
if val > 340: # If the value is greater than empty machine - start the process
counter = counter + 1
else:
counter = 0
if counter == 5:
return True
return False
except KeyboardInterrupt:
ser.close()
def realisation(func_str, event_loop): # Function that realises each function on arduino by sending the function string and then waiting for ending info
try:
ser = serial.Serial(signal_check(), 9600)
start_time = time.time()
duration = 9999
close_flag = 0
time.sleep(3)
ser.write(func_str.encode())
while time.time() - start_time < duration:
val = read_data(ser) # Use a function for non-blocking data flow
print(f"Arduino: {val}")
ending_info = ["Koniec"]
if val in ending_info:
break
except KeyboardInterrupt:
pass
finally:
if 'ser' in locals() and ser.is_open:
ser.close()
event_loop.quit()
def wait_for_signal(win_sel): # Function waiting for button click to continue the process
event_loop = QEventLoop()
win_sel.communicate.buttonClicked.connect(event_loop.quit)
event_loop.exec_()
def wait_for_arduino(func_str): # Function waiting for signal from arduino
event_loop = QEventLoop()
QTimer.singleShot(100, lambda: realisation(func_str, event_loop))
event_loop.exec_()
if __name__ == "__main__":
app = QApplication(sys.argv)
win2 = None
language = 'PL'
if signal_check() == 0: # If computer doesn't connect with arduino - display window with error and close
win2 = PromptWindow('window_signal_check.ui')
win2.communicate.buttonClicked.connect(app.quit)
wait_for_signal(win2)
sys.exit()
while True:
try:
win = MainWindow(f'windows_{language}/mainwindow.ui') # Initialisation of main window
wait_for_signal(win)
win.setEnabled(False)
le = win.findChild(QLineEdit, "lineEdit") # Reading value typed in line windows in main window
le2 = win.findChild(QLineEdit, "lineEdit_2")
force = round(float(le.text()), 1) # Fitting of read force and cycles values
cycles = int(le2.text())
if not (0.5 <= force <= 5.0) or not (1 <= cycles <= 12): # If variables are different from specified ranges in main window - raise a value error
win2 = PromptWindow(f'windows_{language}/window_error.ui')
wait_for_signal(win2)
win2.close()
raise ValueError("Nieprawidłowe wartości force lub cycles")
if win.action == 1:
print("force:", force)
print("cycles:", cycles)
win2 = ProcessWindow(f'windows_{language}/window_info_1.ui') # Go upwards to "base" point with limit switch
wait_for_arduino("u:")
win2.close()
'''if not detection(): # If object is not detected on machine table - raise a value error
win2 = PromptWindow(f'windows_{language}/window_detection.ui')
wait_for_signal(win2)
win2.close()
raise ValueError("Brak wykrycia ciągadła w maszynie")
#win2.communicate.buttonClicked.connect(app.quit)'''
win2 = ProcessWindow(f'windows_{language}/window_info_2.ui') # Go downwards to detect object on the table
wait_for_arduino("d:")
win2.close()
win2 = ProcessWindow(f'windows_{language}/window_info_4.ui') # Go upwards with precise amount of steps to prepare for placing graphene powder
wait_for_arduino("r14000:")
win2.close()
win2 = PromptWindow(f'windows_{language}/window_refill.ui') # Inform about graphene refill
wait_for_signal(win2)
win2.close()
one_rotation = 3200 * 1.6 # Steps required to make one rotation (including gear ratio)
rest = one_rotation % cycles
for i in range(cycles): # Including rest from division to divide cycles in most equal way
if rest > 0:
steps = int(one_rotation / cycles) + 1
rest -= 1
else:
steps = int(one_rotation / cycles)
print(steps)
win2 = ProcessWindow(f'windows_{language}/window_info_2.ui') # Go downwards to detect object on the table
wait_for_arduino("d:")
win2.close()
win2 = ProcessWindow(f'windows_{language}/window_info_3.ui') # Perform precise downward movement to get the closest sensor read to given force value
wait_for_arduino(f"m{force}:")
win2.close()
win2 = PromptWindow(f'windows_{language}/window_electrode_run.ui') # Inform about running up the electrode
wait_for_signal(win2)
win2.close()
win2 = ProcessWindow(f'windows_{language}/window_info_5.ui') # Go upwards with precise amount of steps to make enough space for table spin
wait_for_arduino("r14000:")
win2.close()
win2 = ProcessWindow(f'windows_{language}/window_info_6.ui') # Spin the table with precise amount of steps
wait_for_arduino(f"p{steps}:")
win2.close()
win2 = ProcessWindow(f'windows_{language}/window_info_2.ui') # Go downwards to detect object on the table
wait_for_arduino("d:")
win2.close()
win2 = ProcessWindow(f'windows_{language}/window_info_3.ui') # Perform precise downward movement to get the closest sensor read to given force value
wait_for_arduino(f"m{force}:")
win2.close()
win2 = PromptWindow(f'windows_{language}/window_electrode_run.ui') # Inform about running up the electrode
wait_for_signal(win2)
win2.close()
win2 = ProcessWindow(f'windows_{language}/window_info_1.ui') # Go upwards to "base" point with limit switch
wait_for_arduino("u:")
win2.close()
win2 = ProcessWindow(f'windows_{language}/window_info_6.ui') # Make a full rotation in opposite direction to compensate cycles spins
wait_for_arduino(f"p{-1 * one_rotation}:")
win2.close()
if win.action == 2:
win2 = ProcessWindow(f'windows_{language}/window_info_1.ui') # Go upwards to "base" point with limit switch
wait_for_arduino("u:")
win2.close()
if win.action == 3:
print("force:", force)
win2 = ProcessWindow(f'windows_{language}/window_info_2.ui') # Go downwards to detect object on the table
wait_for_arduino("d:")
win2.close()
win2 = ProcessWindow(f'windows_{language}/window_info_3.ui') # Perform precise downward movement to get the closest sensor read to given force value
wait_for_arduino(f"m{force}:")
win2.close()
if win.action == 4:
if language == 'PL':
language = 'EN'
elif language == 'EN':
language = 'PL'
win.setEnabled(True)
except ValueError as e:
print("Błąd:", e)
continue # Start the while loop from beginning when the error is detected
sys.exit(app.exec())