-
Notifications
You must be signed in to change notification settings - Fork 0
/
Zaber_Full_Automation.py
335 lines (282 loc) · 8.56 KB
/
Zaber_Full_Automation.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
from zaber_motion import Units
from zaber_motion.ascii import Connection
import numpy as np
import keyboard
import pyautogui as pg
import time
def automatic():
l = input("Enter desired capture length of sample (mm): ")
l = int(np.ceil(float(l)))
w = input("Enter desired capture width of sample (mm): ")
w = int(np.ceil(float(w)))
s = input("Enter field of view (mm): ")
o = input("Enter overlap percentage: ")
step = float(s)*((100 - float(o)) / 100) # Step size for motion
return l,w,step
def manual():
sn_type = input("Would you like to:\n"
"1. Snake by rows?\n"
"2. Snake by columns?\n")
sn_type = int(sn_type)
if sn_type == 1:
row_manual()
elif sn_type == 2:
col_manual()
def input_step():
step = input("Enter step size in mm: ")
print("Thank you. If you would like to change your step size at any point, press 'c'.")
print("Please use arrow keys to navigate. Hit 'f' to finish\n")
return float(step)
def free_move():
step = input_step()
while not keyboard.is_pressed('f'):
if keyboard.is_pressed('left'):
left(step)
pg.press('right')
elif keyboard.is_pressed('right'):
right(step)
pg.press('left')
elif keyboard.is_pressed('up'):
up(step)
pg.press('down')
elif keyboard.is_pressed('down'):
down(step)
pg.press('up')
elif keyboard.is_pressed('c'):
pg.press('backspace')
step = input_step()
else:
continue
pg.press('backspace')
def set_origin():
origin = input("Enter your origin in mm(x,y): ")
a, b = origin.split(",")
axis1.move_absolute(float(a), Units.LENGTH_MILLIMETRES)
axis2.move_absolute(float(b), Units.LENGTH_MILLIMETRES)
def col_manual():
l,w,step = automatic()
i = 0
j = 0
k = 0 # Image counter
while i<l:
if (i % 2 == 0):
j = 0
while (j < w-1):
# get_image()
# save_image(tile_{k}.tif) to directory
if keyboard.is_pressed('spacebar'):
up(step)
j += 1
k += 1
else:
j = w
while (j > 1):
# get_image()
# save_image(tile_{k}.tif) to directory
if keyboard.is_pressed('spacebar'):
down(step)
j -= 1
k += 1
keyboard.wait('spacebar')
right(step)
i += 1
def row_manual():
l,w,step = automatic()
i = 0
j = 0
k = 0 # Image counter
while i<w:
if (i % 2 == 0):
j = 0
while (j < l-1):
# get_image()
# save_image(tile_{k}.tif) to directory
if keyboard.is_pressed('spacebar'):
right(step)
j += 1
k += 1
else:
j = l
while (j > 1):
# get_image()
# save_image(tile_{k}.tif) to directory
if keyboard.is_pressed('spacebar'):
left(step)
j -= 1
k += 1
keyboard.wait('spacebar')
up(step)
i += 1
def snake_columns():
l, w, step = automatic()
k = 0 # Image counter
for i in range(0, l):
if (i % 2 == 0):
j = 0
while (j < w-1):
# get_image()
# save_image(tile_{k}.tif) to directory
up(step)
j += 1
k += 1
else:
j = w
while (j > 1):
# get_image()
# save_image(tile_{k}.tif) to directory
down(step)
j -= 1
k += 1
right(step)
def snake_rows():
l, w, step = automatic()
k = 0 # Image counter
for i in range(0, w):
if (i % 2 == 0):
j = 0
while (j < l-1):
# get_image()
# save_image(tile_{k}.tif) to directory
right(step)
j += 1
k += 1
else:
j = l
while (j > 1):
# get_image()
# save_image(tile_{k}.tif) to directory
left(step)
j -= 1
k += 1
up(step)
def full_auto_col():
l, w, step = automatic()
print("Press hit enter after navigating to imaging software and ready")
keyboard.wait('enter')
k = 1 # Image counter
for i in range(0, l):
if (i % 2 == 0):
j = 0
while (j < w - 1):
time.sleep(0.5)
save(k)
up(step)
j += 1
k += 1
else:
j = w
while (j > 1):
time.sleep(0.5)
save(k)
down(step)
j -= 1
k += 1
time.sleep(0.5)
save(k)
k += 1
right(step)
def full_auto_row():
l, w, step = automatic()
print("Press hit enter after navigating to imaging software and ready")
keyboard.wait('enter')
k = 1 # Image counter
for i in range(0, w):
if (i % 2 == 0):
j = 0
while (j < l-1):
time.sleep(0.5)
save(k)
right(step)
j += 1
k += 1
else:
j = l
while (j > 1):
time.sleep(0.5)
save(k)
left(step)
j -= 1
k += 1
time.sleep(0.5)
save(k)
k += 1
up(step)
def save(k):
pg.hotkey('ctrl', 's')
if k <= 9:
pg.write('tile_0' + str(k) + '.tif')
pg.press('enter')
else:
pg.write('tile_' + str(k) + '.tif')
pg.press('enter')
def reset():
axis1.move_absolute(12.5, Units.LENGTH_MILLIMETRES)
axis2.move_absolute(12.5, Units.LENGTH_MILLIMETRES)
def left(a):
try:
axis1.move_relative(float(a), Units.LENGTH_MILLIMETRES)
except Exception:
print("Out of Range - left")
def right(a):
try:
axis1.move_relative(-float(a), Units.LENGTH_MILLIMETRES)
except Exception:
print("Out of Range - right")
def up(a):
try:
axis2.move_relative(-float(a), Units.LENGTH_MILLIMETRES)
except Exception:
print("Out of Range - up")
def down(a):
try:
axis2.move_relative(float(a), Units.LENGTH_MILLIMETRES)
except Exception:
print("Out of Range - down")
with Connection.open_serial_port("COM5") as connection:
connection.enable_alerts()
device_list = connection.detect_devices()
print("Found {} devices".format(len(device_list)))
print(device_list)
dev1 = device_list[0]
dev2 = device_list[1]
axis1 = dev2.get_axis(1)
axis2 = dev1.get_axis(1)
if not axis1.is_homed():
axis1.home()
if not axis2.is_homed():
axis2.home()
option = input("Welcome! Would you like to:\n"
"1. Move the Zaber manually (with your keyboard)\n"
"2. Snake automatically for sample inspection?\n"
"3. Snake for image capture (using spacebar)?\n"
"4. Complete automation including image capture?\n")
option = int(option)
set_in = input("Set origin by coordinates (1) or manually by keyboard (2)?\n")
set_in = int(set_in)
if set_in == 1:
set_origin()
elif set_in == 2:
free_move()
if option == 1:
print("You have chosen to move the Zaber stages manually")
free_move()
elif option == 2:
sn = input("1. Snake by rows? \n2. Snake by columns?\n")
sn = int(sn)
if sn == 1:
snake_rows()
elif sn == 2:
snake_columns()
elif option == 3:
manual()
elif option == 4:
sn = input("1. Snake by rows? \n2. Snake by columns?\n")
sn = int(sn)
if sn == 1:
full_auto_row()
elif sn == 2:
full_auto_col()
else:
print("Invalid, try again")
# Close the serial port when done
connection.close()