forked from freespace/pyAPT
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlinearstage_new.py
316 lines (260 loc) · 8.31 KB
/
linearstage_new.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
# -*- coding: utf-8 -*-
'''
@brief Class that represents a Thorlabs 3D linear stage.
It allows the user to perform different types of scans throught
the workspace.
@author Luis Carlos Garcia-Peraza Herrera <[email protected]>
@author Efthymios Maneas <[email protected]>
The coordinate system of the 3D linear stage is as follows:
Top view: Right view: Front view:
--------- ----------- -----------
Motor controllers
__ __ __
|__||__||__|
Y Z Z
^ ^ ^
| | | __ __ __
| | __ | | || || |
o-----> X o-----> Y |__| o-----> X
'''
import pyAPT
import threading
import time
from runner import runner_serial
import yaml
class LinearStage(object):
config = yaml.load(open("configfile.yml")) # $ pip install pyyaml
X_AXIS_SN = config["X_AXIS_SN"]
Y_AXIS_SN = config["Y_AXIS_SN"]
Z_AXIS_SN = config["Z_AXIS_SN"]
MAX_DIST = config["MAX_DIST"]
ENCODER_SCALE = config["ENCODER_SCALE"]
MAX_DIST_ENCODER = MAX_DIST * ENCODER_SCALE
RIGHT = config["RIGHT"]
LEFT = config["LEFT"]
DOWN = config["DOWN"]
UP = config["UP"]
'''
@brief TODO
'''
# def __init__(self):
# self.running = True
# self.threads = []
def getInfoAxis(self, axis):
con = pyAPT.MTS50(serial_number=axis)
return con.info()
'''
@brief Prints the serial number, model, type, firmware version and servo of all the connected stages.
'''
def getInfo(self):
labels = ['S/N', 'Model', 'Type', 'Firmware Ver', 'Notes', 'H/W Ver', 'Mod State', 'Channels']
xInfo = self.getInfoAxis(self.X_AXIS_SN)
yInfo = self.getInfoAxis(self.Y_AXIS_SN)
zInfo = self.getInfoAxis(self.Z_AXIS_SN)
print("\nInformation of the X axis:")
print('--------------------------')
for idx, ainfo in enumerate(xInfo):
print("\t%12s: %s" % (labels[idx], bytes(ainfo)))
print("\nInformation of the Y axis:")
print('--------------------------')
for idx, ainfo in enumerate(yInfo):
print("\t%12s: %s" % (labels[idx], bytes(ainfo)))
print("\nInformation of the Z axis:")
print('--------------------------')
for idx, ainfo in enumerate(zInfo):
print("\t%12s: %s" % (labels[idx], bytes(ainfo)))
print("\n")
'''
@brief Obtains the current position, velocity and status of a linear stage connected through USB.
@param[in] axis Serial number of the target linear stage.
@returns Status for the stage with the serial number provided.
'''
def getStatusAxis(self, axis):
con = pyAPT.MTS50(serial_number=axis)
return con.status()
'''
@brief Prints the axis, position and velocity of the connected stages.
'''
def getStatus(self):
xStatus = self.getStatusAxis(self.X_AXIS_SN)
yStatus = self.getStatusAxis(self.Y_AXIS_SN)
zStatus = self.getStatusAxis(self.Z_AXIS_SN)
print("\nAxis: Position [mm]: Velocity [mm/s]:")
print('----- -------------- ----------------')
print("X %6.3f %6.3f" % (float(self.MAX_DIST) - xStatus.position, xStatus.velocity))
print("Y %6.3f %6.3f" % (yStatus.position, yStatus.velocity))
print("Z %6.3f %6.3f\n" % (float(self.MAX_DIST) - zStatus.position, zStatus.velocity))
'''
@brief Provides the position of one or all axes.
@param[in] axis String with the name of the axis we want to retrieve.
@returns position[s] [X, Y, Z] of the stage.
'''
def getPos(self, axis=None):
if (axis == 'X' or axis == 'x' or axis == None):
with
pyAPT.MTS50(serial_number=self.X_AXIS_SN)
as
con:
status = con.status()
posX = float(self.MAX_DIST_ENCODER - status.position_apt) / self.ENCODER_SCALE
if (axis != None):
return posX
if (axis == 'Y' or axis == 'y' or axis == None):
with
pyAPT.MTS50(serial_number=self.Y_AXIS_SN)
as
con:
status = con.status()
posY = float(status.position_apt) / self.ENCODER_SCALE
if (axis != None):
return posY
if (axis == 'Z' or axis == 'z' or axis == None):
with
pyAPT.MTS50(serial_number=self.Z_AXIS_SN)
as
con:
status = con.status()
posZ = float(self.MAX_DIST_ENCODER - status.position_apt) / self.ENCODER_SCALE
if (axis != None):
return posZ
return [posX, posY, posZ]
'''
@brief Sends the 3D linear stage to the position (0, 0, 0).
'''
def goHome(self):
con = pyAPT.MTS50(serial_number=self.X_AXIS_SN)
con.home(velocity=40)
con = pyAPT.MTS50(serial_number=self.Y_AXIS_SN)
con.home(velocity=40)
con = pyAPT.MTS50(serial_number=self.Z_AXIS_SN)
con.home(velocity=40)
self.moveAbsolute(0, 0, 0)
'''
@brief This method performs a 3D raster scan.
@param[in] step Increment in microns from point to point.
@param[in] delayMs Time delay after each position has been reached. It stabilises the movement.
'''
def rasterScan(self, step, delayMs):
# Going home to reset the encoders
print('Homing... ')
self.goHome()
print("OK\n")
# Setting the initial direction of the X (k) and Y(j) axes
kDir = self.RIGHT
jDir = self.DOWN
# Initialising iterators
i = 0
j = 0
k = 0
# Looping through the workspace in a raster fashion
while (i <= self.MAX_DIST):
if j > self.MAX_DIST:
j = self.MAX_DIST
jDir = self.UP
if j < 0:
j = 0
jDir = self.DOWN
while (j >= 0 and j <= self.MAX_DIST):
if k > self.MAX_DIST:
k = self.MAX_DIST
kDir = self.LEFT
if k < 0:
k = 0
kDir = self.RIGHT
while (k >= 0 and k <= self.MAX_DIST):
self.moveAbsolute(k, j, i)
time.sleep(delayMs / 1000)
if kDir == self.RIGHT:
k += step
else:
k -= step
if jDir == self.DOWN:
j += step
else:
j -= step
i += step
'''
@brief TODO
@param[in] stepX TODO
@param[in] stepY TODO
@param[in] stepZ TODO
@param[in] delayMs TODO
'''
def spiralScan(self, stepX, stepY, stepZ, delayMs):
return 0
'''
TODO
'''
def moveAbsoluteX(self, x):
x = float(self.MAX_DIST) - x
con = pyAPT.MTS50(serial_number=self.X_AXIS_SN)
con.goto(x, wait=False)
stat = con.status()
while stat.moving:
out = ' pos %3.2fmm vel %3.2fmm/s' % (stat.position, stat.velocity)
# sys.stdout.write(out)
time.sleep(0.01)
stat = con.status()
l = len(out)
# sys.stdout.write('\b'*l)
# sys.stdout.write(' '*l)
# sys.stdout.write('\b'*l)
'''
TODO
'''
def moveAbsoluteY(self, y):
con = pyAPT.MTS50(serial_number=self.Y_AXIS_SN)
con.goto(y, wait=False)
stat = con.status()
while stat.moving:
out = ' pos %3.2fmm vel %3.2fmm/s' % (stat.position, stat.velocity)
# sys.stdout.write(out)
time.sleep(0.01)
stat = con.status()
l = len(out)
# sys.stdout.write('\b'*l)
# sys.stdout.write(' '*l)
# sys.stdout.write('\b'*l)
'''
TODO
'''
def moveAbsoluteZ(self, z):
z = float(self.MAX_DIST) - z
con = pyAPT.MTS50(serial_number=self.Z_AXIS_SN)
con.goto(z, wait=False)
stat = con.status()
while stat.moving:
out = ' pos %3.2fmm vel %3.2fmm/s' % (stat.position, stat.velocity)
# sys.stdout.write(out)
time.sleep(0.01)
stat = con.status()
l = len(out)
# sys.stdout.write('\b'*l)
# sys.stdout.write(' '*l)
# sys.stdout.write('\b'*l)
'''
@brief TODO
@param[in] x TODO
@param[in] y TODO
@param[in] z TODO
@param[in] delayMs TODO
'''
def moveAbsolute(self, x, y, z):
tx = threading.Thread(target=self.moveAbsoluteX(x))
ty = threading.Thread(target=self.moveAbsoluteY(y))
tz = threading.Thread(target=self.moveAbsoluteZ(z))
tx.daemon = True
ty.daemon = True
tz.daemon = True
tx.start()
ty.start()
tz.start()
'''
@brief TODO
@param[in] x TODO
@param[in] y TODO
@param[in] z TODO
@param[in] delayMs TODO
'''
def moveRelative(self, x, y, z):
return 0