-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathbase.py
More file actions
508 lines (442 loc) · 16 KB
/
Copy pathbase.py
File metadata and controls
508 lines (442 loc) · 16 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
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
#!/usr/bin/env python
#
# Copyright (c) 2015 Serguei Glavatski ( verser from cnc-club.ru )
# Copyright (c) 2020 Probe Screen NG Developers
#
# This program 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 2
# of the License, or (at your option) any later version.
# This program 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 this program; If not, see <http://www.gnu.org/licenses/>.
import math
import os
import sys
import time
from datetime import datetime
from functools import wraps
from subprocess import PIPE, Popen
import linuxcnc
import gi
gi.require_version("Gtk", "3.0")
gi.require_version("PangoCairo", "1.0")
from gi.repository import Gtk, Gdk, cairo, Pango, PangoCairo
from .configparser import ProbeScreenConfigParser
from .util import restore_task_mode
CONFIGPATH1 = os.environ["CONFIG_DIR"]
class ProbeScreenBase(object):
# --------------------------
#
# INIT
#
# --------------------------
def __init__(self, halcomp, builder, useropts):
self.builder = builder
self.halcomp = halcomp
# Load the machines INI file
self.inifile = linuxcnc.ini(os.environ["INI_FILE_NAME"])
if not self.inifile:
self.error_dialog("Error, no INI File given")
# Load Probe Screen Preferences
self.prefs = ProbeScreenConfigParser(self.get_preference_file_path())
# Which display is in use? AXIS / gmoccapy / unknown
self.display = self.get_display() or "unknown"
# LinuxCNC Command / Stat / Error Interfaces
self.command = linuxcnc.command()
self.stat = linuxcnc.stat()
self.stat.poll()
if self.stat.linear_units == 1.0:
self.setunits = "G21"
else:
self.setunits = "G20"
# History Area
textarea = builder.get_object("textview1")
self.buffer = textarea.get_property("buffer")
# Warning Dialog
self.window = builder.get_object("window1")
# VCP Reload Action
self._vcp_action_reload = self.builder.get_object("vcp_action_reload")
# Results Display
self._lb_probe_xp = self.builder.get_object("lb_probe_xp")
self._lb_probe_yp = self.builder.get_object("lb_probe_yp")
self._lb_probe_xm = self.builder.get_object("lb_probe_xm")
self._lb_probe_ym = self.builder.get_object("lb_probe_ym")
self._lb_probe_lx = self.builder.get_object("lb_probe_lx")
self._lb_probe_ly = self.builder.get_object("lb_probe_ly")
self._lb_probe_z = self.builder.get_object("lb_probe_z")
self._lb_probe_d = self.builder.get_object("lb_probe_d")
self._lb_probe_xc = self.builder.get_object("lb_probe_xc")
self._lb_probe_yc = self.builder.get_object("lb_probe_yc")
self._lb_probe_a = self.builder.get_object("lb_probe_a")
# --------------------------
#
# MDI Command Methods
#
# --------------------------
@restore_task_mode
def gcode(self, s, data=None):
self.command.mode(linuxcnc.MODE_MDI)
self.command.wait_complete()
for l in s.split("\n"):
# Search for G1 followed by a space, otherwise we'll catch G10 too.
if "G1 " in l:
l += " F#<_ini[TOOLSENSOR]RAPID_SPEED>"
self.command.mdi(l)
self.command.wait_complete()
if self.error_poll() == -1:
return -1
return 0
@restore_task_mode
def ocode(self, s, data=None):
self.command.mode(linuxcnc.MODE_MDI)
self.command.wait_complete()
self.command.mdi(s)
self.stat.poll()
while self.stat.interp_state != linuxcnc.INTERP_IDLE:
if self.error_poll() == -1:
return -1
self.command.wait_complete()
self.stat.poll()
self.command.wait_complete()
if self.error_poll() == -1:
return -1
return 0
def error_poll(self):
if "axis" in self.display:
# AXIS polls for errors every 0.2 seconds, so we wait slightly longer to make sure it's happened.
time.sleep(0.25)
error_pin = Popen(
"halcmd getp probe.user.error ", shell=True, stdout=PIPE
).stdout.read()
elif "gmoccapy" in self.display:
# gmoccapy polls for errors every 0.25 seconds, OR whatever value is in the [DISPLAY]CYCLE_TIME ini
# setting, so we wait slightly longer to make sure it's happened.
ms = int(self.inifile.find("DISPLAY", "CYCLE_TIME") or 250) + 50
time.sleep(ms / 100)
error_pin = Popen(
"halcmd getp gmoccapy.error ", shell=True, stdout=PIPE
).stdout.read()
else:
print("Unable to poll %s GUI for errors" % self.display)
return -1
if "TRUE" in error_pin:
text = "See notification popup"
self.add_history("Error: %s" % text)
print("error", text)
self.command.mode(linuxcnc.MODE_MANUAL)
self.command.wait_complete()
return -1
return 0
# --------------------------
#
# Utility Methods
#
# --------------------------
def get_display(self):
# gmoccapy or axis ?
temp = self.inifile.find("DISPLAY", "DISPLAY")
if not temp:
print(
"**** PROBE SCREEN GET INI INFO **** \n Error recognition of display type : %s"
% temp
)
return temp
def get_preference_file_path(self):
# we get the preference file, if there is none given in the INI
# we use toolchange2.pref in the config dir
temp = self.inifile.find("DISPLAY", "PREFERENCE_FILE_PATH")
if not temp:
machinename = self.inifile.find("EMC", "MACHINE")
if not machinename:
temp = os.path.join(CONFIGPATH1, "probe_screen.pref")
else:
machinename = machinename.replace(" ", "_")
temp = os.path.join(CONFIGPATH1, "%s.pref" % machinename)
print("**** probe_screen GETINIINFO **** \n Preference file path: %s" % temp)
return temp
def vcp_reload(self):
""" Realods the VCP - e.g. after changing changing changing origin/zero points """
self._vcp_action_reload.emit("activate")
# --------------------------
#
# History and Logging Methods
#
# --------------------------
def add_history(
self,
tool_tip_text,
s="",
xm=None,
xc=None,
xp=None,
lx=None,
ym=None,
yc=None,
yp=None,
ly=None,
z=None,
d=None,
a=None,
):
c = "{0: <10}".format(tool_tip_text)
if "Xm" in s:
c += "X-=%.4f " % xm
self._lb_probe_xm.set_text("%.4f" % xm)
if "Xc" in s:
c += "Xc=%.4f " % xc
self._lb_probe_xc.set_text("%.4f" % xc)
if "Xp" in s:
c += "X+=%.4f " % xp
self._lb_probe_xp.set_text("%.4f" % xp)
if "Lx" in s:
c += "Lx=%.4f " % lx
self._lb_probe_lx.set_text("%.4f" % lx)
if "Ym" in s:
c += "Y-=%.4f " % ym
self._lb_probe_ym.set_text("%.4f" % ym)
if "Yc" in s:
c += "Yc=%.4f " % yc
self._lb_probe_yc.set_text("%.4f" % yc)
if "Yp" in s:
c += "Y+=%.4f " % yp
self._lb_probe_yp.set_text("%.4f" % yp)
if "Ly" in s:
c += "Ly=%.4f " % ly
self._lb_probe_ly.set_text("%.4f" % ly)
if "Z" in s:
c += "Z=%.4f " % z
self._lb_probe_z.set_text("%.4f" % z)
if "D" in s:
c += "D=%.4f" % d
self._lb_probe_d.set_text("%.4f" % d)
if "A" in s:
c += "Angle=%.3f" % a
self._lb_probe_a.set_text("%.3f" % a)
self.add_history_text(c)
def add_history_text(self, text):
# Prepend a timestamp to all History lines
text = datetime.now().strftime("%H:%M:%S ") + text
# Remove the oldest history entries when we have a large
# number of entries.
i = self.buffer.get_end_iter()
if i.get_line() > 1000:
i.backward_line()
self.buffer.delete(i, self.buffer.get_end_iter())
# Add the line of text to the top of the history
i.set_line(0)
self.buffer.insert(i, "%s \n" % text)
def _dialog(
self, gtk_type, gtk_buttons, message, secondary=None, title=_("Probe Screen NG")
):
""" displays a dialog """
dialog = Gtk.MessageDialog(
self.window,
Gtk.DialogFlags.DESTROY_WITH_PARENT,
gtk_type,
gtk_buttons,
message,
)
# if there is a secondary message then the first message text is bold
if secondary:
dialog.format_secondary_text(secondary)
dialog.set_keep_above(True)
dialog.show_all()
dialog.set_title(title)
responce = dialog.run()
dialog.destroy()
return responce == Gtk.ResponseType.OK
def warning_dialog(self, message, secondary=None, title=_("Probe Screen NG")):
""" displays a warning dialog """
return self._dialog(
Gtk.MessageType.WARNING, Gtk.ButtonsType.OK, message, secondary, title
)
def error_dialog(self, message, secondary=None, title=_("Probe Screen NG")):
""" displays a warning dialog and exits the probe screen"""
self._dialog(Gtk.MessageType.ERROR, Gtk.ButtonsType.CLOSE, message, secondary, title)
sys.exit(1)
def display_result_a(self, value):
# TODO: Convert the remaining uses of this to add_history and remove.
self._lb_probe_a.set_text("%.3f" % value)
# --------------------------
#
# Generic Probe Movement Methods
#
# --------------------------
def z_clearance_down(self, data=None):
# move Z - z_clearance
s = """%s
G91
G1 Z-%f
G90""" % (
self.setunits,
self.halcomp["ps_z_clearance"]
)
if self.gcode(s) == -1:
return -1
return 0
def z_clearance_up(self, data=None):
# move Z + z_clearance
s = """%s
G91
G1 Z%f
G90""" % (
self.setunits,
self.halcomp["ps_z_clearance"]
)
if self.gcode(s) == -1:
return -1
return 0
# --------------------------
#
# Generic Position Calculations
#
# --------------------------
def set_zerro(self, s="XYZ", x=0.0, y=0.0, z=0.0):
if self.halcomp["set_zero"]:
# Z current position
self.stat.poll()
tmpz = (
self.stat.position[2]
- self.stat.g5x_offset[2]
- self.stat.g92_offset[2]
- self.stat.tool_offset[2]
)
c = "G10 L20 P0"
s = s.upper()
if "X" in s:
x += self.halcomp["ps_offs_x"]
c += " X%s" % x
if "Y" in s:
y += self.halcomp["ps_offs_y"]
c += " Y%s" % y
if "Z" in s:
tmpz = tmpz - z + self.halcomp["ps_offs_z"]
c += " Z%s" % tmpz
self.gcode(c)
self.vcp_reload()
time.sleep(1)
def probed_position_with_offsets(self):
self.stat.poll()
probed_position = list(self.stat.probed_position)
coord = list(self.stat.probed_position)
g5x_offset = list(self.stat.g5x_offset)
g92_offset = list(self.stat.g92_offset)
tool_offset = list(self.stat.tool_offset)
for i in range(0, len(probed_position) - 1):
coord[i] = (
probed_position[i] - g5x_offset[i] - g92_offset[i] - tool_offset[i]
)
angl = self.stat.rotation_xy
res = self._rott00_point(coord[0], coord[1], -angl)
coord[0] = res[0]
coord[1] = res[1]
return coord
def _rott00_point(self, x1=0.0, y1=0.0, a1=0.0):
""" rotate around 0,0 point coordinates """
coord = [x1, y1]
if a1 != 0:
t = math.radians(a1)
coord[0] = x1 * math.cos(t) - y1 * math.sin(t)
coord[1] = x1 * math.sin(t) + y1 * math.cos(t)
return coord
def length_x(self, xm=None, xp=None):
""" Calculates a length in the X direction """
# Use previous value for xm if not supplied
if xm is None:
xm = self._lb_probe_xm.get_text()
# Use None if no previous value exists
if xm == "":
xm = None
else:
xm = float(xm)
# Use previous value for xp if not supplied
if xp is None:
xp = self._lb_probe_xp.get_text()
# Use None if no previous value exists
if xp == "":
xp = None
else:
xp = float(xp)
res = 0
if xm is None or xp is None:
return res
if xm < xp:
res = xp - xm
else:
res = xm - xp
return res
def length_y(self, ym=None, yp=None):
""" Calculates a length in the Y direction """
# Use previous value for ym if not supplied
if ym is None:
ym = self._lb_probe_ym.get_text()
# Use None if no previous value exists
if ym == "":
ym = None
else:
ym = float(ym)
# Use previous value for yp if not supplied
if yp is None:
yp = self._lb_probe_yp.get_text()
# Use None if no previous value exists
if yp == "":
yp = None
else:
yp = float(yp)
res = 0
if ym is None or yp is None:
return res
if ym < yp:
res = yp - ym
else:
res = ym - yp
return res
# --------------------------
#
# Generic UI Methods
#
# --------------------------
def on_common_spbtn_key_press_event(self, pin_name, gtkspinbutton, data=None):
keyname = Gdk.keyval_name(data.keyval)
if keyname == "Return":
# Drop the Italics
gtkspinbutton.modify_font(Pango.FontDescription("normal"))
elif keyname == "Escape":
# Restore the original value
gtkspinbutton.set_value(self.halcomp[pin_name])
# Drop the Italics
gtkspinbutton.modify_font(Pango.FontDescription("normal"))
else:
# Set to Italics
gtkspinbutton.modify_font(Pango.FontDescription("italic"))
def on_common_spbtn_value_changed(
self, pin_name, gtkspinbutton, data=None, _type=float
):
# Drop the Italics
gtkspinbutton.modify_font(Pango.FontDescription("normal"))
# Update the pin
self.halcomp[pin_name] = gtkspinbutton.get_value()
# Update the preferences
self.prefs.putpref(pin_name, gtkspinbutton.get_value(), _type)
# --------------------------
#
# Generic Method Wrappers
#
# --------------------------
@classmethod
def ensure_errors_dismissed(cls, f):
""" Ensures all errors have been dismissed, otherwise, shows a warning dialog """
@wraps(f)
def wrapper(self, *args, **kwargs):
if self.error_poll() == -1:
message = _("Please dismiss & act upon all errors")
secondary = _("You can retry once done")
self.warning_dialog(message, secondary=secondary)
return -1
# Execute wrapped function
return f(self, *args, **kwargs)
return wrapper