/****************************************************************************** * Module: Bunch length fcom * Filename: fcomBlm.c * Author: Qing Yang * Created: 1/5/2010 * Description: * Fcom related bunch length processing ****************************************************************************** * Modification history * * $Log: fcomBlm.c,v $ * Revision 1.9 2015/03/18 01:11:50 gwbrown * Updated to latest modules, including event. Updated so FACET can use the same release as LCLS. Modified to allow TMIT to come from a PV or fcom, depending on settings. * * Revision 1.8 2014/04/30 16:25:37 gwbrown * Fixed a bug where chan. A imax data was put where chan. B imax should be in fcom data. * * Revision 1.7 2013/02/28 02:02:33 gwbrown * Merged branch B_BunchLength_Strauman into HEAD. * * Revision 1.6.4.9 2012/06/13 21:28:39 strauman * - preserve 'double' data while calculating IMAXes. However, all FCOM data * is single-precision only (why I don't know; FCOM could ship double, too, * but we can't change w/o changing the other end...). * * Revision 1.6.4.8 2012/06/13 20:51:05 strauman * - refined clock period estimator. Should wait for a tick boundary before * measuring - otherwise we may measure just the residual until the next tick. * * Revision 1.6.4.7 2012/06/12 15:29:02 strauman * - made sysClkPeriodGet() public. * * Revision 1.6.4.6 2012/06/12 15:28:34 strauman * - added test for system clock frequency and cantProceed() if too low. * * Revision 1.6.4.5 2012/06/08 19:07:58 strauman * - propagate timestamps from drvPadUdpComm up to device support which * sets record timestamps accordingly (if TSE == -2) * * Revision 1.6.4.4 2012/06/08 16:43:32 strauman * - fixed typo in operator (&=, not ~=) * * Revision 1.6.4.3 2012/06/08 16:42:12 strauman * - must clear 'timedout' bit(s) corresponsing to 'imax' * - introduced cpp macros for timedout bitmasks replacing literal numbers. * * Revision 1.6.4.2 2012/05/25 21:00:28 strauman * 20120525 (Till Straumann) * - modified BunchLength App to use the generic PAD application (same * as used by BPMs) to stream raw data using drvPadUdpComm (from the * udpComm module). * All processing is done in the VME/master IOC. * Parallel computation of IMAX in EPICS record was removed; the * low-level computation is picked up by the EPICS record for BSA. * * Revision 1.6.4.1 2012/05/11 17:34:17 strauman * - started cleanup with the goal to * * use generic udpComm instead of private clone/copy * * remove parallel computation of imax in record- and fcom land. * The fcom-based version should be made available to BSA * * improve ill-written code (without redesigning from scratch) * * Revision 1.6 2010/04/14 22:51:44 qing * Change to use evrTimeActive for TS. An old hack to forgive the slowness of BLM was causing the TS problem. That is fixed in the evr db definition. * * Revision 1.5 2010/03/30 19:13:48 qing * Add error codes for the fcom blob data. * * Revision 1.4 2010/03/24 16:59:30 qing * Change tmit to BPMS:LI24:901 because the one in li25 is not on the fnet. * * Revision 1.3 2010/03/23 00:51:37 qing * Change to use the actual tmit in production. * * Revision 1.2 2010/03/08 18:36:09 qing * Change time stamp to use evrTimeGetFromPipeline, to get the time stamp of the current slot. * * Revision 1.1 2010/03/04 23:09:48 qing * Initial check in, after testing in building 34 test network. * *****************************************************************************/ #include #include #include #include #include #include /* for struct subRecord */ #include /* DBADDR */ #include /* DBADDR */ #include #include #include #include #include #include #include #include #include #include #include #include #include "fcomBlm.h" volatile unsigned blmFcomTimeoutMs = 5; int blmFcomDebug = 0; volatile unsigned blmFcomMaxTmitDelayUs = 0; volatile unsigned blmFcomMaxPadDelayUs = 0; #define NUM_SECTIONS 3 int gBlenSection = 0; /* 0 = LI21, 1 = LI24 , 2=FACET LI20*/ int gTmitFromCA = 0; /* true for FACET, get tmit from an input PV, not fcom */ FcomID pvFcomRaw[NUM_RAW_FCOM_DATA]; FcomID pvFcomTmit; int fcomSyncMode = 0; subRecord *pFbSubRecord[NUM_RAW_FCOM_DATA] = {NULL}; DBADDR dbAddrImax[NUM_RAW_FCOM_DATA]; volatile int fcomBlmTmitTimeouts = 0; volatile int fcomBlmFcomErrors = 0; volatile int fcomBlmPadTimeouts = 0; volatile int fcomBlmProcessed = 0; const char pvNameRaw[NUM_RAW_FCOM_DATA * NUM_SECTIONS][21] = { "BLEN:LI21:265:ARAW", "BLEN:LI21:265:BRAW", "BLEN:LI21:280:ARAW", "BLEN:LI21:280:BRAW", "BLEN:LI24:886:ARAW", "BLEN:LI24:886:BRAW", "BLEN:LI25:883:ARAW", "BLEN:LI25:883:BRAW", "BLEN:LI20:3014:ARAW", "BLEN:LI20:3014:BRAW", "THZR:LI20:3075:CRAW", "THZR:LI20:3075:DRAW" }; const char pvNameImax[NUM_RAW_FCOM_DATA * NUM_SECTIONS][21] = { "BLEN:LI21:265:AIMAX", "BLEN:LI21:265:BIMAX", "BLEN:LI21:280:AIMAX", "BLEN:LI21:280:BIMAX", "BLEN:LI24:886:AIMAX", "BLEN:LI24:886:BIMAX", "BLEN:LI25:883:AIMAX", "BLEN:LI25:883:BIMAX", "BLEN:LI20:3014:AIMAX", "BLEN:LI20:3014:BIMAX", "THZR:LI20:3075:CIMAX", "THZR:LI20:3075:DIMAX" }; const char pvNameTmit[NUM_SECTIONS][20] = { "BPMS:LI21:278:TMIT", /*"BPMS:LI22:301:TMIT", */ "BPMS:LI24:901:TMIT", }; /* Fcom */ FcomGroup fcomGroup = NULL; FcomBlob fcomTxBlob[2]; FcomBlobBlenData_t fcomBlenData[2]; /* data in the "blob" */ /* Estimate system clock frequency */ double sysClkPeriodGet() { epicsTimeStamp then,now; struct timespec slp; int st,i; slp.tv_sec = 0; slp.tv_nsec = 1; /* try a few times if nanosleep() fails */ for ( i=0; i<10; i++ ) { /* wait for next tick boundary */ if ( nanosleep( &slp, 0 ) ) continue; if ( epicsTimeGetCurrent( &then ) ) break; /* nanosleep sleeps for *at least* 'slp' ; in * practice until the next tick boundary */ st = nanosleep( &slp, 0 ); if ( epicsTimeGetCurrent( &now ) ) break; if ( 0 == st ) return epicsTimeDiffInSeconds( &now, &then ); } /* Infinity */ return 1.0/0.0; } /****************************************************************************** * Function: fcomBlmInit * Description: Initialization for fcom. Everything we can do ahead of the time * to prepare for fcom processing. Getting fcomId for PVS, subscribe to * tmit, prepare blobs for sending data, etc * Inputs: * Returns: *****************************************************************************/ int fcomBlmInit(void) { int i; int rc = 0; int st = 0; char *fcomMcPrefix; /* We need a system clock frequency >= ~1kHz so that we can have * sub-fiducial timeouts. */ if ( sysClkPeriodGet() > 1./900. ) { cantProceed("FATAL ERROR: Need a OS system clock frequency >= ~1kHz -- are you using a recent GeSys (rtems)"); } /* find all the fcom ids for this section. The section is set in st.cmd 0=LI21, 1=LI24 , 2=FACET LI20*/ if(gBlenSection < 0 || gBlenSection >= NUM_SECTIONS) { printf("ERR: gBlenSection is set to %d, (it should be 0 for LI21 and 1 for LI24)\n", gBlenSection); printf("---Force the section to LI24 --- \n"); gBlenSection = 1; } if(gBlenSection==2) /* For LI20, in FACET timing system */ blmFidProcessActiveTimeslots = TIMESLOT2_MASK | TIMESLOT5_MASK; else blmFidProcessActiveTimeslots = TIMESLOT1_MASK | TIMESLOT4_MASK; for(i=0; i:$(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 Ret: calculted Ipk Future: -1 when bunch readout or TMIT value is bad ==============================================================================*/ double fbckFcomIpkCalc(subRecord *psub, double bl, float tmit) { double *coeff_p; double Ipk = 0.0; double filter = 0.0; /* filter state, 0 = not in */ double divider = 0.0; /* divider to be used whenthe filter is not in */ coeff_p = &psub->a; /* Note A,B,C,... contiguous in subRecord structure */ filter = psub->j; divider = psub->i; if(blmFcomDebug) { 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. Leave of value too small or negative */ if( tmit < 0.000001 || bl < 0.000001 ) { psub->val = 0.0; return 0; } if (coeff_p[0]!=0.0) { Ipk = coeff_p[0]; if(blmFcomDebug) printf("c1 Ipk=%e\n", Ipk); } if (coeff_p[1]!=0.0) { Ipk = Ipk + coeff_p[1]*pow(tmit,coeff_p[2])*pow(bl,coeff_p[3]); if(blmFcomDebug) printf("c2, c3, c4, Ipk=%e\n", Ipk); } if (coeff_p[4]!=0.0) { Ipk = Ipk + coeff_p[4]*pow(tmit,coeff_p[5])*pow(bl,coeff_p[6]); if(blmFcomDebug) printf("c5, c6, c7 Ipk=%e\n", Ipk); } if (coeff_p[7]!=0.0) { Ipk = Ipk + coeff_p[7] /* take out coeff8 and coeff9 *pow(tmit,coeff_p[8])*pow(bl,coeff_p[9]) */; } /* adjust if filter is not in for filter status */ if (divider != 0.0) if (filter == 0.0) Ipk /= divider; return(Ipk); } /****************************************************************************** * Function: blenCalImax * Description: imax calc function * Inputs: * Returns: *****************************************************************************/ int blenCalImax(int recordIndex, BlmOutNumber raw, float tmit, double *pImax) { subRecord *pSub; pSub = pFbSubRecord[recordIndex]; if(pSub == NULL) { *pImax = 0; return -1; } if (gTmitFromCA) tmit = pSub->h; *pImax = fbckFcomIpkCalc(pSub, raw, tmit ); if ( blmFcomDebug ) printf("pSub=%x, %f, %f, %f, %f, %f, %f, %f, %f, %f, %f, %f, %f\n", (int)pSub, pSub->a, pSub->b, pSub->c, pSub->d,pSub->e, pSub->f, pSub->g, pSub->h, pSub->i, pSub->j, pSub->k, pSub->l); return(0); } /****************************************************************************** * Function: fcomBlmProc * Description: fcom processing, called every beam cycle * Inputs: * Returns: *****************************************************************************/ int blmFcomPutGroupErr = 0; int blmFcomTsDebug=0; /* turn this on to take a snap shot of time stamp */ int blmFcomTs[4]; int tmitMissCount=0; int tmitTsMisCount=0; //#define TMIT_SIM void fcomBlmProc(BlmStation station, FcomBlobRef fcomRxBlob) { int rc; float tmit = 0; int sendGroup; int i, j; epicsTimeStamp epicsTs; epicsTimeStamp epicsTsTmit = {0,0}; epicsTimeStamp tsSum[4]; struct { double aimax,bimax; BlmOutNumber araw,braw; } tmpData[2]; /* do some work first before trying to get tmit to save overall time */ if(fcomSyncMode < 0) goto FCOM_EXIT; /* FCOM not available */ if(fcomGroup == NULL) { rc = fcomAllocGroup(FCOM_ID_ANY, &fcomGroup); if(rc != 0) goto FCOM_EXIT; /* give up. Try again next time */ } epicsMutexMustLock( station->mutex ); fcomBlenData[0].araw = (float)(tmpData[0].araw = station->chnls[0].sum); fcomBlenData[0].braw = (float)(tmpData[0].braw = station->chnls[1].sum); fcomBlenData[1].araw = (float)(tmpData[1].araw = station->chnls[2].sum); fcomBlenData[1].braw = (float)(tmpData[1].braw = station->chnls[3].sum); tsSum[0] = station->chnls[0].tsSum; tsSum[1] = station->chnls[1].tsSum; tsSum[2] = station->chnls[2].tsSum; tsSum[3] = station->chnls[3].tsSum; epicsMutexUnlock( station->mutex ); if ( blmFcomDebug ) { printf("STRM data: %i, %i, %i, %i\n", tmpData[0].araw, tmpData[0].braw, tmpData[1].araw, tmpData[1].braw); } rc = evrTimeGetFromPipeline(&epicsTs, evrTimeActive, 0, 0, 0, 0, 0); #ifndef TMIT_SIM if (!gTmitFromCA) { tmit = FC_STAT_BPM_INVAL == fcomRxBlob->hdr.stat ? 0 : fcomRxBlob->fcbl_bpm_T; epicsTsTmit.secPastEpoch = fcomRxBlob->hdr.tsHi; epicsTsTmit.nsec = fcomRxBlob->hdr.tsLo; } else { epicsTsTmit.secPastEpoch = epicsTs.secPastEpoch; epicsTsTmit.nsec = epicsTs.nsec; /* errlogPrintf("%s line %d in %s, epicsTs.secPastEpoch=%u, epicsTs.nsec=%u\n", __FILE__, __LINE__, __FUNCTION__, epicsTs.secPastEpoch, epicsTs.nsec);*/ } #else ////XXXX TESTING ONLY tmit = 9.40191e+08; epicsTsTmit.secPastEpoch = epicsTs.secPastEpoch; epicsTsTmit.nsec = epicsTs.nsec; ////XXXX #endif if ( blmFcomDebug ) printf("rc = %d, epics time stamp %d, %d\n",rc , epicsTs.secPastEpoch, epicsTs.nsec ); if( blmFcomTsDebug) { /* take one shot of the time stamps */ blmFcomTs[0] = epicsTs.secPastEpoch; blmFcomTs[1] = epicsTs.nsec; blmFcomTs[2] = epicsTsTmit.secPastEpoch; blmFcomTs[3] = epicsTsTmit.nsec; blmFcomTsDebug=0; } if(epicsTs.nsec != epicsTsTmit.nsec) { tmit = 0; /* wrong time stamp, invalidate the number */ tmitTsMisCount++; } /* calculate the imax values, use whatever tmit we have */ sendGroup = 0; j=0; for(i=0; i<2; i++) { blenCalImax(j++, tmpData[i].araw, tmit, &(tmpData[i].aimax) ); blenCalImax(j++, tmpData[i].braw, tmit, &(tmpData[i].bimax) ); fcomBlenData[i].aimax = (float)tmpData[i].aimax; fcomBlenData[i].bimax = (float)tmpData[i].bimax; /* fill in the time stamp */ fcomTxBlob[i].hdr.tsHi = epicsTs.secPastEpoch; fcomTxBlob[i].hdr.tsLo = epicsTs.nsec; /* set the status */ fcomTxBlob[i].hdr.stat = 0; if(fcomBlenData[i].aimax == (float)0.0) fcomTxBlob[i].hdr.stat |= FC_STAT_BLEN_INVAL_AIMAX_MASK; if(fcomBlenData[i].bimax == (float)0.0) fcomTxBlob[i].hdr.stat |= FC_STAT_BLEN_INVAL_BIMAX_MASK; /* send the two blobs out */ rc = fcomAddGroup(fcomGroup, &(fcomTxBlob[i]) ); if ( blmFcomDebug ) printf("fcomBlob number=%d, araw=%f, aimax=%f, braw=%f, bimax=%f\n", i, fcomBlenData[i].araw, fcomBlenData[i].aimax, fcomBlenData[i].braw,fcomBlenData[i].bimax); if(rc!=0) { if ( blmFcomDebug ) errlogPrintf("fcomBlmProc: fcomAddGroup Failed %d (status %d)\n", i, rc); } else sendGroup++; } if(sendGroup) { rc = fcomPutGroup(fcomGroup); if ( rc != 0) { blmFcomPutGroupErr++; if(blmFcomDebug ) errlogPrintf("fcomBlmProc: fcomPutGroup Failed %d\n", rc); } } /* Store results to driver struct */ epicsMutexMustLock( station->mutex ); station->chnls[0].imax = tmpData[0].aimax; station->chnls[1].imax = tmpData[0].bimax; station->chnls[2].imax = tmpData[1].aimax; station->chnls[3].imax = tmpData[1].bimax; station->chnls[0].tsImax = tsSum[0]; station->chnls[1].tsImax = tsSum[1]; station->chnls[2].tsImax = tsSum[2]; station->chnls[3].tsImax = tsSum[3]; /* mark IMAX bits in timedout as 'good' */ station->timedout &= ~BLM_TIMEDOUT_ANY_IMAX; epicsMutexUnlock( station->mutex ); /* allocate the groups for the next time */ fcomGroup = NULL; rc = fcomAllocGroup(FCOM_ID_ANY, &fcomGroup); FCOM_EXIT: return; } /****************************************************************************** * Function: blmFcomShow * Description: Debug print function * Inputs: * Returns: *****************************************************************************/ void blmFcomShow() { int i; for(i=0; i<2; i++) { printf("fcomBlob number=%d, araw=%f, aimax=%f, braw=%f, bimax=%f\n", i, fcomBlenData[i].araw, fcomBlenData[i].aimax, fcomBlenData[i].braw,fcomBlenData[i].bimax); printf("fcomBlob status = %"PRId32"\n", fcomTxBlob[i].hdr.stat); } printf("tmit tim stamp mismatch count = %d\n", tmitTsMisCount); printf("tmit timestamp: %d, %d\n", blmFcomTs[2], blmFcomTs[3]); printf("blen timestamp: %d, %d\n", blmFcomTs[0], blmFcomTs[1]); } void blmFcomTask(void *arg) { BlmStation station = arg; FcomBlobRef tmitBlob = 0; epicsTimeStamp then, now; int st; double remaining, diff; unsigned diffus; while ( 1 ) { fcomReleaseBlob( &tmitBlob ); epicsThreadSuspendSelf(); /* Wait until woken up at fiducial time; * Note that using a semaphore for synchronization * is not necessarily guaranteeing that we wake up * at fiducial time. It could be that for some reason * this thread is still executing when the fiducial ISR * releases the semaphore. Next time we come around * we obtain the semaphore but are not necessarily well * synchronized with fiducial-time. */ epicsTimeGetCurrent( &then ); /* Clear PAD-data arrival signal in case it set here. * This is to make sure we wait for the next shot. */ epicsEventTryWait( station->rawDataReady ); /* Wait for TMIT blob to arrive */ #ifndef TMIT_SIM if(!gTmitFromCA) st = fcomGetBlob( pvFcomTmit, &tmitBlob, blmFcomTimeoutMs ); /* ms */ else st=0; #else ////XXXX FOR TESTING ONLY st=0; ////XXXX #endif /* Now we need to wait for PAD data */ epicsTimeGetCurrent( &now ); diff = epicsTimeDiffInSeconds( &now, &then ); diffus = (unsigned)(diff * 1000000.); if ( diffus > blmFcomMaxTmitDelayUs ) blmFcomMaxTmitDelayUs = diffus; switch ( st ) { case 0: break; case FCOM_ERR_TIMEDOUT: fcomBlmTmitTimeouts++; /* FIXME: HANDLE FCOM TIMEOUT */ continue; default: /* FIXME: Handle other FCOM errors */ fcomBlmFcomErrors++; continue; } if ( (remaining = ((double)blmFcomTimeoutMs/1000.0 - diff)) < 0. ) { remaining = 0.; } st = epicsEventWaitWithTimeout( station->rawDataReady, remaining ); /* Now we need to wait for PAD data */ epicsTimeGetCurrent( &now ); diff = epicsTimeDiffInSeconds( &now, &then ); diffus = (unsigned)(diff * 1000000.); if ( diffus > blmFcomMaxPadDelayUs ) blmFcomMaxPadDelayUs = diffus; if ( st ) { /* FIXME: Handle UDPCOMM timeout */ fcomBlmPadTimeouts++; continue; } /* Everything OK */ fcomBlmProc(station, tmitBlob); post_event(station->epics_event_no); fcomBlmProcessed++; } }