-
Notifications
You must be signed in to change notification settings - Fork 10
/
raspyCNC.py
executable file
·250 lines (199 loc) · 8.9 KB
/
raspyCNC.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
#!/usr/bin/env python
# rasPyCNCController
# Copyright 2016 Francesco Santini <[email protected]>
#
# This file is part of rasPyCNCController.
#
# rasPyCNCController is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# rasPyCNCController is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with rasPyCNCController. If not, see <http://www.gnu.org/licenses/>.
from PySide.QtGui import *
from gcode.GrblWriterBasic import GrblWriterBasic
from gcode.GrblWriter import GrblWriter
from JogWidget.JogWidget import JogWidget
from RunWidget.RunWidget import RunWidget
from pyFileList.JoyFileList import JoyFileList
import sys
from splash_ui import Ui_Splash
import time
import argparse
from string_format import config_string_format
class SplashWidget(QWidget, Ui_Splash):
def __init__(self, parent = None):
QWidget.__init__(self, parent)
self.setupUi(self)
def setText(self, txt):
self.label.setText(txt)
class MainWindow(QStackedWidget):
def __init__(self, parent=None):
QStackedWidget.__init__(self,parent)
self.resize(480, 320)
def start_app(self, dummy = False):
self.splash = SplashWidget(self)
self.addWidget(self.splash)
self.setCurrentWidget(self.splash)
self.splash.AbortButton.clicked.connect(self.destroy)
# do not require joystick
#self.splash.setText("Waiting for joystick...")
#QApplication.processEvents()
self.splash.setText("Waiting for CNC...")
QApplication.processEvents()
if dummy:
self.grblWriter = GrblWriterBasic()
else:
self.grblWriter = GrblWriter()
self.grblWriter.grbl_error.connect(self.ask_perform_reset)
# here wait for GRBL and show splash screen
while not self.grblWriter.open():
QApplication.processEvents()
self.splash.setText("Initializing...")
QApplication.processEvents()
self.jogWidget = JogWidget()
self.runWidget = RunWidget()
self.fileListWidget = JoyFileList()
self.runWidget.setGrbl(self.grblWriter)
self.jogWidget.setGrbl(self.grblWriter)
self.addWidget(self.jogWidget)
self.addWidget(self.fileListWidget)
self.addWidget(self.runWidget)
self.jogWidget.load_event.connect(self.loadFile)
self.jogWidget.run_event.connect(self.runFile)
self.jogWidget.error_event.connect(self.grblError)
self.jogWidget.exitButton.clicked.connect(self.exitRequest)
self.fileListWidget.ok_clicked.connect(self.fileSelected)
self.fileListWidget.cancel_clicked.connect(self.cancelFileSelected)
# at the moment end and stop in the run widget have the same effect
self.runWidget.end_event.connect(self.runEnd)
self.runWidget.stop_event.connect(self.runEnd)
self.runWidget.error_event.connect(self.grblError)
self.runWidget.pause_event.connect(self.doPause) # when this is called, the pause was already generated
self.setCurrentWidget(self.jogWidget)
self.jogWidget.startJoggers()
# changes the appearance of the jog widget to work during pause
def _reconfigureJogWidget(self, pauseFlag):
if pauseFlag:
# repurpose jogwidget buttons
try:
self.jogWidget.run_event.disconnect(self.runFile)
except:
print "Can't jogWidget.run_event.disconnect(self.runFile)"
pass
try:
self.jogWidget.load_event.disconnect(self.loadFile)
except:
print "Can't jogWidget.load_event.disconnect(self.loadFile)"
pass
self.jogWidget.RunButton.originalText = self.jogWidget.RunButton.text()
self.jogWidget.RunButton.setText( config_string_format( QApplication.translate("joyWidget", "Cancel (%BTN_OK+1%)", None, QApplication.UnicodeUTF8)) )
self.jogWidget.LoadButton.originalText = self.jogWidget.LoadButton.text()
self.jogWidget.LoadButton.setText(config_string_format( QApplication.translate("joyWidget", "Resume (%BTN_CANCEL+1%)", None, QApplication.UnicodeUTF8)))
self.jogWidget.run_event.connect(self.cancelPause)
self.jogWidget.load_event.connect(self.doResume)
else:
try:
self.jogWidget.run_event.disconnect(self.cancelPause)
except:
pass
try:
self.jogWidget.load_event.disconnect(self.doResume)
except:
pass
self.jogWidget.RunButton.setText(self.jogWidget.RunButton.originalText)
self.jogWidget.LoadButton.setText(self.jogWidget.LoadButton.originalText)
self.jogWidget.run_event.connect(self.runFile)
self.jogWidget.load_event.connect(self.loadFile)
def ask_perform_reset(self, errorLine):
if self.grblWriter.resetting:
return
if self.runWidget.running: # the run widget deals with errors on its own
return
res = QMessageBox.critical(self, "Grbl Error", "%s\nPerform reset?" % (errorLine),
QMessageBox.Yes | QMessageBox.No)
if res == QMessageBox.Yes:
self.grblWriter.reset()
# don't need to generate the pause, this is called when the system is already paused
def doPause(self, pauseFlag):
if not pauseFlag:
return
self._reconfigureJogWidget(True)
self.setCurrentWidget(self.jogWidget)
self.jogWidget.startJoggers()
def doResume(self):
self._reconfigureJogWidget(False)
self.setCurrentWidget(self.runWidget)
self.runWidget.resume()
self.runWidget.startJoy()
def cancelPause(self):
self.runWidget.cancelPause()
self._reconfigureJogWidget(False)
self.jogWidget.startJoggers()
def exitRequest(self):
flags = QMessageBox.StandardButton.Yes
flags |= QMessageBox.StandardButton.No
question = QApplication.translate("cncController", "Do you really want to exit?", None, QApplication.UnicodeUTF8)
response = QMessageBox.question(self, "Question",
question,
flags)
if response == QMessageBox.Yes:
self.jogWidget.stopJoggers()
self.destroy()
# function called when the jog widget wants to select a file: activate the fileListWidget
def loadFile(self):
# reload devices
self.fileListWidget.populateDevices()
# activate the widget
self.setCurrentWidget(self.fileListWidget)
self.fileListWidget.startJoy()
# we get here from the listWidget: activate the jogwidget and load the file
def fileSelected(self, filename):
self.setCurrentWidget(self.jogWidget)
self.jogWidget.startJoggers()
QApplication.processEvents()
self.jogWidget.loadFile(filename)
# reactivate jogWidget without changing anything
def cancelFileSelected(self, filename):
self.setCurrentWidget(self.jogWidget)
self.jogWidget.startJoggers()
def runFile(self):
if self.jogWidget.isFileLoaded == False:
return
self.setCurrentWidget(self.runWidget)
self.runWidget.startJoy()
self.runWidget.runGCode(self.jogWidget.file, self.jogWidget.gcode, self.jogWidget.times, self.jogWidget.totalTime, self.jogWidget.bBox)
def runEnd(self):
self.setCurrentWidget(self.jogWidget)
self.jogWidget.startJoggers()
def grblError(self):
# reset grbl
if self.grblWriter.resetting: # already resetting
return
self.setCurrentWidget(self.splash)
self.splash.setText("GRBL Error. Reconnecting...")
QApplication.processEvents()
while not self.grblWriter.reset():
time.sleep(0.5)
self.setCurrentWidget(self.jogWidget)
self.jogWidget.startJoggers()
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="A convenient GUI for CNC control")
parser.add_argument("-f", "--fullscreen", action="store_true", help="make app fullscreen")
parser.add_argument("-d", "--dummy", action="store_true", help="use dummy sender (debug)")
args = parser.parse_args()
app = QApplication(sys.argv)
app.setStyle(QStyleFactory.create(u'Plastique'))
window = MainWindow()
if args.fullscreen:
window.showFullScreen()
else:
window.show()
window.start_app(args.dummy)
sys.exit(app.exec_())