/******************************************************************* * Sub_IDdata.c * Loaded at boot time reads the data files and keeps in memory. * Does all computaion based on magnetic measurement data read from file. * Called as subroutine records from databases. * Determine energy <=> gap conversions. * Determine the serial number of the device. * Has capacity to load upto two device information. * * 04-24-08 mls Modified for EPICS 3.14 * * 12-05-11 mls Added capability for reading more data from magnetic * measurement file such as device length and total number * of periods. Also added power calculation which was significantly * changed and moved to a subroutine record. The old method relied * on a manual curve fit and power constants obtained from this fit. * The new method has the power calculation embedded into the subroutine * called from EPICS database processing and uses actual SR beam current. * * 01-15-16 MR modified to make sure that the energy conversion does not * fallout of range due to ID creeep. So replace the end point of * calculation to hard stop value from minimum gap. * Also saved the old value of power in f field of subroutine record * * *******************************************************************/ #include #include //#include //#include //#include #include #include //#include #include #include #include #include #include #include #include #include #include #include #include "registryFunction.h" #include "epicsExport.h" #include "xleap.h" #define BUF_SIZE 100 #define DATA_SIZE 20 #define MAX_NUM_ID 4 #define MAX_GAP 210.0 #define K_CNV 0.93372875 /* convertion factor [(Tesla^-1) * (cm^-1)] */ #define C1 0.94963408 /* convertion factor [keV*cm*GeV^-2] */ #define EC_CNV 0.665 /* conversion factor [keV*GeV^-2*Tesla^-1] */ #define BEAM_ENERGY 7.0 /* Electron Beam Energy */ /* Added for power calculations */ #define C 2.99792458 /* Speed of Light [m/s] */ #define MEE 0.51099906 /* Electron rest mass [kg] */ #define EC 1.60217733 /* Elementary charge [C] */ #define EPSZ 8.8541878 /* Permittivity of vacuum [Fm-1] */ #define PI 3.14159 #define C_CM_M 0.01 /* Conversion factor for cm to meters */ #define C_MA_A 0.001 /* Conversion factor for milliamps to amps */ #define C_G_T 10000.0 /* Conversion factor for Gauss to Tesla */ #define PTOT_FAC (PI/3.0 * EC/EPSZ/(MEE*MEE)*0.1) #define PD_FAC (21.0/(16.0*PI*(MEE*MEE))*PTOT_FAC) int idNum; /* Which ID 1 = Upstream, 0 = Downstream */ struct idData { char dataFile[BUF_SIZE]; char name[BUF_SIZE]; float periodLength; float deviceLength; int totNumPeriods; float minimumGap; float hardStops; int numData; float gap[DATA_SIZE]; float beff[DATA_SIZE],logBeff[DATA_SIZE]; float bpeak[DATA_SIZE],logBpeak[DATA_SIZE]; float m, c, mpk, cpk; float minimumField, minimumField_pk; float maximumField, maximumField_pk; int nharm; int harm; float corr[3][15]; }; struct subDNamePvt { struct stringinRecord *psi; char device[40]; }; struct idData idInfo[MAX_NUM_ID]; /*int *pdata; &pdata = malloc( (sizeof(struct idData))*MAX_NUM_ID );*/ int convertDebug = 0; epicsExportAddress(int, convertDebug); int deviceDebug = 0; epicsExportAddress(int, deviceDebug); /******************************************************************* * read the Device Parameters and the Magnetic data... *******************************************************************/ int readData(char *filename,int num) { FILE *fd; int readOK, i,size; float gap, beff, bpeak, sum_x, sum_xx, sum_xy, sum_y, sum_ypk, sum_xypk, det, det_m, det_c; float det_mpk, det_cpk; char strBuffer[BUF_SIZE],strBuffer1[BUF_SIZE]; if ( !(fd=fopen(filename,"r")) ) { epicsPrintf("Can't open \"%s\" file \n",filename); return(ERROR); } idNum = num; /*&idInfo[idNum] = pdata + (sizeof(struct idData))*idNum;*/ epicsPrintf("Reading \"%s\" file \n",filename); strcpy(idInfo[idNum].dataFile,filename); size = sizeof(strBuffer); fgets(strBuffer,size,fd); sscanf(strBuffer,"%s %s",strBuffer1,idInfo[idNum].name); epicsPrintf("Device_Name: %s\n",idInfo[idNum].name); fgets(strBuffer,size,fd); sscanf(strBuffer,"%s %f",strBuffer1,&(idInfo[idNum].deviceLength)); epicsPrintf("Device_Length: %f m\n",idInfo[idNum].deviceLength); fgets(strBuffer,size,fd); sscanf(strBuffer,"%s %f",strBuffer1,&(idInfo[idNum].periodLength)); epicsPrintf("Period_Length: %f cm\n",idInfo[idNum].periodLength); fgets(strBuffer,size,fd); sscanf(strBuffer,"%s %d",strBuffer1,&(idInfo[idNum].totNumPeriods)); epicsPrintf("Number Periods: %d \n",idInfo[idNum].totNumPeriods); fgets(strBuffer,size,fd); sscanf(strBuffer,"%s %f",strBuffer1,&(idInfo[idNum].minimumGap)); /* reduce minimum gap by half a micron idInfo[idNum].minimumGap -= 0.0005; */ epicsPrintf("Minimum_Gap: %f mm\n",idInfo[idNum].minimumGap); fgets(strBuffer,size,fd); sscanf(strBuffer,"%s %f",strBuffer1,&(idInfo[idNum].hardStops)); epicsPrintf("Hard_Stops: %f mm\n",idInfo[idNum].hardStops); fgets(strBuffer,sizeof(strBuffer),fd); fgets(strBuffer,sizeof(strBuffer),fd); epicsPrintf ("%s", strBuffer); fgets(strBuffer,sizeof(strBuffer),fd); epicsPrintf ("%s", strBuffer); readOK=3; idInfo[idNum].numData=0; /* Perform least square fit for magnetic fields */ sum_x = sum_xx = sum_xy = sum_y = sum_ypk = sum_xypk = 0.0; /* sum_x is the incremental cummulative sum of all gaps sum_xx is the incremental cummulative sum of the square of the gap sum_xy is the incremental sum of the gap * log(BEff) field sum_y is the cummulative sum of the log(BEff) fields */ for ( i=0; ((readOK==3)&&(idInfo[idNum].numData0) ? log(beff) : 0.0; idInfo[idNum].logBpeak[i]=(bpeak>0) ? log(bpeak) : 0.0; epicsPrintf("%2d%7.3f%13.3f%13.3f\n",i,gap,beff,bpeak); sum_x = sum_x + gap; sum_xx = sum_xx + gap*gap; sum_xy = sum_xy + gap*idInfo[idNum].logBeff[i]; sum_y = sum_y + idInfo[idNum].logBeff[i]; sum_xypk = sum_xypk + gap*idInfo[idNum].logBpeak[i]; sum_ypk = sum_ypk + idInfo[idNum].logBpeak[i]; } } if (idInfo[idNum].numData > 0) { det = sum_xx*idInfo[idNum].numData - sum_x*sum_x; det_m = sum_xy*idInfo[idNum].numData - sum_x*sum_y; det_c = sum_xx*sum_y - sum_xy*sum_x; idInfo[idNum].m = (det != 0) ? det_m/det : 1.0; /* Used for peak field */ det_mpk = sum_xypk * idInfo[idNum].numData - sum_x*sum_ypk; det_cpk = sum_xx * sum_ypk - sum_xypk * sum_x; idInfo[idNum].mpk = (det != 0) ? det_mpk/det : 1.0; /* * the last data point is used to calculate offset instead of the least * square fit, this avoids discontinuity from the function of field vs. gap */ idInfo[idNum].c = idInfo[idNum].logBeff[idInfo[idNum].numData-1] - idInfo[idNum].m * gap; idInfo[idNum].minimumField = exp(idInfo[idNum].m * MAX_GAP + idInfo[idNum].c); /* replace the value of mimimum gap to hard stop gap. idInfo[idNum].maximumField = exp( idInfo[idNum].m * (idInfo[idNum].minimumGap - idInfo[idNum].gap[0]) + idInfo[idNum].logBeff[0] ); */ idInfo[idNum].maximumField = exp( idInfo[idNum].m * (idInfo[idNum].hardStops - idInfo[idNum].gap[0]) + idInfo[idNum].logBeff[0] ); if(convertDebug == 1) epicsPrintf("readData: maximumField = %f for device %d\n", idInfo[idNum].maximumField,idNum); /* Used for peak field */ idInfo[idNum].cpk = idInfo[idNum].logBpeak[idInfo[idNum].numData-1] - idInfo[idNum].mpk * gap; idInfo[idNum].minimumField_pk = exp(idInfo[idNum].mpk * MAX_GAP + idInfo[idNum].cpk); /* replace the value of mimimum gap to hard stop gap. idInfo[idNum].maximumField_pk = exp( idInfo[idNum].mpk * (idInfo[idNum].minimumGap - idInfo[idNum].gap[0]) + idInfo[idNum].logBpeak[0] ); */ idInfo[idNum].maximumField_pk = exp( idInfo[idNum].mpk * (idInfo[idNum].hardStops - idInfo[idNum].gap[0]) + idInfo[idNum].logBpeak[0] ); if(convertDebug == 1) epicsPrintf("readData: maximumField_pk = %f for device %d\n", idInfo[idNum].maximumField_pk,idNum); epicsPrintf("log(Beff) = %7.3f * Gap + %7.3f\n",idInfo[idNum].m,idInfo[idNum].c); epicsPrintf("log(Bpeak) = %7.3f * Gap + %7.3f\n",idInfo[idNum].mpk,idInfo[idNum].cpk); } return(fclose(fd)); } /******************************************************************* * read the coefficients for correction of the photon energy values * at different undulator harmonics *******************************************************************/ int readDataCorr(char *filename,int id) { FILE *fd; int i; char buf[BUF_SIZE], eol[BUF_SIZE]; if ( !(fd=fopen(filename,"r")) ) { epicsPrintf("Can't open the \"%s\" file \n",filename); return(ERROR); } epicsPrintf("Reading the \"%s\" file \n",filename); do { if ( fscanf(fd,"%[^\n]%[\n]", buf, eol) == -1 ) { epicsPrintf("*** Wrong file structure ***"); return(ERROR); } if ( !strncmp(buf, " Nharm :",5) ) { sscanf((char*)&buf[8],"%d",(int*)&idInfo[id].nharm); epicsPrintf("Nharm = %d \n", idInfo[id].nharm); } } while ( strncmp(buf, "Fitted",5) ); for ( i = 0; i= 5) break; } while ( b != a ); return(b); } /******************************************************************* * linear interpolation *******************************************************************/ static float interpolate(float xi, float xf, float yi, float yf, float x) { return( (xf != xi) ? yi+(x-xi)*(yf-yi)/(xf-xi) : (yf + yi)/2.0 ); } /******************************************************************* * Gap to Field Conversion *******************************************************************/ static int gapToB(float *bEff,float size, int id) { float lower=0, upper=0, lowerBound=0, upperBound=0; float logBfield; int i, done; /* Use the Hard Stops value if (size < idInfo[id].minimumGap) { if (convertDebug == 1) { epicsPrintf("gapToB: Gap size is too small!!!\nGap = %7.3f\nMin gap = %7.3f\n",size,idInfo[id].minimumGap); } return(ERROR); } */ if (size < idInfo[id].hardStops) { if (convertDebug == 1) { epicsPrintf("gapToB: Gap size is too small!!!\nGap = %7.3f\nMin gap = %7.3f\n",size,idInfo[id].minimumGap); } return(ERROR); } done = 0; for(i=1; (!done) && (i= idInfo[id].minimumGap) && (size <= MAX_GAP) ) { done = 2; logBfield = idInfo[id].m * size + idInfo[id].c; *bEff = exp(logBfield); } if (!done) { if (convertDebug == 1) epicsPrintf("gapToB: Gap size is too large!!!\n"); return(1); } if (done == 1) { logBfield = interpolate(lower,upper,lowerBound,upperBound,size); *bEff = exp(logBfield); } if (convertDebug == 1) epicsPrintf("gapToB: ID = %s\nGap Size = %7.3f B field = %7.3f\n", idInfo[id].name,size,*bEff); return(OK); } /******************************************************************* * Field to Gap Conversion *******************************************************************/ static int bToGap(float bEff,float *size, int id) { float lower=0, upper=0, lowerBound=0, upperBound=0; float logBfield; int i, done; if (bEff > idInfo[id].maximumField) { if (convertDebug == 1) epicsPrintf("bToGap: B field: %f is > maximum = %f\n", bEff, idInfo[id].maximumField); return(ERROR); } done = 0; for ( i=1; (!done) && (i= idInfo[id].beff[i]) { done = 1; lower = idInfo[id].logBeff[i-1]; upper = idInfo[id].logBeff[i]; lowerBound = idInfo[id].gap[i-1]; upperBound = idInfo[id].gap[i]; } } if ( (!done) && (bEff >= idInfo[id].minimumField) ) { done = 2; logBfield = (bEff > 0) ? log(bEff) : 0.0; if(idInfo[id].m != 0) { *size = (logBfield - idInfo[id].c)/idInfo[id].m; } else { done = 0; } } if (!done) { if (convertDebug == 1) epicsPrintf("bToGap: B field is too small!!!\n"); return(1); } if (done == 1) { logBfield = log(bEff); *size = interpolate(lower,upper,lowerBound,upperBound,logBfield); } if (convertDebug == 1) epicsPrintf("bToGap: ID = %s\nPeriod = %3.3f\nid = %d\n",idInfo[id].name,idInfo[id].periodLength,id); if (convertDebug == 1) epicsPrintf("bToGap: Gap Size = %7.3f B field = %7.3f\n", *size,bEff); return(OK); } /******************************************************************* * Field to Energy Conversion *******************************************************************/ static int bToEnergy(float bEff,float *energy, int id) { float KEff; KEff = K_CNV*bEff*idInfo[id].periodLength; *energy = C1*(idInfo[id].harm+1)*BEAM_ENERGY*BEAM_ENERGY/ (idInfo[id].periodLength*(1+KEff*KEff/2.0)); if (convertDebug == 1) epicsPrintf("bToEnergy: Calculated Energy = %f\nPeriod = %f\nid = %d\n",*energy,idInfo[id].periodLength,id); return(1); } /******************************************************************* * Energy to Field Conversion *******************************************************************/ static int energyToB(float *bEff, float energy, int id) { float KEff; KEff = ( 2*(C1*BEAM_ENERGY*BEAM_ENERGY/ (energy*idInfo[id].periodLength/(idInfo[id].harm+1)) -1.0) ); if(KEff < 0){ KEff = 0; } else { KEff = sqrt(KEff); } *bEff = KEff/(K_CNV*idInfo[id].periodLength); if (convertDebug == 1) epicsPrintf("energyToB: Calculated B field = %7.3f\n",*bEff); return(1); } /******************************************************************* * Gap to B Peak Field Conversion *******************************************************************/ static int gapToBpk(float *bpk, float size, int id) { float lower=0, upper=0, lowerBound=0, upperBound=0; float logBpkfield; int i, done; /* Use the Hard Stops value if (size < idInfo[id].minimumGap) { if (convertDebug == 2) epicsPrintf("gapToBpk: Gap is too small to find Bpk\n"); return(ERROR); } */ if (size <= idInfo[id].hardStops) { if (convertDebug == 2) epicsPrintf("gapToBpk: Gap is too small to find Bpk\n"); return(ERROR); } done = 0; for(i=1; (!done) && (i= idInfo[id].minimumGap) && (size <= MAX_GAP) ) { done = 2; logBpkfield = idInfo[id].m * size + idInfo[id].c; *bpk = exp(logBpkfield); } if (!done) { if (convertDebug == 2) epicsPrintf("gapToBpk: Gap is too large to find Bpk\n"); return(1); } if (done == 1) { logBpkfield = interpolate(lower,upper,lowerBound,upperBound,size); *bpk = exp(logBpkfield); } if (convertDebug == 2) epicsPrintf("gapToBpk: ID = %s\nGap Size = %7.3f B peak field = %7.3f\n", idInfo[id].name,size,*bpk); return(OK); } /******************************************************************* * Subroutine for gap to total power conversion. - Planar Undulator * * Power is calculated in watts * * Beam current is stored in subroutine record INPA field * This comes from XFD:srCurrent.VAL * INPC - Avg gap value, F field is previously calculated power * L field is status value * 7 GeV Beam Energy is assumed *******************************************************************/ int SubGapPower(struct subRecord *psub) { int id, status; float beam_current; float gapvalue,bpeak=0,oldpower; beam_current = psub->a; /* Beam Current in ma keeps power in watts */ id = psub->b; /* Which ID: 0=DS-A, 1=US-A, 2=DS-B, 3=US-B */ gapvalue = psub->c; /* Average gap value for this ID */ oldpower = psub->val; /* Previous value of power */ psub->f = oldpower; status = gapToBpk(&bpeak,gapvalue,id); if (!status){ psub->e = bpeak; /* Peak field in Gauss */ bpeak = bpeak / C_G_T; /* Peak field in Tesla */ psub->l = 1; } else { if (convertDebug == 3) { epicsPrintf("SubGapPower: Problem calculating Kpeak value for gap %.3f\n",gapvalue); } psub->l = -1; } /* Calculate the total power */ psub->val = 0.633 * (idInfo[idNum].totNumPeriods * (idInfo[id].periodLength * C_CM_M)) * (BEAM_ENERGY*BEAM_ENERGY) * beam_current * (bpeak*bpeak); if(psub->val < 0) psub->val = 0.0; if (convertDebug == 3){ epicsPrintf("SubGapPower: ***** \n"); epicsPrintf("P = 0.633 * ( %d * (%f * %f) ) * (%f * %f) * %f * (%f * %f)\n *****",idInfo[idNum].totNumPeriods, idInfo[id].periodLength,C_CM_M,BEAM_ENERGY,BEAM_ENERGY,beam_current,bpeak,bpeak); } return(OK); } epicsRegisterFunction(SubGapPower); /******************************************************************* * Subroutine for gap to energy conversion. - Undulator *******************************************************************/ int SubGapEnergy(struct subRecord *psub) { int id; float gapvalue, bEff, energ0; int status; id = psub->a; /* Sub Record field a = device # 0=DS-A, 1=US-A, 2=DS-B, 3=US-B */ idInfo[id].harm = psub->b - 1; /* Sub Record field b = harmonic value */ gapvalue = psub->c; /* Sub Record field c holds gap value for this device */ status = 0; if (!(status=gapToB(&bEff,gapvalue,id)) ) { bToEnergy(bEff/C_G_T,&energ0,id); /* Check that the number of possible harmonics in the par correction file is * >= to the user selected harmonic which can be up to 7 for permanent magnet * IDs. If it is then use the user selected value for the energy shift otherwise * use the maximum number of harmonics from nharm variable. This value could be * less than 7 as: (Excerpt from Roger email) * there is just a very small intensity for higher harmonics due * to the small K values. The code for computing the harmonic tuning curves were * changed over the years and it will no longer compute intensities that for all * practical purposes are of no interest to the users. (Excerpt from Roger email) */ if (idInfo[id].nharm >= idInfo[id].harm){ /* apply downshift to the photon energy */ psub->val = e_down(energ0, idInfo[id].harm, id); } else { psub->val = e_down(energ0, idInfo[id].nharm, id); } psub->e = gapvalue; if (convertDebug == 1) epicsPrintf("SubGapEnergy: gap = %7.3f energy = %f psub->name = %s \npsub.c = %f ID = %d\n", gapvalue,psub->val,psub->name,psub->c,id); } else { psub->val = psub->d; if (convertDebug == 1) { epicsPrintf("SubGapEnergy: Gap size is out of range!!!!!! Gap = %7.3f\n",gapvalue); epicsPrintf("SubGapEnergy: Recname = %s Recname.C = %7.3f\n",psub->name,psub->c); epicsPrintf("SubGapEnergy: ID number = %d\n",id); } } psub->l = status; return(OK); } epicsRegisterFunction(SubGapEnergy); /******************************************************************* * Subroutine for energy to gap conversion. - Undulator *******************************************************************/ int SubEnergyGap(struct subRecord *psub) { int id; float gapvalue, energy, bEff, energ0; int status; id = psub->a; idInfo[id].harm = psub->b - 1; energy = psub->c; status = 0; /* trap energy values greater than 1000.0 */ if (energy >= 1000 ) { if(convertDebug == 2) epicsPrintf("SubEnergyGap: energy >= 1000 !!\n"); status = -1; } else if (energy <= 0.005) { if(convertDebug == 2) epicsPrintf("SubEnergyGap: energy < 0.005 !!\n"); status = 1; } else { /* Check that the number of possible harmonics in the par correction file is * >= to the user selected harmonic which can be up to 7 for permanent magnet * IDs. If it is then use the user selected value for the energy shift otherwise * use the maximum number of harmonics from nharm variable. This value could be * less than 7 as: (Excerpt from Roger email) * there is just a very small intensity for higher harmonics due * to the small K values. The code for computing the harmonic tuning curves were * changed over the years and it will no longer compute intensities that for all * practical purposes are of no interest to the users. (Excerpt from Roger email) */ /* apply upshift to the photon energy */ if (idInfo[id].nharm >= idInfo[id].harm){ energ0 = e_up(energy, idInfo[id].harm, id); } else { // Not sure if this fix is correct // Looks like a copy/paste error from above // Probably not required anyway for XLEAP // WL - 2017-05-05 //psub->val = e_down(energ0, idInfo[id].nharm, id); energ0 = e_up(energy, idInfo[id].nharm, id); } energyToB(&bEff,energ0,id); status=bToGap(bEff*C_G_T,&gapvalue,id); } if (status==0) { /* Now make sure that the converted gap is within the operating limits. */ if ( (gapvalue < psub->j) || (gapvalue > psub->k) ) { status = 1; psub->val = psub->d; if (convertDebug == 1) epicsPrintf("SubEnergyGap: Energy is out of Operating range!!!\n"); } else { psub->val = gapvalue; psub->e = energy; if (convertDebug == 1) epicsPrintf("SubEnergyGap: energy = %f gap = %7.3f\n",energy,gapvalue); } } else { psub->val = psub->d; if (convertDebug == 1) epicsPrintf("SubEnergyGap: Energy is out of range!!!\n"); } psub->l = status; return(OK); } epicsRegisterFunction(SubEnergyGap); /******************************************************************* * Subroutine for gap to critical energy conversion. - Wiggler *******************************************************************/ int SubGapEc(struct subRecord *psub) { int id; float gapvalue, bEff; int status; id = psub->a; gapvalue = psub->c; if (!(status=gapToB(&bEff,gapvalue,id))) { psub->val = EC_CNV * BEAM_ENERGY * BEAM_ENERGY * bEff/C_G_T; psub->e = gapvalue; if (convertDebug == 1) epicsPrintf("SubGapEc: gap = %7.3f energy = %f\n", gapvalue,psub->val); } else { psub->val = psub->d; if (convertDebug == 1) epicsPrintf("SubGapEc: Gap size is out of range!!!\n"); } psub->l = status; return(OK); } /******************************************************************* * Subroutine for critical energy to gap conversion. - Wiggler *******************************************************************/ int SubEcGap(struct subRecord *psub) { int id; float gapvalue, energy, bEff; int status; id = psub->a; energy = psub->c; /* trap energy values greater than 1000.0 */ if (energy >= 1000 ) { status = -1; } else if (energy <= 0.005) { status = 1; } else { bEff = energy/BEAM_ENERGY/BEAM_ENERGY/EC_CNV; status=bToGap(bEff*C_G_T,&gapvalue,id); } if (status==0) { psub->val = gapvalue; psub->e = energy; if (convertDebug == 1) epicsPrintf("energy = %f gap = %7.3f\n", energy,gapvalue); } else { psub->val = psub->d; if (convertDebug == 1) epicsPrintf("Energy is out of range!!!\n"); } psub->l = status; return(OK); } /******************************************************************* * Subroutine Init for GAP <=> ENERGY Conversions . *******************************************************************/ int initSubGtoE(struct subRecord *psub) { psub->e = psub->c; /* Copy Gap value into E field of GapToEnergy sub rec */ if(convertDebug == 1) { epicsPrintf("%s: Init completed for Subroutine Record \n",psub->name); } return(OK); } epicsRegisterFunction(initSubGtoE); /******************************************************************* * Subroutine Init for GAP <=> POWER Conversions . *******************************************************************/ int initSubGtoP(psub) struct subRecord *psub; { psub->a = 100.0; /* This was only used for testing since the test IOC cannot get the actual beam current */ return(OK); } epicsRegisterFunction(initSubGtoP); #define DEVICENAME psub->inpb /******************************************************************* * Subroutine Init to retrive the device name from data. *******************************************************************/ int initSubDevName(struct subRecord *psub) { char *xname="initSubDevName"; struct subDNamePvt *p; struct dbAddr *pdbAddr; int status; if ((psub->dpvt = malloc( sizeof(struct subDNamePvt))) == NULL) { errPrintf(S_dev_noMemory, __FILE__, __LINE__, "%s: couldn't allocate memory for %s\n", xname, psub->name); epicsPrintf("%s: couldn't allocate memory for %s\n", xname, psub->name); return(S_dev_noMemory); } p = (struct subDNamePvt *)psub->dpvt; if(!(pdbAddr = dbGetPdbAddrFromLink(&DEVICENAME))) { psub->dpvt = NULL; status = S_dev_NoInit; recGblRecordError(status, (void *)psub, "initSubDevName: inpb not a db link\n"); return(status); } p->psi = (struct stringinRecord *)pdbAddr->precord; return(OK); } epicsRegisterFunction(initSubDevName); /******************************************************************* * Subroutine to retrive the device name from data. * idNum: 0 = DS-A, 1 = US-A, 2 = DS-B, 3 = US-B (for revolver) * idNum: Use 0 and 1 for IDs with 1 set of magnets *******************************************************************/ int subDeviceName(struct subRecord *psub) { struct subDNamePvt *p; /* int id;*/ idNum = (int) psub->a; if ((p=(struct subDNamePvt *)psub->dpvt) == NULL ) { epicsPrintf("For Device Number %d Problem with SubDeviceName\n", idNum); return(OK); } strcpy(p->device,idInfo[idNum].name); psub->c = idInfo[idNum].periodLength; psub->d = idInfo[idNum].minimumGap; psub->e = idInfo[idNum].hardStops; strcpy(p->psi->val, p->device); dbPutLink(&DEVICENAME,DBR_SHORT,1,1); /* p->psi->proc = 1; Process stringin Record */ if (deviceDebug ==1) { epicsPrintf("For Device Number %d\n", idNum); epicsPrintf("For Device Name %s\n", idInfo[idNum].name); epicsPrintf(" Data read from file %s\n",idInfo[idNum].dataFile); epicsPrintf(" Device Name is : %s\n", p->psi->val); epicsPrintf(" Period is %.2f cm\n", psub->c); epicsPrintf(" Mimimum Gap is %.2f mm\n",psub->d); epicsPrintf(" Hard stops is %.2f mm\n",psub->e); } return(OK); } epicsRegisterFunction(subDeviceName); static const iocshArg readDataArg0 = {"Filename",iocshArgString}; static const iocshArg readDataArg1 = {"ID Number (0=DS-A,1=US-A,2=DS-B,3=US-B)", iocshArgInt}; static const iocshArg * const readDataArgs[2] = {&readDataArg0, &readDataArg1}; static const iocshFuncDef readDataFuncDef = {"readData",2,readDataArgs}; static void readDataCallFunc(const iocshArgBuf *args) { readData(args[0].sval,args[1].ival); } static void readDataRegister(void) { iocshRegister(&readDataFuncDef,readDataCallFunc); } static const iocshArg readDataCorrArg0 = {"Filename",iocshArgString}; static const iocshArg readDataCorrArg1 = {"ID Number (0=DS-A,1=US-A,2=DS-B,3=US-B)", iocshArgInt}; static const iocshArg * const readDataCorrArgs[2] = { &readDataCorrArg0, &readDataCorrArg1}; static const iocshFuncDef readDataCorrFuncDef = {"readDataCorr",2,readDataCorrArgs}; static void readDataCorrCallFunc(const iocshArgBuf *args) { readDataCorr(args[0].sval,args[1].ival); } static void readDataCorrRegister(void) { iocshRegister(&readDataCorrFuncDef,readDataCorrCallFunc); } static registryFunctionRef IDdataSubRef[] = { {"GtoESubInit",(REGISTRYFUNCTION)initSubGtoE}, {"DevNameSubInit",(REGISTRYFUNCTION)initSubDevName}, {"DevNameSubProcess",(REGISTRYFUNCTION)subDeviceName}, {"GapEnergySubProcess",(REGISTRYFUNCTION)SubGapEnergy}, {"EnergyGapSubProcess",(REGISTRYFUNCTION)SubEnergyGap}, {"GapPowerSubProcess",(REGISTRYFUNCTION)SubGapPower}, {"GtoPSubInit",(REGISTRYFUNCTION)initSubGtoP} }; void IDdataSub(void) { registryFunctionRefAdd(IDdataSubRef,NELEMENTS(IDdataSubRef)); } epicsExportRegistrar(IDdataSubRef);