/* ************************************************************** */ /* * Module StretchedWire * This module contains functions which control stretched wire measurements. * * Zachary Wolf * 11/23/99 */ /* ************************************************************* */ /* INCLUDE FILES */ #include #include #include #include #include #include "param.h" #include "runparam.h" #include "blwire.h" #include "blwireparam.h" #include "blwireui.h" #include "vtwire.h" #include "vtwireparam.h" #include "vtwireui.h" #include "imag.h" #include "imagparam.h" #include "imagui.h" /* ************************************************************* */ /* GLOBAL VARIABLES */ static char log_file[80]; static char dat_file[80]; static char plt_file[80]; /* ************************************************************* */ /* DECLARATIONS */ enum measurements {INTEG_STR_VS_IMAG, BL_VS_X, MAG_CENTER}; /* ************************************************************* */ /* PRIVATE FUNCTIONS */ void stretched_wire_init(void); void stretched_wire_meas(void); void stretched_wire_meas_integ_str_vs_imag(void); void stretched_wire_meas_bl_vs_x(void); void stretched_wire_find_center(void); void stretched_wire_exit(void); void stretched_wire_error(char* message); /* ************************************************************** */ /* PUBLIC FUNCTIONS */ /* ************************************************************* */ int main(int argc, char *argv[]) { /* Perform all initialization for the program */ stretched_wire_init(); /* Perform the measurements */ stretched_wire_meas(); /* Exit all systems */ stretched_wire_exit(); /* Message */ printf("\nDone\n"); /* Done */ return 0; } /* ************************************************************** */ /* PRIVATE FUNCTIONS */ /* ************************************************************** */ void stretched_wire_init(void) { /* Declare variables */ struct run_param_struct run_param; int err; struct blwire_param_struct blwire_param; struct vtwire_param_struct vtwire_param; struct imag_param_struct imag_param; char par_file[80]; /* Get the run parameters from the user */ try_again: runparam_get_run_param(&run_param); /* Create all output files */ err = runparam_create_file(run_param, "wirelog", log_file); if (err != 0) goto try_again; err = runparam_create_file(run_param, "wiredat", dat_file); if (err != 0) goto try_again; err = runparam_create_file(run_param, "wireplt", plt_file); if (err != 0) goto try_again; err = runparam_create_file(run_param, "wirepar", par_file); if (err != 0) goto try_again; /* Save the parameter file */ err = CopyFile("param.h", par_file); if (err != 0) printf("Error saving parameter file"); /* Update the run index file */ runparam_update_index(run_param); /* Write file headers */ runparam_write_header(run_param, log_file); runparam_write_header(run_param, dat_file); if (WIRE_DESIRED_MEAS == INTEG_STR_VS_IMAG) { imag_write_header(log_file, WIRE_NUM_STAND_CYCLES, WIRE_STAND_MAX, WIRE_STAND_MIN, WIRE_NUM_INTEG_STR_CURRENTS, WIRE_INTEG_STR_CURRENTS); imag_write_header(dat_file, WIRE_NUM_STAND_CYCLES, WIRE_STAND_MAX, WIRE_STAND_MIN, WIRE_NUM_INTEG_STR_CURRENTS, WIRE_INTEG_STR_CURRENTS); } else if (WIRE_DESIRED_MEAS == BL_VS_X) { imag_write_header(log_file, WIRE_NUM_STAND_CYCLES, WIRE_STAND_MAX, WIRE_STAND_MIN, WIRE_NUM_BL_VS_X_CURRENTS, WIRE_BL_VS_X_CURRENTS); imag_write_header(dat_file, WIRE_NUM_STAND_CYCLES, WIRE_STAND_MAX, WIRE_STAND_MIN, WIRE_NUM_BL_VS_X_CURRENTS, WIRE_BL_VS_X_CURRENTS); } else if (WIRE_DESIRED_MEAS == MAG_CENTER) { imag_write_header(log_file, WIRE_NUM_STAND_CYCLES, WIRE_STAND_MAX, WIRE_STAND_MIN, WIRE_NUM_MAG_CENTER_CURRENTS, WIRE_MAG_CENTER_CURRENTS); imag_write_header(dat_file, WIRE_NUM_STAND_CYCLES, WIRE_STAND_MAX, WIRE_STAND_MIN, WIRE_NUM_MAG_CENTER_CURRENTS, WIRE_MAG_CENTER_CURRENTS); } else stretched_wire_error("Unknown measurement type.\n"); /* Fill all parameter structures */ vtwireparam_fill_param_struct(&vtwire_param); blwireparam_fill_param_struct(&blwire_param); imagparam_fill_param_struct(&imag_param); /* Initialize all user interfaces */ SetStdioWindowPosition(430, 15); SetStdioWindowSize(300, 995); vtwireui_init(25, 15, vtwire_param); blwireui_init(25, 350, blwire_param); imagui_init(25, 685, imag_param); if (WIRE_DESIRED_MEAS == INTEG_STR_VS_IMAG) { imagui_scale_horiz_axis(WIRE_NUM_STAND_CYCLES, WIRE_STAND_MIN, WIRE_STAND_MAX, WIRE_NUM_INTEG_STR_CURRENTS, WIRE_INTEG_STR_CURRENTS); blwireui_clear_str_vs_imag(); blwireui_scale_str_vs_imag(WIRE_NUM_INTEG_STR_CURRENTS, WIRE_INTEG_STR_CURRENTS); blwireui_scale_bl_vs_x(WIRE_NUM_X0_POS_INTEG_STR, WIRE_X0_POS_INTEG_STR); } else if (WIRE_DESIRED_MEAS == BL_VS_X) { imagui_scale_horiz_axis(WIRE_NUM_STAND_CYCLES, WIRE_STAND_MIN, WIRE_STAND_MAX, WIRE_NUM_BL_VS_X_CURRENTS, WIRE_BL_VS_X_CURRENTS); blwireui_clear_bl_vs_x(); blwireui_scale_bl_vs_x(WIRE_NUM_X0_POS_BL_VS_X, WIRE_X0_POS_BL_VS_X); } else if (WIRE_DESIRED_MEAS == MAG_CENTER) { imagui_scale_horiz_axis(WIRE_NUM_STAND_CYCLES, WIRE_STAND_MIN, WIRE_STAND_MAX, WIRE_NUM_MAG_CENTER_CURRENTS, WIRE_MAG_CENTER_CURRENTS); blwireui_clear_bl_vs_x(); blwireui_scale_bl_vs_x(WIRE_NUM_X0_POS_MAG_CENTER, WIRE_X0_POS_MAG_CENTER); } else stretched_wire_error("Unknown measurement type.\n"); /* Message */ printf("\n\n*** Stretched Wire Measurement System ***\n"); /* Initialize all systems */ vtwire_init(log_file, vtwire_param); blwire_init(log_file, dat_file, plt_file, blwire_param); imag_init(log_file, imag_param); /* Done */ return; } /* ************************************************************** */ void stretched_wire_meas(void) { /* Standardize the magnet */ if (WIRE_NUM_STAND_CYCLES > 0) { printf("\nStandardizing the magnet...\n"); imag_standardize(WIRE_STAND_MAX, WIRE_STAND_MIN, WIRE_NUM_STAND_CYCLES); } /* Move the wire to the global offset position and zero the stages */ printf("\nMoving to the global offset position...\n"); vtwire_move_wire_abs(WIRE_X0_POS_GLOBAL_OFFSET); vtwire_zero_pos(); /* Do the measurement */ if (WIRE_DESIRED_MEAS == INTEG_STR_VS_IMAG) stretched_wire_meas_integ_str_vs_imag(); else if (WIRE_DESIRED_MEAS == BL_VS_X) stretched_wire_meas_bl_vs_x(); else if (WIRE_DESIRED_MEAS == MAG_CENTER) stretched_wire_find_center(); else stretched_wire_error("Unknown measurement requested\n"); /* Take out the global coordinate system offset and rezero the stages */ printf("\nMoving back to the global zero position...\n"); vtwire_move_wire_abs(-WIRE_X0_POS_GLOBAL_OFFSET); vtwire_zero_pos(); /* Done */ return; } /* ************************************************************** */ /* * stretched_wire_meas_integ_str_vs_imag * This function measures the integrated strength of a magnet * as a function of current. * * Zachary Wolf * 12/15/99 */ void stretched_wire_meas_integ_str_vs_imag(void) { /* Declare variables */ int i, j; double imag_bef_ave, imag_bef_rms; double imag_aft_ave, imag_aft_rms; double imag_ave, imag_rms; double integ_str_ave, integ_str_rms; /* Loop over the test currents */ for (i = 0; i < WIRE_NUM_INTEG_STR_CURRENTS; i++) { /* Ramp the magnet */ imag_ramp(WIRE_INTEG_STR_CURRENTS[i]); for (j = 1; j <= 30; j++) imagui_update(WIRE_INTEG_STR_CURRENTS[i]); /* Measure the magnet current before the strength measurement */ imag_get_ave_current(&imag_bef_ave, &imag_bef_rms); /* Clear the BL vs X measurement plot in preparation for the strength measurement */ blwireui_clear_bl_vs_x(); /* Get the integrated strength */ blwire_get_integ_str(WIRE_NUM_MAIN_HAR, WIRE_NUM_X0_POS_INTEG_STR, WIRE_X0_POS_INTEG_STR, WIRE_DIST_MOVE_WIRE, &integ_str_ave, &integ_str_rms); /* Measure the magnet current after the strength measurement */ imag_get_ave_current(&imag_aft_ave, &imag_aft_rms); /* Compute the average magnet current */ imag_ave = (imag_bef_ave + imag_aft_ave) / 2.; imag_rms = sqrt( pow(imag_bef_ave - imag_aft_ave, 2) + pow(imag_bef_rms, 2) + pow(imag_aft_rms, 2) ); /* Write the results to the data file */ blwire_dat_integ_str_vs_imag(WIRE_DIST_MOVE_WIRE, BLWIRE_NUM_TURNS_WIRE, VTWIRE_NUM_MEAS_AVE, WIRE_NUM_MAIN_HAR, imag_ave, imag_rms, integ_str_ave, integ_str_rms); /* Write the results to the plot file */ blwire_plt_integ_str_vs_imag(WIRE_NUM_MAIN_HAR, imag_ave, imag_rms, integ_str_ave, integ_str_rms); /* Update the plot */ blwireui_update_str_vs_imag(imag_ave, integ_str_ave, integ_str_rms); /* End loop over test currents */ } /* Done */ return; } /* ************************************************************** */ /* * stretched_wire_meas_bl_vs_x * This function measures the integrated field strength of a magnet * at specified x positions. * It measures the magnet current at each x position. * It writes the data to the dat and plt files. * * Zachary Wolf * 12/14/99 */ void stretched_wire_meas_bl_vs_x(void) { /* Declare variables */ int i, j, k; double imag_ave, imag_rms; double bl_ave, bl_rms; /* Check parameters */ if (WIRE_NUM_X0_POS_BL_VS_X < 1 || WIRE_NUM_X0_POS_BL_VS_X > 100) { printf("stretched_wire_meas_bl_vs_x: improper num_x0_pos"); return; } /* Loop over the test currents */ for (i = 0; i < WIRE_NUM_BL_VS_X_CURRENTS; i++) { /* Ramp the magnet */ imag_ramp(WIRE_BL_VS_X_CURRENTS[i]); for (j = 1; j <= 30; j++) imagui_update(WIRE_BL_VS_X_CURRENTS[i]); /* Prepare for a new BL vs X measurement */ blwireui_clear_bl_vs_x(); /* Perform the integrated field strength measurements */ for (k = 0; k < WIRE_NUM_X0_POS_BL_VS_X; k++) { /* Measure the magnet current */ imag_get_ave_current(&imag_ave, &imag_rms); /* Measure the integrated field strength */ blwire_get_bl_ave(WIRE_X0_POS_BL_VS_X[k], WIRE_DIST_MOVE_WIRE, &bl_ave, &bl_rms); /* Write the result to the dat file */ blwire_dat_bl_vs_x(WIRE_DIST_MOVE_WIRE, BLWIRE_NUM_TURNS_WIRE, VTWIRE_NUM_MEAS_AVE, WIRE_X0_POS_BL_VS_X[k], imag_ave, imag_rms, bl_ave, bl_rms); /* Write the result to the plt file */ blwire_plt_bl_vs_x(WIRE_X0_POS_BL_VS_X[k], imag_ave, imag_rms, bl_ave, bl_rms); /* Display the measured field strength */ blwireui_update_bl_vs_x(WIRE_X0_POS_BL_VS_X[k], bl_ave, bl_rms); } /* Move the wire back to the zero position */ printf("\nMoving back to the zero position...\n"); vtwire_move_wire_abs(0.); /* End loop over test currents */ } /* Done */ return; } /* ************************************************************** */ /* * stretched_wire_find_center * This function measures the integrated field strength of a magnet * at specified x positions. * It then calculates the magnetic center position and leaves the wire * at that location. * * Zachary Wolf * 3/31/00 */ void stretched_wire_find_center(void) { /* Declare variables */ int i, j, k; double imag_ave, imag_rms; double bl_ave[WIRE_NUM_X0_POS_MAG_CENTER]; double bl_rms[WIRE_NUM_X0_POS_MAG_CENTER]; double bl_center; double poly_fit_coeff[30], poly_fit_coeff_err[30]; double x_center, x_center_err; char buf[80]; double bl_check_ave, bl_check_rms; /* Check parameters */ if (WIRE_NUM_X0_POS_MAG_CENTER < 1 || WIRE_NUM_X0_POS_MAG_CENTER > 100) { printf("stretched_wire_find_center: improper num_x0_pos"); return; } /* Loop over the test currents */ for (i = 0; i < WIRE_NUM_MAG_CENTER_CURRENTS; i++) { /* Ramp the magnet to the required current */ imag_ramp(WIRE_MAG_CENTER_CURRENTS[i]); for (j = 1; j <= 30; j++) imagui_update(WIRE_MAG_CENTER_CURRENTS[i]); /* Perform the integrated field strength measurements */ for (k = 0; k < WIRE_NUM_X0_POS_MAG_CENTER; k++) { /* Measure the magnet current */ imag_get_ave_current(&imag_ave, &imag_rms); /* Measure the integrated field strength */ blwire_get_bl_ave(WIRE_X0_POS_MAG_CENTER[k], WIRE_DIST_MOVE_WIRE, &bl_ave[k], &bl_rms[k]); /* Write the result to the dat file */ blwire_dat_bl_vs_x(WIRE_DIST_MOVE_WIRE, BLWIRE_NUM_TURNS_WIRE, VTWIRE_NUM_MEAS_AVE, WIRE_X0_POS_MAG_CENTER[k], imag_ave, imag_rms, bl_ave[k], bl_rms[k]); /* Write the result to the plt file */ blwire_plt_bl_vs_x(WIRE_X0_POS_MAG_CENTER[k], imag_ave, imag_rms, bl_ave[k], bl_rms[k]); /* Display the measured field strength */ blwireui_update_bl_vs_x(WIRE_X0_POS_MAG_CENTER[k], bl_ave[k], bl_rms[k]); } /* Calculate BL at the magnet center based on the measured current */ if (fabs(WIRE_MAG_CENTER_CURRENTS[i]) > 0.) bl_center = (imag_ave / WIRE_MAG_CENTER_CURRENTS[i]) * WIRE_MAG_CENTER_BL[i]; else bl_center = WIRE_MAG_CENTER_BL[i]; /* Find the magnetic center */ blwire_calc_mag_center(WIRE_NUM_MAIN_HAR, WIRE_NUM_X0_POS_MAG_CENTER, WIRE_X0_POS_MAG_CENTER, bl_ave, bl_rms, bl_center, poly_fit_coeff, poly_fit_coeff_err, &x_center, &x_center_err); /* Message */ printf("\nThe magnetic center is at X = %f +- %f m\n", x_center, x_center_err); /* Write the result to the data file */ blwire_dat_mag_center(x_center, x_center_err); /* Perform a measurement at the magnetic center as a check */ printf("\nPerforming a measurement at the magnetic center as a check...\n"); /* Measure the magnet current */ imag_get_ave_current(&imag_ave, &imag_rms); /* Measure the integrated field strength */ blwire_get_bl_ave(x_center, WIRE_DIST_MOVE_WIRE, &bl_check_ave, &bl_check_rms); /* Write the result to the dat file */ blwire_dat_mag_center_check(x_center, imag_ave, imag_rms, bl_check_ave, bl_check_rms, bl_center, poly_fit_coeff[0], poly_fit_coeff_err[0], poly_fit_coeff[1], poly_fit_coeff_err[1]); /* Write the result to the plt file */ blwire_plt_bl_vs_x(x_center, imag_ave, imag_rms, bl_check_ave, bl_check_rms); /* Display the measured field strength */ blwireui_update_bl_vs_x(x_center, bl_check_ave, bl_check_rms); /* Message */ printf("\nAt The Position Of The Magnetic Center:\n"); printf("BL = %8.5f +- %8.5f Tm\n", bl_check_ave, bl_check_rms); printf("Imag = %8.3f +- %8.3f A\n", imag_ave, imag_rms); printf("BL_center = %8.5f Tm\n", bl_center); //printf("The fit parameters are\n"); //printf("n_main = %3i", WIRE_NUM_MAIN_HAR); //for (k = 0; k < WIRE_NUM_MAIN_HAR; k++) printf(", c_fit[%2i] = %11.7f", k, poly_fit_coeff[k]); //printf("\n"); //printf("(BL - BL_center)/sig_BL = %8.4f\n", (bl_check_ave - bl_center) / bl_check_rms); //if (poly_fit_coeff[1] != 0.) printf("(sig_BL/Gradient = %8.4f m\n", bl_check_rms / poly_fit_coeff[1]); //if (poly_fit_coeff[1] != 0.) printf("(BL - BL_center)/Gradient = %10.6f m\n", (bl_check_ave - bl_center) / poly_fit_coeff[1]); /* If there is only one test current: */ /* Move the wire to the magnetic center position */ /* Ramp the current down (This allows an alignment crew to find the wire) */ if (WIRE_NUM_MAG_CENTER_CURRENTS == 1) { printf("\nMoving the wire to the magnetic center position...\n"); vtwire_move_wire_abs(x_center); printf("\nRamping the current to zero...\n"); imag_ramp(0.); for (j = 1; j <= 30; j++) imagui_update(0.); printf("\nPlease turn off the power supply.\n"); printf("\nWhen you are done with the wire in this position,\nPress ENTER to continue.\n"); fgets(buf, 80, stdin); } /* For one or more test currents, move the wire to zero, then continue */ printf("\nMoving the wire to the zero position...\n"); vtwire_move_wire_abs(0.); /* End loop over test currents */ } /* Done */ return; } /* ************************************************************** */ void stretched_wire_exit(void) { /* Declare variables */ char buf[80]; /* Exit the VTwire system */ vtwire_exit(); /* Exit the magnet current system */ imag_exit(); /* Leave the display for the operator to see */ printf("\nPress ENTER to terminate program.\n"); fgets(buf, 80, stdin); /* Done */ return; } /* ************************************************************** */ /* * stretched_wire_error * This function handles errors for the stretched wire module. * * Input: * message, string to display in standard I/O * * Zachary Wolf * 10/11/98 */ void stretched_wire_error(char* message) { /* Declare variables */ char buf[80]; /* Notify the operator of the error */ printf("\nERROR: %s\n", message); Beep(); Delay(.5); Beep(); /* Terminate the program if the operator desires */ printf("Press ENTER to continue.\nPress any key then ENTER to terminate program.\n"); fgets(buf, 80, stdin); if (buf[0] == '\n') return; else exit(0); }