forked from mxcube/HardwareObjects
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MicrodiffAperture.py
103 lines (88 loc) · 4.06 KB
/
MicrodiffAperture.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
from MD2Motor import MD2Motor
import logging
import math
class MicrodiffAperture(MD2Motor):
def __init__(self, name):
MD2Motor.__init__(self, name)
def init(self):
self.motor_name = "CurrentApertureDiameter"
self.motor_pos_attr_suffix = "Index"
MD2Motor.init(self)
self.aperture_inout = self.getObjectByRole('inout')
self.predefinedPositions = {}
self.labels = self.addChannel({"type":"exporter", "name":"ap_labels" }, "ApertureDiameters")
self.filters = self.labels.getValue()
self.nb = len(self.filters)
j = 0
while j < self.nb :
for i in self.filters:
if int(i) >= 300:
i = "Outbeam"
self.predefinedPositions[i] = j
j = j+1
if not "Outbeam" in self.predefinedPositions:
self.predefinedPositions["Outbeam"] = self.predefinedPositions.__len__()
self.predefinedPositions.pop("Outbeam")
self.sortPredefinedPositionsList()
def sortPredefinedPositionsList(self):
self.predefinedPositionsNamesList = self.predefinedPositions.keys()
self.predefinedPositionsNamesList.sort(lambda x, y: int(round(self.predefinedPositions[x] - self.predefinedPositions[y])))
def connectNotify(self, signal):
if signal == 'predefinedPositionChanged':
positionName = self.getCurrentPositionName()
try:
pos = self.predefinedPositions[positionName]
except KeyError:
self.emit(signal, ('', None))
else:
self.emit(signal, (positionName, pos))
elif signal == 'apertureChanged':
self.emit('apertureChanged', (self.getApertureSize(), ))
else:
return MD2Motor.connectNotify.im_func(self, signal)
def getLimits(self):
return (1,self.nb)
def getPredefinedPositionsList(self):
return self.predefinedPositionsNamesList
def motorPositionChanged(self, absolutePosition, private={}):
MD2Motor.motorPositionChanged.im_func(self, absolutePosition, private)
positionName = self.getCurrentPositionName(absolutePosition)
self.emit('predefinedPositionChanged', (positionName, positionName and absolutePosition or None, ))
self.emit('apertureChanged', (self.getApertureSize(), ))
def getCurrentPositionName(self, pos=None):
if self.getPosition() is not None:
pos = pos or self.getPosition()
else:
pos = pos
try:
for positionName in self.predefinedPositions:
if math.fabs(self.predefinedPositions[positionName] - pos) <= 1E-3:
return positionName
except:
return ''
def moveToPosition(self, positionName):
logging.getLogger().debug("%s: trying to move %s to %s:%f", self.name(), self.motor_name, positionName,self.predefinedPositions[positionName])
if positionName == 'Outbeam':
self.aperture_inout.actuatorOut()
else:
try:
self.move(self.predefinedPositions[positionName], wait=True, timeout=10)
except:
logging.getLogger("HWR").exception('Cannot move motor %s: invalid position name.', str(self.userName()))
if self.aperture_inout.getActuatorState() != 'in':
self.aperture_inout.actuatorIn()
def setNewPredefinedPosition(self, positionName, positionOffset):
raise NotImplementedError
def getApertureSize(self):
diameter_name = self.getCurrentPositionName()
for diameter in self["diameter"]:
if str(diameter.getProperty("name")) == str(diameter_name):
return (diameter.getProperty("size"),)*2
return (9999,9999)
def getApertureCoef(self):
diameter_name = self.getCurrentPositionName()
for diameter in self["diameter"]:
if str(diameter.getProperty("name")) == str(diameter_name):
aperture_coef = diameter.getProperty("aperture_coef")
return float(aperture_coef)
return 1