Rev 39 | Details | Compare with Previous | Last modification | View Log | RSS feed
| Rev | Author | Line No. | Line |
|---|---|---|---|
| 29 | f9daq | 1 | #define USE_DAQ |
| 2 | #define USE_MIKRO |
||
| 3 | |||
| 4 | // Izberi ustrezni interface v meniju projektnega okna |
||
| 5 | // Options->CompilerDefines (dodaj /DSISVME ali /DWIENVME) |
||
| 6 | #ifdef USE_DAQ |
||
| 7 | # define USE_CAMAC |
||
| 32 | f9daq | 8 | # include "c:\HOME\dino\sipmScan\include\camac.h" |
| 29 | f9daq | 9 | # ifdef SISVME |
| 10 | # include "sisvme_dll.h" |
||
| 11 | # endif |
||
| 12 | # ifdef WIENVME |
||
| 13 | # include "wienvme_dll.h" |
||
| 14 | # endif |
||
| 15 | # include "CAENV673A.h" |
||
| 16 | # include "CAENV462.h" |
||
| 17 | #endif |
||
| 18 | |||
| 19 | #ifdef USE_MIKRO |
||
| 20 | # include "MIKRO.h" |
||
| 21 | #endif |
||
| 22 | |||
| 23 | #include <userint.h> |
||
| 24 | #include <ansi_c.h> |
||
| 25 | #include <utility.h> |
||
| 26 | #include <analysis.h> |
||
| 90 | f9daq | 27 | #include <time.h> |
| 28 | //#include <zlib.h> |
||
| 29 | f9daq | 29 | |
| 32 | f9daq | 30 | #include "c:\HOME\dino\sipmScan\include\sipmScan_ui.h" |
| 29 | f9daq | 31 | |
| 32 | #ifdef USE_DAQ |
||
| 33 | # define USE_CAMAC |
||
| 34 | # include "camac.h" |
||
| 35 | # define NDAC 1 |
||
| 39 | f9daq | 36 | # define ASD8 0 // channels |
| 37 | # define AMPDISSH 1 |
||
| 90 | f9daq | 38 | # define SIPM 15 |
| 29 | f9daq | 39 | //# define CAEN_V673A 0x10110000 // FMF |
| 40 | # define CAEN_V673A 0x22220000 // IJS |
||
| 41 | # define CAEN_V462 0x100300 |
||
| 42 | #endif |
||
| 43 | |||
| 44 | #ifdef USE_MIKRO |
||
| 90 | f9daq | 45 | # define MIKRO_COM 3 |
| 29 | f9daq | 46 | # define MIKRO_X 1 |
| 47 | # define USE_MIKRO_Y |
||
| 48 | # define MIKRO_Y 2 |
||
| 49 | # define USE_MIKRO_Z |
||
| 50 | # define MIKRO_Z 3 |
||
| 51 | # define STEP_TOLERANCE 1 |
||
| 52 | #endif |
||
| 53 | |||
| 90 | f9daq | 54 | #define MAXCH 128 |
| 29 | f9daq | 55 | #define MAX_THREADS 10 |
| 56 | |||
| 57 | #define IWAIT 200 |
||
| 58 | |||
| 39 | f9daq | 59 | #define POWERSUPPLY 66 // Power supply voltage for SiPM bias |
| 29 | f9daq | 60 | #define NCH 64 |
| 61 | |||
| 62 | static int p1h, pID, rID, tfID; |
||
| 63 | static int ph_tdc, ph_adc; |
||
| 64 | static int dtdc[NCH][2][MAXCH]; |
||
| 65 | static int something[NCH][2][MAXCH]; |
||
| 66 | |||
| 67 | static int daq_on; |
||
| 68 | static int poolHandle = 0; |
||
| 69 | static int ntics,dummy; |
||
| 70 | |||
| 90 | f9daq | 71 | static int offsetX = 5600; |
| 72 | static int offsetY = 5400; |
||
| 73 | |||
| 29 | f9daq | 74 | /************************************************************/ |
| 75 | void wait_loop(unsigned long iloop) |
||
| 76 | |||
| 77 | { |
||
| 78 | int i; |
||
| 79 | |||
| 80 | for (i=0;i<iloop;i++); |
||
| 81 | return; |
||
| 82 | } |
||
| 83 | |||
| 84 | int CVICALLBACK cb_timer (int panel, int control, int event, void *callbackData, |
||
| 85 | int eventData1, int eventData2) |
||
| 86 | { |
||
| 87 | QueueUserEvent (9000, p1h, P1_TIMER); |
||
| 88 | return (0); |
||
| 89 | } |
||
| 90 | |||
| 91 | int update_plots (void) |
||
| 92 | { |
||
| 93 | int irange, ch; |
||
| 94 | |||
| 95 | GetCtrlVal (p1h, P1_PLCH, &ch); |
||
| 96 | |||
| 97 | if (ph_tdc>0) DeleteGraphPlot (p1h, P1_TDC, ph_tdc, VAL_DELAYED_DRAW); |
||
| 98 | GetCtrlVal (p1h, P1_TDCHL, &irange); |
||
| 99 | ph_tdc = PlotY (p1h, P1_TDC, &dtdc[ch][irange], MAXCH, VAL_INTEGER, |
||
| 100 | VAL_VERTICAL_BAR, VAL_EMPTY_SQUARE, VAL_SOLID, 1, VAL_RED); |
||
| 101 | |||
| 102 | if (ph_adc>0) DeleteGraphPlot (p1h, P1_ADC, ph_adc, VAL_DELAYED_DRAW); |
||
| 103 | GetCtrlVal (p1h, P1_ADCHL, &irange); |
||
| 104 | ph_adc = PlotY (p1h, P1_ADC, &something[ch][irange], MAXCH, VAL_INTEGER, |
||
| 105 | VAL_VERTICAL_BAR, VAL_EMPTY_SQUARE, VAL_SOLID, 1, VAL_BLUE); |
||
| 106 | return (0); |
||
| 107 | } |
||
| 108 | |||
| 109 | |||
| 110 | int module_header(int recid,unsigned long *data,int len){ |
||
| 111 | data[0] = recid; |
||
| 112 | data[1] = (len >0)? len : 0 ; |
||
| 113 | return data[1]+2; |
||
| 114 | } |
||
| 115 | |||
| 116 | int CVICALLBACK daq_run(void *functionData) |
||
| 117 | { |
||
| 118 | // *** |
||
| 119 | // variable declarations |
||
| 120 | int i,j; |
||
| 121 | int dtype,ch,rg,adc,cres,bsr; |
||
| 122 | |||
| 123 | //unsigned short aa[NCH][4]; |
||
| 124 | unsigned long tdc[NCH]; |
||
| 125 | unsigned long step_minutes, end_time_s, cur_time_s; |
||
| 126 | int start_hours, start_minutes, start_seconds; |
||
| 127 | |||
| 128 | int status,fmax,fcount,fev; |
||
| 129 | int popupret; |
||
| 130 | |||
| 131 | char dfile[MAX_PATHNAME_LEN],dfile0[MAX_PATHNAME_LEN]; |
||
| 132 | int supr0,tdcmin,fseed; |
||
| 133 | float frac; |
||
| 134 | double fracg; |
||
| 135 | unsigned short dum16; |
||
| 136 | int count; |
||
| 137 | int print = 0; |
||
| 138 | int timer_out = 0; |
||
| 139 | //time_t t,told, tstart; |
||
| 140 | int ntrig; |
||
| 141 | int chip, len; |
||
| 142 | unsigned long *pdata; |
||
| 143 | |||
| 144 | FILE *fp; |
||
| 145 | #define RUNREC_ID 1 |
||
| 146 | #define ENDREC_ID 2 |
||
| 147 | #define POSREC_ID 3 |
||
| 148 | #define EVTREC_ID 4 |
||
| 149 | |||
| 150 | typedef struct { |
||
| 151 | unsigned long id,len; |
||
| 152 | unsigned long fver,time; |
||
| 153 | unsigned long nev,nch,ped,xy; |
||
| 154 | long nx,x0,dx,ny,y0,dy; |
||
| 155 | } RUNREC; |
||
| 156 | RUNREC runrec; |
||
| 157 | |||
| 158 | typedef struct { |
||
| 159 | unsigned long id,len; |
||
| 160 | unsigned long time; |
||
| 161 | } ENDREC; |
||
| 162 | ENDREC endrec; |
||
| 163 | |||
| 164 | typedef struct { |
||
| 165 | unsigned long id,len; |
||
| 166 | unsigned long time; |
||
| 167 | long ix,x,xset,iy,y,yset; |
||
| 168 | } POSREC; |
||
| 169 | POSREC posrec; |
||
| 170 | long mikroX; |
||
| 171 | long mikroY; |
||
| 172 | |||
| 173 | typedef struct { |
||
| 174 | unsigned long id,len; |
||
| 175 | unsigned long nev; |
||
| 176 | //unsigned short data[NCH]; |
||
| 177 | } EVTREC; |
||
| 178 | EVTREC evtrec; |
||
| 179 | #define BSIZE 20000 |
||
| 180 | |||
| 181 | int maxn = BSIZE-4; // 2 words x 2 headers |
||
| 182 | int tdcdata, edge_type, overflow, tdc_num, channel, ev_dat, last_dat, nval_dat; |
||
| 183 | // end of declarations |
||
| 184 | // *** |
||
| 185 | |||
| 186 | runrec.id = RUNREC_ID; |
||
| 187 | runrec.len = sizeof(runrec); |
||
| 188 | //runrec.fver = 0x10000; |
||
| 189 | runrec.nch = NCH; |
||
| 190 | GetCtrlVal(p1h, P1_ADCHLSAVE, &runrec.xy);//runrec.xy = 1; |
||
| 191 | GetCtrlVal(p1h, P1_DEBUG, &print); |
||
| 192 | endrec.id = ENDREC_ID; |
||
| 193 | endrec.len = sizeof(endrec); |
||
| 194 | posrec.id = POSREC_ID; |
||
| 195 | posrec.len = sizeof(posrec); |
||
| 196 | evtrec.id = EVTREC_ID; |
||
| 197 | evtrec.len = sizeof(evtrec); |
||
| 198 | |||
| 199 | |||
| 200 | cres = 0; |
||
| 201 | |||
| 202 | GetCtrlVal (p1h, P1_NEVE, &runrec.nev); |
||
| 203 | GetCtrlVal (p1h, P1_PEDESTAL, &runrec.ped); |
||
| 204 | |||
| 205 | GetCtrlVal (p1h, P1_NX, &runrec.nx); |
||
| 206 | GetCtrlVal (p1h, P1_XSTEP, &runrec.dx); |
||
| 207 | GetCtrlVal (p1h, P1_XMIN, &runrec.x0); |
||
| 208 | GetCtrlVal (p1h, P1_NY, &runrec.ny); |
||
| 209 | GetCtrlVal (p1h, P1_YSTEP, &runrec.dy); |
||
| 210 | GetCtrlVal (p1h, P1_YMIN, &runrec.y0); |
||
| 211 | |||
| 212 | GetCtrlVal (p1h, P1_NMIN, &step_minutes); |
||
| 213 | GetSystemTime(&start_hours, &start_minutes, &start_seconds); |
||
| 214 | //cur_time_s = start_hours*3600 + start_minutes*60 + start_seconds; |
||
| 215 | time(&cur_time_s); |
||
| 90 | f9daq | 216 | end_time_s = cur_time_s + (int) (runrec.nx * runrec.ny * runrec.nev/10000.0 * 4.5); // expected seconds |
| 217 | printf("START:%2d-%2d-%2d (cur_time = %u s, end_time = %u s)\n", start_hours, start_minutes, start_seconds, cur_time_s, end_time_s); |
||
| 218 | SetCtrlAttribute(p1h, P1_EXPTIME, ATTR_DIMMED, 0); |
||
| 219 | SetCtrlVal(p1h, P1_EXPTIME, ctime(&end_time_s)); |
||
| 29 | f9daq | 220 | |
| 221 | //GetCtrlVal (p1h, P1_DSAVE, &dsave); |
||
| 222 | //if (dsave) { |
||
| 223 | GetCtrlVal (p1h, P1_DFILE, dfile0); |
||
| 224 | |||
| 225 | fev=0; |
||
| 226 | fcount=1; |
||
| 227 | GetCtrlVal (p1h, P1_NEWF, &fmax); |
||
| 228 | fmax*=1000000;//fmax in Mega Bytes |
||
| 229 | //} |
||
| 230 | GetCtrlVal (p1h, P1_SUPR, &supr0); |
||
| 231 | if (supr0) { |
||
| 232 | GetCtrlVal (p1h, P1_TDCMIN, &tdcmin); |
||
| 233 | GetCtrlVal (p1h, P1_FRAC, &frac); |
||
| 234 | } |
||
| 235 | |||
| 236 | #ifdef USE_DAQ |
||
| 237 | V673A_map(0,CAEN_V673A,0); |
||
| 238 | V673A_init(0); |
||
| 239 | |||
| 240 | V462_map(0,CAEN_V462,0); |
||
| 241 | V462_set0(0,1); |
||
| 242 | #endif |
||
| 243 | |||
| 244 | //if (dsave) { |
||
| 245 | sprintf(dfile,"%s_file%02d.dat",dfile0,fcount); |
||
| 90 | f9daq | 246 | SetCtrlVal(p1h, P1_FCOUNT,fcount); |
| 29 | f9daq | 247 | |
| 248 | fp = fopen (dfile, "wb"); |
||
| 249 | time (&runrec.time); |
||
| 250 | status = fwrite (&runrec, 1, runrec.len, fp); |
||
| 251 | //} |
||
| 252 | if (supr0) { |
||
| 253 | fseed = runrec.time & 0x7fffffff; |
||
| 254 | Uniform (1, fseed, &fracg); |
||
| 255 | } |
||
| 90 | f9daq | 256 | // vertical moving stage loop |
| 257 | for (posrec.iy=0; posrec.iy<runrec.ny; posrec.iy++) { |
||
| 258 | posrec.yset = runrec.y0 + posrec.iy*runrec.dy; |
||
| 259 | #ifdef USE_MIKRO_Y |
||
| 260 | if (print) printf("MIKRO_MoveTo (2, y);%d\n",posrec.yset); |
||
| 261 | do { |
||
| 262 | MIKRO_MoveTo (MIKRO_Y, offsetY + posrec.yset); |
||
| 263 | MIKRO_GetPosition(MIKRO_Y, &mikroY); |
||
| 264 | mikroY -= offsetY; |
||
| 265 | if (print) printf("%d\n", abs(posrec.yset - mikroY)); |
||
| 266 | } while (abs(posrec.yset - mikroY) > STEP_TOLERANCE); |
||
| 267 | if (print) printf("->MIKRO_MoveTo (2, y);%d\n",posrec.yset); |
||
| 268 | #endif |
||
| 269 | posrec.yset = mikroY; |
||
| 270 | SetCtrlVal (p1h, P1_Y, posrec.yset); |
||
| 271 | SetCtrlVal (p1h, P1_IY, posrec.iy); |
||
| 29 | f9daq | 272 | |
| 90 | f9daq | 273 | // horizontal moving stage loop |
| 274 | for (posrec.ix=0; posrec.ix<runrec.nx; posrec.ix++) { |
||
| 275 | posrec.xset = runrec.x0 + posrec.ix*runrec.dx; |
||
| 29 | f9daq | 276 | #ifdef USE_MIKRO |
| 277 | do { |
||
| 32 | f9daq | 278 | if (print) printf("MIKRO_MoveTo (1, x);%d\n",posrec.xset); |
| 90 | f9daq | 279 | MIKRO_MoveTo (MIKRO_X, offsetX + posrec.xset); |
| 29 | f9daq | 280 | MIKRO_GetPosition(MIKRO_X, &mikroX); |
| 90 | f9daq | 281 | mikroX -= offsetX; |
| 32 | f9daq | 282 | if (print) printf("%d\n", abs(posrec.xset - mikroX)); |
| 29 | f9daq | 283 | } while (abs(posrec.xset - mikroX) > STEP_TOLERANCE); |
| 284 | #endif |
||
| 90 | f9daq | 285 | posrec.xset = mikroX; // true value |
| 29 | f9daq | 286 | SetCtrlVal (p1h, P1_X, posrec.xset); |
| 287 | SetCtrlVal (p1h, P1_IX, posrec.ix); |
||
| 90 | f9daq | 288 | |
| 289 | |||
| 290 | //if (dsave) { |
||
| 291 | if (fmax && (ftell(fp) > fmax)) { |
||
| 29 | f9daq | 292 | fcount+=1; |
| 293 | sprintf(dfile,"%s_file%02d.dat",dfile0,fcount); |
||
| 294 | fclose(fp); |
||
| 295 | fp = fopen (dfile, "wb"); |
||
| 296 | } |
||
| 297 | |||
| 298 | time (&posrec.time); |
||
| 299 | status = fwrite (&posrec, 1, posrec.len, fp); |
||
| 300 | if (print) printf("POSREC status %d len %d\n", status, posrec.len); |
||
| 301 | //} |
||
| 302 | |||
| 303 | // clear the plots |
||
| 304 | for (j=0;j<NCH;j++) { |
||
| 305 | for (i=0;i<MAXCH;i++){ |
||
| 306 | dtdc[j][0][i]=0; |
||
| 307 | dtdc[j][1][i]=0; |
||
| 308 | } |
||
| 309 | } |
||
| 310 | |||
| 311 | evtrec.nev=1; |
||
| 312 | //time(&t); |
||
| 313 | //tstart=t; |
||
| 314 | if (print) printf("RUN START.\n"); |
||
| 315 | |||
| 316 | do { |
||
| 317 | unsigned long word[BSIZE]; |
||
| 318 | count=0; |
||
| 319 | ntics=0; |
||
| 320 | if (print) printf("Event counter: %d\n", evtrec.nev); |
||
| 321 | |||
| 322 | //if (t!=told ) printf("%d in %2.2f min daq::event() %s\n",i, (double)(t-tstart)/60., ctime(&t)); |
||
| 323 | |||
| 324 | #ifdef USE_DAQ |
||
| 325 | #ifdef CAEN_V673A |
||
| 326 | |||
| 327 | |||
| 328 | V462_start0(0); |
||
| 329 | //tmlnk (tout); |
||
| 330 | do { |
||
| 331 | ntrig = V673A_ntrig(0); |
||
| 332 | } while (ntrig==0 &&(ntics<2)); |
||
| 333 | //tmulk(); |
||
| 334 | //if (timer_out) { |
||
| 335 | // fprintf(stderr,"-----------------> V673a timeout !!!\n"); |
||
| 336 | // V673A_clallb(0); |
||
| 337 | // continue; |
||
| 338 | //} |
||
| 339 | |||
| 340 | if (ntics>=2) { |
||
| 341 | fprintf(stderr,"-----------------> V673a timeout !!!\n"); |
||
| 342 | printf("Timer tics %d\n", ntics); |
||
| 343 | ntics = 0; |
||
| 344 | evtrec.nev--; |
||
| 345 | V673A_clallb(0); |
||
| 346 | continue; |
||
| 347 | } |
||
| 348 | |||
| 349 | if (ntrig>0){ |
||
| 350 | // chip readout |
||
| 351 | for (chip=0;chip<2;chip++){ |
||
| 352 | if (chip) len = V673A_read1 (0, &word[count+2],maxn-count-2); |
||
| 353 | else len = V673A_read0 (0, &word[count+2],maxn-count-2); |
||
| 354 | |||
| 355 | if (len>16*32*4){ |
||
| 356 | if (print) printf("Length > 2048 per chip!\n"); |
||
| 357 | chip=2; |
||
| 358 | V673A_clallb(0); |
||
| 359 | evtrec.nev--; |
||
| 360 | continue; |
||
| 361 | } |
||
| 362 | pdata=&word[count+2]; |
||
| 363 | for(i=0;i< len;i++){ |
||
| 364 | tdcdata = pdata[i] & 0xFFFF ; |
||
| 365 | edge_type = (pdata[i]>>16)&0x1 ; |
||
| 366 | overflow = (pdata[i]>>17)&0x1 ; |
||
| 367 | tdc_num = (pdata[i]>>25)&0x1 ; |
||
| 368 | channel = ((pdata[i]>>18)&0x1F) |tdc_num<<5 ; |
||
| 369 | ev_dat = (pdata[i]>>23)&0x1 ; |
||
| 370 | last_dat = (pdata[i]>>30)&0x1 ; |
||
| 371 | nval_dat = (pdata[i]>>31)&0x1 ; |
||
| 372 | if (ev_dat) { |
||
| 373 | if(print) printf("Event %d\n",tdcdata); |
||
| 374 | } else { |
||
| 375 | dtdc[channel][edge_type][tdcdata]++; |
||
| 376 | |||
| 377 | if (print) printf |
||
| 378 | ("ch=%d edge=%d ev=%d data=%d last=%d nval=%d\n",channel, edge_type,ev_dat,tdcdata,last_dat,nval_dat); |
||
| 379 | } |
||
| 380 | } |
||
| 381 | if (count+2 < maxn) count+=module_header(0x140+chip,&word[count],len); |
||
| 382 | } |
||
| 383 | if (print) printf("Received triggers: %d \n", ntrig); |
||
| 384 | } else { |
||
| 385 | if (ntrig==0) { |
||
| 386 | fprintf(stderr,"-----------------> no trigger"); |
||
| 90 | f9daq | 387 | //break; |
| 29 | f9daq | 388 | } else { |
| 389 | if (print) fprintf(stderr,"-----------------> wrong number of triggers in chips V673a !!!\n"); |
||
| 390 | } |
||
| 391 | V673A_clallb(0); |
||
| 392 | evtrec.nev--; |
||
| 393 | continue; |
||
| 394 | } |
||
| 395 | |||
| 396 | V673A_clallb(0); |
||
| 397 | #endif |
||
| 398 | #endif |
||
| 399 | |||
| 400 | |||
| 401 | GetCtrlVal(p1h, P1_ADCHLSAVE, &runrec.xy); |
||
| 402 | evtrec.len = count*sizeof(unsigned long) + sizeof(evtrec); |
||
| 403 | status = fwrite (&evtrec, 1, sizeof(evtrec), fp); |
||
| 404 | status = fwrite (&word, 1, count*sizeof(unsigned long), fp); |
||
| 405 | |||
| 406 | |||
| 407 | if (!(evtrec.nev%1000)) SetCtrlVal (p1h, P1_CEVE, evtrec.nev); |
||
| 408 | |||
| 409 | if (!((evtrec.nev+1)%1000)) { |
||
| 410 | if (fmax && (ftell(fp) > fmax)) { |
||
| 411 | time (&endrec.time); |
||
| 412 | status = fwrite (&endrec, 1, endrec.len, fp); |
||
| 413 | fcount+=1; |
||
| 414 | sprintf(dfile,"%s_file%02d.dat",dfile0,fcount); |
||
| 90 | f9daq | 415 | SetCtrlVal(p1h, P1_FCOUNT,fcount); |
| 29 | f9daq | 416 | fclose(fp); |
| 417 | fp = fopen (dfile, "wb"); |
||
| 418 | } |
||
| 419 | } |
||
| 420 | |||
| 421 | if(step_minutes > 0) { |
||
| 422 | GetSystemTime(&start_hours, &start_minutes, &start_seconds); |
||
| 423 | //cur_time_s = start_hours*3600 + start_minutes*60 + start_seconds; |
||
| 424 | time(&cur_time_s); |
||
| 425 | if(cur_time_s >= end_time_s) { |
||
| 426 | end_time_s = cur_time_s + step_minutes*60; |
||
| 427 | printf("STEP (nev):%2d-%2d-%2d @ %d\n", start_hours, start_minutes, start_seconds, posrec.xset); |
||
| 428 | break; |
||
| 429 | } |
||
| 430 | } |
||
| 431 | //told=t; |
||
| 432 | |||
| 433 | GetCtrlVal(p1h, P1_DAQ, &daq_on); |
||
| 434 | |||
| 435 | } while (evtrec.nev++<runrec.nev && daq_on); |
||
| 436 | if (!daq_on) break; |
||
| 90 | f9daq | 437 | } // y loop |
| 29 | f9daq | 438 | if (!daq_on) break; |
| 90 | f9daq | 439 | } // x loop |
| 29 | f9daq | 440 | |
| 441 | //if (dsave) { |
||
| 442 | time (&endrec.time); |
||
| 443 | status = fwrite (&endrec, 1, endrec.len, fp); |
||
| 444 | if (print) printf("ENDREC status %d len %d\n", status, endrec.len); |
||
| 445 | fclose(fp); |
||
| 446 | //} |
||
| 447 | GetSystemTime(&start_hours, &start_minutes, &start_seconds); |
||
| 448 | printf("STOP:%2d-%2d-%2d (start_time = %u s, end_time = %u s)\n", start_hours, start_minutes, start_seconds, runrec.time, endrec.time); |
||
| 449 | printf("Elapsed time: %u s.\n", endrec.time-runrec.time); |
||
| 450 | |||
| 451 | daq_on=0; |
||
| 452 | SetCtrlVal (p1h, P1_DAQ, daq_on); |
||
| 453 | SetCtrlVal (p1h, P1_CEVE, evtrec.nev); |
||
| 454 | if (print) printf("RUN END. \n\n"); |
||
| 455 | |||
| 456 | return 0; |
||
| 457 | } |
||
| 458 | |||
| 459 | |||
| 460 | int SetDac(int ch, double val){ |
||
| 461 | |||
| 462 | const unsigned int maxdac=0xFFF; |
||
| 463 | const double RANGE = +9.9976; //V |
||
| 464 | unsigned int dac; |
||
| 465 | |||
| 466 | if ((val > RANGE) || (val < 0)) { |
||
| 467 | printf("DAC value OUT OF RANGE!!!\n"); |
||
| 468 | return(-1); |
||
| 469 | } |
||
| 470 | |||
| 471 | dac = (val/RANGE)*maxdac; |
||
| 472 | CSSA_W(NDAC,ch,16,&dac); |
||
| 473 | printf("DAC ch %d set to %f V dac=%d\n", ch, val, dac); |
||
| 474 | return 0; |
||
| 475 | } |
||
| 476 | |||
| 477 | int __stdcall WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, |
||
| 478 | LPSTR lpszCmdLine, int nCmdShow) |
||
| 479 | { |
||
| 480 | int i,j,status,refon; |
||
| 481 | long int xpos, ypos, zpos; |
||
| 90 | f9daq | 482 | char dfile[512]; |
| 483 | char message[256]; |
||
| 29 | f9daq | 484 | FILE *fp; |
| 485 | |||
| 486 | typedef struct { |
||
| 487 | unsigned long id,len; |
||
| 488 | unsigned long fver,time; |
||
| 489 | unsigned long nev,nch,ped,xy; |
||
| 490 | long nx,x0,dx,ny,y0,dy; |
||
| 491 | } RUNREC; |
||
| 492 | RUNREC runrec; |
||
| 493 | |||
| 494 | if (InitCVIRTE (hInstance, 0, 0) == 0) |
||
| 495 | return -1; /* out of memory */ |
||
| 496 | |||
| 497 | SetSleepPolicy(VAL_SLEEP_MORE); |
||
| 498 | CmtNewThreadPool (MAX_THREADS, &poolHandle); |
||
| 499 | |||
| 500 | SetStdioWindowOptions (1000, 0, 0); |
||
| 501 | SetStdioWindowSize (200, 560); |
||
| 502 | SetStdioWindowPosition (825, 250); |
||
| 503 | |||
| 504 | #ifdef USE_DAQ |
||
| 505 | VME_START(NULL); |
||
| 506 | #endif |
||
| 507 | |||
| 508 | #ifdef USE_MIKRO |
||
| 509 | MIKRO_Open (MIKRO_COM); |
||
| 510 | MIKRO_Init (MIKRO_X,0); |
||
| 511 | #ifdef USE_MIKRO_Y |
||
| 512 | MIKRO_Init (MIKRO_Y,0); |
||
| 513 | #endif |
||
| 514 | #ifdef USE_MIKRO_Z |
||
| 515 | MIKRO_Init (MIKRO_Z,0); |
||
| 516 | #endif |
||
| 517 | #endif |
||
| 518 | |||
| 519 | if ((p1h = LoadPanel (0, "include/sipmScan_ui.uir", P1)) < 0) return -1; |
||
| 520 | DisplayPanel (p1h); |
||
| 521 | SetCtrlAttribute (p1h, P1_PLCH, ATTR_MAX_VALUE, NCH-1); |
||
| 522 | |||
| 90 | f9daq | 523 | SetCtrlVal (p1h, P1_OFFSETX, offsetX); |
| 524 | SetCtrlVal (p1h, P1_OFFSETY, offsetY); |
||
| 525 | |||
| 29 | f9daq | 526 | GetCtrlVal(p1h, P1_ADCHLSAVE, &runrec.xy); |
| 527 | SetCtrlVal(p1h, P1_ADCHL, runrec.xy-2); |
||
| 528 | |||
| 529 | QueueUserEvent (1000, p1h, P1_RESET); |
||
| 530 | QueueUserEvent (1000, p1h, P1_ASD8THR); |
||
| 531 | QueueUserEvent (1000, p1h, P1_AMPDISSHTHR); |
||
| 39 | f9daq | 532 | QueueUserEvent (1000, p1h, P1_BIAS); |
| 90 | f9daq | 533 | QueueUserEvent (1000, p1h, P1_OFFSETX); |
| 534 | QueueUserEvent (1000, p1h, P1_OFFSETY); |
||
| 39 | f9daq | 535 | |
| 536 | printf("BIAS offset set to: %d\n", POWERSUPPLY); |
||
| 537 | |||
| 29 | f9daq | 538 | do { |
| 539 | GetUserEvent (1, &pID, &rID); |
||
| 540 | switch (rID) { |
||
| 541 | case P1_TIMER: |
||
| 542 | ntics+=1; |
||
| 543 | GetCtrlVal (p1h, P1_REFON, &refon); |
||
| 544 | if (refon) update_plots(); |
||
| 545 | break; |
||
| 546 | case P1_REFRESH: |
||
| 547 | update_plots(); |
||
| 548 | break; |
||
| 549 | case P1_DAQ: |
||
| 550 | GetCtrlVal (p1h, P1_DAQ, &daq_on); |
||
| 551 | if (daq_on) { |
||
| 552 | CmtScheduleThreadPoolFunction (poolHandle, daq_run, (void *)&dummy, &tfID); |
||
| 553 | } else { |
||
| 90 | f9daq | 554 | CmtWaitForThreadPoolFunctionCompletion (poolHandle, tfID, OPT_TP_PROCESS_EVENTS_WHILE_WAITING); |
| 29 | f9daq | 555 | CmtReleaseThreadPoolFunctionID (poolHandle, tfID); |
| 556 | } |
||
| 557 | break; |
||
| 90 | f9daq | 558 | case P1_XMIN: |
| 559 | if (!daq_on) { |
||
| 560 | GetCtrlVal (p1h, P1_XMIN, &xpos); |
||
| 561 | //backlash |
||
| 562 | MIKRO_MoveTo(MIKRO_X, xpos+offsetX-1000); |
||
| 563 | Delay(0.01); |
||
| 564 | MIKRO_MoveTo(MIKRO_X, xpos+offsetX); |
||
| 565 | } |
||
| 566 | break; |
||
| 567 | case P1_YMIN: |
||
| 568 | if (!daq_on) { |
||
| 569 | GetCtrlVal(p1h, P1_YMIN, &ypos); |
||
| 570 | MIKRO_MoveTo(MIKRO_Y, ypos+offsetY-1000); |
||
| 571 | Delay(0.01); |
||
| 572 | MIKRO_MoveTo(MIKRO_Y, ypos+offsetY); |
||
| 573 | } |
||
| 574 | break; |
||
| 29 | f9daq | 575 | case P1_ZSET: |
| 576 | if (!daq_on) { |
||
| 577 | GetCtrlVal (p1h, P1_ZSET, &zpos); |
||
| 578 | #ifdef USE_MIKRO_Z |
||
| 579 | MIKRO_MoveTo (MIKRO_Z, zpos); |
||
| 580 | #endif |
||
| 581 | } |
||
| 582 | break; |
||
| 583 | case P1_REREAD: |
||
| 584 | if (!daq_on) { |
||
| 585 | status = FileSelectPopup ("", "*.dat", ".dat", |
||
| 586 | "Izberi datoteko s podatki", |
||
| 587 | VAL_LOAD_BUTTON, 0, 0, 1, 0, dfile); |
||
| 588 | if (status==1) { |
||
| 589 | fp = fopen (dfile, "rb"); |
||
| 590 | status = fread (&runrec, 1, sizeof(runrec), fp); |
||
| 591 | fclose(fp); |
||
| 592 | if (runrec.id==1) { |
||
| 593 | SetCtrlVal (p1h, P1_NX, runrec.nx); |
||
| 594 | SetCtrlVal (p1h, P1_XSTEP, runrec.dx); |
||
| 595 | SetCtrlVal (p1h, P1_XMIN, runrec.x0); |
||
| 596 | SetCtrlVal (p1h, P1_NY, runrec.ny); |
||
| 597 | SetCtrlVal (p1h, P1_YSTEP, runrec.dy); |
||
| 598 | SetCtrlVal (p1h, P1_YMIN, runrec.y0); |
||
| 599 | SetCtrlVal (p1h, P1_NEVE, runrec.nev); |
||
| 600 | } |
||
| 601 | } |
||
| 602 | } |
||
| 603 | break; |
||
| 604 | case P1_MGET: |
||
| 605 | #ifdef USE_MIKRO |
||
| 606 | MIKRO_GetPosition(MIKRO_X,&xpos); |
||
| 607 | Delay(0.01); |
||
| 90 | f9daq | 608 | SetCtrlVal (p1h, P1_X, xpos-offsetX); |
| 29 | f9daq | 609 | #ifdef USE_MIKRO_Y |
| 610 | MIKRO_GetPosition(MIKRO_Y,&ypos); |
||
| 611 | Delay(0.01); |
||
| 90 | f9daq | 612 | SetCtrlVal (p1h, P1_Y, ypos-offsetY); |
| 29 | f9daq | 613 | #endif |
| 614 | #ifdef USE_MIKRO_Z |
||
| 615 | MIKRO_GetPosition(MIKRO_Z,&zpos); |
||
| 616 | Delay(0.01); |
||
| 617 | SetCtrlVal (p1h, P1_Z, zpos); |
||
| 618 | #endif |
||
| 619 | #endif |
||
| 620 | break; |
||
| 621 | case P1_HO: |
||
| 622 | if (!daq_on) { |
||
| 623 | SetWaitCursor (1); |
||
| 624 | #ifdef USE_MIKRO |
||
| 625 | MIKRO_ReferenceMove (MIKRO_X); |
||
| 626 | #ifdef USE_MIKRO_Y |
||
| 627 | MIKRO_ReferenceMove (MIKRO_Y); |
||
| 628 | #endif |
||
| 629 | #ifdef USE_MIKRO_Z |
||
| 630 | MIKRO_ReferenceMove (MIKRO_Z); |
||
| 631 | #endif |
||
| 632 | #endif |
||
| 633 | SetWaitCursor (0); |
||
| 634 | } |
||
| 635 | break; |
||
| 636 | case P1_RESET: |
||
| 637 | for (j=0;j<NCH;j++) { |
||
| 638 | for (i=0;i<MAXCH;i++){ |
||
| 639 | dtdc[j][0][i]=0; |
||
| 640 | dtdc[j][1][i]=0; |
||
| 641 | |||
| 642 | } |
||
| 643 | } |
||
| 644 | update_plots(); |
||
| 645 | break; |
||
| 646 | case P1_TDCLOG: |
||
| 647 | GetCtrlVal (p1h, P1_TDCLOG, &status); |
||
| 648 | SetCtrlAttribute (p1h, P1_TDC, ATTR_YMAP_MODE, status); |
||
| 649 | update_plots(); |
||
| 650 | break; |
||
| 651 | case P1_ADCLOG: |
||
| 652 | GetCtrlVal (p1h, P1_ADCLOG, &status); |
||
| 653 | SetCtrlAttribute (p1h, P1_ADC, ATTR_YMAP_MODE, status); |
||
| 654 | update_plots(); |
||
| 655 | break; |
||
| 656 | case P1_ASD8THR: |
||
| 657 | { |
||
| 658 | double value; |
||
| 659 | GetCtrlVal (p1h, P1_SASD8THR, &value); |
||
| 39 | f9daq | 660 | SetDac(ASD8, value); |
| 29 | f9daq | 661 | |
| 662 | } |
||
| 663 | break; |
||
| 664 | case P1_AMPDISSHTHR: |
||
| 665 | { |
||
| 666 | double value; |
||
| 667 | GetCtrlVal (p1h, P1_SAMPDISSHTHR, &value); |
||
| 39 | f9daq | 668 | SetDac(AMPDISSH, value); |
| 29 | f9daq | 669 | |
| 670 | } |
||
| 671 | break; |
||
| 39 | f9daq | 672 | case P1_BIAS: |
| 673 | { |
||
| 674 | double value; |
||
| 675 | GetCtrlVal (p1h, P1_SBIAS, &value); |
||
| 676 | value -= POWERSUPPLY; |
||
| 677 | SetDac(SIPM, value); |
||
| 678 | } |
||
| 679 | break; |
||
| 90 | f9daq | 680 | // reset SiPM bias to initial power supply voltage |
| 681 | case P1_BIASOFF: |
||
| 682 | { |
||
| 683 | SetDac(SIPM, 0); |
||
| 684 | SetCtrlVal(p1h, P1_SBIAS, (double) POWERSUPPLY); |
||
| 685 | } |
||
| 686 | case P1_OFFSETX: |
||
| 687 | { |
||
| 688 | GetCtrlVal(p1h, P1_OFFSETX, &offsetX); |
||
| 689 | } |
||
| 690 | case P1_OFFSETY: |
||
| 691 | { |
||
| 692 | GetCtrlVal(p1h, P1_OFFSETY, &offsetY); |
||
| 693 | } |
||
| 29 | f9daq | 694 | |
| 695 | } |
||
| 696 | |||
| 697 | } while ((rID != P1_EXIT)||daq_on); |
||
| 698 | |||
| 699 | CmtDiscardThreadPool (poolHandle); |
||
| 700 | DiscardPanel (p1h); |
||
| 701 | |||
| 702 | #ifdef USE_MIKRO |
||
| 703 | MIKRO_Close (); |
||
| 704 | #endif |
||
| 705 | |||
| 706 | #ifdef USE_DAQ |
||
| 707 | VME_STOP(); |
||
| 708 | #endif |
||
| 709 | return 0; |
||
| 710 | |||
| 711 | } |