/* subIDMotion.c * Author: Mohan Ramanathan * Date: 7/29/98 * * Modification Log: * ----------------- * .01 07/30/98 mr initial * 04-24-08 mls Modified for EPICS 3.14 */ #include #include #include #include #include #include #include #include #include #include #include #include "registryFunction.h" #include "epicsExport.h" #include "xleap.h" #define CIRBUFSIZE 100 #define NO_ERR_RPT -1 #define GAP psub->a #define MAXGAP psub->b #define DELTAGAP psub->c #define TIME psub->d int debugSubIDMotion = 0; epicsExportAddress(int, debugSubIDMotion); struct fcirBuf { short num; short old; short new; short full; short fillCount; double buf[CIRBUFSIZE]; }; static void add2buffer(double new, double *ptr, short *newest, short *oldest, short size) { if(++(*newest) >= size) *newest = 0; ptr[(*newest)] = new; if(++(*oldest) >= size) *oldest = 0; } int initSubIDMotion( struct subRecord *psub) { char *xname="initSubIDMotion"; struct fcirBuf *p; short i; if ((psub->dpvt = malloc( sizeof(struct fcirBuf))) == NULL) { errPrintf(S_dev_noMemory, __FILE__, __LINE__, "%s: couldn't allocate memory for %s", xname, psub->name); return(S_dev_noMemory); } p = (struct fcirBuf *)psub->dpvt; if(debugSubIDMotion) printf("%s: Init completed for Subroutine Record %s\n", xname, psub->name); for ( i = 0 ; i < CIRBUFSIZE; i++) p->buf[i] = 0; p->num = TIME; p->new=0; p->full=0; p->old = 1; p->fillCount =0; return(OK); } epicsRegisterFunction(initSubIDMotion); int SubIDMotion( struct subRecord *psub) { char *xname="SubIDMotion"; int num; short i; short j; short moving=0; struct fcirBuf *p; if((p = (struct fcirBuf *)psub->dpvt) == NULL) { if(debugSubIDMotion) errPrintf(S_dev_noMemory, __FILE__, __LINE__, "%s: dpvt in NULL for %s", xname, psub->name); return(ERROR); } num = (int)TIME; if ( num > CIRBUFSIZE ) { if(debugSubIDMotion) errPrintf(NO_ERR_RPT, __FILE__, __LINE__, "%s: Time to detect (%d) exceeds limit (%d) for PV %s", xname, (int)num, CIRBUFSIZE, psub->name); num = CIRBUFSIZE; } if (p->num != TIME) { for ( i = 0 ; i < CIRBUFSIZE; i++) p->buf[i] = 0; p->num = TIME; p->new=0; p->full=0; p->old = 1; p->fillCount =0; return(OK); } if(++p->fillCount >= num) p->full = 1; if (p->full == 1) { add2buffer(GAP, p->buf, &p->new, &p->old, num); if( fabs(GAP - p->buf[p->old]) >= DELTAGAP && (GAP < MAXGAP || p->buf[p->old] < MAXGAP) ) { moving = 1; } else { moving = 0; } } psub->val = moving; if(debugSubIDMotion == 10) printf (" psub->val = %.0f \n", psub->val); if(debugSubIDMotion > 20) { j = p->old; printf (" p->old = %d , TIME = %.0f \n", p->old, TIME); for(i = 0; i < TIME; i++) { if( j >= TIME) j = 0; printf("%d : %.3f \n", j++, p->buf[j]); } } return(OK); } epicsRegisterFunction(SubIDMotion); static registryFunctionRef IDMotionSubRef[] = { {"SubIDMotion",(REGISTRYFUNCTION)SubIDMotion}, {"initSubIDMotion",(REGISTRYFUNCTION)initSubIDMotion} }; void IDMotionSub(void) { registryFunctionRefAdd(IDMotionSubRef,NELEMENTS(IDMotionSubRef)); } epicsExportRegistrar(IDMotionSubRef);