#include #include #include #include "delay.h" #include #include "toolbox.h" #include "xps_c8.h" #include "xps_c8param.h" #include "XPS_Q8_drivers.h" #include "FDI-com.h" #include "LongCoilOp.h" #include "LongCoilUir.h" #include "LongCoilUi.h" #define INTDIR "W:\\LCLS-II\\data\\Int\\tests" //This the LonCoil main program // Global variables static int panelHandle_INT; static int xps_c8_ID = -1; //xps-handle static char FDIaddr[]="TCPIP0::FDI2056-0011.dhcp.lbl.gov::inst0::INSTR"; static XPS_PARAM nXPC_param; static XPS_2DGROUP nXPS2Dparam [XPS_NUM_OF_2D_GROUPS]; int nmeas=1; float t0[10000]; float WBdat[10000]; float t0A[100][10000]; float t0B[100][10000]; float WBdatA[100][10000]; float WBdatB[100][10000]; float WBdat2[100][10000]; float RecCum[50][100]; // to record the intermediary cumulated values float Fluxdat[100]; float RecX[100]; double xmin = -0.001; double xmax = +0.001; double dx = 0.001; double dy = 0.001; int Nx = 1; int Nrep = 1; //static int panelHandle_XPS; //static int menuHandle; int main (int argc, char *argv[]) { ViRsrc FDIname; //Connect to FDI FDIname=(ViRsrc)FDIaddr; viOpenDefaultRM(&rm); vi=FDIOpenSession(FDIname,rm); // Fill param structure here to avoid compiling all big UI files upon changing any parameter xps_c8_fill_param_struct(&nXPC_param,nXPS2Dparam); //and send them to global variable at driver xps_c8_set_param( nXPC_param, nXPS2Dparam); xps_c8_init(&xps_c8_ID); Delay(1); FDI_Init(vi); Delay(1); panelHandle_INT = LoadPanel (0, "LongCoilUir.uir", INT_PANEL); // Main panel // panelHandle_XPS = LoadPanel (0, "xps_c8uir.uir", XPS_PANEL); // XPS panel //menuHandle= LoadMenuBar (INT_PANEL, "LongCoilUir.uir", MENUBAR); //Loads menu bar DisplayPanel (panelHandle_INT); RunUserInterface (); return 0; } //MENUBAR /*void CVICALLBACK XPS_CB(int menubar, int menuItem, void *callbackData, int panel) { switch (menuItem) { case MENUBAR_XPS: DisplayPanel (panelHandle_XPS); break; } } */ int CVICALLBACK CB_XPS_MOVE_X (int panel, int control, int event, void *callbackData, int eventData1, int eventData2) { switch (event) { int ret; double position=0.01; double positions[2]={0,0}; case EVENT_COMMIT: GetCtrlAttribute (panel, INT_PANEL_POS_X, ATTR_CTRL_VAL, &position); xps_c8_set_group_mode(0,0); ret=xps_c8_move_abs(xps_c8_ID, 0, position , positions); break; } return 0; } int CVICALLBACK CB_XPS_MOVE_Y (int panel, int control, int event, void *callbackData, int eventData1, int eventData2) { switch (event) { int ret; double position=0.01; double positions[2]={0,0}; case EVENT_COMMIT: GetCtrlAttribute (panel, INT_PANEL_POS_Y, ATTR_CTRL_VAL, &position); xps_c8_set_group_mode(1,0); ret=xps_c8_move_abs(xps_c8_ID, 1, position , positions); break; } return 0; } int CVICALLBACK CB_FDI_DEND (int panel, int control, int event, void *callbackData, int eventData1, int eventData2) { switch (event) { int retcnt,ret; char comc[550]; char comc2[50]; ViChar l_buffer[300]; case EVENT_COMMIT: GetCtrlVal (panel, INT_PANEL_STRING_FDI_CMD, comc); sprintf(comc2,"%s",comc); retcnt=FDIWrite(vi, comc2); //viClear(ViSession vi) break; } return 0; } int CVICALLBACK CB_FDI_READ (int panel, int control, int event, void *callbackData, int eventData1, int eventData2) { ViChar l_buffer[1024]; switch (event) { case EVENT_COMMIT: FDIRead(vi, (ViPBuf)l_buffer); SetCtrlVal(panel, INT_PANEL_STRING_FDI_READ , l_buffer); break; } return 0; } int CVICALLBACK CB_FDI_ACQ (int panel, int control, int event, void *callbackData, int eventData1, int eventData2) { int retcnt; ViChar l_buffer[100000]; char sdat[10000][55]; float dat[10000]; float dat3; float dat2[10000]; double freq; int ind[10000] ; FILE *file_handle; char * pch; //char fund="W:\\LCLS-II\\data\\Int\\tests"; char comfreq[50],comnmeas[50],comfetch[50]; int i=0; int numpt=100; int ret; double position=0.01; double positions[2]={0,0}; switch (event) { case EVENT_COMMIT: GetCtrlVal (panel, INT_PANEL_FREQ, &freq); GetCtrlVal (panel, INT_PANEL_NMEAS, &nmeas); sprintf (comfreq,":TRIG:TIM %fKHZ",freq); sprintf (comnmeas,":TRIG:COUN %d",nmeas); //retcnt=FDIWrite(vi, ":SENS:FUNC FLUX"); //retcnt=FDIWrite(vi, ":TRIG:COUN 100"); retcnt=FDIWrite(vi, comnmeas); retcnt=FDIWrite(vi, ":TRIG:SOUR TIM"); retcnt=FDIWrite(vi,":INP:COUP DC"); //retcnt=FDIWrite(vi,":INP:COUP DC"); //retcnt=FDIWrite(vi, ":TRIG:TIM 2KHZ"); retcnt=FDIWrite(vi, comfreq); retcnt=FDIWrite(vi, ":ARM:SOUR TIM"); //retcnt=FDIWrite(vi, ":SENS:CORR:SLOP"); //Delay(20); GetCtrlAttribute (panel, INT_PANEL_POS_X, ATTR_CTRL_VAL, &position); retcnt=FDIWrite(vi, ":INIT"); Delay(1.5); xps_c8_set_group_mode(0,0); ret=xps_c8_move_abs(xps_c8_ID, 0, position , positions); Delay(10); sprintf (comfetch,":FETCH:ARRAY? %d",nmeas); retcnt=FDIWrite(vi, comfetch); //retcnt=FDIWrite(vi, ":FETCH:ARRAY? 100"); FDIRead(vi, (ViPBuf)l_buffer); Delay(1); file_handle = fopen ("TempFDI.dat", "w"); //write to temporary file fprintf (file_handle, "%s", l_buffer); assert(fclose (file_handle) != EOF); // printf ("Splitting string \"%s\" into tokens:\n",str); pch = strtok (l_buffer,","); while (pch != NULL) {ind[i]=i; printf ("%s\n",pch); sprintf (sdat[i],"%s\n",pch); sscanf (sdat[i],"%f S; %f V",&t0[i],&WBdat[i]); sscanf (pch,"%f V",&dat2[i]); pch = strtok (NULL, ","); i++; } DeleteGraphPlot (panelHandle_INT,INT_PANEL_GRAPH_V, -1, VAL_IMMEDIATE_DRAW); PlotXY (panelHandle_INT, INT_PANEL_GRAPH_V, ind, WBdat, nmeas, VAL_INTEGER, VAL_FLOAT, VAL_THIN_LINE, VAL_EMPTY_SQUARE, VAL_SOLID, 1, VAL_RED); retcnt=FDI_WriteDataFile (); //cvi_delay(1); break; } return 0; } int CVICALLBACK FDI_Gain_CB (int panel, int control, int event, void *callbackData, int eventData1, int eventData2) { int gain; char comgain[50]; int retcnt; switch (event) { case EVENT_COMMIT: GetCtrlVal (panel, INT_PANEL_GAIN, &gain); sprintf (comgain,":INP:GAIN %d",gain); retcnt=FDIWrite(vi, comgain); //retcnt=FDI_Init(vi); retcnt=FDI_Corr(vi); break; } return 0; } int FDI_WriteDataFile () { int i; FILE *fpo; char und_dir[]=INTDIR ; char und_file[400]; FileSelectPopup (und_dir, "test1.dat", "", "Data File", VAL_OK_BUTTON, 0, 0, 1, 0,und_file); fpo = fopen (und_file, "w"); // Write header information fprintf (fpo, "LBNL Magnetic Measurements\n"); fprintf (fpo, "Date: %s\n", DateStr ()); fprintf (fpo, "Time: %s\n", TimeStr ()); fprintf (fpo, "\n"); fprintf (fpo, "Project: LCLS-II\n"); //fprintf (fpo, "Undulator Type: %s", und_type); // \n contained in string //fprintf (fpo, "Data Type:\n"); //fprintf (fpo, "Undulator ID: %s", und_ID); // \n contained in string //fprintf (fpo, "Data Set Number: %u\n", data_set); /*switch (tune_stage) { case 0 : fprintf (fpo, "Tuning Stage: Initial Data\n"); break; case 1 : fprintf (fpo, "Tuning Stage: Periodic Section\n"); break; case 2: fprintf (fpo, "Tuning Stage: End Section\n"); break; case 3: fprintf (fpo, "Tuning Stage: Verification\n"); break; default : fprintf (fpo, "Tuning Stage: Illegal Value\n"); break; } */ //fprintf (fpo, "Run Number: %u\n", run_num); fprintf (fpo, "Measurement Type: Long Coil Scan\n"); //fprintf (fpo, "Operator: %s\n", operator); // fprintf (fpo, "Comment: See Log File\n"); // fprintf (fpo, "Session first run:\n"); // fprintf (fpo, "Hall Probe: %s\n", HP_ID); // fprintf (fpo, "Calibration File: %s\n", HP_cal_file ); // fprintf (fpo, "\n"); /* fprintf (fpo, "Data File: %s\n", und_file); fprintf (fpo, "\n"); fprintf (fpo, "Temperature: \n"); fprintf (fpo, "\n\n"); fprintf (fpo, "Zero-Gauss Chamber Measurement at z = %f [mm]\n", HP_z[0]); fprintf (fpo, "Voltage Values:\n"); fprintf (fpo, "Vx = %f V\n", Vx_o1); fprintf (fpo, "Vy = %f V\n", Vy_o1); fprintf (fpo, "\n"); fprintf (fpo, "Zero-Gauss Chamber Measurement at z = %f [mm]\n", HP_z[1]); fprintf (fpo, "Voltage Values:\n"); fprintf (fpo, "Vx = %f V\n", Vx_o2); fprintf (fpo, "Vy = %f V\n", Vy_o2); fprintf (fpo, "\n"); fprintf (fpo, "Offset Correction = offset + index*increment\n"); fprintf (fpo, "index = 0, 1, 2 ....\n"); fprintf (fpo, "Vx: offset = %f V, increment = %e V\n", Vx_offset, Vx_increment); fprintf (fpo, "Vy: offset = %f V, increment = %e V\n", Vy_offset, Vy_increment); fprintf (fpo, "\n"); fprintf (fpo, "Reference Magnet Measurements:\n"); fprintf (fpo, "Bx = [T]\n"); fprintf (fpo, "By = [T]\n"); */ fprintf (fpo, "\n"); // Write data fprintf (fpo, "Magnetic Field Measurements\n"); fprintf (fpo, " t Flux Flux1 Flux2 Flux3 Flux4 Flux5 Flux6 Flux7 Flux8 Flux9 Flux10\n"); fprintf (fpo, " [s] [V.s] [V.s] [V.s] [V.s] [V.s] [V.s] [V.s] [V.s] [V.s] [V.s]\n"); for (i = 0; i < Nx; i++) fprintf (fpo, "%9.2le %9.6le %9.6le %9.6le %9.6le %9.6le %9.6le %9.6le %9.6le %9.6le %9.6le %9.6le\n", RecX[i], Fluxdat[i],RecCum[i][0],RecCum[i][1],RecCum[i][2],RecCum[i][3],RecCum[i][4],RecCum[i][5],RecCum[i][6],RecCum[i][7],RecCum[i][8],RecCum[i][9]); fclose (fpo); return 0; } int CVICALLBACK CB_FDI_SCAN (int panel, int control, int event, void *callbackData, int eventData1, int eventData2) { char comfreq[50],comnmeas[50],comfetch[50],comcum[50]; double freq; double dxstage=0; int retcnt; int ret; double position=0.01; //xps variables double positions[2]={0,0}; //xps variable double Cum[100]; int i; //Nx loop variable int j=0; //NRep loop variable int k=0; int m =0; char * pchA; char * pchB; char sdatA[6000][55]; char sdatB[6000][55]; int ind[6000] ; int condcum=1; int IxyMode=-1; double PosY0; ViChar l_bufferA[25000]; //FDI data buffer ViChar l_bufferB[25000]; switch (event) { case EVENT_COMMIT: GetCtrlVal (panel, INT_PANEL_FREQ, &freq); GetCtrlVal (panel, INT_PANEL_NMEAS, &nmeas); GetCtrlVal (panel, INT_PANEL_CONDCUM , &condcum); sprintf (comfreq,":TRIG:TIM %fHZ",freq); sprintf (comnmeas,":TRIG:COUN %d",nmeas); retcnt=FDIWrite(vi, comnmeas); retcnt=FDIWrite(vi, ":TRIG:SOUR TIM"); retcnt=FDIWrite(vi,":INP:COUP DC"); sprintf (comcum,":CALC:FLUX %d",condcum); //retcnt=FDIWrite(vi,":CALC:FLUX 0"); //Do not CUMULATE FLUX values retcnt=FDIWrite(vi, comcum); retcnt=FDIWrite(vi, comfreq); retcnt=FDIWrite(vi, ":ARM:SOUR TIM"); GetCtrlVal (panel, INT_PANEL_X_MIN, &xmin); GetCtrlVal (panel, INT_PANEL_X_MAX, &xmax); GetCtrlVal (panel, INT_PANEL_DX, &dx); GetCtrlVal (panel, INT_PANEL_NBX, &Nx); GetCtrlVal (panel, INT_PANEL_NBREP, &Nrep); GetCtrlVal (panel, INT_PANEL_BIN_SWITCH, &IxyMode); GetCtrlVal (panel, INT_PANEL_POS_Y, &PosY0); if(Nx!=1) { dxstage = (xmax-xmin)/(Nx-1); } else { dxstage = (xmax-xmin)/(Nx); } //position=-0.0365;//go to Y midpane position //xps_c8_set_group_mode(1,0); //ret=xps_c8_move_abs(xps_c8_ID, 1, position , positions); if(IxyMode == 1) { for (i = 0; i < Nx; i++) { Fluxdat[i]=0; for (j = 0; j < Nrep; j++) { RecX[i]=xmin + i*dxstage; //record X meas position //steps a through b are the basic steps to be repeated for averaging purpose position = xmin + i*dxstage - 2*dx; //pos i0-a initial position farther than xmin-dx/2 to prevent backlash xps_c8_set_group_mode(0,0); ret=xps_c8_move_abs(xps_c8_ID, 0, position , positions); //position=-0.0365;//go to Y midpane position //xps_c8_set_group_mode(1,0); //ret=xps_c8_move_abs(xps_c8_ID, 1, position , positions); position = xmin + i*dxstage - dx/2; //pos i0-b xps_c8_set_group_mode(0,0); ret=xps_c8_move_abs(xps_c8_ID, 0, position , positions); // init FDI retcnt=FDIWrite(vi, ":INIT"); Delay(0.5); // then move to xmin + dx/2 position = xmin + i*dxstage + dx/2; //pos i0-c xps_c8_set_group_mode(0,0); ret=xps_c8_move_abs(xps_c8_ID, 0, position , positions); // Fetch data Delay(3); sprintf (comfetch,":FETCH:ARRAY? %d",nmeas); retcnt=FDIWrite(vi, comfetch); FDIRead(vi, (ViPBuf)l_bufferA); Delay(1); // then move to xmin + dx and back(backlash) position = xmin + i*dxstage + 2*dx; //pos i0-d xps_c8_set_group_mode(0,0); ret=xps_c8_move_abs(xps_c8_ID, 0, position , positions); position = xmin + i*dxstage + dx/2; //pos i0-e xps_c8_set_group_mode(0,0); ret=xps_c8_move_abs(xps_c8_ID, 0, position , positions); //init FDI retcnt=FDIWrite(vi, ":INIT"); Delay(0.5); // then move to xmin - dx/2 position = xmin + i*dxstage - dx/2; //pos i0-f xps_c8_set_group_mode(0,0); ret=xps_c8_move_abs(xps_c8_ID, 0, position , positions); // Fetch data Delay(2); sprintf (comfetch,":FETCH:ARRAY? %d",nmeas); retcnt=FDIWrite(vi, comfetch); FDIRead(vi, (ViPBuf)l_bufferB); Delay(1); //store data pchA = strtok (l_bufferA,","); k=0; while (pchA != NULL) {ind[k]=k; //printf ("%s\n",pchA); //printf ("%s\n",pchB); sprintf (sdatA[k],"%s\n",pchA); //sprintf (sdatB[k],"%s\n",pchB); sscanf (sdatA[k],"%f S; %f WB",&t0A[i][k],&WBdatA[i][k]); //sscanf (sdatB[k],"%f S; %f WB",&t0B[i][k],&WBdatB[i][k]); //sscanf (pch,"%f V",&dat2[i]); pchA = strtok (NULL, ","); //pchB = strtok (NULL, ","); k++; } pchB = strtok (l_bufferB,","); k=0; while (pchB != NULL) {ind[k]=k; //printf ("%s\n",pchA); //printf ("%s\n",pchB); //sprintf (sdatA[k],"%s\n",pchA); sprintf (sdatB[k],"%s\n",pchB); //sscanf (sdatA[k],"%f S; %f WB",&t0A[i][k],&WBdatA[i][k]); sscanf (sdatB[k],"%f S; %f WB",&t0B[i][k],&WBdatB[i][k]); //sscanf (pch,"%f V",&dat2[i]); //pchA = strtok (NULL, ","); pchB = strtok (NULL, ","); k++; } Cum[j]=0; if(condcum==0) { for(m=0; m