comparison FXAnalyse.c @ 142:fd085d61e4ca

Rework data logging
author Daniele Nicolodi <daniele.nicolodi@obspm.fr>
date Wed, 22 Jan 2014 14:45:23 +0100
parents 3f7eef731ccc
children 09a4548e1436
comparison
equal deleted inserted replaced
141:e1dec4f11831 142:fd085d61e4ca
40 // data providers 40 // data providers
41 int CVICALLBACK FakeDataProvider (void *functionData); 41 int CVICALLBACK FakeDataProvider (void *functionData);
42 int CVICALLBACK FileDataProvider (void *functionData); 42 int CVICALLBACK FileDataProvider (void *functionData);
43 int CVICALLBACK KKDataProvider (void *functionData); 43 int CVICALLBACK KKDataProvider (void *functionData);
44 44
45
45 // select which data provider to use 46 // select which data provider to use
46 #define DataProvider KKDataProvider 47 #define DataProvider KKDataProvider
47 48
48 49 struct event event;
49 double utc; 50 double utc;
50 double Ch1, Ch2, Ch3, Ch4; 51 double Ch1, Ch2, Ch3, Ch4;
51 double Math1, Math2, Math3, Math4, Math5; 52 double Math1, Math2, Math3, Math4, Math5;
52 double N1, N2, N3; 53 double N1, N2, N3;
53 54
283 .interval = 1800.0, 284 .interval = 1800.0,
284 .t0 = 0.0 285 .t0 = 0.0
285 }; 286 };
286 287
287 288
288 char * thousands(char *buffer, int size, char *fmt, double val) 289 // data loggging
290 struct datafile {
291 char *name;
292 double *data;
293 int nchan;
294 int control;
295 int write;
296 };
297
298
299 struct datafile datafiles[] = {
300 { "Raw", event.data, 4, PANEL_SAVE_RAW, FALSE },
301 { "DDS", DDS4xAD9912.frequency, 4, PANEL_SAVE_DDS, FALSE },
302 { "Lo", &Math2, 1, PANEL_SAVE_LO, FALSE },
303 { "Hg", &Math3, 1, PANEL_SAVE_HG, FALSE },
304 { "Sr", &Math4, 1, PANEL_SAVE_SR, FALSE },
305 { "Ex", &Math5, 1, PANEL_SAVE_EXTRA, FALSE },
306 { NULL, }
307 };
308
309
310 static void write_data(const char *folder, const char *name, const char *id,
311 const char *timestr, double utc, double *v, int nchan)
312 {
313 char line[1024];
314 char filename[FILENAME_MAX];
315
316 // construct filename in the form folder\\id-name.txt
317 snprintf(filename, sizeof(filename), "%s\\%s-%s.txt", folder, id, name);
318
319 int fd = OpenFile(filename, VAL_WRITE_ONLY, VAL_APPEND, VAL_ASCII);
320 switch (nchan)
321 {
322 case 1:
323 Fmt(line, "%s\t%f[p3]\t%f[p3]",
324 timestr, utc, v[0]);
325 break;
326 case 4:
327 Fmt(line, "%s\t%f[p3]\t%f[p3]\t%f[p3]\t%f[p3]\t%f[p3]",
328 timestr, utc, v[0], v[1], v[2], v[3]);
329 break;
330 default:
331 strcpy(line, "?");
332 }
333 WriteLine(fd, line, -1);
334 CloseFile(fd);
335 }
336
337
338 static inline void datafile_append(struct datafile *d, char *id, char *timestr)
339 {
340 if (d->write)
341 write_data(DATAFOLDER, d->name, id, timestr, utc, d->data, d->nchan);
342 }
343
344
345 static const char * thousands(char *buffer, int size, char *fmt, double val)
289 { 346 {
290 // compute how many separators we need 347 // compute how many separators we need
291 #pragma DisableFunctionRuntimeChecking log10 348 #pragma DisableFunctionRuntimeChecking log10
292 int nsep = log10(fabs(val)); 349 int nsep = log10(fabs(val));
293 nsep = (nsep > 0 ? nsep / 3 : 0); 350 nsep = (nsep > 0 ? nsep / 3 : 0);
344 401
345 // display message 402 // display message
346 SetCtrlVal(LoggingPanel, LOGGING_LOGGING, msg); 403 SetCtrlVal(LoggingPanel, LOGGING_LOGGING, msg);
347 } 404 }
348 405
406
407 int Sr_datalogger_enabled = FALSE;
408
409
349 // Sr data logger 410 // Sr data logger
350 int sendLogger(const char* id, double utc, double data) 411 int Sr_datalogger_send(const char* id, double utc, double data)
351 { 412 {
352 static unsigned int handle = 0; 413 static unsigned int handle = 0;
353 char buffer[1024]; 414 char buffer[1024];
354 415
355 // try to connect and quit if unsuccessfull 416 // try to connect and quit if unsuccessfull
363 424
364 snprintf(buffer, sizeof(buffer), "%s %.7f %.8f", id, utc2mjd(utc), data); 425 snprintf(buffer, sizeof(buffer), "%s %.7f %.8f", id, utc2mjd(utc), data);
365 if (ClientTCPWrite(handle, buffer, strlen(buffer) + 1, 0) < 0) { 426 if (ClientTCPWrite(handle, buffer, strlen(buffer) + 1, 0) < 0) {
366 // try to reconnect and resend 427 // try to reconnect and resend
367 handle = 0; 428 handle = 0;
368 sendLogger(id, utc, data); 429 Sr_datalogger_send(id, utc, data);
369 } 430 }
370 431
371 return 0; 432 return 0;
372 } 433 }
434
373 435
374 muParserHandle_t initMathParser() 436 muParserHandle_t initMathParser()
375 { 437 {
376 muParserHandle_t parser = mupCreate(); 438 muParserHandle_t parser = mupCreate();
377 mupDefineOprtChars(parser, "abcdefghijklmnopqrstuvwxyzµ" 439 mupDefineOprtChars(parser, "abcdefghijklmnopqrstuvwxyzµ"
380 mupDefineVar(parser, "UTC", &utc); 442 mupDefineVar(parser, "UTC", &utc);
381 mupDefineVar(parser, "Ch1", &Ch1); 443 mupDefineVar(parser, "Ch1", &Ch1);
382 mupDefineVar(parser, "Ch2", &Ch2); 444 mupDefineVar(parser, "Ch2", &Ch2);
383 mupDefineVar(parser, "Ch3", &Ch3); 445 mupDefineVar(parser, "Ch3", &Ch3);
384 mupDefineVar(parser, "Ch4", &Ch4); 446 mupDefineVar(parser, "Ch4", &Ch4);
385 mupDefineVar(parser, "DDS1", &(DDS4xAD9912.Frequency1)); 447 mupDefineVar(parser, "DDS1", &(DDS4xAD9912.frequency[0]));
386 mupDefineVar(parser, "DDS2", &(DDS4xAD9912.Frequency2)); 448 mupDefineVar(parser, "DDS2", &(DDS4xAD9912.frequency[1]));
387 mupDefineVar(parser, "DDS3", &(DDS4xAD9912.Frequency3)); 449 mupDefineVar(parser, "DDS3", &(DDS4xAD9912.frequency[2]));
388 mupDefineVar(parser, "DDS4", &(DDS4xAD9912.Frequency4)); 450 mupDefineVar(parser, "DDS4", &(DDS4xAD9912.frequency[3]));
389 mupDefineVar(parser, "N1", &N1); 451 mupDefineVar(parser, "N1", &N1);
390 mupDefineVar(parser, "N2", &N2); 452 mupDefineVar(parser, "N2", &N2);
391 mupDefineVar(parser, "N3", &N3); 453 mupDefineVar(parser, "N3", &N3);
392 mupDefineVar(parser, "Nu1", &Nu1); 454 mupDefineVar(parser, "Nu1", &Nu1);
393 mupDefineVar(parser, "Nu2", &Nu2); 455 mupDefineVar(parser, "Nu2", &Nu2);
410 472
411 return parser; 473 return parser;
412 } 474 }
413 475
414 476
415 void writeData(const char *folder, const char *name, const char *id,
416 const char *timestr, double utc, double value)
417 {
418 char line[1024];
419 char filename[FILENAME_MAX];
420
421 // construct filename in the form folder\\id-name.txt
422 snprintf(filename, sizeof(filename), "%s\\%s-%s.txt", folder, id, name);
423
424 int fd = OpenFile(filename, VAL_WRITE_ONLY, VAL_APPEND, VAL_ASCII);
425 Fmt(line, "%s\t%f[p3]\t%f[p3]", timestr, utc, value);
426 WriteLine(fd, line, -1);
427 CloseFile(fd);
428 }
429
430 void writeData4(const char *folder, const char *name, const char *id,
431 const char *timestr, double utc, double v1, double v2, double v3, double v4)
432 {
433 char line[1024];
434 char filename[FILENAME_MAX];
435
436 // construct filename in the form folder\\id-name.txt
437 snprintf(filename, sizeof(filename), "%s\\%s-%s.txt", folder, id, name);
438
439 int fd = OpenFile(filename, VAL_WRITE_ONLY, VAL_APPEND, VAL_ASCII);
440 Fmt(line, "%s\t%f[p3]\t%f[p3]\t%f[p3]\t%f[p3]\t%f[p3]", timestr, utc, v1, v2, v3, v4);
441 WriteLine(fd, line, -1);
442 CloseFile(fd);
443 }
444
445 void CVICALLBACK DataAvailableCB (CmtTSQHandle queueHandle, unsigned int event, 477 void CVICALLBACK DataAvailableCB (CmtTSQHandle queueHandle, unsigned int event,
446 int value, void *callbackData); 478 int value, void *callbackData);
447 479
448 480
449 int main (int argc, char *argv[]) 481 int main (int argc, char *argv[])
487 GetCtrlVal(MainPanel, PANEL_MATHSTRING1, expr); 519 GetCtrlVal(MainPanel, PANEL_MATHSTRING1, expr);
488 mupSetExpr(MathParser1, expr); 520 mupSetExpr(MathParser1, expr);
489 521
490 MathParser2 = initMathParser(); 522 MathParser2 = initMathParser();
491 mupDefineVar(MathParser2, "Math1", &Math1); 523 mupDefineVar(MathParser2, "Math1", &Math1);
492 mupDefineVar(MathParser2, "DDS", &(DDS4xAD9912.Frequency1)); 524 mupDefineVar(MathParser2, "DDS", &(DDS4xAD9912.frequency[0]));
493 GetCtrlVal(MainPanel, PANEL_MATHSTRING2, expr); 525 GetCtrlVal(MainPanel, PANEL_MATHSTRING2, expr);
494 mupSetExpr(MathParser2, expr); 526 mupSetExpr(MathParser2, expr);
495 527
496 MathParser3 = initMathParser(); 528 MathParser3 = initMathParser();
497 mupDefineVar(MathParser3, "Math1", &Math1); 529 mupDefineVar(MathParser3, "Math1", &Math1);
498 mupDefineVar(MathParser3, "Math2", &Math2); 530 mupDefineVar(MathParser3, "Math2", &Math2);
499 mupDefineVar(MathParser3, "DDS", &(DDS4xAD9912.Frequency2)); 531 mupDefineVar(MathParser3, "DDS", &(DDS4xAD9912.frequency[1]));
500 GetCtrlVal(MainPanel, PANEL_MATHSTRING3, expr); 532 GetCtrlVal(MainPanel, PANEL_MATHSTRING3, expr);
501 mupSetExpr(MathParser3, expr); 533 mupSetExpr(MathParser3, expr);
502 534
503 MathParser4 = initMathParser(); 535 MathParser4 = initMathParser();
504 mupDefineVar(MathParser4, "Math1", &Math1); 536 mupDefineVar(MathParser4, "Math1", &Math1);
701 733
702 734
703 void CVICALLBACK DataAvailableCB (CmtTSQHandle queueHandle, unsigned int ev, 735 void CVICALLBACK DataAvailableCB (CmtTSQHandle queueHandle, unsigned int ev,
704 int value, void *callbackData) 736 int value, void *callbackData)
705 { 737 {
706 struct event event;
707 int read; 738 int read;
708 739
709 switch (ev) { 740 switch (ev) {
710 case EVENT_TSQ_ITEMS_IN_QUEUE: 741 case EVENT_TSQ_ITEMS_IN_QUEUE:
711 // read data from the data queue 742 // read data from the data queue
942 SetCtrlVal(MainPanel, PANEL_DDS1, fDDS1); 973 SetCtrlVal(MainPanel, PANEL_DDS1, fDDS1);
943 DDS4xAD9912_SetFrequency(&DDS4xAD9912, 1, fDDS1); 974 DDS4xAD9912_SetFrequency(&DDS4xAD9912, 1, fDDS1);
944 975
945 // adjust DDS3 to keep beatnote within the bandpass filter. prediction 976 // adjust DDS3 to keep beatnote within the bandpass filter. prediction
946 double fDDS3 = FrequencyDDS3Init - DeltakHz_2*1000*(-Sign1/Sign2)*Ndiv*(Nu2)/(Nu1) - Beatslope_2*(utc-t1_2); 977 double fDDS3 = FrequencyDDS3Init - DeltakHz_2*1000*(-Sign1/Sign2)*Ndiv*(Nu2)/(Nu1) - Beatslope_2*(utc-t1_2);
947 DeltaDDS3 = fDDS3 - DDS4xAD9912.Frequency3; 978 DeltaDDS3 = fDDS3 - DDS4xAD9912_GetFrequency(&DDS4xAD9912, 3);
948 printf("deltaDDS3 = %g\n", DeltaDDS3); 979 printf("deltaDDS3 = %g\n", DeltaDDS3);
949 SetCtrlVal(MainPanel, PANEL_DDS3, fDDS3); 980 SetCtrlVal(MainPanel, PANEL_DDS3, fDDS3);
950 DDS4xAD9912_SetFrequency(&DDS4xAD9912, 3, fDDS3); 981 DDS4xAD9912_SetFrequency(&DDS4xAD9912, 3, fDDS3);
951 982
952 // allow counter to settle 983 // allow counter to settle
964 if (settling > 0) { 995 if (settling > 0) {
965 settling--; 996 settling--;
966 break; 997 break;
967 } 998 }
968 999
969 double fDDS2 = DDS4xAD9912.Frequency2 + 275000 - Ch4; 1000 double fDDS2 = DDS4xAD9912_GetFrequency(&DDS4xAD9912, 2) + 275000 - Ch4;
970 SetCtrlVal(MainPanel, PANEL_DDS2, fDDS2); 1001 SetCtrlVal(MainPanel, PANEL_DDS2, fDDS2);
971 DDS4xAD9912_SetFrequency(&DDS4xAD9912, 2, fDDS2); 1002 DDS4xAD9912_SetFrequency(&DDS4xAD9912, 2, fDDS2);
972 1003
973 double fDDS3 = DDS4xAD9912.Frequency3 + 10000 - Ch2; 1004 double fDDS3 = DDS4xAD9912_GetFrequency(&DDS4xAD9912, 3) + 10000 - Ch2;
974 DeltaDDS3 = DeltaDDS3 + 10000 - Ch2; 1005 DeltaDDS3 = DeltaDDS3 + 10000 - Ch2;
975 SetCtrlVal(MainPanel, PANEL_DDS3, fDDS3); 1006 SetCtrlVal(MainPanel, PANEL_DDS3, fDDS3);
976 DDS4xAD9912_SetFrequency(&DDS4xAD9912, 3, fDDS3); 1007 DDS4xAD9912_SetFrequency(&DDS4xAD9912, 3, fDDS3);
977 1008
978 // allow counter to settle 1009 // allow counter to settle
1008 SetCtrlVal(MainPanel, PANEL_DDS1, fDDS1); 1039 SetCtrlVal(MainPanel, PANEL_DDS1, fDDS1);
1009 DDS4xAD9912_SetFrequency(&DDS4xAD9912, 1, fDDS1); 1040 DDS4xAD9912_SetFrequency(&DDS4xAD9912, 1, fDDS1);
1010 1041
1011 // adjust DDS3 to keep beatnote within the bandpass filter. prediction 1042 // adjust DDS3 to keep beatnote within the bandpass filter. prediction
1012 double fDDS3 = FrequencyDDS3Init + DeltakHz_2*1000*(-Sign1/Sign2)*Ndiv*(Nu2)/(Nu1); 1043 double fDDS3 = FrequencyDDS3Init + DeltakHz_2*1000*(-Sign1/Sign2)*Ndiv*(Nu2)/(Nu1);
1013 DeltaDDS3 = fDDS3 - DDS4xAD9912.Frequency3; 1044 DeltaDDS3 = fDDS3 - DDS4xAD9912_GetFrequency(&DDS4xAD9912, 3);
1014 SetCtrlVal(MainPanel, PANEL_DDS3, fDDS3); 1045 SetCtrlVal(MainPanel, PANEL_DDS3, fDDS3);
1015 DDS4xAD9912_SetFrequency(&DDS4xAD9912, 3, fDDS3); 1046 DDS4xAD9912_SetFrequency(&DDS4xAD9912, 3, fDDS3);
1016 1047
1017 // allow counter to settle 1048 // allow counter to settle
1018 settling = 3; 1049 settling = 3;
1086 stat_zero(&stat_math1); 1117 stat_zero(&stat_math1);
1087 f_rep_plus = f_rep_minus = 0.0; 1118 f_rep_plus = f_rep_minus = 0.0;
1088 f_beat_Sr_plus = f_beat_Sr_minus = 0.0; 1119 f_beat_Sr_plus = f_beat_Sr_minus = 0.0;
1089 1120
1090 // record current DDS frequencies 1121 // record current DDS frequencies
1091 FrequencyDDSBesInit = DDS4xAD9912.Frequency2; 1122 FrequencyDDSBesInit = DDS4xAD9912_GetFrequency(&DDS4xAD9912, 2);
1092 FrequencyDDS3Init = DDS4xAD9912.Frequency3; 1123 FrequencyDDS3Init = DDS4xAD9912_GetFrequency(&DDS4xAD9912, 3);
1093 1124
1094 // next step 1125 // next step
1095 Measuring_3 += 1; 1126 Measuring_3 += 1;
1096 break; 1127 break;
1097 1128
1135 settling--; 1166 settling--;
1136 break; 1167 break;
1137 } 1168 }
1138 1169
1139 // adjust DDS frequency to keep 55 MHz tracker oscillator locked 1170 // adjust DDS frequency to keep 55 MHz tracker oscillator locked
1140 double fDDS2 = DDS4xAD9912.Frequency2 + 275000 - Ch4; 1171 double fDDS2 = DDS4xAD9912_GetFrequency(&DDS4xAD9912, 2) + 275000 - Ch4;
1141 SetCtrlVal(MainPanel, PANEL_DDS2, fDDS2); 1172 SetCtrlVal(MainPanel, PANEL_DDS2, fDDS2);
1142 DDS4xAD9912_SetFrequency(&DDS4xAD9912, 2, fDDS2); 1173 DDS4xAD9912_SetFrequency(&DDS4xAD9912, 2, fDDS2);
1143 1174
1144 // allow counter to settle 1175 // allow counter to settle
1145 settling = 3; 1176 settling = 3;
1412 1443
1413 // run id derived from current local date in the form YYMMDD 1444 // run id derived from current local date in the form YYMMDD
1414 char id[7]; 1445 char id[7];
1415 strftime(id, sizeof(id), "%y%m%d", ltime); 1446 strftime(id, sizeof(id), "%y%m%d", ltime);
1416 1447
1417 int save; 1448 // write datafiles
1418 1449 for (struct datafile *d = datafiles; d->data; d++)
1419 // write counter data to disk 1450 datafile_append(d, id, timestr);
1420 GetCtrlVal(MainPanel, PANEL_SAVE_RAW_DATA, &save);
1421 if (save) {
1422 writeData4(DATAFOLDER, "Raw", id, timestr, utc, Ch1, Ch2, Ch3, Ch4);
1423 }
1424
1425 // write Lo frequency (Math2) to disk
1426 GetCtrlVal(MainPanel, PANEL_CHECKBOX_MATH2SAVE, &save);
1427 if (save) {
1428 writeData(DATAFOLDER, "Lo", id, timestr, utc, Math2);
1429 }
1430
1431 // write Hg frequency (Math3) to disk
1432 GetCtrlVal(MainPanel, PANEL_CHECKBOX_MATH3SAVE, &save);
1433 if (save) {
1434 writeData(DATAFOLDER, "Hg", id, timestr, utc, Math3);
1435 writeData("C:\\Femto\\Results", "Hg", id, timestr, utc, Math3);
1436 }
1437
1438 // write Sr frequency (Math4) to disk
1439 GetCtrlVal(MainPanel, PANEL_CHECKBOX_MATH4SAVE, &save);
1440 if (save) {
1441 writeData(DATAFOLDER, "Sr", id, timestr, utc, Math4);
1442 }
1443 1451
1444 // send Sr frequency (Math4) to Sr data logger 1452 // send Sr frequency (Math4) to Sr data logger
1445 GetCtrlVal(MainPanel, PANEL_SR_LOGGER, &save); 1453 if (Sr_datalogger_enabled)
1446 if (save) { 1454 Sr_datalogger_send("FEMTO2", utc, Math4);
1447 sendLogger("FEMTO2", utc, Math4);
1448 }
1449
1450
1451 // write ExtraMath (Math5) to disk
1452 GetCtrlVal(MainPanel, PANEL_CHECKBOX_MATH5SAVE, &save);
1453 if (save) {
1454 writeData(DATAFOLDER, "Ex", id, timestr, utc, Math5);
1455 }
1456
1457 // write DDS freqs to disk
1458 GetCtrlVal(MainPanel, PANEL_SAVE_DDS_FREQS, &save);
1459 if (save) {
1460 writeData4(DATAFOLDER, "DDS", id, timestr, utc,
1461 DDS4xAD9912.Frequency1, DDS4xAD9912.Frequency2,
1462 DDS4xAD9912.Frequency3, DDS4xAD9912.Frequency4);
1463 }
1464 1455
1465 } 1456 }
1466 break; 1457 break;
1467 } 1458 }
1468 } 1459 }
2234 break; 2225 break;
2235 } 2226 }
2236 return 0; 2227 return 0;
2237 } 2228 }
2238 2229
2230 int CVICALLBACK CB_SaveData (int panel, int control, int event,
2231 void *callbackData, int eventData1, int eventData2)
2232 {
2233 switch (event)
2234 {
2235 case EVENT_COMMIT:
2236 for (struct datafile *d = datafiles; d->data; d++) {
2237 if (d->control == control)
2238 GetCtrlVal(panel, control, &(d->write));
2239 }
2240 break;
2241 }
2242 return 0;
2243 }
2244
2245
2239 int CVICALLBACK CB_RecenterInterval (int panel, int control, int event, 2246 int CVICALLBACK CB_RecenterInterval (int panel, int control, int event,
2240 void *callbackData, int eventData1, int eventData2) 2247 void *callbackData, int eventData1, int eventData2)
2241 { 2248 {
2242 switch (event) 2249 switch (event)
2243 { 2250 {