pyobs-asi 2.0.0.dev3__tar.gz → 2.0.0.dev4__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {pyobs_asi-2.0.0.dev3 → pyobs_asi-2.0.0.dev4}/PKG-INFO +1 -1
- {pyobs_asi-2.0.0.dev3 → pyobs_asi-2.0.0.dev4}/pyobs_asi/asicamera.py +164 -67
- {pyobs_asi-2.0.0.dev3 → pyobs_asi-2.0.0.dev4}/pyproject.toml +1 -1
- {pyobs_asi-2.0.0.dev3 → pyobs_asi-2.0.0.dev4}/uv.lock +1 -1
- {pyobs_asi-2.0.0.dev3 → pyobs_asi-2.0.0.dev4}/.github/workflows/pypi.yml +0 -0
- {pyobs_asi-2.0.0.dev3 → pyobs_asi-2.0.0.dev4}/.github/workflows/ruff.yml +0 -0
- {pyobs_asi-2.0.0.dev3 → pyobs_asi-2.0.0.dev4}/.pre-commit-config.yaml +0 -0
- {pyobs_asi-2.0.0.dev3 → pyobs_asi-2.0.0.dev4}/.readthedocs.yml +0 -0
- {pyobs_asi-2.0.0.dev3 → pyobs_asi-2.0.0.dev4}/CHANGELOG.rst +0 -0
- {pyobs_asi-2.0.0.dev3 → pyobs_asi-2.0.0.dev4}/LICENSE +0 -0
- {pyobs_asi-2.0.0.dev3 → pyobs_asi-2.0.0.dev4}/README.md +0 -0
- {pyobs_asi-2.0.0.dev3 → pyobs_asi-2.0.0.dev4}/docs/Makefile +0 -0
- {pyobs_asi-2.0.0.dev3 → pyobs_asi-2.0.0.dev4}/docs/make.bat +0 -0
- {pyobs_asi-2.0.0.dev3 → pyobs_asi-2.0.0.dev4}/docs/requirements.txt +0 -0
- {pyobs_asi-2.0.0.dev3 → pyobs_asi-2.0.0.dev4}/docs/source/_static/pyobs.gif +0 -0
- {pyobs_asi-2.0.0.dev3 → pyobs_asi-2.0.0.dev4}/docs/source/conf.py +0 -0
- {pyobs_asi-2.0.0.dev3 → pyobs_asi-2.0.0.dev4}/docs/source/index.rst +0 -0
- {pyobs_asi-2.0.0.dev3 → pyobs_asi-2.0.0.dev4}/pyobs_asi/__init__.py +0 -0
- {pyobs_asi-2.0.0.dev3 → pyobs_asi-2.0.0.dev4}/pyobs_asi/gui.py +0 -0
|
@@ -1,8 +1,11 @@
|
|
|
1
1
|
import asyncio
|
|
2
2
|
import logging
|
|
3
3
|
import math
|
|
4
|
+
import threading
|
|
5
|
+
import time
|
|
6
|
+
from collections.abc import Callable
|
|
4
7
|
from datetime import UTC, datetime
|
|
5
|
-
from typing import Any, cast
|
|
8
|
+
from typing import Any, TypeVar, cast
|
|
6
9
|
|
|
7
10
|
import numpy as np
|
|
8
11
|
import zwoasi as asi # type: ignore
|
|
@@ -29,10 +32,10 @@ from pyobs.interfaces import (
|
|
|
29
32
|
from pyobs.modules.camera.basecamera import BaseCamera
|
|
30
33
|
from pyobs.utils import exceptions as exc
|
|
31
34
|
from pyobs.utils.enums import ExposureStatus, ImageFormat
|
|
32
|
-
from pyobs.utils.parallel import event_wait
|
|
33
35
|
|
|
34
36
|
log = logging.getLogger(__name__)
|
|
35
37
|
|
|
38
|
+
_T = TypeVar("_T")
|
|
36
39
|
|
|
37
40
|
FORMATS = {
|
|
38
41
|
ImageFormat.INT8: asi.ASI_IMG_RAW8,
|
|
@@ -40,6 +43,19 @@ FORMATS = {
|
|
|
40
43
|
ImageFormat.RGB24: asi.ASI_IMG_RGB24,
|
|
41
44
|
}
|
|
42
45
|
|
|
46
|
+
# ZWO ASI SDK calls are blocking and are made directly on the event loop thread (see
|
|
47
|
+
# _run_blocking). If the camera has gone unresponsive, they can hang indefinitely, so they're
|
|
48
|
+
# bounded with a timeout rather than let a single dead camera freeze the whole module.
|
|
49
|
+
_SDK_CALL_TIMEOUT = 5.0
|
|
50
|
+
|
|
51
|
+
# reading out a frame after exposure can take longer than the other SDK calls above, depending on
|
|
52
|
+
# sensor resolution/image format.
|
|
53
|
+
_READOUT_TIMEOUT = 30.0
|
|
54
|
+
|
|
55
|
+
# the exposure-wait loop's own timeout needs to cover the actual exposure time plus some margin
|
|
56
|
+
# for overhead -- exposure_time alone would be too tight.
|
|
57
|
+
_EXPOSURE_WAIT_MARGIN = 30.0
|
|
58
|
+
|
|
43
59
|
|
|
44
60
|
class AsiCamera(BaseCamera, IWindow, IBinning, IImageFormat, IGain, ITemperatures):
|
|
45
61
|
"""A pyobs module for ASI cameras."""
|
|
@@ -69,42 +85,96 @@ class AsiCamera(BaseCamera, IWindow, IBinning, IImageFormat, IGain, ITemperature
|
|
|
69
85
|
|
|
70
86
|
self.add_background_task(self._temperature_thread, True)
|
|
71
87
|
|
|
88
|
+
@staticmethod
|
|
89
|
+
async def _run_blocking(func: Callable[[], None], timeout: float = _SDK_CALL_TIMEOUT) -> bool:
|
|
90
|
+
"""Run a blocking ASI SDK call in a daemon thread, so a hung call can't freeze the module.
|
|
91
|
+
|
|
92
|
+
A plain executor isn't used here, since its worker threads are non-daemon and Python joins
|
|
93
|
+
them on interpreter shutdown -- a hung call would then just move the freeze to process exit.
|
|
94
|
+
|
|
95
|
+
Returns:
|
|
96
|
+
True if func completed within timeout, False if it's still running in the background.
|
|
97
|
+
"""
|
|
98
|
+
loop = asyncio.get_running_loop()
|
|
99
|
+
future: asyncio.Future[None] = loop.create_future()
|
|
100
|
+
|
|
101
|
+
def _wrapper() -> None:
|
|
102
|
+
try:
|
|
103
|
+
func()
|
|
104
|
+
finally:
|
|
105
|
+
loop.call_soon_threadsafe(future.set_result, None)
|
|
106
|
+
|
|
107
|
+
threading.Thread(target=_wrapper, daemon=True).start()
|
|
108
|
+
try:
|
|
109
|
+
await asyncio.wait_for(future, timeout=timeout)
|
|
110
|
+
return True
|
|
111
|
+
except TimeoutError:
|
|
112
|
+
return False
|
|
113
|
+
|
|
114
|
+
async def _run_blocking_or_raise(self, func: Callable[[], _T], timeout: float = _SDK_CALL_TIMEOUT) -> _T:
|
|
115
|
+
"""Run a blocking ASI SDK call in a thread, returning its result or re-raising what it raised.
|
|
116
|
+
|
|
117
|
+
Unlike _run_blocking(), this also carries the callable's return value/exception back to the
|
|
118
|
+
caller -- several ASI calls here drive control flow via their return value or a raised
|
|
119
|
+
ValueError (e.g. camera lookup by name, exposure status), which a bare fire-and-forget
|
|
120
|
+
thread call would otherwise silently lose.
|
|
121
|
+
"""
|
|
122
|
+
outcome: list[Any] = []
|
|
123
|
+
|
|
124
|
+
def _wrapper() -> None:
|
|
125
|
+
try:
|
|
126
|
+
outcome.append(func())
|
|
127
|
+
except BaseException as e:
|
|
128
|
+
outcome.append(e)
|
|
129
|
+
|
|
130
|
+
if not await self._run_blocking(_wrapper, timeout=timeout):
|
|
131
|
+
raise TimeoutError(f"Timed out waiting for ASI SDK call after {timeout}s.")
|
|
132
|
+
value = outcome[0]
|
|
133
|
+
if isinstance(value, BaseException):
|
|
134
|
+
raise value
|
|
135
|
+
return cast(_T, value)
|
|
136
|
+
|
|
72
137
|
async def open(self) -> None:
|
|
73
138
|
"""Open module."""
|
|
74
|
-
asi.init(self._sdk_path)
|
|
75
139
|
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
140
|
+
def _open() -> tuple[dict[str, Any], int, tuple[int, int, int, int]]:
|
|
141
|
+
asi.init(self._sdk_path)
|
|
142
|
+
|
|
143
|
+
num_cameras = asi.get_num_cameras()
|
|
144
|
+
if num_cameras == 0:
|
|
145
|
+
raise ValueError("No cameras found")
|
|
146
|
+
|
|
147
|
+
# index() raises ValueError, if camera could not be found
|
|
148
|
+
cameras_found = asi.list_cameras()
|
|
149
|
+
camera_id = cameras_found.index(self._camera_name)
|
|
150
|
+
|
|
151
|
+
self._camera = asi.Camera(camera_id)
|
|
152
|
+
camera_info = self._camera.get_camera_property()
|
|
153
|
+
|
|
154
|
+
# Set some sensible defaults. They will need adjusting depending upon
|
|
155
|
+
# the sensitivity, lens and lighting conditions used.
|
|
156
|
+
self._camera.disable_dark_subtract()
|
|
157
|
+
self._camera.set_control_value(asi.ASI_WB_B, 99)
|
|
158
|
+
self._camera.set_control_value(asi.ASI_WB_R, 75)
|
|
159
|
+
self._camera.set_control_value(asi.ASI_GAMMA, 50)
|
|
160
|
+
self._camera.set_control_value(asi.ASI_FLIP, 0)
|
|
161
|
+
self._camera.set_image_type(asi.ASI_IMG_RAW16)
|
|
79
162
|
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
163
|
+
# enabling image mode
|
|
164
|
+
self._camera.stop_video_capture()
|
|
165
|
+
self._camera.stop_exposure()
|
|
166
|
+
|
|
167
|
+
# get initial window and binning
|
|
168
|
+
binning = self._camera.get_bin()
|
|
169
|
+
left, top, width, height = self._camera.get_roi()
|
|
170
|
+
return camera_info, binning, (left, top, width, height)
|
|
171
|
+
|
|
172
|
+
self._camera_info, self._binning, self._window = await self._run_blocking_or_raise(_open)
|
|
83
173
|
|
|
84
|
-
self._camera = asi.Camera(camera_id)
|
|
85
|
-
self._camera_info = self._camera.get_camera_property()
|
|
86
174
|
log.info("Camera info:")
|
|
87
175
|
for key, val in self._camera_info.items():
|
|
88
176
|
log.info(" - %s: %s", key, val)
|
|
89
177
|
|
|
90
|
-
# Set some sensible defaults. They will need adjusting depending upon
|
|
91
|
-
# the sensitivity, lens and lighting conditions used.
|
|
92
|
-
self._camera.disable_dark_subtract()
|
|
93
|
-
self._camera.set_control_value(asi.ASI_WB_B, 99)
|
|
94
|
-
self._camera.set_control_value(asi.ASI_WB_R, 75)
|
|
95
|
-
self._camera.set_control_value(asi.ASI_GAMMA, 50)
|
|
96
|
-
self._camera.set_control_value(asi.ASI_FLIP, 0)
|
|
97
|
-
self._camera.set_image_type(asi.ASI_IMG_RAW16)
|
|
98
|
-
|
|
99
|
-
# enabling image mode
|
|
100
|
-
self._camera.stop_video_capture()
|
|
101
|
-
self._camera.stop_exposure()
|
|
102
|
-
|
|
103
|
-
# get initial window and binning
|
|
104
|
-
self._binning = self._camera.get_bin()
|
|
105
|
-
left, top, width, height = self._camera.get_roi()
|
|
106
|
-
self._window = (left, top, width, height)
|
|
107
|
-
|
|
108
178
|
# publish capabilities before super().open()
|
|
109
179
|
await self.comm.set_capabilities(
|
|
110
180
|
IWindow,
|
|
@@ -139,7 +209,8 @@ class AsiCamera(BaseCamera, IWindow, IBinning, IImageFormat, IGain, ITemperature
|
|
|
139
209
|
|
|
140
210
|
async def _publish_temperatures(self) -> None:
|
|
141
211
|
await self.comm.set_state(
|
|
142
|
-
ITemperatures,
|
|
212
|
+
ITemperatures,
|
|
213
|
+
TemperaturesState(readings=[SensorReading(name="CCD", value=await self._get_temperature())]),
|
|
143
214
|
)
|
|
144
215
|
|
|
145
216
|
async def set_window(self, left: int, top: int, width: int, height: int, **kwargs: Any) -> None:
|
|
@@ -188,6 +259,7 @@ class AsiCamera(BaseCamera, IWindow, IBinning, IImageFormat, IGain, ITemperature
|
|
|
188
259
|
"""
|
|
189
260
|
if self._camera is None:
|
|
190
261
|
raise ValueError("No camera initialised.")
|
|
262
|
+
camera = self._camera
|
|
191
263
|
|
|
192
264
|
image_format = FORMATS[self._image_format]
|
|
193
265
|
|
|
@@ -205,14 +277,6 @@ class AsiCamera(BaseCamera, IWindow, IBinning, IImageFormat, IGain, ITemperature
|
|
|
205
277
|
self._window[0],
|
|
206
278
|
self._window[1],
|
|
207
279
|
)
|
|
208
|
-
self._camera.set_roi(int(self._window[0]), int(self._window[1]), width, height, self._binning, image_format)
|
|
209
|
-
|
|
210
|
-
# set status and exposure time in us
|
|
211
|
-
self._camera.set_control_value(asi.ASI_EXPOSURE, int(exposure_time * 1e6))
|
|
212
|
-
|
|
213
|
-
# set gain and offset
|
|
214
|
-
self._camera.set_control_value(asi.ASI_GAIN, int(self._gain))
|
|
215
|
-
self._camera.set_control_value(asi.ASI_OFFSET, int(self._gain_offset))
|
|
216
280
|
|
|
217
281
|
log.info(
|
|
218
282
|
"Starting exposure with %s shutter for %s seconds and %s gain...",
|
|
@@ -221,26 +285,45 @@ class AsiCamera(BaseCamera, IWindow, IBinning, IImageFormat, IGain, ITemperature
|
|
|
221
285
|
self._gain,
|
|
222
286
|
)
|
|
223
287
|
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
288
|
+
def _start() -> None:
|
|
289
|
+
camera.set_roi(int(self._window[0]), int(self._window[1]), width, height, self._binning, image_format)
|
|
290
|
+
# set status and exposure time in us
|
|
291
|
+
camera.set_control_value(asi.ASI_EXPOSURE, int(exposure_time * 1e6))
|
|
292
|
+
# set gain and offset
|
|
293
|
+
camera.set_control_value(asi.ASI_GAIN, int(self._gain))
|
|
294
|
+
camera.set_control_value(asi.ASI_OFFSET, int(self._gain_offset))
|
|
295
|
+
camera.start_exposure()
|
|
227
296
|
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
if abort_event.is_set():
|
|
231
|
-
await self._change_exposure_status(ExposureStatus.IDLE)
|
|
232
|
-
raise InterruptedError("Aborted exposure.")
|
|
233
|
-
await event_wait(abort_event, 0.01)
|
|
297
|
+
await self._run_blocking_or_raise(_start)
|
|
298
|
+
await asyncio.sleep(0.01)
|
|
234
299
|
|
|
235
|
-
|
|
300
|
+
# wait for image -- runs the whole poll loop as a single blocking call (see _run_blocking),
|
|
301
|
+
# rather than polling get_exposure_status() every 10ms directly on the event loop
|
|
302
|
+
def _wait() -> Any:
|
|
303
|
+
while True:
|
|
304
|
+
exposure_status = camera.get_exposure_status()
|
|
305
|
+
if exposure_status != asi.ASI_EXP_WORKING or abort_event.is_set():
|
|
306
|
+
return exposure_status
|
|
307
|
+
time.sleep(0.01)
|
|
308
|
+
|
|
309
|
+
exposure_timeout = exposure_time + _EXPOSURE_WAIT_MARGIN
|
|
310
|
+
status = await self._run_blocking_or_raise(_wait, timeout=exposure_timeout)
|
|
311
|
+
|
|
312
|
+
if abort_event.is_set():
|
|
313
|
+
await self._change_exposure_status(ExposureStatus.IDLE)
|
|
314
|
+
raise InterruptedError("Aborted exposure.")
|
|
236
315
|
if status != asi.ASI_EXP_SUCCESS:
|
|
237
316
|
raise exc.GrabImageError(f"Could not capture image: {status}")
|
|
238
317
|
|
|
239
318
|
log.info("Exposure finished, reading out...")
|
|
240
319
|
await self._change_exposure_status(ExposureStatus.READOUT)
|
|
241
|
-
buffer = self._camera.get_data_after_exposure()
|
|
242
320
|
|
|
243
|
-
|
|
321
|
+
def _readout() -> tuple[Any, Any]:
|
|
322
|
+
buffer = camera.get_data_after_exposure()
|
|
323
|
+
whbi = camera.get_roi_format()
|
|
324
|
+
return buffer, whbi
|
|
325
|
+
|
|
326
|
+
buffer, whbi = await self._run_blocking_or_raise(_readout, timeout=_READOUT_TIMEOUT)
|
|
244
327
|
|
|
245
328
|
# decide on image format
|
|
246
329
|
shape = [whbi[1], whbi[0]]
|
|
@@ -291,7 +374,7 @@ class AsiCamera(BaseCamera, IWindow, IBinning, IImageFormat, IGain, ITemperature
|
|
|
291
374
|
if image_format in [asi.ASI_IMG_RAW8, asi.ASI_IMG_RAW16]:
|
|
292
375
|
image.header["BAYERPAT"] = image.header["COLORTYP"] = ("GBRG", "Bayer pattern for colors")
|
|
293
376
|
|
|
294
|
-
image.header["DET-TEMP"] = (self._get_temperature(), "CCD temperature [C]")
|
|
377
|
+
image.header["DET-TEMP"] = (await self._get_temperature(), "CCD temperature [C]")
|
|
295
378
|
|
|
296
379
|
self.set_biassec_trimsec(image.header, *self._window)
|
|
297
380
|
|
|
@@ -312,12 +395,17 @@ class AsiCamera(BaseCamera, IWindow, IBinning, IImageFormat, IGain, ITemperature
|
|
|
312
395
|
self._image_format = fmt
|
|
313
396
|
await self.comm.set_state(IImageFormat, ImageFormatState(image_format=fmt))
|
|
314
397
|
|
|
315
|
-
def _get_temperature(self) -> float:
|
|
398
|
+
async def _get_temperature(self) -> float:
|
|
316
399
|
"""Gets the temperature from the camera.
|
|
317
400
|
|
|
318
401
|
Reading is divided by 10, since ASI_TEMPERATURE returns temp * 10
|
|
319
402
|
"""
|
|
320
|
-
|
|
403
|
+
camera = self._camera
|
|
404
|
+
|
|
405
|
+
def _get() -> float:
|
|
406
|
+
return camera.get_control_value(asi.ASI_TEMPERATURE)[0] / 10 # type: ignore[union-attr]
|
|
407
|
+
|
|
408
|
+
return await self._run_blocking_or_raise(_get)
|
|
321
409
|
|
|
322
410
|
async def set_gain(self, gain: float, **kwargs: Any) -> None:
|
|
323
411
|
"""Set the camera gain.
|
|
@@ -367,16 +455,21 @@ class AsiCoolCamera(AsiCamera, ICooling):
|
|
|
367
455
|
|
|
368
456
|
async def _publish_temperatures(self) -> None:
|
|
369
457
|
await super()._publish_temperatures()
|
|
370
|
-
enabled, setpoint, power = self._get_cooling_status()
|
|
458
|
+
enabled, setpoint, power = await self._get_cooling_status()
|
|
371
459
|
await self.comm.set_state(ICooling, CoolingState(setpoint=setpoint, power=int(power), enabled=enabled))
|
|
372
460
|
|
|
373
|
-
def _get_cooling_status(self) -> tuple[bool, float, float]:
|
|
461
|
+
async def _get_cooling_status(self) -> tuple[bool, float, float]:
|
|
374
462
|
if self._camera is None:
|
|
375
463
|
raise ValueError("No camera initialised.")
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
464
|
+
camera = self._camera
|
|
465
|
+
|
|
466
|
+
def _get() -> tuple[bool, float, float]:
|
|
467
|
+
enabled = camera.get_control_value(asi.ASI_COOLER_ON)[0]
|
|
468
|
+
setpoint = camera.get_control_value(asi.ASI_TARGET_TEMP)[0]
|
|
469
|
+
power = camera.get_control_value(asi.ASI_COOLER_POWER_PERC)[0]
|
|
470
|
+
return bool(enabled), float(setpoint), float(power)
|
|
471
|
+
|
|
472
|
+
return await self._run_blocking_or_raise(_get)
|
|
380
473
|
|
|
381
474
|
async def set_cooling(self, enabled: bool, setpoint: float, **kwargs: Any) -> None:
|
|
382
475
|
"""Enables/disables cooling and sets setpoint.
|
|
@@ -390,16 +483,20 @@ class AsiCoolCamera(AsiCamera, ICooling):
|
|
|
390
483
|
"""
|
|
391
484
|
if self._camera is None:
|
|
392
485
|
raise ValueError("No camera initialised.")
|
|
486
|
+
camera = self._camera
|
|
393
487
|
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
488
|
+
def _set() -> None:
|
|
489
|
+
if enabled:
|
|
490
|
+
log.info("Enabling cooling with a setpoint of %.2f°C...", setpoint)
|
|
491
|
+
camera.set_control_value(asi.ASI_TARGET_TEMP, int(setpoint))
|
|
492
|
+
camera.set_control_value(asi.ASI_COOLER_ON, 1)
|
|
493
|
+
else:
|
|
494
|
+
log.info("Disabling cooling...")
|
|
495
|
+
camera.set_control_value(asi.ASI_COOLER_ON, 0)
|
|
496
|
+
|
|
497
|
+
await self._run_blocking_or_raise(_set)
|
|
401
498
|
|
|
402
|
-
enabled_status, setpoint_status, power = self._get_cooling_status()
|
|
499
|
+
enabled_status, setpoint_status, power = await self._get_cooling_status()
|
|
403
500
|
await self.comm.set_state(
|
|
404
501
|
ICooling, CoolingState(setpoint=setpoint_status, power=int(power), enabled=enabled_status)
|
|
405
502
|
)
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|