diff FXAnalyse.c @ 26:4bb197635664

Cleanup N1 measurement code
author Daniele Nicolodi <daniele.nicolodi@obspm.fr>
date Tue, 02 Oct 2012 16:31:32 +0200
parents fde495ba28d2
children 419db63a9949
line wrap: on
line diff
--- a/FXAnalyse.c	Tue Oct 02 12:23:31 2012 +0200
+++ b/FXAnalyse.c	Tue Oct 02 16:31:32 2012 +0200
@@ -78,6 +78,16 @@
 	N_MEASUREMENT_STEP_4,
 };
 
+enum {
+	N_MEASUREMENT_NONE,
+	N_MEASUREMENT_INIT,
+	N_MEASUREMENT_SLOPE,
+	N_MEASUREMENT_ADJUST_FREQ_PLUS,
+	N_MEASUREMENT_FREP_PLUS,
+	N_MEASUREMENT_ADJUST_FREQ_MINUS,
+	N_MEASUREMENT_FREP_MINUS,
+};
+
 int settling = 0;
 
 volatile bool Measuring_1 = FALSE,Step1_1 = FALSE,Step2_1 = FALSE,Step3_1 = FALSE, Measuring_2 = FALSE,Step1_2 = FALSE,Step2_2 = FALSE,Step3_2 = FALSE;
@@ -783,177 +793,166 @@
 						if (BoxChecked) {
 							Allan_AddFrequency(&AllanMath5, Math5) ;
 							}
+						
 						// Calcul de N
 						
