-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.py
348 lines (280 loc) · 12.7 KB
/
main.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
import logs
import version
import json
import sys
from pathlib import Path
from PyQt5 import QtCore, QtGui, QtWidgets, uic, QtSvg
from PyQt5.QtCore import *
#from PyQt5.QtCore import Qt, QTimer, QThread
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
from PyQt5.QtCore import Qt
import numpy as np
import queue
import utils
import motion
import pkg_resources.py2_warn # fix pyinstaller error
import main_ui
import gcode_generator
import monitor
import manual_control
import settings_ui
import about_ui
import logging
LOGGER = logging.getLogger(__name__)
LOGGER.info('start')
COLOR_GREEN = '#33cc33'
COLOR_YELLOW = '#E5B500'
COLOR_RED = '#A0150E'
SETTINGS_FILE = 'settings.json'
if sys.platform == 'win32':
QtWidgets.QApplication.setAttribute(QtCore.Qt.AA_EnableHighDpiScaling, True)
class VLine(QFrame):
def __init__(self):
super(VLine, self).__init__()
self.setFrameShape(self.VLine|self.Sunken)
class MyWindowClass(QtWidgets.QMainWindow, main_ui.Ui_MainWindow):
def __init__(self, parent=None):
QtWidgets.QMainWindow.__init__(self, parent)
self.current_motion_profile = None
self.current_motion_filename = None
self.hw_connected = False
self.source_filename = ""
# load settings from json
self.config = {}
self.config = utils.json_boot_routine(SETTINGS_FILE)
self.setupUi(self)
self.original_window_name = self.windowTitle()
self.setWindowTitle(self.original_window_name + " (" + version.__version__ + ")")
# Status Bar
self.s_status = QtWidgets.QLabel("--")
self.s_status.setToolTip("Motor status")
self.s_position = QtWidgets.QLabel("--")
self.s_position.setToolTip("Active motor position")
self.s_controller_fw = QtWidgets.QLabel("--")
self.s_controller_fw.setToolTip("Controller firmware")
self.s_script = QtWidgets.QLabel("--")
self.s_script.setToolTip("Loaded script name")
self.statusBar.addPermanentWidget(self.s_status)
self.statusBar.addPermanentWidget(VLine())
self.statusBar.addPermanentWidget(self.s_position)
self.statusBar.addPermanentWidget(VLine())
self.statusBar.addPermanentWidget(self.s_controller_fw)
# Menu buttons
self.action_about.triggered.connect(self.action_about_clicked)
self.action_settings.triggered.connect(self.action_settings_clicked)
self.action_manual_control.triggered.connect(self.action_manual_control_clicked)
self.action_monitor.triggered.connect(self.action_monitor_clicked)
self.action_edit_motion_script.triggered.connect(self.action_edit_motion_script_clicked)
self.action_save_script.triggered.connect(self.action_save_script_clicked)
self.action_save_as_script.triggered.connect(self.action_save_as_script_clicked)
self.action_load_script.triggered.connect(self.action_load_script_clicked)
# all other buttons
self.button_connect.clicked.connect(self.btn_connect_clicked)
self.button_disconnect.clicked.connect(self.btn_disconnect_clicked)
self.button_com_refresh.clicked.connect(self.button_com_refresh_clicked)
self.push_run.clicked.connect(self.push_run_clicked)
# more UI tweaking
self.button_connect.setStyleSheet("background-color: " + COLOR_GREEN)
children = self.findChildren(QtWidgets.QPushButton)
for c in children:
c.setEnabled(False)
self.button_connect.setEnabled(True)
self.button_com_refresh.setEnabled(True)
# prepare serial communications
self.hw = motion.SerialComm()
self.thread_serial = QtCore.QThread()
self.hw.strStatus.connect(self.serStatus)
self.hw.serReceive.connect(self.controller_read)
self.hw.current_line_feedback.connect(self.current_line_feedback)
self.hw.strVersion.connect(self.serVersion)
self.hw.strError.connect(self.strError)
self.hw.serFeedback.connect(self.serFeedback)
self.hw.moveToThread(self.thread_serial)
self.thread_serial.started.connect(self.hw.serial_worker)
self.thread_serial.start()
self.button_com_refresh_clicked()
# initialize gcode generator and manual control windows
self.gcode_generator = gcode_generator.CodeGenerator()
self.manual_control = manual_control.ManualControl(self.hw)
self.monitor = monitor.CommunicationsMonitor()
self.hw.log_tx.connect(self.monitor.add_log_cmd)
self.hw.log_rx.connect(self.monitor.add_log_response)
def push_run_clicked(self):
line_count = 0
for line in self.current_motion_profile["text"]["text_gcode"].splitlines():
self.hw.send(line+"\n")
line_count += 1
self.progress_program.setMaximum(line_count)
self.progress_program.setValue(line_count)
self.update_enabled_elements()
def btn_connect_clicked(self):
self.config["port"] = self.combo_comports.currentText()
self.hw.connect(self.config["port"], self.config["com_baud"], self.config["com_timeout"])
def btn_disconnect_clicked(self):
self.hw.disconnect()
def controller_read(self, data):
pass
def update_enabled_elements(self):
if not self.hw.commands.empty():
self.push_run.setEnabled(False)
if self.hw_connected:
self.combo_comports.setEnabled(False)
self.button_connect.setEnabled(False)
self.button_com_refresh.setEnabled(False)
self.button_disconnect.setEnabled(True)
self.button_connect.setStyleSheet("")
self.manual_control.enable(True)
if self.current_motion_profile:
if self.hw.commands.empty():
self.push_run.setEnabled(True)
else:
self.combo_comports.setEnabled(True)
self.button_connect.setEnabled(True)
self.button_com_refresh.setEnabled(True)
self.button_disconnect.setEnabled(False)
self.manual_control.enable(False)
self.s_position.setText("0°")
self.button_connect.setStyleSheet("background-color: " + COLOR_GREEN)
self.push_run.setEnabled(False)
def serStatus(self, text):
self.s_status.setText(text)
self.combo_comports.setEnabled(False)
self.button_connect.setEnabled(False)
self.button_disconnect.setEnabled(False)
if text == "Connected":
self.hw_connected = True
self.hw.action_recipe.put("status1")
self.hw.action_recipe.put("version")
self.hw.action_recipe.put("get_param_list")
if text == "Disconnected":
self.hw_connected = False
self.update_enabled_elements()
def button_com_refresh_clicked(self):
self.combo_comports.clear()
com_ports = sorted(self.hw.get_compot_list())
for port, desc in com_ports:
self.combo_comports.addItem(port.strip())
self.combo_comports.setCurrentIndex(self.combo_comports.findText(self.config["port"]))
def strError(self, text):
LOGGER.error(text)
msg = QMessageBox()
msg.setIcon(QMessageBox.Critical)
msg.setText("Error")
msg.setInformativeText(text)
msg.setWindowTitle("Error")
msg.exec_()
def serVersion(self, text):
self.s_controller_fw.setText(text)
def serFeedback(self, text):
status = {}
if len(text) < 5:
return None
if text[0] != "<":
return None
if not ((text[0] == "<") and (text[-1] == ">")):
raise Exception("Bad format 1")
text = text[1:-1]
feedback_split = text.split("|")
status["STATUS"] = feedback_split[0]
if feedback_split[1].find("MPos") < 0:
raise Exception("Bad format 2")
positions = feedback_split[1].split(":")
if len(positions) != 2:
raise Exception("Bad format 3")
positions = positions[1]
positions = positions.split(",")
if len(positions) != 4:
raise Exception("Bad format 4")
status["MPOS_X"] = float(positions[0])
status["MPOS_Y"] = float(positions[1])
status["MPOS_Z"] = float(positions[2])
status["MPOS_A"] = float(positions[3])
positions = feedback_split[2].split(":")
if len(positions) != 2:
raise Exception("Bad format 5")
positions = positions[1]
positions = positions.split(",")
if len(positions) != 2:
raise Exception("Bad format 6")
status["BUFFERS"] = int(positions[0])
active_axis = self.gcode_generator.dialog.ui.combo_active_axis.currentText()
mpos_x = float(status["MPOS_"+active_axis])
self.s_position.setText("{:.2f}°".format(mpos_x))
commands_in_buffer = self.hw.commands.qsize()
self.progress_program.setValue(self.progress_program.maximum() - int(commands_in_buffer))
self.s_status.setText(status["STATUS"])
self.update_enabled_elements()
def action_about_clicked(self):
dialog = QtWidgets.QDialog()
dialog.ui = about_ui.Ui_About()
dialog.ui.setupUi(dialog)
dialog.exec_()
def action_settings_clicked(self):
dialog = QtWidgets.QDialog()
dialog.ui = settings_ui.Ui_Settings()
dialog.ui.setupUi(dialog)
dialog.ui.check_remember_last_loaded_profile.setChecked(self.config["remember_last_loaded_profile"])
dialog.ui.check_remember_last_com_port.setChecked(self.config["remember_last_com_port"])
dialog.ui.check_auto_connect_after_restart.setChecked(self.config["auto_connect_after_restart"])
dialog.ui.check_beep_when_done.setChecked(self.config["beep_when_done"])
dialog.ui.check_flash_when_done.setChecked(self.config["flash_when_done"])
dialog.ui.check_start_compact.setChecked(self.config["start_compact"])
ret = dialog.exec_()
if ret:
self.config["remember_last_loaded_profile"] = dialog.ui.check_remember_last_loaded_profile.isChecked()
self.config["remember_last_com_port"] = dialog.ui.check_remember_last_com_port.isChecked()
self.config["auto_connect_after_restart"] = dialog.ui.check_auto_connect_after_restart.isChecked()
self.config["beep_when_done"] = dialog.ui.check_beep_when_done.isChecked()
self.config["flash_when_done"] = dialog.ui.check_flash_when_done.isChecked()
self.config["start_compact"] = dialog.ui.check_start_compact.isChecked()
def action_edit_motion_script_clicked(self):
ret = self.gcode_generator.show_modal()
if ret:
self.gcode_generator.push_generate_clicked()
active_axis = self.gcode_generator.dialog.ui.combo_active_axis.currentText()
self.manual_control.set_active_axis(active_axis)
values = self.gcode_generator.collect_values()
self.current_motion_profile = values
self.action_save_script.setEnabled(True)
self.update_enabled_elements()
def action_manual_control_clicked(self):
self.manual_control.show()
def action_monitor_clicked(self):
self.monitor.show()
def current_line_feedback(self, text):
if len(text)>2:
if text[0] == ";":
text_ = text[1:].strip()
self.label_gcode_comment.setText(text_)
def action_save_script_clicked(self):
with open(self.source_filename, 'w') as outfile:
json.dump(self.current_motion_profile, outfile)
def action_save_as_script_clicked(self):
fileName, _ = QFileDialog.getSaveFileName(self, "Save motion script as...", "", "Motion script (*.profile)")
if fileName:
with open(fileName, 'w') as outfile:
json.dump(self.current_motion_profile, outfile)
def action_load_script_clicked(self):
fileName, _ = QFileDialog.getOpenFileName(self, "Open motion script...", "", "Motion script (*.profile)")
if fileName:
with open(fileName) as json_file:
self.current_motion_profile = json.load(json_file)
self.gcode_generator.populate_values(self.current_motion_profile)
script_name = Path(fileName).stem
self.s_script.setText(script_name)
self.update_enabled_elements()
self.action_save_script.setEnabled(True)
self.action_save_as_script.setEnabled(True)
self.source_filename = fileName
def closeEvent(self, event):
global config
global running
utils.json_exit_routine(SETTINGS_FILE, self.config)
running = False
app.quit()
app = QtWidgets.QApplication(sys.argv)
app.setStyle('Fusion')
myWindow = MyWindowClass(None)
myWindow.show()
app.exec_()