/*============================================================================= 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) 12 MAY 2009 dfairley separated V1 and V2 in phase calculations 8 JUL 2008 dfairley added fbckSupPhasesChk 7 JUL 2008 dfairley added fbckSupPhasesCalc 20 MAY 2008 dfairley added fbckSupIpkCalc =============================================================================*/ /* c includes */ #include /* memcmp */ #include /* sqrt,sin */ #include /* sqrt,sin */ #include /* rand */ #include /* for struct subRecord */ #include /* for epicsExport */ #include /* for epicsRegisterFunction */ #include /* epicsTimeStamp */ #include /* dbGetTimeStamp */ #include /* NO_ALARM, INVALID_ALARM */ static int fbckSupInit(subRecord *psub) { /* * General purpose initialization required since all subroutine records * require a non-NULL init routine even if no initialization is required. * Note that most subroutines in this file use this routine as an init * routine. If init logic is needed for a specific subroutine, create a * new routine for it - don't modify this one. */ return 0; } /*============================================================================= 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; } epicsRegisterFunction(fbckSupInit); epicsRegisterFunction(fbckSupRMSCalc);