diff FXAnalyse.c @ 160:f609fb29536b

Simplification and code cleanup
author Daniele Nicolodi <daniele.nicolodi@obspm.fr>
date Tue, 04 Feb 2014 12:48:37 +0100
parents 07153c41ba16
children 6ee23cb3d6bb
line wrap: on
line diff
--- a/FXAnalyse.c	Mon Feb 03 17:32:47 2014 +0100
+++ b/FXAnalyse.c	Tue Feb 04 12:48:37 2014 +0100
@@ -22,6 +22,10 @@
 
 #define FREP_STEP_SIZE 50000.0
 
+#define SPEED_OF_LIGHT 299792458.0 // m/s
+#define SR_WAVELENGTH  698.4460    // nm
+#define HG_WAVELENGTH 1062.4000    // nm
+
 // data acquisition status
 int acquiring;
 // data queue
@@ -45,7 +49,7 @@
 // panels
 static int MainPanel;
 static int CalcNPanel;
-static int EstimateN3Panel;
+static int EstimateNPanel;
 static int LoggingPanel;
 
 
@@ -157,19 +161,13 @@
 
 muParserHandle_t MathParser1, MathParser2, MathParser3, MathParser4, MathParser5;
 
-double Ndiv = 8.0;
-
-int settling = 0;
-
 enum {
-	MEASURING_N_NONE,
-	MEASURING_N_Lo,
-	MEASURING_N_Hg,
-	MEASURING_N_Sr,
+	N_NONE,
+	N_LO,
+	N_HG,
+	N_SR,
 };
 
