diff FXAnalyse.c @ 46:6503feae1809

Cleanup. Convert N2 measurement logic to state machine like structure
author Daniele Nicolodi <daniele.nicolodi@obspm.fr>
date Wed, 10 Oct 2012 12:27:29 +0200
parents 2e37910b28bc
children 1d3cd8260cbb
line wrap: on
line diff
--- a/FXAnalyse.c	Wed Oct 10 12:14:01 2012 +0200
+++ b/FXAnalyse.c	Wed Oct 10 12:27:29 2012 +0200
@@ -73,7 +73,7 @@
 int settling = 0;
 
 int Measuring_1 = FALSE;
-int Measuring_2 = FALSE, Step1_2 = FALSE, Step2_2 = FALSE, Step3_2 = FALSE;
+int Measuring_2 = FALSE;											  
 int Measuring_3 = FALSE;
 
 double FrequDDS1=110000000.0, FrequDDS2=15300000.0, FrequDDS3=150400000.0, FrequDDS4=110000000.0;
@@ -93,7 +93,6 @@
 
 double FrequencyDDSBes=0.0,FrequencyDDSBesInit=0.0;
 double FrequencyDDS3=0.0,FrequencyDDS3Init=0.0;
-double DDSBesChanged1=FALSE,DDSBesChanged2=FALSE;
 
 double DeltaDDS3=0.0,Delta10K_Plus=0.0,Delta10K_Minus=0.0;
 double Nu1=0.0, Nu2= 200000-147000+282143746.557455e6;  
@@ -102,8 +101,6 @@
 
 double Ch4Plus=0.0,Ch4Minus=0.0;
 
-int nDDSChange=0, nstabilization=0;
-
 double Frequencystep1=10000.0, tbegin1=0.0, Frepbefore1=0.0, Frequency1=0.0;
 double Frequencystep2=10.0, tbegin2=0.0, Frepbefore2=0.0, Ch2before=0.0, Frequency2=0.0;
 double Frequencystep3=100000.0, tbegin3=0.0, Frepbefore3=0.0, Frequency3=0.0;
@@ -201,6 +198,7 @@
 	mupDefinePostfixOprt( * pMathParser, "f", &femto, 1);
 }
 
+
 int main (int argc, char *argv[])
 {
 	double initfreqDDS ;
@@ -603,13 +601,12 @@
 				SetCtrlVal(MainPanel, PANEL_FREQ3, Ch3);  
 				SetCtrlVal(MainPanel, PANEL_FREQ4, Ch4);
 				SetCtrlVal(MainPanel, PANEL_SIGN1, Signe1);
-				SetCtrlVal(MainPanel, PANEL_SIGN2, Signe2); 
-				SetCtrlVal(MainPanel, PANEL_SIGN3, Signe3); 
-				SetCtrlVal(CalcN2Panel, CALCN2_N, N_2);
-				SetCtrlVal(CalcN2Panel, CALCN2_SLOPE, Beatslope_2);
-				SetCtrlVal(MainPanel, PANEL_CENTERFREQUENCY, CenterFrequencyCh2);  
-				SetCtrlVal(MainPanel, PANEL_LED1, SlopeMeasuring); 	   
+				SetCtrlVal(MainPanel, PANEL_SIGN2, Signe2);
+				SetCtrlVal(MainPanel, PANEL_SIGN3, Signe3);
+				SetCtrlVal(MainPanel, PANEL_CENTERFREQUENCY, CenterFrequencyCh2);
+				SetCtrlVal(MainPanel, PANEL_LED1, SlopeMeasuring);
 				SetCtrlVal(MainPanel, PANEL_LED2, OnSlopeCancelling);
+				
 				// Treat data
 				
 				Math1 = mupEval(MathParser1);
@@ -772,7 +769,7 @@
 							
 							// next step
 							Measuring_1 += 1;
-							}
+						}
 						break;
 						
 					case N_MEASUREMENT_ADJUST_FREQ_PLUS:
@@ -872,197 +869,186 @@
 						}
 						break;
 				}
