changeset 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 ec4a2a5c10d5
files FXAnalyse.c FXAnalyse.h FXAnalyse.uir
diffstat 3 files changed, 141 insertions(+), 130 deletions(-) [+]
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;
--- a/FXAnalyse.h	Mon Oct 27 18:03:35 2014 +0100
+++ b/FXAnalyse.h	Mon Oct 27 18:08:17 2014 +0100
@@ -109,44 +109,45 @@
 #define  PANEL_ADJUST_DDS3                69      /* control type: command, callback function: CB_AdjustDDSFreq */
 #define  PANEL_ADJUST_DDS2                70      /* control type: command, callback function: CB_AdjustDDSFreq */
 #define  PANEL_RESETSLOPE                 71      /* control type: command, callback function: CB_OnResetSlope */
-#define  PANEL_CHECKBOX_STOPIFAUTODE      72      /* control type: radioButton, callback function: CB_OnStopSlopeCancellingOnUnlocked */
-#define  PANEL_CHECKBOX_KEEPSLOPE         73      /* control type: radioButton, callback function: CB_OnCROX */
-#define  PANEL_CHECKBOX_KEEP              74      /* control type: radioButton, callback function: CB_OnCROX */
+#define  PANEL_DEDRIFT_STOP_ON_UNLOC      72      /* control type: radioButton, callback function: CB_OnStopSlopeCancellingOnUnlocked */
+#define  PANEL_DEDRIFT_KEEP_SLOPE         73      /* control type: radioButton, callback function: CB_OnDedriftSettingsChange */
+#define  PANEL_DEDRIFT_KEEP_FREQ          74      /* control type: radioButton, callback function: CB_OnDedriftSettingsChange */
 #define  PANEL_SLOPE_REFERENCE            75      /* control type: ring, callback function: CB_OnSlopeReference */
 #define  PANEL_ESTIMATE_N2                76      /* control type: command, callback function: cb_onEstimateN */
 #define  PANEL_ESTIMATE_N3                77      /* control type: command, callback function: cb_onEstimateN */
