-
Notifications
You must be signed in to change notification settings - Fork 11
/
GenericDiffractometer.py
1122 lines (978 loc) · 39.5 KB
/
GenericDiffractometer.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
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#
# Project: MXCuBE
# https://github.com/mxcube.
#
# This file is part of MXCuBE software.
#
# MXCuBE 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 3 of the License, or
# (at your option) any later version.
#
# MXCuBE 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.
#
# You should have received a copy of the GNU General Public License
# along with MXCuBE. If not, see <http://www.gnu.org/licenses/>.
"""
GenericDiffractometer
"""
import copy
import time
import gevent
import logging
import sample_centring
import math,numpy
import queue_model_objects_v1 as queue_model_objects
from HardwareRepository.TaskUtils import *
from HardwareRepository.BaseHardwareObjects import HardwareObject
__credits__ = ["MXCuBE colaboration"]
__version__ = "2.2."
__status__ = "Draft"
class DiffractometerState:
"""
Enumeration of diffractometer states
"""
Created = 0
Initializing = 1
On = 2
Off = 3
Closed = 4
Open = 5
Ready = 6
Busy = 7
Moving = 8
Standby = 9
Running = 10
Started = 11
Stopped = 12
Paused = 13
Remote = 14
Reset = 15
Closing = 16
Disable = 17
Waiting = 18
Positioned = 19
Starting = 20
Loading = 21
Unknown = 22
Alarm = 23
Fault = 24
Invalid = 25
Offline = 26
STATE_DESC = {Created: "Created",
Initializing: "Initializing",
On: "On",
Off: "Off",
Closed: "Closed",
Open: "Open",
Ready: "Ready",
Busy: "Busy",
Moving: "Moving",
Standby: "Standby",
Running: "Running",
Started: "Started",
Stopped: "Stopped",
Paused: "Paused",
Remote: "Remote",
Reset: "Reset",
Closing : "Closing",
Disable: "Disable",
Waiting: "Waiting",
Positioned: " Positioned",
Starting: "Starting",
Loading: "Loading",
Unknown: "Unknown",
Alarm: "Alarm",
Fault: "Fault",
Invalid: "Invalid",
Offline: "Offline"}
@staticmethod
def tostring(state):
return DiffractometerState.STATE_DESC.get(state, "Unknown")
class GenericDiffractometer(HardwareObject):
"""
Abstract base class for diffractometers
"""
CENTRING_MOTORS_NAME = ["phi",
"phiz",
"phiy",
"sampx",
"sampy",
"kappa",
"kappa_phi",
"beam_x",
"beam_y"]
CHANNELS_NAME = ["CoaxCamScaleX",
"CoaxCamScaleY",
"State",
"CurrentPhase",
"TransferMode",
"SampleIsLoaded",
"HeadType"]
COMMANDS_NAME = ["startSetPhase"
]
STATE_CHANGED_EVENT = "stateChanged"
STATUS_CHANGED_EVENT = "statusChanged"
MOTOR_POSITION_CHANGED_EVENT = "motorPositionChanged"
MOTOR_STATUS_CHANGED_EVENT = "motorStatusChanged"
HEAD_TYPE_MINIKAPPA = "MiniKappa"
HEAD_TYPE_SMARTMAGNET = "SmartMagnet"
HEAD_TYPE_PLATE = "Plate"
HEAD_TYPE_PERMANENT = "Permanent"
CENTRING_METHOD_MANUAL = "Manual 3-click"
CENTRING_METHOD_AUTO = "Computer automatic"
CENTRING_METHOD_MOVE_TO_BEAM = "Move to beam"
PHASE_TRANSFER = "Transfer"
PHASE_CENTRING = "Centring"
PHASE_COLLECTION = "DataCollection"
PHASE_BEAM = "BeamLocation"
def __init__(self, name):
HardwareObject.__init__(self, name)
# Hardware objects ----------------------------------------------------
self.motor_hwobj_dict = {}
self.centring_motors_list = None
self.front_light_switch = None
self.back_light_switch = None
self.camera_hwobj = None
self.beam_info_hwobj = None
self.sample_changer = None
self.use_sc = False
# Channels and commands -----------------------------------------------
self.channel_dict = {}
self.used_channels_list = GenericDiffractometer.CHANNELS_NAME
self.command_dict = {}
self.used_commands_list = GenericDiffractometer.COMMANDS_NAME
# flag for using sample_centring hwobj or not
self.use_sample_centring = None
# Internal values -----------------------------------------------------
self.ready_event = None
self.head_type = GenericDiffractometer.HEAD_TYPE_MINIKAPPA
self.phase_list = []
self.grid_direction = None
self.reversing_rotation = None
self.beam_position = None
self.zoom_centre = None
self.pixels_per_mm_x = None
self.pixels_per_mm_y = None
self.image_width = None
self.image_height = None
self.current_state = None
self.current_phase = None
self.transfer_mode = None
self.current_centring_procedure = None
self.current_centring_method = None
self.current_motor_positions = {}
self.current_motor_states = {}
self.fast_shutter_is_open = None
self.centring_status = {"valid": False}
self.centring_time = 0
self.user_confirms_centring = None
self.user_clicked_event = None
self.automatic_centring_try_count = 1
self.omega_reference_par = None
self.move_to_motors_positions_task = None
self.move_to_motors_positions_procedure = None
self.centring_methods = {
GenericDiffractometer.CENTRING_METHOD_MANUAL: \
self.start_manual_centring,
GenericDiffractometer.CENTRING_METHOD_AUTO: \
self.start_automatic_centring,
GenericDiffractometer.CENTRING_METHOD_MOVE_TO_BEAM: \
self.start_move_to_beam}
self.connect(self, 'equipmentReady', self.equipment_ready)
self.connect(self, 'equipmentNotReady', self.equipment_not_ready)
def init(self):
# Internal values -----------------------------------------------------
self.ready_event = gevent.event.Event()
self.user_clicked_event = gevent.event.AsyncResult()
self.user_confirms_centring = True
# Hardware objects ----------------------------------------------------
self.camera_hwobj = self.getObjectByRole("camera")
if self.camera_hwobj is not None:
self.image_height = self.camera_hwobj.getHeight()
self.image_width = self.camera_hwobj.getWidth()
else:
logging.getLogger("HWR").debug("Diffractometer: " + \
"Camera hwobj is not defined")
self.beam_info_hwobj = self.getObjectByRole("beam_info")
if self.beam_info_hwobj is not None:
self.beam_position = self.beam_info_hwobj.get_beam_position()
self.connect(self.beam_info_hwobj,
"beamPosChanged",
self.beam_position_changed)
else:
self.beam_position = [self.image_width / 2,
self.image_height / 2]
logging.getLogger("HWR").warning("Diffractometer: " +\
"BeamInfo hwobj is not defined")
self.front_light_swtich = self.getObjectByRole('frontlightswtich')
self.back_light_swtich = self.getObjectByRole('backlightswtich')
# Channels -----------------------------------------------------------
try:
self.used_channels_list = eval(self.getProperty("used_channels"))
except:
pass # used the default value
for channel_name in self.used_channels_list:
self.channel_dict[channel_name] = self.getChannelObject(channel_name)
if self.channel_dict[channel_name] is None:
continue
if channel_name == "TransferMode":
self.connect(self.channel_dict["TransferMode"], "update",
self.transfer_mode_changed)
elif channel_name =="CurrentPhase":
self.connect(self.channel_dict["CurrentPhase"], "update",
self.current_phase_changed)
elif channel_name =="HeadType":
self.connect(self.channel_dict["HeadType"], "update",
self.head_type_changed)
elif channel_name =="State":
self.connect(self.channel_dict["State"], "update",
self.state_changed)
# Commands -----------------------------------------------------------
try:
self.used_commands_list = eval(self.getProperty("used_commands"))
except:
pass # used the default value
for command_name in self.used_commands_list:
self.command_dict[command_name] = self.getCommandObject(command_name)
# Centring motors ----------------------------------------------------
try:
self.centring_motors_list = eval(self.getProperty("centring_motors"))
except:
self.centring_motors_list = GenericDiffractometer.CENTRING_MOTORS_NAME
queue_model_objects.CentredPosition.\
set_diffractometer_motor_names(*self.centring_motors_list)
for motor_name in self.centring_motors_list:
temp_motor_hwobj = self.getObjectByRole(motor_name)
if temp_motor_hwobj is not None:
logging.getLogger("HWR").debug("Diffractometer: Adding " + \
"%s motor to centring motors" % motor_name)
self.motor_hwobj_dict[motor_name] = temp_motor_hwobj
self.connect(temp_motor_hwobj, "stateChanged",
self.motor_state_changed)
self.connect(temp_motor_hwobj, "positionChanged",
self.centring_motor_moved)
if motor_name == "phi":
self.connect(temp_motor_hwobj,
"positionChanged",
self.emit_diffractometer_moved)
elif motor_name == "zoom":
self.connect(temp_motor_hwobj,
"predefinedPositionChanged",
self.zoom_motor_predefined_position_changed)
self.connect(temp_motor_hwobj,
"stateChanged",
self.zoom_motor_state_changed)
else:
logging.getLogger("HWR").warning("Diffractometer: Motor " + \
"%s listed in the centring motor list, but not initalized" % motor_name)
# sample changer -----------------------------------------------------
self.sample_changer = self.getObjectByRole("samplechanger")
if self.sample_changer is None :
logging.getLogger("HWR").warning('Diffractometer: Sample Changer is not defined')
else:
#By default use sample changer if it's defined and transfer_mode
# is set to SAMPLE_CHANGER.
# if not defined, set use_sc to True
if self.transfer_mode is None or self.transfer_mode == "SAMPLE_CHANGER":
self.use_sc = True
try:
self.use_sample_centring = self.getProperty("sample_centring")
if self.use_sample_centring:
self.centring_phi=sample_centring.CentringMotor(\
self.motor_hwobj_dict['phi'], direction=-1)
self.centring_phiz=sample_centring.CentringMotor(\
self.motor_hwobj_dict['phiz'])
self.centring_phiy=sample_centring.CentringMotor(
self.motor_hwobj_dict['phiy'],direction=-1)
self.centring_sampx=sample_centring.CentringMotor(
self.motor_hwobj_dict['sampx'])
self.centring_sampy=sample_centring.CentringMotor(\
self.motor_hwobj_dict['sampy'])
except:
pass # used the default value
# Other parameters ---------------------------------------------------
try:
self.zoom_centre = eval(self.getProperty("zoom_centre"))
except:
if self.image_width is not None and self.image_height is not None:
self.zoom_centre = {'x': self.image_width / 2,'y' : self.image_height / 2}
self.beam_position = [self.image_width / 2, self.image_height / 2]
logging.getLogger("HWR").warning("Diffractometer: Zoom center is " +\
"not defined. Continuing with the middle: %s" % self.zoom_centre)
else:
self.zoom_centre = {'x': 0, 'y' : 0}
logging.getLogger("HWR").warning("Diffractometer: " + \
"Neither zoom centre nor camera size iz defined")
self.reversing_rotation = self.getProperty("reversing_rotation")
try:
# grid_direction describes how a grid is collected
# 'fast' is collection direction and 'slow' describes
# move to the next collection line
self.grid_direction = eval(self.getProperty("grid_direction"))
except:
self.grid_direction = {"fast": (0, 1),
"slow": (1, 0),
"omega_ref" : 0}
logging.getLogger("HWR").warning("Diffractometer: Grid " + \
"direction is not defined. Using default.")
try:
self.phase_list = eval(self.getProperty("phase_list"))
except:
self.phase_list = [GenericDiffractometer.PHASE_TRANSFER,
GenericDiffractometer.PHASE_CENTRING,
GenericDiffractometer.PHASE_COLLECTION,
GenericDiffractometer.PHASE_BEAM]
#Compatibility
#TODO remove this
self.getCentringStatus = self.get_centring_status
self.getPositions = self.get_positions
self.moveMotors = self.move_motors
self.isReady = self.is_ready
# to make it compatibile
def __getattr__(self, attr):
if attr.startswith("__"):
raise AttributeError(attr)
else:
if attr == "currentCentringProcedure":
return self.current_centring_procedure
if attr == "centringStatus" :
return self.centring_status
if attr == "imageClicked":
return self.image_clicked
if attr == "cancelCentringMethod":
return self.cancel_centring_method
if attr == "pixelsPerMmY":
return self.pixels_per_mm_x
if attr == "pixelsPerMmZ":
return self.pixels_per_mm_y
return HardwareObject.__getattr__(self,attr)
def is_ready(self):
"""
Detects if device is ready
"""
return self.current_state == DiffractometerState.tostring(\
DiffractometerState.Ready)
def wait_device_ready(self, timeout=10):
""" Waits when diffractometer status is ready:
:param timeout: timeout in second
:type timeout: int
"""
with gevent.Timeout(timeout, Exception("Timeout waiting for device ready")):
while not self.is_ready():
gevent.sleep(0.01)
def execute_server_task(self, method, timeout=30, *args):
"""Method is used to execute commands and wait till
diffractometer is in ready state
:param method: method to be executed
:type method: instance
:param timeout: timeout in seconds
:type timeout: seconds
"""
self.ready_event.clear()
self.current_state = DiffractometerState.tostring(\
DiffractometerState.Busy)
task_id = method(*args)
self.wait_device_ready(timeout)
self.ready_event.set()
def in_plate_mode(self):
"""Returns True if diffractometer in plate mod
:returns: boolean
"""
return self.head_type == GenericDiffractometer.HEAD_TYPE_PLATE
def get_head_type(self):
"""Returns head type
:returns: string
"""
return self.head_type
def use_sample_changer(self):
"""Returns True if sample changer is in use
:returns: boolean
"""
return False
def set_use_sc(self, flag):
"""Sets use_sc flag, that indicates if sample changer is used
:param flag: use sample changer flag
:type flag: boolean
"""
if flag:
# check both transfer_mode and sample_Changer
if self.sample_changer is None:
logging.getLogger("HWR").error("Diffractometer: Sample " + \
"Changer is not available")
return False
if self.transfer_mode is None or self.channel_dict['TransferMode'].getValue() == "SAMPLE_CHANGER":
# if transferMode is not defined, ignore the checkup
self.use_sc = True
else:
logging.getLogger("HWR").error("Diffractometer: Set the " + \
"diffractometer TransferMode to SAMPLE_CHANGER first!!")
return False
else:
self.use_sc = False
return True
def transfer_mode_changed(self, transfer_mode):
"""
Descript. :
"""
logging.getLogger("HWR").info("current_transfer_mode is set to %s" % transfer_mode)
self.transfer_mode = transfer_mode
if transfer_mode != "SAMPLE_CHANGER":
self.use_sc = False
self.emit('minidiffTransferModeChanged', (transfer_mode, ))
def get_transfer_mode(self):
"""
"""
return self.transfer_mode
def get_current_phase(self):
"""
Descript. :
"""
return self.current_phase
def get_grid_direction(self):
"""
Descript. :
"""
return self.grid_direction
def get_available_centring_methods(self):
"""
Descript. :
"""
return self.centring_methods.keys()
def get_current_centring_method(self):
"""
Descript. :
"""
return self.current_centring_method
def is_reversing_rotation(self):
"""
"""
return self.reversing_rotation == True
def beam_position_changed(self, value):
"""
Descript. :
"""
self.beam_position = list(value)
#def get_motor_positions(self):
# return
#TODO rename to get_motor_positions
def get_positions(self):
"""
Descript. :
"""
self.current_motor_positions["beam_x"] = (self.beam_position[0] - \
self.zoom_centre['x'] )/self.pixels_per_mm_y
self.current_motor_positions["beam_y"] = (self.beam_position[1] - \
self.zoom_centre['y'] )/self.pixels_per_mm_x
return self.current_motor_positions
def get_omega_position(self):
"""
Descript. :
"""
return self.current_positions_dict.get("phi")
def get_snapshot(self):
if self.camera_hwobj:
return self.camera_hwobj.get_snapshot()
def save_snapshot(self, filename):
"""
"""
if self.camera_hwobj:
return self.camera_hwobj.save_snapshot(filename)
def get_pixels_per_mm(self):
"""
Returns tuple with pixels_per_mm_x and pixels_per_mm_y
:returns: list with two floats
"""
return (self.pixels_per_mm_x, self.pixels_per_mm_y)
def get_phase_list(self):
"""
Returns list of available phases
:returns: list with str
"""
return self.phase_list
def set_phase(self, phase_name, timeout=None):
"""
"""
raise NotImplementedError
def start_centring_method(self, method, sample_info=None, wait=False):
"""
"""
if self.current_centring_method is not None:
logging.getLogger("HWR").error("Diffractometer: already in centring method %s" %
self.current_centring_method)
return
curr_time = time.strftime("%Y-%m-%d %H:%M:%S")
self.centring_status = {"valid": False,
"startTime": curr_time,
"angleLimit": None}
self.emit_centring_started(method)
try:
centring_method = self.centring_methods[method]
except KeyError as diag:
logging.getLogger("HWR").error("Diffractometer: unknown centring method (%s)" % str(diag))
self.emit_centring_failed()
else:
try:
centring_method(sample_info, wait_result=wait)
except:
logging.getLogger("HWR").exception("Diffractometer: problem while centring")
self.emit_centring_failed()
def cancel_centring_method(self, reject=False):
"""
"""
if self.current_centring_procedure is not None:
try:
self.current_centring_procedure.kill()
except:
logging.getLogger("HWR").exception("Diffractometer: problem aborting the centring method")
try:
#TODO... do we need this at all?
#fun = self.cancel_centring_methods[self.current_centring_method]
pass
except KeyError as diag:
self.emit_centring_failed()
else:
try:
fun()
except:
self.emit_centring_failed()
else:
self.emit_centring_failed()
self.emit_progress_message("")
if reject:
self.reject_centring()
def start_manual_centring(self, sample_info=None, wait_result=None):
"""
"""
self.emit_progress_message("Manual 3 click centring...")
if self.use_sample_centring:
self.current_centring_procedure = \
sample_centring.start({"phi": self.centring_phi,
"phiy": self.centring_phiy,
"sampx": self.centring_sampx,
"sampy": self.centring_sampy,
"phiz": self.centring_phiz },
self.pixels_per_mm_x,
self.pixels_per_mm_y,
self.beam_position[0],
self.beam_position[1])
else:
self.current_centring_procedure = gevent.spawn(self.manual_centring)
self.current_centring_procedure.link(self.centring_done)
def start_automatic_centring(self, sample_info=None, loop_only=False, wait_result=None):
"""
"""
self.emit_progress_message("Automatic centring...")
while self.automatic_centring_try_count > 0:
if self.use_sample_centring:
self.current_centring_procedure = \
sample_centring.start_auto(self.camera_hwobj,
{"phi": self.centring_phi,
"phiy": self.centring_phiy,
"sampx": self.centring_sampx,
"sampy": self.centring_sampy,
"phiz": self.centring_phiz },
self.pixels_per_mm_x,
self.pixels_per_mm_y,
self.beam_position[0],
self.beam_position[1],
msg_cb = self.emit_progress_message,
new_point_cb=lambda point: self.emit("newAutomaticCentringPoint", point))
else:
self.current_centring_procedure = gevent.spawn(self.automatic_centring)
self.current_centring_procedure.link(self.centring_done)
if wait_result:
self.ready_event.wait()
self.ready_event.clear()
self.automatic_centring_try_count -= 1
self.automatic_centring_try_count = 1
def start_move_to_beam(self, coord_x=None, coord_y=None, omega=None, wait_result=None):
"""
Descript. :
"""
try:
self.emit_progress_message("Move to beam...")
self.centring_time = time.time()
curr_time = time.strftime("%Y-%m-%d %H:%M:%S")
self.centring_status = {"valid": True,
"startTime": curr_time,
"endTime": curr_time}
if (coord_x is None and
coord_y is None):
coord_x = self.beam_position[0]
coord_y = self.beam_position[1]
motors = self.get_centred_point_from_coord(\
coord_x, coord_y, return_by_names=True)
if omega is not None:
motors["phi"] = omega
self.centring_status["motors"] = motors
self.centring_status["valid"] = True
self.centring_status["angleLimit"] = True
self.emit_progress_message("")
self.accept_centring()
self.current_centring_method = None
self.current_centring_procedure = None
except:
logging.exception("Diffractometer: Could not complete 2D centring")
def centring_done(self, centring_procedure):
"""
Descript. :
"""
try:
motor_pos = centring_procedure.get()
if isinstance(motor_pos, gevent.GreenletExit):
raise motor_pos
except:
logging.exception("Could not complete centring")
self.emit_centring_failed()
else:
self.emit_progress_message("Moving sample to centred position...")
self.emit_centring_moving()
try:
self.move_to_motors_positions(motor_pos)
except:
logging.exception("Could not move to centred position")
self.emit_centring_failed()
else:
#if 3 click centring move -180
if not self.in_plate_mode():
self.motor_hwobj_dict['phi'].syncMoveRelative(-180)
self.centring_time = time.time()
self.emit_centring_successful()
self.emit_progress_message("")
self.ready_event.set()
def manual_centring(self):
"""
"""
raise NotImplementedError
def automatic_centring(self):
"""
"""
raise NotImplementedError
def centring_motor_moved(self, pos):
"""
"""
if time.time() - self.centring_time > 1.0:
self.invalidate_centring()
self.emit_diffractometer_moved()
def invalidate_centring(self):
"""
"""
if self.current_centring_procedure is None and self.centring_status["valid"]:
self.centring_status={"valid":False}
self.emit_progress_message("")
self.emit('centringInvalid', ())
def emit_diffractometer_moved(self, *args):
"""
"""
self.emit("diffractometerMoved", ())
def motor_positions_to_screen(self, centred_positions_dict):
"""
"""
if self.use_sample_centring:
self.update_zoom_calibration()
if None in (self.pixels_per_mm_x, self.pixels_per_mm_y):
return 0, 0
phi_angle = math.radians(self.centring_phi.direction * \
self.centring_phi.getPosition())
sampx = self.centring_sampx.direction * \
(centred_positions_dict["sampx"] - \
self.centring_sampx.getPosition())
sampy = self.centring_sampy.direction * \
(centred_positions_dict["sampy"] - \
self.centring_sampy.getPosition())
phiy = self.centring_phiy.direction * \
(centred_positions_dict["phiy"] - \
self.centring_phiy.getPosition())
phiz = self.centring_phiz.direction * \
(centred_positions_dict["phiz"] - \
self.centring_phiz.getPosition())
rot_matrix = numpy.matrix([math.cos(phi_angle), - \
math.sin(phi_angle), math.sin(phi_angle), \
math.cos(phi_angle)])
rot_matrix.shape = (2, 2)
inv_rot_matrix = numpy.array(rot_matrix.I)
dx, dy = numpy.dot(numpy.array([sampx, sampy]), \
inv_rot_matrix)*self.pixels_per_mm_x
x = (phiy * self.pixels_per_mm_x) + self.beam_position[0]
y = dy + (phiz * self.pixels_per_mm_y) + self.beam_position[1]
return x, y
else:
raise NotImplementedError
def move_to_centred_position(self, centred_position):
"""
"""
self.move_motors(centred_position)
def move_to_motors_positions(self, motors_positions, wait = False):
"""
"""
self.emit_progress_message("Moving to motors positions...")
self.move_to_motors_positions_procedure = gevent.spawn(\
self.move_motors, motors_positions)
self.move_to_motors_positions_procedure.link(self.move_motors_done)
def move_motors(self, motor_positions, timeout=15):
"""
Moves diffractometer motors to the requested positions
:param motors_dict: dictionary with motor names or hwobj
and target values.
:type motors_dict: dict
"""
for motor in motor_positions.keys():
position = motor_positions[motor]
if type(motor) in (str, unicode):
motor_role = motor
motor = self.motor_hwobj_dict.get(motor_role)
del motor_positions[motor_role]
if None in (motor, position):
continue
motor_positions[motor] = position
motor.move(position)
self.wait_device_ready(timeout)
def move_motors_done(self, move_motors_procedure):
"""
Descript. :
"""
self.move_to_motors_positions_procedure = None
self.emit_progress_message("")
def move_to_beam(self, x, y, omega=None):
"""
Descript. : function to create a centring point based on all motors
positions.
"""
try:
pos = self.get_centred_point_from_coord(x, y, return_by_names=False)
if omega is not None:
pos["phiMotor"] = omega
self.move_to_motors_positions(pos)
except:
logging.getLogger("HWR").exception("Diffractometer: could not center to beam, aborting")
def image_clicked(self, x, y, xi=None, yi=None):
"""
Descript. :
"""
if self.use_sample_centring:
sample_centring.user_click(x,y)
else:
self.user_clicked_event.set((x, y))
def accept_centring(self):
"""
Descript. :
Arg. " fully_centred_point. True if 3 click centring
else False
"""
self.centring_status["valid"] = True
self.centring_status["accepted"] = True
self.emit('centringAccepted', (True, self.get_centring_status()))
def reject_centring(self):
"""
Descript. :
"""
if self.current_centring_procedure:
self.current_centring_procedure.kill()
self.centring_status = {"valid":False}
self.emit_progress_message("")
self.emit('centringAccepted', (False, self.get_centring_status()))
def emit_centring_started(self, method):
"""
Descript. :
"""
self.current_centring_method = method
self.emit('centringStarted', (method, False))
def emit_centring_moving(self):
"""
Descript. :
"""
self.emit('centringMoving', ())
def emit_centring_failed(self):
"""
Descript. :
"""
self.centring_status = {"valid": False}
method = self.current_centring_method
self.current_centring_method = None
self.current_centring_procedure = None
self.emit('centringFailed', (method, self.get_centring_status()))
def emit_centring_successful(self):
"""
Descript. :
"""
if self.current_centring_procedure is not None:
curr_time = time.strftime("%Y-%m-%d %H:%M:%S")
self.centring_status["endTime"] = curr_time
motor_pos = self.current_centring_procedure.get()
self.centring_status["motors"] = self.convert_from_obj_to_name(motor_pos)
self.centring_status["method"] = self.current_centring_method
self.centring_status["valid"] = True
method = self.current_centring_method
self.emit('centringSuccessful', (method, self.get_centring_status()))
self.current_centring_method = None
self.current_centring_procedure = None
else:
logging.getLogger("HWR").debug("Diffractometer: Trying to emit " + \
"centringSuccessful outside of a centring")
def emit_progress_message(self, msg = None):
"""
Descript. :
"""
self.emit('progressMessage', (msg,))
def get_centring_status(self):
"""
Descript. :
"""
return copy.deepcopy(self.centring_status)
def get_centred_point_from_coord(self):
"""
"""
raise NotImplementedError
def get_point_between_two_points(self, point_one, point_two, frame_num, frame_total):
"""
Method returns a centring point between two centring points
It is used to get a position on a helical line based on
frame number and total frame number
"""
new_point = {}
point_one = point_one.as_dict()
point_two = point_two.as_dict()
for motor in point_one.keys():
new_motor_pos = frame_num / float(frame_total) * abs(point_one[motor] - \
point_two[motor]) + point_one[motor]
new_motor_pos += 0.5 * (point_two[motor] - point_one[motor]) / \
frame_total
new_point[motor] = new_motor_pos
return new_point
def convert_from_obj_to_name(self, motor_pos):
"""
"""
motors = {}
for motor_role in self.centring_motors_list:
motor_obj = self.getObjectByRole(motor_role)
try:
motors[motor_role] = motor_pos[motor_obj]
except KeyError:
if motor_obj:
motors[motor_role] = motor_obj.getPosition()
motors["beam_x"] = (self.beam_position[0] - \
self.zoom_centre['x'] )/self.pixels_per_mm_y
motors["beam_y"] = (self.beam_position[1] - \
self.zoom_centre['y'] )/self.pixels_per_mm_x
return motors
def visual_align(self, point_1, point_2):
"""
Descript. :
"""
return
def move_omega_relative(self, relative_angle):
"""
Descript. :
"""
return
def get_scan_limits(self, speed=None):
"""
Gets scan limits. Necessary for example in the plate mode
where osc range is limited