diff FXAnalyse.c @ 133:7540703b8473

Major code cleanup. Implement beatnote recentering.
author Daniele Nicolodi <daniele.nicolodi@obspm.fr>
date Wed, 22 Jan 2014 12:10:17 +0100
parents fbd72d87b4dc
children bd28161e5ac2
line wrap: on
line diff
--- a/FXAnalyse.c	Tue Dec 17 17:21:17 2013 +0100
+++ b/FXAnalyse.c	Wed Jan 22 12:10:17 2014 +0100
@@ -14,6 +14,7 @@
 #include "DDS4xAD9912.h"
 #include "DDS_Fox.h" 
 #include "muParserDLL.h"
+#include "stat.h"
 
 #define SR_LOGGER_IP "145.238.204.91"
 #define DATAFOLDER "Z:\\Measures-2013"
@@ -118,38 +119,64 @@
 volatile bool Getsign1=FALSE,Getsign2=FALSE,Getsign3=FALSE;
 double Sign1=1.0, Sign2=1.0, Sign3=0.0;
 
-// slope cancelling
+
+struct stat stat_math1, stat_ch2, stat_ch3, stat_ch4, freq;
+struct rollmean rollmean_ch1, rollmean_ch2, rollmean_ch3, rollmean_ch4;
+
 
-double dedrift_DDS_freq = 70e6;
-int SlopeMeasuring = FALSE;
-int StopSlopeCancellingOnUnlocked = TRUE;
-double TimetoSlope = 60.0;
-double SlopeMeasuringTimeBegin = 0.0;
-double appliedSlope = 0.0; // currently applied frequency dedrifiting slope
-int slopeX2 = FALSE;
-
-double limitotakoff=70.0;
-
-int ratio=10; //Recentre la frequence tous les ratios
-
-int FrequCorrec = FALSE, KeepFrequ = TRUE, KeepSlope = TRUE;
-int Nratio = -1;
-double MoyMath2 = 0.0, CenterFrequencyCh2 = 0.0;
-int CenterFrequencyCh2ToDetermine = FALSE;
+// dedrift
+struct dedrift {
+	int enabled;        // dedrift enabled
+	int reference;      // reference frequency
+	int invert;         // invert applied slope sign
+	int doubleslope;    // double applied slope
+	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 threshold;   // maximum allowed frequency change
+	double applied;     // currently applied slope
+	double interval;    // measurement duration
+	double t0;          // beginning of currrent measurement interval
+};
 
 enum {
-	SLOPE_REFERENCE_MICROWAVE = 0,
-	SLOPE_REFERENCE_HG_CAVITY = 1,
+	DEDRIFT_REFERENCE_MICROWAVE = 0,
+	DEDRIFT_REFERENCE_HG = 1,
+};
+
+struct dedrift dedrift = {
+	.enabled = FALSE,
+	.reference = DEDRIFT_REFERENCE_MICROWAVE,
+	.invert = FALSE,
+	.doubleslope = FALSE,
+	.keep_freq = TRUE,
+	.keep_slope = TRUE,
+	.freq0 = 70e6,
+	.threshold = 100.0,
+	.applied = 0.0,
+	.interval = 40.0,
+	.t0 = 0.0
 };
 
-int invertSlopeSign = 0;
-int slopeReference = SLOPE_REFERENCE_MICROWAVE;
 
-int KeepCentering = FALSE; 
-double Timetorecenter275K = 3600.0 * 10;
-double Timetorecenter10K = 3600.0 * 3;
-double CenteringTimeBegin275K = 0.0;
-double CenteringTimeBegin10K = 0.0;
+// 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
+};
+
+struct recenter recenter = {
+	.enabled = FALSE,
+	.Lo = FALSE,
+	.Sr = FALSE,
+	.Hg = FALSE,
+	.interval = 1800.0,
+	.t0 = 0.0
+};
 
 
 // panels
@@ -184,35 +211,6 @@
 }
 
 
