/* ************************************************************** */ /* * Module XMAG * This module contains functions for making special magnet * parameter measurements. It needs to be edited to add devices * for each specialized measurement. * * Zachary Wolf * 4/5/01 */ /* ************************************************************** */ /* INCLUDES */ #include #include #include #include #include "xmag.h" #include "vrz404.h" /* ************************************************************** */ /* PRIVATE FUNCTIONS */ void xmag_message(char* msg); void xmag_error(char* msg); void xmag_log_device_type(enum xmag_device_type_enum device_type); void xmag_log_dev_meas(int dev_num, char* name, double meas); /* ************************************************************** */ /* PRIVATE GLOBAL VARIABLES */ static int dev_ID_0; static int dev_ID_1; /* ************************************************************** */ /* PRIVATE PARAMETERS */ static char log_file[100]; static struct xmag_param_struct xmag_param; /* ************************************************************** */ /* PUBLIC FUNCTIONS */ /* ************************************************************** */ /* * xmag_init * This function opens devices, queries for ID, and * initializes the devices to a known state. * * Zachary Wolf * 4/5/01 */ void xmag_init(char log_file_in[], struct xmag_param_struct xmag_param_in) { /* Save the module parameters */ strcpy(log_file, log_file_in); xmag_param = xmag_param_in; /* Check parameters */ if (xmag_param.num_dev < 0 || xmag_param.num_dev > XMAG_MAX_NUM_DEV) { xmag_error("xmag_init: Improper num_dev"); return; } /* Initialize the appropriate devices */ if (xmag_param.device_type == XMAG_VRZ404a_VRZ404b) { vrz404_init(0, xmag_param.vrz404a_addr, &dev_ID_0); vrz404_init(0, xmag_param.vrz404b_addr, &dev_ID_1); } else if (xmag_param.device_type == XMAG_NONE); else xmag_error("Unknown device type."); /* Log the device type */ xmag_log_device_type(xmag_param.device_type); /* Done */ return; } /* ************************************************************** */ /* * xmag_get_num_dev * This function gets the number of devices. * * Output: * num_dev, number of xmag devices * * Zachary Wolf * 4/5/01 */ void xmag_get_num_dev(int* num_dev) { /* Return the number of devices */ if (xmag_param.device_type != XMAG_NONE) *num_dev = xmag_param.num_dev; else *num_dev = 0; /* Done */ return; } /* ************************************************************** */ /* * xmag_get_dev_name * This function gets the name of a specified device. * * Input: * dev_num, device number, 0 to num_dev - 1 * * Output: * name, name describing the measurement (including units) * * Zachary Wolf * 4/5/01 */ void xmag_get_dev_name(int dev_num, char* dev_name) { /* Check parameters */ if (xmag_param.device_type == XMAG_NONE) return; if (dev_num < 0 || dev_num >= xmag_param.num_dev) { xmag_error("Improper dev_num"); return; } /* Return the name of the device */ if (dev_num == 0 && xmag_param.device_type == XMAG_VRZ404a_VRZ404b) { strcpy(dev_name, xmag_param.dev_name[dev_num]); } else if (dev_num == 1 && xmag_param.device_type == XMAG_VRZ404a_VRZ404b) { strcpy(dev_name, xmag_param.dev_name[dev_num]); } else if (xmag_param.device_type == XMAG_NONE) strcpy(dev_name, ""); else xmag_error("Unknown device type."); /* Done */ return; } /* ************************************************************** */ /* * xmag_get_dev_meas * This function gets the measurement from a specified device. * * Input: * dev_num, device number, 0 to num_dev - 1 * * Output: * meas, measurement from the specified device * * Zachary Wolf * 4/5/01 */ void xmag_get_dev_meas(int dev_num, double* dev_meas) { /* Declare variables */ char dev_name[XMAG_MAX_NAME_LENGTH]; /* Check parameters */ if (xmag_param.device_type == XMAG_NONE) return; if (dev_num < 0 || dev_num >= xmag_param.num_dev) { xmag_error("Improper dev_num"); return; } /* Perform the measurement */ if (dev_num == 0 && xmag_param.device_type == XMAG_VRZ404a_VRZ404b) { vrz404_get_pos(dev_ID_0, dev_meas); *dev_meas = *dev_meas * 1.e6; /* Convert from meters to microns */ } else if (dev_num == 1 && xmag_param.device_type == XMAG_VRZ404a_VRZ404b) { vrz404_get_pos(dev_ID_1, dev_meas); *dev_meas = *dev_meas * 1.e6; /* Convert from meters to microns */ } else if (xmag_param.device_type == XMAG_NONE) *dev_meas = 0.; else xmag_error("Unknown device type."); /* Log the measurement */ xmag_get_dev_name(dev_num, dev_name); xmag_log_dev_meas(dev_num, dev_name, *dev_meas); /* Done */ return; } /* ************************************************************** */ /* * xmag_exit * This function configures the xmag system for program exit. * * Zachary Wolf * 4/5/01 */ void xmag_exit(void) { /* Exit the appropriate devices */ if (xmag_param.device_type == XMAG_VRZ404a_VRZ404b) { vrz404_exit(dev_ID_0); vrz404_exit(dev_ID_1); } else if (xmag_param.device_type == XMAG_NONE); else xmag_error("Unknown device type."); /* Done */ return; } /* ************************************************************** */ /* PRIVATE FUNCTIONS */ /* ************************************************************** */ /* * xmag_message * This function handles messages for the module. * * Input: * message, string to display in standard I/O * * Zachary Wolf * 4/5/01 */ void xmag_message(char* message) { /* Print the message */ printf("%s\n", message); /* Done */ return; } /* ************************************************************** */ /* * xmag_error * This function handles error messages for the module. * * Input: * message, string to display in standard I/O * * Zachary Wolf * 4/5/01 */ void xmag_error(char* message) { /* Declare variables */ char buf[80]; /* Notify the operator of the error */ printf("\nXMAG 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); } /* ************************************************************** */ /* * xmag_log_device_type * This function logs the type of equipment being used. * * Input: * device_type, type of xmag devices * * Zachary Wolf * 4/13/01 */ void xmag_log_device_type(enum xmag_device_type_enum device_type) { /* Declare variables */ FILE* file_ptr; char type[80]; /* Put the device type in a string */ if (device_type == XMAG_VRZ404a_VRZ404b) strcpy(type, "Heidenhain VRZ404 counters"); else if (device_type == XMAG_NONE) strcpy(type, "None"); else xmag_error("Unknown device type in log device function."); /* Open the log file */ file_ptr = fopen(log_file, "a"); if (file_ptr == NULL) { xmag_message("Unable to open log file"); return; } /* Write the device type to the log file */ fprintf(file_ptr, "%s XMAG Device(s) Being Used, type = %s\n", TimeStr(), type); /* Close the log file */ fclose(file_ptr); /* Done */ return; } /* ************************************************************** */ /* * xmag_log_dev_meas * This function logs the result of a measurement. * * Input: * dev_num, device number * name, device name including units * meas, measured value * * Zachary Wolf * 4/5/01 */ void xmag_log_dev_meas(int dev_num, char* name, double meas) { /* Declare variables */ FILE* file_ptr; /* Open the log file */ file_ptr = fopen(log_file, "a"); if (file_ptr == NULL) { xmag_message("Unable to open log file"); return; } /* Write the measured value to the log file */ fprintf(file_ptr, "%s XMAG Measurement, Dev # = %i, Name = %s, Meas = %f\n", TimeStr(), dev_num, name, meas); /* Close the log file */ fclose(file_ptr); /* Done */ return; }