Debian Codegraph @

src/indi_gige.cpp

raw
1 /*
2 GigE interface for INDI based on aravis
3 Copyright (C) 2016 Hendrik Beijeman ([email protected])
4
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
9
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public
16 License along with this library; if not, write to the Free Software
17 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18 */
19
20 #include <time.h>
21 #include <list>
22 #include <sys/time.h>
23 #include <deque>
24 #include <memory>
25
26 #include "indidevapi.h"
27 #include "eventloop.h"
28
29 #include "indi_gige.h"
30
31 #define TIME_VAL_INIT(x) \
32 ({ \
33 (x)->tv_sec = 0; \
34 (x)->tv_usec = 0; \
35 })
36 #define TIME_VAL_ISSET(x) (((x)->tv_sec != 0) && ((x)->tv_usec != 0))
37 #define TIME_VAL_US(x) (((x)->tv_sec) * 1000000 + ((x)->tv_usec))
38 #define TIME_VAL_GET(x) (gettimeofday(x, nullptr))
39
40 #define TIMER_TRANSFER_TIMEOUT_US (5000000UL) /* Allow for relatively large link-layer delays */
41 #define TIMER_EXPOSURE_TIMEOUT_US (200000UL) /* GigE cameras are very precise, so set 100ms time-out */
42
43 #define TIMER_US_TO_MS (1000)
44 #define TIMER_US_TO_S (1000000)
45 #define TIMER_TICK_MS (100)
46 #define CAPS (CCD_CAN_ABORT | CCD_CAN_BIN | CCD_CAN_SUBFRAME)
47
48 static class Loader
49 {
50 std::deque<std::unique_ptr<GigECCD>> cameras;
51 public:
52 Loader()
53 {
54 arv::ArvCamera *camera = arv::ArvFactory::find_first_available();
55 cameras.push_back(std::unique_ptr<GigECCD>(new GigECCD(camera)));
56 IDLog("Found Camera: %s\n", camera->model_name());
57 }
58 } loader;
59
60 const char *GigECCD::getDefaultName()
61 {
62 return "GigE CCD";
63 }
64
65 GigECCD::GigECCD(arv::ArvCamera *camera)
66 {
67 this->camera = camera;
68 snprintf(this->name, sizeof(this->name), "GigE CCD%s", this->camera->model_name());
69 setDeviceName(this->name);
70 }
71
72 GigECCD::~GigECCD()
73 {
74 }
75
76 bool GigECCD::initProperties()
77 {
78 INDI::CCD::initProperties();
79 this->SetCCDCapability((CAPS));
80 this->addConfigurationControl();
81 this->addDebugControl();
82 return true;
83 }
84
85 bool GigECCD::_update_geometry(void)
86 {
87 /* Get actual values */
88 this->camera->update_geometry();
89
90 /* Sync these with INDI */
91 PrimaryCCD.setBin(this->camera->get_bin_x().val(), this->camera->get_bin_y().val());
92 PrimaryCCD.setFrame(this->camera->get_x_offset().val(), this->camera->get_y_offset().val(),
93 this->camera->get_width().val(), this->camera->get_height().val());
94
95 /* Sanity checks, reserve buffers */
96 int const width = this->camera->get_width().val();
97 int const height = this->camera->get_height().val();
98 int const frame_byte_size = this->camera->get_frame_byte_size();
99 int const indi_bufsize = PrimaryCCD.getSubW() * PrimaryCCD.getSubH() * PrimaryCCD.getBPP() / 8;
100
101 if (indi_bufsize != frame_byte_size)
102 {
103 LOGF_ERROR("Unexpected INDI image buffer size, has %i bytes, camera has %i", indi_bufsize,
104 frame_byte_size);
105 PrimaryCCD.setFrameBufferSize(0);
106 }
107 else
108 {
109 LOGF_INFO("Reserving INDI image buffer size %i bytes", indi_bufsize);
110 PrimaryCCD.setFrameBufferSize(frame_byte_size);
111 }
112 }
113
114 void GigECCD::_update_indi_properties(void)
115 {
116 LOG_INFO("update_indi_properties()");
117 IUFillNumber(&this->indiprop_gain[0], "Range", "", "%g", (double)this->camera->get_gain().min(),
118 (double)this->camera->get_gain().max(), 1., (double)this->camera->get_gain().val());
119 IUFillNumberVector(&this->indiprop_gain_prop, this->indiprop_gain, 1, getDeviceName(), "Gain", "", MAIN_CONTROL_TAB,
120 IP_RW, 60, IPS_IDLE);
121
122 IUFillText(&indiprop_info[0], "Vendor Name", "", this->camera->vendor_name());
123 IUFillText(&indiprop_info[1], "Model Name", "", this->camera->model_name());
124 IUFillText(&indiprop_info[2], "Device ID", "", this->camera->device_id());
125 IUFillTextVector(&indiprop_info_prop, indiprop_info, 3, getDeviceName(), "Camera Info", "", MAIN_CONTROL_TAB, IP_RO,
126 0, IPS_IDLE);
127
128 defineProperty(&indiprop_info_prop);
129 defineProperty(&this->indiprop_gain_prop);
130 }
131
132 void GigECCD::_delete_indi_properties(void)
133 {
134 this->deleteProperty(this->indiprop_gain_prop.name);
135 this->deleteProperty(this->indiprop_info_prop.name);
136 }
137
138 //Initial call
139 bool GigECCD::updateProperties()
140 {
141 INDI::CCD::updateProperties();
142
143 if (this->camera->is_connected())
144 {
145 this->_update_indi_properties();
146 this->SetCCDParams(this->camera->get_width().max(), this->camera->get_height().max(),
147 this->camera->get_bpp().val(), this->camera->get_pixel_pitch().val(),
148 this->camera->get_pixel_pitch().val());
149
150 (void)this->_update_geometry();
151 this->timer_id = this->SetTimer(TIMER_TICK_MS);
152 }
153 else
154 {
155 rmTimer(this->timer_id);
156 this->_delete_indi_properties();
157 }
158
159 return true;
160 }
161
162 bool GigECCD::Connect()
163 {
164 IDLog("Connect to Camera: %s\n", camera->model_name());
165 return camera->connect();
166 }
167
168 bool GigECCD::Disconnect()
169 {
170 LOGF_INFO("%s", __PRETTY_FUNCTION__);
171 #if 0
172 //TODO: re-iterate and acquire proper camera from AvrFactory (based on ID?)
173 return camera->disconnect();
174 #endif
175 return true;
176 }
177
178 bool GigECCD::StartExposure(float duration)
179 {
180 LOGF_INFO("%s exposure_time=%.4f", __PRETTY_FUNCTION__, duration);
181 /* Driver will clamp to lowest possible exposure */
182 if (PrimaryCCD.getFrameType() == INDI::CCDChip::BIAS_FRAME)
183 duration = 0;
184
185 camera->set_exposure_time((double)(duration)*1000000.0);
186
187 TIME_VAL_INIT(&this->exposure_transfer_time);
188 TIME_VAL_GET(&this->exposure_start_time);
189
190 camera->exposure_start();
191 return camera->is_exposing();
192 }
193
194 bool GigECCD::AbortExposure()
195 {
196 LOGF_INFO("%s", __PRETTY_FUNCTION__);
197 camera->exposure_abort();
198 return true;
199 }
200
201 void GigECCD::_update_image(uint8_t const *const data, size_t size)
202 {
203 LOGF_INFO("Receiving %i bytes image", size);
204
205 size_t const frame_buf_size = PrimaryCCD.getFrameBufferSize();
206
207 if ((size == frame_buf_size) && (data != nullptr))
208 {
209 uint8_t *const image = PrimaryCCD.getFrameBuffer();
210 memcpy(image, (void *const)data, frame_buf_size);
211 this->ExposureComplete(&PrimaryCCD);
212 }
213 else
214 {
215 LOGF_ERROR("Unexpected failure during image download. Framebuf has %i bytes, got %i",
216 frame_buf_size, size);
217 this->_handle_failed();
218 }
219 }
220
221 void GigECCD::_receive_image_hook(void *const class_ptr, uint8_t const *const data, size_t size)
222 {
223 GigECCD *const cls = static_cast<GigECCD *const>(class_ptr);
224 cls->_update_image(data, size);
225 }
226
227 void GigECCD::_handle_failed(void)
228 {
229 LOG_ERROR("Failure occurred, filling image with black");
230
231 camera->exposure_abort();
232
233 PrimaryCCD.setExposureLeft(0);
234
235 /* Fill with black */
236 uint8_t *const image = PrimaryCCD.getFrameBuffer();
237 memset(image, 0, PrimaryCCD.getFrameBufferSize());
238
239 this->ExposureComplete(&PrimaryCCD);
240 }
241
242 void GigECCD::_handle_timeout(struct timeval *const tv, uint32_t timeout_us)
243 {
244 if (!TIME_VAL_ISSET(tv))
245 TIME_VAL_GET(tv);
246
247 struct timeval now;
248 TIME_VAL_GET(&now);
249
250 uint32_t const elapsed = ((TIME_VAL_US(&now)) - (TIME_VAL_US(tv)));
251 uint32_t const exposure_time = (uint32_t)this->camera->get_exposure().val();
252 uint32_t const time_left = exposure_time - elapsed;
253
254 if (elapsed >= exposure_time)
255 PrimaryCCD.setExposureLeft(0);
256 else
257 PrimaryCCD.setExposureLeft((float)time_left / (float)TIMER_US_TO_S);
258
259 if (elapsed > timeout_us)
260 this->_handle_failed();
261 }
262
263 void GigECCD::TimerHit()
264 {
265 this->timer_id = this->SetTimer(TIMER_TICK_MS);
266 if (!this->camera->is_connected() || !this->camera->is_exposing())
267 return;
268
269 arv::ARV_EXPOSURE_STATUS const status = camera->exposure_poll(this->_receive_image_hook, this);
270 switch (status)
271 {
272 case arv::ARV_EXPOSURE_FINISHED:
273 /* Nothing to do, ArvCamera automatically unsets is_exposing */
274 break;
275 case arv::ARV_EXPOSURE_UNKNOWN:
276 case arv::ARV_EXPOSURE_FAILED:
277 this->_handle_failed();
278 break;
279 case arv::ARV_EXPOSURE_FILLING:
280 this->_handle_timeout(&this->exposure_transfer_time, TIMER_TRANSFER_TIMEOUT_US);
281 break;
282 case arv::ARV_EXPOSURE_BUSY:
283 this->_handle_timeout(&this->exposure_start_time,
284 ((uint32_t)this->camera->get_exposure().val() + TIMER_EXPOSURE_TIMEOUT_US));
285 break;
286 }
287 }
288
289 bool GigECCD::ISNewNumber(const char *dev, const char *name, double values[], char *names[], int n)
290 {
291 if (!strcmp(dev, this->getDeviceName()))
292 {
293 if (!strcmp(name, this->indiprop_gain_prop.name))
294 {
295 IUUpdateNumber(&this->indiprop_gain_prop, values, names, n);
296 this->camera->set_gain(this->indiprop_gain[0].value);
297 this->indiprop_gain_prop.s = IPS_OK;
298
299 /* Get-back from camera system */
300 double actual_value = this->camera->get_gain().val();
301 IUUpdateNumber(&this->indiprop_gain_prop, &actual_value, names, n);
302 IDSetNumber(&this->indiprop_gain_prop, nullptr);
303 return true;
304 }
305 }
306
307 return INDI::CCD::ISNewNumber(dev, name, values, names, n);
308 }
309
310 bool GigECCD::UpdateCCDFrame(int x, int y, int w, int h)
311 {
312 LOGF_INFO("%s x=%i y=%i w=%i h=%i", __PRETTY_FUNCTION__, x, y, w, h);
313
314 this->camera->set_geometry(x, y, w, h);
315 return this->_update_geometry();
316 }
317
318 bool GigECCD::UpdateCCDBin(int binx, int biny)
319 {
320 LOGF_INFO("%s binx=%i biny=%i", __PRETTY_FUNCTION__, binx, biny);
321 camera->set_bin(binx, biny);
322 return UpdateCCDFrame(PrimaryCCD.getSubX(), PrimaryCCD.getSubY(), PrimaryCCD.getSubW(), PrimaryCCD.getSubH());
323 }
324
325 bool GigECCD::UpdateCCDFrameType(INDI::CCDChip::CCD_FRAME fType)
326 {
327 LOGF_INFO("%s", __PRETTY_FUNCTION__);
328 PrimaryCCD.setFrameType(fType);
329 return true;
330 }