-int measuring = MEASURING_N_NONE;
-
 enum {
 	N_MEASUREMENT_NONE,
 	N_MEASUREMENT_INIT,
@@ -184,18 +182,22 @@
 int Measuring_2 = N_MEASUREMENT_NONE;											  
 int Measuring_3 = N_MEASUREMENT_NONE;
 
+double Ndiv = 8.0;
+
+int nobs = 0;
+int settling = 0;
+
 double f0_DDS1 = 110000000.0, f0_DDS2, f0_DDS3;
 
 double N_1=0.0, N_2=0.0, N_3=0.0;
-double SlopeTime1=40.0, DeltaT_1=20.0, delta_f_lock_1=500.0, t1_1=0.0, t2_1=0.0, t3_1=0.0;
-double SlopeTime2=40.0, DeltaT_2=20.0, delta_f_lock_2=500.0, t1_2=0.0, t2_2=0.0, t3_2=0.0;
-double SlopeTime3=40.0, DeltaT_3=20.0, delta_f_lock_3=500.0, t1_3=0.0, t2_3=0.0, t3_3=0.0;
-
-int n_1=0, n_2=0, n_3=0;
+double SlopeTime1=40.0, DeltaT_1=20.0, delta_f_lock_1=500.0;
+double SlopeTime2=40.0, DeltaT_2=20.0, delta_f_lock_2=500.0;
+double SlopeTime3=40.0, DeltaT_3=20.0, delta_f_lock_3=500.0;
 
 double DeltaDDS3=0.0;
 double Nu1=0.0, Nu2= 200000-147000+282143746.557455e6;  
 
+double t1, t2, t3;
 double f_rep_slope, f_beat_slope;
 double f_rep_plus, f_rep_minus;
 double f_beat_plus, f_beat_minus;
@@ -432,7 +434,7 @@
 		return -1;
 	if ((CalcNPanel = LoadPanel (MainPanel, "FXAnalyse.uir", CALCN)) < 0)
 		return -1;
-	if ((EstimateN3Panel = LoadPanel (MainPanel, "FXAnalyse.uir", ESTIMATEN3)) < 0)
+	if ((EstimateNPanel = LoadPanel (MainPanel, "FXAnalyse.uir", ESTIMATEN)) < 0)
 		return -1;
 	if ((LoggingPanel = LoadPanel (0, "FXAnalyse.uir", LOGGING)) < 0)
 		return -1;
@@ -730,11 +732,14 @@
 						// set DDS1 to nominal frequency
 						DDS4xAD9912_SetFrequency(&DDS4xAD9912, 1, f0_DDS1);
 						
+						// record current DDS frequencies
 						f0_DDS2 = DDS4xAD9912_GetFrequency(&DDS4xAD9912, 2);
-						t1_1 = utc;
-						t2_1 = 0.0;
-						t3_1 = 0.0;
+						
+						t1 = utc;
+						t2 = t3 = 0.0;
+						nobs = 0;
 						stat_zero(&stat_math1);
+						f_rep_plus = f_rep_minus = 0.0;
 						
 						// next step
 						Measuring_1 += 1;
@@ -745,7 +750,7 @@
 						
 						stat_accumulate(&stat_math1, Math1);
 						
-						if ((utc - t1_1) > SlopeTime1) {
+						if ((utc - t1) > SlopeTime1) {
 							f_rep_slope = stat_math1.slope;
 							
 							// frep positive step
@@ -782,15 +787,15 @@
 						if (settling-- > 0)
 							break;
 						
-						if (t2_1 == 0.0)
-							t2_1 = utc;
+						if (t2 == 0.0)
+							t2 = utc;
 						
-						f_rep_plus += Math1 - f_rep_slope * (utc - t2_1);
-						n_1 += 1;
+						f_rep_plus += Math1 - f_rep_slope * (utc - t2);
+						nobs += 1;
 						
-						if ((utc - t2_1) > DeltaT_1) {
-							f_rep_plus = f_rep_plus / n_1;
-							n_1 = 0;
+						if ((utc - t2) > DeltaT_1) {
+							f_rep_plus = f_rep_plus / nobs;
+							nobs = 0;
 							
 							// frep negative step
 							DDS4xAD9912_RampFrequency(&DDS4xAD9912, 1, f0_DDS1 - delta_f_lock_1, FREP_STEP_SIZE);
@@ -809,23 +814,20 @@
 						if (settling-- > 0)
 							break;
 						
-						if (t3_1 == 0.0)
-							t3_1 = utc;
+						if (t3 == 0.0)
+							t3 = utc;
 						
-						f_rep_minus += Math1 - f_rep_slope * (utc - t2_1);
-						n_1 += 1;
+						f_rep_minus += Math1 - f_rep_slope * (utc - t2);
+						nobs += 1;
 						
-						if ((utc - t3_1) > DeltaT_1) {
-							f_rep_minus = f_rep_minus / n_1;
-							n_1 = 0;
+						if ((utc - t3) > DeltaT_1) {
+							f_rep_minus = f_rep_minus / nobs;
+							nobs = 0;
 							
 							// compute N1
 							double delta_f_rep = f_rep_minus - f_rep_plus;
 							N_1 = Sign1 * 2 * Ndiv * delta_f_lock_1 / delta_f_rep;
 							SetCtrlVal(CalcNPanel, CALCN_N, N_1);
-						
-							t1_1 = t2_1 = t3_1 = 0.0;
-							f_rep_minus = f_rep_plus = 0.0;
 							
 							// back to nominal frep
 							DDS4xAD9912_RampFrequency(&DDS4xAD9912, 1, f0_DDS1, FREP_STEP_SIZE);
@@ -849,13 +851,17 @@
 						// set DDS1 to nominal frequency
 						DDS4xAD9912_SetFrequency(&DDS4xAD9912, 1, f0_DDS1);
 						
+						// record current DDS frequencies
 						f0_DDS2 = DDS4xAD9912_GetFrequency(&DDS4xAD9912, 2);
 						f0_DDS3 = DDS4xAD9912_GetFrequency(&DDS4xAD9912, 3);
-						t1_2 = utc;
+						
+						t1 = utc;
+						t2 = t3 = 0.0;
+						nobs = 0;
 						stat_zero(&stat_math1);
 						stat_zero(&stat_ch2);
-						
-						Nu1 = N1 * (250000000 + Math1);
+						f_rep_plus = f_rep_minus = 0.0;
+						f_beat_plus = f_beat_minus = 0.0;
 						
 						// next step
 						Measuring_2 += 1;
@@ -867,7 +873,7 @@
 						stat_accumulate(&stat_math1, Math1);
 						stat_accumulate(&stat_ch2, Ch2);
 
-						if ((utc - t1_2) > SlopeTime2) {
+						if ((utc - t1) > SlopeTime2) {
 							f_rep_slope = stat_math1.slope;
 							f_beat_slope = stat_ch2.slope;
 							
@@ -876,7 +882,7 @@
 							DDS4xAD9912_RampFrequency(&DDS4xAD9912,1, fDDS1, FREP_STEP_SIZE);
 							
 							// adjust DDS3 to keep beatnote within the bandpass filter. prediction
-							double fDDS3 = f0_DDS3 - delta_f_lock_2 *(-Sign1/Sign2)*Ndiv*(Nu2)/(Nu1) - f_beat_slope * (utc-t1_2);
+							double fDDS3 = f0_DDS3 + Sign1 * Sign2 * N2/N1 * Ndiv * delta_f_lock_2;
 							DeltaDDS3 = fDDS3 - DDS4xAD9912_GetFrequency(&DDS4xAD9912, 3);
 							DDS4xAD9912_SetFrequency(&DDS4xAD9912, 3, fDDS3);
 						
@@ -915,24 +921,24 @@
 						if (settling-- > 0)
 							break;
 
-						if (t2_1 == 0.0)
-							t2_1 = utc;
+						if (t2 == 0.0)
+							t2 = utc;
 
-						f_rep_plus += Math1 + 250000000 - f_rep_slope * (utc - t2_2);
-						f_beat_plus += Ch2 - f_beat_slope * (utc - t2_2);
-						n_2 += 1;
+						f_rep_plus += Math1 + 250000000 - f_rep_slope * (utc - t2);
+						f_beat_plus += Ch2 - f_beat_slope * (utc - t2);
+						nobs += 1;
 						
-						if ((utc - t2_2) > DeltaT_2) {
-							f_rep_plus = f_rep_plus / n_2;
-							f_beat_plus = f_beat_plus / n_2;
-							n_2 = 0;
+						if ((utc - t2) > DeltaT_2) {
+							f_rep_plus = f_rep_plus / nobs;
+							f_beat_plus = f_beat_plus / nobs;
+							nobs = 0;
 
 							// negative frequency step
 							double fDDS1 = f0_DDS1 - delta_f_lock_2;
 							DDS4xAD9912_RampFrequency(&DDS4xAD9912, 1, fDDS1, FREP_STEP_SIZE);
 							
 							// adjust DDS3 to keep beatnote within the bandpass filter. prediction
-							double fDDS3 = f0_DDS3 + delta_f_lock_2 *(-Sign1/Sign2)*Ndiv*(Nu2)/(Nu1);
+							double fDDS3 = f0_DDS3 - Sign1 * Sign2 * N2/N1 * Ndiv * delta_f_lock_2;
 							DeltaDDS3 = fDDS3 - DDS4xAD9912_GetFrequency(&DDS4xAD9912, 3);
 							DDS4xAD9912_SetFrequency(&DDS4xAD9912, 3, fDDS3);
 
@@ -950,17 +956,17 @@
 						if (settling-- > 0)
 							break;
 						
-						if (t3_1 == 0.0)
-							t3_1 = utc;
+						if (t3 == 0.0)
+							t3 = utc;
 
-						f_rep_minus += Math1 + 250000000 - f_rep_slope * (utc - t2_2);
-						f_beat_minus += Ch2 + f_beat_slope * (utc - t2_2);
-						n_2 += 1;
+						f_rep_minus += Math1 + 250000000 - f_rep_slope * (utc - t2);
+						f_beat_minus += Ch2 + f_beat_slope * (utc - t2);
+						nobs += 1;
 
-						if ((utc -t3_2) > DeltaT_2) {
-							f_rep_minus = f_rep_minus / n_2;
-							f_beat_minus = f_beat_minus / n_2;
-							n_2 = 0;
+						if ((utc -t3) > DeltaT_2) {
+							f_rep_minus = f_rep_minus / nobs;
+							f_beat_minus = f_beat_minus / nobs;
+							nobs = 0;
 
 							// compute N2
 							double delta_f_rep = f_rep_minus - f_rep_plus;
@@ -972,10 +978,6 @@
 							DDS4xAD9912_SetFrequency(&DDS4xAD9912, 2, f0_DDS2);
 							DDS4xAD9912_SetFrequency(&DDS4xAD9912, 3, f0_DDS3);
 							
-							t1_2 = t2_2 = t3_2 = 0.0;
-							f_rep_minus = f_rep_plus = 0.0;
-							f_beat_minus = f_beat_plus = 0.0;
-
 							// done
 							Measuring_2 = N_MEASUREMENT_NONE;
 						}
@@ -991,19 +993,21 @@
 					case N_MEASUREMENT_INIT:
 						// init
 						
+						// set DDS1 to nominal frequency
 						DDS4xAD9912_SetFrequency(&DDS4xAD9912, 1, f0_DDS1);
-						settling = 3;
 						
-						t1_3 = utc;
+						// record current DDS frequencies
+						f0_DDS2 = DDS4xAD9912_GetFrequency(&DDS4xAD9912, 2);
+						f0_DDS3 = DDS4xAD9912_GetFrequency(&DDS4xAD9912, 3);
+						
+						t1 = utc;
+						t2 = t3 = 0.0;
+						nobs = 0;
 						stat_zero(&stat_math1);
 						stat_zero(&stat_ch3);
 						f_rep_plus = f_rep_minus = 0.0;
 						f_beat_plus = f_beat_minus = 0.0;
 						
-						// record current DDS frequencies
-						f0_DDS2 = DDS4xAD9912_GetFrequency(&DDS4xAD9912, 2);
-						f0_DDS3 = DDS4xAD9912_GetFrequency(&DDS4xAD9912, 3);
-						
 						// next step
 						Measuring_3 += 1;
 						break;
@@ -1017,14 +1021,13 @@
 						stat_accumulate(&stat_math1, Math1);
 						stat_accumulate(&stat_ch3, Ch3);
 						
-						if (utc - t1_3 > SlopeTime3) {
+						if (utc - t1 > SlopeTime3) {
 							// slope measurement
 							f_rep_slope = stat_math1.slope;
+							f_beat_slope = stat_ch3.slope;
+							
 							logmsg("f_rep_slope=%e Hz/s", f_rep_slope);
-							f_beat_slope = stat_ch3.slope;
 							logmsg("f_beat_slope=%e Hz/s", f_rep_slope);
-
-							t2_3 = utc;
 							
 							// frep positive step
 							DDS4xAD9912_RampFrequency(&DDS4xAD9912, 1, f0_DDS1 + delta_f_lock_3, FREP_STEP_SIZE);
@@ -1064,18 +1067,18 @@
 						
 						if (settling-- > 0)
 							break;
-						
-						n_3++;
-						f_rep_plus += Math1 + 250000000 - f_rep_slope * (utc - t3_2);
-						f_beat_plus += Ch3 - f_beat_slope * (utc - t3_2);
+
+						if (t2 == 0.0)
+							t2 = utc;
 						
-						if (utc - t2_3 > DeltaT_3) {
-							// positive step measurement
-							f_rep_plus = f_rep_plus / n_3;
-							f_beat_plus = f_beat_plus / n_3;
-							
-							n_3 = 0;
-							t3_3 = utc;
+						f_rep_plus += Math1 + 250000000 - f_rep_slope * (utc - t2);
+						f_beat_plus += Ch3 - f_beat_slope * (utc - t2);
+						nobs += 1;
+						
+						if (utc - t2 > DeltaT_3) {
+							f_rep_plus = f_rep_plus / nobs;
+							f_beat_plus = f_beat_plus / nobs;
+							nobs = 0;
 							
 							// frep negative step
 							DDS4xAD9912_RampFrequency(&DDS4xAD9912, 1, f0_DDS1 - delta_f_lock_3, FREP_STEP_SIZE);
@@ -1098,14 +1101,17 @@
 						if (settling-- > 0)
 							break;
 						
-						n_3++;
-						f_rep_minus += Math1 + 250000000 - f_rep_slope * (utc - t3_2);
-						f_beat_minus += Ch3 - f_beat_slope * (utc - t3_2);
+						if (t3 == 0.0)
+							t3 = utc;
 						
-						if (utc - t3_3 > DeltaT_3) {
-							// negative step measurement
-							f_rep_minus = f_rep_minus / n_3;
-							f_beat_minus = f_beat_minus / n_3;
+						f_rep_minus += Math1 + 250000000 - f_rep_slope * (utc - t2);
+						f_beat_minus += Ch3 - f_beat_slope * (utc - t2);
+						nobs += 1;
+						
+						if (utc - t3 > DeltaT_3) {
+							f_rep_minus = f_rep_minus / nobs;
+							f_beat_minus = f_beat_minus / nobs;
+							nobs = 0;
 							
 							// check delta frep
 							double delta_f_rep_m = f_rep_plus - f_rep_minus;
@@ -1125,11 +1131,6 @@
 							logmsg("measured N3=%.3f", N_3);
 							SetCtrlVal(CalcNPanel, CALCN_N, N_3);
 							
-							t1_3=0.0;
-							t2_3=0.0;
-							t3_3=0.0;
-							n_3 = 0;
-							
 							// back to nominal frep
 							DDS4xAD9912_RampFrequency(&DDS4xAD9912, 1, f0_DDS1, FREP_STEP_SIZE);
 							DDS4xAD9912_SetFrequency(&DDS4xAD9912, 2, f0_DDS2);
@@ -1554,18 +1555,21 @@
 int CVICALLBACK CB_OnAcceptN (int panel, int control, int event,
 		void *callbackData, int eventData1, int eventData2)
 {
+	int measure;
+	
 	switch (event) {
 		case EVENT_COMMIT:
-			switch (measuring) {
-				case MEASURING_N_Lo:
+			GetPanelAttribute(panel, ATTR_CALLBACK_DATA, &measure);
+			switch (measure) {
+				case N_LO:
 					N1 = round(N_1);
 					SetCtrlVal(MainPanel, PANEL_N1CHOICE, N1);
 					break;
-				case MEASURING_N_Hg:
+				case N_HG:
 					N2 = round(N_2);
 					SetCtrlVal(MainPanel, PANEL_N2CHOICE, N2);
 					break;
-				case MEASURING_N_Sr:
+				case N_SR:
 					N3 = round(N_3);
 					SetCtrlVal(MainPanel, PANEL_N3CHOICE, N3);
 					break;
@@ -1587,7 +1591,7 @@
 				case PANEL_N1CALCULUS:
 					GetPanelAttribute(CalcNPanel, ATTR_VISIBLE, &visible);
 					if (! visible) {
-						measuring = MEASURING_N_Lo;
+						SetPanelAttribute(panel, ATTR_CALLBACK_DATA, N_LO);
 						SetPanelAttribute(CalcNPanel, ATTR_TITLE, "Measure N_Lo");
 						SetCtrlVal(CalcNPanel, CALCN_INTEGRATIONTIME, DeltaT_1);
 						SetCtrlVal(CalcNPanel, CALCN_SLOPETIME, SlopeTime1);
@@ -1599,7 +1603,7 @@
 				case PANEL_N2CALCULUS:
 					GetPanelAttribute(CalcNPanel, ATTR_VISIBLE, &visible);
 					if (! visible) {
-						measuring = MEASURING_N_Hg;
+						SetPanelAttribute(panel, ATTR_CALLBACK_DATA, N_HG);
 						SetPanelAttribute(CalcNPanel, ATTR_TITLE, "Measure N_Hg");
 						SetCtrlVal(CalcNPanel, CALCN_INTEGRATIONTIME, DeltaT_2);
 						SetCtrlVal(CalcNPanel, CALCN_SLOPETIME, SlopeTime2);
@@ -1611,7 +1615,7 @@
 				case PANEL_N3CALCULUS:
 					GetPanelAttribute(CalcNPanel, ATTR_VISIBLE, &visible);
 					if (! visible) {
-						measuring = MEASURING_N_Sr;
+						SetPanelAttribute(panel, ATTR_CALLBACK_DATA, N_SR);
 						SetPanelAttribute(CalcNPanel, ATTR_TITLE, "Measure N_Sr");
 						SetCtrlVal(CalcNPanel, CALCN_INTEGRATIONTIME, DeltaT_3);
 						SetCtrlVal(CalcNPanel, CALCN_SLOPETIME, SlopeTime3);
@@ -1629,10 +1633,13 @@
 int CVICALLBACK CB_OnStartNCalculus (int panel, int control, int event,
 		void *callbackData, int eventData1, int eventData2)
 {
+	int measuring;
+	
 	switch (event) {
 		case EVENT_COMMIT:
+			GetPanelAttribute(panel, ATTR_CALLBACK_DATA, &measuring);
 			switch (measuring) {
-				case MEASURING_N_Lo:
+				case N_LO:
 					GetCtrlVal(CalcNPanel, CALCN_INTEGRATIONTIME, &DeltaT_1);
 					GetCtrlVal(CalcNPanel, CALCN_SLOPETIME, &SlopeTime1);
 					GetCtrlVal(CalcNPanel, CALCN_DELTAFREQ, &delta_f_lock_1);
@@ -1640,7 +1647,7 @@
 					delta_f_lock_1 = delta_f_lock_1 * 1000.0;
 					Measuring_1 = TRUE;
 					break;
-				case MEASURING_N_Hg:
+				case N_HG:
 					GetCtrlVal(CalcNPanel, CALCN_INTEGRATIONTIME, &DeltaT_2);
 					GetCtrlVal(CalcNPanel, CALCN_SLOPETIME, &SlopeTime2);
 					GetCtrlVal(CalcNPanel, CALCN_DELTAFREQ, &delta_f_lock_2);
@@ -1648,7 +1655,7 @@
 					delta_f_lock_2 = delta_f_lock_2 * 1000.0;
 					Measuring_2 = TRUE;
 					break;
-				case MEASURING_N_Sr:
+				case N_SR:
 					GetCtrlVal(CalcNPanel, CALCN_INTEGRATIONTIME, &DeltaT_3);
 					GetCtrlVal(CalcNPanel, CALCN_SLOPETIME, &SlopeTime3);
 					GetCtrlVal(CalcNPanel, CALCN_DELTAFREQ, &delta_f_lock_3);
@@ -1665,22 +1672,25 @@
 int CVICALLBACK CB_OnNStop (int panel, int control, int event,
 		void *callbackData, int eventData1, int eventData2)
 {
+	int measuring;
+	
 	switch (event) {
 		case EVENT_COMMIT:
 			HidePanel(CalcNPanel);
+			GetPanelAttribute(panel, ATTR_CALLBACK_DATA, &measuring);
 			switch (measuring) {
-				case MEASURING_N_Lo:
+				case N_LO:
 					Measuring_1 = FALSE;
 					DDS4xAD9912_RampFrequency(&DDS4xAD9912, 1, f0_DDS1, FREP_STEP_SIZE);
 					DDS4xAD9912_SetFrequency(&DDS4xAD9912, 2, f0_DDS2);
 					break;
-				case MEASURING_N_Hg:
+				case N_HG:
 					Measuring_2 = FALSE;
 					DDS4xAD9912_RampFrequency(&DDS4xAD9912, 1, f0_DDS1, FREP_STEP_SIZE);
 					DDS4xAD9912_SetFrequency(&DDS4xAD9912, 2, f0_DDS2);
 					DDS4xAD9912_SetFrequency(&DDS4xAD9912, 3, f0_DDS3);
 					break;
-				case MEASURING_N_Sr:
+				case N_SR:
 					Measuring_3 = FALSE;
 					DDS4xAD9912_RampFrequency(&DDS4xAD9912, 1, f0_DDS1, FREP_STEP_SIZE);
 					DDS4xAD9912_SetFrequency(&DDS4xAD9912, 2, f0_DDS2);
@@ -1908,31 +1918,31 @@
 		void *callbackData, int eventData1, int eventData2)
 {
 	int visible;
-	double wl;
 	
 	switch (event)
 	{
 		case EVENT_COMMIT:
-			// be prepared to support more N estimates for different beat notes
-			switch (control)
-			{
+			GetPanelAttribute(EstimateNPanel, ATTR_VISIBLE , &visible);
+			if (! visible)
+				DisplayPanel(EstimateNPanel);
+			// reset N estimate
+			SetCtrlVal(EstimateNPanel, ESTIMATEN_N, 0.0);
+			// set current frep
+			SetCtrlVal(EstimateNPanel, ESTIMATEN_FREP, 250e6 + Math1);
+			switch (control) {
+				case PANEL_ESTIMATE_N2:
+					SetCtrlVal(EstimateNPanel, ESTIMATEN_WAVELENGTH, HG_WAVELENGTH);
+					SetPanelAttribute(panel, ATTR_TITLE, "Estimate N_Hg");
+					SetPanelAttribute(panel, ATTR_CALLBACK_DATA, N_HG);
+					break;
 				case PANEL_ESTIMATE_N3:
-					GetPanelAttribute(EstimateN3Panel, ATTR_VISIBLE , &visible);
-					if (! visible)
-						DisplayPanel(EstimateN3Panel);
-					// set current frep
-					SetCtrlVal(EstimateN3Panel, ESTIMATEN3_FREP, 250e6 + Math1);
-					// default wavelenght for Sr cavity
-					GetCtrlVal(EstimateN3Panel, ESTIMATEN3_WAVELENGTH, &wl);
-					if (wl == 0.0)
-						SetCtrlVal(EstimateN3Panel, ESTIMATEN3_WAVELENGTH, 698.446);
-					// reset N3 estimate
-					SetCtrlVal(EstimateN3Panel, ESTIMATEN3_N, 0.0);
+					SetCtrlVal(EstimateNPanel, ESTIMATEN_WAVELENGTH, SR_WAVELENGTH);
+					SetPanelAttribute(panel, ATTR_TITLE, "Estimate N_Sr");
+					SetPanelAttribute(panel, ATTR_CALLBACK_DATA, N_SR);
 					break;
 			}
 			break;
 	}
-	
 	return 0;
 }			
 
@@ -1955,11 +1965,22 @@
 int CVICALLBACK CB_OnNEstimateSet (int panel, int control, int event,
 		void *callbackData, int eventData1, int eventData2)
 {
+	int estimate;
+	
 	switch (event)
 	{
 		case EVENT_COMMIT:
-			GetCtrlVal(panel, ESTIMATEN3_N, &N3);
-			SetCtrlVal(MainPanel, PANEL_N3CHOICE, N3);
+			GetPanelAttribute(panel, ATTR_CALLBACK_DATA, &estimate);
+			switch (estimate) {
+				case N_HG:
+					GetCtrlVal(panel, ESTIMATEN_N, &N2);
+					SetCtrlVal(MainPanel, PANEL_N3CHOICE, N2);
+					break;
+				case N_SR:
+					GetCtrlVal(panel, ESTIMATEN_N, &N3);
+					SetCtrlVal(MainPanel, PANEL_N3CHOICE, N3);
+					break;
+			}
 			HidePanel(panel);
 			break;
 	}
@@ -1974,10 +1995,10 @@
 	switch (event)
 	{
 		case EVENT_COMMIT:
-			GetCtrlVal(panel, ESTIMATEN3_FREP, &frep);
-			GetCtrlVal(panel, ESTIMATEN3_WAVELENGTH, &wl);
-			N = round(299792458.0 / wl / 1e-9 / frep);
-			SetCtrlVal(panel, ESTIMATEN3_N, N);
+			GetCtrlVal(panel, ESTIMATEN_FREP, &frep);
+			GetCtrlVal(panel, ESTIMATEN_WAVELENGTH, &wl);
+			N = round(SPEED_OF_LIGHT / wl / 1e-9 / frep);
+			SetCtrlVal(panel, ESTIMATEN_N, N);
 			break;
 	}
 	return 0;