#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include static long SUCCESS = 0; static long ERROR = -1; /* Copied from drvBpm.h */ #define RAD ((PI)/180.0) #ifndef PI #define PI 3.1415926535897932385 #endif /* end copied */ #undef DEBUG #undef CDEBUG #undef SDEBUG long subBpmParmsInit(struct aSubRecord *asub_p) { if( asub_p == NULL) return ERROR; return SUCCESS; } /* Conversion steps for BPM U/V -> X/Y u = u*uscl+uoff; v = v*vscl+voff; xraw = ruu*u + ruv*v; yraw = rvu*u + rvv*v; x = (ruu*(u*uscl+uoff) + ruv*(v*vscl+voff))* xscl + xoff; y = (rvu*(u*uscl+uoff) + rvv*(v*vscl+voff))* yscl + yoff; x = (ruu*u*uscl + ruu*uoff + ruv*v*vscl + ruv*voff)*xscl + xoff; y = (rvu*u*uscl + rvu*uoff + rvv*v*vscl + rvv*voff)*yscl + yoff; x = u*(ruu*uscl*xscl) + ruu*uoff*xscl + v*(ruv*vscl*xscl) + ruv*voff*xscl + xoff; y = u*(rvu*uscl*yscl) + rvu*uoff*yscl + v*(rvv*vscl*yscl) + rvv*voff*yscl + yoff; x = u*(ruu*uscl*xscl) + v*(ruv*vscl*xscl) + (ruu*uoff*xscl + ruv*voff*xscl + xoff); y = u*(rvu*uscl*yscl) + v*(rvv*vscl*yscl) + (rvu*uoff*yscl + rvv*voff*yscl + yoff); */ /* * Calculate BPM U/V -> X/Y rotation matrix elements * (A11, A12, A21, A22) and offsets (O_1, O_2) * * |X| = |A11 A12| * |U| + |O_1| * |Y| |A21 A22| |V| |O_2| * */ /* Support for calculating BPM algorithm parameters that will be written to low-level hardware * plus X/Y scale factors used by software and client applications */ long subBpmAlgParms(struct aSubRecord *asub_p) { /* Inputs */ epicsFloat64 psi = *(epicsFloat64 *)asub_p->a; /* Axes angle */ epicsFloat64 phi = *(epicsFloat64 *)asub_p->b; /* Rotation angle */ epicsFloat64 uscl = *(epicsFloat64 *)asub_p->c; /* U scale factor (ASLO) */ epicsFloat64 uoff = *(epicsFloat64 *)asub_p->d; /* U offset (AOFF) */ epicsFloat64 vscl = *(epicsFloat64 *)asub_p->e; /* V scale factor (ASLO) */ epicsFloat64 voff = *(epicsFloat64 *)asub_p->f; /* V offset (AOFF) */ epicsFloat64 xscl = *(epicsFloat64 *)asub_p->g; /* X scale factor (ASLO) */ epicsFloat64 xoff = *(epicsFloat64 *)asub_p->h; /* X offset (AOFF) */ epicsFloat64 yscl = *(epicsFloat64 *)asub_p->i; /* Y scale factor (ASLO) */ epicsFloat64 yoff = *(epicsFloat64 *)asub_p->j; /* Y offset (AOFF) */ epicsFloat64 xymult = *(epicsFloat64 *)asub_p->k; /* X/Y alg parms scaled by USCL/2/xymult and VSCL/2/xymult */ /* Intermediate calculated values */ epicsFloat64 spsi, ruu, ruv, rvu, rvv; /* Algorithm parameters written to FW registers */ epicsFloat64 *a11 = (epicsFloat64 *)asub_p->vala; /* Rotation matrix element A11 */ epicsFloat64 *a12 = (epicsFloat64 *)asub_p->valb; /* Rotation matrix element A12 */ epicsFloat64 *a21 = (epicsFloat64 *)asub_p->valc; /* Rotation matrix element A21 */ epicsFloat64 *a22 = (epicsFloat64 *)asub_p->vald; /* Rotation matrix element A22 */ epicsFloat64 *o_1 = (epicsFloat64 *)asub_p->vale; /* X offset */ epicsFloat64 *o_2 = (epicsFloat64 *)asub_p->valf; /* Y offset */ /* Scale factors used by software and other clients to scale to standard units */ epicsFloat64 *fw_xscl_client = (epicsFloat64 *)asub_p->valg;/* Scales FW raw X reading to mm units */ epicsFloat64 *fw_yscl_client = (epicsFloat64 *)asub_p->valh;/* Scales FW raw Y reading to mm units */ /* Scale factors used internally and for diagnostic screens */ epicsFloat64 *fw_xscl = (epicsFloat64 *)asub_p->valj;/* 2^17 * fw_xscl */ epicsFloat64 *fw_yscl = (epicsFloat64 *)asub_p->valk;/* 2^17 * fw_yscl */ /* 1/2 of EPICS scale because FW does not divide by 2 * in U/V calculation, as software does */ uscl = uscl/2; vscl = vscl/2; spsi = sin(psi*RAD); ruu = sin((psi-phi)*RAD)/spsi; ruv = sin(phi*RAD) /spsi; rvu = -cos((psi-phi)*RAD)/spsi; rvv = cos(phi*RAD) /spsi; /* Before sending values, divide by scale factor * When reading back from FW, software will * multiply raw data by that same scale factor */ *fw_xscl = uscl/xymult; *fw_yscl = vscl/xymult; *a11 = (ruu*uscl*xscl)/(*fw_xscl); *a12 = (ruv*vscl*xscl)/(*fw_xscl); *a21 = (rvu*uscl*yscl)/(*fw_yscl); *a22 = (rvv*vscl*yscl)/(*fw_yscl); *o_1 = (ruu*uoff*xscl + ruv*voff*xscl + xoff)/(*fw_xscl); *o_2 = (rvu*uoff*yscl + rvv*voff*yscl + yoff)/(*fw_yscl); *fw_xscl_client = *fw_xscl/pow(2,17); *fw_yscl_client = *fw_yscl/pow(2,17); #ifdef DEBUG printf("%s: inputs phi %f psi %f uscl %f uoff %f vscl %f voff %f xscl %f xoff %f yscl %f yoff %f xymult %f\n", asub_p->name, phi, psi, uscl, uoff, vscl, voff, xscl, xoff, yscl, yoff, xymult); printf("%s: write a11 %f a12 %f a21 %f a22 %f o_1 %f o_2 %f fw_xscl %f fw_yscl %f fw_xscl_client %f " "fw_yscl_client %f\n", asub_p->name, *a11, *a12, *a21, *a22, *o_1, *o_2, *fw_xscl, *fw_yscl, *fw_xscl_client, *fw_yscl_client); #endif return SUCCESS; } long subBpmCalParms(struct aSubRecord *asub_p) { /* Inputs */ epicsFloat64 gr = *(epicsFloat64 *)asub_p->a; /* Gain ratio */ unsigned short gr_sevr = *(unsigned short *)asub_p->b; /* EPICS severity of gain ratio */ /* Outputs */ epicsFloat64 *gr_delta = (epicsFloat64 *)asub_p->vala; /* Gain ratio - 1 */ /* Only write new val to hardware if gain ratio in NO_ALARM state */ if ( gr_sevr == 0 ) *gr_delta = gr - 1.0; else { #ifdef SDEBUG printf("%s: input gr %f sevr %i, do not write new value\n", asub_p->name, gr, gr_sevr); #endif return ERROR; } #ifdef SDEBUG printf("%s: input gr %f\n", asub_p->name, gr); printf("%s: write gr_delta %f\n", asub_p->name, *gr_delta); #endif return SUCCESS; } /* Build bit mask from individual bits. */ long subBpmMask(struct aSubRecord *asub_p) { unsigned nbits = 21; unsigned short bits[nbits]; int i; /* inputs */ bits[0] = *(unsigned short *)asub_p->a ? 1 : 0; bits[1] = *(unsigned short *)asub_p->b ? 1 : 0; bits[2] = *(unsigned short *)asub_p->c ? 1 : 0; bits[3] = *(unsigned short *)asub_p->d ? 1 : 0; bits[4] = *(unsigned short *)asub_p->e ? 1 : 0; bits[5] = *(unsigned short *)asub_p->f ? 1 : 0; bits[6] = *(unsigned short *)asub_p->g ? 1 : 0; bits[7] = *(unsigned short *)asub_p->h ? 1 : 0; bits[8] = *(unsigned short *)asub_p->i ? 1 : 0; bits[9] = *(unsigned short *)asub_p->j ? 1 : 0; bits[10] = *(unsigned short *)asub_p->k ? 1 : 0; bits[11] = *(unsigned short *)asub_p->l ? 1 : 0; bits[12] = *(unsigned short *)asub_p->m ? 1 : 0; bits[13] = *(unsigned short *)asub_p->n ? 1 : 0; bits[14] = *(unsigned short *)asub_p->o ? 1 : 0; bits[15] = *(unsigned short *)asub_p->p ? 1 : 0; bits[16] = *(unsigned short *)asub_p->q ? 1 : 0; bits[17] = *(unsigned short *)asub_p->r ? 1 : 0; bits[18] = *(unsigned short *)asub_p->s ? 1 : 0; bits[19] = *(unsigned short *)asub_p->t ? 1 : 0; bits[20] = *(unsigned short *)asub_p->u ? 1 : 0; /* output */ long *mask = (long *)asub_p->vala; *mask = 0; for ( i = 0; i < nbits; i++ ) *mask = *mask | (bits[i] << i ); #ifdef DEBUG printf("%s: mask output %i 0x%x\n", asub_p->name, (int)*mask, (unsigned int)*mask); #endif return SUCCESS; } /* Extract real/imag from complex. Assumes sizes, shifts, precision. */ long subBpmUnpack(struct aSubRecord *asub_p) { uint16_t mask = 0xFFFF; /* Each component occupies 16 bits */ unsigned short prec = 15; /* Component precision in bits */ unsigned short rsh = 16; /* Upper 16 bits contains real part */ unsigned short ish = 0; /* Lower 16 bits contains imaginary part */ /* inputs */ uint32_t source = *(uint32_t *)asub_p->a; /* Outputs */ epicsFloat64 *real = (epicsFloat64 *)asub_p->vala; epicsFloat64 *imag = (epicsFloat64 *)asub_p->valb; *real = (epicsFloat64)(((int16_t)( (source & (mask << rsh)) >> rsh))/pow(2,prec)); *imag = (epicsFloat64)(((int16_t)( (source & (mask << ish)) >> ish))/pow(2,prec)); #ifdef DEBUG printf("\n%s: source %lu 0x%x real %f imag %f\n", asub_p->name, source, source, *real, *imag); #endif return SUCCESS; } #define U 0 #define V 1 #define NUM_CAV_SIGS 2 #define NUM_CAV_DFTS 6 #define FILT_WF_SIZE 192 long subBpmCavDftFreq(struct aSubRecord *asub_p) { epicsFloat64 fc[NUM_CAV_SIGS]; epicsFloat64 bw[NUM_CAV_SIGS]; epicsFloat64 *f[NUM_CAV_DFTS]; printf("get/set filtnelms in init routine or #define\n"); epicsFloat64 fs; /* Digitizer sampling frequency */ int i, j; epicsFloat64 *wu[NUM_CAV_DFTS]; /* Weight factors for U, currently used=1 and unused=0 */ epicsFloat64 *wv[NUM_CAV_DFTS]; /* Weight factors for V, currently used=1 and unused=0 */ /* Inputs */ fc[U] = *(epicsFloat64 *)asub_p->a; /* U center frequency [MHz] */ bw[U] = *(epicsFloat64 *)asub_p->b; /* U bandwidth [MHz] */ fc[V] = *(epicsFloat64 *)asub_p->c; /* V frequency [MHz] */ bw[V] = *(epicsFloat64 *)asub_p->d; /* V bandwidth [MHz] */ fs = *(epicsFloat64 *)asub_p->e; /* Digitizer sampling frequency [MHz] */ /* Outputs */ f[0] = (epicsFloat64 *)asub_p->vala; /* DFT 0 frequency */ f[1] = (epicsFloat64 *)asub_p->valb; /* DFT 1 frequency */ f[2] = (epicsFloat64 *)asub_p->valc; /* DFT 2 frequency */ f[3] = (epicsFloat64 *)asub_p->vald; /* DFT 3 frequency */ f[4] = (epicsFloat64 *)asub_p->vale; /* DFT 4 frequency */ f[5] = (epicsFloat64 *)asub_p->valf; /* DFT 5 frequency */ wu[0] = (epicsFloat64 *)asub_p->valg; /* U weight factor for DFT 0 */ wu[1] = (epicsFloat64 *)asub_p->valh; /* U weight factor for DFT 1 */ wu[2] = (epicsFloat64 *)asub_p->vali; /* U weight factor for DFT 2 */ wu[3] = (epicsFloat64 *)asub_p->valj; /* U weight factor for DFT 3 */ wu[4] = (epicsFloat64 *)asub_p->valk; /* U weight factor for DFT 4 */ wu[5] = (epicsFloat64 *)asub_p->vall; /* U weight factor for DFT 5 */ wv[0] = (epicsFloat64 *)asub_p->valm; /* V weight factor for DFT 0 */ wv[1] = (epicsFloat64 *)asub_p->valn; /* V weight factor for DFT 1 */ wv[2] = (epicsFloat64 *)asub_p->valo; /* V weight factor for DFT 2 */ wv[3] = (epicsFloat64 *)asub_p->valp; /* V weight factor for DFT 3 */ wv[4] = (epicsFloat64 *)asub_p->valq; /* V weight factor for DFT 4 */ wv[5] = (epicsFloat64 *)asub_p->valr; /* V weight factor for DFT 5 */ /* Parameters used in determining detector frequencies */ epicsFloat64 fhi[NUM_CAV_SIGS]; /* Highest frequency (based on center frequency and bandwidth) */ epicsFloat64 flo[NUM_CAV_SIGS]; /* Lowest frequency (based on center frequency and bandwidth) */ epicsFloat64 fhi_min; /* Lowest 'high' frequency among all signals */ epicsFloat64 flo_max; /* Highest 'low' frequency among all signals */ epicsFloat64 fdiff; /* Difference between minimum high and maximum low */ epicsFloat64 bw_ol; /* Bandwidth of selected overlapping range */ epicsFloat64 fd; /* Selected frequency spacing (for evenly spaced frequencies) */ #ifdef CDEBUG printf("%s inputs: freq u %f bw u %f freq v %f bw v %f\n", asub_p->name, fc[U], bw[U], fc[V], bw[V]); #endif /* Check that values seem reasonable (need to figure out what reasonable means) */ /* Consider calculating reasonable ranges from ideal values */ /* Start with un-allowed values */ fhi_min = fs/2.; flo_max = 0; for ( i = 0; i < NUM_CAV_SIGS; i++ ) { fhi[i] = fc[i] + bw[i]/2; flo[i] = fc[i] - bw[i]/2; fhi_min = fmin( fhi_min, fhi[i] ); flo_max = fmax( flo_max, flo[i] ); } fdiff = fhi_min - flo_max; bw_ol = fdiff > 0 ? fdiff : 0; #ifdef CDEBUG printf("%s: lowest high %f highest low %f, overlapping bw %f\n", asub_p->name, fhi_min, flo_max, bw_ol); #endif /* If overlapping bandwidth is insufficient, then choose different * frequencies for U and for V and use the weight factors to * correctly supress the unused frequencies for each. * * Currently using 1/2 bandwidth as test. This is arbitrary--revisit this */ if ( (bw_ol < bw[U]/2) || (bw_ol < bw[V]/2) ) { printf("Insufficient overlap among U/V. Choose different frequencies for each.\n" ); /* Use first half for U, second half for V */ fd = bw[U]/((NUM_CAV_DFTS/2)-1); for ( i = 0; i < NUM_CAV_DFTS/2; i++ ) { *f[i] = fc[U] - (bw[U]/2) + i*fd; *wu[i] = 1; *wv[i] = 0; } fd = bw[V]/((NUM_CAV_DFTS/2)-1); for ( j = i, i = 0; j < NUM_CAV_DFTS; j++, i++ ) { *f[j] = fc[V] - (bw[V]/2) + i*fd; *wv[j] = 1; *wu[j] = 0; } } else { /* Sufficient overlap; U and V can share all 6 frequencies */ fd = bw_ol/(NUM_CAV_DFTS-1); for ( i = 0; i < NUM_CAV_DFTS; i++ ) { *f[i] = flo_max + i*fd; *wu[i] = *wv[i] = 1; } #ifdef CDEBUG epicsFloat64 fc_ol; /* Center frequency of selected overlapping range */ fc_ol = flo_max + bw_ol/2; printf("%s: Selected overlapping range center frequency %f bandwidth %f fd %f\n", asub_p->name, fc_ol, bw_ol, fd); #endif } #ifdef CDEBUG printf("%s outputs: DFT0 freq %f DFT1 freq %f DFT2 freq %f DFT3 freq %f DFT4 freq %f DFT5 freq %f\n", asub_p->name, *f[0], *f[1], *f[2], *f[3], *f[4], *f[5]); printf("%s outputs: U weight factors %f %f %f %f %f %f, V weight factors %f %f %f %f %f %f\n", asub_p->name, *wu[0], *wu[1], *wu[2], *wu[3], *wu[4], *wu[5], *wv[0], *wv[1], *wv[2], *wv[3], *wv[4], *wv[5]); #endif return SUCCESS; } long subBpmCavDftCoeff(struct aSubRecord *asub_p) { /* Frequency input */ epicsFloat64 f = *(epicsFloat64 *)asub_p->a; /* DFT frequency */ epicsFloat64 fsampl = *(epicsFloat64 *)asub_p->b; /* Sampling frequency */ /* Coefficient outputs */ epicsFloat64 *c2c4 = (epicsFloat64 *)asub_p->vala; epicsFloat64 *c2c2 = (epicsFloat64 *)asub_p->valb; epicsFloat64 *c2c1 = (epicsFloat64 *)asub_p->valc; epicsFloat64 *c2s1 = (epicsFloat64 *)asub_p->vald; epicsFloat64 p = 2.*PI*f/fsampl; #ifdef CDEBUG printf("%s inputs: DFT freq %f sampling freq %f\n", asub_p->name, f, fsampl); #endif *c2c4 = 2*cos(4*p); *c2c2 = 2*cos(2*p); *c2c1 = 2*cos(p); *c2s1 = 2*sin(p); #ifdef CDEBUG printf("%s outputs: Coefficients %f %f %f %f\n", asub_p->name, *c2c4, *c2c2, *c2c1, *c2s1); #endif return SUCCESS; } long subBpmCalcPhas(struct aSubRecord *asub_p) { /* Inputs */ epicsFloat64 real = *(epicsFloat64 *)asub_p->a; /* Real part of cpmplex value */ epicsFloat64 imag = *(epicsFloat64 *)asub_p->b; /* Imag part of complex value */ epicsFloat64 phas_i = *(epicsFloat64 *)asub_p->c; /* Current phase value */ /* Output */ epicsFloat64 *phas = (epicsFloat64 *)asub_p->vala; /* Phase angle to rotate all into real plane */ complex z = real + imag*I; epicsFloat64 angle = carg( z ); /* Angle, in radians */ *phas = phas_i -angle*180/PI; /* Convert to degrees and subtract from current phase */ #ifdef CDEBUG printf("%s inputs: real %f imag %f, current phase %f, angle in rad %f, calculated phas %f\n", asub_p->name, real, imag, phas_i, angle, *phas); #endif return SUCCESS; } epicsRegisterFunction(subBpmParmsInit); epicsRegisterFunction(subBpmAlgParms); epicsRegisterFunction(subBpmCalParms); epicsRegisterFunction(subBpmMask); epicsRegisterFunction(subBpmUnpack); epicsRegisterFunction(subBpmCalcPhas);