File: axis_tool_button.py

package info (click to toggle)
linuxcnc 1%3A2.9.7-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 285,604 kB
  • sloc: python: 202,568; ansic: 109,036; cpp: 99,239; tcl: 16,054; xml: 10,631; sh: 10,303; makefile: 1,255; javascript: 138; sql: 72; asm: 15
file content (300 lines) | stat: -rw-r--r-- 11,942 bytes parent folder | download | duplicates (3)
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
#!/usr/bin/env python3
#
# QTVcp Widget
# Copyright (c) 2018 Chris Morley
#
# 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.
###############################################################################

import hal

from PyQt5.QtWidgets import QToolButton, QMenu, QAction
from PyQt5.QtCore import pyqtProperty
from PyQt5.QtGui import QIcon

from qtvcp.widgets.widget_baseclass import _HalWidgetBase
from qtvcp.core import Status, Action, Info
from qtvcp import logger

# Instiniate the libraries with global reference
# STATUS gives us status messages from linuxcnc
# AUX_PRGM holds helper program loader
# INI holds ini details
# ACTION gives commands to linuxcnc
# LOG is for running code logging
STATUS = Status()
INFO = Info()
ACTION = Action()
LOG = logger.getLogger(__name__)
# Force the log level for this module
#LOG.setLevel(logger.DEBUG) # One of DEBUG, INFO, WARNING, ERROR, CRITICAL

