-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPiScAnt_with_scAnt_setup.py
354 lines (283 loc) · 10.5 KB
/
PiScAnt_with_scAnt_setup.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
# v.0.0.9007
from time import sleep
from time import strftime
from picamerax import PiCamera
import os
import subprocess
import tkinter as tk
from tkinter import messagebox
# range definitions
# ARM (x)
x_min = -120 # get_motor_pos('x')
x_max = 120 # x_min
positions_x = 5 # 5
# SPECIMEN TURNTABLE (y)
y_min = get_motor_pos('y') # get_motor_pos('y') + get_motor_pos('y')
y_max = y_min + 1600 # y_min + 1600
positions_y = 20 # 20
# FOCUS (z)
z_min = 4650
z_max = 50
positions_z = 80
def start_camera():
camera = PiCamera()
camera.resolution = (640, 480)
preview = camera.start_preview()
preview.fullscreen = False
preview.window = (800, 5, 640, 480)
def stop_camera():
camera.stop_preview()
camera.close()
camera = PiCamera()
camera.resolution = (640, 480)# (4056, 3040) # (1024, 768)
preview = camera.start_preview()
preview.fullscreen = False
preview.window = (800, 5, 640, 480)
x_axis_stop_dir = 'rev'
z_axis_stop_dir = 'fwd'
position_x = 0
position_y = 0
position_z = 0
def get_motor_ID(axis):
x_axis_ID = '00363443' # arm
y_axis_ID = '00338490' # specimen turntable
z_axis_ID = '00374283' # focus rail
if axis == 'x':
curr_axis_ID = x_axis_ID
if axis == 'y':
curr_axis_ID = y_axis_ID
if axis == 'z':
curr_axis_ID = z_axis_ID
return(curr_axis_ID)
def get_motor_pos(axis):
motor_ID = get_motor_ID(axis)
cmd = 'ticcmd -s -d ' + motor_ID + ' --full'
test = subprocess.check_output(cmd, shell=True)
test_string = test.decode()
index_1 = test_string.index("Current position:")
index_2 = test_string.index("Position uncertain:")
motor_pos = int(test_string[(index_1 + 17):(index_2 - 1)].strip(" "))# sleep shortly to read correct number
# print('Current position of ' + axis + '-axis: ' + str(motor_pos) + '.')
return(motor_pos)
def move_motor_by_steps(axis, by_steps):
curr_axis_ID = get_motor_ID(axis)
curr_position = get_motor_pos(axis)
# print(axis + '-axis now at ' + str(curr_position))
target = curr_position + by_steps
print('moving ' + axis + '-axis by ' + str(by_steps) + ' to ' + str(target) + '...')
cmd = 'ticcmd --exit-safe-start -d ' + curr_axis_ID + ' --position ' + str(target)
os.system(cmd)
# sleep shortly to read correct number
# sleep(0.25)
# print(axis + '-axis now at ' + str(get_motor_pos(axis)))
def energize():
cmd = 'ticcmd -d 00363443 --energize'
os.system(cmd)
cmd = 'ticcmd -d 00338490 --energize'
os.system(cmd)
cmd = 'ticcmd -d 00374283 --energize'
os.system(cmd)
move_motor_to('x', get_motor_pos('x')+1)
move_motor_to('y', get_motor_pos('y')+1)
move_motor_to('z', get_motor_pos('z')+1)
def deenergize():
cmd = 'ticcmd -d 00363443 --deenergize'
os.system(cmd)
cmd = 'ticcmd -d 00338490 --deenergize'
os.system(cmd)
cmd = 'ticcmd -d 00374283 --deenergize'
os.system(cmd)
def move_motor_to(axis, target):
curr_position = get_motor_pos(axis)
by_steps = target - curr_position
move_motor_by_steps(axis, by_steps)
sleep(abs(round(by_steps/2000, 2)))
curr_position = get_motor_pos(axis)
print(axis + '-axis now at ' + str(curr_position))
counter = 1
while curr_position != target:
counter += 1
curr_position = get_motor_pos(axis)
by_steps = target - curr_position
move_motor_by_steps(axis, by_steps)
sleep(abs(round(by_steps/2000, 2)))
curr_position = get_motor_pos(axis)
print(axis + '-axis now at ' + str(curr_position) + '(' +str(counter) + ')')
def shoot_image_scan(x, y, z):
# ~ x_pos = get_motor_pos('x')
# ~ y_pos = get_motor_pos('y')
# ~ z_pos = get_motor_pos('z')
sleep(0.5)
curr_image_name = './Chrysis_sp_02/x_' + str(x) + '_y_' + str(y) + '_z_' + str(z) + '.jpg'
# curr_image_name = './img_01/x_' + str(x_pos) + '_y_' + str(y_pos) + '_z_' + str(z_pos) + '.jpg'
# print('Saving ' + curr_image_name + '...')
cmd = 'raspistill -t 1 -o ' + curr_image_name + ' -n --ISO 200 --shutter 3000 -awb off -awbg 2.9,1.5 -sh 0, -co 0, -br 50, -sa 0'
os.system(cmd)
def start_scan():
camera.stop_preview()
camera.close()
# arm
steps_x = round((x_max - x_min) / positions_x)
print('steps_x = ' + str(steps_x))
# turntable
steps_y = round((y_max - y_min)/positions_y)
print('steps_y = ' + str(steps_y))
#focus
steps_z = round((z_max - z_min)/positions_z)
print('steps_z = ' + str(steps_z))
print('Making ' + str(positions_x*positions_y*(positions_z)) + ' photos,')
print('resulting in ~' + str(round(((positions_x*positions_y*positions_z)*4.11024)/1024, 2)) + ' GB.')
# subtract one from each positions value because at each second last step
# an additional action is taken
positions_x -= 1
positions_y -= 1
positions_z -= 1
# define initial motor positions
x_pos_init = x_min
y_pos_init = y_min
z_pos_init = z_min
# energize()
print('Moving all axes to start positions...')
move_motor_to('x', x_min)
# sleep(2)
move_motor_to('y', y_min)
# sleep(2)
move_motor_to('z', z_min)
# sleep(2)
for x in range(positions_x):
for y in range(positions_y):
for z in range(positions_z):
shoot_image_scan(x, y, z)
move_motor_by_steps('z', steps_z)
if(z == positions_z-1):
z += 1
shoot_image_scan(x, y, z)
move_motor_to('z', z_pos_init)
move_motor_by_steps('y', steps_y)
if(y == positions_y-1):
y += 1
for z in range(positions_z):
shoot_image_scan(x, y, z)
move_motor_by_steps('z', steps_z)
if(z == positions_z-1):
z += 1
shoot_image_scan(x, y, z)
move_motor_to('z', z_pos_init)
# move_motor_to('y', y_pos_init)
move_motor_by_steps('x', steps_x)
sleep(1)
if(x == positions_x-1):
x += 1
for y in range(positions_y):
for z in range(positions_z):
shoot_image_scan(x, y, z)
move_motor_by_steps('z', steps_z)
if(z == positions_z-1):
z += 1
shoot_image_scan(x, y, z)
move_motor_to('z', z_pos_init)
move_motor_by_steps('y', steps_y)
if(y == positions_y-1):
y += 1
for z in range(positions_z):
shoot_image_scan(x, y, z)
move_motor_by_steps('z', steps_z)
if(z == positions_z-1):
z += 1
shoot_image_scan(x, y, z)
move_motor_to('z', z_pos_init)
# move_motor_to('y', y_pos_init)
print('done')
# GUI
root = tk.Tk()
root.title("piscAnt v.0.0.9004")
# Fixing the window size.
root.minsize(width=350, height=70)
label = tk.Label(root, text="piscAnt", fg="black", font="Verdana 14 bold")
energize_b = tk.Button(root,
text='Energize',
bg='green',
width=20,
state='normal',
command=lambda: energize())
deenergize_b = tk.Button(root,
text='De-Energize',
bg='red',
width=20,
state='normal',
command=lambda: deenergize())
lable_turntable = tk.Label(
root,
text='specimen turntable',
)
lable_arm = tk.Label(
root,
text='specimen arm',
)
lable_focus = tk.Label(
root,
text='specimen focus',
)
left_sm = tk.Button(root, text='< turntable left',
width=15, state='normal', command=lambda: move_motor_by_steps('y', -80))
right_sm = tk.Button(root, text='turntable right >',
width=15, state='normal', command=lambda: move_motor_by_steps('y', +80))
left_big = tk.Button(root, text='<< turntable left',
width=15, state='normal', command=lambda: move_motor_by_steps('y', -320))
right_big = tk.Button(root, text='turntable right >>',
width=15, state='normal', command=lambda: move_motor_by_steps('y', +320))
arm_left_sm = tk.Button(root, text='< arm further',
width=15, state='normal', command=lambda: move_motor_by_steps('x', +10))
arm_right_sm = tk.Button(root, text='arm closer >',
width=15, state='normal', command=lambda: move_motor_by_steps('x', -10))
arm_left_big = tk.Button(root, text='<< arm further',
width=15, state='normal', command=lambda: move_motor_by_steps('x', +50))
arm_right_big = tk.Button(root, text='arm closer >>',
width=15, state='normal', command=lambda: move_motor_by_steps('x', -50))
focus_left_sm = tk.Button(root, text='< focus closer',
width=15, state='normal', command=lambda: move_motor_by_steps('z', +100))
focus_right_sm = tk.Button(root, text='focus further >',
width=15, state='normal', command=lambda: move_motor_by_steps('z', -100))
focus_left_big = tk.Button(root, text='<< focus closer',
width=15, state='normal', command=lambda: move_motor_by_steps('z', +2000))
focus_right_big = tk.Button(root, text='focus further >>',
width=15, state='normal', command=lambda: move_motor_by_steps('z', -2000))
start_scan_b = tk.Button(root, text='Start Scan!',
width=15, state='normal', command=lambda: start_scan())
# end = tk.Button(root, text='Exit',
# width=8, state='normal', command=ExitApplication)
r=0
energize_b.grid(row=r,column=0)
deenergize_b.grid(row=r,column=1)
# ~ r+=1
# ~ shoot.grid(row=r, column=0, columnspan = 2)
r+=1
lable_turntable.grid(row=r, column=0, columnspan = 2)
r+=1
left_sm.grid(row=r,column=0)
right_sm.grid(row=r,column=1)
r+=1
left_big.grid(row=r,column=0)
right_big.grid(row=r,column=1)
r+=1
lable_arm.grid(row=r, column=0, columnspan = 2)
r+=1
arm_left_sm.grid(row=r,column=0)
arm_right_sm.grid(row=r,column=1)
r+=1
arm_left_big.grid(row=r,column=0)
arm_right_big.grid(row=r,column=1)
r+=1
lable_focus.grid(row=r, column=0, columnspan = 2)
r+=1
focus_left_sm.grid(row=r,column=0)
focus_right_sm.grid(row=r,column=1)
r+=1
focus_left_big.grid(row=r,column=0)
focus_right_big.grid(row=r,column=1)
r+=1
start_scan_b.grid(row=r,column=0, columnspan = 2)
# end.grid(row=r,column=1)
# fan_off['state']='disabled'
# pump_off['state']='disabled'
root.mainloop()