#include "InitStatus.h" #include "RdParam.h" //============================================================================== // // Title: ZScan // Purpose: A short description of the application. // // Created on: 5/6/2014 at 12:45:37 PM by Arnaud Madur. // Copyright: LBNL. All Rights Reserved. // //============================================================================== //============================================================================== // Include files #include #include #include #include "ZScan.h" #include "toolbox.h" #include "Utilities.h" #include "Hp3458.h" #include "umac.h" #include "umacParam.h" #include "Param_umac.h" #include "spline.h" //============================================================================== // Constants #define PRMFILE "w:\\LCLS-II\\data\\ZScan.prm" #define UNDPRMFILE "w:\\LCLS-II\\data\\Und.prm" #define ZGAUSSFILE "w:\\LCLS-II\\data\\ZGauss.log" #define X0 -9.0 #define NZG 5 #define NCOLORS 5 //============================================================================== // Types //============================================================================== // Static global variables static int panelHandle_SC; static int panelHandle_PM; static int panelHandle_IN; static int umac_ID; //============================================================================== // Static functions //============================================================================== // Global variables double posx, posy, posz; double posrx, posry, posrz, poswz, posvy; unsigned int plot_num = 0; int colors[] = {VAL_RED, VAL_GREEN, VAL_BLUE, VAL_CYAN, VAL_MAGENTA}; unsigned int LED_toggle = TRUE; FILE *fpl; // pointer to log file // UMAC variables UMAC_PARAM_STRUCT nParam[9]; int ID =-1; static DWORD UMAC_ID; //============================================================================== // Global functions /// HIFN The main entry-point function. int main (int argc, char *argv[]) { int error = 0; char und_prm_file[FILENAME] = UNDPRMFILE; char param_file[FILENAME] = PRMFILE; char log_file[FILENAME]; char buffer[LINE]; int i, j, ln; FILE *fpi; // initialize and load resources nullChk (InitCVIRTE (0, argv, 0)); errChk (panelHandle_SC = LoadPanel (0, "ZScan.uir", SCAN_PANEL)); // Main panel errChk (panelHandle_PM = LoadPanel (0, "RdParam.uir", PRM_PANEL)); // Parameter panel errChk (panelHandle_IN = LoadPanel (0, "InitStatus.uir", PRM_PANEL)); // Parameter panel // Display the panels errChk (DisplayPanel (panelHandle_SC)); errChk (DisplayPanel (panelHandle_PM)); errChk (DisplayPanel (panelHandle_IN)); sprintf (buffer, "INITIALIZATION STATUS\n"); SetCtrlVal (panelHandle_IN, IN_PANEL_TEXTBOX_STATUS, buffer); sprintf (buffer, "After Hardware Initialization is complete, You Will be Prompted to Proceed\n\n"); SetCtrlVal (panelHandle_IN, IN_PANEL_TEXTBOX_STATUS, buffer); sprintf (buffer, "Read in undulator definition from %s\n", und_prm_file); SetCtrlVal (panelHandle_IN, IN_PANEL_TEXTBOX_STATUS, buffer); // Read in undulator type, ID and data directory fpi = fopen (und_prm_file, "r"); fgets (und_type, LINE, fpi); fgets (und_ID, LINE, fpi); fgets (und_dir, FILENAME, fpi); fclose (fpi); SetCtrlVal (panelHandle_SC, SCAN_PANEL_STR_ID, und_ID); SetCtrlVal (panelHandle_SC, SCAN_PANEL_STR_TYPE, und_type); // Create data file name ln = strlen (und_dir); ln--; // last character is EOL - don't copy for (i = 0; i < ln; i++) und_file[i] = und_dir[i]; ln = strlen (und_ID); ln--; // last character is EOL - don't copy for (j = 0; j < ln; j++, i++) und_file[i] = und_ID[j]; und_file[i] = '\0'; sprintf (buffer, "%d", und_ID); strcat (und_file, buffer); strcat (und_file, ".dat"); // Read parameters sprintf (buffer, "Read in parameters from %s and setup Parameter Panel\n", param_file); SetCtrlVal (panelHandle_IN, IN_PANEL_TEXTBOX_STATUS, buffer); RdParamFile (param_file); // Setup parameter panel and display SetCtrlVal (panelHandle_PM, PRM_PANEL_ST_ID, und_ID); SetCtrlVal (panelHandle_PM, PRM_PANEL_ST_ID_TYP, und_type); SetCtrlVal (panelHandle_PM, PRM_PANEL_ST_DATA_DIR, und_dir); SetCtrlVal (panelHandle_PM, PRM_PANEL_GPIB_1, GPIBDVM[0]); SetCtrlVal (panelHandle_PM, PRM_PANEL_GPIB_2, GPIBDVM[1]); SetCtrlVal (panelHandle_PM, PRM_PANEL_N_VS_DVM1, DCV[0]); SetCtrlVal (panelHandle_PM, PRM_PANEL_N_VS_DVM2, DCV[1]); SetCtrlVal (panelHandle_PM, PRM_PANEL_N_INT_TM, int_tm); SetCtrlVal (panelHandle_PM, PRM_PANEL_N_TR_AXIS, trig_axis); SetCtrlVal (panelHandle_PM, PRM_PANEL_N_SC_V, Sc_V); SetCtrlVal (panelHandle_PM, PRM_PANEL_N_HP_X0, x_scan); SetCtrlVal (panelHandle_PM, PRM_PANEL_N_HP_Y0, y_scan); SetCtrlVal (panelHandle_PM, PRM_PANEL_N_SC_STRT, Sc_strt); SetCtrlVal (panelHandle_PM, PRM_PANEL_N_SC_STP, Sc_stp); SetCtrlVal (panelHandle_PM, PRM_PANEL_N_HP_Z1, HP_z[0]); SetCtrlVal (panelHandle_PM, PRM_PANEL_N_HP_Z2, HP_z[1]); SetCtrlVal (panelHandle_PM, PRM_PANEL_N_TR_SPC, trig_spc); SetCtrlVal (panelHandle_PM, PRM_PANEL_ST_HP_NM, HP_ID); SetCtrlVal (panelHandle_PM, PRM_PANEL_ST_HP_FL, HP_cal_file); // Open log file ln = strlen (und_dir); ln--; // last character is EOL - don't copy for (i = 0; i < ln; i++) log_file[i] = und_dir[i]; ln = strlen (und_ID); ln--; // last character is EOL - don't copy for (j = 0; j < ln; j++, i++) log_file[i] = und_ID[j]; log_file[i] = '\0'; strcat (log_file, ".log"); fpl = fopen (log_file, "a"); sprintf (buffer, "Open log file %s\n", log_file); SetCtrlVal (panelHandle_IN, IN_PANEL_TEXTBOX_STATUS, buffer); sprintf (buffer, "\nBEGIN HARDWARE INITIALIZATION BY HITTING THE BUTTON AT THE TOP OF PANEL\n\n"); SetCtrlVal (panelHandle_IN, IN_PANEL_TEXTBOX_STATUS, buffer); errChk (RunUserInterface ()); Error: /* clean up */ if (panelHandle_IN > 0) DiscardPanel (panelHandle_IN); if (panelHandle_PM > 0) DiscardPanel (panelHandle_PM); if (panelHandle_SC > 0) DiscardPanel (panelHandle_SC); fclose (fpl); free (HP_cal_nx); free (HP_cal_px); free (HP_cal_ny); free (HP_cal_py); return 0; } int CVICALLBACK panelCB (int panel, int event, void *callbackData, int eventData1, int eventData2) { BOOL res; double xm; if (event == EVENT_CLOSE) { // Retract probe xm = X0/1000; res = umac_abs_move(umac_ID, 3, UMAC_AXIS3_ACCELERATION, UMAC_AXIS3_VELOCITY, xm); umac_exit(0); QuitUserInterface (0); } return 0; } //============================================================================== // UI callback function prototypes /// HIFN Exit when the user dismisses the panel. // // Callback functions for main panel // int CVICALLBACK Initialize (int panel, int control, int event, void *callbackData, int eventData1, int eventData2) { int resp = 0; char StatBuffer[LINE]; switch (event) { case EVENT_COMMIT: // Begin initializing hardware // Turn off LED timer SuspendTimerCallbacks (); // Turn off LED SetCtrlVal (panelHandle_IN, IN_PANEL_LED, 0); // Initialize HP3458s sprintf (StatBuffer, "Initialize HP3458 @ GPIB:%d.\n", GPIBDVM[0]); SetCtrlVal (panelHandle_IN, IN_PANEL_TEXTBOX_STATUS, StatBuffer); hp3458_init (0, GPIBDVM[0], &hp3458_ID[0]); //Bx sprintf (StatBuffer, "Initialize HP3458 @ GPIB:%d.\n\n", GPIBDVM[1]); SetCtrlVal (panelHandle_IN, IN_PANEL_TEXTBOX_STATUS, StatBuffer); hp3458_init (0, GPIBDVM[1], &hp3458_ID[1]); //By // Initialize Kugler controls sprintf (StatBuffer, "Initialize Kugler Controls.\n"); SetCtrlVal (panelHandle_IN, IN_PANEL_TEXTBOX_STATUS, StatBuffer); umac_data_fill_param_struct(nParam); UMAC_ID = umac_init(0, &ID, nParam); if (UMAC_ID == -1) { // Shutdown program for restart QuitUserInterface (0); return -1; } umac_ID=ID; // Acquire initial Kugler axes position sprintf (StatBuffer, "Determine Axes Positions.\n"); SetCtrlVal (panelHandle_IN, IN_PANEL_TEXTBOX_STATUS, StatBuffer); if (umac_get_mot_position(umac_ID, 1, &posz)) { //position in [m] sprintf (StatBuffer, "Motor 1 is Ready.\n"); SetCtrlVal (panelHandle_IN, IN_PANEL_TEXTBOX_STATUS, StatBuffer); } if (umac_get_mot_position(umac_ID, 2, &posy)) { //position in [m] sprintf (StatBuffer, "Motor 2 is Ready.\n"); SetCtrlVal (panelHandle_IN, IN_PANEL_TEXTBOX_STATUS, StatBuffer); } if (umac_get_mot_position(umac_ID, 3, &posx)) { //position in [m] sprintf (StatBuffer, "Motor 3 is Ready.\n"); SetCtrlVal (panelHandle_IN, IN_PANEL_TEXTBOX_STATUS, StatBuffer); } if (umac_get_mot_position(umac_ID, 5, &posrx)) { //position in [m] sprintf (StatBuffer, "Motor 5 is Ready.\n"); SetCtrlVal (panelHandle_IN, IN_PANEL_TEXTBOX_STATUS, StatBuffer); } if (umac_get_mot_position(umac_ID, 8, &posry)) { //position in [m] sprintf (StatBuffer, "Motor 8 is Ready.\n"); SetCtrlVal (panelHandle_IN, IN_PANEL_TEXTBOX_STATUS, StatBuffer); } if (umac_get_mot_position(umac_ID, 9, &posrz)) { //position in [m] sprintf (StatBuffer, "Motor 9 is Ready.\n"); SetCtrlVal (panelHandle_IN, IN_PANEL_TEXTBOX_STATUS, StatBuffer); } if (umac_get_mot_position(umac_ID, 6, &poswz)) { //position in [m] sprintf (StatBuffer, "Motor 6 is Ready.\n"); SetCtrlVal (panelHandle_IN, IN_PANEL_TEXTBOX_STATUS, StatBuffer); } if (umac_get_mot_position(umac_ID, 7, &posvy)) { //position in [m] sprintf (StatBuffer, "Motor 7 is Ready.\n"); SetCtrlVal (panelHandle_IN, IN_PANEL_TEXTBOX_STATUS, StatBuffer); } posz=posz*1000; posy=posy*1000; posx=posx*1000; //convert position to [mm] SetCtrlVal(panelHandle_SC,SCAN_PANEL_NUM_X,posx); // display position in [mm] SetCtrlVal(panelHandle_SC,SCAN_PANEL_NUM_Y,posy); // display position in [mm] SetCtrlVal(panelHandle_SC,SCAN_PANEL_NUM_Z,posz); // display position in [mm] SetCtrlVal (panelHandle_SC, SCAN_PANEL_NUMERIC_X0, x_scan); // Scan x SetCtrlVal (panelHandle_SC, SCAN_PANEL_NUMERIC_Y0, y_scan); // Scan y sprintf (StatBuffer, "\nBEFORE RUNNING SCAN\n"); SetCtrlVal (panelHandle_IN, IN_PANEL_TEXTBOX_STATUS, StatBuffer); sprintf (StatBuffer, "MAKE SURE x AND y PROBE POSITIONS ARE SET TO CLEAR OBSTACLES\n\n"); SetCtrlVal (panelHandle_IN, IN_PANEL_TEXTBOX_STATUS, StatBuffer); // Get operator if (!operator_set) { PromptPopup ("Operator", "Must Specify Operator", operator, NAME-1); SetCtrlVal (panelHandle_SC, SCAN_PANEL_STR_OPRTR, operator); operator_set = TRUE; } // Prompt to proceed resp = ConfirmPopup ("Continue Program", "Proceed with Parameter Set?"); if (resp) { DiscardPanel (panelHandle_IN); DisplayPanel (panelHandle_PM); } break; case EVENT_RIGHT_CLICK: break; } return 0; } int CVICALLBACK Init_timer (int panel, int control, int event, void *callbackData, int eventData1, int eventData2) { switch (event) { case EVENT_TIMER_TICK: SetCtrlVal (panelHandle_IN, IN_PANEL_LED, LED_toggle); if (LED_toggle) LED_toggle = FALSE; else LED_toggle = TRUE; break; } return 0; } int CVICALLBACK Rd_Param (int panel, int control, int event, void *callbackData, int eventData1, int eventData2) { switch (event) { case EVENT_COMMIT: // Initializing panel values not necessary - set values are saved DisplayPanel (panelHandle_PM); // display the panel and run the user interface break; case EVENT_RIGHT_CLICK: break; } return 0; } int CVICALLBACK Log_Entry (int panel, int control, int event, void *callbackData, int eventData1, int eventData2) { char buffer[LINE]; switch (event) { case EVENT_COMMIT: GetCtrlVal (panelHandle_SC, SCAN_PANEL_STR_LOG, buffer); fprintf (fpl, "%s\n", buffer); break; case EVENT_RIGHT_CLICK: break; } return 0; } int CVICALLBACK Move_probe (int panel, int control, int event, void *callbackData, int eventData1, int eventData2) { float x, y, z, dx, dy, dz; float xm, ym, zm, dxm, dym, dzm; double posx, posy, posz; BOOL res; switch (event) { case EVENT_COMMIT: // Read x, y, z values GetCtrlVal (panelHandle_SC, SCAN_PANEL_NUM_X, &x); // Get specified x in mm GetCtrlVal (panelHandle_SC, SCAN_PANEL_NUM_Y, &y); // Get specified y in mm GetCtrlVal (panelHandle_SC, SCAN_PANEL_NUM_Z, &z); // Get specified z in mm GetCtrlVal (panelHandle_SC, SCAN_PANEL_NUM_DX, &dx); // Get specified dx in mm GetCtrlVal (panelHandle_SC, SCAN_PANEL_NUM_DY, &dy); // Get specified dy in mm GetCtrlVal (panelHandle_SC, SCAN_PANEL_NUM_DZ, &dz); // Get specified dz in mm SetCtrlVal (panelHandle_SC, SCAN_PANEL_LED_MOVE, 1); // Turn on Move LED // Execute move xm=x/1000; ym=y/1000; zm=z/1000; dxm=dx/1000; dym=dy/1000; dzm=dz/1000; res = umac_abs_move(umac_ID, 3, UMAC_AXIS3_ACCELERATION, UMAC_AXIS3_VELOCITY, xm); Delay(0.25); res = umac_abs_move(umac_ID, 2, UMAC_AXIS2_ACCELERATION, UMAC_AXIS2_VELOCITY, ym); Delay(0.25); res = umac_abs_move(umac_ID, 1, UMAC_AXIS1_ACCELERATION, UMAC_AXIS1_VELOCITY, zm); Delay(0.25); // umac_rel_move doesn't work res = umac_rel_move(umac_ID, 3, UMAC_AXIS3_ACCELERATION, UMAC_AXIS3_VELOCITY, dxm); Delay(0.25); res = umac_rel_move(umac_ID, 2, UMAC_AXIS2_ACCELERATION, UMAC_AXIS2_VELOCITY, dym); Delay(0.25); res = umac_rel_move(umac_ID, 1, UMAC_AXIS1_ACCELERATION, UMAC_AXIS1_VELOCITY, dzm); Delay(0.25); //res = umac_abs_move(umac_ID, 6, UMAC_AXIS6_ACCELERATION, UMAC_AXIS6_VELOCITY, wzm); Delay(0.25); //res = umac_abs_move(umac_ID, 7, UMAC_AXIS7_ACCELERATION, UMAC_AXIS7_VELOCITY, vym); Delay(0.25); SetCtrlVal (panelHandle_SC, SCAN_PANEL_LED_MOVE, 0); // Turn off Move LED // Set x, y, z values // Acquire Kugler axes position umac_get_mot_position(umac_ID, 1, &posz); //position in [m] umac_get_mot_position(umac_ID, 2, &posy); //position in [m] umac_get_mot_position(umac_ID, 3, &posx); //position in [m] posz=posz*1000; posy=posy*1000; posx=posx*1000; //convert position to [mm] SetCtrlVal (panelHandle_SC, SCAN_PANEL_NUM_X, x); // Record x SetCtrlVal (panelHandle_SC, SCAN_PANEL_NUM_Y, y); // Record y SetCtrlVal (panelHandle_SC, SCAN_PANEL_NUM_Z, z); // Record z SetCtrlVal (panelHandle_SC, SCAN_PANEL_NUM_DX, 0.0); // Record rx SetCtrlVal (panelHandle_SC, SCAN_PANEL_NUM_DY, 0.0); // Record ry SetCtrlVal (panelHandle_SC, SCAN_PANEL_NUM_DZ, 0.0); // Record rz break; case EVENT_RIGHT_CLICK: break; } return 0; } int CVICALLBACK TimedFunctions (int panel, int control, int event, void *callbackData, int eventData1, int eventData2) { double Vx, Vy, Bx, By; switch (event) { case EVENT_TIMER_TICK: SetCtrlVal (panelHandle_SC, SCAN_PANEL_LED_TEMP, 1); // Turn on Temp. LED // Sample temperatures // Display temperatures SetCtrlVal (panelHandle_SC, SCAN_PANEL_NUM_RM_TEMP, RmTemp); SetCtrlVal (panelHandle_SC, SCAN_PANEL_NUM_U_TEMP1, Temp[0]); SetCtrlVal (panelHandle_SC, SCAN_PANEL_NUM_U_TEMP2, Temp[1]); SetCtrlVal (panelHandle_SC, SCAN_PANEL_NUM_U_TEMP3, Temp[2]); SetCtrlVal (panelHandle_SC, SCAN_PANEL_NUM_U_TEMP4, Temp[3]); SetCtrlVal (panelHandle_SC, SCAN_PANEL_NUM_U_TEMP5, Temp[4]); SetCtrlVal (panelHandle_SC, SCAN_PANEL_NUM_U_TEMP6, Temp[5]); // Sample Bx and By // Measure votages hp3458_get_voltage(hp3458_ID[0], &Vx); hp3458_get_voltage(hp3458_ID[1], &Vy); // Convert to B Bx = HP_Calibrate (Vx, HP_cal_nx, HP_cal_px, ncalx); By = HP_Calibrate (Vy, HP_cal_ny, HP_cal_py, ncaly); SetCtrlVal (panelHandle_SC, SCAN_PANEL_LED_TEMP, 0); // Turn off Temp. LED break; } return 0; } int CVICALLBACK DoScan (int panel, int control, int event, void *callbackData, int eventData1, int eventData2) { int i; double trigval[5]={trig_axis,trig_spc,Sc_strt,Sc_stp,Sc_V}; char umacCom[120]; char umacRes[100]; //int i=-1; double maxPulse=-2; int velval; BOOL res; BOOL zero_Gauss = TRUE; BOOL ZG_log = TRUE; double xm, ym, zm; double Vx0G, Vy0G; double *y2, *I1, *I2; double Vx_ave, Vy_ave, Vx_rms, Vy_rms; FILE *fpi; unsigned int move = 0; switch (event) { case EVENT_COMMIT: y2 = calloc (num_pnts, sizeof(double)); I1 = calloc (num_pnts, sizeof(double)); I2 = calloc (num_pnts, sizeof(double)); // Allocate voltage vectors SuspendTimerCallbacks (); // Turn off timer SetCtrlVal (panelHandle_SC, SCAN_PANEL_LED_SCAN, 1); // Turn on scan LED SetCtrlVal (panelHandle_SC, SCAN_PANEL_TEXTBOX, "\nBegin Scan\n"); // Go to x and y probe positions for scan SetCtrlVal (panelHandle_IN, IN_PANEL_TEXTBOX_STATUS, "\nMove probe to scan x and y positions\n"); ym = y_scan/1000; res = umac_abs_move(umac_ID, 2, UMAC_AXIS2_ACCELERATION, UMAC_AXIS2_VELOCITY, ym); Delay(0.5); move = umac_AxisIsMoving(umac_ID, 2); while (move) { Delay(1.0); move = umac_AxisIsMoving(umac_ID, 2); } xm = x_scan/1000; res = umac_abs_move(umac_ID, 3, UMAC_AXIS3_ACCELERATION, UMAC_AXIS3_VELOCITY, xm); Delay(0.5); move = umac_AxisIsMoving(umac_ID, 3); while (move) { Delay(1.0); move = umac_AxisIsMoving(umac_ID, 3); } SetCtrlVal (panelHandle_SC, SCAN_PANEL_NUM_X, x_scan); // Record x SetCtrlVal (panelHandle_SC, SCAN_PANEL_NUM_Y, y_scan); // Record y // Retrieve historical 0-Gauss readings from log SetCtrlVal (panelHandle_SC, SCAN_PANEL_TEXTBOX, "Retrieve zero-Gauss value from log\n"); fpi = fopen (ZGAUSSFILE, "r"); ReadZGauss (fpi, &Vx_ave, &Vy_ave, &Vx_rms, &Vy_rms); fclose (fpi); GetCtrlVal (panelHandle_SC, SCAN_PANEL_BINARYSWITCH_0G, &ZG_log); GetCtrlVal (panelHandle_PM, PRM_PANEL_BINARYSWITCH_ZG, &zero_Gauss); if (zero_Gauss) { // Measure Vx and Vy offsets // Zero-Gauss reading before scan // Go to z location for chamber #1 SetCtrlVal (panelHandle_SC, SCAN_PANEL_TEXTBOX, "Move to First zero-Gauss Chamber\n"); zm = HP_z[0]/1000; res = umac_abs_move(umac_ID, 1, UMAC_AXIS1_ACCELERATION, UMAC_AXIS1_VELOCITY, zm); Delay(0.5); move = umac_AxisIsMoving(umac_ID, 1); while (move) { // wait until motion has stoped Delay(1.0); move = umac_AxisIsMoving(umac_ID, 1); } // Do measurement Vx_o1 = Vy_o1 = 0.0; for (i = 0; i < NZG; i++) { hp3458_get_voltage(hp3458_ID[0], &Vx0G); // Vx hp3458_get_voltage(hp3458_ID[1], &Vy0G); // Vy Vx_o1 += Vx0G; Vy_o1 += Vy0G; } Vx_o1 /= NZG; Vy_o1 /= NZG; SetCtrlVal (panelHandle_SC, SCAN_PANEL_NUM_Z, HP_z[0]); // Record z } // Display on screen SetCtrlVal (panelHandle_SC, SCAN_PANEL_NUM_VX_REF1, Vx_o1); SetCtrlVal (panelHandle_SC, SCAN_PANEL_NUM_VY_REF1, Vy_o1); // Move to start location // Setup triggering SetCtrlVal (panelHandle_SC, SCAN_PANEL_TEXTBOX, "Go to start position and do scan\n"); hp3458_setup_triggered_voltage_samples (hp3458_ID[0], DCV[0], int_tm, num_pnts); // Bx hp3458_setup_triggered_voltage_samples (hp3458_ID[1], DCV[1], int_tm, num_pnts); // By for (i = 0; i < num_pnts; i++) { z[i]=(Sc_strt+i*trig_spc)/1000.0; // values in m } // Start motion sprintf(umacCom,"q0=%f",trigval[0]); //set the trigger source (Kugler manual p.60) umac_command(umac_ID, umacRes, 0, umacCom); sprintf(umacCom,"q1=%f",trigval[1]); //set the trigger pitch in mm (Kugler manual p.60) umac_command(umac_ID, umacRes, 0, umacCom); sprintf(umacCom,"p1=%f",trigval[2]); //set init position of the carriage (motion program 8) umac_command(umac_ID, umacRes, 0, umacCom); sprintf(umacCom,"p2=%f",trigval[3]); umac_command(umac_ID, umacRes, 0, umacCom); //set final position of the carriage (motion program 8) velval=floor(trigval[0]); trigval[4]=trigval[4]*20; sprintf(umacCom,"i%i22=%f",velval,trigval[4]); umac_command(umac_ID, umacRes, 0, umacCom); //set the trigger process velocity (motion program 8) umac_command(umac_ID, umacRes, 0, "q10=1"); //set the flexible start pos var to 1 (enable) sprintf(umacCom,"q11=%f",trigval[2]); umac_command(umac_ID, umacRes, 0, umacCom); // set the position of the 1st trigger maxPulse=fabs(floor((trigval[3]-trigval[2])/trigval[1])); sprintf(umacCom,"q13=%f",maxPulse); umac_command(umac_ID, umacRes, 0, umacCom); // set the max number of trigger pulses umac_command(umac_ID, umacRes, 0, "q14=0"); //select the trigger edge 0=rising edge umac_command(umac_ID, umacRes, 0, "b8r"); //launch triggering process // Start scan triggers hp3458_start_triggered_voltage_samples (hp3458_ID[0]); // Bx hp3458_start_triggered_voltage_samples (hp3458_ID[1]); // By Delay(1.0); // Retrieve data hp3458_collect_triggered_voltage_samples (hp3458_ID[0], num_pnts, Vx); // Bx hp3458_collect_triggered_voltage_samples (hp3458_ID[1], num_pnts, Vy); // By Delay(1.0); if (zero_Gauss) { // Measure Vx and Vy offsets // Zero-Gauss reading after scan // Go to z location for chamber #1 SetCtrlVal (panelHandle_SC, SCAN_PANEL_TEXTBOX, "Go to second zero-Gauss chamber\n"); zm = HP_z[1]/1000; res = umac_abs_move(umac_ID, 1, UMAC_AXIS1_ACCELERATION, UMAC_AXIS1_VELOCITY, zm); Delay(0.5); move = umac_AxisIsMoving(umac_ID, 1); while (move) { // wait until motion has stoped Delay(1.0); move = umac_AxisIsMoving(umac_ID, 1); } // Do measurement Vx_o2 = Vy_o2 = 0.0; for (i = 0; i < NZG; i++) { hp3458_get_voltage(hp3458_ID[0], &Vx0G); // Vx hp3458_get_voltage(hp3458_ID[1], &Vy0G); // Vy Vx_o2 += Vx0G; Vy_o2 += Vy0G; } Vx_o2 /= NZG; Vy_o2 /= NZG; // Display on screen SetCtrlVal (panelHandle_SC, SCAN_PANEL_NUM_VX_REF2, Vx_o2); SetCtrlVal (panelHandle_SC, SCAN_PANEL_NUM_VY_REF2, Vy_o2); Vx0G = 0.5*(Vx_o1 + Vx_o2); Vy0G = 0.5*(Vy_o1 + Vy_o2); if ((fabs(Vx0G - Vx_ave) > 2*Vx_rms) || (fabs(Vy0G - Vy_ave) > 2*Vy_rms)) { MessagePopup ("Zero-Gauss Deviation", "Zero-Gauss reading has exceeded allowed limit"); Vx0G = Vx_ave; Vy0G = Vy_ave; } else if (ZG_log) { // Recored value to log fpi = fopen (ZGAUSSFILE, "a"); LogZGauss (fpi, Vx_o2, Vy_o2); fclose (fpi); } SetCtrlVal (panelHandle_SC, SCAN_PANEL_NUM_Z, HP_z[1]); // Record z } else { // use the values from the log Vx0G = Vx_ave; Vy0G = Vy_ave; } // Turn off LED SetCtrlVal (panelHandle_SC, SCAN_PANEL_LED_SCAN, 0); // Turn off scan LED // Convert to Bx, By // Get V offset correction - average first and second readings Vx_offset = Vx0G - Vx_cal0; Vy_offset = Vy0G - Vy_cal0; for (i = 0; i < num_pnts; i++) { Vx_corr[i] = Vx[i] - Vx_offset; Vy_corr[i] = Vy[i] - Vy_offset; } for (i = 0; i < num_pnts; i++) { Bx[i] = HP_Calibrate (Vx_corr[i], HP_cal_nx, HP_cal_px, ncalx); By[i] = HP_Calibrate (Vy_corr[i], HP_cal_ny, HP_cal_py, ncaly); } // Plot data num_pnts1 = num_pnts - 1; if (plot_num >= NCOLORS) plot_num = 0; PlotXY (panelHandle_SC, SCAN_PANEL_GRAPH_BX, z, Bx, num_pnts1, VAL_DOUBLE, VAL_DOUBLE, VAL_THIN_LINE, VAL_EMPTY_SQUARE, VAL_SOLID, 1, colors[plot_num]); PlotXY (panelHandle_SC, SCAN_PANEL_GRAPH_BY, z, By, num_pnts1, VAL_DOUBLE, VAL_DOUBLE, VAL_THIN_LINE, VAL_EMPTY_SQUARE, VAL_SOLID, 1, colors[plot_num]); // Calculate first and second integrals and plot spline (z, Bx, num_pnts, y2); spli1 (z, Bx, y2, num_pnts, z, I1, num_pnts); spline (z, I1, num_pnts, y2); spli1 (z, I1, y2, num_pnts, z, I2, num_pnts); PlotXY (panelHandle_SC, SCAN_PANEL_GRAPH_IX, z, I1, num_pnts1, VAL_DOUBLE, VAL_DOUBLE, VAL_THIN_LINE, VAL_EMPTY_SQUARE, VAL_SOLID, 1, colors[plot_num]); PlotXY (panelHandle_SC, SCAN_PANEL_GRAPH_I2X, z, I2, num_pnts1, VAL_DOUBLE, VAL_DOUBLE, VAL_THIN_LINE, VAL_EMPTY_SQUARE, VAL_SOLID, 1, colors[plot_num]); spline (z, By, num_pnts, y2); spli1 (z, By, y2, num_pnts, z, I1, num_pnts); spline (z, I1, num_pnts, y2); spli1 (z, I1, y2, num_pnts, z, I2, num_pnts); PlotXY (panelHandle_SC, SCAN_PANEL_GRAPH_IY, z, I1, num_pnts1, VAL_DOUBLE, VAL_DOUBLE, VAL_THIN_LINE, VAL_EMPTY_SQUARE, VAL_SOLID, 1, colors[plot_num]); PlotXY (panelHandle_SC, SCAN_PANEL_GRAPH_I2Y, z, I2, num_pnts1, VAL_DOUBLE, VAL_DOUBLE, VAL_THIN_LINE, VAL_EMPTY_SQUARE, VAL_SOLID, 1, colors[plot_num]); plot_num++; // Tuning stage GetCtrlVal (panelHandle_SC, SCAN_PANEL_RING_TUNE, &tune_stage); // Data set and run number GetCtrlVal (panelHandle_SC, SCAN_PANEL_NUM_DATA_SET, &data_set); GetCtrlVal (panelHandle_SC, SCAN_PANEL_NUM_RUN, &run_num); // Write to data file FileSelectPopup (und_dir, und_file, "", "Data File", VAL_OK_BUTTON, 0, 0, 1, 0, und_file); WriteDataFile (); // Write to log LogRecord (und_ID, fpl); // Turn off scan LED ResumeTimerCallbacks (); free (y2); free (I1); free (I2); break; case EVENT_RIGHT_CLICK: break; } return 0; } int CVICALLBACK ClearGraphs (int panel, int control, int event, void *callbackData, int eventData1, int eventData2) { switch (event) { case EVENT_COMMIT: DeleteGraphPlot (panelHandle_SC, SCAN_PANEL_GRAPH_BX, -1, VAL_IMMEDIATE_DRAW); DeleteGraphPlot (panelHandle_SC, SCAN_PANEL_GRAPH_BY, -1, VAL_IMMEDIATE_DRAW); DeleteGraphPlot (panelHandle_SC, SCAN_PANEL_GRAPH_IX, -1, VAL_IMMEDIATE_DRAW); DeleteGraphPlot (panelHandle_SC, SCAN_PANEL_GRAPH_IY, -1, VAL_IMMEDIATE_DRAW); DeleteGraphPlot (panelHandle_SC, SCAN_PANEL_GRAPH_I2X, -1, VAL_IMMEDIATE_DRAW); DeleteGraphPlot (panelHandle_SC, SCAN_PANEL_GRAPH_I2Y, -1, VAL_IMMEDIATE_DRAW); break; case EVENT_RIGHT_CLICK: break; } return 0; } int CVICALLBACK Quit (int panel, int control, int event, void *callbackData, int eventData1, int eventData2) { BOOL res; double xm; switch (event) { case EVENT_COMMIT: // Retract probe xm = X0/1000; res = umac_abs_move(umac_ID, 3, UMAC_AXIS3_ACCELERATION, UMAC_AXIS3_VELOCITY, xm); umac_exit(0); QuitUserInterface (0); break; case EVENT_RIGHT_CLICK: break; } return 0; } // ******************************************************************************* // // Callback functions for Parameter Panel // // ******************************************************************************* int CVICALLBACK Get_und_ID (int panel, int control, int event, void *callbackData, int eventData1, int eventData2) { switch (event) { case EVENT_COMMIT: GetCtrlVal (panelHandle_PM, PRM_PANEL_ST_ID, und_ID); break; case EVENT_RIGHT_CLICK: break; } return 0; } int CVICALLBACK Get_und_type (int panel, int control, int event, void *callbackData, int eventData1, int eventData2) { switch (event) { case EVENT_COMMIT: GetCtrlVal (panelHandle_PM, PRM_PANEL_ST_ID_TYP, und_type); break; case EVENT_RIGHT_CLICK: break; } return 0; } int CVICALLBACK Data_dir (int panel, int control, int event, void *callbackData, int eventData1, int eventData2) { switch (event) { case EVENT_COMMIT: GetCtrlVal (panelHandle_PM, PRM_PANEL_ST_DATA_DIR, und_dir); break; case EVENT_RIGHT_CLICK: DirSelectPopup ("w:\\LCLS-II\\data", "Select Directory", 1, 1, und_dir); break; } return 0; } int CVICALLBACK Get_GPIB1 (int panel, int control, int event, void *callbackData, int eventData1, int eventData2) { switch (event) { case EVENT_COMMIT: GetCtrlVal (panelHandle_PM, PRM_PANEL_GPIB_1, &GPIBDVM[0]); hp3458_init (0, GPIBDVM[0], &hp3458_ID[0]); //Bx break; case EVENT_RIGHT_CLICK: break; } return 0; } int CVICALLBACK Get_GPIB2 (int panel, int control, int event, void *callbackData, int eventData1, int eventData2) { switch (event) { case EVENT_COMMIT: GetCtrlVal (panelHandle_PM, PRM_PANEL_GPIB_2, &GPIBDVM[1]); hp3458_init (0, GPIBDVM[1], &hp3458_ID[1]); //By break; case EVENT_RIGHT_CLICK: break; } return 0; } int CVICALLBACK Get_VScale1 (int panel, int control, int event, void *callbackData, int eventData1, int eventData2) { switch (event) { case EVENT_COMMIT: GetCtrlVal (panelHandle_PM, PRM_PANEL_N_VS_DVM1, &DCV[0]); break; case EVENT_RIGHT_CLICK: break; } return 0; } int CVICALLBACK Get_VScale2 (int panel, int control, int event, void *callbackData, int eventData1, int eventData2) { switch (event) { case EVENT_COMMIT: GetCtrlVal (panelHandle_PM, PRM_PANEL_N_VS_DVM2, &DCV[1]); break; case EVENT_RIGHT_CLICK: break; } return 0; } int CVICALLBACK Get_Int_Tm (int panel, int control, int event, void *callbackData, int eventData1, int eventData2) { switch (event) { case EVENT_COMMIT: GetCtrlVal (panelHandle_PM, PRM_PANEL_N_INT_TM, &int_tm); break; case EVENT_RIGHT_CLICK: break; } return 0; } int CVICALLBACK Get_Trig_axis (int panel, int control, int event, void *callbackData, int eventData1, int eventData2) { switch (event) { case EVENT_COMMIT: GetCtrlVal (panelHandle_PM, PRM_PANEL_N_TR_AXIS, &trig_axis); break; } return 0; } int CVICALLBACK Get_Vel (int panel, int control, int event, void *callbackData, int eventData1, int eventData2) { switch (event) { case EVENT_COMMIT: GetCtrlVal (panelHandle_PM, PRM_PANEL_N_SC_V, &Sc_V); break; case EVENT_RIGHT_CLICK: break; } return 0; } int CVICALLBACK Get_x0 (int panel, int control, int event, void *callbackData, int eventData1, int eventData2) { switch (event) { case EVENT_COMMIT: GetCtrlVal (panelHandle_PM, PRM_PANEL_N_HP_X0, &x_scan); break; } return 0; } int CVICALLBACK Get_y0 (int panel, int control, int event, void *callbackData, int eventData1, int eventData2) { switch (event) { case EVENT_COMMIT: GetCtrlVal (panelHandle_PM, PRM_PANEL_N_HP_Y0, &y_scan); break; } return 0; } int CVICALLBACK Get_Strt (int panel, int control, int event, void *callbackData, int eventData1, int eventData2) { switch (event) { case EVENT_COMMIT: GetCtrlVal (panelHandle_PM, PRM_PANEL_N_SC_STRT, &Sc_strt); break; case EVENT_RIGHT_CLICK: break; } return 0; } int CVICALLBACK Get_Stp (int panel, int control, int event, void *callbackData, int eventData1, int eventData2) { switch (event) { case EVENT_COMMIT: GetCtrlVal (panelHandle_PM, PRM_PANEL_N_SC_STP, &Sc_stp); break; case EVENT_RIGHT_CLICK: break; } return 0; } int CVICALLBACK Get_Trig (int panel, int control, int event, void *callbackData, int eventData1, int eventData2) { switch (event) { case EVENT_COMMIT: GetCtrlVal (panelHandle_PM, PRM_PANEL_N_TR_SPC, &trig_spc); break; case EVENT_RIGHT_CLICK: break; } return 0; } int CVICALLBACK Get_File (int panel, int control, int event, void *callbackData, int eventData1, int eventData2) { switch (event) { case EVENT_COMMIT: GetCtrlVal (panelHandle_PM, PRM_PANEL_ST_HP_FL, HP_cal_file); break; case EVENT_RIGHT_CLICK: FileSelectPopup ("w:\\LCLS-II\\data\\HP calibration", "*.*", "", "", VAL_LOAD_BUTTON, 0, 0, 1, 0, HP_cal_file); SetCtrlVal (panelHandle_PM, PRM_PANEL_ST_HP_FL, HP_cal_file); break; } return 0; } int CVICALLBACK Get_Name (int panel, int control, int event, void *callbackData, int eventData1, int eventData2) { switch (event) { case EVENT_COMMIT: GetCtrlVal (panelHandle_PM, PRM_PANEL_ST_HP_NM, HP_ID); break; } return 0; } int CVICALLBACK SaveQuit (int panel, int control, int event, void *callbackData, int eventData1, int eventData2) { double dz1, dt; switch (event) { case EVENT_COMMIT: // Check compatibility between velocity, sample spacing and integration time dt = int_tm/LINECYCLE; dz1 = Sc_V*dt; // Allow maximum sampling interval = 0.5*(trigger spacing) if (dz1 > trig_spc/2.0) // Don't save and quit until it is OK MessagePopup ("Parameter Check", "Incompatibility:scan velocity, integration time, and sample spacing."); else { // Save and quit num_pnts = (Sc_stp - Sc_strt)/trig_spc; HidePanel (panelHandle_PM); RdCalFile (HP_cal_file); // Read HP calibration time SetCtrlVal (panelHandle_IN, IN_PANEL_TEXTBOX_STATUS, "READY FOR SCAN\n"); DisplayPanel (panelHandle_SC); } break; case EVENT_RIGHT_CLICK: break; } return 0; } int CVICALLBACK Get_z1 (int panel, int control, int event, void *callbackData, int eventData1, int eventData2) { switch (event) { case EVENT_COMMIT: GetCtrlVal (panelHandle_PM, PRM_PANEL_N_HP_Z1, &HP_z[0]); break; case EVENT_RIGHT_CLICK: break; } return 0; } int CVICALLBACK Get_z2 (int panel, int control, int event, void *callbackData, int eventData1, int eventData2) { switch (event) { case EVENT_COMMIT: GetCtrlVal (panelHandle_PM, PRM_PANEL_N_HP_Z2, &HP_z[1]); break; case EVENT_RIGHT_CLICK: break; } return 0; } int CVICALLBACK Load_PRM_File (int panel, int control, int event, void *callbackData, int eventData1, int eventData2) { char param_file[FILENAME] = PRMFILE; switch (event) { case EVENT_COMMIT: RdParamFile (param_file); SetCtrlVal (panelHandle_PM, PRM_PANEL_ST_ID, und_ID); SetCtrlVal (panelHandle_PM, PRM_PANEL_ST_ID_TYP, und_type); SetCtrlVal (panelHandle_PM, PRM_PANEL_ST_DATA_DIR, und_dir); SetCtrlVal (panelHandle_PM, PRM_PANEL_GPIB_1, GPIBDVM[0]); SetCtrlVal (panelHandle_PM, PRM_PANEL_GPIB_2, GPIBDVM[1]); SetCtrlVal (panelHandle_PM, PRM_PANEL_N_VS_DVM1, DCV[0]); SetCtrlVal (panelHandle_PM, PRM_PANEL_N_VS_DVM2, DCV[1]); SetCtrlVal (panelHandle_PM, PRM_PANEL_N_INT_TM, int_tm); SetCtrlVal (panelHandle_PM, PRM_PANEL_N_TR_AXIS, trig_axis); SetCtrlVal (panelHandle_PM, PRM_PANEL_N_SC_V, Sc_V); SetCtrlVal (panelHandle_PM, PRM_PANEL_N_SC_STRT, Sc_strt); SetCtrlVal (panelHandle_PM, PRM_PANEL_N_SC_STP, Sc_stp); SetCtrlVal (panelHandle_PM, PRM_PANEL_N_HP_Z1, HP_z[0]); SetCtrlVal (panelHandle_PM, PRM_PANEL_N_HP_Z2, HP_z[1]); SetCtrlVal (panelHandle_PM, PRM_PANEL_N_TR_SPC, trig_spc); SetCtrlVal (panelHandle_PM, PRM_PANEL_ST_HP_NM, HP_ID); SetCtrlVal (panelHandle_PM, PRM_PANEL_ST_HP_FL, HP_cal_file); break; } return 0; } int CVICALLBACK Change_X0 (int panel, int control, int event, void *callbackData, int eventData1, int eventData2) { double dx; switch (event) { case EVENT_COMMIT: GetCtrlVal (panelHandle_SC, SCAN_PANEL_NUMERIC_X0, &x_scan); GetCtrlVal (panelHandle_SC, SCAN_PANEL_NUMERIC_DX, &dx); x_scan += dx; SetCtrlVal (panelHandle_SC, SCAN_PANEL_NUMERIC_X0, x_scan); SetCtrlVal (panelHandle_SC, SCAN_PANEL_NUMERIC_DX, 0.0); break; } return 0; } int CVICALLBACK Change_Y0 (int panel, int control, int event, void *callbackData, int eventData1, int eventData2) { double dy; switch (event) { case EVENT_COMMIT: GetCtrlVal (panelHandle_SC, SCAN_PANEL_NUMERIC_Y0, &y_scan); GetCtrlVal (panelHandle_SC, SCAN_PANEL_NUMERIC_DY, &dy); y_scan += dy; SetCtrlVal (panelHandle_SC, SCAN_PANEL_NUMERIC_Y0, y_scan); SetCtrlVal (panelHandle_SC, SCAN_PANEL_NUMERIC_DY, 0.0); break; } return 0; }