-				
-				if (Measuring_2==TRUE)
-				{
-					if (Step1_2==FALSE) {
+
+				switch (Measuring_2) {
+
+					case N_MEASUREMENT_NONE:
+						// not measuring
+						break;
+					
+					case N_MEASUREMENT_INIT:
+						// initialization step
+						
+						// set DDS1 to nominal frequency
 						SetCtrlVal(MainPanel, PANEL_DDS1, FrequDDS1);
 						DDS4xAD9912_SetFrequency(&DDS4xAD9912, 1, FrequDDS1);
 						
 						GetCtrlVal(MainPanel, PANEL_DDS2, &FrequencyDDSBesInit);
 						GetCtrlVal(MainPanel, PANEL_DDS3, &FrequencyDDS3Init);
-						t1_2=utc;
-						Frequ_slope_2=Math1;
-						Beat_slope_2=Ch2;
-						Moy_slope_2= Frequ_slope_2;
-						Moy_Beatslope_2= Beat_slope_2;
-						N_slope_2=1;
-						Step1_2=TRUE;
-						Nu1=(250000000+Math1)*N1;
-					}
-					else {
-						if(Step2_2==FALSE){
-							N_slope_2=N_slope_2+1; 
-							Frequ_slope_2=Math1;
-							Beat_slope_2=Ch2;  
-							Moy_slope_2=((N_slope_2-1)*Moy_slope_2 + Frequ_slope_2)/N_slope_2;
-							Moy_Beatslope_2=((N_slope_2-1)*Moy_Beatslope_2 + Beat_slope_2)/N_slope_2;
-							Slope_slope_2 = (Slope_slope_2*(N_slope_2-2) + 6*(Frequ_slope_2-Moy_slope_2)/N_slope_2)/(N_slope_2+1);
-							Slope_Beatslope_2 = (Slope_Beatslope_2*(N_slope_2-2) + 6*(Beat_slope_2-Moy_Beatslope_2)/N_slope_2)/(N_slope_2+1);
-							if (utc-t1_2>SlopeTime2) {
-								Slope_2 = Slope_slope_2;
-								Beatslope_2 = Slope_Beatslope_2;
-								Step2_2=TRUE;
-								N_slope_2=0;
-								Frequ_slope_2=0.0;
-								Moy_slope_2=0.0;
-								Slope_slope_2 =0.0;
-								Moy_Beatslope_2=0.0;
-								Slope_Beatslope_2 =0.0;
-								Beat_slope_2=0.0;
-								
-								DDS4xAD9912_FrequencyRampe (&DDS4xAD9912,1, FrequDDS1,(FrequDDS1+DeltakHz_2*1000), Step2/Ndiv );
-								SetCtrlVal(MainPanel, PANEL_DDS1, (FrequDDS1+DeltakHz_2*1000));   
-								DDS4xAD9912_SetFrequency(&DDS4xAD9912, 1, (FrequDDS1+DeltakHz_2*1000));
-								Delay(0.1); 
-								DeltaDDS3 = -DeltakHz_2*1000*(-Signe1/Signe2)*Ndiv*(Nu2)/(Nu1) - Beatslope_2*(utc-t1_2);
-								SetCtrlVal(MainPanel, PANEL_DDS3, (FrequencyDDS3Init+DeltaDDS3)); 
-								DDS4xAD9912_SetFrequency (&DDS4xAD9912,3,(FrequencyDDS3Init+DeltaDDS3));
-								
-							}
+						t1_2 = utc;
+						Frequ_slope_2 = Math1;
+						Beat_slope_2 = Ch2;
+						Moy_slope_2 = Frequ_slope_2;
+						Moy_Beatslope_2 = Beat_slope_2;
+						N_slope_2 = 1;
+						Nu1 = N1 * (250000000 + Math1);
+						
+						// next step
+						Measuring_2 += 1;
+						break;
+						
+					case N_MEASUREMENT_SLOPE:
+						// slope measurement
+						
+						N_slope_2 = N_slope_2 + 1; 
+						Frequ_slope_2 = Math1;
+						Beat_slope_2 = Ch2;  
+						Moy_slope_2 = ((N_slope_2-1)*Moy_slope_2 + Frequ_slope_2)/N_slope_2;
+						Moy_Beatslope_2 = ((N_slope_2-1)*Moy_Beatslope_2 + Beat_slope_2)/N_slope_2;
+						Slope_slope_2 = (Slope_slope_2*(N_slope_2-2) + 6*(Frequ_slope_2-Moy_slope_2)/N_slope_2)/(N_slope_2+1);
+						Slope_Beatslope_2 = (Slope_Beatslope_2*(N_slope_2-2) + 6*(Beat_slope_2-Moy_Beatslope_2)/N_slope_2)/(N_slope_2+1);
+
+						if ((utc - t1_2) > SlopeTime2) {
+							Slope_2 = Slope_slope_2;
+							Beatslope_2 = Slope_Beatslope_2;
+							SetCtrlVal(CalcN2Panel, CALCN2_SLOPE, Beatslope_2);
+
+							N_slope_2 = 0;
+							Frequ_slope_2 = 0.0;
+							Moy_slope_2 = 0.0;
+							Slope_slope_2 = 0.0;
+							Moy_Beatslope_2 = 0.0;
+							Slope_Beatslope_2 = 0.0;
+							Beat_slope_2 = 0.0;
+							
+							// frep positive step
+							double fDDS1 = FrequDDS1 + DeltakHz_2 * 1000;
+							DDS4xAD9912_FrequencyRampe(&DDS4xAD9912,1, FrequDDS1, fDDS1, Step2/Ndiv);
+							SetCtrlVal(MainPanel, PANEL_DDS1, fDDS1);
+							DDS4xAD9912_SetFrequency(&DDS4xAD9912, 1, fDDS1);
+							DeltaDDS3 = -DeltakHz_2*1000*(-Signe1/Signe2)*Ndiv*(Nu2)/(Nu1) - Beatslope_2*(utc-t1_2);
+							SetCtrlVal(MainPanel, PANEL_DDS3, FrequencyDDS3Init + DeltaDDS3);
+							DDS4xAD9912_SetFrequency(&DDS4xAD9912, 3, FrequencyDDS3Init + DeltaDDS3);
+						
+							// allow counter to settle
+							settling = 3;
+							
+							// next step
+							Measuring_2 += 1;
+						}
+						break;
+
+					case N_MEASUREMENT_ADJUST_FREQ_PLUS:
+					case N_MEASUREMENT_ADJUST_FREQ_MINUS:
+						// adjust DDS frequency to keep beatnote within the bandpass filter
+						
+						if (settling > 0) {
+							settling--;
+							break;
 						}
-						else {
-							if (DDSBesChanged1==FALSE){
-								
-								if (nDDSChange<3)
-								{ nDDSChange=nDDSChange+1;}
-								
-								else
-								{
-									nDDSChange=0;
-									
-									double DeltaFrep275 = 275000-Ch4;
-									double DeltaFrep10 = 10000-Ch2;
-									DeltaDDS3 = DeltaDDS3 + DeltaFrep10;
-									FrequencyDDSBes = FrequencyDDSBesInit + DeltaFrep275;
-									FrequencyDDS3 = FrequencyDDS3Init + DeltaDDS3;
-									
-									SetCtrlVal(MainPanel, PANEL_DDS2, FrequencyDDSBes);
-									DDS4xAD9912_SetFrequency(&DDS4xAD9912,2,FrequencyDDSBes);
-									
-									Delay(0.1); 
-									SetCtrlVal(MainPanel, PANEL_DDS3, FrequencyDDS3);    
-									DDS4xAD9912_SetFrequency(&DDS4xAD9912,3,FrequencyDDS3);
-									
-									DDSBesChanged1=TRUE;
-									t2_2=utc;
-									
-								}
-								
-							}
-							else{
-								
-								if(Step3_2==FALSE){
-									if (nstabilization<3)
-									{nstabilization= nstabilization+1;}
-									else
-									{
-										if (utc-t2_2<DeltaT_2) {
-											Frepplus_2=Frepplus_2 +Math1+250000000-Slope_2*(utc-t2_2);
-											Delta10K_Plus= Delta10K_Plus + 10000 - (Ch2 -Beatslope_2*(utc-t2_2));
-											n_2=n_2+1;
-										}
-										else
-										{
-											Frepplus_2=Frepplus_2/n_2;
-											Delta10K_Plus=Delta10K_Plus/n_2;
-											n_2=0;
-											Step3_2=TRUE;
-											nstabilization=0;
-											DDS4xAD9912_FrequencyRampe ( &DDS4xAD9912,1, (FrequDDS1+DeltakHz_2*1000),(FrequDDS1-DeltakHz_2*1000), Step2/Ndiv );
-											SetCtrlVal(MainPanel, PANEL_DDS1, (FrequDDS1-DeltakHz_2*1000)); 
-											DDS4xAD9912_SetFrequency (&DDS4xAD9912,1, (FrequDDS1-DeltakHz_2*1000) );
-											
-											Delay(0.1); 
-											DeltaDDS3 = (FrequencyDDS3Init+DeltakHz_2*1000*(-Signe1/Signe2)*Ndiv*(Nu2)/(Nu1)) - FrequencyDDS3;
-											SetCtrlVal(MainPanel, PANEL_DDS3, FrequencyDDS3+DeltaDDS3); 
-											DDS4xAD9912_SetFrequency (&DDS4xAD9912,3, FrequencyDDS3+DeltaDDS3 ); 
-											
-										}
-									}
-								}
-								
-								else {
-									if (DDSBesChanged2==FALSE){
-										
-										if (nDDSChange<3) {
-											nDDSChange=nDDSChange+1;
-										}
-										else
-										{
-											nDDSChange=0;
+
+						double DeltaFrep275 = 275000 - Ch4;
+						double DeltaFrep10 = 10000 - Ch2;
+						
+						DeltaDDS3 = DeltaDDS3 + DeltaFrep10;
+						FrequencyDDSBes = FrequencyDDSBes + DeltaFrep275;
+						FrequencyDDS3 = FrequencyDDS3 + DeltaDDS3;
+						SetCtrlVal(MainPanel, PANEL_DDS2, FrequencyDDSBes);
+						DDS4xAD9912_SetFrequency(&DDS4xAD9912,2,FrequencyDDSBes);
+						SetCtrlVal(MainPanel, PANEL_DDS3, FrequencyDDS3);
+						DDS4xAD9912_SetFrequency(&DDS4xAD9912,3,FrequencyDDS3);
+
+						// allow counter to settle
+						settling = 3;
+
+						// next step
+						Measuring_2 += 1;
+						break;
+						
+					case N_MEASUREMENT_FREP_PLUS:
+						// frep positive step
+						
+						if (settling > 0) {
+							settling--;
+							break;
+						}
+
+						if (t2_1 == 0.0)
+							t2_1 = utc;
+
+						Frepplus_2 = Frepplus_2 + Math1 + 250000000 - Slope_2 * (utc - t2_2);
+						Delta10K_Plus = Delta10K_Plus + 10000 - (Ch2 - Beatslope_2 * (utc - t2_2));
+						n_2 += 1;
+						
+						if ((utc - t2_2) > DeltaT_2) {
+							Frepplus_2 = Frepplus_2 / n_2;
+							Delta10K_Plus = Delta10K_Plus / n_2;
+							n_2 = 0;
+
+							// negative frequency step
+							DDS4xAD9912_FrequencyRampe(&DDS4xAD9912,1, (FrequDDS1+DeltakHz_2*1000),(FrequDDS1-DeltakHz_2*1000), Step2/Ndiv);
+							SetCtrlVal(MainPanel, PANEL_DDS1, (FrequDDS1-DeltakHz_2*1000));
+							DDS4xAD9912_SetFrequency(&DDS4xAD9912,1, (FrequDDS1-DeltakHz_2*1000));
+							DeltaDDS3 = (FrequencyDDS3Init+DeltakHz_2*1000*(-Signe1/Signe2)*Ndiv*(Nu2)/(Nu1)) - FrequencyDDS3;
+							SetCtrlVal(MainPanel, PANEL_DDS3, FrequencyDDS3+DeltaDDS3);
+							DDS4xAD9912_SetFrequency(&DDS4xAD9912,3, FrequencyDDS3+DeltaDDS3);
 
-											double DeltaFrep275=275000-Ch4;
-											double DeltaFrep10=10000-Ch2;
-											DeltaDDS3 = DeltaDDS3+(DeltaFrep10);
-											FrequencyDDSBes=FrequencyDDSBes+(DeltaFrep275);
-											FrequencyDDS3=FrequencyDDS3+DeltaDDS3; 
-											DDS4xAD9912_SetFrequency(&DDS4xAD9912,2,FrequencyDDSBes);
-											SetCtrlVal(MainPanel, PANEL_DDS2, FrequencyDDSBes);
-											
-											Delay(0.1); 
-											SetCtrlVal(MainPanel, PANEL_DDS3, FrequencyDDS3); 
-											DDS4xAD9912_SetFrequency(&DDS4xAD9912,3,FrequencyDDS3);
-											
-											DDSBesChanged2=TRUE;
-											t3_2=utc;
-										}
-									}
-									else
-									{
-										if (nstabilization<3) {
-											nstabilization=nstabilization+1;
-										}
-										else
-										{
-											if (utc-t3_2<DeltaT_2) {
-												Frepminus_2=Frepminus_2 +Math1+250000000-Slope_2*(utc-t3_2);
-												Delta10K_Minus=  Delta10K_Minus +10000 - ( Ch2 -Beatslope_2*(utc-t3_2));
-												n_2=n_2+1;
-											}
-											else
-											{
-												Frepminus_2=Frepminus_2/(n_2);
-												Delta10K_Minus= Delta10K_Minus/n_2;
-												N_2 = (Signe2)*(-DeltaDDS3+Delta10K_Plus-Delta10K_Minus-Beatslope_2*(t3_2-t2_2) )/(Frepminus_2-Frepplus_2-Slope_2*(t3_2-t2_2));
-												n_2=0;
-												Frepminus_2=0.0;
-												Frepplus_2=0.0;
-												Delta10K_Minus=0.0;
-												Delta10K_Plus=0.0;  
-												DDS4xAD9912_FrequencyRampe (&DDS4xAD9912, 1, FrequDDS1-DeltakHz_2*1000,FrequDDS1, Step2/Ndiv );
-												SetCtrlVal(MainPanel, PANEL_DDS1, FrequDDS1);
-												DDS4xAD9912_SetFrequency(&DDS4xAD9912,1,FrequDDS1);
-												
-												Delay(0.1); 
-												
-												SetCtrlVal(MainPanel, PANEL_DDS2, FrequencyDDSBesInit);  
-												DDS4xAD9912_SetFrequency (&DDS4xAD9912, 2, FrequencyDDSBesInit );
-												
-												Delay(0.1); 
-												
-												SetCtrlVal(MainPanel, PANEL_DDS3, FrequencyDDS3Init-Beatslope_2*(utc-t1_2));
-												DDS4xAD9912_SetFrequency (&DDS4xAD9912, 3, FrequencyDDS3Init-Beatslope_2*(utc-t1_2) );
-												
-												Measuring_2=FALSE;
-												Step1_2=FALSE;
-												Step2_2=FALSE;
-												Step3_2=FALSE;
-												t1_2=0.0;
-												t2_2=0.0;
-												t3_2=0.0;
-												DDSBesChanged1=FALSE;
-												DDSBesChanged2=FALSE;
-												FrequencyDDSBes=0.0;
-												nstabilization=0;
-												
-											}
-										}
-									}
-								}
-							}
+							// allow counter to settle
+							settling = 3;
+							
+							// next step
+							Measuring_2 += 1;
+						}
+						break;
+						
+					case N_MEASUREMENT_FREP_MINUS:
+						// frep negative step
+						
+						if (settling > 0) {
+							settling--;
+							break;
 						}
-					}
+						
+						if (t3_1 == 0.0)
+							t3_1 = utc;
+
+						Frepminus_2 = Frepminus_2 + Math1 + 250000000 - Slope_2 * (utc - t3_2);
+						Delta10K_Minus=  Delta10K_Minus + 10000 - (Ch2 - Beatslope_2 * (utc - t3_2));
+						n_2 += 1;
+
+						if ((utc -t3_2) > DeltaT_2) {
+							Frepminus_2 = Frepminus_2 / n_2;
+							Delta10K_Minus = Delta10K_Minus / n_2;
+							n_2 = 0;
+
+							// compute N2
+							N_2 = (Signe2)*(-DeltaDDS3+Delta10K_Plus-Delta10K_Minus-Beatslope_2*(t3_2-t2_2) )/(Frepminus_2-Frepplus_2-Slope_2*(t3_2-t2_2));
+							SetCtrlVal(CalcN2Panel, CALCN2_N, N_2);
+							
+							// back to nominal frequency
+							DDS4xAD9912_FrequencyRampe (&DDS4xAD9912, 1, FrequDDS1-DeltakHz_2*1000,FrequDDS1, Step2/Ndiv );
+							SetCtrlVal(MainPanel, PANEL_DDS1, FrequDDS1);
+							DDS4xAD9912_SetFrequency(&DDS4xAD9912,1,FrequDDS1);
+							Delay(0.1); 
+							SetCtrlVal(MainPanel, PANEL_DDS2, FrequencyDDSBesInit);  
+							DDS4xAD9912_SetFrequency (&DDS4xAD9912, 2, FrequencyDDSBesInit );
+							Delay(0.1); 
+							SetCtrlVal(MainPanel, PANEL_DDS3, FrequencyDDS3Init-Beatslope_2*(utc-t1_2));
+							DDS4xAD9912_SetFrequency (&DDS4xAD9912, 3, FrequencyDDS3Init-Beatslope_2*(utc-t1_2) );
+							
+							t1_2 = 0.0;
+							t2_2 = 0.0;
+							t3_2 = 0.0;
+							Frepminus_2 = 0.0;
+							Frepplus_2 = 0.0;
+							Delta10K_Minus = 0.0;
+							Delta10K_Plus = 0.0;
+							FrequencyDDSBes = 0.0;
+
+							// done
+							Measuring_2 = N_MEASUREMENT_NONE;
+						}
+						break;
 				}
 				
 				switch (Measuring_3) {
@@ -2043,15 +2029,11 @@
 				Ch4_slope=0.0;
 				Moy_Ch4slope_1=0.0;
 				Slope_Ch4slope_1=0.0;
-				DDSBesChanged1=FALSE;
-				DDSBesChanged2=FALSE; 
-				nDDSChange=0.0;
 					
 				SetCtrlVal(MainPanel, PANEL_DDS1, FrequDDS1) ;
 				DDS4xAD9912_SetFrequency(&DDS4xAD9912, 1, FrequDDS1);
 				SetCtrlVal(MainPanel, PANEL_DDS2, FrequencyDDSBesInit) ;
 				DDS4xAD9912_SetFrequency(&DDS4xAD9912, 2, FrequencyDDSBesInit); 
-				nstabilization=0;
 			}	
 			if (panel == CalcN2Panel) {
 				GetPanelAttribute (CalcN2Panel, ATTR_VISIBLE, &PanelIsVisible);
@@ -2059,9 +2041,6 @@
 					HidePanel(CalcN2Panel);
 				
 				Measuring_2=FALSE;
-				Step1_2=FALSE ;
-				Step2_2=FALSE ;
-				Step3_2=FALSE ;
 				Frepminus_2=0.0;
 				Delta10K_Minus=0.0;
 				Frepplus_2=0.0;
@@ -2077,9 +2056,6 @@
 				Moy_Beatslope_2=0.0;
 				Slope_Beatslope_2 =0.0;
 				Slope_slope_2 =0.0;
-				DDSBesChanged1=FALSE;
-				DDSBesChanged2=FALSE; 
-				nDDSChange=0.0;
 				
 				SetCtrlVal(MainPanel, PANEL_DDS1, FrequDDS1) ;
 				DDS4xAD9912_SetFrequency(&DDS4xAD9912, 1, FrequDDS1);
@@ -2087,7 +2063,6 @@
 				DDS4xAD9912_SetFrequency(&DDS4xAD9912, 2, FrequencyDDSBesInit);
 				SetCtrlVal(MainPanel, PANEL_DDS3, FrequencyDDS3Init) ;
 				DDS4xAD9912_SetFrequency(&DDS4xAD9912, 3, FrequencyDDS3Init); 
-				nstabilization=0;
 			}
 			if (panel == CalcN3Panel) {		
 				GetPanelAttribute (CalcN3Panel, ATTR_VISIBLE, &PanelIsVisible);