-struct stat {
-	int samples;
-	double mean;
-	double slope;
-	double previous;
-};
-
-void stat_zero(struct stat *s)
-{
-	s->samples = 0;
-	s->mean = 0.0;
-	s->slope = 0.0;
-	s->previous = 0.0;
-}
-
-void stat_accumulate(struct stat *s, double value)
-{
-	s->samples += 1;
-	
-	if (s->samples > 1)
-		s->slope = (s->slope * (s->samples - 2) + 6 * (value - s->mean) / s->samples) / (s->samples + 1);
-					
-	s->mean = ((s->samples - 1) * s->mean + value) / s->samples;
-	
-	s->previous = value;
-}
-
-struct stat stat_math1, stat_ch2, stat_ch4, stat_ch3, freq;
-
 #define MIN(x, y) (x) < (y) ? (x) : (y)
 
 // MJD functiom used by the Sr programs
@@ -377,7 +375,7 @@
 	DDS4xAD9912_SetFrequency(&DDS4xAD9912, 4, frequency);
 	
 	// initialyze 1x AD9956 DDS box
-	DDSFox_Initialize(&DDS1xAD9956, "145.238.205.58", 6665, dedrift_DDS_freq);
+	DDSFox_Initialize(&DDS1xAD9956, "145.238.205.58", 6665, dedrift.freq0);
 	
 	Ch1 = Ch2 = Ch3 = Ch4 = 0.0;
 	
@@ -661,8 +659,6 @@
 				SetCtrlVal(MainPanel, PANEL_CH2, Ch2);
 				SetCtrlVal(MainPanel, PANEL_CH3, Ch3);  
 				SetCtrlVal(MainPanel, PANEL_CH4, Ch4);
-
-				SetCtrlVal(MainPanel, PANEL_CENTERFREQUENCY, CenterFrequencyCh2);
 				
 				// compute
 				Math1 = mupEval(MathParser1);
@@ -1297,105 +1293,112 @@
 				}
 				
 				// select reference
