API
 
Loading...
Searching...
No Matches
qhyccd.py
Go to the documentation of this file.
1#!/bin/python3
2# Descended from code in https://github.com/JiangXL/qhyccd-python (GPLv3)
3# authored by H.F <moyuejian@outlook.com>
4import ctypes
5import numpy as np
6import logging
7from .libqhy import *
8
9log = logging.getLogger(__name__)
10
11TYPE_CHAR20 = ctypes.c_char * 20
12TYPE_CHAR32 = ctypes.c_char * 32
13
14class QHYCCDSDK():
15 '''Class interface for the QHYCCD SDK
16 '''
17 def __init__(self, dll_path='/usr/local/lib/libqhyccd.so'):
18 '''
19 '''
20 # create sdk handle
21 self._sdk = ctypes.CDLL(dll_path)
22
25
26 ret = self._sdk.InitQHYCCDResource()
27
29 self._ids = []
30 for i in range(self._number_of_cameras):
31 self._ids.append( TYPE_CHAR32() )
32 self._sdk.GetQHYCCDId(ctypes.c_int(i), self._ids[-1])
33 log.debug("Cameras {:d} ID {:s}".format(i, self._ids[-1].value.decode('utf8')))
34
36
37 def __del__(self):
38 '''
39 '''
40 # Go through all camera handles and close the ones that are open
41 for cam_handle in self._camera_handles:
42 try:
43 self._sdk.CloseQHYCCD(cam_handle)
44 except Exception:
45 pass
47
48 def list_cameras(self) -> list:
49 '''
50 '''
51 return [_id.value for _id in self._ids]
52
53 def open_camera(self, camera_id):
54 '''
55 '''
56 if camera_id in self._camera_handles:
57 return self._camera_handles[camera_id]
58 else:
59 # Open connection to the camera and initialize its resources
60 self._camera_handles[camera_id] = self._sdk.OpenQHYCCD(self._ids[camera_id])
61 self._sdk.InitQHYCCD(self._camera_handles[camera_id])
62
63 return self._camera_handles[camera_id]
64
65 def close_camera(self, camera_id):
66 '''
67 '''
68 if camera_id in self._camera_handles:
69 # Close connection to camera
70 self._sdk.CloseQHYCCD(self._camera_handles[camera_id])
71
72 # Remove camera from active list
73 del self._camera_handles[camera_id]
74
75 #def get_camera_properties(self, camera_handle):
76 # GetQHYCCDModel(TYPE_CHAR20)
77
78 def get_parameter_limits(self, camera_handle, parameter):
79 param_min = ctypes.c_double
80 param_max = ctypes.c_double
81 param_step = ctypes.c_double
82
83 self._sdk.GetQHYCCDParamMinMaxStep(camera_handle, parameter, ctypes.byref(param_min), ctypes.byref(param_max), ctypes.byref(param_step))
84
86
87 def get_all_limits(self, camera_handle):
88 min_gain, max_gain, step_gain = self.get_parameter_limits(camera_handle, CONTROL_ID.CONTROL_GAIN)
89 min_exp, max_exp, step_exp = self.get_parameter_limits(camera_handle, CONTROL_ID.CONTROL_EXPOSURE)
90
91 parameter_limits = {
92 'exp' : [min_exp, max_exp, step_exp],
93 'gain' : [min_gain, max_gain, step_gain]
94 }
95
96 return parameter_limits
97
98 def get_chip_info(self, camera_handle):
99 # Get Camera Parameters
100 chip_width = ctypes.c_double()
101 chip_height = ctypes.c_double()
102 width = ctypes.c_uint()
103 height = ctypes.c_uint()
104 pixel_width = ctypes.c_double()
105 pixel_height = ctypes.c_double()
106 channels = ctypes.c_uint32(1)
107 bpp = ctypes.c_uint()
108
109 self._sdk.GetQHYCCDChipInfo(camera_handle, ctypes.byref(chip_width), ctypes.byref(chip_height), ctypes.byref(width), ctypes.byref(height), ctypes.byref(pixel_width), ctypes.byref(pixel_height), ctypes.byref(bpp))
110
111 chip_info = {
112 'physical' : [chip_width.value, chip_height.value],
113 'size' : [width.value, height.value],
114 'pixel_size' : [pixel_width.value, pixel_height.value],
115 'channels' : channels.value,
116 'bpp' : bpp.value
117 }
118
119 return chip_info
120
121
122 @property
124 return self._number_of_cameras
125
126 @property
127 def version(self):
128 year = ctypes.c_uint32()
129 month = ctypes.c_uint32()
130 day = ctypes.c_uint32()
131 subday = ctypes.c_uint32()
132
133 # Year starts counting at 2000 so we add 2000 to the returned value
134 ret = self._sdk.GetQHYCCDSDKVersion(ctypes.byref(year), ctypes.byref(month), ctypes.byref(day), ctypes.byref(subday))
135 return '{:d}-{:>02d}-{:>02d}'.format(2000 + year.value, month.value, day.value)
136
137 def set_parameter(self, camera_handle, parameter, value):
138 self._sdk.SetQHYCCDParam(camera_handle, parameter, value)
139
140 def get_parameter(self, camera_handle, parameter):
141 return self._sdk.GetQHYCCDParam(camera_handle, parameter)
142
144 ''' A class that interface with the QHYCCD series of cameras.
145 '''
146 def __init__(self, sdk, camera_id, new_bpp=16):
147 # create sdk handle
148 self._sdk = sdk
149 self._camera = self._sdk.open_camera(camera_id)
150
151 self._stream_mode = 0 # set default mode to stream mode, otherwise set 0 for single frame mode
152
153 self.bppbppbpp = new_bpp
155 self.gaingaingain = 1.0
156
157 # Get Camera Parameters
158 self._chip_info = self._sdk.get_chip_info(self._camera)
159 self._width = self._chip_info['size'][0]
160 self._height = self._chip_info['size'][1]
161 self._channels = ctypes.c_uint32(self._chip_info['channels'])
162
163 # Always cool to ten at startup.
165
166 # Set ROI and readout parameters
168 self.set_roi(0, 0, self._width, self._height)
169 self._sdk.set_parameter(self._camera, CONTROL_ID.CONTROL_USBTRAFFIC, ctypes.c_double(50))
170 self._sdk.set_parameter(self._camera, CONTROL_ID.CONTROL_TRANSFERBIT, self._bpp)
171
173 pass
174
175 @property
176 def temperature(self):
177 self._temperature = self._sdk.get_parameter(self._camera, CONTROL_ID.CONTROL_CURTEMP)
178 return self._temperature
179
180 @property
182 return self._sdk.get_parameter(self._camera, CONTROL_ID.CONTROL_COOLER)
183
184 @target_temperature.setter
185 def target_temperature(self, new_temperature):
186 self._target_temperature = new_temperature
188
189 @property
190 def exposure_time(self):
191 return self._exposure_time
192
193 @exposure_time.setter
194 def exposure_time(self, new_exposure_time):
195 # QHYCCD SDK uses microseconds as unit
196 # The QHYCCD VIS-X interface uses seconds as the unit. Carefull with converting units!
197 self._exposure_time = new_exposure_time
198 self._sdk.set_parameter(self._camera, CONTROL_ID.CONTROL_EXPOSURE, ctypes.c_double(self._exposure_time * 1e6))
199 log.debug("Set exposure time to", self._sdk.get_parameter(self._camera, CONTROL_ID.CONTROL_EXPOSURE) / 1e6)
200
201 @property
202 def gain(self):
203 return self._gain
204
205 @gain.setter
206 def gain(self, new_gain):
207 self._gain = new_gain
208 self._sdk.set_parameter(self._camera, CONTROL_ID.CONTROL_GAIN, ctypes.c_double(self._gain))
209
210 #""" Set camera depth """
211 @property
212 def bpp(self):
213 return self._bpp.value
214
215 @bpp.setter
216 def bpp(self, new_bpp):
217 self._bpp = ctypes.c_double(new_bpp)
218 self._sdk.set_parameter(self._camera, CONTROL_ID.CONTROL_TRANSFERBIT, self._bpp)
219
220 #""" Set camera ROI """
221 def set_roi(self, x0, y0, roi_w, roi_h):
222 self._roi_w = ctypes.c_uint(roi_w)
223 self._roi_h = ctypes.c_uint(roi_h)
224 # update the image buffer
225 if self._bpp.value == 16:
226 self._imgdata = (ctypes.c_uint16 * roi_w * roi_h)()
228 else: # 8 bit
229 self._imgdata = (ctypes.c_uint8 * roi_w * roi_h)()
231
232 def start_exposure(self):
233 ret = self._sdk._sdk.ExpQHYCCDSingleFrame(self._camera)
234
235 def remaining_time(self):
236 percentage_complete = self._sdk._sdk.GetQHYCCDExposureRemaining(self._camera) # This counts the completion rate in percentages
237 remaining = (100.0 - percentage_complete)/100.0 * self.exposure_timeexposure_timeexposure_time
238 return remaining
239
241 if self.remaining_time < 1.0:
242 return True
243 else:
244 return False
245
246 def readout(self):
248 return np.asarray(self._imgdata)
249
251 ret = self._sdk._sdk.ExpQHYCCDSingleFrame(self._camera)
253 return np.asarray(self._imgdata)
254
255 @property
256 def read_mode(self):
257 return self._read_mode
258
259 @read_mode.setter
260 def set_readout_modes(self, new_read_mode):
261 if new_read_mode == 0 or new_read_mode == 1:
262 self._read_mode = new_read_mode
gain(self, new_gain)
Definition qhyccd.py:206
target_temperature(self, new_temperature)
Definition qhyccd.py:185
__init__(self, sdk, camera_id, new_bpp=16)
Definition qhyccd.py:146
exposure_time(self, new_exposure_time)
Definition qhyccd.py:194
set_readout_modes(self, new_read_mode)
Definition qhyccd.py:260
set_roi(self, x0, y0, roi_w, roi_h)
Definition qhyccd.py:221
open_camera(self, camera_id)
Definition qhyccd.py:53
get_parameter_limits(self, camera_handle, parameter)
Definition qhyccd.py:78
list list_cameras(self)
Definition qhyccd.py:48
get_chip_info(self, camera_handle)
Definition qhyccd.py:98
get_all_limits(self, camera_handle)
Definition qhyccd.py:87
__init__(self, dll_path='/usr/local/lib/libqhyccd.so')
Definition qhyccd.py:17
close_camera(self, camera_id)
Definition qhyccd.py:65
set_parameter(self, camera_handle, parameter, value)
Definition qhyccd.py:137
get_parameter(self, camera_handle, parameter)
Definition qhyccd.py:140
int TYPE_CHAR32
Definition qhyccd.py:12