-						if (Measuring_1==TRUE)
-							{
-								if (Step1_1==FALSE) {
-									SetCtrlVal(MainPanel, PANEL_DDS1, FrequDDS1) ;
-									DDS4xAD9912_SetFrequency(&DDS4xAD9912, 1, FrequDDS1);
-									
-									GetCtrlVal(MainPanel, PANEL_DDS2, &FrequencyDDSBesInit) ;
-									t1_1=utc;
-									Frequ_slope_1=Math1;
-									Moy_slope_1= Frequ_slope_1;
-									Ch4_slope=Ch4;
-									Moy_Ch4slope_1= Ch4_slope;
-									N_slope_1=1;
-									Step1_1=TRUE ;
-									}
-								else {
-									if(Step2_1==FALSE){
-										N_slope_1=N_slope_1+1; 
-										Frequ_slope_1=Math1;
-										Ch4_slope=Ch4; 
-										Moy_slope_1=((N_slope_1-1)*Moy_slope_1 + Frequ_slope_1)/N_slope_1;
-										Moy_Ch4slope_1=((N_slope_1-1)*Moy_Ch4slope_1 + Ch4_slope)/N_slope_1; 
-										Slope_slope_1 = (Slope_slope_1*(N_slope_1-2) + 6*(Frequ_slope_1-Moy_slope_1)/N_slope_1)/(N_slope_1+1) ;
-										Slope_Ch4slope_1 = (Slope_Ch4slope_1*(N_slope_1-2) + 6*(Ch4_slope-Moy_Ch4slope_1)/N_slope_1)/(N_slope_1+1) ;
-										if (utc-t1_1>SlopeTime1) {
-											Slope_1 = Slope_slope_1;
-											Ch4Slope =  Slope_Ch4slope_1;
-											Step2_1=TRUE ;
-											N_slope_1=0;
-											Frequ_slope_1=0.0;
-											Moy_slope_1=0.0;
-											Slope_slope_1 =0.0;
-											Ch4_slope=0.0;
-											Moy_Ch4slope_1=0.0;
-											Slope_Ch4slope_1=0.0;
-											
-											DDS4xAD9912_FrequencyRampe (&DDS4xAD9912,1, FrequDDS1,(FrequDDS1+DeltakHz_1*1000), Step1/Ndiv ) ;
-											SetCtrlVal(MainPanel, PANEL_DDS1, (FrequDDS1+DeltakHz_1*1000)) ;
-											DDS4xAD9912_SetFrequency(&DDS4xAD9912, 1, (FrequDDS1+DeltakHz_1*1000)); 
-											
-											}
-										}
-									else {
-									  if (DDSBesChanged1==FALSE){
-										  
-										  		if (nDDSChange<3)
-												{ nDDSChange=nDDSChange+1;}
-												
-												else
-												{
-												nDDSChange=0;
-													
-												DeltaFrep275=275000-Ch4;
-												FrequencyDDSBes=FrequencyDDSBesInit+(DeltaFrep275);
-												SetCtrlVal(MainPanel, PANEL_DDS2, FrequencyDDSBes) ;
-												DDS4xAD9912_SetFrequency(&DDS4xAD9912,2,FrequencyDDSBes);
-												DDSBesChanged1=TRUE;
-												t2_1=utc;
+						switch (Measuring_1) {
+
+							case N_MEASUREMENT_NONE:
+								// not measuring
+								break;
 							
-												}
-										  	
-									  	}
-									   else{
-										  
-										if(Step3_1==FALSE){
-										  if (nstabilization<3)
-										  {nstabilization= nstabilization+1;
-										  TimetoBegin=TRUE;}
-										  else
-										  {
-											if (utc-t2_1<DeltaT_1) {
-													if (TimetoBegin==TRUE) 
-														{
-															t2_1=utc;
-															TimetoBegin=FALSE;
-														}
-													Frepplus_1=Frepplus_1 +Math1-Slope_1*(utc-t2_1);
-													Ch4Plus=Ch4Plus + Ch4 -Ch4Slope*(utc-t2_1);
-													n_1=n_1+1;
-													}
-											else{
-												Frepplus_1=Frepplus_1/n_1;
-												Ch4Plus=Ch4Plus/n_1; 
-												n_1=0;
-												Step3_1=TRUE ;
-												nstabilization=0;
-												DDS4xAD9912_FrequencyRampe ( &DDS4xAD9912,1, (FrequDDS1+DeltakHz_1*1000),(FrequDDS1-DeltakHz_1*1000), Step1/Ndiv ) ;  
-												SetCtrlVal(MainPanel, PANEL_DDS1, (FrequDDS1-DeltakHz_1*1000)) ;  
-												DDS4xAD9912_SetFrequency(&DDS4xAD9912, 1, (FrequDDS1-DeltakHz_1*1000)); 
-												}
-										  }
-										}
-										
-										else {
-											if (DDSBesChanged2==FALSE){
-												
-												if (nDDSChange<3)
-												{ nDDSChange=nDDSChange+1;}
-												
-												else
-												{
-												nDDSChange=0;
-										
-												
-												DeltaFrep275=275000-Ch4;
-												FrequencyDDSBes=FrequencyDDSBes+(DeltaFrep275) ;
-												SetCtrlVal(MainPanel, PANEL_DDS2, FrequencyDDSBes) ;
-												DDS4xAD9912_SetFrequency(&DDS4xAD9912,2,FrequencyDDSBes);   
-												DDSBesChanged2=TRUE;
-												t3_1=utc;
+							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);
+								t2_1 = t3_1 = 0.0;
+								t1_1 = utc;
+								Frequ_slope_1 = Math1;
+								Moy_slope_1 = Frequ_slope_1;
+								Ch4_slope = Ch4;
+								Moy_Ch4slope_1 = Ch4_slope;
+								N_slope_1 = 1;
+								
+								// next step
+								Measuring_1 += 1;
+								break;
+								
+							case N_MEASUREMENT_SLOPE:
+								// slope measurement
+								
+								N_slope_1 = N_slope_1 + 1; 
+								Frequ_slope_1 = Math1;
+								Ch4_slope = Ch4; 
+								Moy_slope_1 = ((N_slope_1-1)*Moy_slope_1 + Frequ_slope_1)/N_slope_1;
+								Moy_Ch4slope_1 = ((N_slope_1-1)*Moy_Ch4slope_1 + Ch4_slope)/N_slope_1;
+								Slope_slope_1 = (Slope_slope_1*(N_slope_1-2) + 6*(Frequ_slope_1-Moy_slope_1)/N_slope_1)/(N_slope_1+1);
+								Slope_Ch4slope_1 = (Slope_Ch4slope_1*(N_slope_1-2) + 6*(Ch4_slope-Moy_Ch4slope_1)/N_slope_1)/(N_slope_1+1);
+								
+								if ((utc - t1_1) > SlopeTime1) {
+									Slope_1 = Slope_slope_1;
+									Ch4Slope =  Slope_Ch4slope_1;
+									
+									N_slope_1 = 0;
+									Frequ_slope_1 = 0.0;
+									Moy_slope_1 = 0.0;
+									Slope_slope_1 = 0.0;
+									Ch4_slope = 0.0;
+									Moy_Ch4slope_1 = 0.0;
+									Slope_Ch4slope_1 = 0.0;
+
+									// frep positive step
+									DDS4xAD9912_FrequencyRampe(&DDS4xAD9912,1, FrequDDS1,(FrequDDS1+DeltakHz_1*1000), Step1/Ndiv );
+									SetCtrlVal(MainPanel, PANEL_DDS1, (FrequDDS1+DeltakHz_1*1000));
+									DDS4xAD9912_SetFrequency(&DDS4xAD9912, 1, (FrequDDS1+DeltakHz_1*1000));
+								
+									// allow counter to settle
+									settling = 3;
+								
+									// next step
+									Measuring_1 += 1;
+								}
+								break;
+								
+							case N_MEASUREMENT_ADJUST_FREQ_PLUS:
+							case N_MEASUREMENT_ADJUST_FREQ_MINUS:
+								// adjust DDS frequency to keep beatnote within the bandpass filter
 
-						
-												}
-											}
-											
-											else{
-											  if (nstabilization<3)
-											  { nstabilization=nstabilization+1;
-											  TimetoBegin=TRUE;}
-											  else
-											  {
-												if (utc-t3_1<DeltaT_1) {
-													if (TimetoBegin==TRUE) 
-														{
-															t3_1=utc;
-															TimetoBegin=FALSE;
-														}
-													
-													Frepminus_1=Frepminus_1 +Math1-Slope_1*(utc-t3_1);
-													Ch4Minus=Ch4Minus+Ch4-Ch4Slope*(utc-t3_1);
-													n_1=n_1+1;
-													}
-												else{
-													Frepminus_1=Frepminus_1/(n_1);
-													Ch4Minus=Ch4Minus/(n_1); 
-													N_1 = Signe1*(2*Ndiv*DeltakHz_1*1000 /*-(Ch4Plus-Ch4Minus)-Ch4Slope*(t3_1-t2_1)*/)/(Frepminus_1-Frepplus_1-Slope_1*(t3_1-t2_1));
-													n_1=0;
-													Frepminus_1=0.0;
-													Frepplus_1=0.0;
-													DDS4xAD9912_FrequencyRampe (&DDS4xAD9912, 1, FrequDDS1-DeltakHz_1*1000,FrequDDS1, Step1/Ndiv ) ;
-													SetCtrlVal(MainPanel, PANEL_DDS1, FrequDDS1) ;
-													DDS4xAD9912_SetFrequency(&DDS4xAD9912,1,FrequDDS1);   
-													DDS4xAD9912_SetFrequency(&DDS4xAD9912,2,FrequencyDDSBesInit); 
-													SetCtrlVal(MainPanel, PANEL_DDS2, FrequencyDDSBesInit) ;
-													Measuring_1=FALSE ;
-													Step1_1=FALSE ;
-													Step2_1=FALSE ;
-													Step3_1=FALSE ;
-													t1_1=0.0;
-													t2_1=0.0;
-													t3_1=0.0;
-													DDSBesChanged1=FALSE;
-													DDSBesChanged2=FALSE;
-													FrequencyDDSBes=0.0;
-													nstabilization=0;
-													
-													}
-											  }
-											}
-										}
-										}
-										}
-									}
-							}
-						
-						
-						
-						
-						
+								if (settling > 0) {
+									settling--;
+									break;
+								}
+								
+								DeltaFrep275 = 275000 - Ch4;
+								double fDDS2 = FrequencyDDSBesInit + (DeltaFrep275);
+								SetCtrlVal(MainPanel, PANEL_DDS2, fDDS2) ;
+								DDS4xAD9912_SetFrequency(&DDS4xAD9912, 2, fDDS2);
+								
+								// allow counter to settle
+								settling = 3;
+								
+								// next step
+								Measuring_1 += 1;
+								break;								
+								
+
+							case N_MEASUREMENT_FREP_PLUS:
+								// frep positive step
+								
+								if (settling > 0) {
+									settling--;
+									break;
+								}
+								
+								if (t2_1 == 0.0)
+									t2_1 = utc;
+								
+								Frepplus_1 = Frepplus_1 + Math1 - Slope_1 * (utc - t2_1);
+								Ch4Plus = Ch4Plus + Ch4 - Ch4Slope * (utc - t2_1);
+								n_1 += 1;
+									
+								if ((utc - t2_1) > DeltaT_1) {
+									Frepplus_1 = Frepplus_1 / n_1;
+									Ch4Plus = Ch4Plus / n_1; 
+									n_1 = 0;
+									
+									// frep negative step
+									DDS4xAD9912_FrequencyRampe(&DDS4xAD9912,1, (FrequDDS1+DeltakHz_1*1000),(FrequDDS1-DeltakHz_1*1000), Step1/Ndiv);
+									SetCtrlVal(MainPanel, PANEL_DDS1, (FrequDDS1-DeltakHz_1*1000));  
+									DDS4xAD9912_SetFrequency(&DDS4xAD9912, 1, (FrequDDS1-DeltakHz_1*1000));
+									
+									// allow counter to settle
+									settling = 3;
+								
+									// next step
+									Measuring_1 += 1;
+								}
+								break;
+												
+							case N_MEASUREMENT_FREP_MINUS:
+								// frep negative step
+								
+								if (settling > 0) {
+									settling--;
+									break;
+								}
+
+								if (t3_1 == 0.0)
+									t3_1 = utc;
+								
+								Frepminus_1 = Frepminus_1 + Math1 - Slope_1 * (utc - t3_1);
+								Ch4Minus = Ch4Minus + Ch4 - Ch4Slope * (utc - t3_1);
+								n_1 += 1;
+								
+								if ((utc - t3_1) > DeltaT_1) {
+									Frepminus_1 = Frepminus_1 / n_1;
+									Ch4Minus = Ch4Minus / n_1;
+									n_1 = 0;
+									
+									t1_1 = 0.0;
+									t2_1 = 0.0;
+									t3_1 = 0.0;
+									Frepminus_1 = 0.0;
+									Frepplus_1 = 0.0;
+									
+									// compute N1
+									N_1 = Signe1 * (2*Ndiv * DeltakHz_1 * 1000)/(Frepminus_1 - Frepplus_1 - Slope_1 * (t3_1 - t2_1));
+									
+									DDS4xAD9912_FrequencyRampe(&DDS4xAD9912, 1, FrequDDS1-DeltakHz_1*1000,FrequDDS1, Step1/Ndiv );
+									SetCtrlVal(MainPanel, PANEL_DDS1, FrequDDS1);
+									DDS4xAD9912_SetFrequency(&DDS4xAD9912, 1, FrequDDS1);
+									SetCtrlVal(MainPanel, PANEL_DDS2, FrequencyDDSBesInit);
+									DDS4xAD9912_SetFrequency(&DDS4xAD9912, 2, FrequencyDDSBesInit);
+									
+									// done
+									Measuring_1 = N_MEASUREMENT_NONE;
+								}
+								break;
+						}
 						
 						if (Measuring_2==TRUE)
 							{