forked from mxcube/HardwareObjects
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Resolution.py
165 lines (129 loc) · 5.27 KB
/
Resolution.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
from AbstractMotor import AbstractMotor
from HardwareRepository import BaseHardwareObjects
import logging
import math
class Resolution(AbstractMotor, BaseHardwareObjects.HardwareObject):
def __init__(self, *args, **kwargs):
AbstractMotor.__init__(self)
BaseHardwareObjects.HardwareObject.__init__(self, *args, **kwargs)
#self.get_value = self.getPosition
self.valid = True
def init(self):
self.currentResolution = None
self.energy = None
self.dtox = self.getObjectByRole("dtox")
self.energy = self.getObjectByRole("energy")
self.detector = self.getObjectByRole("detector")
if self.detector:
self.det_width = float(self.detector.getProperty('width'))
self.det_height = float(self.detector.getProperty('height'))
else:
self.valid = False
logging.getLogger().exception('Cannot get detector properties')
raise AttributeError("Cannot get detector properties")
self.connect(self.dtox, "stateChanged", self.dtoxStateChanged)
self.connect(self.dtox, "positionChanged", self.dtoxPositionChanged)
self.connect(self.energy, "valueChanged", self.energyChanged)
def isReady(self):
if self.valid:
try:
return self.dtox.isReady()
except:
return False
return False
def get_beam_centre(self, dtox=None):
if dtox is None:
dtox = self.dtox.getPosition()
ax = float(self.detector['beam'].getProperty('ax'))
bx = float(self.detector['beam'].getProperty('bx'))
ay = float(self.detector['beam'].getProperty('ay'))
by = float(self.detector['beam'].getProperty('by'))
return float(dtox*ax+bx), float(dtox*ay+by)
def update_beam_centre(self, dtox):
beam_x, beam_y = self.get_beam_centre(dtox)
self.det_radius = min(self.det_width - beam_x, self.det_height - beam_y, beam_x, beam_y)
def getWavelength(self):
try:
return self.energy.getCurrentWavelength()
except:
current_en = self.energy.getPosition()
if current_en:
return (12.3984/current_en)
return None
def energyChanged(self, egy):
return self.recalculateResolution()
def res2dist(self, res=None):
current_wavelength = self.getWavelength()
if res is None:
res = self.currentResolution
try:
ttheta = 2*math.asin(current_wavelength / (2*res))
return self.det_radius / math.tan(ttheta)
except:
return None
def _calc_res(self, radius, dist):
current_wavelength = self.getWavelength()
try:
ttheta = math.atan(radius / dist)
if ttheta != 0:
return current_wavelength / (2*math.sin(ttheta/2))
else:
return None
except:
logging.getLogger().exception("error while calculating resolution")
return None
def dist2res(self, dist=None):
if dist is None:
dist = self.dtox.getPosition()
return self._calc_res(self.det_radius, dist)
def recalculateResolution(self):
dtox_pos = self.dtox.getPosition()
self.update_beam_centre(dtox_pos)
new_res = self.dist2res(dtox_pos)
if new_res is None:
return
self.update_resolution(new_res)
def getPosition(self):
if self.currentResolution is None:
self.recalculateResolution()
return self.currentResolution
def get_value_at_corner(self):
dtox_pos = self.dtox.getPosition()
beam_x, beam_y = self.get_beam_centre(dtox_pos)
distance_at_corners = [math.sqrt(beam_x**2+beam_y**2),
math.sqrt((self.det_width-beam_x)**2+beam_y**2),
math.sqrt((beam_x**2+(self.det_height-beam_y)**2)),
math.sqrt((self.det_width-beam_x)**2+(self.det_height-beam_y)**2)]
return self._calc_res(max(distance_at_corners), dtox_pos)
def update_resolution(self, res):
self.currentResolution = res
self.emit("positionChanged", (res, ))
self.emit('valueChanged', (res, ))
def getState(self):
return self.dtox.getState()
def connectNotify(self, signal):
if signal == "stateChanged":
self.dtoxStateChanged(self.dtox.getState())
def dtoxStateChanged(self, state):
self.emit("stateChanged", (state, ))
if state == self.dtox.READY:
self.recalculateResolution()
def dtoxPositionChanged(self, pos):
self.update_beam_centre(pos)
self.update_resolution(self.dist2res(pos))
def getLimits(self):
low, high = self.dtox.getLimits()
return (self.dist2res(low), self.dist2res(high))
def move(self, pos, wait=False):
logging.getLogger().info("move Resolution to %s", pos)
if wait:
self.dtox.syncMove(self.res2dist(pos))
else:
self.dtox.move(self.res2dist(pos))
def motorIsMoving(self):
return self.dtox.motorIsMoving()
def stop(self):
try:
self.dtox.stop()
except:
pass