-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscicam_camd
227 lines (180 loc) · 8.5 KB
/
scicam_camd
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
#!/usr/bin/env python3
#
# This file is part of the Robotic Observatory Control Kit (rockit)
#
# rockit is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# rockit is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with rockit. If not, see <http://www.gnu.org/licenses/>.
"""Daemon for controlling a Princeton IR SciCam 1280 via Pyro"""
# pylint: disable=too-many-arguments
# pylint: disable=too-many-return-statements
# pylint: disable=too-many-instance-attributes
# pylint: disable=too-many-lines
# pylint: disable=too-many-branches
# pylint: disable=too-many-statements
import argparse
from ctypes import c_bool
from multiprocessing import Process, Queue, Value, Pipe
from multiprocessing.sharedctypes import RawArray
import queue
import threading
import Pyro4
from rockit.common import TryLock
from rockit.common.helpers import pyro_client_matches
from rockit.camera.scicam import Config, CommandStatus, CameraStatus, output_process, scicam_process
class CameraDaemon:
"""Daemon interface for andor camera"""
def __init__(self, config):
self._config = config
self._command_lock = threading.Lock()
self._cam_process = self._cam_pipe = None
self._cam_lock = threading.Lock()
# Subprocesses for processing acquired frames
self._processing_queue = Queue()
self._processing_framebuffer = RawArray('B', config.framebuffer_bytes)
self._processing_framebuffer_offsets = Queue()
self._processing_stop_signal = Value(c_bool, False)
for _ in range(config.worker_processes):
Process(target=output_process, daemon=True, args=(
self._processing_queue, self._processing_framebuffer, self._processing_framebuffer_offsets,
self._processing_stop_signal, config.camera_id,
config.header_card_capacity, config.output_path, config.log_name,
config.pipeline_daemon_name, config.pipeline_handover_timeout)).start()
def cam_command(self, command, oneway=False, **kwargs):
"""Send a command to the camera process and return the response"""
with self._cam_lock:
if self._cam_process is None or not self._cam_process.is_alive():
return CommandStatus.CameraNotInitialized
self._cam_pipe.send({
'command': command,
'args': kwargs
})
if oneway:
return CommandStatus.Succeeded
return self._cam_pipe.recv()
@Pyro4.expose
def initialize(self):
"""Connects to the camera driver"""
if not pyro_client_matches(self._config.control_ips):
return CommandStatus.InvalidControlIP
with TryLock(self._command_lock) as success:
if not success:
return CommandStatus.Blocked
with self._cam_lock:
if self._cam_process is not None and self._cam_process.is_alive():
return CommandStatus.CameraNotUninitialized
self._cam_pipe, camd_pipe = Pipe()
self._cam_process = Process(target=scicam_process, args=(
camd_pipe, self._config,
self._processing_queue,
self._processing_framebuffer, self._processing_framebuffer_offsets,
self._processing_stop_signal
), daemon=True)
self._cam_process.start()
return self._cam_pipe.recv()
@Pyro4.expose
def shutdown(self):
"""Disconnects from the camera driver"""
if not pyro_client_matches(self._config.control_ips):
return CommandStatus.InvalidControlIP
with TryLock(self._command_lock) as success:
if not success:
return CommandStatus.Blocked
if self._cam_process is None:
return CommandStatus.CameraNotInitialized
self.cam_command('shutdown', oneway=True)
with self._cam_lock:
# The pyro timeout is usually set to 5s,
# so allow a little time to terminate the process if needed
self._cam_process.join(4.5)
if self._cam_process.exitcode is None:
print('force-terminating Raptor process')
self._cam_process.terminate()
# Clean up dirty state
while not self._processing_queue.empty():
try:
self._processing_queue.get(block=False)
except queue.Empty:
continue
while not self._processing_framebuffer_offsets.empty():
try:
self._processing_framebuffer_offsets.get(block=False)
except queue.Empty:
continue
return CommandStatus.Succeeded
@Pyro4.expose
def report_status(self):
"""Returns a dictionary containing the current camera state"""
data = self.cam_command('status')
if isinstance(data, dict):
return data
return {'state': CameraStatus.Disabled}
@Pyro4.expose
def set_target_temperature(self, temperature, quiet=False):
"""Set the target camera temperature"""
if not pyro_client_matches(self._config.control_ips):
return CommandStatus.InvalidControlIP
with TryLock(self._command_lock) as success:
if not success:
return CommandStatus.Blocked
return self.cam_command('temperature', temperature=temperature, quiet=quiet)
@Pyro4.expose
def set_exposure(self, seconds, quiet=False):
"""Set the exposure time in seconds"""
if not pyro_client_matches(self._config.control_ips):
return CommandStatus.InvalidControlIP
with TryLock(self._command_lock) as success:
if not success:
return CommandStatus.Blocked
return self.cam_command('exposure', exposure=seconds, quiet=quiet)
@Pyro4.expose
def configure(self, params, quiet=False):
"""Set camera configuration to the requested state
params should be a dictionary with the following keys:
temperature: Temperature set point
exposure: Exposure time in seconds
Any properties not specified in params will be reset to its default
The params dictionary should be validated using the
schema returned by the configure_validation_schema helper
Set quiet=True to disable log messages
"""
if not pyro_client_matches(self._config.control_ips):
return CommandStatus.InvalidControlIP
with TryLock(self._command_lock) as success:
if not success:
return CommandStatus.Blocked
temperature = params.get('temperature', self._config.cooler_setpoint)
self.cam_command('temperature', temperature=temperature, quiet=quiet)
exposure = params.get('exposure', 1)
self.cam_command('exposure', exposure=exposure, quiet=quiet)
return CommandStatus.Succeeded
@Pyro4.expose
def start_sequence(self, count, quiet=False):
"""Starts an exposure sequence with a set number of frames, or 0 to run until stopped"""
if not pyro_client_matches(self._config.control_ips):
return CommandStatus.InvalidControlIP
with TryLock(self._command_lock) as success:
if not success:
return CommandStatus.Blocked
return self.cam_command('start', count=count, quiet=quiet)
@Pyro4.expose
def stop_sequence(self, quiet=False):
"""Stops any active exposure sequence"""
if not pyro_client_matches(self._config.control_ips):
return CommandStatus.InvalidControlIP
return self.cam_command('stop', quiet=quiet)
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='Camera control daemon')
parser.add_argument('config', help='Path to configuration json file')
args = parser.parse_args()
c = Config(args.config)
c.daemon.launch(CameraDaemon(c))