-
Notifications
You must be signed in to change notification settings - Fork 9
/
TwoAxisAlignmentBrick.py
339 lines (265 loc) · 13.6 KB
/
TwoAxisAlignmentBrick.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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
'''
Doc please...
'''
__version__ = 1.0
__author__ = "Matias Guijarro"
__category__ = 'Motor'
import logging
from qt import *
from BlissFramework.BaseComponents import BlissWidget
from BlissFramework import Icons
class TwoAxisAlignmentBrick(BlissWidget):
def __init__(self, *args):
"""
Constructor
"""
BlissWidget.__init__(self, *args)
self.verticalMotor = None
self.horizontalMotor = None
self.motorStatesColors = [self.colorGroup().background(),
self.colorGroup().background(),
Qt.green,
Qt.yellow,
Qt.yellow,
Qt.red]
# addProperty adds a property for the brick :
# - 1st argument is the name of the property ;
# - 2d argument is the type (one of string, integer, float, file, combo) ;
# - 3rd argument is the default value.
# In some cases (e.g combo), the third argument is a tuple of choices
# and the fourth argument is the default value.
# When a property is changed, the propertyChanged() method is called.
self.addProperty("mnemonic", "string", "")
self.addProperty("defaultStepSize", "float", 0.1)
self.addProperty("moveUpStepFactor", "combo", ("1", "-1"), "1")
self.addProperty("moveLeftStepFactor", "combo", ("1", "-1"), "-1")
self.addProperty("title", "string", "")
self.addProperty("formatString", "formatString", "###.##")
# now we can "draw" the brick, i.e creating GUI widgets
# and arranging them in a layout. It is the default appearance
# for the brick (if no property is set for example)
self.lblTitle = QLabel(self)
upBox = QWidget(self)
upBoxLayout = QVBoxLayout(upBox, 0, 0)
upBox.setSizePolicy(QSizePolicy.MinimumExpanding, QSizePolicy.Fixed)
self.lblUp = QLabel(upBox)
self.cmdUp = QPushButton(upBox)
self.lblUp.setFont(QFont("courier"))
# Icons is a Python module that comes with the Bliss Framework ;
# it can load an icon file from the Resource directory inside the
# BlissFramework package given its name (without extension).
self.cmdUp.setPixmap(Icons.load("up_small"))
upBoxLayout.addWidget(self.lblUp, 0, Qt.AlignCenter)
upBoxLayout.addWidget(self.cmdUp, 0)
downBox = QWidget(self)
downBox.setSizePolicy(QSizePolicy.MinimumExpanding, QSizePolicy.Fixed)
downBoxLayout = QVBoxLayout(downBox, 0, 0)
self.cmdDown = QPushButton(downBox)
self.cmdDown.setPixmap(Icons.load("down_small"))
self.lblDown = QLabel(downBox)
self.lblDown.setFont(QFont("courier"))
downBoxLayout.addWidget(self.cmdDown, 0)
downBoxLayout.addWidget(self.lblDown, 0, Qt.AlignCenter)
leftBox = QHBox(self)
leftBox.setSpacing(5)
leftBox.setSizePolicy(QSizePolicy.Fixed, QSizePolicy.MinimumExpanding)
self.lblLeft = QLabel(leftBox)
self.lblLeft.setAlignment(Qt.AlignCenter)
self.lblLeft.setFont(QFont("courier"))
self.cmdLeft = QPushButton(leftBox)
self.cmdLeft.setPixmap(Icons.load("left_small"))
self.cmdLeft.setSizePolicy(QSizePolicy.Fixed, QSizePolicy.Expanding)
rightBox = QHBox(self)
rightBox.setSpacing(5)
rightBox.setSizePolicy(QSizePolicy.Fixed, QSizePolicy.MinimumExpanding)
self.cmdRight = QPushButton(rightBox)
self.cmdRight.setPixmap(Icons.load("right_small"))
self.cmdRight.setSizePolicy(QSizePolicy.Fixed, QSizePolicy.Expanding)
self.lblRight = QLabel(rightBox)
self.lblRight.setAlignment(Qt.AlignCenter)
self.lblRight.setFont(QFont("courier"))
centralBox = QVBox(self)
centralBox.setSpacing(10)
centralBox.setMargin(10)
centralBox.setSizePolicy(QSizePolicy.Fixed, QSizePolicy.Fixed)
stepBox = QVBox(centralBox)
self.lblCurrentStep = QLabel("Current step : ?", stepBox)
self.cmdChangeStep = QToolButton(stepBox)
self.cmdChangeStep.setTextLabel("Change step")
self.cmdChangeStep.setUsesTextLabel(True)
self.cmdChangeStep.setIconSet(QIconSet(Icons.load("steps_small")))
self.cmdAbort = QToolButton(centralBox)
self.cmdAbort.setIconSet(QIconSet(Icons.load("stop_small")))
self.cmdAbort.setTextLabel("Abort")
self.cmdAbort.setUsesTextLabel(True)
QGridLayout(self, 4, 3, 5, 5)
self.layout().addMultiCellWidget(self.lblTitle, 0, 0, 0, 2, Qt.AlignHCenter)
self.layout().addWidget(upBox, 1, 1)
self.layout().addWidget(leftBox, 2, 0)
self.layout().addWidget(centralBox, 2, 1, Qt.AlignCenter)
self.layout().addWidget(rightBox, 2, 2)
self.layout().addWidget(downBox, 3, 1)
# establish connections between signals and slots
QObject.connect(self.cmdUp, SIGNAL("clicked()"), self.cmdUpClicked)
QObject.connect(self.cmdLeft, SIGNAL("clicked()"), self.cmdLeftClicked)
QObject.connect(self.cmdRight, SIGNAL("clicked()"), self.cmdRightClicked)
QObject.connect(self.cmdDown, SIGNAL("clicked()"), self.cmdDownClicked)
QObject.connect(self.cmdAbort, SIGNAL("clicked()"), self.cmdAbortClicked)
QObject.connect(self.cmdChangeStep, SIGNAL("clicked()"), self.cmdChangeStepClicked)
self.currentVerticalLabel = self.lblUp
self.currentHorizontalLabel = self.lblRight
self.cmdAbort.setEnabled(False)
def updateHMotorPosition(self, position=None):
if position is not None:
mne = self.horizontalMotor.userName()
pos = self["formatString"] % position
else:
mne = "h. motor"
pos = "?"
if self.horizontalMotor is not None:
mne = self.horizontalMotor.userName()
if self.horizontalMotor.isReady():
hpos = self.horizontalMotor.getPosition()
pos = self["formatString"] % hpos
for label in (self.lblLeft, self.lblRight):
length = max(len(mne), len(str(pos)))
label.setFixedSize(label.fontMetrics().width(length*"#"), 2*label.fontMetrics().height())
if label == self.currentHorizontalLabel:
label.setText('%s\n%s' % (mne, pos))
else:
label.setText("")
label.setPaletteBackgroundColor(self.colorGroup().background())
def updateVMotorPosition(self, position=None):
if position is not None:
mne = self.verticalMotor.userName()
pos = self["formatString"] % position
else:
mne = "v. motor"
pos = "?"
if self.verticalMotor is not None:
mne = self.verticalMotor.userName()
if self.verticalMotor.isReady():
vpos = self.verticalMotor.getPosition()
pos = self["formatString"] % vpos
for label in (self.lblUp, self.lblDown):
label.setFixedSize(label.fontMetrics().width(mne + " " + str(pos)), label.fontMetrics().height())
if label == self.currentVerticalLabel:
label.setText('%s %s' % (mne, pos))
else:
label.setText("")
label.setPaletteBackgroundColor(self.colorGroup().background())
def updateMotorPositions(self):
self.updateHMotorPosition()
self.updateVMotorPosition()
def propertyChanged(self, property, oldValue, newValue):
if property == 'mnemonic':
if self.verticalMotor is not None and self.horizontalMotor is not None:
# remove connections previously established
self.disconnect(self.verticalMotor, PYSIGNAL("stateChanged"), self.updateVMotorState)
self.disconnect(self.horizontalMotor, PYSIGNAL("stateChanged"), self.updateHMotorState)
self.disconnect(self.verticalMotor, PYSIGNAL("positionChanged"), self.updateVMotorPosition)
self.disconnect(self.horizontalMotor, PYSIGNAL("positionChanged"), self.updateHMotorPosition)
equipment = self.getHardwareObject(newValue)
if equipment is not None:
try:
self.verticalMotor = equipment.getDeviceByRole("vertical")
self.horizontalMotor = equipment.getDeviceByRole("horizontal")
except:
logging.getLogger().error("%s: could not find vertical and horizontal motors in Hardware Object %s", str(self.name()), equipment.name())
else:
if self.verticalMotor is not None and self.horizontalMotor is not None:
self.connect(self.verticalMotor, PYSIGNAL("stateChanged"), self.updateVMotorState)
self.connect(self.horizontalMotor, PYSIGNAL("stateChanged"), self.updateHMotorState)
self.connect(self.verticalMotor, PYSIGNAL("positionChanged"), self.updateVMotorPosition)
self.connect(self.horizontalMotor, PYSIGNAL("positionChanged"), self.updateHMotorPosition)
self.currentVerticalLabel = self.lblUp
self.currentHorizontalLabel = self.lblRight
# refresh labels
self["formatString"] = self.getProperty("formatString").getUserValue()
else:
logging.getLogger().error("%s: invalid vertical/horizontal motors in Hardware Object %s", str(self.name()), equipment.name())
self.updateMotorPositions()
self.updateMotorStates()
elif property == 'defaultStepSize':
self.lblCurrentStep.setText("Current step : %s" % (newValue or "?"))
elif property == 'title':
self.lblTitle.setText(newValue)
elif property == 'formatString':
self.updateMotorPositions()
def setAbortState(self):
for motor in (self.horizontalMotor, self.verticalMotor):
if motor is not None and motor.isReady() and motor.getState() == motor.MOVING:
self.cmdAbort.setEnabled(True)
return
self.cmdAbort.setEnabled(False)
def updateMotorStates(self):
self.updateVMotorState()
self.updateHMotorState()
def updateVMotorState(self, state=None):
if state is None:
state = 0
enabled = False
if self.verticalMotor is not None:
if self.verticalMotor.isReady():
state = self.verticalMotor.getState()
enabled = state > self.verticalMotor.UNUSABLE
else:
enabled = state > self.verticalMotor.UNUSABLE
color = self.motorStatesColors[state]
self.cmdDown.setEnabled(enabled)
self.cmdUp.setEnabled(enabled)
for label in (self.lblDown, self.lblUp):
label.setEnabled(enabled)
if label == self.currentVerticalLabel:
label.setPaletteBackgroundColor(color)
else:
label.setPaletteBackgroundColor(label.colorGroup().background())
label.setText("")
self.updateVMotorPosition()
self.setAbortState()
def updateHMotorState(self, state=None):
if state is None:
state = 0
enabled = False
if self.horizontalMotor is not None:
if self.horizontalMotor.isReady():
state = self.horizontalMotor.getState()
enabled = state > self.horizontalMotor.UNUSABLE
else:
enabled = state > self.horizontalMotor.UNUSABLE
color = self.motorStatesColors[state]
self.cmdLeft.setEnabled(enabled)
self.cmdRight.setEnabled(enabled)
for label in (self.lblLeft, self.lblRight):
label.setEnabled(enabled)
if label == self.currentHorizontalLabel:
label.setPaletteBackgroundColor(color)
else:
label.setPaletteBackgroundColor(label.colorGroup().background())
label.setText("")
self.updateHMotorPosition()
self.setAbortState()
def cmdUpClicked(self):
self.currentVerticalLabel = self.lblUp
stepFactor = int(self["moveUpStepFactor"])
self.verticalMotor.moveRelative(self["defaultStepSize"]*stepFactor)
def cmdDownClicked(self):
self.currentVerticalLabel = self.lblDown
stepFactor = int(self["moveUpStepFactor"])
self.verticalMotor.moveRelative(-self["defaultStepSize"]*stepFactor)
def cmdLeftClicked(self):
self.currentHorizontalLabel = self.lblLeft
stepFactor = int(self["moveLeftStepFactor"])
self.horizontalMotor.moveRelative(self["defaultStepSize"]*stepFactor)
def cmdRightClicked(self):
self.currentHorizontalLabel = self.lblRight
stepFactor = int(self["moveLeftStepFactor"])
self.horizontalMotor.moveRelative(-self["defaultStepSize"]*stepFactor)
def cmdChangeStepClicked(self):
newStep, ok = QInputDialog.getDouble("New step :", "Change step", self["defaultStepSize"], 0.00001, 100000, 5)
if ok:
self["defaultStepSize"] = newStep
def cmdAbortClicked(self):
for motor in (self.verticalMotor, self.horizontalMotor):
if motor.getState() == motor.MOVING:
motor.stop()