File: probe_subprog.py

package info (click to toggle)
linuxcnc 1%3A2.9.4-2
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 282,780 kB
  • sloc: python: 201,110; ansic: 106,370; cpp: 99,219; tcl: 16,054; xml: 10,617; sh: 10,258; makefile: 1,251; javascript: 138; sql: 72; asm: 15
file content (280 lines) | stat: -rw-r--r-- 10,333 bytes parent folder | download | duplicates (2)
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
#!/usr/bin/env python3
# Qtvcp probe subprogram
#
# Copyright (c) 2018  Chris Morley <chrisinnanaimo@hotmail.com>
#
# 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.
# This subprogram is used by both versa_probe and basic_probe widgets

import sys
import time
import json

from PyQt5.QtCore import QObject
from qtvcp.core import Status, Action, Info
from qtvcp.widgets.probe_routines import ProbeRoutines

# Instantiate the libraries with global reference
# STATUS gives us status messages from linuxcnc
STATUS = Status()
ACTION = Action()
INFO = Info()
class ProbeSubprog(QObject, ProbeRoutines):
    def __init__(self):
        QObject.__init__(self)
        ProbeRoutines.__init__(self)
        if INFO.MACHINE_IS_METRIC:
            self._format_template = "%3.3f"
        else:
            self._format_template = "%2.4f"

        self.send_dict = {}
        self.error_status = None
        # list of parameters received from main probe program
        # excluding booleans, these are handled separately
        self.parm_list = ['probe_diam',
                          'latch_return_dist',
                          'max_travel',
                          'max_z_travel',
                          'search_vel',
                          'probe_vel',
                          'rapid_vel',
                          'side_edge_length',
                          'xy_clearance',
                          'adj_x',
                          'adj_y',
                          'adj_z',
                          'adj_angle',
                          'x_hint_bp',
                          'y_hint_bp',
                          'x_hint_rv',
                          'y_hint_rv',
                          'diameter_hint',
                          'z_clearance',
                          'extra_depth',
                          'cal_x_width',
                          'cal_y_width',
                          'cal_diameter',
                          'calibration_offset',
                          'ts_diam',
                          'z_max_clear',
                          'ts_x',
                          'ts_y',
                          'ts_z',
                          'ts_max',
                          'tool_diameter',
                          'tool_number',
                          'tool_probe_height',
                          'tool_block_height']

        # data structure to hold parameters
        # common
        self.data_probe_diam = 1.0
        self.data_latch_return_dist = 1.0
        self.data_search_vel = 10.0
        self.data_probe_vel = 10.0
        self.data_rapid_vel = 10.0
        self.data_max_travel = 10.0
        self.data_max_z_travel = 10.0
        self.data_side_edge_length = 1.0
        self.data_xy_clearance = 1.0
        self.data_z_clearance = 1.0
        self.data_extra_depth = 0.0
        self.allow_auto_zero = False
        self.allow_auto_skew = False
        # VersaProbe exclusive
        self.data_adj_x = 0.0
        self.data_adj_y = 0.0
        self.data_adj_z = 0.0
        self.data_adj_angle = 0.0
        self.data_ts_diam = None
        self.data_z_max_clear = None
        self.data_ts_x= None
        self.data_ts_y= None
        self.data_ts_z= None
        self.data_ts_max = None
        self.data_tool_diameter = None
        self.data_tool_number = None
        self.data_tool_probe_height = None
        self.data_tool_block_height = None
        # BasicProbe exclusive
        self.data_x_hint_bp = 0.0
        self.data_y_hint_bp = 0.0
        self.data_x_hint_rv = 0.0
        self.data_y_hint_rv = 0.0
        self.data_diameter_hint = 0.0
        self.data_cal_x_width = 0.0
        self.data_cal_y_width = 0.0
        self.data_cal_diameter = 0.0
        self.data_calibration_offset = 0.0
        self.cal_avg_error = False
        self.cal_x_error = False
        self.cal_y_error = False
        # list of results to be transferred to main program
        self.status_list = ['xm', 'xc', 'xp', 'ym', 'yc', 'yp', 'lx', 'ly', 'z', 'd', 'a', 'delta','th','bh']
        # data structure to hold result values
        self.status_xm = None
        self.status_xc = None
        self.status_xp = None
        self.status_ym = None
        self.status_yc = None
        self.status_yp = None
        self.status_lx = None
        self.status_ly = None
        self.status_z = None
        self.status_d = None
        self.status_a = None
        self.status_delta = None
        self.status_th = None
        self.status_bh = None
        self.history_log = ""

        self.process()

    def process(self):
        while 1:
            try:
                line = sys.stdin.readline()
            except KeyboardInterrupt:
                break
            if line:
                cmd = line
                line = None
                try:
                    error = self.process_command(cmd)

                    # block polling here 0 main program should start polling in their end
                    STATUS.block_error_polling()

                    # error = 1 means success,
                    # error = None means ignore,
                    # anything else is an error - a returned string is an error message
                    if error is not None:
                        if error != 1:
                            if type(error) == str:
                                sys.stdout.write("ERROR INFO Probe routine: {}\n".format(error))
                            else:
                                sys.stdout.write("ERROR Probe routine returned with error\n")
                        else:
                            self.collect_status()
                            sys.stdout.write("COMPLETE$" + json.dumps(self.send_dict) + "\n")
                            sys.stdout.flush()
                        sys.stdout.flush()

                    # print history
                    if self.history_log != "":
                        time.sleep(0.1)
                        sys.stdout.write("HISTORY {}\n".format(self.history_log))
                        self.history_log = ""
                        sys.stdout.flush()

                except Exception as e:
                    sys.stdout.write("ERROR Command Error: {}\n".format(e))
                    sys.stdout.flush()
                break

    # check that the command is actually a method in our class else
    # this message isn't for us - ignore it
    def process_command(self, cmd):
        cmd = cmd.rstrip().split('$')
        if cmd[0] in dir(self):
            if not STATUS.is_on_and_idle(): return None
            pre = self.prechecks()
            if pre is not None: return pre
            parms = json.loads(cmd[1])
            self.update_data(parms)
            # start polling errors here - parent program should have blocked their polling
            STATUS.unblock_error_polling()
            error = self[cmd[0]]()
            if (error != 1 or type(error)== str) and STATUS.is_on_and_idle():
                ACTION.CALL_MDI("G90")
            self.postreset()
            return error
        else:
            return 'Command function {} not in probe routines'.format(cmd[0])

    def update_data(self, parms):
        for key in parms:
            if key in self.parm_list:
                try:
                    self['data_' + key] = float(parms[key].encode('utf-8'))
                except:
                    pass
        self.allow_auto_zero = True if parms['allow_auto_zero'] == '1' else False
        self.allow_auto_skew = True if parms['allow_auto_skew'] == '1' else False
        try:
            self.cal_avg_error = True if parms['cal_avg_error'] == '1' else False
            self.cal_x_error = True if parms['cal_x_error'] == '1' else False
            self.cal_y_error = True if parms['cal_y_error'] == '1' else False
        except:
            pass
        # adjust z_clearance data
        #self.data_z_clearance += self.data_extra_depth
        # clear all previous probe results
        for i in (self.status_list):
            self['status_' + i] = None

    def collect_status(self):
       try:
        tmpl = lambda s: self._format_template % s
        for key in self.status_list:
            if self['status_' + key] is None:
                data = 'None'
            else:
                data = tmpl(self['status_' + key])
            self.send_dict.update( {key: data} )
       except Exception as e:
        print('ERROR ',e)
       return

        #for key in self.status_list:
        #    data = "{:3.3f}".format(self['status_' + key])
        #    self.send_dict.update( {key: data} )
        #return

    # need to be in the right mode - entries are in machine units
    def prechecks(self):
        # This is a work around. If a user sets the spindle running in MDI
        # but turn it off with a manual button, then when M72 will turn the
        # spindle back on! So we explicitly set M5 here.
        ACTION.CALL_MDI('M5')
        # record current G,S,M codes
        ACTION.CALL_MDI('M70')

        # set proper mode based on what machine is based
        if INFO.MACHINE_IS_METRIC and STATUS.is_metric_mode():
            return None
        if not INFO.MACHINE_IS_METRIC and not STATUS.is_metric_mode():
            return None
        if INFO.MACHINE_IS_METRIC:
            ACTION.CALL_MDI('g21')
        else:
            ACTION.CALL_MDI('g20')
        return None

    # return to previous motion modes
    def postreset(self):
        ACTION.CALL_MDI('M72')

########################################
# required boiler code
########################################
    def __getitem__(self, item):
        return getattr(self, item)
    def __setitem__(self, item, value):
        return setattr(self, item, value)

####################################
# Testing
####################################
if __name__ == "__main__":
    w = ProbeSubprog()