/* ************************************************************** */ /* * Module SENSOR * This module contains functions for making sensor voltage, resistance, ... * measurements. * * Zachary Wolf * 4/12/00 */ /* ************************************************************** */ /* INCLUDES */ #include #include #include #include #include "sensor.h" #include "hp3457.h" #include "k7011.h" /* ************************************************************** */ /* PRIVATE FUNCTIONS */ void sensor_message(char* msg); void sensor_error(char* msg); void sensor_log_device_type(enum sensor_device_type_enum device_type); void sensor_log_chan_voltage(int chan, double v); void sensor_log_multi_chan_voltages(int num_chan, int chan[], double v[]); /* ************************************************************** */ /* PRIVATE GLOBAL VARIABLES */ static int hp3457_ID; static int k7011_ID; /* ************************************************************** */ /* PRIVATE PARAMETERS */ static char log_file[100]; static struct sensor_param_struct sensor_param; /* ************************************************************** */ /* PUBLIC FUNCTIONS */ /* ************************************************************** */ /* * sensor_init * This function opens the device, queries for ID, and * initializes the device to a known state. * * Zachary Wolf * 4/12/00 */ void sensor_init(char log_file_in[], struct sensor_param_struct sensor_param_in) { /* Save the module parameters */ strcpy(log_file, log_file_in); sensor_param = sensor_param_in; /* Initialize the appropriate devices */ if (sensor_param.device_type == SENSOR_HP3457) { hp3457_init(sensor_param.board_addr, sensor_param.hp3457_addr, &hp3457_ID); } else if (sensor_param.device_type == SENSOR_K7011_HP3457) { k7011_init(sensor_param.board_addr, sensor_param.k7011_addr, &k7011_ID); hp3457_init(sensor_param.board_addr, sensor_param.hp3457_addr, &hp3457_ID); } else if (sensor_param.device_type == SENSOR_NONE); else sensor_error("Unknown device type."); /* Log the device type */ sensor_log_device_type(sensor_param.device_type); /* Done */ return; } /* ************************************************************** */ /* * sensor_default_setup * This function sets up the system in a default state. * * Zachary Wolf * 4/12/00 */ void sensor_default_setup(void) { /* Set up the appropriate Hall probe */ if (sensor_param.device_type == SENSOR_HP3457) { hp3457_preset(hp3457_ID); } else if (sensor_param.device_type == SENSOR_K7011_HP3457) { k7011_open_all(k7011_ID); hp3457_preset(hp3457_ID); } else if (sensor_param.device_type == SENSOR_NONE); else sensor_error("Unknown device type."); /* Done */ return; } /* ************************************************************** */ /* * sensor_get_chan_voltage * This function gets the sensor voltage from a specified channel. * * Input: * chan, channel number to read the voltage from * * Output: * v, voltage from the sensor (V) * * Zachary Wolf * 4/13/00 */ void sensor_get_chan_voltage(int chan, double* v) { /* Get the appropriate sensor voltage measurement */ if (sensor_param.device_type == SENSOR_HP3457) { hp3457_get_chan_voltage(hp3457_ID, chan, v); } else if (sensor_param.device_type == SENSOR_K7011_HP3457) { k7011_close_card_chan(k7011_ID, sensor_param.k7011_card_num, chan); hp3457_get_chan_voltage(hp3457_ID, sensor_param.k7011_hp3457_chan, v); k7011_open_all(k7011_ID); } else if (sensor_param.device_type == SENSOR_NONE) *v = 0.; else sensor_error("Unknown device type."); /* Log the measurement */ sensor_log_chan_voltage(chan, *v); /* Done */ return; } /* ************************************************************** */ /* * sensor_get_multi_chan_voltages * This function gets the sensor voltages from the specified channels. * * Input: * num_chan, number of channels * chan[0 to num_chan - 1], channel numbers to read the voltage from * * Output: * v[0 to num_chan - 1], voltages from the sensors (V) * * Zachary Wolf * 4/13/00 */ void sensor_get_multi_chan_voltages(int num_chan, int chan[], double v[]) { /* Declare variables */ int i; /* Check input parameters */ if (num_chan < 1 || num_chan > SENSOR_MAX_NUM_CHAN) { sensor_error("sensor_get_multi_chan_voltages: improper num_chan"); return; } for (i = 0; i < num_chan; i++) { if (chan[i] < 0) { sensor_error("sensor_get_multi_chan_voltages: improper channel number"); return; } } /* Get the appropriate sensor voltage measurements */ if (sensor_param.device_type == SENSOR_HP3457) { hp3457_setup_voltage(hp3457_ID); //for (i = 0; i < num_chan; i++) hp3457_read_chan_voltage(hp3457_ID, chan[i], &v[i]); for (i = num_chan - 1; i >= 0; i--) hp3457_read_chan_voltage(hp3457_ID, chan[i], &v[i]); /* Done so level is read last in the gap system */ } else if (sensor_param.device_type == SENSOR_K7011_HP3457) { hp3457_setup_voltage(hp3457_ID); for (i = 0; i < num_chan; i++) { k7011_close_card_chan(k7011_ID, sensor_param.k7011_card_num, chan[i]); hp3457_read_chan_voltage(hp3457_ID, sensor_param.k7011_hp3457_chan, &v[i]); k7011_open_all(k7011_ID); } } else if (sensor_param.device_type == SENSOR_NONE) for (i = 0; i < num_chan; i++) v[i] = 0.; else sensor_error("Unknown device type."); /* Log the measurement */ sensor_log_multi_chan_voltages(num_chan, chan, v); /* Done */ return; } /* ************************************************************** */ /* * sensor_exit * This function configures the sensor system for program exit. * * Zachary Wolf * 4/13/00 */ void sensor_exit(void) { /* Get the appropriate field measurement */ if (sensor_param.device_type == SENSOR_HP3457) { hp3457_exit(hp3457_ID); } else if (sensor_param.device_type == SENSOR_K7011_HP3457) { k7011_exit(k7011_ID); hp3457_exit(hp3457_ID); } else if (sensor_param.device_type == SENSOR_NONE); else sensor_error("Unknown device type."); /* Done */ return; } /* ************************************************************** */ /* PRIVATE FUNCTIONS */ /* ************************************************************** */ /* * sensor_message * This function handles messages about the SENSOR. * * Input: * message, string to display in standard I/O * * Zachary Wolf * 12/20/99 */ void sensor_message(char* message) { /* Print the message */ printf("%s\n", message); /* Done */ return; } /* ************************************************************** */ /* * sensor_error * This function handles error messages for the SENSOR. * * Input: * message, string to display in standard I/O * * Zachary Wolf * 12/20/99 */ void sensor_error(char* message) { /* Declare variables */ char buf[80]; /* Notify the operator of the error */ printf("\nSENSOR 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); } /* ************************************************************** */ /* * sensor_log_device_type * This function logs the type of Hall probe being used. * * Input: * device_type, type of sensor readout devices * * Zachary Wolf * 4/13/00 */ void sensor_log_device_type(enum sensor_device_type_enum device_type) { /* Declare variables */ FILE* file_ptr; char type[30]; /* Put the device type in a string */ if (device_type == SENSOR_HP3457) strcpy(type, "HP3457"); else if (device_type == SENSOR_K7011_HP3457) strcpy(type, "K7011_HP3457"); else if (device_type == SENSOR_NONE) strcpy(type, "None"); else sensor_error("Unknown device type in log device function."); /* Open the log file */ file_ptr = fopen(log_file, "a"); if (file_ptr == NULL) { sensor_message("Unable to open log file"); return; } /* Write the measured value to the log file */ fprintf(file_ptr, "%s Sensor Readout Device(s) Being Used, type = %s\n", TimeStr(), type); /* Close the log file */ fclose(file_ptr); /* Done */ return; } /* ************************************************************** */ /* * sensor_log_chan_voltage * This function logs the result of a channel voltage measurement. * * Input: * chan, channel number * v, voltage (V) * * Zachary Wolf * 4/13/00 */ void sensor_log_chan_voltage(int chan, double v) { /* Declare variables */ FILE* file_ptr; /* Open the log file */ file_ptr = fopen(log_file, "a"); if (file_ptr == NULL) { sensor_message("Unable to open log file"); return; } /* Write the measured value to the log file */ fprintf(file_ptr, "%s Sensor Voltage Measurement, chan = %i, V = %f V\n", TimeStr(), chan, v); /* Close the log file */ fclose(file_ptr); /* Done */ return; } /* ************************************************************** */ /* * sensor_log_multi_chan_voltages * This function logs the result of a multi-channel voltage measurement. * * Input: * num_chan, number of channels * chan[0 to num_chan - 1], channel numbers * v[0 to num_chan - 1], voltages (V) * * Zachary Wolf * 4/13/00 */ void sensor_log_multi_chan_voltages(int num_chan, int chan[], double v[]) { /* Declare variables */ FILE* file_ptr; int i; /* Open the log file */ file_ptr = fopen(log_file, "a"); if (file_ptr == NULL) { sensor_message("Unable to open log file"); return; } /* Write the measured value to the log file */ fprintf(file_ptr, "%s Sensor Voltage Measurements:\n", TimeStr()); for (i = 0; i < num_chan; i++) fprintf(file_ptr, " chan = %3i, V = %12.7f V\n", chan[i], v[i]); /* Close the log file */ fclose(file_ptr); /* Done */ return; }