-
Notifications
You must be signed in to change notification settings - Fork 0
/
fluo_timelapse.py
executable file
·308 lines (241 loc) · 9.27 KB
/
fluo_timelapse.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
#!/usr/bin/env python3
import os
import sys
import time
import json
import argparse
# filter wheel
import hid
# relay switch
import serial
# camera
import gphoto2 as gp
DEBUG = True
config_dict = {
'camera': {
'aWHITE': {
'exp': '1/4',
'iso': '200',
'f_val': '2',
},
'bGFP': {
'exp': '1/8',
'iso': '200',
'f_val': '1',
},
'cCFP': {
'exp': '1/8',
'iso': '400',
'f_val': '2',
},
'mCherry': {
'exp': '1/4',
'iso': '400',
'f_val': '1',
},
},
'wheel': {
'ven_id': 0x1278,
'pro_id': 0x0920,
'filters': ['aWHITE', 'bGFP', 'cCFP', 'mCherry'],
},
'relay': {
'path': '/dev/tty.usbmodem1421',
},
'interval': 5,
'work_dir': '.',
'out_fmt': 'arw',
}
# class CameraException(Exception):
# pass
#
# class WheelException(Exception):
# pass
#
# class RelayException(Exception):
# pass
def clean_env(camera, wheel, relay):
if DEBUG:
print('Closing camera connection...', file=sys.stderr)
gp.check_result(gp.gp_camera_exit(camera))
if DEBUG:
print('Camera connection closed', file=sys.stderr)
print('Closing filter wheel connection...', file=sys.stderr)
wheel.close()
if DEBUG:
print('Filter wheel connection closed', file=sys.stderr)
print('Closing relay switch connection...', file=sys.stderr)
relay.close()
if DEBUG:
print('Relay switch connection closed', file=sys.stderr)
def set_camera_config(camera, exp, iso, f_val):
if DEBUG:
print('Getting previous camera configuration...', file=sys.stderr)
camera_config = camera.get_config()
error, exp_conf = gp.gp_widget_get_child_by_name(camera_config, 'shutterspeed')
assert error == 0, "ERROR while retrieving current exposure"
error, iso_conf = gp.gp_widget_get_child_by_name(camera_config, 'iso')
assert error == 0, "ERROR while retrieving current ISO"
error, f_conf = gp.gp_widget_get_child_by_name(camera_config, 'f-number')
assert error == 0, "ERROR while retrieving current aperture"
error = gp.check_result(gp.gp_widget_set_value(exp_conf, exp))
assert error == 0, "ERROR while setting exposure to {}".format(exp)
error = gp.check_result(gp.gp_widget_set_value(iso_conf, iso))
assert error == 0, "ERROR while setting ISO to {}".format(iso)
error = gp.check_result(gp.gp_widget_set_value(f_conf, f_val))
assert error == 0, "ERROR while setting aperture to {}".format(f_val)
if DEBUG:
print("Setting new camera configuration (exp {}, iso {}, f {})...".format(exp, iso, f_val), file=sys.stderr)
error = gp.check_result(gp.gp_camera_set_config(camera, camera_config))
assert error == 0, "ERROR while setting camera configuration"
if DEBUG:
print('New camera configuration set', file=sys.stderr)
def timelapse(camera, wheel, relay, config_dict):
ch_idx = 0
# TODO: detect pictures already present in work_dir and continue numbering
capture_idx = -1
work_dir = config_dict['work_dir']
interval = config_dict['interval']
out_fmt = config_dict['out_fmt']
channels = config_dict['wheel']['filters']
assert len(config_dict['camera']) == len(channels), "ERROR: Different number of channels for camera and filter wheel"
ch = channels[0]
# INIT
exp = str(config_dict['camera'][ch]['exp'])
iso = str(config_dict['camera'][ch]['iso'])
f_val = float(config_dict['camera'][ch]['f_val'])
set_camera_config(camera, exp, iso, f_val)
relay.write("reset\n\r".encode('utf-8'))
wheel.write([1, 0])
try:
while True:
if ch_idx == 0:
capture_idx += 1
if DEBUG:
print("CHANNEL {} (ch) [IT {}]".format(ch_idx, capture_idx), file=sys.stderr)
# LIGHTS UP AND CAPTURE
if DEBUG:
print("Lights up...", file=sys.stderr)
relay_cmd = "relay on {}\n\r".format(ch_idx).encode('utf-8')
relay.write(relay_cmd)
if DEBUG:
print("Lights up! Relay status:", file=sys.stderr)
relay_cmd = "relay readall\n\r".encode('utf-8')
relay.write(relay_cmd)
print(relay.readlines(), file=sys.stderr)
print("Shoot...", file=sys.stderr)
camera_fn = gp.check_result(gp.gp_camera_capture(camera, gp.GP_CAPTURE_IMAGE))
if DEBUG:
print("Shoot!", file=sys.stderr)
print("Lights down...", file=sys.stderr)
relay_cmd = "relay off {}\n\r".format(ch_idx).encode('utf-8')
relay.write(relay_cmd)
if DEBUG:
print("Lights down! Relay status:", file=sys.stderr)
relay_cmd = "relay readall\n\r".encode('utf-8')
relay.write(relay_cmd)
print(relay.readlines(), file=sys.stderr)
# SAVE PICTURE
# TODO: save all channels' pictures during sleep
if DEBUG:
print('Saving picture...', file=sys.stderr)
camera_f = gp.check_result(gp.gp_camera_file_get(camera, camera_fn.folder, camera_fn.name, gp.GP_FILE_TYPE_NORMAL))
out_fn = os.path.join(work_dir, "{}_{}.{}".format(str(capture_idx).zfill(10), ch, out_fmt))
gp.check_result(gp.gp_file_save(camera_f, out_fn))
if DEBUG:
print('Picture saved', file=sys.stderr)
if ch_idx == 0:
time_first_shot = time.time()
# GET READY FOR NEXT SHOT
ch_idx = (ch_idx+1) % len(channels)
ch = channels[ch_idx]
# TODO: multithreaded/asynchronous config
exp = str(config_dict['camera'][ch]['exp'])
iso = str(config_dict['camera'][ch]['iso'])
f_val = float(config_dict['camera'][ch]['f_val'])
set_camera_config(camera, exp, iso, f_val)
# TODO: check that the wheel is on the right position
if DEBUG:
print('Rotating filter wheel...', file=sys.stderr)
wheel.write([ch_idx+1, 0])
time.sleep(1)
if DEBUG:
print('Filter wheel rotated', file=sys.stderr)
if ch_idx == 0:
# just to be sure...if relay off command lost, screw up only one shot
relay.write("reset\n\r".encode('utf-8'))
if DEBUG:
print("Relay switch reset! Relay status:", file=sys.stderr)
relay_cmd = "relay readall\n\r".encode('utf-8')
relay.write(relay_cmd)
print(relay.readlines(), file=sys.stderr)
# TODO: sleep the diff between time of first shot and now
# (so that same channel has ~ interval)
print("Going to sleep", file=sys.stderr)
time.sleep(interval)
except KeyboardInterrupt:
clean_env(camera, wheel, relay)
def init_camera(**kwd_args):
context = gp.gp_context_new()
error, camera = gp.gp_camera_new()
error = gp.gp_camera_init(camera, context)
if DEBUG:
error, summary = gp.gp_camera_get_summary(camera, context)
print('Summary', file=sys.stderr)
print('=======', file=sys.stderr)
print(summary.text, file=sys.stderr)
return camera
def init_wheel(ven_id, pro_id, **kwd_args):
wheel = hid.device()
wheel.open(ven_id, pro_id)
if DEBUG:
# TODO: check filter total positions
if not wheel:
print("Error", file=sys.stderr)
return wheel
def init_relay(path, **kwd_args):
relay = serial.Serial(path, 19200, timeout=1)
relay.write(b'reset\n\r')
if DEBUG:
relay.readlines()
relay.write(b'relay readall\n\r')
res = relay.readlines()
# TODO: check that all relays are off
if not res:
print("Error", file=sys.stderr)
return relay
def parse_args():
desc = "Script for running fluorescent timelapses"
parser = argparse.ArgumentParser(description=desc)
parser.add_argument(
'config_fn',
metavar='conf.json',
type=str,
help='json file containing the channel configurations'
)
args = parser.parse_args()
return args
def main(config_dict):
if DEBUG:
print('Initializing camera connection...', file=sys.stderr)
camera = init_camera(**config_dict['camera'])
if DEBUG:
print('Camera connection initialized', file=sys.stderr)
print('Initializing filter wheel connection...', file=sys.stderr)
wheel = init_wheel(**config_dict['wheel'])
if DEBUG:
print('Filter wheel connection initialized', file=sys.stderr)
print('Initializing relay switch connection...', file=sys.stderr)
relay = init_relay(**config_dict['relay'])
if DEBUG:
print('Relay switch connection initialized', file=sys.stderr)
print('Starting timelapse', file=sys.stderr)
timelapse(camera, wheel, relay, config_dict)
if __name__ == '__main__':
if len(sys.argv) > 1:
args = parse_args()
with open(args.config_fn, 'r') as config_f:
config_dict = json.load(config_f)
if DEBUG:
print(config_dict, file=sys.stderr)
main(config_dict)