/* ************************************************************** */ /* * Module AMBIENT * This module contains functions for making ambient temperature, * humidity, pressure, ... measurements. * * Zachary Wolf * 3/22/02 */ /* ************************************************************** */ /* INCLUDES */ #include #include #include #include #include "ambient.h" #include "hp3457.h" #include "k7011.h" /* ************************************************************** */ /* PRIVATE FUNCTIONS */ void ambient_message(char* msg); void ambient_error(char* msg); void ambient_log_meas_system(enum ambient_meas_system_enum meas_system); void ambient_log_meas(int sensor_num, enum ambient_sensor_type_enum sensor_type, double raw_output, double meas_value); void ambient_log_sensor_voltage(int sensor_num, double voltage); void ambient_log_sensor_resistance(int sensor_num, double resistance); void ambient_log_sensor_temperature(int sensor_num, double temperature); /* ************************************************************** */ /* PRIVATE GLOBAL VARIABLES */ static int hp3457_ID; static int k7011_ID; /* ************************************************************** */ /* PRIVATE PARAMETERS */ static char dat_file[100]; static char log_file[100]; static struct ambient_param_struct ambient_param; /* ************************************************************** */ /* PUBLIC FUNCTIONS */ /* ************************************************************** */ /* * ambient_init * This function opens devices, queries for ID, and * initializes the devices to a known state. * * Zachary Wolf * 3/22/02 */ void ambient_init(char log_file_in[], char dat_file_in[], struct ambient_param_struct ambient_param_in) { /* Save the module parameters */ strcpy(log_file, log_file_in); strcpy(dat_file, dat_file_in); ambient_param = ambient_param_in; /* Check parameters */ if (ambient_param.num_sensors < 0 || ambient_param.num_sensors > AMBIENT_MAX_NUM_SENSORS) { ambient_error("ambient_init: Improper num_sensors"); return; } /* Initialize the measurement devices */ if (ambient_param.meas_system == AMBIENT_HP3457) { hp3457_init(ambient_param.board_addr, ambient_param.hp3457_addr, &hp3457_ID); } else if (ambient_param.meas_system == AMBIENT_K7011_HP3457) { k7011_init(ambient_param.board_addr, ambient_param.k7011_addr, &k7011_ID); hp3457_init(ambient_param.board_addr, ambient_param.hp3457_addr, &hp3457_ID); } else if (ambient_param.meas_system == AMBIENT_NONE); else ambient_error("Unknown device type."); /* Log the type of measurement system being used */ ambient_log_meas_system(ambient_param.meas_system); /* Done */ return; } /* ************************************************************** */ /* * ambient_get_num_sensors * This function gets the number of sensors being used. * * Output: * num_sensors, number of sensors * * Zachary Wolf * 3/22/02 */ void ambient_get_num_sensors(int* num_sensors) { /* Return the number of sensors */ if (ambient_param.meas_system != AMBIENT_NONE) *num_sensors = ambient_param.num_sensors; else *num_sensors = 0; /* Done */ return; } /* ************************************************************** */ /* * ambient_get_sensor_name * This function gets the name of a specified sensor. * * Input: * sensor_num, sensor number, 0 to ambient_param.num_sensors - 1 * * Output: * sensor_name, name of the specified sensor * * Zachary Wolf * 3/22/02 */ void ambient_get_sensor_name(int sensor_num, char sensor_name[]) { /* Check parameters */ if (ambient_param.meas_system == AMBIENT_NONE) return; if (sensor_num < 0 || sensor_num >= ambient_param.num_sensors) { ambient_error("Improper sensor_num"); return; } /* Return the name of the sensor */ strcpy(sensor_name, ambient_param.sensor[sensor_num].name); /* Done */ return; } /* ************************************************************** */ /* * ambient_get_sensor_voltage * This function gets the measured voltage from a specified sensor. * * Input: * sensor_num, sensor number, 0 to ambient_param.num_sensors - 1 * * Output: * voltage, voltage from the specified sensor (V) * * Zachary Wolf * 3/25/02 */ void ambient_get_sensor_voltage(int sensor_num, double* voltage) { /* Declare variables */ int chan; int k7011_chan; /* Check parameters */ if (sensor_num < 0 || sensor_num >= ambient_param.num_sensors) { ambient_error("Improper sensor_num"); return; } /* Get the channel number for the specified sensor */ chan = ambient_param.sensor[sensor_num].chan_num; /* Measure the voltage */ if (ambient_param.meas_system == AMBIENT_HP3457) { hp3457_get_chan_voltage(hp3457_ID, chan, voltage); } else if (ambient_param.meas_system == AMBIENT_K7011_HP3457) { k7011_close_card_chan(k7011_ID, ambient_param.k7011_card_num, chan); k7011_chan = ambient_param.k7011_hp3457_chan; hp3457_get_chan_voltage(hp3457_ID, k7011_chan, voltage); k7011_open_all(k7011_ID); } else if (ambient_param.meas_system == AMBIENT_NONE) { *voltage = 0.; return; } else ambient_error("Unknown measurement system type."); /* Log the measurement */ ambient_log_sensor_voltage(sensor_num, *voltage); /* Done */ return; } /* ************************************************************** */ /* * ambient_get_sensor_resistance * This function gets the measured resistance from a specified sensor. * * Input: * sensor_num, sensor number, 0 to ambient_param.num_sensors - 1 * * Output: * resistance, resistance from the specified sensor (ohm) * * Zachary Wolf * 3/25/02 */ void ambient_get_sensor_resistance(int sensor_num, double* resistance) { /* Declare variables */ int chan; int k7011_chan; /* Check parameters */ if (sensor_num < 0 || sensor_num >= ambient_param.num_sensors) { ambient_error("Improper sensor_num"); return; } /* Get the channel number for the specified sensor */ chan = ambient_param.sensor[sensor_num].chan_num; /* Measure the resistance */ if (ambient_param.meas_system == AMBIENT_HP3457) { hp3457_get_chan_resistance(hp3457_ID, chan, resistance); } else if (ambient_param.meas_system == AMBIENT_K7011_HP3457) { k7011_close_card_chan(k7011_ID, ambient_param.k7011_card_num, chan); k7011_chan = ambient_param.k7011_hp3457_chan; hp3457_get_chan_resistance(hp3457_ID, k7011_chan, resistance); k7011_open_all(k7011_ID); } else if (ambient_param.meas_system == AMBIENT_NONE) { *resistance = 0.; return; } else ambient_error("Unknown measurement system type."); /* Log the measurement */ ambient_log_sensor_resistance(sensor_num, *resistance); /* Done */ return; } /* ************************************************************** */ /* * ambient_get_sensor_temperature * This function gets the measured temperature from a specified sensor. * * Input: * sensor_num, sensor number, 0 to ambient_param.num_sensors - 1 * * Output: * temperature, temperature from the specified sensor (C) * * Zachary Wolf * 3/25/02 */ void ambient_get_sensor_temperature(int sensor_num, double* temperature) { /* Declare variables */ int chan; int k7011_chan; /* Check parameters */ if (sensor_num < 0 || sensor_num >= ambient_param.num_sensors) { ambient_error("Improper sensor_num"); return; } /* Get the channel number for the specified sensor */ chan = ambient_param.sensor[sensor_num].chan_num; /* Measure the temperature */ if (ambient_param.meas_system == AMBIENT_HP3457) { hp3457_get_chan_temperature(hp3457_ID, chan, temperature); } else if (ambient_param.meas_system == AMBIENT_K7011_HP3457) { k7011_close_card_chan(k7011_ID, ambient_param.k7011_card_num, chan); k7011_chan = ambient_param.k7011_hp3457_chan; hp3457_get_chan_temperature(hp3457_ID, k7011_chan, temperature); k7011_open_all(k7011_ID); } else if (ambient_param.meas_system == AMBIENT_NONE) { *temperature = 0.; return; } else ambient_error("Unknown measurement system type."); /* Log the measurement */ ambient_log_sensor_temperature(sensor_num, *temperature); /* Done */ return; } /* ************************************************************** */ /* * ambient_get_sensor_output * This function gets the measured value from a specified sensor. * * Input: * sensor_num, sensor number, 0 to ambient_param.num_sensors - 1 * * Output: * meas_value, measured quantity from the specified sensor (ie., deg C, atm, ...) * * Zachary Wolf * 3/22/02 */ void ambient_get_sensor_output(int sensor_num, double* meas_value) { /* Declare variables */ double temperature; double resistance; double voltage; double R_25, dRdT_25, T_25, A, B; double raw_sensor_output; double pressure_calib[12] = {20.8, 22.0, 23.0, 24.0, 25.0, 26.0, 27.0, 28.0, 29.0, 30.0, 31.0, 32.0}; /* From manual */ double p_voltage_calib[12] = {1.00, 1.43, 1.78, 2.14, 2.49, 2.85, 3.21, 3.56, 3.92, 4.27, 4.63, 5.00}; int i, index; static int ncall = 0; /* Check parameters */ if (ambient_param.meas_system == AMBIENT_NONE) { *meas_value = 0.; return; } if (sensor_num < 0 || sensor_num >= ambient_param.num_sensors) { ambient_error("Improper sensor_num"); return; } /* Perform the appropriate measurement */ if (ambient_param.sensor[sensor_num].sensor_type == AMBIENT_HPTHERM_T) { ambient_get_sensor_temperature(sensor_num, &temperature); raw_sensor_output = temperature; *meas_value = temperature; } else if (ambient_param.sensor[sensor_num].sensor_type == AMBIENT_YSI44036_T) { ambient_get_sensor_resistance(sensor_num, &resistance); raw_sensor_output = resistance; R_25 = AMBIENT_YSI44036_R25; dRdT_25 = AMBIENT_YSI44036_dRdT25; T_25 = 25. + 273.15; /* R = A e^(B/T) */ B = - pow(T_25, 2) * dRdT_25 / R_25; A = R_25 * exp(-B/T_25); *meas_value = (B / log(resistance / A)) - 273.15; } else if (ambient_param.sensor[sensor_num].sensor_type == AMBIENT_OMHX93AV_T) { ambient_get_sensor_voltage(sensor_num, &voltage); raw_sensor_output = voltage; *meas_value = AMBIENT_OMHX93AV_DEG_C_PER_VOLT * voltage + AMBIENT_OMHX93AV_DEG_C_AT_0_V; } else if (ambient_param.sensor[sensor_num].sensor_type == AMBIENT_OMHX93AV_RH) { ambient_get_sensor_voltage(sensor_num, &voltage); raw_sensor_output = voltage; *meas_value = AMBIENT_OMHX93AV_RH_PER_VOLT * voltage; } else if (ambient_param.sensor[sensor_num].sensor_type == AMBIENT_OMEWSBPA_P) { ambient_get_sensor_voltage(sensor_num, &voltage); /* For testing */ #ifdef DUMMY_DEVICES voltage = p_voltage_calib[ncall] + .175; ncall++; if (ncall > 11) ncall = 0; #endif raw_sensor_output = voltage; index = -1; for (i = 0; i < 11; i++) if (voltage >= p_voltage_calib[i] && voltage <= p_voltage_calib[i+1]) index = i; if (index == -1) *meas_value = 0.; else *meas_value = pressure_calib[index] + ((pressure_calib[index + 1] - pressure_calib[index]) / (p_voltage_calib[index + 1] - p_voltage_calib[index])) * (voltage - p_voltage_calib[index]); } else ambient_error("Unknown sensor type."); /* Log the measurement */ ambient_log_meas(sensor_num, ambient_param.sensor[sensor_num].sensor_type, raw_sensor_output, *meas_value); /* Done */ return; } /* ************************************************************** */ /* * ambient_measure * This function measures all sensor outputs. * * Output: * num_sensors, number of sensors * sensor_name[0 to num_sensors - 1], name of each sensor * meas_value[0 to num_sensors - 1], measured values (deg C, atm, ...) * * Zachary Wolf * 3/22/02 */ void ambient_measure(int* num_sensors, char sensor_name[][AMBIENT_MAX_NAME_LENGTH], double meas_value[]) { /* Declare variables */ int i; /* Measure all ambient parameters */ ambient_get_num_sensors(num_sensors); if (*num_sensors > AMBIENT_MAX_NUM_SENSORS) ambient_error("Maximum number of sensors exceeded"); for (i = 0; i < *num_sensors; i++) { ambient_get_sensor_name(i, sensor_name[i]); ambient_get_sensor_output(i, &meas_value[i]); } /* Write a header for the results */ printf("\n"); printf(" Ambient Measurements\n"); printf("\n"); for (i = 0; i < *num_sensors; i++) { printf("P%i = %s\n", i, sensor_name[i]); } printf("\n"); for (i = 0; i < *num_sensors; i++) printf(" P%i ", i); printf("\n"); for (i = 0; i < *num_sensors; i++) printf(" -------"); printf("\n"); /* Write the data */ for (i = 0; i < *num_sensors; i++) printf(" %7.2f", meas_value[i]); printf("\n"); /* Done */ return; } /* ************************************************************** */ /* * ambient_dat_meas * This function writes the results of ambient parameter measurements * to the data file. * * Input: * num_sensors, number of sensor measurements * sensor_name[0 to num_sensors - 1], name of each sensor measurement * meas_value[0 to num_sensors - 1], measurement values (deg C, atm, ...) * * Zachary Wolf * 3/22/02 */ void ambient_dat_meas(int num_sensors, char sensor_name[][AMBIENT_MAX_NAME_LENGTH], double meas_value[]) { /* Declare variables */ FILE* file_ptr; int i; /* Open the data file */ file_ptr = fopen(dat_file, "a"); if (file_ptr == NULL) { printf("ambient_dat_meas: Unable to open dat file\n"); return; } /* Write a header */ fprintf(file_ptr, "\nAmbient Measurements:\n"); /* Write the data */ for (i = 0; i < num_sensors; i++) fprintf(file_ptr, "%s, P%i = %7.2f\n", sensor_name[i], i, meas_value[i]); /* Close the dat file */ fclose(file_ptr); /* Done */ return; } /* ************************************************************** */ /* * ambient_dat_meas_vs_time * This function writes the results of ambient measurements * to the data file. * * Input: * num_sensors, number of sensor measurements * sensor_name[0 to num_sensors - 1], name of each sensor * meas_value[0 to num_sensors - 1], measurement value (deg C, atm) * * Zachary Wolf * 3/22/02 */ void ambient_dat_meas_vs_time(int num_sensors, char sensor_name[][AMBIENT_MAX_NAME_LENGTH], double meas_value[]) { /* Declare variables */ FILE* file_ptr; static int call_num; int i; /* Open the data file */ file_ptr = fopen(dat_file, "a"); if (file_ptr == NULL) { printf("ambient_dat_meas: 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, " Ambient Measurements\n"); fprintf(file_ptr, "\n"); for (i = 0; i < num_sensors; i++) { fprintf(file_ptr, "P%i = %s\n", i, sensor_name[i]); } fprintf(file_ptr, "\n"); fprintf(file_ptr, " Time "); for (i = 0; i < num_sensors; i++) fprintf(file_ptr, " P%i ", i); fprintf(file_ptr, "\n"); fprintf(file_ptr, "--------"); for (i = 0; i < num_sensors; i++) fprintf(file_ptr, " -------"); fprintf(file_ptr, "\n"); } /* Write the data */ fprintf(file_ptr, "%s", TimeStr()); for (i = 0; i < num_sensors; i++) fprintf(file_ptr, " %7.2f", meas_value[i]); fprintf(file_ptr, "\n"); /* Close the dat file */ fclose(file_ptr); /* Done */ return; } /* ************************************************************** */ /* * ambient_exit * This function configures the ambient system for program exit. * * Zachary Wolf * 3/22/02 */ void ambient_exit(void) { /* Exit the appropriate devices */ if (ambient_param.meas_system == AMBIENT_HP3457) { hp3457_exit(hp3457_ID); } else if (ambient_param.meas_system == AMBIENT_K7011_HP3457) { k7011_exit(k7011_ID); hp3457_exit(hp3457_ID); } else if (ambient_param.meas_system == AMBIENT_NONE); else ambient_error("Unknown device type."); /* Done */ return; } /* ************************************************************** */ /* PRIVATE FUNCTIONS */ /* ************************************************************** */ /* * ambient_message * This function handles messages for the module. * * Input: * message, string to display in standard I/O * * Zachary Wolf * 3/22/02 */ void ambient_message(char* message) { /* Print the message */ printf("%s\n", message); /* Done */ return; } /* ************************************************************** */ /* * ambient_error * This function handles error messages for the module. * * Input: * message, string to display in standard I/O * * Zachary Wolf * 3/22/02 */ void ambient_error(char* message) { /* Declare variables */ char buf[80]; /* Notify the operator of the error */ printf("\nAMBIENT 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); } /* ************************************************************** */ /* * ambient_log_meas_system * This function logs the type of equipment being used. * * Input: * meas_system, type of ambient readout devices * * Zachary Wolf * 3/22/02 */ void ambient_log_meas_system(enum ambient_meas_system_enum meas_system) { /* Declare variables */ FILE* file_ptr; char type[80]; /* Put the device type in a string */ if (meas_system == AMBIENT_HP3457) strcpy(type, "HP3457"); else if (meas_system == AMBIENT_K7011_HP3457) strcpy(type, "K7011_HP3457"); else if (meas_system == AMBIENT_NONE) strcpy(type, "None"); else ambient_error("Unknown device type in log device function."); /* Open the log file */ file_ptr = fopen(log_file, "a"); if (file_ptr == NULL) { ambient_message("Unable to open log file"); return; } /* Write the device type to the log file */ fprintf(file_ptr, "%s Ambient Measurement Device(s) Being Used, type = %s\n", TimeStr(), type); /* Close the log file */ fclose(file_ptr); /* Done */ return; } /* ************************************************************** */ /* * ambient_log_meas * This function logs the result of a sensor measurement. * * Input: * sensor_num, sensor number * sensor_type, type of sensor being used * raw_output, parameter being measured (V, A, ohm, etc.) * meas_value, result of the measurement (C, atm., %RH, etc.) * * Zachary Wolf * 3/25/02 */ void ambient_log_meas(int sensor_num, enum ambient_sensor_type_enum sensor_type, double raw_output, double meas_value) { /* Declare variables */ FILE* file_ptr; char type[80]; /* Open the log file */ file_ptr = fopen(log_file, "a"); if (file_ptr == NULL) { ambient_message("Unable to open log file"); return; } /* Put the sensor type in a string */ if (sensor_type == AMBIENT_HPTHERM_T) strcpy(type, "HPTHERM_T"); else if (sensor_type == AMBIENT_YSI44036_T) strcpy(type, "YSI44036_T"); else if (sensor_type == AMBIENT_OMHX93AV_T) strcpy(type, "OMHX93AV_T"); else if (sensor_type == AMBIENT_OMHX93AV_RH) strcpy(type, "OMHX93AV_RH"); else if (sensor_type == AMBIENT_OMEWSBPA_P) strcpy(type, "OMEWSBPA_P"); else ambient_error("Unknown sensor type in log meas function."); /* Write the measurement result to the log file */ fprintf(file_ptr, "%s Sensor Measurement, Sensor Number = %i, Sensor Type = %s\n", TimeStr(), sensor_num, type); fprintf(file_ptr, " Raw Sensor Output = %f (V, A, ohm, ...), Measured Value = %f (C, atm, RH, ...)\n", raw_output, meas_value); /* Close the log file */ fclose(file_ptr); /* Done */ return; } /* ************************************************************** */ /* * ambient_log_sensor_voltage * This function logs the result of a sensor voltage measurement. * * Input: * sensor_num, sensor number * voltage, sensor voltage (V) * * Zachary Wolf * 3/22/02 */ void ambient_log_sensor_voltage(int sensor_num, double voltage) { /* Declare variables */ FILE* file_ptr; /* Open the log file */ file_ptr = fopen(log_file, "a"); if (file_ptr == NULL) { ambient_message("Unable to open log file"); return; } /* Write the measured value to the log file */ fprintf(file_ptr, "%s Sensor Voltage Measurement, Sensor Number = %i, V = %f V\n", TimeStr(), sensor_num, voltage); /* Close the log file */ fclose(file_ptr); /* Done */ return; } /* ************************************************************** */ /* * ambient_log_sensor_resistance * This function logs the result of a sensor resistance measurement. * * Input: * sensor_num, sensor number * resistance, sensor resistance (ohm) * * Zachary Wolf * 3/22/02 */ void ambient_log_sensor_resistance(int sensor_num, double resistance) { /* Declare variables */ FILE* file_ptr; /* Open the log file */ file_ptr = fopen(log_file, "a"); if (file_ptr == NULL) { ambient_message("Unable to open log file"); return; } /* Write the measured value to the log file */ fprintf(file_ptr, "%s Sensor Resistance Measurement, Sensor Number = %i, R = %f ohm\n", TimeStr(), sensor_num, resistance); /* Close the log file */ fclose(file_ptr); /* Done */ return; } /* ************************************************************** */ /* * ambient_log_sensor_temperature * This function logs the result of a sensor temperature measurement. * * Input: * sensor_num, sensor number * temperature, sensor temperature (C) * * Zachary Wolf * 3/22/02 */ void ambient_log_sensor_temperature(int sensor_num, double temperature) { /* Declare variables */ FILE* file_ptr; /* Open the log file */ file_ptr = fopen(log_file, "a"); if (file_ptr == NULL) { ambient_message("Unable to open log file"); return; } /* Write the measured value to the log file */ fprintf(file_ptr, "%s Sensor Temperature Measurement, Sensor Number = %i, T = %f C\n", TimeStr(), sensor_num, temperature); /* Close the log file */ fclose(file_ptr); /* Done */ return; }