/* ************************************************************** */ /* * Module VCOIL * This module contains functions for measuring the voltage from * a spinning coil. * * Zachary Wolf * 10/8/02 */ /* ************************************************************** */ /* INCLUDES */ #include #include #include #include #include "vcoil.h" #include "vcoilui.h" #include "sr850.h" /* ************************************************************** */ /* PRIVATE FUNCTIONS */ void vcoil_message(char* msg); void vcoil_error(char* msg); void vcoil_log_device_type(enum vcoil_device_type_enum device_type); void vcoil_log_get_vn_coil(int num_har, double v_amp[], double v_ph[]); void vcoil_log_get_vn_coil_raw(int har_num, double v_amp, double v_ph); void vcoil_log_get_coil_rot_freq(double freq); /* ************************************************************** */ /* PRIVATE GLOBAL VARIABLES */ static int sr850_ID; /* ************************************************************** */ /* PRIVATE PARAMETERS */ static char log_file[100]; static struct vcoil_param_struct vcoil_param; /* ************************************************************** */ /* PUBLIC FUNCTIONS */ /* ************************************************************** */ /* * vcoil_init * This function opens the device, queries for ID, and * initializes the device to a known state. * * Zachary Wolf * 10/8/02 */ void vcoil_init(char log_file_in[], struct vcoil_param_struct vcoil_param_in) { /* Save the module parameters */ strcpy(log_file, log_file_in); vcoil_param = vcoil_param_in; /* Initialize the appropriate device */ if (vcoil_param.device_type == VCOIL_SR850) { sr850_init(vcoil_param.board_addr, vcoil_param.sr850_addr, &sr850_ID); sr850_set_ext_ttl_trig(sr850_ID); /* External TTL reference */ sr850_set_time_constant(sr850_ID, 10); /* Time constant set to 1 sec */ sr850_set_synchronous_filter(sr850_ID); /* Turn on synchronous filter, f < 200 Hz */ sr850_set_sensitivity(sr850_ID, 26); /* Sensitivity 1 V range */ sr850_set_reserve_mode(sr850_ID, 0); /* Maximum reserve */ sr850_set_har_num(sr850_ID, 1); /* Fundamental signal */ } else if (vcoil_param.device_type == VCOIL_NONE); else vcoil_error("Unknown device type."); /* Log the device type */ vcoil_log_device_type(vcoil_param.device_type); /* Done */ return; } /* ************************************************************** */ /* * vcoil_get_vn_coil * This function measures the voltage at the n'th harmonic of the * rotation frequency from the coil. * * Input: * num_har, number of harmonics to measure * * Output: * v_amp[0 to num_har], coil voltage amplitude 0 to peak (V), v_amp[0] is unused * v_ph[0 to num_har], coil voltage phase (deg), cosine convention, v = v_amp * cos(nwt + v_ph), v_hp[0] is unused * * Zachary Wolf * 10/8/02 */ void vcoil_get_vn_coil(int num_har, double v_amp[], double v_ph[]) { /* Declare variables */ double meas_v_mag, meas_v_ph; int i; /* Perform the coil voltage measurement */ if (vcoil_param.device_type == VCOIL_SR850) { v_amp[0] = 0.; v_ph[0] = 0.; for (i = 1; i <= num_har; i++) { sr850_set_har_num(sr850_ID, i); sr850_automeasure(sr850_ID); sr850_get_vrms(sr850_ID, &meas_v_mag); sr850_get_phase(sr850_ID, &meas_v_ph); v_amp[i] = meas_v_mag * 1.4142136; /* rms -> 0 to peak */ v_ph[i] = meas_v_ph - 90.; /* cosine convention */ if (v_ph[i] < -180.) v_ph[i] = v_ph[i] + 360.; if (v_ph[i] > 180.) v_ph[i] = v_ph[i] - 360.; } sr850_set_sensitivity(sr850_ID, 26); /* Leave on 1 V range so don't overload */ sr850_set_reserve_mode(sr850_ID, 0); /* Leave on max reserve so don't overload */ } else if (vcoil_param.device_type == VCOIL_NONE) { for (i = 1; i <= num_har; i++) { v_amp[i] = 0.; v_ph[i] = 0; } } else vcoil_error("Unknown device type."); /* Log the measurement */ vcoil_log_get_vn_coil(num_har, v_amp, v_ph); /* Done */ return; } /* ************************************************************** */ /* * vcoil_get_vn_coil_raw * This function measures the voltage at the n'th harmonic of the * rotation frequency from the rotating coil. * The units are the device units. No corrections are made. * * Input: * har_num, desired harmonic number (1, 2, 3, ...) * * Output: * v_amp, coil voltage amplitude (Vrms) * v_ph, coil voltage phase (deg), sine convention, v = sqrt(2) * v_amp * sin(nwt + v_ph) * * Zachary Wolf * 10/8/02 */ void vcoil_get_vn_coil_raw(int har_num, double* v_amp, double* v_ph) { /* Perform the coil voltage measurement */ if (vcoil_param.device_type == VCOIL_SR850) { sr850_set_har_num(sr850_ID, har_num); sr850_automeasure(sr850_ID); sr850_get_vrms(sr850_ID, v_amp); sr850_get_phase(sr850_ID, v_ph); sr850_set_sensitivity(sr850_ID, 26); /* Leave on 1 V range so don't overload */ sr850_set_reserve_mode(sr850_ID, 0); /* Leave on max reserve so don't overload */ } else if (vcoil_param.device_type == VCOIL_NONE) { *v_amp = 0.; *v_ph = 0; } else vcoil_error("Unknown device type."); /* Log the measurement */ vcoil_log_get_vn_coil_raw(har_num, *v_amp, *v_ph); /* Done */ return; } /* ************************************************************** */ /* * vcoil_get_coil_rot_freq * This function measures the rotation frequency from the coil. * * Output: * freq, coil rotation frequency (Hz) * * Zachary Wolf * 10/8/02 */ void vcoil_get_coil_rot_freq(double* freq) { if (vcoil_param.device_type == VCOIL_SR850) { sr850_set_har_num(sr850_ID, 1); /* Set the harmonic number to 1 */ sr850_get_freq(sr850_ID, freq); /* Measure the coil rotation frequency */ } else if (vcoil_param.device_type == VCOIL_NONE) { *freq = 0.; } else vcoil_error("Unknown device type."); /* Log the measurement */ vcoil_log_get_coil_rot_freq(*freq); /* Done */ return; } /* ************************************************************** */ /* * vcoil_exit * This function configures the VCOIL for program exit and * closes the device. * * Zachary Wolf * 10/8/02 */ void vcoil_exit(void) { /* Get the appropriate field measurement */ if (vcoil_param.device_type == VCOIL_SR850) sr850_exit(sr850_ID); else if (vcoil_param.device_type == VCOIL_NONE); else vcoil_error("Unknown device type."); /* Done */ return; } /* ************************************************************** */ /* PRIVATE FUNCTIONS */ /* ************************************************************** */ /* * vcoil_message * This function handles messages about the VCOIL. * * Input: * message, string to display in standard I/O * * Zachary Wolf * 12/20/99 */ void vcoil_message(char* message) { /* Print the message */ printf("%s\n", message); /* Done */ return; } /* ************************************************************** */ /* * vcoil_error * This function handles error messages for the VCOIL. * * Input: * message, string to display in standard I/O * * Zachary Wolf * 12/20/99 */ void vcoil_error(char* message) { /* Declare variables */ char buf[80]; /* Notify the operator of the error */ printf("\nVCOIL 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); } /* ************************************************************** */ /* * vcoil_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 vcoil_log_device_type(enum vcoil_device_type_enum device_type) { /* Declare variables */ FILE* file_ptr; char type[30]; /* Put the device type in a string */ if (device_type == VCOIL_SR850) strcpy(type, "SR850"); else if (device_type == VCOIL_NONE) strcpy(type, "None"); else vcoil_error("Unknown device type in log device function."); /* Open the log file */ file_ptr = fopen(log_file, "a"); if (file_ptr == NULL) { vcoil_message("Unable to open log file"); return; } /* Write the measured value to the log file */ fprintf(file_ptr, "%s Vcoil, device being used, type = %s\n", TimeStr(), type); /* Close the log file */ fclose(file_ptr); /* Done */ return; } /* ************************************************************** */ /* * vcoil_log_get_vn_coil * This function logs the result of a coil voltage measurement. * * Input: * num_har, number of harmonics * v_amp[0 to num_har], coil voltage, amplitude, 0 to peak (V) * v_ph[0 to num_har], coil voltage phase (deg), cosine convention, v = v_amp * cos(nwt + v_ph) * * Zachary Wolf * 10/8/02 */ void vcoil_log_get_vn_coil(int num_har, double v_amp[], double v_ph[]) { /* Declare variables */ FILE* file_ptr; int i; /* Open the log file */ file_ptr = fopen(log_file, "a"); if (file_ptr == NULL) { vcoil_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, " n v_amp v_ph \n"); fprintf(file_ptr, " (V0p) (deg) \n"); fprintf(file_ptr, " --- ---------- ----------\n"); for (i = 1; i <= num_har; i++) fprintf(file_ptr, " %3i %10.7f %10.3f\n", i, v_amp[i], v_ph[i]); /* Close the log file */ fclose(file_ptr); /* Done */ return; } /* ************************************************************** */ /* * vcoil_log_get_vn_coil_raw * This function logs the result of a coil voltage measurement. * * Input: * har_num, harmonic number * v_amp, coil voltage amplitude (Vrms) * v_ph, coil voltage phase (deg), sine convention, v = sqrt(2) * v_amp * sin(nwt + v_ph) * * Zachary Wolf * 10/8/02 */ void vcoil_log_get_vn_coil_raw(int har_num, 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) { vcoil_message("Unable to open log file"); return; } /* Write the measured value to the log file */ fprintf(file_ptr, "%s Raw Coil Voltage Measurement\n", TimeStr()); fprintf(file_ptr, " har_num = %i, v_amp = %f Vrms, v_ph = %f deg\n", har_num, v_amp, v_ph); /* Close the log file */ fclose(file_ptr); /* Done */ return; } /* ************************************************************** */ /* * vcoil_log_get_coil_rot_freq * This function logs the result of a coil rotation frequency measurement. * * Input: * freq, coil rotation frequency (Hz) * * Zachary Wolf * 10/8/02 */ void vcoil_log_get_coil_rot_freq(double freq) { /* Declare variables */ FILE* file_ptr; /* Open the log file */ file_ptr = fopen(log_file, "a"); if (file_ptr == NULL) { vcoil_message("Unable to open log file"); return; } /* Write the measured value to the log file */ fprintf(file_ptr, "%s Coil Rotation Frequency Measurement\n", TimeStr()); fprintf(file_ptr, " freq = %f Hz\n", freq); /* Close the log file */ fclose(file_ptr); /* Done */ return; }