class AxisToolButton(QToolButton, _HalWidgetBase):
    def __init__(self, parent=None):
        super(AxisToolButton, self).__init__(parent)
        self._joint = 0
        self._axis = ''
        self._last = 0
        self._block_signal = False
        self._halpin_option = True
        self.dialog_code = 'ENTRY'
        self.display_units_mm = 0
        homeOption = False

        SettingMenu = QMenu(self)
        self.settingMenu = SettingMenu
        self.zeroButton = QAction(QIcon('exit24.png'), 'Zero', self)
        self.zeroButton.triggered.connect(self.Zero)
        SettingMenu.addAction(self.zeroButton)
        self.setButton = QAction(QIcon('exit24.png'), 'Set', self)
        self.setButton.triggered.connect(self.SetOrigin)
        SettingMenu.addAction(self.setButton)
        self.divideButton = QAction(QIcon('exit24.png'), 'Divide By 2', self)
        self.divideButton.triggered.connect(self.Divide)
        SettingMenu.addAction(self.divideButton)
        self.lastButton = QAction(QIcon('exit24.png'), 'Set To Last', self)
        self.lastButton.triggered.connect(self.Last)
        SettingMenu.addAction(self.lastButton)

        # option for later - should put a sub menu in for joints homing though
        if homeOption:
            SettingMenu.addSeparator()
            self.homeButton = QAction(QIcon('exit24.png'), 'Home', self)
            self.homeButton.triggered.connect(self.Home)
            SettingMenu.addAction(self.homeButton)
            #self.homeButton.setEnabled(False)

            self.UnHomeButton = QAction(QIcon('exit24.png'), 'Unhome', self)
            self.UnHomeButton.triggered.connect(self.Unhome)
            SettingMenu.addAction(self.UnHomeButton)
            #self.homeButton.setEnabled(False)

        self.setMenu(SettingMenu)
        self.clicked.connect(self.selectJoint)


    def _hal_init(self):
        def homed_on_test():
            return (STATUS.machine_is_on()
                    and (STATUS.is_all_homed() or INFO.NO_HOME_REQUIRED))

        STATUS.connect('metric-mode-changed', self._switch_units)
        STATUS.connect('state-off', lambda w: self.settingMenu.setEnabled(False))
        STATUS.connect('state-on', lambda w: self.settingMenu.setEnabled(True))
        STATUS.connect('interp-idle', lambda w: self._enableGroup(homed_on_test()))
        STATUS.connect('interp-run', lambda w: self._enableGroup(False))
        STATUS.connect('all-homed', lambda w: self._enableGroup(homed_on_test()))
        STATUS.connect('not-all-homed', lambda w, data: self._enableGroup(False))
        STATUS.connect('interp-paused', lambda w: self._enableGroup(False))
        STATUS.connect('motion-mode-changed', lambda w,data: self.modeChanged(data))
        STATUS.connect('joint-selection-changed', lambda w,data: self.ChangeState(joint = data, axis= STATUS.get_selected_axis()))
        STATUS.connect('axis-selection-changed', lambda w,data: self.ChangeState(joint = STATUS.get_selected_joint(), axis = data))
        if self._halpin_option and self._joint != -1:
            self.hal_pin_joint = self.HAL_GCOMP_.newpin(str(self.HAL_NAME_ + '-joint'), hal.HAL_BIT, hal.HAL_OUT)
            self.hal_pin_axis = self.HAL_GCOMP_.newpin(str(self.HAL_NAME_ + '-axis'), hal.HAL_BIT, hal.HAL_OUT)
        STATUS.connect('general',self.return_value)

    def _enableGroup(self, state):
        for i in(self.zeroButton, self.setButton,self.divideButton,
                self.lastButton, ):
            i.setEnabled(state)
        self.setEnabled(state)

    def Zero(self):
        axis, now = self._a_from_j(self._axis)
        if axis:
            self._last = now
            ACTION.SET_AXIS_ORIGIN(axis, 0)
            STATUS.emit('update-machine-log', 'Zeroed Axis %s' % axis, 'TIME')

    def SetOrigin(self):
        axis, now = self._a_from_j(self._axis)
        if axis:
            mess = {'NAME':self.dialog_code,'ID':'%s__' % self.objectName(),
            'AXIS':axis,'CURRENT':now,
            'TITLE':'Set %s Origin'% axis,
            'GEONAME':'axisToolButtonDialog_{}'.format(self.dialog_code),
            'AXIS':axis}
            STATUS.emit('dialog-request', mess)
            LOG.debug('message sent:{}'.format (mess))

    # process the STATUS return message
    def return_value(self, w, message):
        num = message['RETURN']
        code = bool(message.get('ID') == '%s__'% self.objectName())
        name = bool(message.get('NAME') == self.dialog_code)
        if code and name and num is not None:
            LOG.debug('message return:{}'.format (message))
            axis = message.get('AXIS')
            self._last = message.get('CURRENT')
            ACTION.SET_AXIS_ORIGIN(axis, num)
            STATUS.emit('update-machine-log', 'Set Origin of Axis %s to %f' %(axis, num), 'TIME')

    def Divide(self):
        axis, now = self._a_from_j(self._axis)
        if axis:
            self._last = now
            try:
                x = now/2.0
                ACTION.SET_AXIS_ORIGIN(axis, x)
                text = 'Divided Axis %s in Half - %f'% (axis, x)
                STATUS.emit('update-machine-log', text, 'TIME')
            except ZeroDivisionError:
                pass

    def Last(self):
        axis, now = self._a_from_j(self._axis)
        if axis:
            ACTION.SET_AXIS_ORIGIN(axis, self._last)
            text = 'Reset Axis %s from %f to Last Value: %f' %(axis, now, self._last)
            STATUS.emit('update-machine-log', text, 'TIME')
            self._last = now

    def Home(self):
        #axis, now = self._a_from_j(self._axis)
        #if axis:
            ACTION.SET_MACHINE_HOMING(self._joint)
            STATUS.emit('update-machine-log', 'Homed Axis %s' % self._joint, 'TIME')

    def Unhome(self):
        #axis, now = self._a_from_j(self._axis)
        #if axis:
            ACTION.SET_MACHINE_UNHOMED(self._joint)
            STATUS.emit('update-machine-log', 'Unhomed Axis %s' % self._joint, 'TIME')


    def _a_from_j(self, axis):
        if STATUS.is_joint_mode():
            return None, None
        if axis == '':
            axis = STATUS.get_selected_axis()
        j = "XYZABCUVW"
        try:
            jnum = j.find(axis)
        except:
            return None, None
        p,r,d = STATUS.get_position()
        if self.display_units_mm != INFO.MACHINE_IS_METRIC:
            r = INFO.convert_units_9(r)
        return axis, r[jnum]

    def selectJoint(self):
        if self._block_signal or self._joint == -1 or self._axis == '': return
        if self.isChecked() == True:
            if STATUS.is_joint_mode():
                ACTION.SET_SELECTED_JOINT(self._joint)
                if self._halpin_option:
                    self.hal_pin_joint.set(self.isChecked())
            else:
                ACTION.SET_SELECTED_AXIS(self._axis)
                # set this without causing a STATUS message output
                # in case we are selecting an axis to un/home
                STATUS.selected_joint = self._joint
                if self._halpin_option:
                    self.hal_pin_axis.set(self.isChecked())
        else:
            if STATUS.is_joint_mode():
                ACTION.SET_SELECTED_JOINT(-1)
            else:
                ACTION.SET_SELECTED_AXIS('None')
                # set this without causing a STATUS message output
                # in case we are selecting an axis to un/home
                STATUS.selected_joint = -1
            if self._halpin_option:
                self.hal_pin_axis.set(False)

    def ChangeState(self, joint = None, axis = None):
        if STATUS.is_joint_mode():
            if int(joint) != self._joint:
                self._block_signal = True
                self.setChecked(False)
                self._block_signal = False
                if self._halpin_option and self._joint != -1:
                    self.hal_pin_joint.set(False)
        else:
            if str(axis) != self._axis:
                self._block_signal = True
                self.setChecked(False)
                self._block_signal = False
                if self._halpin_option and self._axis != '':
                    self.hal_pin_joint.set(False)

    def modeChanged(self, mode):
        self.selectJoint()
        return
        if mode == STATUS.linuxcnc.TRAJ_MODE_FREE:
            ACTION.SET_SELECTED_JOINT(self._joint)
        else:
            ACTION.SET_SELECTED_AXIS(self._axis)

    def _switch_units(self, widget, data):
        self.display_units_mm = data
        if self.display_units_mm != INFO.MACHINE_IS_METRIC:
            self._last = INFO.convert_units(self._last)

    #########################################################################
    # This is how designer can interact with our widget properties.
    # designer will show the pyqtProperty properties in the editor
    # it will use the get set and reset calls to do those actions
    #
    ########################################################################

    def set_joint(self, data):
        self._joint = data
        if data == -1:
            self.setPopupMode(QToolButton.InstantPopup)
    def get_joint(self):
        return self._joint
    def reset_joint(self):
        self._joint = -1
    joint_number = pyqtProperty(int, get_joint, set_joint, reset_joint)

    def set_axis(self, data):
        if data.upper() in('X','Y','Z','A','B','C','U','V','W'):
            self._axis = str(data.upper())
    def get_axis(self):
        return self._axis
    def reset_axis(self):
        self._axis = 'X'
    axis_letter = pyqtProperty(str, get_axis, set_axis, reset_axis)

    def set_halpin_option(self, value):
        self._halpin_option = value
    def get_halpin_option(self):
        return self._halpin_option
    def reset_halpin_option(self):
        self._halpin_option = True
    halpin_option = pyqtProperty(bool, get_halpin_option, set_halpin_option, reset_halpin_option)

    def set_dialog_code(self, data):
        self.dialog_code = data
    def get_dialog_code(self):
        return self.dialog_code
    def reset_dialog_code(self):
        self.dialog_code = 'ENTRY'
    dialog_code_string = pyqtProperty(str, get_dialog_code, set_dialog_code, reset_dialog_code)

    def __getitem__(self, item):
        return getattr(self, item)
    def __setitem__(self, item, value):
        return setattr(self, item, value)

# for testing without editor:
def main():
    import sys
    from PyQt5.QtWidgets import QApplication
    app = QApplication(sys.argv)
    widget = AxisToolButton()
    widget.show()

    sys.exit(app.exec_())
if __name__ == "__main__":
    main()