-				double current = 0.0;
-				switch (slopeReference) {
-					case SLOPE_REFERENCE_MICROWAVE:
-						current = Math2;
+				double f = 0.0;
+				switch (dedrift.reference) {
+					case DEDRIFT_REFERENCE_MICROWAVE:
+						f = Math2;
 						break;
-					case SLOPE_REFERENCE_HG_CAVITY:
-						current = Ch2 * 1062.5 / 1542.2;
+					case DEDRIFT_REFERENCE_HG:
+						f = Ch2 * 1062.5 / 1542.2;
 						break;
 				}
 				
-				// stop slope cancelling if the comb is not locked
-				if (SlopeMeasuring & StopSlopeCancellingOnUnlocked
+				// stop dedrift if the comb is not locked
+				if ((dedrift.enabled)
+					& (dedrift.threshold != 0.0)
 					& (freq.previous != 0.0)
-					& (fabs(current - freq.previous) > limitotakoff)) {
+					& (fabs(f - freq.previous) > dedrift.threshold)) {
 						
-					if (! KeepSlope) {
-						appliedSlope = 0.0;
-						DDSFox_SetSweepRate(&DDS1xAD9956, appliedSlope);
-						SetCtrlVal(MainPanel, PANEL_SLOPE_APPLIED, appliedSlope);
+					if (! dedrift.keep_slope) {
+						dedrift.applied = 0.0;
+						DDSFox_SetSweepRate(&DDS1xAD9956, dedrift.applied);
+						SetCtrlVal(MainPanel, PANEL_SLOPE_APPLIED, dedrift.applied);
 					}
-					if (! KeepFrequ) {
-						DDSFox_Set(&DDS1xAD9956, dedrift_DDS_freq, appliedSlope);
+					if (! dedrift.keep_freq) {
+						DDSFox_Set(&DDS1xAD9956, dedrift.freq0, dedrift.applied);
 					}
 					
 					stat_zero(&freq);
 					SetCtrlVal(MainPanel, PANEL_SLOPE_MEASURED, freq.slope);
-					SlopeMeasuring = FALSE;
+					dedrift.enabled = FALSE;
 					SetCtrlVal(MainPanel, PANEL_MEASURE_SLOPE, 0);
 				}
 				
-				// slope measurement 
-				if (SlopeMeasuring)
+				// dedrifting 
+				if (dedrift.enabled)
 				{
 					// update slope measurement
-					stat_accumulate(&freq, current);
+					stat_accumulate(&freq, f);
 					
 					// update indicator
 					SetCtrlVal(MainPanel, PANEL_SLOPE_MEASURED, freq.slope);
 					
 					// update applied slope
-					if ((utc - SlopeMeasuringTimeBegin) > TimetoSlope) {
+					if ((utc - dedrift.t0) > dedrift.interval) {
 						
-						if (invertSlopeSign)
-							appliedSlope = appliedSlope - freq.slope;
+						if (dedrift.invert)
+							dedrift.applied -= freq.slope;
 						else
-							appliedSlope = appliedSlope + freq.slope;
+							dedrift.applied += freq.slope;
 						
-						if (FrequCorrec) {
-							// proportional correction
-							
-							Nratio += 1;
-							if (Nratio >= 1) {
-								MoyMath2 = MoyMath2 + freq.mean;
-							}
-							if (Nratio == 1 && CenterFrequencyCh2ToDetermine == TRUE) {
-								CenterFrequencyCh2 = MoyMath2;
-								CenterFrequencyCh2ToDetermine = FALSE;
-							}
-							if (Nratio == ratio) {
-								double correction = (MoyMath2/ratio-CenterFrequencyCh2)/TimetoSlope;
-								appliedSlope = appliedSlope + correction;
-								Nratio = 0;
-								MoyMath2 = 0.0;
-							}
-						}
+						SetCtrlVal(MainPanel, PANEL_SLOPE_APPLIED, dedrift.applied);
 						
-						SetCtrlVal(MainPanel, PANEL_SLOPE_APPLIED, appliedSlope);
-						if (slopeX2)
-							DDSFox_SetSweepRate(&DDS1xAD9956, appliedSlope * 2.0);
+						if (dedrift.doubleslope)
+							DDSFox_SetSweepRate(&DDS1xAD9956, dedrift.applied * 2.0);
 						else
-							DDSFox_SetSweepRate(&DDS1xAD9956, appliedSlope);
+							DDSFox_SetSweepRate(&DDS1xAD9956, dedrift.applied);
 						
-						logmsg("Slope correction update (%+6g) %6g", freq.slope, appliedSlope);
+						logmsg("dedrift update (%+6g) %6g", freq.slope, dedrift.applied);
 						
 						stat_zero(&freq);
-						SlopeMeasuringTimeBegin = utc;
+						dedrift.t0 = utc;
 					}
 				}
 				
-				// re-centering
-				if (KeepCentering) {
-					if ((utc - CenteringTimeBegin275K) > Timetorecenter275K && CenteringTimeBegin275K > 10) {
-						double frequency;
-						GetCtrlVal(MainPanel, PANEL_DDS2, &frequency);
-						// adjust DDS2 frequency to keep Ch4 reading around 275000
-						frequency = frequency + 275000 - Ch4;
-						SetCtrlVal(MainPanel, PANEL_DDS2, frequency);
-						DDS4xAD9912_SetFrequency(&DDS4xAD9912, 2, frequency);
-						CenteringTimeBegin275K = utc;
+				// recenter
+				if (recenter.enabled)
+				{
+					if (recenter.t0 == 0.0) {
+						recenter.t0 = utc;
+						rollmean_zero(&rollmean_ch2, ROLLMEAN_N_OBS);
+						rollmean_zero(&rollmean_ch3, ROLLMEAN_N_OBS);
+						rollmean_zero(&rollmean_ch4, ROLLMEAN_N_OBS);
 					}
 					
-					if ((utc - CenteringTimeBegin10K) > Timetorecenter10K && CenteringTimeBegin10K > 10) {
-						double frequency;
-						GetCtrlVal(MainPanel, PANEL_DDS3, &frequency);
-						// adjust DDS3 frequency to keep Ch2 reading around 10000
-						frequency = frequency + 10000 - Ch2;
-						SetCtrlVal(MainPanel, PANEL_DDS3, frequency);
-						DDS4xAD9912_SetFrequency(&DDS4xAD9912, 3, frequency);
-						CenteringTimeBegin10K = utc;
+					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 = DDS4xAD9912_GetFrequency(&DDS4xAD9912, 2);
+							freq = freq + 275000.0 - rollmean_ch4.mean;
+							SetCtrlVal(MainPanel, PANEL_DDS2, freq);
+							DDS4xAD9912_SetFrequency(&DDS4xAD9912, 2, freq);
+						}
+					
+						if (recenter.Hg) {
+							// adjust DDS3 frequency to keep Ch2 reading at 10 kHz
+							double freq = DDS4xAD9912_GetFrequency(&DDS4xAD9912, 3);
+							freq = freq + 10000 - rollmean_ch2.mean;
+							SetCtrlVal(MainPanel, PANEL_DDS3, freq);
+							DDS4xAD9912_SetFrequency(&DDS4xAD9912, 3, freq);
+						}
+					
+						if (recenter.Sr) {
+							// adjust DDS3 frequency to keep Ch3 reading at 10 kHz
+							double freq = DDS4xAD9912_GetFrequency(&DDS4xAD9912, 3);
+							freq = freq + 10000 - rollmean_ch3.mean;
+							SetCtrlVal(MainPanel, PANEL_DDS3, freq);
+							DDS4xAD9912_SetFrequency(&DDS4xAD9912, 3, freq);
+						}
+					
+						recenter.t0 = utc;
+						rollmean_zero(&rollmean_ch2, ROLLMEAN_N_OBS);
+						rollmean_zero(&rollmean_ch3, ROLLMEAN_N_OBS);
+						rollmean_zero(&rollmean_ch4, ROLLMEAN_N_OBS);
 					}
 				}
 				
@@ -1955,7 +1958,8 @@
 		void *callbackData, int eventData1, int eventData2)
 {
 	
-	switch (event) {
+	switch (event)
+	{
 		case EVENT_COMMIT:
 			switch (control)
 			{
@@ -2024,7 +2028,8 @@
  int  CVICALLBACK CB_OnChangeNdiv (int panel, int control, int event, 
 		 void *callbackData, int eventData1, int eventData2)
 {
-	switch (event) {
+	switch (event)
+	{
 		case EVENT_COMMIT:
 			GetCtrlVal(MainPanel, PANEL_CHANGENDIV, &Ndiv);
 			FrequDDS1 = 880000000.0 / Ndiv;
@@ -2039,36 +2044,26 @@
  int  CVICALLBACK CB_MeasureSlope (int panel, int control, int event, 
 		 void *callbackData, int eventData1, int eventData2)
 {
-	switch (event) {
+	switch (event)
+	{
 		case EVENT_COMMIT:
-				   
-			GetCtrlVal(panel, control, &SlopeMeasuring);
-			if (SlopeMeasuring) {
-				
-				SlopeMeasuringTimeBegin = utc;
-				
+			GetCtrlVal(panel, control, &dedrift.enabled);
+			if (dedrift.enabled) {
+				dedrift.t0 = utc;
 				stat_zero(&freq);
-				
-				Nratio = -1;
-				MoyMath2 = 0.0; 
-				CenterFrequencyCh2ToDetermine = TRUE ;
-				CenterFrequencyCh2 = 0.0;
-				
-				logmsg("Start slope measurement");
-				
+				logmsg("dedrift start");
 			} else {
-				if (! KeepSlope) {
-					appliedSlope = 0.0;
-					DDSFox_SetSweepRate(&DDS1xAD9956, appliedSlope);
-					SetCtrlVal(MainPanel, PANEL_SLOPE_APPLIED, appliedSlope);
+				if (! dedrift.keep_slope) {
+					dedrift.applied = 0.0;
+					DDSFox_SetSweepRate(&DDS1xAD9956, dedrift.applied);
+					SetCtrlVal(MainPanel, PANEL_SLOPE_APPLIED, dedrift.applied);
 				}
-				if (! KeepFrequ) {
-					DDSFox_Set(&DDS1xAD9956, dedrift_DDS_freq, appliedSlope);
+				if (! dedrift.keep_freq) {
+					DDSFox_Set(&DDS1xAD9956, dedrift.freq0, dedrift.applied);
 				}
 				stat_zero(&freq);
 				SetCtrlVal(panel, PANEL_SLOPE_MEASURED, freq.slope);
-				
-				logmsg("Stop slope measurement");
+				logmsg("dedrift stop");
 			}
 			break;
 	}
@@ -2080,16 +2075,13 @@
 		void *callbackData, int eventData1, int eventData2)
 {
 
-	switch (event) {
+	switch (event)
+	{
 		case EVENT_COMMIT:
-			CenterFrequencyCh2 = 0.0;
-			MoyMath2 = 0.0;
-			Nratio = -1;
-			CenterFrequencyCh2ToDetermine = TRUE ;
-			appliedSlope = 0.0; 
-			SetCtrlVal(panel, PANEL_SLOPE_APPLIED, appliedSlope);
-			DDSFox_Set(&DDS1xAD9956, dedrift_DDS_freq, appliedSlope);
-			logmsg("Reset slope measurement");
+			dedrift.applied = 0.0; 
+			SetCtrlVal(panel, PANEL_SLOPE_APPLIED, dedrift.applied);
+			DDSFox_Set(&DDS1xAD9956, dedrift.freq0, dedrift.applied);
+			logmsg("dedrift reset");
 			break;
 	}
 	return 0;
@@ -2100,9 +2092,10 @@
 		void *callbackData, int eventData1, int eventData2)
 {
 
-	switch (event) {
+	switch (event)
+	{
 		case EVENT_COMMIT:
-			GetCtrlVal(MainPanel, PANEL_SLOPETIME, &TimetoSlope); 
+			GetCtrlVal(MainPanel, PANEL_SLOPETIME, &dedrift.interval); 
 			break;
 	}
 	return 0;
@@ -2112,23 +2105,24 @@
 int  CVICALLBACK CB_OnCROX (int panel, int control, int event, 
 		 void *callbackData, int eventData1, int eventData2)
 {
-	switch (event) {
+	switch (event)
+	{
 		case EVENT_COMMIT:
 			switch (control) {
 				
 				case PANEL_CHECKBOX_CORRFREQU:
 					// enable frequency correction
-				   	GetCtrlVal(MainPanel, PANEL_CHECKBOX_CORRFREQU, &FrequCorrec);
+				   	// GetCtrlVal(MainPanel, PANEL_CHECKBOX_CORRFREQU, &FrequCorrec);
 					break;
 			
 				case PANEL_CHECKBOX_KEEP:
 					// keep current dedrifting frequency when dedrifting is disabled
-				   	GetCtrlVal(MainPanel, PANEL_CHECKBOX_KEEP, &KeepFrequ);
+				   	GetCtrlVal(MainPanel, PANEL_CHECKBOX_KEEP, &dedrift.keep_freq);
 					break;
 					
 				case PANEL_CHECKBOX_KEEPSLOPE:
 					// keep current dedrifting slope when dedrifting is disabled
-				   	GetCtrlVal(MainPanel, PANEL_CHECKBOX_KEEPSLOPE, &KeepSlope);
+				   	GetCtrlVal(MainPanel, PANEL_CHECKBOX_KEEPSLOPE, &dedrift.keep_slope);
 					break;
 			}
  			break;
@@ -2141,28 +2135,13 @@
 int  CVICALLBACK CB_OnReCentering (int panel, int control, int event, 
 		 void *callbackData, int eventData1, int eventData2)
 {
-		bool BoxChecked = FALSE; 
-
 	switch (event)
-		{
+	{
 		case EVENT_COMMIT:
-			
-			if (control== PANEL_CHECKBOX_RECENTER)  
-			{
-				   	GetCtrlVal(MainPanel, PANEL_CHECKBOX_RECENTER, &BoxChecked) ;
-						if (BoxChecked) {
-							KeepCentering=TRUE ;
-							CenteringTimeBegin275K=utc;
-							CenteringTimeBegin10K=utc;
-							}
-						else {KeepCentering=FALSE ;}
-							
-			}
-			
-			
+			GetCtrlVal(panel, control, &recenter.enabled);
+			recenter.t0 = 0.0;
  			break;
-			
-		}
+	}
 	return 0;
 }
 
@@ -2170,10 +2149,12 @@
 int  CVICALLBACK CB_OnStopSlopeCancellingOnUnlocked (int panel, int control, int event, 
 		 void *callbackData, int eventData1, int eventData2)
 {
+	int value;
 	switch (event)
 	{
 		case EVENT_COMMIT:
-			GetCtrlVal(MainPanel, PANEL_CHECKBOX_STOPIFAUTODE, &StopSlopeCancellingOnUnlocked);
+			GetCtrlVal(MainPanel, PANEL_CHECKBOX_STOPIFAUTODE, &value);
+			dedrift.threshold = value ? 100.0 : 0.0;
 			break;
 	}
 	return 0;
@@ -2185,7 +2166,7 @@
 	switch (event)
 	{
 		case EVENT_COMMIT:
-			GetCtrlVal(MainPanel, PANEL_SLOPE_REFERENCE, &slopeReference);
+			GetCtrlVal(MainPanel, PANEL_SLOPE_REFERENCE, &dedrift.reference);
 			break;
 	}
 	return 0;
@@ -2278,8 +2259,8 @@
 	switch (event)
 	{
 		case EVENT_COMMIT:
-			GetCtrlVal(panel, control, &appliedSlope);
-			DDSFox_SetSweepRate(&DDS1xAD9956, appliedSlope);
+			GetCtrlVal(panel, control, &dedrift.applied);
+			DDSFox_SetSweepRate(&DDS1xAD9956, dedrift.applied);
 			break;
 	}
 	return 0;
@@ -2291,7 +2272,7 @@
 	switch (event)
 	{
 		case EVENT_COMMIT:
-			GetCtrlVal(panel, control, &invertSlopeSign);
+			GetCtrlVal(panel, control, &dedrift.invert);
 			break;
 	}
 	return 0;
@@ -2304,19 +2285,15 @@
 	{
 		case EVENT_COMMIT:
 			// stop slope measurement and reset slope 
-			SlopeMeasuring = 0;
-			SetCtrlVal(panel, PANEL_MEASURE_SLOPE, SlopeMeasuring);
-			CenterFrequencyCh2 = 0.0;
-			MoyMath2 = 0.0;
-			Nratio = -1;
-			CenterFrequencyCh2ToDetermine = TRUE ;
-			appliedSlope = 0.0;
-			SetCtrlVal(panel, PANEL_SLOPE_APPLIED, appliedSlope);
+			dedrift.enabled = FALSE;
+			SetCtrlVal(panel, PANEL_MEASURE_SLOPE, 0);
+			dedrift.applied = 0.0;
+			SetCtrlVal(panel, PANEL_SLOPE_APPLIED, dedrift.applied);
 			// reset DDS
 			DDSFox_Reset(&DDS1xAD9956);
 			DDSFox_SetProfile(&DDS1xAD9956);
 			DDSFox_SetDiv(&DDS1xAD9956, 1);
-			DDSFox_Set(&DDS1xAD9956, dedrift_DDS_freq, appliedSlope);
+			DDSFox_Set(&DDS1xAD9956, dedrift.freq0, 0.0);
 			break;
 	}
 	return 0;
@@ -2367,7 +2344,7 @@
 	switch (event)
 	{
 		case EVENT_COMMIT:
-			GetCtrlVal(panel, control, &slopeX2);
+			GetCtrlVal(panel, control, &dedrift.doubleslope);
 			break;
 	}
 	return 0;
@@ -2379,8 +2356,8 @@
 	switch (event)
 	{
 		case EVENT_COMMIT:
-			GetCtrlVal(panel, control, &dedrift_DDS_freq);
-			DDSFox_Set(&DDS1xAD9956, dedrift_DDS_freq, appliedSlope);
+			GetCtrlVal(panel, control, &dedrift.freq0);
+			DDSFox_Set(&DDS1xAD9956, dedrift.freq0, dedrift.applied);
 			break;
 	}
 	return 0;