/*============================================================================= Name: fbckSup.c Abs: This file contains all subroutine support for the feedback soft IOC subroutine records. Rem: All functions called by the subroutine record get passed one argument: psub Pointer to the subroutine record data. Use: pointer Type: struct subRecord * Acc: read/write Mech: reference All functions return a long integer. 0 = OK, -1 = ERROR. The subroutine record ignores the status returned by the Init routines. Auth: D. Fairley Rev: -----------------------------------------------------------------------------*/ /*----------------------------------------------------------------------------- Mod: (newest to oldest) 20 MAY 2008 dfairley added fbckSupIpkCalc =============================================================================*/ /* c includes */ #include /* memcmp */ #include /* sqrt,sin */ #include /* for struct subRecord */ #include /* for epicsExport */ #include /* for epicsRegisterFunction */ #include /* DBADDR */ /*============================================================================= Name: fbckSupRMSCalc Abs: longitudinal machine simulation. Calculate the average of the values in the circular buffer from INPA, and the rms value of the squared values in the circular buffer from INPB. These will be displayed as a measure of the effectiveness of the feedback algorithm on the signal. Args: Type Name Access Description ------------------- ----------- ---------- ---------------------------- subRecord * psub read point to subroutine record Rem: Subroutine for $FBDEV:$(STATE)RMS Side: INPA = $FBCK:$LOCA:$UNIT:$SNAMESQHST (square of err) history VAL = the resulting rms value of the state, as a percentage Ret: 0 ==============================================================================*/ static long fbckSupRMSCalc(subRecord *psub) { DBADDR *sqhstAddr = dbGetPdbAddrFromLink(&psub->inpa); double *sqhst_p; int i = 0; double sum_sqhst = 0; /*printf("starting longCalc\n");*/ /* check for error conditions, no input, no waveform ptr, not enough elements */ if (!sqhstAddr) return -1; /* printf("compress record addrs are good\n");*/ sqhst_p = (double *)sqhstAddr->pfield; if (!sqhst_p) return -1; /*printf("hst pointers and sizes are good\n");*/ if (sqhstAddr->no_elements <=0) { psub->val = 0; } else { /* now calculate */ for ( i=0; i < sqhstAddr->no_elements; i++) { sum_sqhst += sqhst_p[i]; } /* calc and report the value as a percentage */ psub->val = 100*(sqrt(sum_sqhst / sqhstAddr->no_elements)); } return 0; } /*============================================================================= Name: fbckSupIpkCalc Abs: Calculates the bunch length in peak current (amperes) given the BLEN readout in 'counts', the current BPM2 TMIT, and the calibration coefficients provided by the operator. Args: Type Name Access Description ------------------- ----------- ---------- ---------------------------- subRecord * psub read point to subroutine record Rem: Subroutine for :$(BLEN)IMAX Side: INPA to INPH = Coefficients for the given bunch length monitor INPI = divide by number to be used when filter is not in INPJ = filter in state, 1=filter in INPK = $(BLEN) bunch length readout in counts INPL = $(TMIT) BPM2 TMIT value Should severity of BLEN and TMIT also be input? VAL = the resulting peak current value for the bunchlength monitor Ret: 0 Future: -1 when bunch readout or TMIT value is bad ==============================================================================*/ long fbckSupIpkCalc(subRecord *psub) { double *coeff_p; double Ipk = 0; double tmit = 0; double bl = 0; double filter = 0; /* filter state, 0 = not in */ double divider = 0; /* divider to be used whenthe filter is not in */ /*printf("starting BLCalc\n");*/ coeff_p = &psub->a; /* Note A,B,C,... contiguous in subRecord structure */ /*printf("coeff. pointers and sizes are good\n");*/ tmit = psub->l; bl = psub->k; filter = psub->j; divider = psub->i; /* Should severity of bl and tmit be checked? Should values of bl and tmit be checked for reasonability before using in pow? */ /*printf("tmit=%e, bl=%e\n", tmit, bl); printf("c1=%e, c2=%e, c3=%e, c4=%e\n", coeff_p[0],coeff_p[1], coeff_p[2],coeff_p[3]);*/ /* now calculate */ if(tmit < 1e-6 || bl < 1e-6) { psub->val = 0; return 0; } if (coeff_p[0]!=0) { Ipk = coeff_p[0]; /*printf("c1 Ipk=%e\n", Ipk);*/ } if (coeff_p[1]!=0) { Ipk = Ipk + coeff_p[1]*pow(tmit,coeff_p[2])*pow(bl,coeff_p[3]); /*printf("c2, c3, c4 Ipk=%e\n", Ipk);*/ } if (coeff_p[4]!=0) { Ipk = Ipk + coeff_p[4]*pow(tmit,coeff_p[5])*pow(bl,coeff_p[6]); /*printf("c5, c6, c7 Ipk=%e\n", Ipk);*/ } if (coeff_p[7]!=0) { Ipk = Ipk + coeff_p[7] /* take out coeff8 and coeff9 *pow(tmit,coeff_p[8])*pow(bl,coeff_p[9]) */; /*printf("c8, c9, c10 Ipk=%e\n", Ipk);*/ } /*printf("full Ipk=%e\n", Ipk);*/ /* adjust if filter is not in for filter status */ if (divider != 0) if (filter == 0) Ipk /= divider; /* report the value */ psub->val = Ipk; return 0; } epicsRegisterFunction(fbckSupRMSCalc); epicsRegisterFunction(fbckSupIpkCalc);