/* $Id: drvBpmStripl.c,v 1.21 2013/03/12 14:42:26 strauman Exp $ */ #define _ISOC99_SOURCE #undef __STRICT_ANSI__ #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #ifndef USE_VECTORIZATION #define USE_VECTORIZATION 1 #endif #define DEBUG_ERR_REPORT 1 int drvBpmStriplDebug = 0; /* obtain last values */ /* Assignment of signals to ADC channels (by AFE wiring) */ #define ADC_MAP_AFE_SIGNAL { RED, YLO, BLU, GRN } /* RED CAL cycle; we look only at blue and green */ #define ADC_MAP_AFE_CAL_RED { -1, -1, BLU, GRN } /* GRN CAL cycle; we look only at red and yellow */ #define ADC_MAP_AFE_CAL_GRN { RED, YLO, -1, -1 } #define IIR128(old, new) ((old)*(1.0-1.0/128.) + (new)/128.) /* have these match the 'kind' so that we can index * the bpmGainRatios arrays either way */ #define GAIN_GRNOVERRED PadDataBpm #define GAIN_GRNOVERBLU PadDataCalRed #define GAIN_REDOVERYLO PadDataCalGrn #define GRN 0 #define RED 1 #define BLU 2 #define YLO 3 #define T_G 4 #define T_R 5 #define T_B 6 #define T_Y 7 #define BPM_AMPL_UDR_GRN (BPM_AMPL_UDR(0)) #define BPM_AMPL_UDR_RED (BPM_AMPL_UDR(1)) #define BPM_AMPL_UDR_BLU (BPM_AMPL_UDR(2)) #define BPM_AMPL_UDR_YLO (BPM_AMPL_UDR(3)) #define BPM_AMPL_OVR_GRN (BPM_AMPL_OVR(0)) #define BPM_AMPL_OVR_RED (BPM_AMPL_OVR(1)) #define BPM_AMPL_OVR_BLU (BPM_AMPL_OVR(2)) #define BPM_AMPL_OVR_YLO (BPM_AMPL_OVR(3)) /* [slowly varying] online calibrator gain ratios */ volatile BpmNumber bpmGainRatios[MAX_BPM][NUM_DATA_KINDS]; volatile BpmNumber bpmGainRatioSlopes[MAX_BPM][NUM_DATA_KINDS]; volatile int bpmGainRatiosOK[MAX_BPM][NUM_DATA_KINDS]; volatile int drvBpmStriplUseVect = USE_VECTORIZATION; uint32_t drvBpmStriplUsePincMask = 0; /* 1st index matches PadDataKind */ static int adcMaps[NUM_DATA_KINDS][PADRPLY_STRM_NCHANNELS] = { ADC_MAP_AFE_SIGNAL, /* PadDataBpm */ ADC_MAP_AFE_CAL_RED, /* PadDataCalRed */ ADC_MAP_AFE_CAL_GRN /* PadDataCalGrn */ }; static uint8_t check_UDR_and_sqrt(BpmNumber *pacc, int *map, BpmNumber limit) { BpmNumber s2; int j,jmapped; uint8_t rval = 0; for ( j = 0; j pacc[map[0]] * Channel 1 -> pacc[map[1]] * ... * If a 'map' entry is < 0 then the associated channel is skipped. * * RETURNS: bitset of over/underrange channels. */ uint8_t drvPadDetectEnergy(BpmNumber *pacc, BpmNumber *ptim, int16_t *p, int *map, int nsamples, int layout_colmajor) { int i,j,x,x2,iinc,jinc,ii,jj; uint8_t clipped; BpmNumber s1, m1, s2, s1t, s2t, xf, N; clipped = 0; if ( layout_colmajor ) { jinc=1; iinc=PADRPLY_STRM_NCHANNELS; } else { jinc=nsamples; iinc=1; } for ( j=jj=0; j sum(n=0..N-1)/N = (N-1)/2; * */ N = (BpmNumber)nsamples; m1 = s1/N; s2 -= m1*s1; s2t = (s2t - 2 * s1t * m1) + m1*s1*(N-1.)/2.; /* TODO: subtract noise pedestal */ /* normalize */ s2t /= s2; pacc[map[j]] = s2; ptim[map[j]] = s2t; } return clipped; } static int dataNoCook(PadReply rply) { int chnl = rply->chnl; volatile BpmRawAmplitudes a = bpmRawAmplitudesAlloc(chnl); int key; /* just pet up-to-date status */ key = epicsInterruptLock(); a->status &= ~BPM_AMPL_TIMEDOUT; bpmGainRatiosOK[chnl][GAIN_GRNOVERRED] &= ~BPM_AMPL_TIMEDOUT; epicsInterruptUnlock(key); bpmRawAmplitudesPut(chnl, a, 0); return 0; } static long drvBpmStriplCalcPos( unsigned c, BpmNumber grn, BpmNumber red, BpmNumber blu, BpmNumber ylo, uint32_t raw_status, unsigned do_pinc, TimeStamp *p_ts ) { BpmNumber u, v, t, s1, s2, pinc; /* Calculate differences over sums */ s1 = (grn + blu); s2 = (red + ylo); u = (grn - blu)/s1/2.; /* grn - blu */ v = (red - ylo)/s2/2.; /* red - ylo */ if ( (raw_status & (BPM_AMPL_UDR_MSK)) ) { /* underflow; fall back to trivial 'tmit' */ t = (grn + blu + ylo + grn)/4.0; } else { /* Turns out that 1 / ( 1/(a+c) + 1/(b+d) ) * is 4th order in r -> better than (a+b+c+d)/4. t = s1*s2/(s1+s2); * However: the simple formula is only correct if * the strips are Pi/2 apart. For (opposite) strips * with other separation angles, the expression: * * 1/tmit = 1/(g+b) + 1/(r+y) + 4 cos(psi) u v (1/g+1/b+1/r+1/y) * * is invariant to rotation about the origin (pure 'R' dependency) * * However: it is easier to just remove the 'R' dependency from * the 'second order' formula 4/tmit = (1/g+1/b+1/r+1/y) */ /* they ask for pincushion correction ? */ if ( do_pinc ) { BpmNumber cpsi = bpmCosPsi[c]; BpmNumber spsi2 = 1 - cpsi*cpsi; BpmNumber r2 = (u*u+v*v-2*u*v*cpsi)/spsi2; /* 'pinc' -> 1+r^2 */ if ( r2 < 1.0E-6 ) { pinc = 1.0 + r2; } else { pinc = (0.5 - sqrtf(0.25 - r2))/r2; } t = 4./(1./grn+1./red+1./blu+1./ylo)*pinc/(2.0-pinc); } else { pinc = 1.0; /* use simple formula */ t = s1*s2/(s1+s2); } } return drvBpmCalcPos(c, u, v, t, raw_status, pinc, p_ts); } /* trivial 'energy' detection algorithm */ static int dataCook(PadReply rply, int nsamples, int layout_cm) { BpmNumber max[PADRPLY_STRM_NCHANNELS]; BpmNumber acc[PADRPLY_STRM_NCHANNELS]; int i; uint8_t clipped; int chnl = rply->chnl; volatile BpmRawAmplitudes a = bpmRawAmplitudesAlloc(chnl); TimeStamp ts; BpmNumber lolim; if (!a) { /* no buffers [???] don't bother working */ errlogPrintf("drvBpmStripl: cook got no pan [out of buffers]\n"); return PAD_UDPCOMM_COOK_STAT_NOSCAN_NOPOST; } try_again: if ( drvBpmStriplUseVect ) { i = vec_detect_energy_4(acc, max, (vShort *)rply->data, adcMaps[PadDataBpm], nsamples, layout_cm); if ( i < 0 ) { errlogPrintf("WARNING - drvBpmStripl: data misaligned - cannot use vectorized algorithm; DISABLING\n"); drvBpmStriplUseVect = 0; goto try_again; } clipped = i; } else { clipped = drvPadDetectEnergy(acc, max, (int16_t*)rply->data, adcMaps[PadDataBpm], nsamples, layout_cm); } lolim = BPM_PARM_UDR_THRESHOLD[chnl]; clipped |= check_UDR_and_sqrt(acc, adcMaps[PadDataBpm], nsamples*lolim*lolim); if ( DIGI_NO_OVFL_SUP(chnl) ) clipped &= ~BPM_AMPL_OVR_MSK; /* Calibration */ /* gainRatios 0 is GRN/BLU -> BLU has to be multiplied * by GRN/BLU, YLO has to be multiplied by RED/YLO which * is gain ration [1] * * Apply the slope (cached value of corresponding AI record's ASLO) * here - don't factor it into the gain ratios because the AI read * routine applies ASLO... */ acc[BLU] *= bpmGainRatios[chnl][GAIN_GRNOVERBLU] * bpmGainRatioSlopes[chnl][GAIN_GRNOVERBLU]; acc[YLO] *= bpmGainRatios[chnl][GAIN_REDOVERYLO] * bpmGainRatioSlopes[chnl][GAIN_REDOVERYLO]; /* We use an invariant property of 'opposite' strips. * 'Opposite' means the the strips have to be opposite * to each other if the 2D domain of the vacuum chamber * is mapped conformally onto a circle. * * In this case, 1/a + 1/b is approximately * * 2/Q/Ggrn (1+r^2)/(1-r^2) * * which is invariant to a rotation about the origin * * hence: (1/c + 1/d) / (1/a+1/b) = Ggrn/Gred */ { BpmNumber r; if ( !clipped ) { int key; r = (1./acc[RED] + 1./acc[YLO])/(1./acc[GRN]+1./acc[BLU]); key = epicsInterruptLock(); r = IIR128(bpmGainRatios[chnl][GAIN_GRNOVERRED], r); bpmGainRatios[chnl][GAIN_GRNOVERRED] = r; epicsInterruptUnlock(key); } else { r = 0.; } bpmGainRatiosOK[chnl][GAIN_GRNOVERRED] |= clipped; bpmGainRatiosOK[chnl][GAIN_GRNOVERRED] &= ~BPM_AMPL_TIMEDOUT; if ( r > 0.5 ) { acc[RED]*=r; acc[YLO]*=r; } } ts.nsec = ntohl(rply->timestampLo); ts.secPastEpoch = ntohl(rply->timestampHi); drvBpmStriplCalcPos( chnl, acc[0], acc[1], acc[2], acc[3], BPM_AMPL_OK | clipped, (drvBpmStriplUsePincMask & (1<vals[GRN] = acc[0]; a->vals[RED] = acc[1]; a->vals[BLU] = acc[2]; a->vals[YLO] = acc[3]; a->timeStamp = ts; a->status = BPM_AMPL_OK | clipped ; a->vals[T_G] = max[0]; a->vals[T_R] = max[1]; a->vals[T_B] = max[2]; a->vals[T_Y] = max[3]; epicsInterruptUnlock(i); bpmRawAmplitudesPut(chnl, a, 0); if ( clipped ) return PAD_UDPCOMM_COOK_STAT_DEBUG_DOSCAN(WAV_BUF_NUM_KINDS - NUM_DATA_KINDS + PadDataBpm); return PAD_UDPCOMM_COOK_STAT_OK; } int padBpmCalCook(PadReply rply, int nsamples, int layout_cm, PadDataKind kind) { uint8_t clipped; BpmNumber max[PADRPLY_STRM_NCHANNELS]; BpmNumber acc[PADRPLY_STRM_NCHANNELS]; BpmNumber calr,ocalr; int i; int chnl = rply->chnl; try_again: if ( drvBpmStriplUseVect ) { i = vec_detect_energy_4(acc, max, (vShort *)rply->data, adcMaps[kind], nsamples, layout_cm); if ( i < 0 ) { errlogPrintf("WARNING - drvBpmStripl: (CAL-)data misaligned - cannot use vectorized algorithm; DISABLING\n"); drvBpmStriplUseVect = 0; goto try_again; } clipped = i; } else { clipped = drvPadDetectEnergy(acc, max, (int16_t*)rply->data, adcMaps[kind], nsamples, layout_cm); } /* Signal under-range if we have less than 64 * noise * contribution. If we have noise of ~2.5 bits then the * integrated energy is 2^5 = 32 * N */ clipped |= check_UDR_and_sqrt(acc, adcMaps[kind], nsamples << 11); if ( DIGI_NO_OVFL_SUP(chnl) ) clipped &= ~BPM_AMPL_OVR_MSK; /* TODO: sanity check on 1st moment */ if ( 0 == clipped ) { ocalr = bpmGainRatios[chnl][kind]; if ( PadDataCalRed == kind ) calr = acc[GRN]/acc[BLU]; else calr = acc[RED]/acc[YLO]; bpmGainRatios[chnl][kind] = IIR128(ocalr, calr); } bpmGainRatiosOK[chnl][kind] |= clipped; bpmGainRatiosOK[chnl][kind] &= ~BPM_AMPL_TIMEDOUT; if ( clipped ) return PAD_UDPCOMM_COOK_STAT_DEBUG_DOSCAN(WAV_BUF_NUM_KINDS - NUM_DATA_KINDS + kind); return PAD_UDPCOMM_COOK_STAT_OK; } int padBpmDataCook(PadReply rply, int nsamples, int layout_cm) { if ( fidHasBeam() || drvPadUdpCommGetSimMode(rply->chnl) ) { return dataCook(rply, nsamples, layout_cm); } dataNoCook(rply); /* Return positive value to indicate that we received a trigger */ return PAD_UDPCOMM_COOK_STAT_NOSCAN; } static int striplCook(PadReply rply, int nsamples, int layout_cm, int kind, void *closure) { int rval = PAD_UDPCOMM_COOK_STAT_NOSCAN_NOPOST; switch (kind) { default: break; case PadDataBpm: rval = padBpmDataCook(rply, nsamples, layout_cm); break; case PadDataCalRed: case PadDataCalGrn: rval = padBpmCalCook(rply, nsamples, layout_cm, kind); break; } DIGI_OVFL_SUP_TEST( rply ); return rval; } static void striplWatchdog(int channel, int kind, void *closure) { int key; if ( channel < 0 || channel >= MAX_BPM ) return; if ( kind < 0 || kind >= NUM_DATA_KINDS ) return; key = epicsInterruptLock(); bpmWatchdog(channel, kind, closure); bpmGainRatiosOK[channel][kind] |= BPM_AMPL_TIMEDOUT; epicsInterruptUnlock(key); if ( (DEBUG_ERR_REPORT & drvBpmStriplDebug) ) epicsPrintf("Stripline Watchdog (%i/%i)\n", channel, kind); } /* * Stripline response to beam position (x,y) [normalized to BPM radius]; * angular coverage of stripline '2*del' and stripline located at angle 'fi0' * (measured from x-axis). */ static BpmNumber cbpm(BpmNumber x, BpmNumber y, BpmNumber fi0, BpmNumber del) { BpmNumber sdel, cdel, xx, r2; r2 = x*x + y*y; if ( del != 0. ) { cdel = cosf(del); sdel = sinf(del); } else { sdel = 1.0; cdel = 1.0; } xx = (1-r2)*sdel / ((1+r2)*cdel - 2 * ( x * cos(fi0) + y * sin(fi0) ) ); return ( del != 0. ? 2*atanf( xx) : xx ) / 2. / PI; } static int adcRMap(idx) { int i; /* reverse map lookup - not efficient but who cares... */ for ( i=0; i 1.0 ) { x=x/r; y=y/r; } vals_p[adcRMap(GRN)] = cbpm(x, y, -phi*RAD , w*PI)*q; vals_p[adcRMap(RED)] = cbpm(x, y, (-phi+psi)*RAD , w*PI)*q; vals_p[adcRMap(BLU)] = cbpm(x, y, -phi*RAD+PI , w*PI)*q; vals_p[adcRMap(YLO)] = cbpm(x, y, (-phi+psi)*RAD+PI, w*PI)*q; return 0; } static int striplInit(DrvPadUdpCommCallbacks cbs) { #if defined(__PPC__) || defined(__powerpc__) || defined(__POWERPC__) vec_task_enable(); altivec_nonC9xmode(); epicsPrintf("Using vectorized code\n"); #endif return 0; } static DrvPadUdpCommCallbacksRec striplCb = { init: striplInit, cookClosure: 0, cook: striplCook, watchdogClosure: 0, watchdog: striplWatchdog, watchKinds: (1<aslo == 0. ? 1. : pai->aslo; /* Supply them with the value */ pai->val = bpmGainRatios[c][s]; st = bpmGainRatiosOK[c][s]; /* filter status bits depending on cal. mode */ switch ( s ) { default: case GAIN_GRNOVERRED: umsk = BPM_AMPL_UDR_MSK; omsk = BPM_AMPL_OVR_MSK; break; case GAIN_GRNOVERBLU: umsk = BPM_AMPL_UDR_GRN | BPM_AMPL_UDR_BLU; omsk = BPM_AMPL_OVR_GRN | BPM_AMPL_OVR_BLU; break; case GAIN_REDOVERYLO: umsk = BPM_AMPL_UDR_RED | BPM_AMPL_UDR_YLO; omsk = BPM_AMPL_OVR_RED | BPM_AMPL_OVR_YLO; break; } key = epicsInterruptLock(); bpmGainRatiosOK[c][s] = BPM_AMPL_TIMEDOUT; epicsInterruptUnlock(key); err = drvBpm_map_status(st, omsk, umsk); epicsTimeGetCurrent(&now); if ( (DEBUG_ERR_REPORT & drvBpmStriplDebug) && (st & BPM_AMPL_RNG_MSK(-1)) ) { epicsPrintf("ERROR on %i/%i: 0x%08"PRIx32"\n",c,s,st); epicsPrintf("Now: %us, Last Processing %us\n", now.secPastEpoch, pai->time.secPastEpoch); } } else { return -1; } return err; } long drvBpmStripl_read_raw_ai(aiRecord *pai) { return drvBpm_read_raw_ai_helper(pai, stripl_cb); } static long bpmStriplReport(int level) { epicsPrintf("Stripline BPM Support\n"); return 0; } long bpmStriplInit(void) { int i; for ( i=0; i