File: tutorial_ccdpreview.c

package info (click to toggle)
libindi 0.9.1-2
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 3,232 kB
  • sloc: ansic: 17,921; cpp: 17,821; xml: 260; makefile: 12
file content (421 lines) | stat: -rw-r--r-- 12,301 bytes parent folder | download
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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
/*
   INDI Developers Manual
   Tutorial CCDPreview
   
   "CCD Preview"
   
   In this tutorial, demostrate the ccdpreview feature, and simulate the readout of a CCD camera. 
   
   Refer to README, which contains instruction on how to build this driver, and use it 
   with an INDI-compatible client.
   
   For use with Kstars: click "Connect"; click "ON"; click "Start" 

*/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
#include <math.h>
#include <unistd.h>
#include <time.h>
#include <fcntl.h>
#include <signal.h>
#include <errno.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <zlib.h>
#include <math.h>

/* INDI Core headers */
#include "indidevapi.h"
#include "indicom.h"
#include "eventloop.h"
#include "base64.h"

#define mydev         	"Device with Data Transfer"
#define IMAGE_CONTROL   "Image Control"
#define COMM_GROUP	"Main Control"
#define IMAGE_GROUP	"Image Settings"
#define	POLLMS			100
#define WIDTH ((int) CtrlN[0].value)
#define HEIGHT ((int) CtrlN[1].value)
#define BYTESPERPIX ((int) CtrlN[3].value)
#define BYTEORDER ((int) CtrlN[4].value)
#define FWHMPIX (CCDInfoN[0].value)
#define FOCUS (FocusN[0].value)

void uploadStream(uLong maxBytes);
void readoutSim();
void makepic();

/*INDI controls */

