comparison FXAnalyse.c @ 232:52f882f39c16

Implement correction proportional to frequency in dedrift code Reorganize code and options handling
author Daniele Nicolodi <daniele.nicolodi@obspm.fr>
date Mon, 27 Oct 2014 18:08:17 +0100
parents c10236b5a3e9
children b0b4b2aea43a
comparison
equal deleted inserted replaced
231:c10236b5a3e9 232:52f882f39c16
310 struct beatsign beatsign = { 310 struct beatsign beatsign = {
311 .measure = 0, 311 .measure = 0,
312 }; 312 };
313 313
314 314
315 struct stat stat_math1, stat_ch2, stat_ch3, freq; 315 struct stat stat_math1, stat_ch2, stat_ch3;
316 struct rollmean rollmean_ch1, rollmean_ch2, rollmean_ch3, rollmean_ch4; 316 struct rollmean rollmean_ch1, rollmean_ch2, rollmean_ch3, rollmean_ch4;
317 317
318 318
319 // dedrift 319 // dedrift
320 enum { 320 enum {
322 DEDRIFT_REFERENCE_HG = 1, 322 DEDRIFT_REFERENCE_HG = 1,
323 }; 323 };
324 324
325 struct dedrift { 325 struct dedrift {
326 int enabled; // dedrift enabled 326 int enabled; // dedrift enabled
327 int proportional; // enable proportional correction
327 int reference; // reference frequency 328 int reference; // reference frequency
328 int invert; // invert applied slope sign 329 int sign; // sign of the correction
329 int doubleslope; // double applied slope 330 int x2; // double the applied correction
330 int keep_freq; // keep current frequency value when dedrift is disabled 331 int keep_freq; // keep current frequency value when dedrift is disabled
331 int keep_slope; // keep current slope value when dedrift is disabled 332 int keep_slope; // keep current slope value when dedrift is disabled
332 double freq0; // DDS center frequency 333 double f0; // target frequency
334 double fDDS; // DDS center frequency
333 double threshold; // maximum allowed frequency change 335 double threshold; // maximum allowed frequency change
334 double applied; // currently applied slope 336 double applied; // currently applied slope
335 double interval; // measurement duration 337 double interval; // measurement duration
336 double t0; // beginning of currrent measurement interval 338 double t0; // beginning of currrent measurement interval
339 struct stat stat; // frequency mean and slope
337 }; 340 };
338 341
339 struct dedrift dedrift = { 342 struct dedrift dedrift = {
340 .enabled = FALSE, 343 .enabled = FALSE,
344 .proportional = FALSE,
341 .reference = DEDRIFT_REFERENCE_MICROWAVE, 345 .reference = DEDRIFT_REFERENCE_MICROWAVE,
342 .invert = FALSE, 346 .sign = +1,
343 .doubleslope = FALSE, 347 .x2 = FALSE,
344 .keep_freq = TRUE, 348 .keep_freq = TRUE,
345 .keep_slope = TRUE, 349 .keep_slope = TRUE,
346 .freq0 = 70e6, 350 .f0 = 0.0,
351 .fDDS = 70e6,
347 .threshold = 100.0, 352 .threshold = 100.0,
348 .applied = 0.0, 353 .applied = 0.0,
349 .interval = 30.0, 354 .interval = 30.0,
350 .t0 = 0.0 355 .t0 = 0.0
351 }; 356 };
357
358
359 void dedrift_update(double f)
360 {
361 // stop dedrift if the comb is not locked
362 if ((dedrift.enabled) &&
363 (dedrift.threshold != 0.0) &&
364 (dedrift.stat.previous != 0.0) &&
365 (fabs(f - dedrift.stat.previous) > dedrift.threshold)) {
366
367 logmsg("dedrift stop: frequency jump detected");
368
369 if (! dedrift.keep_slope) {
370 dedrift.applied = 0.0;
371 ad9956_set_sweep_rate_w(&ad9956, dedrift.applied);
372 SetCtrlVal(MainPanel, PANEL_SLOPE_APPLIED, dedrift.applied);
373 }
374 if (! dedrift.keep_freq) {
375 ad9956_set_w(&ad9956, dedrift.fDDS, dedrift.applied);
376 }
377
378 stat_zero(&dedrift.stat);
379 SetCtrlVal(MainPanel, PANEL_SLOPE_MEASURED, dedrift.stat.slope);
380 dedrift.enabled = FALSE;
381 SetCtrlVal(MainPanel, PANEL_MEASURE_SLOPE, 0);
382 }
383
384 // dedrifting
385 if (dedrift.enabled) {
386
387 // update measurement
388 stat_accumulate(&dedrift.stat, f);
389 SetCtrlVal(MainPanel, PANEL_SLOPE_MEASURED, dedrift.stat.slope);
390
391 // update applied slope
392 if ((utc - dedrift.t0) > dedrift.interval) {
393
394 // target frequency
395 if (dedrift.f0 == 0.0)
396 dedrift.f0 = dedrift.stat.mean;
397
398 // compute correction
399 double dt = utc - dedrift.t0;
400 double corr = dedrift.stat.slope \
401 + dedrift.proportional * ((dedrift.stat.mean - dedrift.f0) / dt + 0.5 * dedrift.stat.slope);
402
403 // update
404 dedrift.applied += dedrift.sign * corr * (dedrift.x2 ? 2 : 1);
405 ad9956_set_sweep_rate_w(&ad9956, dedrift.applied);
406 SetCtrlVal(MainPanel, PANEL_SLOPE_APPLIED, dedrift.applied);
407 logmsg("dedrift update: correction=%+3e slope=%+3e", corr, dedrift.applied);
408
409 // start over
410 dedrift.t0 = utc;
411 stat_zero(&dedrift.stat);
412 }
413 }
414 }
352 415
353 416
354 // recenter 417 // recenter
355 struct recenter { 418 struct recenter {
356 int enabled; // recenter enabled 419 int enabled; // recenter enabled
540 603
541 // initialize AD9956 dedrift DDS 604 // initialize AD9956 dedrift DDS
542 rv = ad9956_init(&ad9956, host, clock); 605 rv = ad9956_init(&ad9956, host, clock);
543 if (rv) 606 if (rv)
544 logmessage(ERROR, "ad9956 init erorr=%d", -rv); 607 logmessage(ERROR, "ad9956 init erorr=%d", -rv);
545 ad9956_set_w(&ad9956, dedrift.freq0, dedrift.applied); 608 ad9956_set_w(&ad9956, dedrift.fDDS, dedrift.applied);
546 609
547 // ad9912 configuration parameters 610 // ad9912 configuration parameters
548 rv = Ini_GetStringIntoBuffer(configuration, "AD9912", "host", host, sizeof(host)); 611 rv = Ini_GetStringIntoBuffer(configuration, "AD9912", "host", host, sizeof(host));
549 if (! rv) 612 if (! rv)
550 return -1; 613 return -1;
1292 // beatnote right in those cases 1355 // beatnote right in those cases
1293 if (f_rep_sign != Sign1) 1356 if (f_rep_sign != Sign1)
1294 logmessage(ERROR, "merasured f_rep_sign does not agree with previous determination!"); 1357 logmessage(ERROR, "merasured f_rep_sign does not agree with previous determination!");
1295 } 1358 }
1296 1359
1297 // select reference 1360 // select dedrift reference
1298 double f = 0.0; 1361 double f = 0.0;
1299 switch (dedrift.reference) { 1362 switch (dedrift.reference) {
1300 case DEDRIFT_REFERENCE_MICROWAVE: 1363 case DEDRIFT_REFERENCE_MICROWAVE:
1301 f = Math2; 1364 f = Math2;
1302 break; 1365 break;
1303 case DEDRIFT_REFERENCE_HG: 1366 case DEDRIFT_REFERENCE_HG:
1304 f = Ch2 * 1062.5 / 1542.2; 1367 f = Ch2 * 1062.5 / 1542.2;
1305 break; 1368 break;
1306 } 1369 }
1307 1370
1308 // stop dedrift if the comb is not locked 1371 // dedrift
1309 if ((dedrift.enabled) && 1372 dedrift_update(f);
1310 (dedrift.threshold != 0.0) && 1373
1311 (freq.previous != 0.0) &&
1312 (fabs(f - freq.previous) > dedrift.threshold)) {
1313
1314 logmsg("dedrift stop: frequency jump detected");
1315
1316 if (! dedrift.keep_slope) {
1317 dedrift.applied = 0.0;
1318 ad9956_set_sweep_rate_w(&ad9956, dedrift.applied);
1319 SetCtrlVal(MainPanel, PANEL_SLOPE_APPLIED, dedrift.applied);
1320 }
1321 if (! dedrift.keep_freq) {
1322 ad9956_set_w(&ad9956, dedrift.freq0, dedrift.applied);
1323 }
1324
1325 stat_zero(&freq);
1326 SetCtrlVal(MainPanel, PANEL_SLOPE_MEASURED, freq.slope);
1327 dedrift.enabled = FALSE;
1328 SetCtrlVal(MainPanel, PANEL_MEASURE_SLOPE, 0);
1329 }
1330
1331 // dedrifting
1332 if (dedrift.enabled)
1333 {
1334 // update slope measurement
1335 stat_accumulate(&freq, f);
1336
1337 // update indicator
1338 SetCtrlVal(MainPanel, PANEL_SLOPE_MEASURED, freq.slope);
1339
1340 // update applied slope
1341 if ((utc - dedrift.t0) > dedrift.interval) {
1342
1343 if (dedrift.invert)
1344 dedrift.applied -= freq.slope;
1345 else
1346 dedrift.applied += freq.slope;
1347
1348 SetCtrlVal(MainPanel, PANEL_SLOPE_APPLIED, dedrift.applied);
1349
1350 if (dedrift.doubleslope)
1351 ad9956_set_sweep_rate_w(&ad9956, 2 * dedrift.applied);
1352 else
1353 ad9956_set_sweep_rate_w(&ad9956, dedrift.applied);
1354
1355 logmsg("dedrift update: adjustment=%+3e slope=%+3e", freq.slope, dedrift.applied);
1356
1357 stat_zero(&freq);
1358 dedrift.t0 = utc;
1359 }
1360 }
1361
1362 // recenter 1374 // recenter
1363 if (recenter.enabled) 1375 if (recenter.enabled)
1364 { 1376 {
1365 rollmean_accumulate(&rollmean_ch2, Ch2); 1377 rollmean_accumulate(&rollmean_ch2, Ch2);
1366 rollmean_accumulate(&rollmean_ch3, Ch3); 1378 rollmean_accumulate(&rollmean_ch3, Ch3);
1833 { 1845 {
1834 case EVENT_COMMIT: 1846 case EVENT_COMMIT:
1835 GetCtrlVal(panel, control, &dedrift.enabled); 1847 GetCtrlVal(panel, control, &dedrift.enabled);
1836 if (dedrift.enabled) { 1848 if (dedrift.enabled) {
1837 dedrift.t0 = utc; 1849 dedrift.t0 = utc;
1838 stat_zero(&freq); 1850 stat_zero(&dedrift.stat);
1839 logmsg("dedrift start"); 1851 logmsg("dedrift start");
1840 } else { 1852 } else {
1841 if (! dedrift.keep_slope) { 1853 if (! dedrift.keep_slope) {
1842 dedrift.applied = 0.0; 1854 dedrift.applied = 0.0;
1843 ad9956_set_sweep_rate_w(&ad9956, dedrift.applied); 1855 ad9956_set_sweep_rate_w(&ad9956, dedrift.applied);
1844 SetCtrlVal(MainPanel, PANEL_SLOPE_APPLIED, dedrift.applied); 1856 SetCtrlVal(MainPanel, PANEL_SLOPE_APPLIED, dedrift.applied);
1845 } 1857 }
1846 if (! dedrift.keep_freq) { 1858 if (! dedrift.keep_freq) {
1847 ad9956_set_w(&ad9956, dedrift.freq0, dedrift.applied); 1859 ad9956_set_w(&ad9956, dedrift.fDDS, dedrift.applied);
1848 } 1860 }
1849 stat_zero(&freq); 1861 stat_zero(&dedrift.stat);
1850 SetCtrlVal(panel, PANEL_SLOPE_MEASURED, freq.slope); 1862 SetCtrlVal(panel, PANEL_SLOPE_MEASURED, dedrift.stat.slope);
1851 logmsg("dedrift stop"); 1863 logmsg("dedrift stop");
1852 } 1864 }
1853 break; 1865 break;
1854 } 1866 }
1855 return 0; 1867 return 0;
1861 switch (event) 1873 switch (event)
1862 { 1874 {
1863 case EVENT_COMMIT: 1875 case EVENT_COMMIT:
1864 dedrift.applied = 0.0; 1876 dedrift.applied = 0.0;
1865 SetCtrlVal(panel, PANEL_SLOPE_APPLIED, dedrift.applied); 1877 SetCtrlVal(panel, PANEL_SLOPE_APPLIED, dedrift.applied);
1866 ad9956_set_w(&ad9956, dedrift.freq0, dedrift.applied); 1878 ad9956_set_w(&ad9956, dedrift.fDDS, dedrift.applied);
1867 logmsg("dedrift reset"); 1879 logmsg("dedrift reset");
1868 break; 1880 break;
1869 } 1881 }
1870 return 0; 1882 return 0;
1871 } 1883 }
1880 break; 1892 break;
1881 } 1893 }
1882 return 0; 1894 return 0;
1883 } 1895 }
1884 1896
1885 int CVICALLBACK CB_OnCROX (int panel, int control, int event, 1897 int CVICALLBACK CB_OnDedriftSettingsChange (int panel, int control, int event,
1886 void *callbackData, int eventData1, int eventData2) 1898 void *callbackData, int eventData1, int eventData2)
1887 { 1899 {
1888 switch (event) 1900 switch (event)
1889 { 1901 {
1890 case EVENT_COMMIT: 1902 case EVENT_COMMIT:
1891 switch (control) { 1903 switch (control)
1892 case PANEL_CHECKBOX_KEEP: 1904 {
1905 case PANEL_DEDRIFT_PROPORTIONAL:
1906 // include correction proportional to frequency
1907 GetCtrlVal(panel, control, &dedrift.proportional);
1908 break;
1909 case PANEL_DEDRIFT_DOUBLE_CORR:
1910 // double slope correction
1911 GetCtrlVal(panel, control, &dedrift.x2);
1912 break;
1913 case PANEL_DEDRIFT_KEEP_FREQ:
1893 // keep current dedrifting frequency when dedrifting is disabled 1914 // keep current dedrifting frequency when dedrifting is disabled
1894 GetCtrlVal(MainPanel, PANEL_CHECKBOX_KEEP, &dedrift.keep_freq); 1915 GetCtrlVal(panel, control, &dedrift.keep_freq);
1895 break; 1916 break;
1896 case PANEL_CHECKBOX_KEEPSLOPE: 1917 case PANEL_DEDRIFT_KEEP_SLOPE:
1897 // keep current dedrifting slope when dedrifting is disabled 1918 // keep current dedrifting slope when dedrifting is disabled
1898 GetCtrlVal(MainPanel, PANEL_CHECKBOX_KEEPSLOPE, &dedrift.keep_slope); 1919 GetCtrlVal(panel, control, &dedrift.keep_slope);
1899 break; 1920 break;
1900 } 1921 }
1901 break; 1922 break;
1902 } 1923 }
1903 return 0; 1924 return 0;
1924 { 1945 {
1925 int value; 1946 int value;
1926 switch (event) 1947 switch (event)
1927 { 1948 {
1928 case EVENT_COMMIT: 1949 case EVENT_COMMIT:
1929 GetCtrlVal(MainPanel, PANEL_CHECKBOX_STOPIFAUTODE, &value); 1950 GetCtrlVal(panel, control, &value);
1930 dedrift.threshold = value ? 100.0 : 0.0; 1951 dedrift.threshold = value ? 100.0 : 0.0;
1931 break; 1952 break;
1932 } 1953 }
1933 return 0; 1954 return 0;
1934 } 1955 }
1937 void *callbackData, int eventData1, int eventData2) 1958 void *callbackData, int eventData1, int eventData2)
1938 { 1959 {
1939 switch (event) 1960 switch (event)
1940 { 1961 {
1941 case EVENT_COMMIT: 1962 case EVENT_COMMIT:
1942 GetCtrlVal(MainPanel, PANEL_SLOPE_REFERENCE, &dedrift.reference); 1963 GetCtrlVal(panel, control, &dedrift.reference);
1943 break; 1964 break;
1944 } 1965 }
1945 return 0; 1966 return 0;
1946 } 1967 }
1947 1968
1959 } 1980 }
1960 1981
1961 int CVICALLBACK CB_InvertSlopeSign (int panel, int control, int event, 1982 int CVICALLBACK CB_InvertSlopeSign (int panel, int control, int event,
1962 void *callbackData, int eventData1, int eventData2) 1983 void *callbackData, int eventData1, int eventData2)
1963 { 1984 {
1964 switch (event) 1985 int invert;
1965 { 1986 switch (event)
1966 case EVENT_COMMIT: 1987 {
1967 GetCtrlVal(panel, control, &dedrift.invert); 1988 case EVENT_COMMIT:
1989 GetCtrlVal(panel, control, &invert);
1990 dedrift.sign = invert ? -1 : +1;
1968 break; 1991 break;
1969 } 1992 }
1970 return 0; 1993 return 0;
1971 } 1994 }
1972 1995
1980 dedrift.enabled = FALSE; 2003 dedrift.enabled = FALSE;
1981 SetCtrlVal(panel, PANEL_MEASURE_SLOPE, 0); 2004 SetCtrlVal(panel, PANEL_MEASURE_SLOPE, 0);
1982 dedrift.applied = 0.0; 2005 dedrift.applied = 0.0;
1983 SetCtrlVal(panel, PANEL_SLOPE_APPLIED, dedrift.applied); 2006 SetCtrlVal(panel, PANEL_SLOPE_APPLIED, dedrift.applied);
1984 // reset DDS 2007 // reset DDS
1985 ad9956_set_w(&ad9956, dedrift.freq0, dedrift.applied); 2008 ad9956_set_w(&ad9956, dedrift.fDDS, dedrift.applied);
1986 break; 2009 break;
1987 } 2010 }
1988 return 0; 2011 return 0;
1989 } 2012 }
1990 2013
2044 break; 2067 break;
2045 case PANEL_SIGN3: 2068 case PANEL_SIGN3:
2046 GetCtrlVal(panel, control, &Sign3); 2069 GetCtrlVal(panel, control, &Sign3);
2047 break; 2070 break;
2048 } 2071 }
2049 break;
2050 }
2051 return 0;
2052 }
2053
2054 int CVICALLBACK CB_SlopeX2 (int panel, int control, int event,
2055 void *callbackData, int eventData1, int eventData2)
2056 {
2057 switch (event)
2058 {
2059 case EVENT_COMMIT:
2060 GetCtrlVal(panel, control, &dedrift.doubleslope);
2061 break; 2072 break;
2062 } 2073 }
2063 return 0; 2074 return 0;
2064 } 2075 }
2065 2076
2130 void *callbackData, int eventData1, int eventData2) 2141 void *callbackData, int eventData1, int eventData2)
2131 { 2142 {
2132 switch (event) 2143 switch (event)
2133 { 2144 {
2134 case EVENT_COMMIT: 2145 case EVENT_COMMIT:
2135 GetCtrlVal(panel, control, &dedrift.freq0); 2146 GetCtrlVal(panel, control, &dedrift.fDDS);
2136 ad9956_set_w(&ad9956, dedrift.freq0, dedrift.applied); 2147 ad9956_set_w(&ad9956, dedrift.fDDS, dedrift.applied);
2137 break; 2148 break;
2138 } 2149 }
2139 return 0; 2150 return 0;
2140 } 2151 }
2141 2152