-
Notifications
You must be signed in to change notification settings - Fork 1
/
thordevices.py
462 lines (390 loc) · 16.5 KB
/
thordevices.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
'''
thordevices.py
Controlls devices from Thorlabs using the Thorlabs.MotionControl.C_API DLLs that go with
the Thorlabs kinesis software.
Last Updated: January 2020
| Trevor Arp
| Gabor Lab
| University of California, Riverside
All Rights Reserved
'''
import parameters as pm
import ctypes
from ctypes import byref
from os.path import join
import numpy as np
import time
from traceback import format_exc
int32 = ctypes.c_long
uInt32 = ctypes.c_ulong
uInt64 = ctypes.c_ulonglong
float64 = ctypes.c_double
motor_stop_bits = -2147470336
rotation_stop_bits = -2147482624
Nanorotator_stop_bits = -2146429952
DLL_device = "Thorlabs.MotionControl.DeviceManager.dll"
DLL_delay = "Thorlabs.MotionControl.Benchtop.BrushlessMotor.dll"
DLL_rotation = "Thorlabs.MotionControl.IntegratedStepperMotors.dll"
DLL_BSC = "Thorlabs.MotionControl.Benchtop.StepperMotor.dll"
class DelayController():
'''
A class for controlling a Thorlabs Delay Stage such as a DDS220, but more genercally a
"Benchtop Burshless Motor"
'''
def __init__(self, serialNum, autohome=True):
s = str(serialNum)
self.serial = byref(ctypes.create_string_buffer(s.encode('utf-8')))
self.channel = ctypes.c_short(1)
self.pollingrate = ctypes.c_int(100)
self.device_to_mm = pm.DELAY_step_to_mm
self.velocity_to_mm = pm.DELAY_vel_to_mm
self.position = 0.0
# Load the DLLs
self.device_manager = ctypes.CDLL(join(pm.DLL_Thorlabs, DLL_device)) #Here to make subsequent modules load
self.dll = ctypes.CDLL(join(pm.DLL_Thorlabs, DLL_delay))
# Initialize the device
self.dll.BMC_Open(self.serial)
self.dll.BMC_EnableChannel(self.serial, self.channel)
self.dll.BMC_StartPolling(self.serial, self.channel, self.pollingrate)
time.sleep(0.1)
self.minPos = 1.0*self.GetStageAxisInfo_MinPos()/self.device_to_mm
self.maxPos = 1.0*self.GetStageAxisInfo_MaxPos()/self.device_to_mm
# Home the device
if bool(self.dll.BMC_NeedsHoming(self.serial, self.channel)) and autohome:
self.dll.BMC_Home(self.serial, self.channel)
time.sleep(0.25)
self.wait_till_stopped()
#Get Velocity Parameters
self.accel = ctypes.c_int(int(pm.DELAY_default_accel))
self.velocity = ctypes.c_int(int(100.0*self.velocity_to_mm))
self.dll.BMC_SetVelParams(self.serial, self.channel, self.velocity, self.accel)
self.GetPosition()
self.MoveMM(100.0)
#
def wait_till_stopped(self):
loopcnt = 0
time.sleep(0.1)
while self.dll.BMC_GetStatusBits(self.serial, self.channel) != motor_stop_bits:
if loopcnt > 600:
print("Error throdevices.DelayController.wait_till_stopped: Timed out after 60 seconds")
break
time.sleep(0.1)
loopcnt += 1
#
#
def GetPosition(self):
time.sleep(0.05)
pos = self.dll.BMC_GetPosition(self.serial, self.channel)
self.position = float(1.0*pos/self.device_to_mm)
return self.position
#
def MoveMM(self, position, wait_forever=False):
if position >= self.minPos and position <= self.maxPos:
loopcnt = 0
pos = int(position*self.device_to_mm)
self.dll.BMC_SetVelParams(self.serial, self.channel, self.velocity, self.accel)
self.dll.BMC_MoveToPosition(self.serial, self.channel, pos)
while self.dll.BMC_GetPosition(self.serial, self.channel) != pos:
if loopcnt > 600 and not wait_forever:
print("Error throdevices.DelayController.wait_till_stopped: Timed out after 30 seconds")
break
time.sleep(0.1)
loopcnt += 1
self.position = float(1.0*pos/self.device_to_mm)
else:
print("Error thordevices.MoveMM: Cannot Move to given position")
#
def MoveToIn(self, position, time):
"""
Move to a position in a given amount of time
Args:
position : the position to move to
time : The amount of time to take, must not cause stage to exceed maximum velocity
"""
if position >= self.minPos and position <= self.maxPos:
vel = np.abs(position - self.position)/time
vel_c = ctypes.c_int(int(vel*self.velocity_to_mm))
pos = int(position*self.device_to_mm)
self.dll.BMC_SetVelParams(self.serial, self.channel, vel_c, self.accel)
self.dll.BMC_MoveToPosition(self.serial, self.channel, pos)
self.position = float(1.0*pos/self.device_to_mm)
else:
print("Error thordevices.MoveToIn: Cannot move to given position")
#
def GetStageAxisInfo_MaxPos(self):
return self.dll.BMC_GetStageAxisMaxPos(self.serial, self.channel)
#
def GetStageAxisInfo_MinPos(self):
return self.dll.BMC_GetStageAxisMinPos(self.serial, self.channel)
#
def __del__(self):
self.dll.BMC_StopPolling(self.serial, self.channel)
self.dll.BMC_Close(self.serial)
#
#
class RotationController():
'''
A class for controlling a Thorlabs Rotation Stage such as a K10CR1, but more genercally a
"Integrated Stepper Motor"
'''
def __init__(self, serialNum, autohome=True):
s = str(serialNum)
self.serialNum = serialNum
self.serial = byref(ctypes.create_string_buffer(s.encode('utf-8')))
self.channel = ctypes.c_short(1)
self.pollingrate = ctypes.c_int(100)
self.angle = 0.0
self.device_to_deg = pm.ROTATION_step_to_deg
# Load the DLLs
self.device_manager = ctypes.CDLL(join(pm.DLL_Thorlabs, DLL_device)) #Here to make subsequent modules load
self.dll = ctypes.CDLL(join(pm.DLL_Thorlabs, DLL_rotation))
# Initialize the device
self.dll.TLI_BuildDeviceList() # Somehow allows it to find the device
self.dll.ISC_Open(self.serial)
self.dll.ISC_EnableChannel(self.serial)
self.dll.ISC_StartPolling(self.serial, self.pollingrate)
time.sleep(0.1)
self.minPos = 1.0*self.GetStageAxisInfo_MinPos()
self.maxPos = 1.0*self.GetStageAxisInfo_MaxPos()
try:
velocity = ctypes.c_int(180000000)
current_accel = ctypes.c_int(0)
current_velocity = ctypes.c_int(0)
self.dll.ISC_GetVelParams(self.serial, byref(current_velocity), byref(current_accel))
self.dll.ISC_SetVelParams(self.serial, velocity, current_accel)
except Exception as e:
print(format_exc())
# Home the device
if autohome:
self.dll.ISC_Home(self.serial)
time.sleep(0.25)
self.wait_till_stopped()
self.GetPosition()
#
def wait_till_stopped(self):
loopcnt = 0
time.sleep(0.1)
while self.dll.ISC_GetStatusBits(self.serial) != rotation_stop_bits:
if loopcnt > 300:
print("Error throdevices.RotationController.wait_till_stopped: Timed out after 30 seconds")
break
time.sleep(0.1)
loopcnt += 1
#
def GetPosition(self):
time.sleep(0.05)
pos = self.dll.ISC_GetPosition(self.serial)
pos = float(1.0*pos/self.device_to_deg)
self.angle = pos
return pos
#
def MoveDeg(self, position, wait_forever=False):
if position >= 0.0 and position <= 360.0:
loopcnt = 0
pos = int(position*self.device_to_deg)
self.dll.ISC_MoveToPosition(self.serial, pos)
while self.dll.ISC_GetPosition(self.serial) != pos:
if loopcnt > 600 and not wait_forever:
print("Error throdevices.RotationController.MoveDeg: Timed out after 60 seconds")
break
time.sleep(0.1)
loopcnt += 1
self.angle = float(1.0*pos/self.device_to_deg)
else:
print("Error thordevices.MoveMM: Cannot Move to given position")
#
def GetStageAxisInfo_MaxPos(self):
return self.dll.ISC_GetStageAxisMaxPos(self.serial)
#
def GetStageAxisInfo_MinPos(self):
return self.dll.ISC_GetStageAxisMinPos(self.serial)
#
def __del__(self):
self.dll.ISC_StopPolling(self.serial)
self.dll.ISC_Close(self.serial)
#
#
class DualRotationController():
'''
A class for controlling two Thorlabs Rotation Stages, such as a K10CR1, in concert to control
two laser beam-lines. Highly specialized to dual beam control applications.
'''
def __init__(self, serialDEL, serialREF, autohome=True):
self.serialNumDel = serialDEL
sd = str(serialDEL)
self.serialD = byref(ctypes.create_string_buffer(sd.encode('utf-8')))
self.serialNumRef = serialREF
sr = str(serialREF)
self.serialR = byref(ctypes.create_string_buffer(sr.encode('utf-8')))
self.channel = ctypes.c_short(1)
self.pollingrate = ctypes.c_int(100)
self.device_to_deg = pm.ROTATION_step_to_deg
self.angleD = 0.0
self.angleR = 0.0
# Load the DLLs
self.device_manager = ctypes.CDLL(join(pm.DLL_Thorlabs, DLL_device)) #Here to make subsequent modules load
self.dll = ctypes.CDLL(join(pm.DLL_Thorlabs, DLL_rotation))
# Initialize the device
self.dll.TLI_BuildDeviceList() # Somehow allows it to find the device
self.dll.ISC_Open(self.serialD)
self.dll.ISC_Open(self.serialR)
self.dll.ISC_EnableChannel(self.serialD)
self.dll.ISC_EnableChannel(self.serialR)
self.dll.ISC_StartPolling(self.serialD, self.pollingrate)
self.dll.ISC_StartPolling(self.serialR, self.pollingrate)
time.sleep(0.1)
self.minPosD = 1.0*self.GetStageAxisInfo_MinPos(self.serialD)
self.maxPosD = 1.0*self.GetStageAxisInfo_MaxPos(self.serialD)
self.minPosR = 1.0*self.GetStageAxisInfo_MinPos(self.serialR)
self.maxPosR = 1.0*self.GetStageAxisInfo_MaxPos(self.serialR)
try:
velocity = ctypes.c_int(180000000)
current_accel = ctypes.c_int(0)
current_velocity = ctypes.c_int(0)
self.dll.ISC_GetVelParams(self.serialD, byref(current_velocity), byref(current_accel))
self.dll.ISC_SetVelParams(self.serialD, velocity, current_accel)
self.dll.ISC_GetVelParams(self.serialR, byref(current_velocity), byref(current_accel))
self.dll.ISC_SetVelParams(self.serialR, velocity, current_accel)
except Exception as e:
print(format_exc())
if autohome:
self.dll.ISC_Home(self.serialD)
self.dll.ISC_Home(self.serialR)
time.sleep(0.25)
self.wait_till_stopped()
self.GetPosition()
#
def wait_till_stopped(self):
loopcnt = 0
time.sleep(0.1)
while self.dll.ISC_GetStatusBits(self.serialD) != rotation_stop_bits or self.dll.ISC_GetStatusBits(self.serialR) != rotation_stop_bits:
if loopcnt > 600:
print("Error throdevices.DualRotationController.wait_till_stopped: Timed out after 60 seconds")
break
time.sleep(0.1)
loopcnt += 1
#
def GetPosition(self):
time.sleep(0.05)
pos1 = self.dll.ISC_GetPosition(self.serialD)
pos1 = float(1.0*pos1/self.device_to_deg)
self.angleD = pos1
pos2 = self.dll.ISC_GetPosition(self.serialR)
pos2 = float(1.0*pos2/self.device_to_deg)
self.angleR = pos2
return pos1, pos2
#
def MoveDeg(self, positionDelay, positionRef, wait_forever=False):
if positionDelay >= 0.0 and positionDelay <= 360.0 and positionRef >= 0.0 and positionRef <= 360.0:
loopcnt = 0
posd = int(positionDelay*self.device_to_deg)
self.dll.ISC_MoveToPosition(self.serialD, posd)
posr = int(positionRef*self.device_to_deg)
self.dll.ISC_MoveToPosition(self.serialR, posr)
while self.dll.ISC_GetPosition(self.serialD) != posd or self.dll.ISC_GetPosition(self.serialR) != posr:
if loopcnt > 600 and not wait_forever:
print("Error throdevices.RotationController.MoveDeg: Timed out after 30 seconds")
break
time.sleep(0.1)
loopcnt += 1
self.angleD = float(1.0*posd/self.device_to_deg)
self.angleR = float(1.0*posr/self.device_to_deg)
else:
print("Error thordevices.MoveMM: Cannot Move to given position")
#
def GetStageAxisInfo_MaxPos(self, serial):
return self.dll.ISC_GetStageAxisMaxPos(serial)
#
def GetStageAxisInfo_MinPos(self, serial):
return self.dll.ISC_GetStageAxisMinPos(serial)
#
def __del__(self):
self.dll.ISC_StopPolling(self.serialD)
self.dll.ISC_StopPolling(self.serialR)
self.dll.ISC_Close(self.serialD)
self.dll.ISC_Close(self.serialR)
#
#
class BSCRotationController():
'''
A class for controlling a Thorlabs larger size Rotation Stage, but more
genercally a "Benchtop Stepper Motor"
'''
def __init__(self, serialNum, autohome=True):
s = str(serialNum)
self.serialNum = serialNum
self.serial = byref(ctypes.create_string_buffer(s.encode('utf-8')))
self.channel = ctypes.c_short(1)
self.pollingrate = ctypes.c_int(200)
self.angle = 0.0
self.device_to_deg = pm.ROTATION_NanoRotator_step_to_deg
# Load the DLLs
self.device_manager = ctypes.CDLL(join(pm.DLL_Thorlabs, DLL_device)) #Here to make subsequent modules load
self.dll = ctypes.CDLL(join(pm.DLL_Thorlabs, DLL_BSC))
# Initialize the device
self.dll.TLI_BuildDeviceList() # Somehow allows it to find the device
self.dll.SBC_Open(self.serial)
self.dll.SBC_EnableChannel(self.serial, self.channel)
self.dll.SBC_StartPolling(self.serial, self.channel, self.pollingrate)
time.sleep(0.1)
self.minPos = 1.0*self.GetStageAxisInfo_MinPos()
self.maxPos = 1.0*self.GetStageAxisInfo_MaxPos()
try:
velocity = ctypes.c_int(pm.ROTATION_NanoRotator_default_vel)
acceleration = ctypes.c_int(pm.ROTATION_NanoRotator_default_acc)
current_accel = ctypes.c_int(0)
current_velocity = ctypes.c_int(0)
self.dll.SBC_SetVelParams(self.serial, self.channel, velocity, acceleration)
self.dll.SBC_GetVelParams(self.serial, self.channel, byref(current_velocity), byref(current_accel))
except Exception as e:
print(format_exc())
if autohome:
self.dll.SBC_Home(self.serial, self.channel)
time.sleep(0.25)
self.wait_till_stopped()
self.GetPosition()
#
def wait_till_stopped(self):
loopcnt = 0
time.sleep(0.1)
while self.dll.SBC_GetStatusBits(self.serial, self.channel) != Nanorotator_stop_bits:
if loopcnt > 300:
print("Error throdevices.RotationController.wait_till_stopped: Timed out after 30 seconds")
break
time.sleep(0.1)
loopcnt += 1
#
def GetPosition(self):
time.sleep(0.05)
pos = self.dll.SBC_GetPosition(self.serial, self.channel)
pos = float(1.0*pos/self.device_to_deg)
self.angle = pos
return pos
#
def MoveDeg(self, position, wait_forever=False):
if position >= 0.0 and position <= 360.0:
loopcnt = 0
pos = int(position*self.device_to_deg)
self.dll.SBC_MoveToPosition(self.serial, self.channel, pos)
while self.dll.SBC_GetPosition(self.serial, self.channel) != pos:
if loopcnt > 600 and not wait_forever:
print("Error throdevices.RotationController.MoveDeg: Timed out after 60 seconds")
break
self.angle = float(self.dll.SBC_GetPosition(self.serial, self.channel)/self.device_to_deg)
time.sleep(0.1)
loopcnt += 1
self.angle = float(self.dll.SBC_GetPosition(self.serial, self.channel)/self.device_to_deg)
else:
print("Error thordevices.MoveMM: Cannot Move to given position")
#
def GetStageAxisInfo_MaxPos(self):
return self.dll.SBC_GetStageAxisMaxPos(self.serial, self.channel)
#
def GetStageAxisInfo_MinPos(self):
return self.dll.SBC_GetStageAxisMinPos(self.serial, self.channel)
#
def __del__(self):
self.dll.SBC_StopPolling(self.serial, self.channel)
self.dll.SBC_Close(self.serial)
#
#