-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathMachinekitExecute.py
320 lines (271 loc) · 13.2 KB
/
MachinekitExecute.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
# Dock widget to load and manage machine tasks.
import FreeCAD
import FreeCADGui
import MKUtils
import MachinekitManualToolChange
import PathScripts.PathLog as PathLog
import PathScripts.PathPost as PathPost
import PathScripts.PathUtil as PathUtil
import PySide.QtCore
import PySide.QtGui
import ftplib
import io
import machinekit
import machinetalk.protobuf.motcmds_pb2 as MOTCMDS
import machinetalk.protobuf.status_pb2 as STATUS
import machinetalk.protobuf.types_pb2 as TYPES
from MKCommand import *
from MKServiceCommand import *
#PathLog.setLevel(PathLog.Level.DEBUG, PathLog.thisModule())
#PathLog.trackModule(PathLog.thisModule())
class TreeSelectionObserver(object):
'''An observer used to determine if currently a Job is selected.'''
def __init__(self, notify):
self.notify = notify
def addSelection(self, doc, obj, sub, pnt):
self.notify()
def removeSelection(self, doc, obj, sub):
self.notify()
def setSelection(self, doc, something=None):
self.notify()
def clearSelection(self, doc):
self.notify()
class Execute(object):
'''Dock widget to load and manage an FC job into MK.'''
def __init__(self, mk):
self.mk = mk
self.ui = FreeCADGui.PySideUic.loadUi(machinekit.FileResource('execute.ui'), self)
palette = PySide.QtGui.QPalette()
palette.setColor(PySide.QtGui.QPalette.Background, PySide.QtGui.QColor(0xffd75e))
self.ui.dockWidgetContents.setAutoFillBackground(True)
self.ui.dockWidgetContents.setPalette(palette)
self.oi = 'v'
if True:
lo = PySide.QtGui.QHBoxLayout()
lo.setSpacing(0)
lo.setContentsMargins(0, 0, 0, 0)
tb = PySide.QtGui.QFrame()
tb.setFrameShape(PySide.QtGui.QFrame.Box)
tb.setLayout(lo)
#tb.setContentsMargins(0, 0, 0, 0)
self.title = PySide.QtGui.QLabel()
self.title.setText('-.-')
self.title.setContentsMargins(0, 0, 0, 0)
lo.addWidget(self.title, 10)
self.ob = PySide.QtGui.QPushButton()
#self.ob.setFlat(False)
self.ob.setIcon(PySide.QtGui.QApplication.style().standardIcon(PySide.QtGui.QStyle.SP_TitleBarUnshadeButton))
self.ob.clicked.connect(self.toggleOrientation)
lo.addWidget(self.ob)
bs = None
for b in self.ui.findChildren(PySide.QtGui.QAbstractButton):
if 'qt_dockwidget' in b.objectName():
bt = PySide.QtGui.QPushButton()
bt.setIcon(b.icon())
bs = b.size()
if bs.height() < bs.width():
bs.setWidth(bs.height())
else:
bs.setHeight(bs.width())
bt.setMaximumSize(bs)
#bt.setFlat(False)
bt.setContentsMargins(0, 0, 0, 0)
bt.clicked.connect(b.click)
lo.addWidget(bt)
if bs:
self.title.setMaximumHeight(bs.height())
self.ob.setMaximumSize(bs)
#tb.setMaximumHeight(bs.height())
self.ui.setTitleBarWidget(tb)
self.job = None
self.ui.load.clicked.connect(self.executeUpload)
self.ui.run.clicked.connect(self.executeRun)
self.ui.step.clicked.connect(self.executeStep)
self.ui.pause.clicked.connect(self.executePause)
self.ui.stop.clicked.connect(self.executeStop)
self.ui.scaleInt.valueChanged.connect(self.executeScaleInt)
self.ui.scaleInt.sliderReleased.connect(self.executeScaleVal)
self.ui.scaleVal.editingFinished.connect(self.executeScaleVal)
self.ui.status.setText('')
rect = self.ui.geometry()
self.ui.resize(rect.width(), 0)
self.observer = TreeSelectionObserver(self.objectSelectionChanged)
FreeCADGui.Selection.addObserver(self.observer)
self.objectSelectionChanged()
self.updateOverride()
self.updateJob(self.mk.getJob())
self.updateUI()
self.toolChange = MachinekitManualToolChange.Controller(self.mk)
machinekit.execute = self
self.mk.statusUpdate.connect(self.changed)
self.mk.jobUpdate.connect(self.updateJob)
if not self.mk.getJob():
self.mk.updateJob()
def terminate(self):
'''Called when the dock is closed.'''
self.mk.statusUpdate.disconnect(self.changed)
self.mk = None
FreeCADGui.Selection.removeObserver(self.observer)
if machinekit.execute == self:
machinekit.execute = None
def isConnected(self, topics=None):
'''Return True if the MK instance is accessible and responding.'''
return self.mk.isValid()
def objectSelectionChanged(self):
'''Callback when the selection in FC's tree view changes.'''
jobs = [sel.Object for sel in FreeCADGui.Selection.getSelectionEx() if sel.Object.Name.startswith('Job')]
if len(jobs) == 1:
self.job = jobs[0]
else:
self.job = None
self.ui.load.setEnabled(self.isIdle() and not self.job is None and self.mk.isHomed())
def toggleOrientation(self):
'''The layout of the widget can be horizontal or vertical, switch between those two.'''
if 'v' == self.oi:
self.ui.execute.layout().setDirection(PySide.QtGui.QBoxLayout.TopToBottom)
self.ob.setIcon(PySide.QtGui.QApplication.style().standardIcon(PySide.QtGui.QStyle.SP_TitleBarShadeButton))
self.oi = 'h'
else:
self.ui.execute.layout().setDirection(PySide.QtGui.QBoxLayout.LeftToRight)
self.ob.setIcon(PySide.QtGui.QApplication.style().standardIcon(PySide.QtGui.QStyle.SP_TitleBarUnshadeButton))
self.oi = 'v'
def isState(self, state):
'''Return True if the MK interpreter is currently in the given state.'''
return STATUS.EmcInterpStateType.Value(state) == self.mk['status.interp.state']
def isIdle(self):
return self.isState('EMC_TASK_INTERP_IDLE')
def isPaused(self):
return self.isState('EMC_TASK_INTERP_PAUSED')
def executeUpload(self):
'''Post process the current Path.Job and upload the resulting g-code into MK.
Tag the uploaded g-code with the job and a hash so we can determine if the uploaded
version is consistent with what is currently in FC.'''
job = self.job
if job:
currTool = None
postlist = []
for obj in job.Operations.Group:
tc = PathUtil.toolControllerForOp(obj)
if tc is not None:
if tc.ToolNumber != currTool:
postlist.append(tc)
currTool = tc.ToolNumber
postlist.append(obj)
post = PathPost.CommandPathPost()
fail, gcode = post.exportObjectsWith(postlist, job, False)
if not fail:
print("POST: ", fail)
preamble = "(FreeCAD.Job: %s)\n(FreeCAD.File: %s)\n(FreeCAD.Signature: %d)\n" % (job.Name, job.Document.FileName, MKUtils.pathSignature(job.Path))
buf = io.BytesIO((preamble + gcode).encode())
endpoint = self.mk.instance.endpoint.get('file')
if endpoint:
ftp = ftplib.FTP()
ftp.connect(endpoint.address(), endpoint.port())
ftp.login()
ftp.storbinary("STOR %s" % self.mk.RemoteFilename, buf)
ftp.quit()
sequence = MKUtils.taskModeMDI(self.mk)
for tc in job.ToolController:
t = tc.Tool
radius = float(t.Diameter) / 2 if hasattr(t, 'Diameter') else 0.
offset = t.LengthOffset if hasattr(t, 'LengthOffset') else 0.
sequence.append(MKCommandTaskExecute("G10 L1 P%d R%g Z%g" % (tc.ToolNumber, radius, offset)))
sequence.extend(MKUtils.taskModeAuto(self.mk))
sequence.append(MKCommandTaskReset(False))
sequence.extend([MKCommandOpenFile(self.mk.remoteFilePath(), True), MKCommandOpenFile(self.mk.remoteFilePath(), False)])
sequence.append(MKCommandTaskRun(True))
self.mk['command'].sendCommands(sequence)
else:
PathLog.error('No endpoint found')
else:
PathLog.error('Post processing failed')
def executeRun(self):
'''Start the task to execute the uploaded g-code.'''
sequence = MKUtils.taskModeMDI(self.mk)
sequence.append(MKCommandTaskExecute('M6 T0'))
sequence.append(MKCommandWaitUntil(lambda : self.mk['status.io.tool.nr'] <= 0))
sequence.extend(MKUtils.taskModeAuto(self.mk, True))
sequence.append(MKCommandTaskRun(False))
self.mk['command'].sendCommands(sequence)
def executeStep(self):
'''Perform a single step of the uploaded g-code.'''
sequence = MKUtils.taskModeAuto(self.mk)
sequence.append(MKCommandTaskStep())
self.mk['command'].sendCommands(sequence)
def executePause(self):
'''Pause execution of the uploaded task.'''
if self.isPaused():
self.mk['command'].sendCommand(MKCommandTaskResume())
else:
self.mk['command'].sendCommand(MKCommandTaskPause())
def executeStop(self):
'''Stop execution of the uploaded task.'''
self.mk['command'].sendCommand(MKCommandTaskAbort())
def executeScaleInt(self):
'''The feed override spin box has been edited, update the slider accordingly.'''
percent = self.ui.scaleInt.value()
scale = percent / 100.0
self.ui.scaleVal.blockSignals(True)
self.ui.scaleVal.setValue(scale)
self.ui.scaleVal.blockSignals(False)
def executeScaleVal(self):
'''The feed override has changed, send the new value to MK.'''
scale = self.ui.scaleVal.value()
self.mk['command'].sendCommand(MKCommandTrajSetScale(scale))
def updateExecute(self, connected, powered):
'''Update the view according to the current state.'''
if connected and powered:
if self.isIdle():
self.ui.load.setEnabled(not self.job is None)
self.ui.run.setEnabled(self.mk['status.task.file'] != '')
self.ui.step.setEnabled(self.mk['status.task.file'] != '')
self.ui.pause.setEnabled(False)
self.ui.stop.setEnabled(False)
else:
self.ui.load.setEnabled(False)
self.ui.run.setEnabled(False)
self.ui.step.setEnabled(self.mk['status.motion.type'] == MOTCMDS._EMC_MOTION_TYPE_NONE)
self.ui.pause.setEnabled(True)
if self.ui.pause.isChecked() != self.isPaused():
self.ui.pause.setChecked(self.isPaused())
self.ui.stop.setEnabled(True)
mode = TYPES.RCS_STATUS.Name(self.mk['status.motion.state']).split('_')[1].lower()
state = ' '.join(STATUS.EmcTaskExecStateType.Name(self.mk['status.task.state']).split('_')[3:]).lower()
if mode == 'auto':
istate = STATUS.EmcInterpStateType.Name(self.mk['status.interp.state']).split('_')[3].lower()
mode = "%s.%s" % (mode, istate)
self.ui.status.setText("%s:%s (%d/%d)" % (mode, state, self.mk['status.motion.line'], self.mk['status.task.line.total']))
def updateUI(self):
'''Callback when some state changes require the view to be updated.'''
connected = self.isConnected()
powered = self.mk.isPowered()
self.updateExecute(connected, powered)
self.ui.dockWidgetContents.setEnabled(powered)
self.ui.override.setEnabled(powered and connected and self.mk['status.motion.feed.override'])
def updateOverride(self):
'''Update the override slider and spin box.'''
if self.mk['status.motion.feed'] and self.mk['status.config.override.feed']:
self.ui.scaleInt.blockSignals(True)
self.ui.scaleInt.setMinimum(self.mk['status.config.override.feed.min'] * 100)
self.ui.scaleInt.setMaximum(self.mk['status.config.override.feed.max'] * 100)
self.ui.scaleInt.setSliderPosition(self.mk['status.motion.feed.rate'] * 100)
self.ui.scaleInt.blockSignals(False)
self.ui.scaleVal.blockSignals(True)
self.ui.scaleVal.setValue(self.mk['status.motion.feed.rate'])
self.ui.scaleVal.blockSignals(False)
def updateJob(self, job):
'''Callback when MK's loaded g-code changes. Depending on the g-code set the title accordingly.'''
title = '-.-'
if not job:
job = self.mk.getJob()
if job:
title = "%s.%s" % (job.Document.Label, job.Label)
self.title.setText(title)
self.ui.execute.setTitle(title)
def changed(self, service, updated):
'''Callback by the framework on status changes.'''
if self.mk:
if not updated is None:
if service.topicName() == 'status.motion' and 'feed.rate' in updated:
self.updateOverride()
self.updateUI()