/* ************************************************************** */ /* * Module SENMEAS * This module contains functions for making various sensor * measurements. * * Zachary Wolf * 2/7/03 */ /* ************************************************************** */ /* INCLUDES */ #include #include #include #include #include "sensor.h" #include "calib.h" #include "senmeas.h" /* ************************************************************** */ /* PRIVATE FUNCTIONS */ void senmeas_message(char* msg); void senmeas_error(char* msg); void senmeas_log_get_sensor_raw_output(int sensor_num, double sensor_raw_output); void senmeas_log_scan_sensor_raw_output(int num_sensor, int sensor_num[], double sensor_raw_output[]); void senmeas_log_apply_sensor_calib(int sensor_num, double sensor_raw_output, double sensor_meas_val); void senmeas_log_get_sensor_meas_val(int sensor_num, double sensor_raw_output, double sensor_meas_val); void senmeas_log_scan_sensor_meas_val(int num_sensor, int sensor_num[], double sensor_raw_output[], double sensor_meas_val[]); /* ************************************************************** */ /* PRIVATE PARAMETERS */ static char log_file[100]; static struct senmeas_param_struct senmeas_param; /* ************************************************************** */ /* PUBLIC FUNCTIONS */ /* ************************************************************** */ /* * senmeas_init * This function initializes the SENMEAS module. * * Zachary Wolf * 2/7/03 */ void senmeas_init(char log_file_in[], struct senmeas_param_struct senmeas_param_in) { /* Save the module parameters */ strcpy(log_file, log_file_in); senmeas_param = senmeas_param_in; /* Done */ return; } /* ************************************************************** */ /* * senmeas_get_sensor_info * This function gets the information about a specified sensor. * * Input: * sensor_num, sensor number * * Output: * sensor_info, information about the sensor * * Zachary Wolf * 2/7/03 */ void senmeas_get_sensor_info(int sensor_num, struct sensor_info_struct* sensor_info) { /* Check input parameters */ if (sensor_num < 0 || sensor_num >= senmeas_param.num_sensor) { senmeas_error("Invalid sensor number."); return; } /* Return the sensor information */ *sensor_info = senmeas_param.sensor_info[sensor_num]; /* Done */ return; } /* ************************************************************** */ /* * senmeas_get_sensor_raw_output * This function gets the output from the specified sensor. * * Input: * sensor_num, sensor number * * Output: * sensor_raw_output, sensor output (V, ohm, ...) * * Zachary Wolf * 2/7/03 */ void senmeas_get_sensor_raw_output(int sensor_num, double* sensor_raw_output) { /* Declare variables */ int chan; enum senmeas_meas_type_enum meas_type; /* Check input parameters */ if (sensor_num < 0 || sensor_num >= senmeas_param.num_sensor) { senmeas_error("Invalid sensor number."); *sensor_raw_output = 0.; return; } /* Perform the appropiate measurement */ meas_type = senmeas_param.sensor_info[sensor_num].meas_type; chan = senmeas_param.sensor_info[sensor_num].channel; if (meas_type == SENMEAS_V) sensor_get_chan_volt(chan, sensor_raw_output); else if (meas_type == SENMEAS_R) sensor_get_chan_res(chan, sensor_raw_output); else if (meas_type == SENMEAS_R_4WIRE) sensor_get_chan_4wire_res(chan, sensor_raw_output); else if (meas_type == SENMEAS_T) sensor_get_chan_temp(chan, sensor_raw_output); else senmeas_error("Unknown measurement type."); /* Log the measurement */ senmeas_log_get_sensor_raw_output(sensor_num, *sensor_raw_output); /* Done */ return; } /* ************************************************************** */ /* * senmeas_scan_sensor_raw_output * This function gets the output from all the sensors. * * Output: * num_sensor, number of sensors * sensor_num[0 to num_sensor - 1], sensor number * sensor_raw_output[0 to num_sensor - 1], sensor output (V, ohm, ...) * * Zachary Wolf * 2/7/03 */ void senmeas_scan_sensor_raw_output(int* num_sensor, int sensor_num[], double sensor_raw_output[]) { /* Declare variables */ int i; int chan[SENMEAS_MAX_NUM_SENSOR]; char meas[SENMEAS_MAX_NUM_SENSOR]; enum senmeas_meas_type_enum meas_type; /* Fill in the channel number and measurement type for each sensor */ for (i = 0; i < senmeas_param.num_sensor; i++) { chan[i] = senmeas_param.sensor_info[i].channel; meas_type = senmeas_param.sensor_info[i].meas_type; if (meas_type == SENMEAS_V) meas[i] = 'v'; else if (meas_type == SENMEAS_R) meas[i] = 'r'; else if (meas_type == SENMEAS_R_4WIRE) meas[i] = '4'; else if (meas_type == SENMEAS_T) senmeas_error("Unknown measurement type for scan."); else senmeas_error("Unknown measurement type."); } /* Fill in the number of sensors */ *num_sensor = senmeas_param.num_sensor; /* Fill in the sensor number array */ for (i = 0; i < senmeas_param.num_sensor; i++) sensor_num[i] = i; /* Get the sensor outputs */ sensor_scan_chan(*num_sensor, chan, meas, sensor_raw_output); /* Log the measurement */ senmeas_log_scan_sensor_raw_output(*num_sensor, sensor_num, sensor_raw_output); /* Done */ return; } /* ************************************************************** */ /* * senmeas_apply_sensor_calib * This function applies a calibration to get from the * sensor raw output to the measured value. * * Input: * sensor_num, sensor number * sensor_raw_output, sensor output (V, ohm, ...) * * Output: * sensor_meas_val, sensor measured value (deg C, atm, ...) * * Zachary Wolf * 2/7/03 */ void senmeas_apply_sensor_calib(int sensor_num, double sensor_raw_output, double* sensor_meas_val) { /* Declare variables */ enum senmeas_calib_type_enum calib_type; char calib_file_name[SENMEAS_MAX_NUM_CHAR]; struct calib_data_struct calib_data; struct calib_poly_fit_struct calib_poly_fit; struct calib_steinhart_hart_struct calib_steinhart_hart; /* Check input parameters */ if (sensor_num < 0 || sensor_num >= senmeas_param.num_sensor) { senmeas_error("Invalid sensor number."); *sensor_meas_val = 0.; return; } /* Get the calibration information and perform the calibration */ calib_type = senmeas_param.sensor_info[sensor_num].calib_type; strcpy(calib_file_name, senmeas_param.sensor_info[sensor_num].calib_file_name); if (calib_type == SENMEAS_CALIB_DATA) { calib_get_calib_data(calib_file_name, &calib_data); calib_apply_calib_data_cubic_spline_interp(calib_data, 1, &sensor_raw_output, sensor_meas_val); } else if (calib_type == SENMEAS_POLY_FIT) { calib_get_calib_poly_fit(calib_file_name, &calib_poly_fit); calib_apply_calib_poly_fit_interp(calib_poly_fit, 1, &sensor_raw_output, sensor_meas_val); } else if (calib_type == SENMEAS_STEINHART_HART) { calib_get_calib_steinhart_hart(calib_file_name, &calib_steinhart_hart); calib_apply_calib_steinhart_hart(calib_steinhart_hart, sensor_raw_output, sensor_meas_val); } else if (calib_type == SENMEAS_CALIB_NONE) { *sensor_meas_val = sensor_raw_output; } else senmeas_error("Unknown calibration type."); /* Log the measurement */ senmeas_log_apply_sensor_calib(sensor_num, sensor_raw_output, *sensor_meas_val); /* Done */ return; } /* ************************************************************** */ /* * senmeas_get_sensor_meas_val * This function performs a measurement using the specified sensor. * * Input: * sensor_num, sensor number * * Output: * sensor_meas_val, sensor measured value (deg C, atm, ...) * * Zachary Wolf * 2/7/03 */ void senmeas_get_sensor_meas_val(int sensor_num, double* sensor_meas_val) { /* Declare variables */ double sensor_raw_output; /* Check input parameters */ if (sensor_num < 0 || sensor_num >= senmeas_param.num_sensor) { senmeas_error("Invalid sensor number."); *sensor_meas_val = 0.; return; } /* Measure the sensor output */ senmeas_get_sensor_raw_output(sensor_num, &sensor_raw_output); /* Perform the calibration */ senmeas_apply_sensor_calib(sensor_num, sensor_raw_output, sensor_meas_val); /* Log the measurement */ senmeas_log_get_sensor_meas_val(sensor_num, sensor_raw_output, *sensor_meas_val); /* Done */ return; } /* ************************************************************** */ /* * senmeas_scan_sensor_meas_val * This function performs measurements using all the sensors. * * Output: * num_sensor, number of sensors * sensor_num[0 to num_sensor - 1], sensor number * sensor_meas_val[0 to num_sensor - 1], sensor measured values (deg C, atm, ...) * * Zachary Wolf * 2/7/03 */ void senmeas_scan_sensor_meas_val(int* num_sensor, int sensor_num[], double sensor_meas_val[]) { /* Declare variables */ double sensor_raw_output[SENMEAS_MAX_NUM_SENSOR]; int i; /* Scan the sensor outputs */ senmeas_scan_sensor_raw_output(num_sensor, sensor_num, sensor_raw_output); /* Perform the calibrations */ for (i = 0; i < *num_sensor; i++) senmeas_apply_sensor_calib(sensor_num[i], sensor_raw_output[i], &sensor_meas_val[i]); /* Log the measurement */ senmeas_log_scan_sensor_meas_val(*num_sensor, sensor_num, sensor_raw_output, sensor_meas_val); /* Done */ return; } /* ************************************************************** */ /* * senmeas_measure_all * This function returns measurements from all sensors. * * Output: * num_sensor, number of sensors * sensor_info[0 to num_sensor - 1], information about each sensor * sensor_meas_val[0 to num_sensor - 1], measured values (deg C, atm, ...) * * Zachary Wolf * 2/7/03 */ void senmeas_measure_all(int* num_sensor, struct sensor_info_struct sensor_info[], double sensor_meas_val[]) { /* Declare variables */ int i; /* Number of sensors */ *num_sensor = senmeas_param.num_sensor; /* Sensor information */ for (i = 0; i < senmeas_param.num_sensor; i++) sensor_info[i] = senmeas_param.sensor_info[i]; /* Sensor measurements */ for (i = 0; i < senmeas_param.num_sensor; i++) senmeas_get_sensor_meas_val(i, &sensor_meas_val[i]); /* Write a header for the results */ printf("\n"); printf(" Sensor Measurements\n"); printf("\n"); for (i = 0; i < senmeas_param.num_sensor; i++) printf("S%i = %s\n", i, senmeas_param.sensor_info[i].description); printf("\n"); for (i = 0; i < senmeas_param.num_sensor; i++) printf(" S%i ", i); printf("\n"); for (i = 0; i < senmeas_param.num_sensor; i++) printf(" -------"); printf("\n"); /* Write the data */ for (i = 0; i < senmeas_param.num_sensor; i++) printf(" %7.2f", sensor_meas_val[i]); printf("\n"); /* Done */ return; } /* ************************************************************** */ /* * senmeas_measure_scan_all * This function returns measurements from all sensors. * * Output: * num_sensor, number of sensors * sensor_info[0 to num_sensor - 1], information about each sensor * sensor_meas_val[0 to num_sensor - 1], measured values (deg C, atm, ...) * * Zachary Wolf * 2/7/03 */ void senmeas_measure_scan_all(int* num_sensor, struct sensor_info_struct sensor_info[], double sensor_meas_val[]) { /* Declare variables */ int i; int sensor_num[SENMEAS_MAX_NUM_SENSOR]; /* Perform the sensor measurements */ senmeas_scan_sensor_meas_val(num_sensor, sensor_num, sensor_meas_val); /* Sensor information */ for (i = 0; i < *num_sensor; i++) sensor_info[sensor_num[i]] = senmeas_param.sensor_info[sensor_num[i]]; /* Write a header for the results */ printf("\n"); printf(" Sensor Measurements\n"); printf("\n"); for (i = 0; i < *num_sensor; i++) printf("S%i = %s\n", i, senmeas_param.sensor_info[sensor_num[i]].description); printf("\n"); for (i = 0; i < *num_sensor; i++) printf(" S%i ", i); printf("\n"); for (i = 0; i < *num_sensor; i++) printf(" -------"); printf("\n"); /* Write the data */ for (i = 0; i < *num_sensor; i++) printf(" %7.2f", sensor_meas_val[i]); printf("\n"); /* Done */ return; } /* ************************************************************** */ /* * senmeas_dat_measure_all * This function writes the results of sensor measurements * to the data file. * * Input: * dat_file_name, name of the data file * num_sensor, number of sensors * sensor_info[0 to num_sensor - 1], information about each sensor * sensor_meas_val[0 to num_sensor - 1], measured values (deg C, atm, ...) * * Zachary Wolf * 2/7/03 */ void senmeas_dat_measure_all(char dat_file_name[], int num_sensor, struct sensor_info_struct sensor_info[], double sensor_meas_val[]) { /* Declare variables */ FILE* file_ptr; int i; /* Open the data file */ file_ptr = fopen(dat_file_name, "a"); if (file_ptr == NULL) { printf("senmeas_dat_measure_all: Unable to open dat file\n"); return; } /* Write a header */ fprintf(file_ptr, "\nSensor Measurements:\n"); /* Write the data */ for (i = 0; i < num_sensor; i++) fprintf(file_ptr, "%s, S%i = %7.2f\n", sensor_info[i].description, i, sensor_meas_val[i]); /* Close the dat file */ fclose(file_ptr); /* Done */ return; } /* ************************************************************** */ /* * senmeas_dat_measure_all_vs_time * This function writes the results of sensor measurements * to the data file. * * Input: * dat_file_name, name of the data file * num_sensor, number of sensors * sensor_info[0 to num_sensor - 1], information about each sensor * sensor_meas_val[0 to num_sensor - 1], measured values (deg C, atm, ...) * * Zachary Wolf * 2/7/03 */ void senmeas_dat_measure_all_vs_time(char dat_file_name[], int num_sensor, struct sensor_info_struct sensor_info[], double sensor_meas_val[]) { /* Declare variables */ FILE* file_ptr; static int call_num; int i; /* Open the data file */ file_ptr = fopen(dat_file_name, "a"); if (file_ptr == NULL) { printf("senmeas_dat_measure_all_vs_time: Unable to open dat file\n"); return; } /* Increment the number of calls */ call_num++; /* Write a header on the first call */ if (call_num == 1) { fprintf(file_ptr, "\n"); fprintf(file_ptr, "\n"); fprintf(file_ptr, " Sensor Measurements\n"); fprintf(file_ptr, "\n"); for (i = 0; i < num_sensor; i++) { fprintf(file_ptr, "S%i = %s\n", i, sensor_info[i].description); } fprintf(file_ptr, "\n"); fprintf(file_ptr, " Time "); for (i = 0; i < num_sensor; i++) fprintf(file_ptr, " S%i ", i); fprintf(file_ptr, "\n"); fprintf(file_ptr, "--------"); for (i = 0; i < num_sensor; i++) fprintf(file_ptr, " -------"); fprintf(file_ptr, "\n"); } /* Write the data */ fprintf(file_ptr, "%s", TimeStr()); for (i = 0; i < num_sensor; i++) fprintf(file_ptr, " %7.2f", sensor_meas_val[i]); fprintf(file_ptr, "\n"); /* Close the dat file */ fclose(file_ptr); /* Done */ return; } /* ************************************************************** */ /* * senmeas_plt_measure_all_vs_time * This function writes the results of sensor measurements * to the plot file. * * Input: * plt_file_name, name of the plot file * num_sensor, number of sensors * sensor_info[0 to num_sensor - 1], information about each sensor * sensor_meas_val[0 to num_sensor - 1], measured values (deg C, atm, ...) * * Zachary Wolf * 2/7/03 */ void senmeas_plt_measure_all_vs_time(char plt_file_name[], int num_sensor, struct sensor_info_struct sensor_info[], double sensor_meas_val[]) { /* Declare variables */ FILE* file_ptr; static int call_num; static time_t t_0; time_t t_now; int i; /* Open the data file */ file_ptr = fopen(plt_file_name, "a"); if (file_ptr == NULL) { printf("senmeas_plt_measure_all_vs_time: Unable to open plt file\n"); return; } /* Increment the number of calls */ call_num++; /* Write a header on the first call */ if (call_num == 1) { fprintf(file_ptr, "%%time (hours), S1, S2, ...\n"); for (i = 0; i < num_sensor; i++) { fprintf(file_ptr, "%%S%i = %s\n", i, sensor_info[i].description); } } /* Get a time reference on the first call */ if (call_num == 1) t_0 = time(NULL); /* Write the data */ t_now = time(NULL); fprintf(file_ptr, "%8.3f", difftime(t_now, t_0) / 3600.); /* convert seconds to hours */ for (i = 0; i < num_sensor; i++) fprintf(file_ptr, " %7.2f", sensor_meas_val[i]); fprintf(file_ptr, "\n"); /* Close the plt file */ fclose(file_ptr); /* Done */ return; } /* ************************************************************** */ /* * senmeas_exit * This function configures the senmeas system for program exit. * * Zachary Wolf * 2/7/03 */ void senmeas_exit(void) { /* Nothing needs to be done */ /* Done */ return; } /* ************************************************************** */ /* PRIVATE FUNCTIONS */ /* ************************************************************** */ /* * senmeas_message * This function handles messages about the SENMEAS. * * Input: * message, string to display in standard I/O * * Zachary Wolf * 12/20/99 */ void senmeas_message(char* message) { /* Print the message */ printf("%s\n", message); /* Done */ return; } /* ************************************************************** */ /* * senmeas_error * This function handles error messages for the SENMEAS. * * Input: * message, string to display in standard I/O * * Zachary Wolf * 12/20/99 */ void senmeas_error(char* message) { /* Declare variables */ char buf[80]; /* Notify the operator of the error */ printf("\nSENMEAS 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); } /* ************************************************************** */ /* * senmeas_log_get_sensor_raw_output * This function logs the result of a sensor output measurement. * * Input: * sensor_num, sensor number * sensor_raw_output, measured value (V, ohm, ...) * * Zachary Wolf * 2/7/03 */ void senmeas_log_get_sensor_raw_output(int sensor_num, double sensor_raw_output) { /* Declare variables */ FILE* file_ptr; /* Open the log file */ file_ptr = fopen(log_file, "a"); if (file_ptr == NULL) { senmeas_message("Unable to open log file"); return; } /* Write the measured value to the log file */ fprintf(file_ptr, "%s Sensor Raw Output Measurement, sensor_num = %i, sensor_raw_output = %f\n", TimeStr(), sensor_num, sensor_raw_output); /* Close the log file */ fclose(file_ptr); /* Done */ return; } /* ************************************************************** */ /* * senmeas_log_scan_sensor_raw_output * This function logs the result of a sensor output scan. * * Input: * num_sensor, number of sensors * sensor_num[0 to num_sensor - 1], sensor number * sensor_raw_output[0 to num_sensor - 1], measured values (V, ohm, ...) * * Zachary Wolf * 2/7/03 */ void senmeas_log_scan_sensor_raw_output(int num_sensor, int sensor_num[], double sensor_raw_output[]) { /* Declare variables */ FILE* file_ptr; int i; /* Open the log file */ file_ptr = fopen(log_file, "a"); if (file_ptr == NULL) { senmeas_message("Unable to open log file"); return; } /* Write the measured values to the log file */ fprintf(file_ptr, "%s Sensor Raw Output Measurements:\n", TimeStr()); for (i = 0; i < num_sensor; i++) fprintf(file_ptr, " sensor_num = %i, sensor_raw_output = %f\n", sensor_num[i], sensor_raw_output[i]); /* Close the log file */ fclose(file_ptr); /* Done */ return; } /* ************************************************************** */ /* * senmeas_log_apply_sensor_calib * This function logs the result of a calibration calculation. * * Input: * sensor_num, sensor number * sensor_raw_output, measured value (V, ohm, ...) * sensor_meas_val, measured value (deg C) * * Zachary Wolf * 2/7/03 */ void senmeas_log_apply_sensor_calib(int sensor_num, double sensor_raw_output, double sensor_meas_val) { /* Declare variables */ FILE* file_ptr; /* Open the log file */ file_ptr = fopen(log_file, "a"); if (file_ptr == NULL) { senmeas_message("Unable to open log file"); return; } /* Write the measured value to the log file */ fprintf(file_ptr, "%s Sensor Calibration Calculation, sensor_num = %i, sensor_raw_out = %f, sensor_meas_val = %f\n", TimeStr(), sensor_num, sensor_raw_output, sensor_meas_val); /* Close the log file */ fclose(file_ptr); /* Done */ return; } /* ************************************************************** */ /* * senmeas_log_get_sensor_meas_val * This function logs the result of a measured value measurement. * * Input: * sensor_num, sensor number * sensor_raw_output, measured value (V, ohm, ...) * sensor_meas_val, measured value (deg C, atm, ...) * * Zachary Wolf * 2/7/03 */ void senmeas_log_get_sensor_meas_val(int sensor_num, double sensor_raw_output, double sensor_meas_val) { /* Declare variables */ FILE* file_ptr; /* Open the log file */ file_ptr = fopen(log_file, "a"); if (file_ptr == NULL) { senmeas_message("Unable to open log file"); return; } /* Write the measured value to the log file */ fprintf(file_ptr, "%s Sensor Measurement, sensor_num = %i, sensor_raw_output = %f, sensor_meas_val = %f\n", TimeStr(), sensor_num, sensor_raw_output, sensor_meas_val); /* Close the log file */ fclose(file_ptr); /* Done */ return; } /* ************************************************************** */ /* * senmeas_log_scan_sensor_meas_val * This function logs the result of a measured value measurement. * * Input: * num_sensor, number of sensors * sensor_num[0 to num_sensor - 1], sensor number * sensor_raw_output[0 to num_sensor - 1], sensor output (V, ohm, ...) * sensor_meas_val[0 to num_sensor - 1], measured value (deg C, atm, ...) * * Zachary Wolf * 2/7/03 */ void senmeas_log_scan_sensor_meas_val(int num_sensor, int sensor_num[], double sensor_raw_output[], double sensor_meas_val[]) { /* Declare variables */ FILE* file_ptr; int i; /* Open the log file */ file_ptr = fopen(log_file, "a"); if (file_ptr == NULL) { senmeas_message("Unable to open log file"); return; } /* Write the measured value to the log file */ fprintf(file_ptr, "%s Sensor Measurement:\n", TimeStr()); for (i = 0; i < num_sensor; i++) fprintf(file_ptr, " sensor_num = %i, sensor_raw_output = %f, sensor_meas_val = %f\n", sensor_num[i], sensor_raw_output[i], sensor_meas_val[i]); /* Close the log file */ fclose(file_ptr); /* Done */ return; }