-
Notifications
You must be signed in to change notification settings - Fork 0
/
turboprobe.py
107 lines (82 loc) · 3.58 KB
/
turboprobe.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
#!/usr/bin/env python
# coding: utf-8
# Copyright (c) 2018 Kurt Jacobson
#
# 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 os # For file path manipulation
import linuxcnc # For commanding linuxcnc
from PyQt5 import uic, QtWidgets
PARENT_DIR = os.path.dirname(os.path.realpath(__file__))
# Change this path to match [RS274NGC] SUBROUTINE_PATH given in the INI
SUBROUTINE_PATH = os.path.join(PARENT_DIR, 'subroutines')
CMD = linuxcnc.command()
STAT = linuxcnc.stat()
class SubCaller(QtWidgets.QMainWindow):
def __init__(self):
super(SubCaller, self).__init__()
uic.loadUi(os.path.join(PARENT_DIR, "turboprobe.ui"), self)
for filename in os.listdir(SUBROUTINE_PATH):
filename_and_ext = os.path.splitext(filename)
subname = filename_and_ext[0]
self.subComboBox.addItem(filename, subname)
self.callSubButton.clicked.connect(self.callSub)
def callSub(self):
filename = self.subComboBox.currentText()
subname = self.subComboBox.currentData()
filepath = os.path.join(SUBROUTINE_PATH, filename)
arg_str = ''
with open(filepath, 'r') as fh:
line = fh.readline()
if line.startswith('(ARGS,'):
args_format = line.strip('(ARGS,').strip().strip(')')
args = self.getArgs()
arg_str = args_format.format(**args)
cmd_str = "o<{}> call {}".format(subname, arg_str)
# Print the command to the terminal so the user can see what is happening
print("Calling MDI command: ", cmd_str)
self.statusBar.setStyleSheet("QStatusBar{color:black}")
self.statusBar.showMessage("Probing ...")
# Set the LinuxCNC mode to MDI
CMD.mode(linuxcnc.MODE_MDI)
# Issue the MDI command to call the sub
CMD.mdi(cmd_str)
CMD.wait_complete(10000)
print('Done')
STAT.poll()
if STAT.probe_tripped:
self.statusBar.setStyleSheet("QStatusBar{color:green}")
self.statusBar.showMessage("Probing finished successfully")
self.onProbeSuccess()
else:
self.statusBar.setStyleSheet("QStatusBar{color:red}")
self.statusBar.showMessage("ERROR: Probe move finished without making contact", msecs=5000)
def onProbeSuccess(self):
probed_pos = STAT.probed_position
for anum, aletter in enumerate('xyz'):
pos_str = "{:6.4f}".format(probed_pos[anum])
print("Probed {} pos: {}".format(aletter.upper(), pos_str))
getattr(self, '{}_probed_pos'.format(aletter)).setText(pos_str)
def getArgs(self):
args = {}
for line_edit in self.findChildren(QtWidgets.QLineEdit):
key = line_edit.objectName()
value = line_edit.text()
args[key] = value
return args
if __name__ == '__main__':
import sys
app = QtWidgets.QApplication(sys.argv)
sub_caller = SubCaller()
sub_caller.show()
sys.exit(app.exec_())