/* ************************************************************** */ /* * Module VRAMP * This module contains functions to make voltage * measurements during a current ramp. * * Zachary Wolf * 5/6/02 */ /* ************************************************************** */ /* INCLUDES */ #include #include #include #include #include #include "vramp.h" #include "vrampui.h" #include "imag.h" #include "tds3012b.h" #include "hp3458.h" /* ************************************************************** */ /* PRIVATE PARAMETERS */ static char log_file[100]; static char plt_file[100]; static struct vramp_param_struct vramp_param; /* ************************************************************** */ /* PRIVATE GLOBAL VARIABLES */ static int tds3012b_ID; static int hp3458_ID; static char msg[80]; /* ************************************************************** */ /* PRIVATE FUNCTIONS */ int vramp_check_param(void); void vramp_error(char* message); void vramp_message(char* message); void vramp_log_meas(void); void vramp_log_vt(double imag, long int num_samp); /* ************************************************************** */ /* PUBLIC FUNCTIONS */ /* ************************************************************** */ /* * vramp_init * This function is used to initialize the voltage * measurement system. * * Zachary Wolf * 5/6/02 */ void vramp_init(char log_file_in[], char plt_file_in[], struct vramp_param_struct vramp_par) { /* Declare variables */ int err; /* Save parameters for future use */ strcpy(log_file, log_file_in); strcpy(plt_file, plt_file_in); vramp_param = vramp_par; /* Check all parameter values to find any problems */ err = vramp_check_param(); if (err != 0) { vramp_error("Problem with parameter values"); return; } /* Initialize the required hardware */ if (vramp_param.config == VRAMP_TDS3012B) { tds3012b_init(vramp_param.board_addr, vramp_param.tds3012b_addr, &tds3012b_ID); } else if (vramp_param.config == VRAMP_HP3458) { hp3458_init(vramp_param.board_addr, vramp_param.hp3458_addr, &hp3458_ID); } else { vramp_error("Unknown voltage system configuration"); return; } /* Prepare the hardware for the measurements */ if (vramp_param.config == VRAMP_TDS3012B) { tds3012b_factory_setup(tds3012b_ID); tds3012b_set_vert_chan_select(tds3012b_ID, vramp_param.tds3012b_imag_chan, ON); tds3012b_set_vert_chan_select(tds3012b_ID, vramp_param.tds3012b_v_chan, ON); tds3012b_set_vert_scale(tds3012b_ID, vramp_param.tds3012b_imag_chan, vramp_param.tds3012b_imag_scale); tds3012b_set_vert_scale(tds3012b_ID, vramp_param.tds3012b_v_chan, vramp_param.tds3012b_v_scale); tds3012b_set_vert_position(tds3012b_ID, vramp_param.tds3012b_imag_chan, -3.); tds3012b_set_vert_position(tds3012b_ID, vramp_param.tds3012b_v_chan, -3.); tds3012b_set_horiz_scale(tds3012b_ID, .5); /* temporary scale, reset later */ } else if (vramp_param.config == VRAMP_HP3458) { } else { vramp_error("Unknown voltage system configuration"); return; } /* Done */ return; } /* ************************************************************** */ /* * vramp_get_v * This function measures the voltage from a measurement * device as a power supply is ramped. * * Input: * imag, nominal magnet current to ramp to (A) * * Output: * num_samp, number of voltage samples * t_samp[0 to num_samp - 1], the time of each sample (sec) * imag_samp[0 to num_samp - 1], the current when each sample was taken (A) * v_samp[0 to num_samp - 1], the voltage samples (V) * * Zachary Wolf * 5/6/02 */ void vramp_get_v(double imag, long int* num_samp, double t_samp[], double imag_samp[], double v_samp[]) { /* Declare variables */ int err; int num_plc = 3; time_t t_start, t_finish; double ramp_time; double samp_time; long int nom_num_samp; double delta_t_samp; int i; /* Check the configuration */ if (vramp_param.config != VRAMP_TDS3012B && vramp_param.config != VRAMP_HP3458) { vramp_error("vramp_get_vt: unknown configuration"); return; } /* Check all parameter values */ err = vramp_check_param(); if (err != 0) { vramp_error("Problem with parameter values"); return; } /* Log the measurement */ vramp_log_meas(); /* Compute sampling parameters */ /* Sample every num_plc power line cycles, leave time for the HP3458 to store the samples */ imag_estimate_ramp_time(imag, &ramp_time); samp_time = vramp_param.t_samp_before + ramp_time + vramp_param.t_samp_after; nom_num_samp = (samp_time * 60 / num_plc) * .995 - 1; if (nom_num_samp > VRAMP_MAX_NUM_SAMP) { printf("vramp_get_v: the number of samples is too large, will set to maximum value\n"); nom_num_samp = VRAMP_MAX_NUM_SAMP; } delta_t_samp = samp_time / (double)nom_num_samp; /* Message */ printf("\nStarting to sample...\n"); /* Get the starting time */ t_start = time(NULL); /* Begin sampling */ if (vramp_param.config == VRAMP_TDS3012B) { tds3012b_set_horiz_scale(tds3012b_ID, samp_time / 10.); tds3012b_single_shot(tds3012b_ID); } else if (vramp_param.config == VRAMP_HP3458) { hp3458_setup_timed_voltage_samples(hp3458_ID, vramp_param.hp3458_range, (double) num_plc, nom_num_samp, delta_t_samp); hp3458_trigger_timed_voltage_samples(hp3458_ID); } else { vramp_error("Unknown voltage system configuration"); return; } /* Wait before starting the current ramp */ Delay(vramp_param.t_samp_before); /* Ramp the current */ imag_ramp(imag); /* Message */ printf("\nRamp complete, sampling after the ramp...\n"); /* Wait after the current ramp */ Delay(vramp_param.t_samp_after); /* Wait for all the samples */ t_finish = time(NULL); if (difftime(t_finish, t_start) < samp_time) Delay(samp_time - difftime(t_finish, t_start) + .5); /* Message */ printf("\nCollecting the samples...\n"); /* Get the samples */ if (vramp_param.config == VRAMP_TDS3012B) { tds3012b_get_waveform(tds3012b_ID, vramp_param.tds3012b_imag_chan, num_samp, t_samp, imag_samp); tds3012b_get_waveform(tds3012b_ID, vramp_param.tds3012b_v_chan, num_samp, t_samp, v_samp); } else if (vramp_param.config == VRAMP_HP3458) { hp3458_collect_timed_voltage_samples(hp3458_ID, nom_num_samp, v_samp); /* v[0 to num_samp - 1] are samples */ *num_samp = nom_num_samp; for (i = 0; i < nom_num_samp; i++) t_samp[i] = i * delta_t_samp; for (i = 0; i < nom_num_samp; i++) imag_samp[i] = 0.; } else { vramp_error("Unknown voltage system configuration"); return; } /* Message */ printf("\nSampling complete.\n"); /* Generate test data if testing without hardware */ #ifdef DUMMY_DEVICES *num_samp = nom_num_samp; for (i = 0; i < nom_num_samp; i++) t_samp[i] = i * delta_t_samp; for (i = 0; i < nom_num_samp; i++) imag_samp[i] = imag * i / nom_num_samp; for (i = 0; i < nom_num_samp; i++) v_samp[i] = 1. * i / nom_num_samp; #endif /* Update the user interface */ if (vramp_param.show_ui == VRAMP_TRUE) vrampui_update(*num_samp, t_samp, imag_samp, v_samp); /* Log the measurement results */ vramp_log_vt(imag, *num_samp); /* Done */ return; } /* ************************************************************** */ /* * vramp_plt_v * This function plots the voltage samples from a measurement * device as a power supply is ramped. * * Input: * num_samp, number of voltage samples * t_samp[0 to num_samp - 1], the time of each sample (sec) * imag_samp[0 to num_samp - 1], the current when each sample was taken (A) * v_samp[0 to num_samp - 1], the voltage samples (V) * * Zachary Wolf * 5/8/02 */ void vramp_plt_v(long int num_samp, double t_samp[], double imag_samp[], double v_samp[]) { /* Declare variables */ FILE* file_ptr; long int i; /* Open the plot file */ file_ptr = fopen(plt_file, "a"); if (file_ptr == NULL) { vramp_message("Unable to open plt file"); return; } /* Write a header */ fprintf(file_ptr, "%%t_samp, imag_samp, v_samp\n"); /* Write the measured values to the plt file */ for (i = 0; i < num_samp; i++) { fprintf(file_ptr, "%12.4f %12.7f %12.7f\n", t_samp[i], imag_samp[i], v_samp[i]); } /* Close the log file */ fclose(file_ptr); /* Done */ return; } /* ************************************************************** */ /* * vramp_exit * This function is used to exit the voltage * measurement system. * * Zachary Wolf * 5/6/02 */ void vramp_exit(void) { /* Exit the hardware */ if (vramp_param.config == VRAMP_TDS3012B) { tds3012b_exit(tds3012b_ID); } else if (vramp_param.config == VRAMP_HP3458) { hp3458_exit(hp3458_ID); } else { vramp_error("Unknown voltage system configuration"); return; } /* Done */ return; } /* ************************************************************** */ /* INTERNAL FUNCTIONS */ /* ************************************************************** */ /* * vramp_error * This function handles errors for the VRAMP module. * * Input: * message, string to display in standard I/O * * Zachary Wolf * 10/11/98 */ void vramp_error(char* message) { /* Declare variables */ char buf[80]; /* Notify the operator of the error */ printf("\nVRAMP 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); } /* ************************************************************** */ /* * vramp_message * This function handles messages for the IMAG module. * * Input: * message, string to display in standard I/O * * Zachary Wolf * 10/12/98 */ void vramp_message(char* message) { /* Print the message */ printf("%s\n", message); /* Done */ return; } /* ************************************************************** */ /* * vramp_check_param * This function checks that all required parameters have been assigned * appropriate values. * * Output: * err, 0 = no error, non-zero = error * * Zachary Wolf * 8/11/98 */ int vramp_check_param() { /* log_file */ if (CompareStrings(log_file, 0, "", 0, 0) == 0) { vramp_error("Log file not defined."); return -1; } /* vramp_param.board_addr */ if (vramp_param.board_addr < 0 || vramp_param.board_addr > 10) { Fmt(msg, "%s 30) { Fmt(msg, "%s 30) { Fmt(msg, "%s 10.) { Fmt(msg, "%s 10.) { Fmt(msg, "%s 100.) { Fmt(msg, "%s 10.) { Fmt(msg, "%s 1000.) { Fmt(msg, "%s