/* These properties with these names are required display a video stream*/
static ISwitch PowerS[]          	= {{"CONNECT" , "Connect" , ISS_OFF, 0, 0},{"DISCONNECT", "Disconnect", ISS_ON, 0, 0}};
static ISwitchVectorProperty PowerSP	= { mydev, "CONNECTION" , "Connection", COMM_GROUP, IP_RW, ISR_1OFMANY, 60, IPS_IDLE, PowerS, NARRAY(PowerS), "", 0};
static ISwitch StreamS[2]={{ "ON", "", ISS_OFF},{"OFF", "", ISS_ON}};
static ISwitchVectorProperty StreamSP={mydev, "CCDPREVIEW_STREAM", "Video Stream", COMM_GROUP, IP_RW, ISR_1OFMANY, 0, IPS_IDLE,StreamS, NARRAY(StreamS),"",0};
static INumber CtrlN[] = {{"WIDTH", "Width", "%0.f",16., 512., 16., 256.},
	{"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.},
	{"BYTESPERPIXEL", "Bytes/pix", "%0.f", 1., 4., 1., 2.},{"BYTEORDER", "Byte Order", "%0.f", 1., 2., 1., 2.}};
static INumberVectorProperty CtrlNP = {mydev ,  "CCDPREVIEW_CTRL", "Image Size", COMM_GROUP, IP_RW, 60, IPS_IDLE,CtrlN, NARRAY(CtrlN),"",0};
/* These properties with these names are required by the indi client to calculate the FWHM in arcsec*/
/* The FWHM here is in units of pixels. The Pixelsize here is in units of microns. */
static INumber CCDInfoN[] = {{"CCD_FWHM_PIXEL", "FWHM[pix]", "%0.2f",0., 100., 1., 3.},
	{"CCD_PIXEL_SIZE", "Pixelsize[mu]", "%0.1f",0., 100., 1., 15.5}};
static INumberVectorProperty CCDInfoNP = {mydev ,  "CCD_INFO", "CCD Info", COMM_GROUP, IP_RO, 60, IPS_IDLE,CCDInfoN, NARRAY(CCDInfoN),"",0};



/* Blob for sending image (required but the names can be chosen freely)*/
static IBLOB imageB = {"CCD1", "Feed", "", 0, 0, 0, 0, 0, 0, 0};
static IBLOBVectorProperty imageBP = {mydev, "Video", "Video", COMM_GROUP,
  IP_RO, 0, IPS_IDLE, &imageB, 1, "", 0};
  
/* Start / Stop Switch (not required, and only defined in this specific driver)*/
static ISwitch ReadoutS[2]={{ "Start", "", ISS_OFF},{"Stop", "", ISS_ON}};
static ISwitchVectorProperty ReadoutSP={mydev, "readout", "Readout", COMM_GROUP, IP_RW, ISR_1OFMANY, 0, IPS_IDLE,ReadoutS, NARRAY(ReadoutS),"",0};
/* Adjustable Focus (not required, and only defined in this specific driver)*/
static INumber FocusN[] = {{"Focus", "Focus", "%0.f",0., 100., 1., 40.}};
static INumberVectorProperty FocusNP = {mydev ,  "Telescope", "Telescope", COMM_GROUP, IP_RW, 60, IPS_IDLE,FocusN, NARRAY(FocusN),"",0};


/* 1 if "readout simulation" is running, 0 otherwise*/
volatile int readout_is_running=0;
/* The number of bytes read from the "virtual CCD" up to now */
volatile uLong readout_bytes_done=0;
/* 1 if readout simulation is aked to stop, 0 otherwise*/
volatile uLong readout_stop=0 ;   
  
/* a buffer to hold a 16 bit image to be displayed*/
unsigned char *image;

/* in this tutorial we simply calculate the FWHM from the focus-position
   in a real driver you will have calculate from the FWHM from the image you
   took with your CCD. For example you can write a fitsfile and use 
   sextractor (sextractor.sourceforge.net) to make a list of all detected 
   objects with their "FWHM_WORLD" parameters and take the median of
   that list. But make your FWHM is in units of pixels.
*/
double fwhm()
{
	return 0.3*sqrt((FOCUS-50.0)*(FOCUS-50.0))+1.0;
}

/* Initlization routine */
static void readout_start()
{
	if (readout_is_running!=0)
	    return;
	
	/* start timer to simulate CCD readout
	   The timer will call function previewSim after POLLMS milliseconds */
	image=malloc(WIDTH*HEIGHT*BYTESPERPIX);
	readout_bytes_done=0;
	readout_stop=0;
	readout_is_running = 1;
	CtrlNP.s=IPS_OK;
	IDSetNumber(&CtrlNP, NULL);
	makepic();
	IEAddTimer (POLLMS, readoutSim, NULL);
}


void ISGetProperties (const char *dev)
{ 

  if (dev && strcmp (mydev, dev))
    return;

  IDDefSwitch(&StreamSP, NULL); 
  IDDefSwitch(&ReadoutSP, NULL);
  IDDefSwitch(&PowerSP, NULL);
  IDDefNumber(&CtrlNP, NULL);
  IDDefNumber(&FocusNP, NULL);
  IDDefNumber(&CCDInfoNP, NULL);
  FocusNP.s=IPS_OK;
  IDSetNumber(&FocusNP, NULL);
  CCDInfoNP.s=IPS_OK;
  IDSetNumber(&CCDInfoNP, NULL);
  CtrlNP.s=IPS_OK;
  IDSetNumber(&CtrlNP, NULL);
  IDDefBLOB(&imageBP, NULL);
  
}

void ISNewBLOB (const char *dev, const char *name, int sizes[], char *blobs[], char *formats[], char *names[], int n)
{
	
       /* ignore if not ours */ 
       if (dev && strcmp (mydev, dev))
         return;

	/* suppress warning */
	n=n; dev=dev; name=name; names=names; 

}
  
void ISNewSwitch (const char *dev, const char *name, ISState *states, char *names[], int n)
{
	
	/* ignore if not ours */
	if (dev && strcmp (dev, mydev))
	    return;
	    
	/* Connection */
	if (!strcmp (name, PowerSP.name))
	{
	  IUUpdateSwitches(&PowerSP, states, names, n);
   	  PowerSP.s = IPS_OK;
          IDSetSwitch(&PowerSP, NULL);
	  return;
	}
	
	if (!strcmp (name, StreamSP.name))
	{
	  IUUpdateSwitches(&StreamSP, states, names, n);
   	  StreamSP.s = IPS_OK;
          IDSetSwitch(&StreamSP, NULL);
	  return;
	}
	
	/* Readout start/stop*/
	if (!strcmp (name, ReadoutSP.name))
	{
	  IUUpdateSwitches(&ReadoutSP, states, names, n);
   	  if (ReadoutS[0].s==ISS_ON) {
		readout_start();  
	  }
	  if (ReadoutS[1].s==ISS_ON) {
		readout_stop=1;  
	  }
	  ReadoutSP.s = IPS_OK;
          IDSetSwitch(&ReadoutSP, NULL);
	  return;
	}
}

void ISNewText (const char *dev, const char *name, char *texts[], char *names[], int n)
{
	
       /* ignore if not ours */ 
       if (dev && strcmp (mydev, dev))
         return;

	/* suppress warning */
	n=n; dev=dev; name=name; names=names; texts=texts;
	
}


void ISNewNumber (const char *dev, const char *name, double values[], char *names[], int n)
{
	/* ignore if not ours */ 
       if (dev && strcmp (mydev, dev))
         return;
       
       /* suppress warning */
       n=n; dev=dev; name=name; names=names; 
       
	if (!strcmp (name, CtrlNP.name)) {
		if (readout_is_running==0) {
			IUUpdateNumbers(&CtrlNP, values, names, n);
		}
		else {
			 IDMessage (mydev, "The Parameters can not be change during readout\n");
		}
		CtrlNP.s=IPS_OK;
		IDSetNumber(&CtrlNP, NULL);
	}
	if (!strcmp (name, FocusNP.name)) {
		if (readout_is_running==0) {
			IUUpdateNumbers(&FocusNP, values, names, n);
		}
		else {
			 IDMessage (mydev, "The Parameters can not be change during readout\n");
		}
		FocusNP.s=IPS_OK;
		IDSetNumber(&FocusNP, NULL);
	}
}


void uploadStream(uLong maxBytes)
{
	
   unsigned char *Data, *compressedData;
   int r=0,offs=0;
   unsigned int i =0;
   unsigned char j;
   static uLong old_maxBytes, sendBytes;	
   uLongf compressedBytes=0;
   uLong  totalBytes;
   /* #0 Allocate buffer*/
   totalBytes     = WIDTH*HEIGHT*BYTESPERPIX;
   Data       = (unsigned char *) malloc (sizeof(unsigned char) * totalBytes);
   compressedData = (unsigned char *) malloc (sizeof(unsigned char) * totalBytes + totalBytes / 64 + 16 + 3);
   if (Data == NULL || compressedData == NULL)
   {
     IDLog("Error! low memory. Unable to initialize fits buffers.\n");
     return;
   }
   if (maxBytes<old_maxBytes) {
     old_maxBytes=0;
   }
   
   /* #1 fill transferbuffer ("Data") with data read from the imagebuffer ("image")*/
   if (maxBytes>totalBytes) {
	maxBytes=totalBytes;
   }
   j=0;
   for (i=old_maxBytes; i < maxBytes; i+= 1) {
     if (BYTEORDER==1) {
       	Data[i-old_maxBytes]=image[i];
     }
     if (BYTEORDER==2) {
       if (i%(2*BYTESPERPIX)==0) {
         offs=i/2;
         }
         if ((i%(2*BYTESPERPIX))<BYTESPERPIX) {
	 Data[i-old_maxBytes]=image[i-offs];
         }
        else {
          Data[i-old_maxBytes]=image[WIDTH*HEIGHT*BYTESPERPIX-(i-offs)];
       }
     }
   }
   sendBytes=maxBytes-old_maxBytes;
   compressedBytes = sizeof(char) * sendBytes + sendBytes / 64 + 16 + 3;
   
   /* #2 Compress it */ 
   r = compress2(compressedData, &compressedBytes, Data, sendBytes, 9);
   if (r != Z_OK)
   {
 	/* this should NEVER happen */
 	IDLog("internal error - compression failed: %d\n", r);
	return;
   }
   /* #3 Send it */
   imageB.blob = compressedData;
   imageB.bloblen = compressedBytes;
   imageB.size = sendBytes;
   strcpy(imageB.format, ".ccdpreview.z");
   imageBP.s = IPS_OK;
   IDSetBLOB (&imageBP, NULL);
   free (Data);   
   free (compressedData);
   old_maxBytes=maxBytes;
   
}


/* readout the "virtual CCD" over time*/
void readoutSim (void *p)
{
	readout_bytes_done+=WIDTH*BYTESPERPIX*8;
	uploadStream(readout_bytes_done);
	/* again */
	if ((readout_bytes_done<WIDTH*HEIGHT*BYTESPERPIX)&&(readout_stop==0)) {
		IEAddTimer (POLLMS, readoutSim, NULL);
	}
	else {
		/* Here you should calculate the fwhm from your image*/
		FWHMPIX=fwhm();
		free(image);
		CCDInfoNP.s=IPS_OK;
		IDSetNumber(&CCDInfoNP, NULL);
		readout_is_running=0;
	}
}

/* Writes a simulated star to the image buffer*/
void makestar(int x, int y, unsigned long f,int boxsize,double fwhm)
{
	int i,j,b;
	unsigned long val;
	float rr;
	for (i=x-boxsize/2;i<x+boxsize/2;i++){
		for (j=y-boxsize/2;j<y+boxsize/2;j++){
			if ((i<0)||(j<0)||(i>WIDTH-1)||(j>HEIGHT-1)) {
				continue;
			}
			rr=exp(-2.0*0.7*((x-i)*(x-i)+(y-j)*(y-j))/(fwhm*fwhm));
			val=0;
			for (b=0;b<BYTESPERPIX;b++) {
				val+=(unsigned long) image[BYTESPERPIX*i+WIDTH*BYTESPERPIX*j+b]*pow(256.0,b);
			}
			val+=f*rr;
			if (BYTESPERPIX==1) {
				val=val/256;
			}
			for (b=0;b<BYTESPERPIX;b++) {
				image[BYTESPERPIX*i+WIDTH*BYTESPERPIX*j+b]=val % 256;
				val=(val-(val % 256))/256;
			}
		}
	}
}

/*Write a simulated bias with readout noise to the image buffer */
void makebias(unsigned long bias,unsigned long noise)
{
	int i,j,b;
	unsigned long val;
	for (i=0;i<WIDTH;i+=1){
		for (j=0;j<HEIGHT;j+=1){
			val=bias;
			val+=(unsigned long) (noise*(rand()/(1.0*RAND_MAX)));
			if (BYTESPERPIX==1) {
				val=val/256;
			}
			for (b=0;b<BYTESPERPIX;b++) {
				image[BYTESPERPIX*i+WIDTH*BYTESPERPIX*j+b]=val % 256;
				val=(val-(val % 256))/256;
			}
		}
	}
}

/*Fill the image buffer with some pseudoastronimical data*/
void makepic()
{
	unsigned long i,x,y,b;
	double rr;
	makebias(200,6);
	/*some faint stars*/
	for (i=1;i<50;i++) {
		x=(int) ((WIDTH-1)*(rand()/(1.0*RAND_MAX)));
		y=(int) ((HEIGHT-1)*(rand()/(1.0*RAND_MAX)));
		rr=pow((rand()/(1.0*RAND_MAX)),2.0)+pow((rand()/(1.0*RAND_MAX)),2.0)
		  +pow((rand()/(1.0*RAND_MAX)),2.0);
		rr*=10;
		b=(int) (10000.0/(rr+1));
		makestar(x,y,b,3*fwhm(),fwhm());
	}
	/*some bright stars*/
	for (i=1;i<50;i++) {
		x=(int) ((WIDTH-1)*(rand()/(1.0*RAND_MAX)));
		y=(int) ((HEIGHT-1)*(rand()/(1.0*RAND_MAX)));
		rr=pow((rand()/(1.0*RAND_MAX)),2.0)+pow((rand()/(1.0*RAND_MAX)),2.0)
		  +pow((rand()/(1.0*RAND_MAX)),2.0);
		rr*=10;
		b=(unsigned long) (10000.0/(rr+1));
		if (BYTESPERPIX>1) {
			b=(unsigned long) b*(pow(256.0,BYTESPERPIX-2.0));
		}
		makestar(x,y,b,3*fwhm(),fwhm() );
	}
	/*some  stars of defined brightness */
	for (i=1;i<3;i++) {
		x=(int) ((WIDTH-1)*(rand()/(1.0*RAND_MAX)));
		y=(int) ((HEIGHT-1)*(rand()/(1.0*RAND_MAX)));
		b=65000;
		makestar(x,y,b,100,fwhm());
	}
	makestar(WIDTH/2,HEIGHT/2,65000,100,fwhm());
}