1 | /*
|
---|
2 | INDI Developers Manual
|
---|
3 | Tutorial CCDPreview
|
---|
4 |
|
---|
5 | "CCD Preview"
|
---|
6 |
|
---|
7 | In this tutorial, demostrate the ccdpreview feature, and simulate the readout of a CCD camera.
|
---|
8 |
|
---|
9 | Refer to README, which contains instruction on how to build this driver, and use it
|
---|
10 | with an INDI-compatible client.
|
---|
11 |
|
---|
12 | For use with Kstars: click "Connect"; click "ON"; click "Start"
|
---|
13 |
|
---|
14 | */
|
---|
15 |
|
---|
16 | #include <stdio.h>
|
---|
17 | #include <stdlib.h>
|
---|
18 | #include <string.h>
|
---|
19 | #include <stdarg.h>
|
---|
20 | #include <math.h>
|
---|
21 | #include <unistd.h>
|
---|
22 | #include <time.h>
|
---|
23 | #include <fcntl.h>
|
---|
24 | #include <signal.h>
|
---|
25 | #include <errno.h>
|
---|
26 | #include <sys/stat.h>
|
---|
27 | #include <sys/types.h>
|
---|
28 | #include <sys/socket.h>
|
---|
29 | #include <netinet/in.h>
|
---|
30 | #include <netdb.h>
|
---|
31 | #include <zlib.h>
|
---|
32 | #include <math.h>
|
---|
33 |
|
---|
34 | /* INDI Core headers */
|
---|
35 | #include "indidevapi.h"
|
---|
36 | #include "indicom.h"
|
---|
37 | #include "eventloop.h"
|
---|
38 | #include "base64.h"
|
---|
39 |
|
---|
40 | #define mydev "Device with Data Transfer"
|
---|
41 | #define IMAGE_CONTROL "Image Control"
|
---|
42 | #define COMM_GROUP "Main Control"
|
---|
43 | #define IMAGE_GROUP "Image Settings"
|
---|
44 | #define POLLMS 100
|
---|
45 | #define WIDTH ((int) CtrlN[0].value)
|
---|
46 | #define HEIGHT ((int) CtrlN[1].value)
|
---|
47 | #define BYTESPERPIX ((int) CtrlN[3].value)
|
---|
48 | #define BYTEORDER ((int) CtrlN[4].value)
|
---|
49 | #define FWHMPIX (CCDInfoN[0].value)
|
---|
50 | #define FOCUS (FocusN[0].value)
|
---|
51 |
|
---|
52 | void uploadStream(uLong maxBytes);
|
---|
53 | void readoutSim();
|
---|
54 | void makepic();
|
---|
55 |
|
---|
56 | /*INDI controls */
|
---|
57 |
|
---|
58 | /* These properties with these names are required display a video stream*/
|
---|
59 | static ISwitch PowerS[] = {{"CONNECT" , "Connect" , ISS_OFF, 0, 0},{"DISCONNECT", "Disconnect", ISS_ON, 0, 0}};
|
---|
60 | static ISwitchVectorProperty PowerSP = { mydev, "CONNECTION" , "Connection", COMM_GROUP, IP_RW, ISR_1OFMANY, 60, IPS_IDLE, PowerS, NARRAY(PowerS), "", 0};
|
---|
61 | static ISwitch StreamS[2]={{ "ON", "", ISS_OFF},{"OFF", "", ISS_ON}};
|
---|
62 | static ISwitchVectorProperty StreamSP={mydev, "CCDPREVIEW_STREAM", "Video Stream", COMM_GROUP, IP_RW, ISR_1OFMANY, 0, IPS_IDLE,StreamS, NARRAY(StreamS),"",0};
|
---|
63 | static INumber CtrlN[] = {{"WIDTH", "Width", "%0.f",16., 512., 16., 256.},
|
---|
64 | {"HEIGHT", "Height", "%0.f", 16., 512., 16., 256.},{"MAXGOODDATA", "max. good data value", "%0.f", 1., 256.0*256.0*256.0*256.0, 0., 30000.},
|
---|
65 | {"BYTESPERPIXEL", "Bytes/pix", "%0.f", 1., 4., 1., 2.},{"BYTEORDER", "Byte Order", "%0.f", 1., 2., 1., 2.}};
|
---|
66 | static INumberVectorProperty CtrlNP = {mydev , "CCDPREVIEW_CTRL", "Image Size", COMM_GROUP, IP_RW, 60, IPS_IDLE,CtrlN, NARRAY(CtrlN),"",0};
|
---|
67 | /* These properties with these names are required by the indi client to calculate the FWHM in arcsec*/
|
---|
68 | /* The FWHM here is in units of pixels. The Pixelsize here is in units of microns. */
|
---|
69 | static INumber CCDInfoN[] = {{"CCD_FWHM_PIXEL", "FWHM[pix]", "%0.2f",0., 100., 1., 3.},
|
---|
70 | {"CCD_PIXEL_SIZE", "Pixelsize[mu]", "%0.1f",0., 100., 1., 15.5}};
|
---|
71 | static INumberVectorProperty CCDInfoNP = {mydev , "CCD_INFO", "CCD Info", COMM_GROUP, IP_RO, 60, IPS_IDLE,CCDInfoN, NARRAY(CCDInfoN),"",0};
|
---|
72 |
|
---|
73 |
|
---|
74 |
|
---|
75 | /* Blob for sending image (required but the names can be chosen freely)*/
|
---|
76 | static IBLOB imageB = {"CCD1", "Feed", "", 0, 0, 0, 0, 0, 0, 0};
|
---|
77 | static IBLOBVectorProperty imageBP = {mydev, "Video", "Video", COMM_GROUP,
|
---|
78 | IP_RO, 0, IPS_IDLE, &imageB, 1, "", 0};
|
---|
79 |
|
---|
80 | /* Start / Stop Switch (not required, and only defined in this specific driver)*/
|
---|
81 | static ISwitch ReadoutS[2]={{ "Start", "", ISS_OFF},{"Stop", "", ISS_ON}};
|
---|
82 | static ISwitchVectorProperty ReadoutSP={mydev, "readout", "Readout", COMM_GROUP, IP_RW, ISR_1OFMANY, 0, IPS_IDLE,ReadoutS, NARRAY(ReadoutS),"",0};
|
---|
83 | /* Adjustable Focus (not required, and only defined in this specific driver)*/
|
---|
84 | static INumber FocusN[] = {{"Focus", "Focus", "%0.f",0., 100., 1., 40.}};
|
---|
85 | static INumberVectorProperty FocusNP = {mydev , "Telescope", "Telescope", COMM_GROUP, IP_RW, 60, IPS_IDLE,FocusN, NARRAY(FocusN),"",0};
|
---|
86 |
|
---|
87 |
|
---|
88 | /* 1 if "readout simulation" is running, 0 otherwise*/
|
---|
89 | volatile int readout_is_running=0;
|
---|
90 | /* The number of bytes read from the "virtual CCD" up to now */
|
---|
91 | volatile uLong readout_bytes_done=0;
|
---|
92 | /* 1 if readout simulation is aked to stop, 0 otherwise*/
|
---|
93 | volatile uLong readout_stop=0 ;
|
---|
94 |
|
---|
95 | /* a buffer to hold a 16 bit image to be displayed*/
|
---|
96 | unsigned char *image;
|
---|
97 |
|
---|
98 | /* in this tutorial we simply calculate the FWHM from the focus-position
|
---|
99 | in a real driver you will have calculate from the FWHM from the image you
|
---|
100 | took with your CCD. For example you can write a fitsfile and use
|
---|
101 | sextractor (sextractor.sourceforge.net) to make a list of all detected
|
---|
102 | objects with their "FWHM_WORLD" parameters and take the median of
|
---|
103 | that list. But make your FWHM is in units of pixels.
|
---|
104 | */
|
---|
105 | double fwhm()
|
---|
106 | {
|
---|
107 | return 0.3*sqrt((FOCUS-50.0)*(FOCUS-50.0))+1.0;
|
---|
108 | }
|
---|
109 |
|
---|
110 | /* Initlization routine */
|
---|
111 | static void readout_start()
|
---|
112 | {
|
---|
113 | if (readout_is_running!=0)
|
---|
114 | return;
|
---|
115 |
|
---|
116 | /* start timer to simulate CCD readout
|
---|
117 | The timer will call function previewSim after POLLMS milliseconds */
|
---|
118 | image=malloc(WIDTH*HEIGHT*BYTESPERPIX);
|
---|
119 | readout_bytes_done=0;
|
---|
120 | readout_stop=0;
|
---|
121 | readout_is_running = 1;
|
---|
122 | CtrlNP.s=IPS_OK;
|
---|
123 | IDSetNumber(&CtrlNP, NULL);
|
---|
124 | makepic();
|
---|
125 | IEAddTimer (POLLMS, readoutSim, NULL);
|
---|
126 | }
|
---|
127 |
|
---|
128 |
|
---|
129 | void ISGetProperties (const char *dev)
|
---|
130 | {
|
---|
131 |
|
---|
132 | if (dev && strcmp (mydev, dev))
|
---|
133 | return;
|
---|
134 |
|
---|
135 | IDDefSwitch(&StreamSP, NULL);
|
---|
136 | IDDefSwitch(&ReadoutSP, NULL);
|
---|
137 | IDDefSwitch(&PowerSP, NULL);
|
---|
138 | IDDefNumber(&CtrlNP, NULL);
|
---|
139 | IDDefNumber(&FocusNP, NULL);
|
---|
140 | IDDefNumber(&CCDInfoNP, NULL);
|
---|
141 | FocusNP.s=IPS_OK;
|
---|
142 | IDSetNumber(&FocusNP, NULL);
|
---|
143 | CCDInfoNP.s=IPS_OK;
|
---|
144 | IDSetNumber(&CCDInfoNP, NULL);
|
---|
145 | CtrlNP.s=IPS_OK;
|
---|
146 | IDSetNumber(&CtrlNP, NULL);
|
---|
147 | IDDefBLOB(&imageBP, NULL);
|
---|
148 |
|
---|
149 | }
|
---|
150 |
|
---|
151 | void ISNewBLOB (const char *dev, const char *name, int sizes[], char *blobs[], char *formats[], char *names[], int n)
|
---|
152 | {
|
---|
153 |
|
---|
154 | /* ignore if not ours */
|
---|
155 | if (dev && strcmp (mydev, dev))
|
---|
156 | return;
|
---|
157 |
|
---|
158 | /* suppress warning */
|
---|
159 | n=n; dev=dev; name=name; names=names;
|
---|
160 |
|
---|
161 | }
|
---|
162 |
|
---|
163 | void ISNewSwitch (const char *dev, const char *name, ISState *states, char *names[], int n)
|
---|
164 | {
|
---|
165 |
|
---|
166 | /* ignore if not ours */
|
---|
167 | if (dev && strcmp (dev, mydev))
|
---|
168 | return;
|
---|
169 |
|
---|
170 | /* Connection */
|
---|
171 | if (!strcmp (name, PowerSP.name))
|
---|
172 | {
|
---|
173 | IUUpdateSwitches(&PowerSP, states, names, n);
|
---|
174 | PowerSP.s = IPS_OK;
|
---|
175 | IDSetSwitch(&PowerSP, NULL);
|
---|
176 | return;
|
---|
177 | }
|
---|
178 |
|
---|
179 | if (!strcmp (name, StreamSP.name))
|
---|
180 | {
|
---|
181 | IUUpdateSwitches(&StreamSP, states, names, n);
|
---|
182 | StreamSP.s = IPS_OK;
|
---|
183 | IDSetSwitch(&StreamSP, NULL);
|
---|
184 | return;
|
---|
185 | }
|
---|
186 |
|
---|
187 | /* Readout start/stop*/
|
---|
188 | if (!strcmp (name, ReadoutSP.name))
|
---|
189 | {
|
---|
190 | IUUpdateSwitches(&ReadoutSP, states, names, n);
|
---|
191 | if (ReadoutS[0].s==ISS_ON) {
|
---|
192 | readout_start();
|
---|
193 | }
|
---|
194 | if (ReadoutS[1].s==ISS_ON) {
|
---|
195 | readout_stop=1;
|
---|
196 | }
|
---|
197 | ReadoutSP.s = IPS_OK;
|
---|
198 | IDSetSwitch(&ReadoutSP, NULL);
|
---|
199 | return;
|
---|
200 | }
|
---|
201 | }
|
---|
202 |
|
---|
203 | void ISNewText (const char *dev, const char *name, char *texts[], char *names[], int n)
|
---|
204 | {
|
---|
205 |
|
---|
206 | /* ignore if not ours */
|
---|
207 | if (dev && strcmp (mydev, dev))
|
---|
208 | return;
|
---|
209 |
|
---|
210 | /* suppress warning */
|
---|
211 | n=n; dev=dev; name=name; names=names; texts=texts;
|
---|
212 |
|
---|
213 | }
|
---|
214 |
|
---|
215 |
|
---|
216 | void ISNewNumber (const char *dev, const char *name, double values[], char *names[], int n)
|
---|
217 | {
|
---|
218 | /* ignore if not ours */
|
---|
219 | if (dev && strcmp (mydev, dev))
|
---|
220 | return;
|
---|
221 |
|
---|
222 | /* suppress warning */
|
---|
223 | n=n; dev=dev; name=name; names=names;
|
---|
224 |
|
---|
225 | if (!strcmp (name, CtrlNP.name)) {
|
---|
226 | if (readout_is_running==0) {
|
---|
227 | IUUpdateNumbers(&CtrlNP, values, names, n);
|
---|
228 | }
|
---|
229 | else {
|
---|
230 | IDMessage (mydev, "The Parameters can not be change during readout\n");
|
---|
231 | }
|
---|
232 | CtrlNP.s=IPS_OK;
|
---|
233 | IDSetNumber(&CtrlNP, NULL);
|
---|
234 | }
|
---|
235 | if (!strcmp (name, FocusNP.name)) {
|
---|
236 | if (readout_is_running==0) {
|
---|
237 | IUUpdateNumbers(&FocusNP, values, names, n);
|
---|
238 | }
|
---|
239 | else {
|
---|
240 | IDMessage (mydev, "The Parameters can not be change during readout\n");
|
---|
241 | }
|
---|
242 | FocusNP.s=IPS_OK;
|
---|
243 | IDSetNumber(&FocusNP, NULL);
|
---|
244 | }
|
---|
245 | }
|
---|
246 |
|
---|
247 |
|
---|
248 | void uploadStream(uLong maxBytes)
|
---|
249 | {
|
---|
250 |
|
---|
251 | unsigned char *Data, *compressedData;
|
---|
252 | int r=0,offs=0;
|
---|
253 | unsigned int i =0;
|
---|
254 | unsigned char j;
|
---|
255 | static uLong old_maxBytes, sendBytes;
|
---|
256 | uLongf compressedBytes=0;
|
---|
257 | uLong totalBytes;
|
---|
258 | /* #0 Allocate buffer*/
|
---|
259 | totalBytes = WIDTH*HEIGHT*BYTESPERPIX;
|
---|
260 | Data = (unsigned char *) malloc (sizeof(unsigned char) * totalBytes);
|
---|
261 | compressedData = (unsigned char *) malloc (sizeof(unsigned char) * totalBytes + totalBytes / 64 + 16 + 3);
|
---|
262 | if (Data == NULL || compressedData == NULL)
|
---|
263 | {
|
---|
264 | IDLog("Error! low memory. Unable to initialize fits buffers.\n");
|
---|
265 | return;
|
---|
266 | }
|
---|
267 | if (maxBytes<old_maxBytes) {
|
---|
268 | old_maxBytes=0;
|
---|
269 | }
|
---|
270 |
|
---|
271 | /* #1 fill transferbuffer ("Data") with data read from the imagebuffer ("image")*/
|
---|
272 | if (maxBytes>totalBytes) {
|
---|
273 | maxBytes=totalBytes;
|
---|
274 | }
|
---|
275 | j=0;
|
---|
276 | for (i=old_maxBytes; i < maxBytes; i+= 1) {
|
---|
277 | if (BYTEORDER==1) {
|
---|
278 | Data[i-old_maxBytes]=image[i];
|
---|
279 | }
|
---|
280 | if (BYTEORDER==2) {
|
---|
281 | if (i%(2*BYTESPERPIX)==0) {
|
---|
282 | offs=i/2;
|
---|
283 | }
|
---|
284 | if ((i%(2*BYTESPERPIX))<BYTESPERPIX) {
|
---|
285 | Data[i-old_maxBytes]=image[i-offs];
|
---|
286 | }
|
---|
287 | else {
|
---|
288 | Data[i-old_maxBytes]=image[WIDTH*HEIGHT*BYTESPERPIX-(i-offs)];
|
---|
289 | }
|
---|
290 | }
|
---|
291 | }
|
---|
292 | sendBytes=maxBytes-old_maxBytes;
|
---|
293 | compressedBytes = sizeof(char) * sendBytes + sendBytes / 64 + 16 + 3;
|
---|
294 |
|
---|
295 | /* #2 Compress it */
|
---|
296 | r = compress2(compressedData, &compressedBytes, Data, sendBytes, 9);
|
---|
297 | if (r != Z_OK)
|
---|
298 | {
|
---|
299 | /* this should NEVER happen */
|
---|
300 | IDLog("internal error - compression failed: %d\n", r);
|
---|
301 | return;
|
---|
302 | }
|
---|
303 | /* #3 Send it */
|
---|
304 | imageB.blob = compressedData;
|
---|
305 | imageB.bloblen = compressedBytes;
|
---|
306 | imageB.size = sendBytes;
|
---|
307 | strcpy(imageB.format, ".ccdpreview.z");
|
---|
308 | imageBP.s = IPS_OK;
|
---|
309 | IDSetBLOB (&imageBP, NULL);
|
---|
310 | free (Data);
|
---|
311 | free (compressedData);
|
---|
312 | old_maxBytes=maxBytes;
|
---|
313 |
|
---|
314 | }
|
---|
315 |
|
---|
316 |
|
---|
317 | /* readout the "virtual CCD" over time*/
|
---|
318 | void readoutSim (void *p)
|
---|
319 | {
|
---|
320 | readout_bytes_done+=WIDTH*BYTESPERPIX*8;
|
---|
321 | uploadStream(readout_bytes_done);
|
---|
322 | /* again */
|
---|
323 | if ((readout_bytes_done<WIDTH*HEIGHT*BYTESPERPIX)&&(readout_stop==0)) {
|
---|
324 | IEAddTimer (POLLMS, readoutSim, NULL);
|
---|
325 | }
|
---|
326 | else {
|
---|
327 | /* Here you should calculate the fwhm from your image*/
|
---|
328 | FWHMPIX=fwhm();
|
---|
329 | free(image);
|
---|
330 | CCDInfoNP.s=IPS_OK;
|
---|
331 | IDSetNumber(&CCDInfoNP, NULL);
|
---|
332 | readout_is_running=0;
|
---|
333 | }
|
---|
334 | }
|
---|
335 |
|
---|
336 | /* Writes a simulated star to the image buffer*/
|
---|
337 | void makestar(int x, int y, unsigned long f,int boxsize,double fwhm)
|
---|
338 | {
|
---|
339 | int i,j,b;
|
---|
340 | unsigned long val;
|
---|
341 | float rr;
|
---|
342 | for (i=x-boxsize/2;i<x+boxsize/2;i++){
|
---|
343 | for (j=y-boxsize/2;j<y+boxsize/2;j++){
|
---|
344 | if ((i<0)||(j<0)||(i>WIDTH-1)||(j>HEIGHT-1)) {
|
---|
345 | continue;
|
---|
346 | }
|
---|
347 | rr=exp(-2.0*0.7*((x-i)*(x-i)+(y-j)*(y-j))/(fwhm*fwhm));
|
---|
348 | val=0;
|
---|
349 | for (b=0;b<BYTESPERPIX;b++) {
|
---|
350 | val+=(unsigned long) image[BYTESPERPIX*i+WIDTH*BYTESPERPIX*j+b]*pow(256.0,b);
|
---|
351 | }
|
---|
352 | val+=f*rr;
|
---|
353 | if (BYTESPERPIX==1) {
|
---|
354 | val=val/256;
|
---|
355 | }
|
---|
356 | for (b=0;b<BYTESPERPIX;b++) {
|
---|
357 | image[BYTESPERPIX*i+WIDTH*BYTESPERPIX*j+b]=val % 256;
|
---|
358 | val=(val-(val % 256))/256;
|
---|
359 | }
|
---|
360 | }
|
---|
361 | }
|
---|
362 | }
|
---|
363 |
|
---|
364 | /*Write a simulated bias with readout noise to the image buffer */
|
---|
365 | void makebias(unsigned long bias,unsigned long noise)
|
---|
366 | {
|
---|
367 | int i,j,b;
|
---|
368 | unsigned long val;
|
---|
369 | for (i=0;i<WIDTH;i+=1){
|
---|
370 | for (j=0;j<HEIGHT;j+=1){
|
---|
371 | val=bias;
|
---|
372 | val+=(unsigned long) (noise*(rand()/(1.0*RAND_MAX)));
|
---|
373 | if (BYTESPERPIX==1) {
|
---|
374 | val=val/256;
|
---|
375 | }
|
---|
376 | for (b=0;b<BYTESPERPIX;b++) {
|
---|
377 | image[BYTESPERPIX*i+WIDTH*BYTESPERPIX*j+b]=val % 256;
|
---|
378 | val=(val-(val % 256))/256;
|
---|
379 | }
|
---|
380 | }
|
---|
381 | }
|
---|
382 | }
|
---|
383 |
|
---|
384 | /*Fill the image buffer with some pseudoastronimical data*/
|
---|
385 | void makepic()
|
---|
386 | {
|
---|
387 | unsigned long i,x,y,b;
|
---|
388 | double rr;
|
---|
389 | makebias(200,6);
|
---|
390 | /*some faint stars*/
|
---|
391 | for (i=1;i<50;i++) {
|
---|
392 | x=(int) ((WIDTH-1)*(rand()/(1.0*RAND_MAX)));
|
---|
393 | y=(int) ((HEIGHT-1)*(rand()/(1.0*RAND_MAX)));
|
---|
394 | rr=pow((rand()/(1.0*RAND_MAX)),2.0)+pow((rand()/(1.0*RAND_MAX)),2.0)
|
---|
395 | +pow((rand()/(1.0*RAND_MAX)),2.0);
|
---|
396 | rr*=10;
|
---|
397 | b=(int) (10000.0/(rr+1));
|
---|
398 | makestar(x,y,b,3*fwhm(),fwhm());
|
---|
399 | }
|
---|
400 | /*some bright stars*/
|
---|
401 | for (i=1;i<50;i++) {
|
---|
402 | x=(int) ((WIDTH-1)*(rand()/(1.0*RAND_MAX)));
|
---|
403 | y=(int) ((HEIGHT-1)*(rand()/(1.0*RAND_MAX)));
|
---|
404 | rr=pow((rand()/(1.0*RAND_MAX)),2.0)+pow((rand()/(1.0*RAND_MAX)),2.0)
|
---|
405 | +pow((rand()/(1.0*RAND_MAX)),2.0);
|
---|
406 | rr*=10;
|
---|
407 | b=(unsigned long) (10000.0/(rr+1));
|
---|
408 | if (BYTESPERPIX>1) {
|
---|
409 | b=(unsigned long) b*(pow(256.0,BYTESPERPIX-2.0));
|
---|
410 | }
|
---|
411 | makestar(x,y,b,3*fwhm(),fwhm() );
|
---|
412 | }
|
---|
413 | /*some stars of defined brightness */
|
---|
414 | for (i=1;i<3;i++) {
|
---|
415 | x=(int) ((WIDTH-1)*(rand()/(1.0*RAND_MAX)));
|
---|
416 | y=(int) ((HEIGHT-1)*(rand()/(1.0*RAND_MAX)));
|
---|
417 | b=65000;
|
---|
418 | makestar(x,y,b,100,fwhm());
|
---|
419 | }
|
---|
420 | makestar(WIDTH/2,HEIGHT/2,65000,100,fwhm());
|
---|
421 | }
|
---|