diff FXAnalyse.c @ 256:708de02ef948

Implement limit to frequency adjustment during recenter
author Daniele Nicolodi <daniele.nicolodi@obspm.fr>
date Fri, 05 Jun 2015 21:00:54 +0200
parents 67c8ace9d5f6
children 8cbfce046d41
line wrap: on
line diff
--- a/FXAnalyse.c	Fri Jun 05 18:10:46 2015 +0200
+++ b/FXAnalyse.c	Fri Jun 05 21:00:54 2015 +0200
@@ -257,9 +257,10 @@
 
 
 enum {
-	LO = 1,
-	HG = 2,
-	SR = 4,
+	LO,
+	HG,
+	SR,
+	_N_BEATS,
 };
 
 enum {
@@ -499,24 +500,96 @@
 
 // recenter
 struct recenter {
-	int enabled;		// recenter enabled
-	int lo;				// recenter microwave beatnote
-	int sr;				// recenter Sr beatnote
-	int hg;				// recenter Hg beatnote
-	double interval;	// interval
-	double t0;			// beginning of current interval
+	int active;					// recenter enabled
+	int enabled[_N_BEATS];		// which beatnotes to recenter
+	double threshold[_N_BEATS];	// maximum frequency correction
+	double interval;			// interval
+	double t0;					// beginning of current interval
 };
 
 struct recenter recenter = {
-	.enabled = FALSE,
-	.lo = FALSE,
-	.sr = FALSE,
-	.hg = FALSE,
+	.active = FALSE,
+	.enabled = { FALSE, FALSE, FALSE },
+	.threshold = { 10.0, 2000.0, 2000.0 },
 	.interval = 1800.0,
 	.t0 = 0.0
 };
 
 
+int recenter_enabled()
+{
+	if (! recenter.active)
+		return FALSE;
+	
+	for (int i = 0; i < _N_BEATS; i++)
+		if (recenter.enabled[i])
+			return TRUE;
+
+	return FALSE;
+}
+
+
+void recenter_update()
+{
+	if (! recenter_enabled())
+		return;
+	
+	rollmean_accumulate(&rollmean_ch2, Ch2);
+	rollmean_accumulate(&rollmean_ch3, Ch3);
+	rollmean_accumulate(&rollmean_ch4, Ch4);
+			
+	if ((utc - recenter.t0) > recenter.interval) {
+		
+		if (recenter.enabled[LO]) {
+			// adjust DDS2 frequency to keep Ch4 reading at 275 kHz
+			double freq = ad9912.frequency[1];
+			double adj = 275000.0 - rollmean_ch4.mean;
+			if (fabs(adj) > recenter.threshold[LO]) {
+				logmessage(WARNING, "not recenter ch4 to 275 kHz: DDS2 adjustment=%+3e exceeds threshold", adj);
+			} else {
+				freq = freq + adj;
+				ad9912_set_frequency_w(&ad9912, 1, freq);
+				SetCtrlVal(MainPanel, PANEL_DDS2, ad9912.frequency[1]);
+				logmsg("recenter ch4 to 275 kHz: DDS2 adjustment=%+3e", adj);
+			}
+		}
+				
+		if (recenter.enabled[HG]) {
+			// adjust DDS3 frequency to keep Ch2 reading at 10 kHz
+			double freq = ad9912.frequency[2];
+			double adj = 10000 - rollmean_ch2.mean;
+			if (fabs(adj) > recenter.threshold[HG]) {
+				logmessage(WARNING, "not recenter Hg beatnote (ch2) to 10 kHz: DDS3 adjustment=%+3e exceeds threshold", adj);
+			} else {
+				freq = freq + adj;
+				ad9912_set_frequency_w(&ad9912, 2, freq);
+				SetCtrlVal(MainPanel, PANEL_DDS3, ad9912.frequency[2]);
+				logmsg("recenter Hg beatnote (ch2) to 10 kHz: DDS3 adjustment=%+3e", adj);
+			}
+		}
+			
+		if (recenter.enabled[SR]) {
+			// adjust DDS4 frequency to keep Ch3 reading at 10 kHz
+			double freq = ad9912.frequency[3];
+			double adj = 10000 - rollmean_ch3.mean;
+			if (fabs(adj) > recenter.threshold[SR]) {
+				logmessage(WARNING, "not recenter Sr beatnote (ch3) to 10 kHz: DDS4 adjustment=%+3e exceeds threshold", adj);
+			} else {
+				freq = freq + adj;
+				ad9912_set_frequency_w(&ad9912, 3, freq);
+				SetCtrlVal(MainPanel, PANEL_DDS4, ad9912.frequency[3]);
+				logmsg("recenter Sr beatnote (ch3) to 10 kHz: DDS4 adjustment=%+3e", adj);
+			}
+		}
+				
+		recenter.t0 = utc;
+		rollmean_zero(&rollmean_ch2);
+		rollmean_zero(&rollmean_ch3);
+		rollmean_zero(&rollmean_ch4);
+	}
+}
+
+
 // data loggging
 static char *datafolder;
 
@@ -1474,50 +1547,7 @@
 				dedrift_update(f);
 								
 				// recenter
-				if (recenter.enabled)
-				{
-					rollmean_accumulate(&rollmean_ch2, Ch2);
-					rollmean_accumulate(&rollmean_ch3, Ch3);
-					rollmean_accumulate(&rollmean_ch4, Ch4);
-					
-					if ((utc - recenter.t0) > recenter.interval) {
-		
-						if (recenter.lo) {
-							// adjust DDS2 frequency to keep Ch4 reading at 275 kHz
-							double freq = ad9912.frequency[1];
-							double adj = 275000.0 - rollmean_ch4.mean;
-							freq = freq + adj;
-							ad9912_set_frequency_w(&ad9912, 1, freq);
-							SetCtrlVal(MainPanel, PANEL_DDS2, ad9912.frequency[1]);
-							logmsg("recenter ch4 to 275 kHz: DDS2 adjustment=%+3e", adj);
-						}
-					
-						if (recenter.hg) {
-							// adjust DDS3 frequency to keep Ch2 reading at 10 kHz
-							double freq = ad9912.frequency[2];
-							double adj = 10000 - rollmean_ch2.mean;
-							freq = freq + adj;
-							ad9912_set_frequency_w(&ad9912, 2, freq);
-							SetCtrlVal(MainPanel, PANEL_DDS3, ad9912.frequency[2]);
-							logmsg("recenter Hg beatnote (ch2) to 10 kHz: DDS3 adjustment=%+3e", adj);
-						}
-					
-						if (recenter.sr) {
-							// adjust DDS4 frequency to keep Ch3 reading at 10 kHz
-							double freq = ad9912.frequency[3];
-							double adj = 10000 - rollmean_ch3.mean;
-							freq = freq + adj;
-							ad9912_set_frequency_w(&ad9912, 3, freq);
-							SetCtrlVal(MainPanel, PANEL_DDS4, ad9912.frequency[3]);
-							logmsg("recenter Sr beatnote (ch3) to 10 kHz: DDS4 adjustment=%+3e", adj);
-						}
-					
-						recenter.t0 = utc;
-						rollmean_zero(&rollmean_ch2);
-						rollmean_zero(&rollmean_ch3);
-						rollmean_zero(&rollmean_ch4);
-					}
-				}
+				recenter_update();
 
 				struct tm *time = gmtime(&ev.time.tv_sec);
 				// round to milliseconds
@@ -2021,7 +2051,7 @@
 	switch (event)
 	{
 		case EVENT_COMMIT:
-			GetCtrlVal(panel, control, &recenter.enabled);
+			GetCtrlVal(panel, control, &recenter.active);
 			recenter.t0 = utc;
 			rollmean_zero(&rollmean_ch2);
 			rollmean_zero(&rollmean_ch3);
@@ -2200,13 +2230,36 @@
 			switch (control)
 			{
 				case PANEL_RECENTER_LO:
-					GetCtrlVal(panel, control, &recenter.lo);
+					GetCtrlVal(panel, control, &recenter.enabled[LO]);
 					break;
 				case PANEL_RECENTER_HG:
-					GetCtrlVal(panel, control, &recenter.hg);
+					GetCtrlVal(panel, control, &recenter.enabled[HG]);
 					break;
 				case PANEL_RECENTER_SR:
-					GetCtrlVal(panel, control, &recenter.sr);
+					GetCtrlVal(panel, control, &recenter.enabled[SR]);
+					break;
+			}
+			break;
+	}
+	return 0;
+}
+
+int CVICALLBACK CB_RecenterThreshold (int panel, int control, int event,
+		void *callbackData, int eventData1, int eventData2)
+{
+	switch (event)
+	{
+		case EVENT_COMMIT:
+			switch (control)
+			{
+				case PANEL_RECENTER_THRESHOLD_LO:
+					GetCtrlVal(panel, control, &recenter.threshold[LO]);
+					break;
+				case PANEL_RECENTER_THRESHOLD_HG:
+					GetCtrlVal(panel, control, &recenter.threshold[HG]);
+					break;
+				case PANEL_RECENTER_THRESHOLD_SR:
+					GetCtrlVal(panel, control, &recenter.threshold[SR]);
 					break;
 			}
 			break;
@@ -2409,3 +2462,4 @@
 	}
 	return 0;
 }
+