Mercurial > hg > fxanalyse
diff 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 |
line wrap: on
line diff
--- a/FXAnalyse.c Mon Oct 27 18:03:35 2014 +0100 +++ b/FXAnalyse.c Mon Oct 27 18:08:17 2014 +0100 @@ -312,7 +312,7 @@ }; -struct stat stat_math1, stat_ch2, stat_ch3, freq; +struct stat stat_math1, stat_ch2, stat_ch3; struct rollmean rollmean_ch1, rollmean_ch2, rollmean_ch3, rollmean_ch4; @@ -324,26 +324,31 @@ struct dedrift { int enabled; // dedrift enabled + int proportional; // enable proportional correction int reference; // reference frequency - int invert; // invert applied slope sign - int doubleslope; // double applied slope + int sign; // sign of the correction + int x2; // double the applied correction int keep_freq; // keep current frequency value when dedrift is disabled int keep_slope; // keep current slope value when dedrift is disabled - double freq0; // DDS center frequency + double f0; // target frequency + double fDDS; // DDS center frequency double threshold; // maximum allowed frequency change double applied; // currently applied slope double interval; // measurement duration double t0; // beginning of currrent measurement interval + struct stat stat; // frequency mean and slope }; struct dedrift dedrift = { .enabled = FALSE, + .proportional = FALSE, .reference = DEDRIFT_REFERENCE_MICROWAVE, - .invert = FALSE, - .doubleslope = FALSE, + .sign = +1, + .x2 = FALSE, .keep_freq = TRUE, .keep_slope = TRUE, - .freq0 = 70e6, + .f0 = 0.0, + .fDDS = 70e6, .threshold = 100.0, .applied = 0.0, .interval = 30.0, @@ -351,6 +356,64 @@ }; +void dedrift_update(double f) +{ + // stop dedrift if the comb is not locked + if ((dedrift.enabled) && + (dedrift.threshold != 0.0) && + (dedrift.stat.previous != 0.0) && + (fabs(f - dedrift.stat.previous) > dedrift.threshold)) { + + logmsg("dedrift stop: frequency jump detected"); + + if (! dedrift.keep_slope) { + dedrift.applied = 0.0; + ad9956_set_sweep_rate_w(&ad9956, dedrift.applied); + SetCtrlVal(MainPanel, PANEL_SLOPE_APPLIED, dedrift.applied); + } + if (! dedrift.keep_freq) { + ad9956_set_w(&ad9956, dedrift.fDDS, dedrift.applied); + } + + stat_zero(&dedrift.stat); + SetCtrlVal(MainPanel, PANEL_SLOPE_MEASURED, dedrift.stat.slope); + dedrift.enabled = FALSE; + SetCtrlVal(MainPanel, PANEL_MEASURE_SLOPE, 0); + } + + // dedrifting + if (dedrift.enabled) { + + // update measurement + stat_accumulate(&dedrift.stat, f); + SetCtrlVal(MainPanel, PANEL_SLOPE_MEASURED, dedrift.stat.slope); + + // update applied slope + if ((utc - dedrift.t0) > dedrift.interval) { + + // target frequency + if (dedrift.f0 == 0.0) + dedrift.f0 = dedrift.stat.mean; + + // compute correction + double dt = utc - dedrift.t0; + double corr = dedrift.stat.slope \ + + dedrift.proportional * ((dedrift.stat.mean - dedrift.f0) / dt + 0.5 * dedrift.stat.slope); + + // update + dedrift.applied += dedrift.sign * corr * (dedrift.x2 ? 2 : 1); + ad9956_set_sweep_rate_w(&ad9956, dedrift.applied); + SetCtrlVal(MainPanel, PANEL_SLOPE_APPLIED, dedrift.applied); + logmsg("dedrift update: correction=%+3e slope=%+3e", corr, dedrift.applied); + + // start over + dedrift.t0 = utc; + stat_zero(&dedrift.stat); + } + } +} + + // recenter struct recenter { int enabled; // recenter enabled @@ -542,7 +605,7 @@ rv = ad9956_init(&ad9956, host, clock); if (rv) logmessage(ERROR, "ad9956 init erorr=%d", -rv); - ad9956_set_w(&ad9956, dedrift.freq0, dedrift.applied); + ad9956_set_w(&ad9956, dedrift.fDDS, dedrift.applied); // ad9912 configuration parameters rv = Ini_GetStringIntoBuffer(configuration, "AD9912", "host", host, sizeof(host)); @@ -1294,7 +1357,7 @@ logmessage(ERROR, "merasured f_rep_sign does not agree with previous determination!"); } - // select reference + // select dedrift reference double f = 0.0; switch (dedrift.reference) { case DEDRIFT_REFERENCE_MICROWAVE: @@ -1304,61 +1367,10 @@ f = Ch2 * 1062.5 / 1542.2; break; } - - // stop dedrift if the comb is not locked - if ((dedrift.enabled) && - (dedrift.threshold != 0.0) && - (freq.previous != 0.0) && - (fabs(f - freq.previous) > dedrift.threshold)) { - - logmsg("dedrift stop: frequency jump detected"); - - if (! dedrift.keep_slope) { - dedrift.applied = 0.0; - ad9956_set_sweep_rate_w(&ad9956, dedrift.applied); - SetCtrlVal(MainPanel, PANEL_SLOPE_APPLIED, dedrift.applied); - } - if (! dedrift.keep_freq) { - ad9956_set_w(&ad9956, dedrift.freq0, dedrift.applied); - } - - stat_zero(&freq); - SetCtrlVal(MainPanel, PANEL_SLOPE_MEASURED, freq.slope); - dedrift.enabled = FALSE; - SetCtrlVal(MainPanel, PANEL_MEASURE_SLOPE, 0); - } - - // dedrifting - if (dedrift.enabled) - { - // update slope measurement - stat_accumulate(&freq, f); - - // update indicator - SetCtrlVal(MainPanel, PANEL_SLOPE_MEASURED, freq.slope); - - // update applied slope - if ((utc - dedrift.t0) > dedrift.interval) { - - if (dedrift.invert) - dedrift.applied -= freq.slope; - else - dedrift.applied += freq.slope; - - SetCtrlVal(MainPanel, PANEL_SLOPE_APPLIED, dedrift.applied); - - if (dedrift.doubleslope) - ad9956_set_sweep_rate_w(&ad9956, 2 * dedrift.applied); - else - ad9956_set_sweep_rate_w(&ad9956, dedrift.applied); - - logmsg("dedrift update: adjustment=%+3e slope=%+3e", freq.slope, dedrift.applied); - - stat_zero(&freq); - dedrift.t0 = utc; - } - } - + + // dedrift + dedrift_update(f); + // recenter if (recenter.enabled) { @@ -1835,7 +1847,7 @@ GetCtrlVal(panel, control, &dedrift.enabled); if (dedrift.enabled) { dedrift.t0 = utc; - stat_zero(&freq); + stat_zero(&dedrift.stat); logmsg("dedrift start"); } else { if (! dedrift.keep_slope) { @@ -1844,10 +1856,10 @@ SetCtrlVal(MainPanel, PANEL_SLOPE_APPLIED, dedrift.applied); } if (! dedrift.keep_freq) { - ad9956_set_w(&ad9956, dedrift.freq0, dedrift.applied); + ad9956_set_w(&ad9956, dedrift.fDDS, dedrift.applied); } - stat_zero(&freq); - SetCtrlVal(panel, PANEL_SLOPE_MEASURED, freq.slope); + stat_zero(&dedrift.stat); + SetCtrlVal(panel, PANEL_SLOPE_MEASURED, dedrift.stat.slope); logmsg("dedrift stop"); } break; @@ -1863,7 +1875,7 @@ case EVENT_COMMIT: dedrift.applied = 0.0; SetCtrlVal(panel, PANEL_SLOPE_APPLIED, dedrift.applied); - ad9956_set_w(&ad9956, dedrift.freq0, dedrift.applied); + ad9956_set_w(&ad9956, dedrift.fDDS, dedrift.applied); logmsg("dedrift reset"); break; } @@ -1882,20 +1894,29 @@ return 0; } -int CVICALLBACK CB_OnCROX (int panel, int control, int event, +int CVICALLBACK CB_OnDedriftSettingsChange (int panel, int control, int event, void *callbackData, int eventData1, int eventData2) { switch (event) { case EVENT_COMMIT: - switch (control) { - case PANEL_CHECKBOX_KEEP: + switch (control) + { + case PANEL_DEDRIFT_PROPORTIONAL: + // include correction proportional to frequency + GetCtrlVal(panel, control, &dedrift.proportional); + break; + case PANEL_DEDRIFT_DOUBLE_CORR: + // double slope correction + GetCtrlVal(panel, control, &dedrift.x2); + break; + case PANEL_DEDRIFT_KEEP_FREQ: // keep current dedrifting frequency when dedrifting is disabled - GetCtrlVal(MainPanel, PANEL_CHECKBOX_KEEP, &dedrift.keep_freq); + GetCtrlVal(panel, control, &dedrift.keep_freq); break; - case PANEL_CHECKBOX_KEEPSLOPE: + case PANEL_DEDRIFT_KEEP_SLOPE: // keep current dedrifting slope when dedrifting is disabled - GetCtrlVal(MainPanel, PANEL_CHECKBOX_KEEPSLOPE, &dedrift.keep_slope); + GetCtrlVal(panel, control, &dedrift.keep_slope); break; } break; @@ -1926,7 +1947,7 @@ switch (event) { case EVENT_COMMIT: - GetCtrlVal(MainPanel, PANEL_CHECKBOX_STOPIFAUTODE, &value); + GetCtrlVal(panel, control, &value); dedrift.threshold = value ? 100.0 : 0.0; break; } @@ -1939,7 +1960,7 @@ switch (event) { case EVENT_COMMIT: - GetCtrlVal(MainPanel, PANEL_SLOPE_REFERENCE, &dedrift.reference); + GetCtrlVal(panel, control, &dedrift.reference); break; } return 0; @@ -1961,10 +1982,12 @@ int CVICALLBACK CB_InvertSlopeSign (int panel, int control, int event, void *callbackData, int eventData1, int eventData2) { + int invert; switch (event) { case EVENT_COMMIT: - GetCtrlVal(panel, control, &dedrift.invert); + GetCtrlVal(panel, control, &invert); + dedrift.sign = invert ? -1 : +1; break; } return 0; @@ -1982,7 +2005,7 @@ dedrift.applied = 0.0; SetCtrlVal(panel, PANEL_SLOPE_APPLIED, dedrift.applied); // reset DDS - ad9956_set_w(&ad9956, dedrift.freq0, dedrift.applied); + ad9956_set_w(&ad9956, dedrift.fDDS, dedrift.applied); break; } return 0; @@ -2051,18 +2074,6 @@ return 0; } -int CVICALLBACK CB_SlopeX2 (int panel, int control, int event, - void *callbackData, int eventData1, int eventData2) -{ - switch (event) - { - case EVENT_COMMIT: - GetCtrlVal(panel, control, &dedrift.doubleslope); - break; - } - return 0; -} - int CVICALLBACK CB_SaveData (int panel, int control, int event, void *callbackData, int eventData1, int eventData2) { @@ -2132,8 +2143,8 @@ switch (event) { case EVENT_COMMIT: - GetCtrlVal(panel, control, &dedrift.freq0); - ad9956_set_w(&ad9956, dedrift.freq0, dedrift.applied); + GetCtrlVal(panel, control, &dedrift.fDDS); + ad9956_set_w(&ad9956, dedrift.fDDS, dedrift.applied); break; } return 0;