comparison FXAnalyse.c @ 144:be87c8e78266

Code cleanup
author Daniele Nicolodi <daniele.nicolodi@obspm.fr>
date Wed, 22 Jan 2014 14:45:24 +0100
parents 09a4548e1436
children 75aad9969f87
comparison
equal deleted inserted replaced
143:09a4548e1436 144:be87c8e78266
5 #include <userint.h> 5 #include <userint.h>
6 #include <formatio.h> 6 #include <formatio.h>
7 #include <string.h> 7 #include <string.h>
8 #include <future.h> 8 #include <future.h>
9 9
10 #include "YLCStuff.h"
11 #include "FXAnalyse.h" 10 #include "FXAnalyse.h"
12 #include "Plot.h" 11 #include "Plot.h"
13 #include "Allan.h" 12 #include "Allan.h"
14 #include "DDS4xAD9912.h" 13 #include "DDS4xAD9912.h"
15 #include "DDS_Fox.h" 14 #include "DDS_Fox.h"
16 #include "muParserDLL.h" 15 #include "muParserDLL.h"
16 #include "utils.h"
17 #include "stat.h" 17 #include "stat.h"
18 #include "data-provider.h"
18 19
19 #define SR_LOGGER_IP "145.238.204.91" 20 #define SR_LOGGER_IP "145.238.204.91"
20 #define DATAFOLDER "Z:\\Measures-2013" 21 #define DATAFOLDER "Z:\\Measures-2013"
21 22
22 #define FREP_STEP_SIZE 50000.0 23 #define FREP_STEP_SIZE 50000.0
23
24 // number of channels read
25 #define NCHAN 4
26
27 // data acquisition event
28 struct event {
29 struct timeval time;
30 double data[NCHAN];
31 };
32 24
33 // data acquisition status 25 // data acquisition status
34 int acquiring; 26 int acquiring;
35 // data queue 27 // data queue
36 CmtTSQHandle dataQueue; 28 CmtTSQHandle dataQueue;
37 // data provider thread 29 // data provider thread
38 CmtThreadFunctionID dataProviderThread; 30 CmtThreadFunctionID dataProviderThread;
39 31
40 // data providers
41 int CVICALLBACK FakeDataProvider (void *functionData);
42 int CVICALLBACK FileDataProvider (void *functionData);
43 int CVICALLBACK KKDataProvider (void *functionData);
44
45
46 // select which data provider to use 32 // select which data provider to use
33 #ifdef NDEBUG
47 #define DataProvider KKDataProvider 34 #define DataProvider KKDataProvider
35 #else
36 #define DataProvider FakeDataProvider
37 #endif
48 38
49 struct event event; 39 struct event event;
50 double utc; 40 double utc;
51 double Ch1, Ch2, Ch3, Ch4; 41 double Ch1, Ch2, Ch3, Ch4;
52 double Math1, Math2, Math3, Math4, Math5; 42 double Math1, Math2, Math3, Math4, Math5;
53 double N1, N2, N3; 43 double N1, N2, N3;
54
55 44
56 // panels 45 // panels
57 static int MainPanel; 46 static int MainPanel;
58 static int CalcNPanel; 47 static int CalcNPanel;
59 static int EstimateN3Panel; 48 static int EstimateN3Panel;
221 210
222 double Frequencystep1=10000.0, tbegin1=0.0, Frepbefore1=0.0, Frequency1=0.0; 211 double Frequencystep1=10000.0, tbegin1=0.0, Frepbefore1=0.0, Frequency1=0.0;
223 double Frequencystep2=10.0, tbegin2=0.0, Frepbefore2=0.0, Ch2before=0.0, Frequency2=0.0; 212 double Frequencystep2=10.0, tbegin2=0.0, Frepbefore2=0.0, Ch2before=0.0, Frequency2=0.0;
224 double Frequencystep3=100000.0, tbegin3=0.0, Frepbefore3=0.0, Frequency3=0.0; 213 double Frequencystep3=100000.0, tbegin3=0.0, Frepbefore3=0.0, Frequency3=0.0;
225 214
226 volatile bool Getsign1=FALSE,Getsign2=FALSE,Getsign3=FALSE; 215 int Getsign1=FALSE,Getsign2=FALSE,Getsign3=FALSE;
227 double Sign1=1.0, Sign2=1.0, Sign3=0.0; 216 double Sign1=1.0, Sign2=1.0, Sign3=0.0;
228 217
229 218
230 struct stat stat_math1, stat_ch2, stat_ch3, stat_ch4, freq; 219 struct stat stat_math1, stat_ch2, stat_ch3, stat_ch4, freq;
231 struct rollmean rollmean_ch1, rollmean_ch2, rollmean_ch3, rollmean_ch4; 220 struct rollmean rollmean_ch1, rollmean_ch2, rollmean_ch3, rollmean_ch4;
267 256
268 257
269 // recenter 258 // recenter
270 struct recenter { 259 struct recenter {
271 int enabled; // recenter enabled 260 int enabled; // recenter enabled
272 int Lo; // recenter microwave beatnote 261 int lo; // recenter microwave beatnote
273 int Sr; // recenter Sr beatnote 262 int sr; // recenter Sr beatnote
274 int Hg; // recenter Hg beatnote 263 int hg; // recenter Hg beatnote
275 double interval; // interval 264 double interval; // interval
276 double t0; // beginning of current interval 265 double t0; // beginning of current interval
277 }; 266 };
278 267
279 struct recenter recenter = { 268 struct recenter recenter = {
280 .enabled = FALSE, 269 .enabled = FALSE,
281 .Lo = FALSE, 270 .lo = FALSE,
282 .Sr = FALSE, 271 .sr = FALSE,
283 .Hg = FALSE, 272 .hg = FALSE,
284 .interval = 1800.0, 273 .interval = 1800.0,
285 .t0 = 0.0 274 .t0 = 0.0
286 }; 275 };
287 276
288 277
337 326
338 static inline void datafile_append(struct datafile *d, char *id, char *timestr) 327 static inline void datafile_append(struct datafile *d, char *id, char *timestr)
339 { 328 {
340 if (d->write) 329 if (d->write)
341 write_data(DATAFOLDER, d->name, id, timestr, utc, d->data, d->nchan); 330 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)
346 {
347 // compute how many separators we need
348 #pragma DisableFunctionRuntimeChecking log10
349 int nsep = log10(fabs(val));
350 nsep = (nsep > 0 ? nsep / 3 : 0);
351 // format value
352 int len = snprintf(buffer, size, fmt, val);
353 char *src = buffer + len;
354 char *dst = src + nsep;
355 // copy till decimal separator
356 while (*src != '.')
357 *dst-- = *src--;
358 // the next char is the decimal separator
359 int n = -1;
360 // copy till src and dst point to the same location
361 while (src != dst) {
362 *dst-- = *src--;
363 // insert separator
364 if (isdigit(*src) && (++n) && ((n % 3) == 0))
365 *dst-- = ' ';
366 }
367 return buffer;
368 }
369
370
371 #define MIN(x, y) (x) < (y) ? (x) : (y)
372
373 // MJD functiom used by the Sr programs
374 static inline double utc2mjd(double utc)
375 {
376 return 15020.0 + utc / 86400.0;
377 } 331 }
378 332
379 333
380 void logmsg(const char *frmt, ...) 334 void logmsg(const char *frmt, ...)
381 { 335 {
667 621
668 } 622 }
669 return 0; 623 return 0;
670 } 624 }
671 625
672
673 int CVICALLBACK CB_OnStart (int panel, int control, int event, 626 int CVICALLBACK CB_OnStart (int panel, int control, int event,
674 void *callbackData, int eventData1, int eventData2) 627 void *callbackData, int eventData1, int eventData2)
675 { 628 {
676 switch (event) 629 switch (event)
677 { 630 {
715 668
716 break; 669 break;
717 } 670 }
718 return 0; 671 return 0;
719 } 672 }
720
721
722 void CVICALLBACK MessageCB (void *msg)
723 {
724 if (msg != NULL)
725 logmsg(msg);
726 }
727
728 673
729 void CVICALLBACK DataAvailableCB (CmtTSQHandle queueHandle, unsigned int ev, 674 void CVICALLBACK DataAvailableCB (CmtTSQHandle queueHandle, unsigned int ev,
730 int value, void *callbackData) 675 int value, void *callbackData)
731 { 676 {
732 int read; 677 int read;
1381 rollmean_accumulate(&rollmean_ch3, Ch3); 1326 rollmean_accumulate(&rollmean_ch3, Ch3);
1382 rollmean_accumulate(&rollmean_ch4, Ch4); 1327 rollmean_accumulate(&rollmean_ch4, Ch4);
1383 1328
1384 if ((utc - recenter.t0) > recenter.interval) { 1329 if ((utc - recenter.t0) > recenter.interval) {
1385 1330
1386 if (recenter.Lo) { 1331 if (recenter.lo) {
1387 // adjust DDS2 frequency to keep Ch4 reading at 275 kHz 1332 // adjust DDS2 frequency to keep Ch4 reading at 275 kHz
1388 double freq = DDS4xAD9912_GetFrequency(&DDS4xAD9912, 2); 1333 double freq = DDS4xAD9912_GetFrequency(&DDS4xAD9912, 2);
1389 double adj = 275000.0 - rollmean_ch4.mean; 1334 double adj = 275000.0 - rollmean_ch4.mean;
1390 freq = freq + adj; 1335 freq = freq + adj;
1391 SetCtrlVal(MainPanel, PANEL_DDS2, freq); 1336 SetCtrlVal(MainPanel, PANEL_DDS2, freq);
1392 DDS4xAD9912_SetFrequency(&DDS4xAD9912, 2, freq); 1337 DDS4xAD9912_SetFrequency(&DDS4xAD9912, 2, freq);
1393 logmsg("recenter ch4 to 275 kHz: DDS2 adjustment=%+3e", adj); 1338 logmsg("recenter ch4 to 275 kHz: DDS2 adjustment=%+3e", adj);
1394 } 1339 }
1395 1340
1396 if (recenter.Hg) { 1341 if (recenter.hg) {
1397 // adjust DDS3 frequency to keep Ch2 reading at 10 kHz 1342 // adjust DDS3 frequency to keep Ch2 reading at 10 kHz
1398 double freq = DDS4xAD9912_GetFrequency(&DDS4xAD9912, 3); 1343 double freq = DDS4xAD9912_GetFrequency(&DDS4xAD9912, 3);
1399 double adj = 10000 - rollmean_ch2.mean; 1344 double adj = 10000 - rollmean_ch2.mean;
1400 freq = freq + adj; 1345 freq = freq + adj;
1401 SetCtrlVal(MainPanel, PANEL_DDS3, freq); 1346 SetCtrlVal(MainPanel, PANEL_DDS3, freq);
1402 DDS4xAD9912_SetFrequency(&DDS4xAD9912, 3, freq); 1347 DDS4xAD9912_SetFrequency(&DDS4xAD9912, 3, freq);
1403 logmsg("recenter Hg beatnote (ch2) to 10 kHz: DDS3 adjustment=%+3e", adj); 1348 logmsg("recenter Hg beatnote (ch2) to 10 kHz: DDS3 adjustment=%+3e", adj);
1404 } 1349 }
1405 1350
1406 if (recenter.Sr) { 1351 if (recenter.sr) {
1407 // adjust DDS3 frequency to keep Ch3 reading at 10 kHz 1352 // adjust DDS3 frequency to keep Ch3 reading at 10 kHz
1408 double freq = DDS4xAD9912_GetFrequency(&DDS4xAD9912, 3); 1353 double freq = DDS4xAD9912_GetFrequency(&DDS4xAD9912, 3);
1409 double adj = 10000 - rollmean_ch3.mean; 1354 double adj = 10000 - rollmean_ch3.mean;
1410 freq = freq + adj; 1355 freq = freq + adj;
1411 SetCtrlVal(MainPanel, PANEL_DDS3, freq); 1356 SetCtrlVal(MainPanel, PANEL_DDS3, freq);
1450 } 1395 }
1451 break; 1396 break;
1452 } 1397 }
1453 } 1398 }
1454 1399
1455
1456 int CVICALLBACK CB_OnFreqPlot (int panel, int control, int event, 1400 int CVICALLBACK CB_OnFreqPlot (int panel, int control, int event,
1457 void *callbackData, int eventData1, int eventData2) 1401 void *callbackData, int eventData1, int eventData2)
1458 { 1402 {
1459 switch (event) 1403 switch (event)
1460 { 1404 {
1465 } 1409 }
1466 break; 1410 break;
1467 } 1411 }
1468 return 0; 1412 return 0;
1469 } 1413 }
1470
1471 1414
1472 int CVICALLBACK CB_OnAllanPlot (int panel, int control, int event, 1415 int CVICALLBACK CB_OnAllanPlot (int panel, int control, int event,
1473 void *callbackData, int eventData1, int eventData2) 1416 void *callbackData, int eventData1, int eventData2)
1474 { 1417 {
1475 switch (event) 1418 switch (event)
1635 break; 1578 break;
1636 } 1579 }
1637 return 0; 1580 return 0;
1638 } 1581 }
1639 1582
1640
1641 int CVICALLBACK CB_OnMinus10k (int panel, int control, int event, 1583 int CVICALLBACK CB_OnMinus10k (int panel, int control, int event,
1642 void *callbackData, int eventData1, int eventData2) 1584 void *callbackData, int eventData1, int eventData2)
1643 { 1585 {
1644 double Frequency; 1586 double Frequency;
1645 1587
1676 break; 1618 break;
1677 } 1619 }
1678 return 0; 1620 return 0;
1679 } 1621 }
1680 1622
1681
1682 int CVICALLBACK CB_OnAcceptN (int panel, int control, int event, 1623 int CVICALLBACK CB_OnAcceptN (int panel, int control, int event,
1683 void *callbackData, int eventData1, int eventData2) 1624 void *callbackData, int eventData1, int eventData2)
1684 { 1625 {
1685 switch (event) { 1626 switch (event) {
1686 case EVENT_COMMIT: 1627 case EVENT_COMMIT:
1700 } 1641 }
1701 break; 1642 break;
1702 } 1643 }
1703 return 0; 1644 return 0;
1704 } 1645 }
1705
1706 1646
1707 int CVICALLBACK CB_OnNCalculus (int panel, int control, int event, 1647 int CVICALLBACK CB_OnNCalculus (int panel, int control, int event,
1708 void *callbackData, int eventData1, int eventData2) 1648 void *callbackData, int eventData1, int eventData2)
1709 { 1649 {
1710 1650
1756 break; 1696 break;
1757 } 1697 }
1758 return 0; 1698 return 0;
1759 } 1699 }
1760 1700
1761
1762 int CVICALLBACK CB_OnStartNCalculus (int panel, int control, int event, 1701 int CVICALLBACK CB_OnStartNCalculus (int panel, int control, int event,
1763 void *callbackData, int eventData1, int eventData2) 1702 void *callbackData, int eventData1, int eventData2)
1764 { 1703 {
1765 switch (event) { 1704 switch (event) {
1766 case EVENT_COMMIT: 1705 case EVENT_COMMIT:
1786 } 1725 }
1787 break; 1726 break;
1788 } 1727 }
1789 return 0; 1728 return 0;
1790 } 1729 }
1791
1792
1793 1730
1794 int CVICALLBACK CB_OnNStop (int panel, int control, int event, 1731 int CVICALLBACK CB_OnNStop (int panel, int control, int event,
1795 void *callbackData, int eventData1, int eventData2) 1732 void *callbackData, int eventData1, int eventData2)
1796 { 1733 {
1797 switch (event) { 1734 switch (event) {
1826 } 1763 }
1827 break; 1764 break;
1828 } 1765 }
1829 return 0; 1766 return 0;
1830 } 1767 }
1831
1832 1768
1833 int CVICALLBACK CB_OnFindSign (int panel, int control, int event, 1769 int CVICALLBACK CB_OnFindSign (int panel, int control, int event,
1834 void *callbackData, int eventData1, int eventData2) 1770 void *callbackData, int eventData1, int eventData2)
1835 { 1771 {
1836 1772
1868 break; 1804 break;
1869 } 1805 }
1870 return 0; 1806 return 0;
1871 } 1807 }
1872 1808
1873
1874 int CVICALLBACK CB_OnFind275K (int panel, int control, int event, 1809 int CVICALLBACK CB_OnFind275K (int panel, int control, int event,
1875 void *callbackData, int eventData1, int eventData2) 1810 void *callbackData, int eventData1, int eventData2)
1876 { 1811 {
1877 double frequency; 1812 double frequency;
1878 1813
1898 break; 1833 break;
1899 } 1834 }
1900 return 0; 1835 return 0;
1901 } 1836 }
1902 1837
1903 1838 int CVICALLBACK CB_OnChangeNdiv (int panel, int control, int event,
1904 int CVICALLBACK CB_OnChangeNdiv (int panel, int control, int event,
1905 void *callbackData, int eventData1, int eventData2) 1839 void *callbackData, int eventData1, int eventData2)
1906 { 1840 {
1907 switch (event) 1841 switch (event)
1908 { 1842 {
1909 case EVENT_COMMIT: 1843 case EVENT_COMMIT:
1914 break; 1848 break;
1915 } 1849 }
1916 return 0; 1850 return 0;
1917 } 1851 }
1918 1852
1919 1853 int CVICALLBACK CB_MeasureSlope (int panel, int control, int event,
1920 int CVICALLBACK CB_MeasureSlope (int panel, int control, int event,
1921 void *callbackData, int eventData1, int eventData2) 1854 void *callbackData, int eventData1, int eventData2)
1922 { 1855 {
1923 switch (event) 1856 switch (event)
1924 { 1857 {
1925 case EVENT_COMMIT: 1858 case EVENT_COMMIT:
1944 break; 1877 break;
1945 } 1878 }
1946 return 0; 1879 return 0;
1947 } 1880 }
1948 1881
1949
1950 int CVICALLBACK CB_OnResetSlope (int panel, int control, int event, 1882 int CVICALLBACK CB_OnResetSlope (int panel, int control, int event,
1951 void *callbackData, int eventData1, int eventData2) 1883 void *callbackData, int eventData1, int eventData2)
1952 { 1884 {
1953 1885
1954 switch (event) 1886 switch (event)
1961 break; 1893 break;
1962 } 1894 }
1963 return 0; 1895 return 0;
1964 } 1896 }
1965 1897
1966
1967 int CVICALLBACK CB_ChangeSlopeTime (int panel, int control, int event, 1898 int CVICALLBACK CB_ChangeSlopeTime (int panel, int control, int event,
1968 void *callbackData, int eventData1, int eventData2) 1899 void *callbackData, int eventData1, int eventData2)
1969 { 1900 {
1970 1901
1971 switch (event) 1902 switch (event)
1974 GetCtrlVal(MainPanel, PANEL_SLOPETIME, &dedrift.interval); 1905 GetCtrlVal(MainPanel, PANEL_SLOPETIME, &dedrift.interval);
1975 break; 1906 break;
1976 } 1907 }
1977 return 0; 1908 return 0;
1978 } 1909 }
1979
1980 1910
1981 int CVICALLBACK CB_OnCROX (int panel, int control, int event, 1911 int CVICALLBACK CB_OnCROX (int panel, int control, int event,
1982 void *callbackData, int eventData1, int eventData2) 1912 void *callbackData, int eventData1, int eventData2)
1983 { 1913 {
1984 switch (event) 1914 switch (event)
1996 } 1926 }
1997 break; 1927 break;
1998 } 1928 }
1999 return 0; 1929 return 0;
2000 } 1930 }
2001
2002
2003 1931
2004 int CVICALLBACK CB_RecenterEnable (int panel, int control, int event, 1932 int CVICALLBACK CB_RecenterEnable (int panel, int control, int event,
2005 void *callbackData, int eventData1, int eventData2) 1933 void *callbackData, int eventData1, int eventData2)
2006 { 1934 {
2007 switch (event) 1935 switch (event)
2015 break; 1943 break;
2016 } 1944 }
2017 return 0; 1945 return 0;
2018 } 1946 }
2019 1947
2020
2021 int CVICALLBACK CB_OnStopSlopeCancellingOnUnlocked (int panel, int control, int event, 1948 int CVICALLBACK CB_OnStopSlopeCancellingOnUnlocked (int panel, int control, int event,
2022 void *callbackData, int eventData1, int eventData2) 1949 void *callbackData, int eventData1, int eventData2)
2023 { 1950 {
2024 int value; 1951 int value;
2025 switch (event) 1952 switch (event)
2041 GetCtrlVal(MainPanel, PANEL_SLOPE_REFERENCE, &dedrift.reference); 1968 GetCtrlVal(MainPanel, PANEL_SLOPE_REFERENCE, &dedrift.reference);
2042 break; 1969 break;
2043 } 1970 }
2044 return 0; 1971 return 0;
2045 } 1972 }
2046
2047 1973
2048 int CVICALLBACK CB_OnEstimateN (int panel, int control, int event, 1974 int CVICALLBACK CB_OnEstimateN (int panel, int control, int event,
2049 void *callbackData, int eventData1, int eventData2) 1975 void *callbackData, int eventData1, int eventData2)
2050 { 1976 {
2051 int visible; 1977 int visible;
2256 { 2182 {
2257 case EVENT_COMMIT: 2183 case EVENT_COMMIT:
2258 switch (control) 2184 switch (control)
2259 { 2185 {
2260 case PANEL_RECENTER_LO: 2186 case PANEL_RECENTER_LO:
2261 GetCtrlVal(panel, control, &recenter.Lo); 2187 GetCtrlVal(panel, control, &recenter.lo);
2262 break; 2188 break;
2263 case PANEL_RECENTER_HG: 2189 case PANEL_RECENTER_HG:
2264 GetCtrlVal(panel, control, &recenter.Hg); 2190 GetCtrlVal(panel, control, &recenter.hg);
2265 break; 2191 break;
2266 case PANEL_RECENTER_SR: 2192 case PANEL_RECENTER_SR:
2267 GetCtrlVal(panel, control, &recenter.Sr); 2193 GetCtrlVal(panel, control, &recenter.sr);
2268 break; 2194 break;
2269 } 2195 }
2196 break;
2197 }
2198 return 0;
2199 }
2200
2201 int CVICALLBACK CB_SrDatalogger (int panel, int control, int event,
2202 void *callbackData, int eventData1, int eventData2)
2203 {
2204 switch (event)
2205 {
2206 case EVENT_COMMIT:
2207 GetCtrlVal(panel, control, &Sr_datalogger_enabled);
2270 break; 2208 break;
2271 } 2209 }
2272 return 0; 2210 return 0;
2273 } 2211 }
2274 int CVICALLBACK CB_DedriftDDSFreq (int panel, int control, int event, 2212 int CVICALLBACK CB_DedriftDDSFreq (int panel, int control, int event,