/* ************************************************************** */ /* * Module BTCOIL * This module contains functions for making transverse field * measurements using a spinning coil. * * Zachary Wolf * 2/3/00 */ /* ************************************************************** */ /* INCLUDES */ #include #include #include #include #include "btcoil.h" #include "btcoilui.h" #include "sr850.h" /* ************************************************************** */ /* PRIVATE FUNCTIONS */ void btcoil_message(char* msg); void btcoil_error(char* msg); void btcoil_log_device_type(enum btcoil_device_type_enum device_type); void btcoil_log_get_v_coil(double meas_freq, double meas_v_mag, double meas_v_ph, double freq, double v_amp, double v_ph); void btcoil_log_calc_Bt(double freq, double v_amp, double v_ph, double coil_constant, double Bt, double th); /* ************************************************************** */ /* PRIVATE GLOBAL VARIABLES */ static int dev_ID; /* ************************************************************** */ /* PRIVATE PARAMETERS */ static char log_file[100]; static struct btcoil_param_struct btcoil_param; /* ************************************************************** */ /* PUBLIC FUNCTIONS */ /* ************************************************************** */ /* * btcoil_init * This function opens the device, queries for ID, and * initializes the device to a known state. * * Zachary Wolf * 2/3/00 */ void btcoil_init(char log_file_in[], struct btcoil_param_struct btcoil_param_in) { /* Save the module parameters */ strcpy(log_file, log_file_in); btcoil_param = btcoil_param_in; /* Initialize the appropriate device */ if (btcoil_param.device_type == BTCOIL_SR850) { sr850_init(btcoil_param.board_addr, btcoil_param.sr850_addr, &dev_ID); sr850_set_ext_ttl_trig(dev_ID); sr850_set_synchronous_filter(dev_ID); } else if (btcoil_param.device_type == BTCOIL_NONE); else btcoil_error("Unknown device type."); /* Log the device type */ btcoil_log_device_type(btcoil_param.device_type); /* Done */ return; } /* ************************************************************** */ /* * btcoil_get_Bt * This function measures the transverse field. * * Output: * Bt, transverse field (T) * th, angle of the south pole (deg) * * Zachary Wolf * 2/3/00 */ void btcoil_get_Bt(double* Bt, double* th) { /* Declare variables */ double freq, v_amp, v_ph; /* Measure the coil voltage */ btcoil_get_v_coil(&freq, &v_amp, &v_ph); /* Calculate Bt */ btcoil_calc_Bt(freq, v_amp, v_ph, Bt, th); /* Done */ return; } /* ************************************************************** */ /* * btcoil_get_v_coil * This function measures the voltage from the rotating coil. * * Output: * freq, coil rotation frequency (Hz) * v_amp, coil voltage, amplitude, 0 to peak (V) * v_ph, coil voltage phase (deg), cosine convention, v = v_amp * cos(wt + v_ph) * * Zachary Wolf * 2/3/00 */ void btcoil_get_v_coil(double* freq, double* v_amp, double* v_ph) { /* Declare variables */ double meas_freq, meas_v_mag, meas_v_ph; /* Perform the coil voltage measurement */ if (btcoil_param.device_type == BTCOIL_SR850) { sr850_automeasure(dev_ID); sr850_get_freq(dev_ID, &meas_freq); sr850_get_vrms(dev_ID, &meas_v_mag); sr850_get_phase(dev_ID, &meas_v_ph); sr850_set_sensitivity(dev_ID, 26); /* Leave on 1 V range so don't overload */ sr850_set_reserve_mode(dev_ID, 0); /* Leave on max reserve so don't overload */ *freq = meas_freq; *v_amp = meas_v_mag * 1.4142136; /* rms -> 0 to peak */ *v_ph = meas_v_ph - 90.; /* cosine convention */ if (*v_ph < -180.) *v_ph = *v_ph + 360.; if (*v_ph > 180.) *v_ph = *v_ph - 360.; } else if (btcoil_param.device_type == BTCOIL_NONE) { meas_freq = 0.; meas_v_mag = 0.; meas_v_ph = 0.; *freq = 0.; *v_amp = 0.; *v_ph = 0; } else btcoil_error("Unknown device type."); /* Log the measurement */ btcoil_log_get_v_coil(meas_freq, meas_v_mag, meas_v_ph, *freq, *v_amp, *v_ph); /* Done */ return; } /* ************************************************************** */ /* * btcoil_calc_Bt * This function calculates the transverse field. * It assumes the coil constant is determined at the rotation frequency. * The input frequency is presently not used. * * Input: * freq, coil rotation frequency (Hz) * v_amp, coil voltage, amplitude, 0 to peak (V) * v_ph, coil voltage phase (deg), cosine convention, v = v_amp * cos(wt + v_ph) * * Output: * Bt, transverse field (T) * th, angle of the south pole (deg) * * Zachary Wolf * 2/3/00 */ void btcoil_calc_Bt(double freq, double v_amp, double v_ph, double* Bt, double* th) { /* Calculate Bt using the coil constant */ *Bt = v_amp * btcoil_param.coil_constant; /* Calculate th from the voltage phase */ *th = -v_ph; /* Log the calculation */ btcoil_log_calc_Bt(freq, v_amp, v_ph, btcoil_param.coil_constant, *Bt, *th); /* Done */ return; } /* ************************************************************** */ /* * btcoil_get_v_coil_raw * This function measures the voltage from the rotating coil. * The units are the device units. No corrections are made. * * Output: * freq, coil rotation frequency * v_amp, coil voltage, amplitude * v_ph, coil voltage phase * * Zachary Wolf * 2/3/00 */ void btcoil_get_v_coil_raw(double* freq, double* v_amp, double* v_ph) { /* Perform the coil voltage measurement */ if (btcoil_param.device_type == BTCOIL_SR850) { sr850_automeasure(dev_ID); sr850_get_freq(dev_ID, freq); sr850_get_vrms(dev_ID, v_amp); sr850_get_phase(dev_ID, v_ph); sr850_set_sensitivity(dev_ID, 26); /* Leave on 1 V range so don't overload */ sr850_set_reserve_mode(dev_ID, 0); /* Leave on max reserve so don't overload */ } else if (btcoil_param.device_type == BTCOIL_NONE) { *freq = 0.; *v_amp = 0.; *v_ph = 0; } else btcoil_error("Unknown device type."); /* Done */ return; } /* ************************************************************** */ /* * btcoil_exit * This function configures the BTCOIL for program exit and * closes the device. * * Zachary Wolf * 2/3/00 */ void btcoil_exit(void) { /* Get the appropriate field measurement */ if (btcoil_param.device_type == BTCOIL_SR850) sr850_exit(dev_ID); else if (btcoil_param.device_type == BTCOIL_NONE); else btcoil_error("Unknown device type."); /* Done */ return; } /* ************************************************************** */ /* PRIVATE FUNCTIONS */ /* ************************************************************** */ /* * btcoil_message * This function handles messages about the BTCOIL. * * Input: * message, string to display in standard I/O * * Zachary Wolf * 12/20/99 */ void btcoil_message(char* message) { /* Print the message */ printf("%s\n", message); /* Done */ return; } /* ************************************************************** */ /* * btcoil_error * This function handles error messages for the BTCOIL. * * Input: * message, string to display in standard I/O * * Zachary Wolf * 12/20/99 */ void btcoil_error(char* message) { /* Declare variables */ char buf[80]; /* Notify the operator of the error */ printf("\nBTCOIL ERROR: %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); } /* ************************************************************** */ /* * btcoil_log_device_type * This function logs the type of Hall probe being used. * * Input: * device_type, type of Hall probe * * Zachary Wolf * 1/31/00 */ void btcoil_log_device_type(enum btcoil_device_type_enum device_type) { /* Declare variables */ FILE* file_ptr; char type[30]; /* Put the device type in a string */ if (device_type == BTCOIL_SR850) strcpy(type, "SR850"); else if (device_type == BTCOIL_NONE) strcpy(type, "None"); else btcoil_error("Unknown device type in log device function."); /* Open the log file */ file_ptr = fopen(log_file, "a"); if (file_ptr == NULL) { btcoil_message("Unable to open log file"); return; } /* Write the measured value to the log file */ fprintf(file_ptr, "%s Btcoil, device being used, type = %s\n", TimeStr(), type); /* Close the log file */ fclose(file_ptr); /* Done */ return; } /* ************************************************************** */ /* * btcoil_log_get_v_coil * This function logs the result of a coil voltage measurement. * * Input: * meas_freq, measured coil rotation frequency (Hz) * meas_v_mag, measured coil voltage magnitude (V) * meas_v_ph, measured coil voltage phase (deg) * freq, coil rotation frequency (Hz) * v_amp, coil voltage, amplitude, 0 to peak (V) * v_ph, coil voltage phase (deg), cosine convention, v = v_amp * cos(wt + v_ph) * * Zachary Wolf * 2/3/00 */ void btcoil_log_get_v_coil(double meas_freq, double meas_v_mag, double meas_v_ph, double freq, double v_amp, double v_ph) { /* Declare variables */ FILE* file_ptr; /* Open the log file */ file_ptr = fopen(log_file, "a"); if (file_ptr == NULL) { btcoil_message("Unable to open log file"); return; } /* Write the measured value to the log file */ fprintf(file_ptr, "%s Coil Voltage Measurement\n", TimeStr()); fprintf(file_ptr, " meas_freq = %f Hz, meas_v_mag = %f V, meas_v_ph = %f deg\n", meas_freq, meas_v_mag, meas_v_ph); fprintf(file_ptr, " freq = %f Hz, v_amp = %f V, v_ph = %f deg\n", freq, v_amp, v_ph); /* Close the log file */ fclose(file_ptr); /* Done */ return; } /* ************************************************************** */ /* * btcoil_log_calc_Bt * This function logs the result of a transverse field calculation. * * Input: * freq, coil rotation frequency (Hz) * v_amp, coil voltage, amplitude, 0 to peak (V) * v_ph, coil voltage phase (deg), cosine convention, v = v_amp * cos(wt + v_ph) * coil_constant, coil constant (T/V_0p) * Bt, transverse field strength (T) * th, south pole angle (deg) * * Zachary Wolf * 2/3/00 */ void btcoil_log_calc_Bt(double freq, double v_amp, double v_ph, double coil_constant, double Bt, double th) { /* Declare variables */ FILE* file_ptr; /* Open the log file */ file_ptr = fopen(log_file, "a"); if (file_ptr == NULL) { btcoil_message("Unable to open log file"); return; } /* Write the measured value to the log file */ fprintf(file_ptr, "%s Transverse Field Calculation\n", TimeStr()); fprintf(file_ptr, " freq = %f Hz, v_amp = %f V, v_ph = %f deg\n", freq, v_amp, v_ph); fprintf(file_ptr, " Coil Constant = %f (T/V0p)\n", coil_constant); fprintf(file_ptr, " Transverse Field, Bt = %f T, THsp = %f deg\n", Bt, th); /* Close the log file */ fclose(file_ptr); /* Done */ return; }