forked from AFMD/sEQE-Setup
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmonochromator.py
357 lines (278 loc) · 11.9 KB
/
monochromator.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
import time
import logging
import warnings
import codecs
import serial
class Monochromator():
"""Implements Monochromator for Princeton Instruments HRS-300.
Note: HRS-300 has manual filter wheel controls on the device. Users
should periodically query the current monochromator filter wheel position.
"""
def __init__(self,com):
self.mono_usb = com
self.connected = False
def connect(self):
"""Function to establish connection to monochromator.
Returns
-------
bool
True if connection successful, False otherwise
Raises
------
LoggingError
Raises Loggingerror for Exception handling
"""
try:
with serial.Serial(self.mono_usb, 9600, timeout=0) as self.p:
self.p.write('HELLO\r'.encode()) # "Hello" initializes the Monochromator
time.sleep(25) # During initialization we want to avoid that the user sends signals
self.connected = self.waitForOK() # Checks for OK response of Monochromator
return self.connected
except Exception as err:
logging.exception("Unexpected during connect function:")
# Check Monochromator response
def waitForOK(self):
"""Function to wait for acceptance signal from monochromator.
Returns
-------
bool
True if connection successful, False otherwise
Raises
------
LoggingError
Raises error if monochromator connection failed or Exception handling
Notes
-----
After 10 unsuccessfull readouts, the function interrupts itself.
"""
counter = 0
ret = False
self.p.timeout = 10
shouldbEOk = 'filler'
try:
while shouldbEOk != 'ok\r\n':
shouldbEOk = self.p.readline()
shouldbEOk = codecs.decode(shouldbEOk)
print(shouldbEOk)
if shouldbEOk.endswith('ok\r\n'):
ret = True
return ret
else:
counter += 1
logging.info(f'Waiting for "ok" signal - {10-counter} more attempts before exiting')
if counter > 10:
logging.error('waitForOK function could not find "ok" response - please check monochromator connections')
break
self.p.timeout = 0
return ret
except Exception as error:
logging.exception("Unexpected error during waitForOk function:")
def chooseWavelength(self, wavelength): # Function to send GOTO command to monochromator
"""Function to send wavelength command to monochromator.
Parameters
----------
wavelength: float, required
target wavelength
Returns
-------
None
Raises
------
LoggerError
Raises error if monochromator not connected or Exception handling
"""
try:
if self.connected:
with serial.Serial(self.mono_usb, 9600, timeout=0) as self.p:
print('%d nm' % wavelength)
self.p.write('{:.2f} GOTO\r'.format(wavelength).encode())
self.waitForOK()
else:
logging.error('Monochromator Not Connected')
except Exception as err:
logging.exception("Unexpected error during execution of chooseWavelength function:")
def chooseScanSpeed(self, speed):
"""Function to send scan speed command to monochromator.
Parameters
----------
speed: float, required
monochromator grating scan speed
Returns
-------
None
Raises
------
LoggerError
Raises error if monochromator not connected or Exception handling
"""
try:
if self.connected:
with serial.Serial(self.mono_usb, 9600, timeout=0) as self.p:
# logger.info('Updating Scan Speed to %d nm/min.' % speed)
self.p.write('{:.2f} NM/MIN\r'.format(speed).encode())
self.waitForOK()
else:
logging.error('Monochromator Not Connected')
except Exception as err:
logging.exception("Unexpected error during execution of chooseScanSpeed function:")
def chooseGrating(self, gratingNo):
"""Function to send grating command to monochromator.
Parameters
----------
gratingNo: float, required
Monochromator grating number
Returns
-------
None
Raises
-------
LoggerError
Raises error if monochromator not connected or Exception handling
"""
try:
if self.connected:
if self.p.is_open:
logging.info('Moving to Grating %d' % gratingNo)
self.p.write('{:d} grating\r'.format(gratingNo).encode())
#print(self.p.readline())
self.waitForOK()
else:
with serial.Serial(self.mono_usb, 9600, timeout=0) as self.p:
logging.info('Moving to Grating %d' % gratingNo)
self.p.write('{:d} grating\r'.format(gratingNo).encode())
#print(self.p.readline())
self.waitForOK()
else:
logging.error('Monochromator Not Connected')
except Exception as err:
logging.exception("Unexpected error during execution of chooseGrating function:")
def chooseFilter(self, filterNo):
"""Function to send filter selection command to filter wheel.
Parameters
----------
filterNo: float, required
Filter position
Returns
-------
None
Raises
------
LoggerError
Raises error if monochromator not connected or Exception handling
"""
try:
if self.connected:
if self.p.is_open:
logging.info('Moving to Monochromator Filter %d' % filterNo)
self.p.write('{:d} FILTER\r'.format(filterNo).encode())
#print(self.p.readline())
self.waitForOK()
else:
with serial.Serial(self.mono_usb, 9600, timeout=0) as self.p:
logging.info('Moving to Monochromator Filter %d' % filterNo)
self.p.write('{:d} FILTER\r'.format(filterNo).encode())
#print(self.p.readline())
self.waitForOK()
else:
logging.error('Monochromator Not Connected')
except Exception as err:
logging.exception("Unexpected error during execution of chooseFilter function:")
def initializeFilter(self, filterDiff):
"""Function to initialize filter wheel.
Parameters
----------
filterDiff: int, required
Difference between filter position and initialization position
Returns
-------
None
Raises
------
LoggerError:
Raises error if monochromator not connected or Exception handling
"""
try:
if self.connected:
with serial.Serial(self.mono_usb, 9600, timeout=0) as self.p:
logging.info('Initializing Monochromator Filter Wheel')
self.p.write('{:d} FILTER\r'.format(filterDiff).encode())
self.p.write('FHOME\r'.encode())
self.waitForOK()
else:
logging.error('Monochromator Not Connected')
except Exception as err:
logging.exception("Unexpected error during execution of initializeFilter function:")
def checkFilter(self,): # Filter switching points from GUI
"""Function to read position of monochromators filter wheel.
Returns
-------
int
current monochromator's filter position
Raises
------
LoggerError
Raises error if monochromator is not connected or Exception handling
"""
try:
if self.connected:
with serial.Serial(self.mono_usb, 9600, timeout=0) as self.p:
self.p.write('?filter\r'.encode())
self.p.timeout = 30000
response = self.p.readline()
print(response)
if response.endswith('1 ok\r\n'.encode(errors='ignore')):
filterNo = 1
elif response.endswith('2 ok\r\n'.encode(errors='ignore')):
filterNo = 2
elif response.endswith('3 ok\r\n'.encode(errors='ignore')):
filterNo = 3
elif response.endswith('4 ok\r\n'.encode(errors='ignore')):
filterNo = 4
elif response.endswith('5 ok\r\n'.encode(errors='ignore')):
filterNo = 5
elif response.endswith('6 ok\r\n'.encode(errors='ignore')):
filterNo = 6
elif response.endswith('ok\r\n'.encode(errors='ignore')):
filterNo = 0
else: # Do I need this?
logging.error('Error: Monchromator Filter Response')
return filterNo
else:
logging.error('Monochromator Not Connected')
except Exception as err:
logging.exception("Unexpected error during execution of checkFilter function:}")
def checkGrating(self,): # Grating switching points from GUI
"""Function to update monochromator grating position from GUI defaults.
Parameters
----------
wavelength: float, required
Current wavelength position of monochromator
Returns
--------
int
Current grating position
Raises
------
LoggerError
Raises error if monochromator not connected or Exception handling
"""
try:
if self.connected:
with serial.Serial(self.mono_usb, 9600, timeout=0) as self.p:
self.p.write('?grating\r'.encode())
self.p.timeout = 30000
response = self.p.readline()
print(response)
if response.endswith('1 ok\r\n'.encode()):
gratingNo = 1
elif response.endswith('2 ok\r\n'.encode()):
gratingNo = 2
elif response.endswith('3 ok\r\n'.encode()):
gratingNo = 3
else: # Do I need this?
logging.error('Error: Grating Response')
return gratingNo
else:
logging.error('Monochromator Not Connected')
except Exception as err:
logging.exception("Unexpected error during execution of checkGrating function:")