forked from labscript-suite/labscript-devices
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCamera.py
329 lines (280 loc) · 14.5 KB
/
Camera.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
#####################################################################
# #
# /labscript_devices/Camera.py #
# #
# Copyright 2013, Monash University #
# #
# This file is part of labscript_devices, in the labscript suite #
# (see http://labscriptsuite.org), and is licensed under the #
# Simplified BSD License. See the license.txt file in the root of #
# the project for the full license. #
# #
#####################################################################
from __future__ import division, unicode_literals, print_function, absolute_import
from labscript_utils import check_version
check_version('labscript', '2.0.1', '3')
check_version('zprocess', '2.4.8', '3')
from labscript_utils import PY2
if PY2:
str = unicode
from labscript_devices import BLACS_tab
from labscript import TriggerableDevice, LabscriptError, set_passed_properties
import numpy as np
class Camera(TriggerableDevice):
description = 'Generic Camera'
# To be set as instantiation arguments:
trigger_edge_type = None
minimum_recovery_time = None
@set_passed_properties(
property_names = {
"connection_table_properties": ["BIAS_port"],
"device_properties": ["serial_number", "SDK", "effective_pixel_size", "exposure_time", "orientation", "trigger_edge_type", "minimum_recovery_time"]}
)
def __init__(self, name, parent_device, connection,
BIAS_port = 1027, serial_number = 0x0, SDK='', effective_pixel_size=0.0,
exposure_time=float('nan'), orientation='side', trigger_edge_type='rising', minimum_recovery_time=0,
**kwargs):
# not a class attribute, so we don't have to have a subclass for each model of camera:
self.trigger_edge_type = trigger_edge_type
self.minimum_recovery_time = minimum_recovery_time
self.exposure_time = exposure_time
self.orientation = orientation
self.BLACS_connection = BIAS_port
if isinstance(serial_number, str) or isinstance(serial_number, bytes):
serial_number = int(serial_number,16)
self.sn = np.uint64(serial_number)
self.sdk = str(SDK)
self.effective_pixel_size = effective_pixel_size
self.exposures = []
# DEPRECATED: backward compatibility:
if 'exposuretime' in kwargs:
# We will call self.set_property later to overwrite the non-underscored kwarg's default value.
self.exposure_time = kwargs.pop('exposuretime')
import sys
sys.stderr.write('WARNING: Camera\'s keyword argument \'exposuretime\' deprecated. Use \'exposure_time\' instead.\n')
TriggerableDevice.__init__(self, name, parent_device, connection, **kwargs)
def expose(self, name, t , frametype, exposure_time=None):
if exposure_time is None:
duration = self.exposure_time
else:
duration = exposure_time
if duration is None:
raise LabscriptError('Camera %s has not had an exposure_time set as an instantiation argument, '%self.name +
'and one was not specified for this exposure')
if not duration > 0:
raise LabscriptError("exposure_time must be > 0, not %s"%str(duration))
# Only ask for a trigger if one has not already been requested by
# another camera attached to the same trigger:
already_requested = False
for camera in self.trigger_device.child_devices:
if camera is not self:
for _, other_t, _, other_duration in camera.exposures:
if t == other_t and duration == other_duration:
already_requested = True
if not already_requested:
self.trigger_device.trigger(t, duration)
# Check for exposures too close together (check for overlapping
# triggers already performed in self.trigger_device.trigger()):
start = t
end = t + duration
for exposure in self.exposures:
_, other_t, _, other_duration = exposure
other_start = other_t
other_end = other_t + other_duration
if abs(other_start - end) < self.minimum_recovery_time or abs(other_end - start) < self.minimum_recovery_time:
raise LabscriptError('%s %s has two exposures closer together than the minimum recovery time: ' %(self.description, self.name) + \
'one at t = %fs for %fs, and another at t = %fs for %fs. '%(t,duration,start,duration) + \
'The minimum recovery time is %fs.'%self.minimum_recovery_time)
self.exposures.append((name, t, frametype, duration))
return duration
def do_checks(self):
# Check that all Cameras sharing a trigger device have exposures when we have exposures:
for camera in self.trigger_device.child_devices:
if camera is not self:
for exposure in self.exposures:
if exposure not in camera.exposures:
_, start, _, duration = exposure
raise LabscriptError('Cameras %s and %s share a trigger. ' % (self.name, camera.name) +
'%s has an exposure at %fs for %fs, ' % (self.name, start, duration) +
'but there is no matching exposure for %s. ' % camera.name +
'Cameras sharing a trigger must have identical exposure times and durations.')
def generate_code(self, hdf5_file):
self.do_checks()
table_dtypes = [('name','a256'), ('time',float), ('frametype','a256'), ('exposure_time',float)]
data = np.array(self.exposures,dtype=table_dtypes)
group = self.init_device_group(hdf5_file)
if self.exposures:
group.create_dataset('EXPOSURES', data=data)
# DEPRECATED backward campatibility for use of exposuretime keyword argument instead of exposure_time:
self.set_property('exposure_time', self.exposure_time, location='device_properties', overwrite=True)
import os
from qtutils.qt.QtCore import *
from qtutils.qt.QtGui import *
from blacs.tab_base_classes import Worker, define_state
from blacs.tab_base_classes import MODE_MANUAL, MODE_TRANSITION_TO_BUFFERED, MODE_TRANSITION_TO_MANUAL, MODE_BUFFERED
from blacs.device_base_class import DeviceTab
from qtutils import UiLoader
import qtutils.icons
@BLACS_tab
class CameraTab(DeviceTab):
def initialise_GUI(self):
layout = self.get_tab_layout()
ui_filepath = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'camera.ui')
self.ui = UiLoader().load(ui_filepath)
layout.addWidget(self.ui)
port = int(self.settings['connection_table'].find_by_name(self.settings["device_name"]).BLACS_connection)
self.ui.port_label.setText(str(port))
self.ui.check_connectivity_pushButton.setIcon(QIcon(':/qtutils/fugue/arrow-circle'))
self.ui.host_lineEdit.returnPressed.connect(self.update_settings_and_check_connectivity)
self.ui.use_zmq_checkBox.toggled.connect(self.update_settings_and_check_connectivity)
self.ui.check_connectivity_pushButton.clicked.connect(self.update_settings_and_check_connectivity)
def get_save_data(self):
return {'host': str(self.ui.host_lineEdit.text()), 'use_zmq': self.ui.use_zmq_checkBox.isChecked()}
def restore_save_data(self, save_data):
if save_data:
host = save_data['host']
self.ui.host_lineEdit.setText(host)
if 'use_zmq' in save_data:
use_zmq = save_data['use_zmq']
self.ui.use_zmq_checkBox.setChecked(use_zmq)
else:
self.logger.warning('No previous front panel state to restore')
# call update_settings if primary_worker is set
# this will be true if you load a front panel from the file menu after the tab has started
if self.primary_worker:
self.update_settings_and_check_connectivity()
def initialise_workers(self):
worker_initialisation_kwargs = {'port': self.ui.port_label.text()}
self.create_worker("main_worker", CameraWorker, worker_initialisation_kwargs)
self.primary_worker = "main_worker"
self.update_settings_and_check_connectivity()
@define_state(MODE_MANUAL, queue_state_indefinitely=True, delete_stale_states=True)
def update_settings_and_check_connectivity(self, *args):
icon = QIcon(':/qtutils/fugue/hourglass')
pixmap = icon.pixmap(QSize(16, 16))
status_text = 'Checking...'
self.ui.status_icon.setPixmap(pixmap)
self.ui.server_status.setText(status_text)
kwargs = self.get_save_data()
responding = yield(self.queue_work(self.primary_worker, 'update_settings_and_check_connectivity', **kwargs))
self.update_responding_indicator(responding)
def update_responding_indicator(self, responding):
if responding:
icon = QIcon(':/qtutils/fugue/tick')
pixmap = icon.pixmap(QSize(16, 16))
status_text = 'Server is responding'
else:
icon = QIcon(':/qtutils/fugue/exclamation')
pixmap = icon.pixmap(QSize(16, 16))
status_text = 'Server not responding'
self.ui.status_icon.setPixmap(pixmap)
self.ui.server_status.setText(status_text)
class CameraWorker(Worker):
def init(self):
global socket; import socket
global zmq; import zmq
global zprocess; import zprocess
global shared_drive; import labscript_utils.shared_drive as shared_drive
self.host = ''
self.use_zmq = False
def update_settings_and_check_connectivity(self, host, use_zmq):
self.host = host
self.use_zmq = use_zmq
if not self.host:
return False
if not self.use_zmq:
return self.initialise_sockets(self.host, self.port)
else:
response = zprocess.zmq_get_string(self.port, self.host, data='hello')
if response == 'hello':
return True
else:
raise Exception('invalid response from server: ' + str(response))
def initialise_sockets(self, host, port):
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
assert port, 'No port number supplied.'
assert host, 'No hostname supplied.'
assert str(int(port)) == port, 'Port must be an integer.'
s.settimeout(10)
s.connect((host, int(port)))
s.send(b'hello\r\n')
response = s.recv(1024).decode('utf8')
s.close()
if 'hello' in response:
return True
else:
raise Exception('invalid response from server: ' + response)
def transition_to_buffered(self, device_name, h5file, initial_values, fresh):
h5file = shared_drive.path_to_agnostic(h5file)
if not self.use_zmq:
return self.transition_to_buffered_sockets(h5file,self.host, self.port)
response = zprocess.zmq_get_string(self.port, self.host, data=h5file)
if response != 'ok':
raise Exception('invalid response from server: ' + str(response))
response = zprocess.zmq_get_string(self.port, self.host, timeout = 10)
if response != 'done':
raise Exception('invalid response from server: ' + str(response))
return {} # indicates final values of buffered run, we have none
def transition_to_buffered_sockets(self, h5file, host, port):
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.settimeout(120)
s.connect((host, int(port)))
s.send(b'%s\r\n' % h5file.encode('utf8'))
response = s.recv(1024).decode('utf8')
if not 'ok' in response:
s.close()
raise Exception(response)
response = s.recv(1024).decode('utf8')
if not 'done' in response:
s.close()
raise Exception(response)
return {} # indicates final values of buffered run, we have none
def transition_to_manual(self):
if not self.use_zmq:
return self.transition_to_manual_sockets(self.host, self.port)
response = zprocess.zmq_get_string(self.port, self.host, 'done')
if response != 'ok':
raise Exception('invalid response from server: ' + str(response))
response = zprocess.zmq_get_string(self.port, self.host, timeout = 10)
if response != 'done':
raise Exception('invalid response from server: ' + str(response))
return True # indicates success
def transition_to_manual_sockets(self, host, port):
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.settimeout(120)
s.connect((host, int(port)))
s.send(b'done\r\n')
response = s.recv(1024).decode('utf8')
if response != 'ok\r\n':
s.close()
raise Exception(response)
response = s.recv(1024).decode('utf8')
if not 'done' in response:
s.close()
raise Exception(response)
return True # indicates success
def abort_buffered(self):
return self.abort()
def abort_transition_to_buffered(self):
return self.abort()
def abort(self):
if not self.use_zmq:
return self.abort_sockets(self.host, self.port)
response = zprocess.zmq_get_string(self.port, self.host, 'abort')
if response != 'done':
raise Exception('invalid response from server: ' + str(response))
return True # indicates success
def abort_sockets(self, host, port):
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.settimeout(120)
s.connect((host, int(port)))
s.send(b'abort\r\n')
response = s.recv(1024).decode('utf8')
if not 'done' in response:
s.close()
raise Exception(response)
return True # indicates success
def program_manual(self, values):
return {}
def shutdown(self):
return