/* ************************************************************** */ /* * Module VTRAMP * This module contains functions to make integrated voltage * measurements during a current ramp. * * Zachary Wolf * 4/11/02 */ /* ************************************************************** */ /* INCLUDES */ #include #include #include #include #include #include "vtramp.h" #include "vtrampui.h" #include "imag.h" #include "pdi5025.h" #include "hp3458.h" /* ************************************************************** */ /* PRIVATE PARAMETERS */ static char log_file[100]; static struct vtramp_param_struct vtramp_param; /* ************************************************************** */ /* PRIVATE GLOBAL VARIABLES */ static int pdi5025_ID; static int hp3458_ID; static char msg[80]; /* ************************************************************** */ /* PRIVATE FUNCTIONS */ int vtramp_check_param(void); void vtramp_error(char* message); void vtramp_message(char* message); void vtramp_offset_correction(double delta_t_samp, int num_samp, double v_samp[], double vt_samp[]); void vtramp_log_meas(void); void vtramp_log_vt(double imag, double vt); void vtramp_log_offset_correction(double v_offset_ini, double v_offset_fin); /* ************************************************************** */ /* PUBLIC FUNCTIONS */ /* ************************************************************** */ /* * vtramp_init * This function is used to initialize the integrated voltage * measurement system. * * Zachary Wolf * 4/16/02 */ void vtramp_init(char log_file_in[], struct vtramp_param_struct vtramp_par) { /* Declare variables */ int err; /* Save parameters for future use */ strcpy(log_file, log_file_in); vtramp_param = vtramp_par; /* Check all parameter values to find any problems */ err = vtramp_check_param(); if (err != 0) { vtramp_error("Problem with parameter values"); return; } /* Initialize the required hardware */ if (vtramp_param.config == VTRAMP_PDI5025) { pdi5025_init(vtramp_param.board_addr, vtramp_param.pdi5025_addr, &pdi5025_ID); } else if (vtramp_param.config == VTRAMP_HP3458) { hp3458_init(vtramp_param.board_addr, vtramp_param.hp3458_addr, &hp3458_ID); } else { vtramp_error("Unknown integrated voltage system configuration"); return; } /* Done */ return; } /* ************************************************************** */ /* * vtramp_get_vt * This function measures the integrated voltage from a measurement * coil as a power supply is ramped. * * Input: * imag, nominal magnet current to ramp to (A) * * Output: * vt, the integrated voltage from the measurement coil (Vs) * * Zachary Wolf * 4/11/02 */ void vtramp_get_vt(double imag, double* vt) { /* Declare variables */ int err; time_t t_start, t_finish; double ramp_time; double samp_time; int num_samp; double delta_t_samp; double vt_samp[VTRAMP_MAX_NUM_SAMP + 1] = {0}; double v_samp[VTRAMP_MAX_NUM_SAMP + 1] = {0}; int i; double sum; int num_ave; /* Check the configuration */ if (vtramp_param.config != VTRAMP_PDI5025 && vtramp_param.config != VTRAMP_HP3458) { vtramp_error("vtramp_get_vt: unknown configuration"); return; } /* Check all parameter values */ err = vtramp_check_param(); if (err != 0) { vtramp_error("Problem with parameter values"); return; } /* Log the measurement */ vtramp_log_meas(); /* Compute sampling parameters */ /* Sample every 2 power line cycles, leave time for the HP3458 to store the samples */ imag_estimate_ramp_time(imag, &ramp_time); samp_time = vtramp_param.t_samp_before + ramp_time + vtramp_param.t_samp_after; num_samp = (samp_time * 30) - 1; if (num_samp > VTRAMP_MAX_NUM_SAMP) { printf("vtramp_get_vt: the number of samples is too large, will set to maximum value\n"); num_samp = VTRAMP_MAX_NUM_SAMP; } delta_t_samp = samp_time / (double)num_samp; /* Get the starting time */ t_start = time(NULL); /* Begin sampling */ if (vtramp_param.config == VTRAMP_PDI5025) { pdi5025_get_VT_timer_start(pdi5025_ID, vtramp_param.pdi5025_channel, vtramp_param.pdi5025_gain, delta_t_samp, num_samp); } if (vtramp_param.config == VTRAMP_HP3458) { hp3458_setup_timed_voltage_samples(hp3458_ID, vtramp_param.hp3458_range, 2., num_samp, delta_t_samp); /* Integrate over 2 power line cycles, must agree with sampling parameter calculations above */ hp3458_trigger_timed_voltage_samples(hp3458_ID); } /* Wait before starting the current ramp */ Delay(vtramp_param.t_samp_before); /* Ramp the current */ imag_ramp(imag); /* Wait after the current ramp */ Delay(vtramp_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); /* Get the samples */ if (vtramp_param.config == VTRAMP_PDI5025) { pdi5025_get_VT_timer_finish(pdi5025_ID, num_samp, vt_samp); /* vt[0] = 0, vt[1 to num_samp] are samples */ } if (vtramp_param.config == VTRAMP_HP3458) { hp3458_collect_timed_voltage_samples(hp3458_ID, num_samp, v_samp); /* v[0 to num_samp - 1] are samples */ } /* When using an integrator, compute voltage samples */ /* v[0 to n-1], vt[0 to n], throw out vt[n] so use vt[0 to n-1] */ if (vtramp_param.config == VTRAMP_PDI5025) { for (i = 0; i < num_samp; i++) v_samp[i] = (vt_samp[i+1] - vt_samp[i]) / delta_t_samp; } /* When using a voltmeter, compute integrated voltage samples */ /* v[0 to n-1], vt[0 to n-1] */ if (vtramp_param.config == VTRAMP_HP3458) { vt_samp[0] = 0.; for (i = 1; i < num_samp; i++) vt_samp[i] = vt_samp[i-1] + 0.5 * (v_samp[i] + v_samp[i-1]) * delta_t_samp; } /* Perform an offset correction */ vtramp_offset_correction(delta_t_samp, num_samp, v_samp, vt_samp); /* Update the user interface */ if (vtramp_param.show_ui == VTRAMP_TRUE) vtrampui_update(delta_t_samp, num_samp, v_samp, vt_samp); /* Return the integrated voltage, average over the last couple readings */ num_ave = 10; sum = 0.; for (i = 1; i <= num_ave; i++) sum = sum + vt_samp[num_samp - i]; *vt = sum / (double) num_ave; /* Log the measurement results */ vtramp_log_vt(imag, *vt); /* Done */ return; } /* ************************************************************** */ /* * vtramp_exit * This function is used to exit the integrated voltage * measurement system. * * Zachary Wolf * 4/11/02 */ void vtramp_exit(void) { /* Exit the hardware */ if (vtramp_param.config == VTRAMP_PDI5025) { pdi5025_exit(pdi5025_ID); } else if (vtramp_param.config == VTRAMP_HP3458) { hp3458_exit(hp3458_ID); } else { vtramp_error("Unknown integrated voltage system configuration"); return; } /* Done */ return; } /* ************************************************************** */ /* INTERNAL FUNCTIONS */ /* ************************************************************** */ /* * vtramp_error * This function handles errors for the VTRAMP module. * * Input: * message, string to display in standard I/O * * Zachary Wolf * 10/11/98 */ void vtramp_error(char* message) { /* Declare variables */ char buf[80]; /* Notify the operator of the error */ printf("\nVTRAMP 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); } /* ************************************************************** */ /* * vtramp_message * This function handles messages for the IMAG module. * * Input: * message, string to display in standard I/O * * Zachary Wolf * 10/12/98 */ void vtramp_message(char* message) { /* Print the message */ printf("%s\n", message); /* Done */ return; } /* ************************************************************** */ /* * vtramp_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 vtramp_check_param() { /* log_file */ if (CompareStrings(log_file, 0, "", 0, 0) == 0) { vtramp_error("Log file not defined."); return -1; } /* vtramp_param.board_addr */ if (vtramp_param.board_addr < 0 || vtramp_param.board_addr > 10) { Fmt(msg, "%s 30) { Fmt(msg, "%s 30) { Fmt(msg, "%s 1000) { Fmt(msg, "%s 1.) { Fmt(msg, "%s 10.) { Fmt(msg, "%s 10.) { Fmt(msg, "%s