-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathMicrodiff.py
314 lines (264 loc) · 15.2 KB
/
Microdiff.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
from HardwareRepository.BaseHardwareObjects import Equipment
import tempfile
import logging
import math
import os
import time
from HardwareRepository import HardwareRepository
import MiniDiff
from HardwareRepository import EnhancedPopen
import copy
import gevent
import sample_centring
MICRODIFF = None
class Microdiff(MiniDiff.MiniDiff):
def init(self):
global MICRODIFF
MICRODIFF = self
self.timeout = 3
self.phiMotor = self.getDeviceByRole('phi')
self.exporter_addr = self.phiMotor.exporter_address
self.x_calib = self.addChannel({ "type":"exporter", "exporter_address": self.exporter_addr, "name":"x_calib" }, "CoaxCamScaleX")
self.y_calib = self.addChannel({ "type":"exporter", "exporter_address": self.exporter_addr, "name":"y_calib" }, "CoaxCamScaleY")
self.moveMultipleMotors = self.addCommand({"type":"exporter", "exporter_address":self.exporter_addr, "name":"move_multiple_motors" }, "SyncMoveMotors")
self.head_type = self.addChannel({ "type":"exporter", "exporter_address": self.exporter_addr, "name":"head_type" }, "HeadType")
self.kappa = self.addChannel({ "type":"exporter", "exporter_address": self.exporter_addr, "name":"kappa_enable" }, "KappaIsEnabled")
self.phases = {"Centring":1, "BeamLocation":2, "DataCollection":3, "Transfer":4}
self.movePhase = self.addCommand({"type":"exporter", "exporter_address":self.exporter_addr, "name":"move_to_phase" }, "startSetPhase")
self.readPhase = self.addChannel({ "type":"exporter", "exporter_address": self.exporter_addr, "name":"read_phase" }, "CurrentPhase")
self.scanLimits = self.addCommand({"type":"exporter", "exporter_address":self.exporter_addr, "name":"scan_limits" }, "getOmegaMotorDynamicScanLimits")
if self.getProperty("use_hwstate"):
self.hwstate_attr = self.addChannel({"type":"exporter", "exporter_address": self.exporter_addr, "name":"hwstate" }, "HardwareState")
else:
self.hwstate_attr = None
self.swstate_attr = self.addChannel({"type":"exporter", "exporter_address": self.exporter_addr, "name":"swstate" }, "State")
self.nb_frames = self.addChannel({"type":"exporter", "exporter_address": self.exporter_addr, "name":"nbframes" }, "ScanNumberOfFrames")
# raster scan attributes
self.scan_range = self.addChannel({"type":"exporter", "exporter_address": self.exporter_addr, "name":"scan_range" }, "ScanRange")
self.scan_exposure_time = self.addChannel({"type":"exporter", "exporter_address": self.exporter_addr, "name":"exposure_time" }, "ScanExposureTime")
self.scan_start_angle = self.addChannel({"type":"exporter", "exporter_address": self.exporter_addr, "name":"start_angle" }, "ScanStartAngle")
self.scan_detector_gate_pulse_enabled = self.addChannel({"type":"exporter", "exporter_address": self.exporter_addr, "name":"detector_gate_pulse_enabled" }, "DetectorGatePulseEnabled")
self.scan_detector_gate_pulse_readout_time = self.addChannel({"type":"exporter", "exporter_address": self.exporter_addr, "name":"detector_gate_pulse_readout_time" }, "DetectorGatePulseReadoutTime")
MiniDiff.MiniDiff.init(self)
self.centringPhiy.direction = -1
self.MOTOR_TO_EXPORTER_NAME = self.getMotorToExporterNames()
self.move_to_coord = self.moveToBeam
self.centringVertical = self.getDeviceByRole('centringVertical')
self.centringFocus = self.getDeviceByRole('centringFocus')
self.frontLight = self.getDeviceByRole('flight')
self.backLight = self.getDeviceByRole('light')
self.beam_info = self.getObjectByRole('beam_info')
def getMotorToExporterNames(self):
#only temporary. Get the names from the xml files
MOTOR_TO_EXPORTER_NAME = {"focus":"AlignmentX", "kappa":"Kappa",
"kappa_phi":"Phi", "phi": "Omega",
"phiy":"AlignmentY", "phiz":"AlignmentZ",
"sampx":"CentringX", "sampy":"CentringY",
"zoom":"Zoom"}
return MOTOR_TO_EXPORTER_NAME
def getCalibrationData(self, offset):
return (1.0/self.x_calib.getValue(), 1.0/self.y_calib.getValue())
def emitCentringSuccessful(self):
#check first if all the motors have stopped
self._wait_ready(10)
# save position in MD2 software
self.getCommandObject("save_centring_positions")()
# do normal stuff
return MiniDiff.MiniDiff.emitCentringSuccessful(self)
def _ready(self):
if self.hwstate_attr:
if self.hwstate_attr.getValue() == "Ready" and self.swstate_attr.getValue() == "Ready":
return True
else:
if self.swstate_attr.getValue() == "Ready":
return True
return False
def _wait_ready(self, timeout=None):
if timeout <= 0:
timeout = self.timeout
tt1 = time.time()
while time.time() - tt1 < timeout:
if self._ready():
break
else:
time.sleep(0.5)
def moveToPhase(self, phase, wait=False, timeout=None):
if self._ready():
if self.phases.has_key(phase):
self.movePhase(phase)
if wait:
if not timeout:
timeout = 40
self._wait_ready(timeout)
else:
print "moveToPhase - Ready is: ", self._ready()
def getPhase(self):
return self.readPhase.getValue()
def moveSyncMotors(self, motors_dict, wait=False, timeout=None):
argin = ""
#print "start moving motors =============", time.time()
for motor in motors_dict.keys():
position = motors_dict[motor]
if position is None:
continue
name=self.MOTOR_TO_EXPORTER_NAME[motor]
argin += "%s=%0.3f;" % (name, position)
if not argin:
return
move_sync_motors = self.addCommand({"type":"exporter", "exporter_address":self.exporter_addr, "name":"move_sync_motors" }, "startSimultaneousMoveMotors")
move_sync_motors(argin)
if wait:
while not self._ready():
time.sleep(0.5)
#print "end moving motors =============", time.time()
def oscilScan(self, start, end, exptime, wait=False):
if self.in_plate_mode():
scan_speed = math.fabs(end-start) / exptime
low_lim, hi_lim = map(float, self.scanLimits(scan_speed))
if start < low_lim:
raise ValueError("Scan start below the allowed value %f" % low_lim)
elif end > hi_lim:
raise ValueError("Scan end abobe the allowed value %f" % hi_lim)
self.nb_frames.setValue(1)
scan_params = "1\t%0.3f\t%0.3f\t%0.4f\t1"% (start, (end-start), exptime)
scan = self.addCommand({"type":"exporter", "exporter_address":self.exporter_addr, "name":"start_scan" }, "startScanEx")
scan(scan_params)
print "scan started at ----------->", time.time()
if wait:
self._wait_ready(300) #timeout of 5 min
print "finished at ---------->", time.time()
def oscilScan4d(self, start, end, exptime, motors_pos, wait=False):
if self.in_plate_mode():
scan_speed = math.fabs(end-start) / exptime
low_lim, hi_lim = map(float, self.scanLimits(scan_speed))
if start < low_lim:
raise ValueError("Scan start below the allowed value %f" % low_lim)
elif end > hi_lim:
raise ValueError("Scan end abobe the allowed value %f" % hi_lim)
self.nb_frames.setValue(1)
scan_params = "%0.3f\t%0.3f\t%f\t"% (start, (end-start), exptime)
scan_params += "%0.3f\t" % motors_pos['1']['phiy']
scan_params += "%0.3f\t" % motors_pos['1']['phiz']
scan_params += "%0.3f\t" % motors_pos['1']['sampx']
scan_params += "%0.3f\t" % motors_pos['1']['sampy']
scan_params += "%0.3f\t" % motors_pos['2']['phiy']
scan_params += "%0.3f\t" % motors_pos['2']['phiz']
scan_params += "%0.3f\t" % motors_pos['2']['sampx']
scan_params += "%0.3f" % motors_pos['2']['sampy']
scan = self.addCommand({"type":"exporter", "exporter_address":self.exporter_addr, "name":"start_scan4d" }, "startScan4DEx")
scan(scan_params)
print "scan started at ----------->", time.time()
if wait:
self._wait_ready(900) #timeout of 15 min
print "finished at ---------->", time.time()
def oscilScanMesh(self,start, end, exptime, dead_time, mesh_num_lines, mesh_total_nb_frames, mesh_center, mesh_range, wait=False):
#import pdb; pdb.set_trace()
self.scan_range.setValue(end-start)
self.scan_exposure_time.setValue(exptime/mesh_num_lines)
self.scan_start_angle.setValue(start)
self.scan_detector_gate_pulse_enabled.setValue(True)
servo_time = 0.110 # adding the servo time to the readout time to avoid any servo cycle jitter
self.scan_detector_gate_pulse_readout_time.setValue(dead_time*1000 +servo_time) # TODO
# Prepositionning at the center of the grid
self.moveMotors(mesh_center.as_dict())
self.centringVertical.syncMoveRelative((mesh_range['vertical_range'])/2)
self.centringPhiy.syncMoveRelative(-(mesh_range['horizontal_range'])/2)
scan_params = "%0.3f\t" % mesh_range['vertical_range']
scan_params += "%0.3f\t" % -mesh_range['horizontal_range']
scan_params += "%d\t" % mesh_num_lines
scan_params += "%d\t" % (mesh_total_nb_frames/mesh_num_lines)
#scan_params += "%d\t" % 1
scan_params += "%r" % True # TODO
scan = self.addCommand({"type":"exporter", "exporter_address":self.exporter_addr, "name":"start_raster_scan" }, "startRasterScan")
scan(scan_params)
print "scan started at ----------->", time.time()
if wait:
self._wait_ready(1800) #timeout of 30 min
print "finished at ---------->", time.time()
def in_plate_mode(self):
try:
return self.head_type.getValue() == "Plate"
except:
return False
def in_kappa_mode(self):
return self.head_type.getValue() == "MiniKappa" and self.kappa.getValue()
def getPositions(self):
pos = { "phi": float(self.phiMotor.getPosition()),
"focus": float(self.focusMotor.getPosition()),
"phiy": float(self.phiyMotor.getPosition()),
"phiz": float(self.phizMotor.getPosition()),
"sampx": float(self.sampleXMotor.getPosition()),
"sampy": float(self.sampleYMotor.getPosition()),
"zoom": float(self.zoomMotor.getPosition()),
"kappa": float(self.sampleYMotor.getPosition()) if self.in_kappa_mode() else None,
"kappa_phi": float(self.zoomMotor.getPosition()) if self.in_kappa_mode() else None,
}
if self.in_kappa_mode() == True:
try:
kappa = float(self.kappaMotor.getPosition())
except:
kappa = 0.
try:
kappa_phi = float(self.kappaPhiMotor.getPosition())
except:
kappa_phi = 0.
pos.update({"kappa": kappa, "kappa_phi": kappa_phi})
return pos
def moveMotors(self, roles_positions_dict):
self.moveSyncMotors(roles_positions_dict, wait=True)
def moveToBeam(self, x, y):
if not self.in_plate_mode():
super(Microdiff,this).moveToBeam(x,y)
else:
try:
beam_xc = self.getBeamPosX()
beam_yc = self.getBeamPosY()
self.centringVertical.moveRelative((y-beam_yc)/float(self.pixelsPerMmZ))
self.centringPhiy.moveRelative(-(x-beam_xc)/float(self.pixelsPerMmY))
except:
logging.getLogger("user_level_log").exception("Microdiff: could not move to beam, aborting")
def start3ClickCentring(self, sample_info=None):
if self.in_plate_mode():
plateTranslation = self.getDeviceByRole('plateTranslation')
cmd_set_plate_vertical = self.addCommand({"type":"exporter", "exporter_address":self.exporter_addr, "name":"plate_vertical" }, "setPlateVertical")
low_lim, high_lim = self.phiMotor.getDynamicLimits()
phi_range = math.fabs(high_lim - low_lim -1)
self.currentCentringProcedure = sample_centring.start_plate_1_click({"phi":self.centringPhi,
"phiy":self.centringPhiy,
"sampx": self.centringSamplex,
"sampy": self.centringSampley,
"phiz": self.centringVertical,
"plateTranslation": plateTranslation},
self.pixelsPerMmY, self.pixelsPerMmZ,
self.getBeamPosX(), self.getBeamPosY(),
cmd_set_plate_vertical,
low_lim+0.5, high_lim-0.5)
else:
self.currentCentringProcedure = sample_centring.start({"phi":self.centringPhi,
"phiy":self.centringPhiy,
"sampx": self.centringSamplex,
"sampy": self.centringSampley,
"phiz": self.centringPhiz },
self.pixelsPerMmY, self.pixelsPerMmZ,
self.getBeamPosX(), self.getBeamPosY())
self.currentCentringProcedure.link(self.manualCentringDone)
def interruptAndAcceptCentring(self):
""" Used when plate. Kills the current 1 click centring infinite loop
and accepts fake centring - only save the motor positions
"""
self.currentCentringProcedure.kill()
self.do_centring = False
self.startCentringMethod(self,self.MANUAL3CLICK_MODE)
self.do_centring = True
def getFrontLightLevel(self):
return self.frontLight.getPosition()
def setFrontLightLevel(self, level):
return self.frontLight.move(level)
def getBackLightLevel(self):
return self.backLight.getPosition()
def setBackLightLevel(self, level):
return self.backLight.move(level)
def set_light_in(light, light_motor, zoom):
MICRODIFF.getDeviceByRole("flight").move(0)
MICRODIFF.getDeviceByRole("lightInOut").actuatorIn()
MiniDiff.set_light_in = set_light_in