-#define  PANEL_INVERT_SLOPE_SIGN          78      /* control type: radioButton, callback function: CB_InvertSlopeSign */
-#define  PANEL_RECENTER                   79      /* control type: LED, callback function: CB_RecenterEnable */
-#define  PANEL_MEASURE_SLOPE              80      /* control type: LED, callback function: CB_MeasureSlope */
-#define  PANEL_SLOPE_MEASURED             81      /* control type: numeric, callback function: (none) */
-#define  PANEL_RESET_DEDRIFT_DDS          82      /* control type: command, callback function: CB_ResetDedriftDDS */
-#define  PANEL_SAVE_RAW                   83      /* control type: radioButton, callback function: CB_SaveData */
-#define  PANEL_SHOWLOG                    84      /* control type: radioButton, callback function: CB_ShowLog */
-#define  PANEL_SLOPEX2                    85      /* control type: radioButton, callback function: CB_SlopeX2 */
-#define  PANEL_SAVE_DDS                   86      /* control type: radioButton, callback function: CB_SaveData */
-#define  PANEL_DECORATION_2               87      /* control type: deco, callback function: (none) */
-#define  PANEL_DECORATION_8               88      /* control type: deco, callback function: (none) */
-#define  PANEL_DECORATION                 89      /* control type: deco, callback function: (none) */
-#define  PANEL_DECORATION_3               90      /* control type: deco, callback function: (none) */
-#define  PANEL_TEXTMSG_6                  91      /* control type: textMsg, callback function: (none) */
-#define  PANEL_TEXTMSG_22                 92      /* control type: textMsg, callback function: (none) */
-#define  PANEL_TEXTMSG_21                 93      /* control type: textMsg, callback function: (none) */
-#define  PANEL_TEXTMSG_7                  94      /* control type: textMsg, callback function: (none) */
-#define  PANEL_DECORATION_4               95      /* control type: deco, callback function: (none) */
-#define  PANEL_DECORATION_7               96      /* control type: deco, callback function: (none) */
-#define  PANEL_DECORATION_6               97      /* control type: deco, callback function: (none) */
-#define  PANEL_DECORATION_5               98      /* control type: deco, callback function: (none) */
-#define  PANEL_TEXTMSG_20                 99      /* control type: textMsg, callback function: (none) */
-#define  PANEL_TEXTMSG_19                 100     /* control type: textMsg, callback function: (none) */
-#define  PANEL_MATH4                      101     /* control type: string, callback function: (none) */
-#define  PANEL_MATH5                      102     /* control type: string, callback function: (none) */
-#define  PANEL_MATH3                      103     /* control type: string, callback function: (none) */
-#define  PANEL_MATH2                      104     /* control type: string, callback function: (none) */
-#define  PANEL_MATH1                      105     /* control type: string, callback function: (none) */
-#define  PANEL_RECENTER_SR                106     /* control type: radioButton, callback function: CB_RecenterChannel */
-#define  PANEL_RECENTER_HG                107     /* control type: radioButton, callback function: CB_RecenterChannel */
-#define  PANEL_RECENTER_LO                108     /* control type: radioButton, callback function: CB_RecenterChannel */
-#define  PANEL_ERROR                      109     /* control type: LED, callback function: CB_ShowError */
+#define  PANEL_DEDRIFT_PROPORTIONAL       78      /* control type: radioButton, callback function: CB_OnDedriftSettingsChange */
+#define  PANEL_DEDRIFT_INVERT_SIGN        79      /* control type: radioButton, callback function: CB_InvertSlopeSign */
+#define  PANEL_RECENTER                   80      /* control type: LED, callback function: CB_RecenterEnable */
+#define  PANEL_MEASURE_SLOPE              81      /* control type: LED, callback function: CB_MeasureSlope */
+#define  PANEL_SLOPE_MEASURED             82      /* control type: numeric, callback function: (none) */
+#define  PANEL_RESET_DEDRIFT_DDS          83      /* control type: command, callback function: CB_ResetDedriftDDS */
+#define  PANEL_SAVE_RAW                   84      /* control type: radioButton, callback function: CB_SaveData */
+#define  PANEL_SHOWLOG                    85      /* control type: radioButton, callback function: CB_ShowLog */
+#define  PANEL_DEDRIFT_DOUBLE_CORR        86      /* control type: radioButton, callback function: CB_OnDedriftSettingsChange */
+#define  PANEL_SAVE_DDS                   87      /* control type: radioButton, callback function: CB_SaveData */
+#define  PANEL_DECORATION_2               88      /* control type: deco, callback function: (none) */
+#define  PANEL_DECORATION_8               89      /* control type: deco, callback function: (none) */
+#define  PANEL_DECORATION                 90      /* control type: deco, callback function: (none) */
+#define  PANEL_DECORATION_3               91      /* control type: deco, callback function: (none) */
+#define  PANEL_TEXTMSG_6                  92      /* control type: textMsg, callback function: (none) */
+#define  PANEL_TEXTMSG_22                 93      /* control type: textMsg, callback function: (none) */
+#define  PANEL_TEXTMSG_21                 94      /* control type: textMsg, callback function: (none) */
+#define  PANEL_TEXTMSG_7                  95      /* control type: textMsg, callback function: (none) */
+#define  PANEL_DECORATION_4               96      /* control type: deco, callback function: (none) */
+#define  PANEL_DECORATION_7               97      /* control type: deco, callback function: (none) */
+#define  PANEL_DECORATION_6               98      /* control type: deco, callback function: (none) */
+#define  PANEL_DECORATION_5               99      /* control type: deco, callback function: (none) */
+#define  PANEL_TEXTMSG_20                 100     /* control type: textMsg, callback function: (none) */
+#define  PANEL_TEXTMSG_19                 101     /* control type: textMsg, callback function: (none) */
+#define  PANEL_MATH4                      102     /* control type: string, callback function: (none) */
+#define  PANEL_MATH5                      103     /* control type: string, callback function: (none) */
+#define  PANEL_MATH3                      104     /* control type: string, callback function: (none) */
+#define  PANEL_MATH2                      105     /* control type: string, callback function: (none) */
+#define  PANEL_MATH1                      106     /* control type: string, callback function: (none) */
+#define  PANEL_RECENTER_SR                107     /* control type: radioButton, callback function: CB_RecenterChannel */
+#define  PANEL_RECENTER_HG                108     /* control type: radioButton, callback function: CB_RecenterChannel */
+#define  PANEL_RECENTER_LO                109     /* control type: radioButton, callback function: CB_RecenterChannel */
+#define  PANEL_ERROR                      110     /* control type: LED, callback function: CB_ShowError */
 
 
      /* Control Arrays: */
@@ -174,7 +175,7 @@
 int  CVICALLBACK CB_OnAcceptN(int panel, int control, int event, void *callbackData, int eventData1, int eventData2);
 int  CVICALLBACK CB_OnAllanPlot(int panel, int control, int event, void *callbackData, int eventData1, int eventData2);
 int  CVICALLBACK CB_OnChangeNdiv(int panel, int control, int event, void *callbackData, int eventData1, int eventData2);
-int  CVICALLBACK CB_OnCROX(int panel, int control, int event, void *callbackData, int eventData1, int eventData2);
+int  CVICALLBACK CB_OnDedriftSettingsChange(int panel, int control, int event, void *callbackData, int eventData1, int eventData2);
 int  CVICALLBACK cb_onEstimateN(int panel, int control, int event, void *callbackData, int eventData1, int eventData2);
 int  CVICALLBACK cb_onEstimateNChange(int panel, int control, int event, void *callbackData, int eventData1, int eventData2);
 int  CVICALLBACK cb_onEstimateNClose(int panel, int control, int event, void *callbackData, int eventData1, int eventData2);
@@ -201,7 +202,6 @@
 int  CVICALLBACK CB_SetSlope(int panel, int control, int event, void *callbackData, int eventData1, int eventData2);
 int  CVICALLBACK CB_ShowError(int panel, int control, int event, void *callbackData, int eventData1, int eventData2);
 int  CVICALLBACK CB_ShowLog(int panel, int control, int event, void *callbackData, int eventData1, int eventData2);
-int  CVICALLBACK CB_SlopeX2(int panel, int control, int event, void *callbackData, int eventData1, int eventData2);
 int  CVICALLBACK CB_SrDatalogger(int panel, int control, int event, void *callbackData, int eventData1, int eventData2);
 int  CVICALLBACK QuitCallback(int panel, int control, int event, void *callbackData, int eventData1, int eventData2);
 
Binary file FXAnalyse.uir has changed