/* Copyright 1996 ** by ** The Board of Trustees of the ** Leland Stanford Junior University. ** All rights reserved. ** ** ** Work supported by the U.S. Department of Energy under contract ** DE-AC03-76SF00515. ** ** Disclaimer Notice ** ** The items furnished herewith were developed under the sponsorship ** of the U.S. Government. Neither the U.S., nor the U.S. D.O.E., nor the ** Leland Stanford Junior University, nor their employees, makes any war- ** ranty, express or implied, or assumes any liability or responsibility ** for accuracy, completeness or usefulness of any information, apparatus, ** product or process disclosed, or represents that its use will not in- ** fringe privately-owned rights. Mention of any product, its manufactur- ** er, or suppliers shall not, nor is it intended to, imply approval, dis- ** approval, or fitness for any particular use. The U.S. and the Univer- ** sity at all times retain the right to use and disseminate the furnished ** items for any purpose whatsoever. Notice 91 02 01 */ /*============================================================================= Abs: Pulse Signal Monitoring Calculations. Name: subpsm.c subpsmWfm - Convert Waveform to Volts and Eng Units subpsmWfmProcInit- Initialize Waveform Processing subpsmWfmProc - Process Eng Units Waveform subpsmWfmProcCoeffInit - Init Coefficients for above 2 routines subpsmTime - Time of a Selected Index subpsmTimeArray - Fill in Waveform for Time Range X subpsmAverageProc- Select Parameter Value and Sum for Time Average subpsmAverage - Time Average and Standard Deviation subpsmAverageRun - Waveform Time Average and Standard Deviation subpsmIQ - Amplitude and Phase to I and Q Calculation subpsmCharge - Beam Charge Calculations subpsmChargeSum - Beam Charge Summation init_record_wf - Initialize for Waveform of Parameters read_wf - Process Waveform of Parameters write_wf_data - Write a Parameter into the Waveform Proto: not required and not used. 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. For the calculation routines, the record status (STAT) is set to SOFT_ALARM (unless it is already set to LINK_ALARM due to severity maximization) and the severity (SEVR) is set to psub->brsv (BRSV - set by the user in the database though it is expected to be invalid). Auth: 23-Jun-2006, Stephanie Allison Rev: DD-MMM-YYYY, Reviewer's Name (.NE. Author's Name) ------------------------------------------------------------------------------- Mod: 14-Oct-2019, K. Kim (KHKIM): replace typed_rset with rset 14-Oct-2019, K. Luchini (LUCHINI): struc rset in recSub.h was chagned to typed_rset =============================================================================*/ #include /* memset prototype */ #include /* calloc, free prototypes */ #include /* sqrt prototype */ #include /* for DBADDR */ #include /* for post_event */ #include /* for recGblRecordError */ #include /* for struct typed_rset */ #include /* for struct dset */ #include /* for struct subRecord */ #include /* for struct sSubRecord */ #include /* for struct waveformRecord */ #include /* for epicsRegisterFunction */ #include /* for iocsh* */ #include /* for epicsRegisterFunction */ #include /* for epicsTimeStamp */ #include /* for epicsMutex* */ #define SIN_PI 3.14159265358979 #define SIN_DIM 3 // mdunning 8/3/17: define macro dbGetPdbAddrFromLink below, due to its removal in base 3.15 #define dbGetPdbAddrFromLink(PLNK) \ ( ( (PLNK)->type != DB_LINK ) \ ? 0 \ : ( ( (struct dbAddr *)( (PLNK)->value.pv_link.pvt) ) ) ) typedef struct { double nord; double ncycle; double *cosPhi; double *sinPhi; double cR[SIN_DIM*SIN_DIM]; double cI[SIN_DIM*SIN_DIM]; double rad2deg; double phiFactor; } subpsmSinStruct; #define PSM_PARAM_PER_WAVEFORM 20 #define PSM_WAVEFORM_PER_DGTZR 8 #define PSM_DIGITIZER_PER_IOC 12 #define PSM_PARAM_MAX (PSM_PARAM_PER_WAVEFORM * PSM_WAVEFORM_PER_DGTZR * PSM_DIGITIZER_PER_IOC) #define PSM_NAN ((double)0./(double)0.) typedef struct { epicsMutexId mutex; long empty; double data[PSM_PARAM_MAX]; unsigned long doneMask[PSM_PARAM_MAX/32]; epicsTimeStamp time; } psmWfmBufferStruct; static psmWfmBufferStruct *psmWfmBuffer = 0; static long init_record_wf(waveformRecord *pwf) { if (INST_IO != pwf->inp.type) { recGblRecordError(S_db_badField, (void*)pwf, "devpsmWf (init_record) Illegal INP field"); return S_db_badField; } if ((pwf->nelm < (PSM_PARAM_PER_WAVEFORM+2)) || (DBF_DOUBLE != pwf->ftvl)) { recGblRecordError(S_db_badField, (void*)pwf, "devpsmWf (init_record) Illegal NELM or FTVL field"); return S_db_badField; } if (psmWfmBuffer) { recGblRecordError(S_db_onlyOne, (void*)pwf, "psmWf (init_record) Only one record allowed per IOC"); return S_db_onlyOne; } assert(psmWfmBuffer = calloc(1, sizeof(psmWfmBufferStruct))); psmWfmBuffer->mutex = epicsMutexMustCreate(); psmWfmBuffer->empty = 1; return 0; } static long read_wf(waveformRecord *pwf) { long idx, midx, dataidx; if (pwf->nelm < (PSM_PARAM_MAX+2)) pwf->nord = pwf->nelm - 2; else pwf->nord = PSM_PARAM_MAX; epicsMutexLock(psmWfmBuffer->mutex); /* Copy data into buffer if it exists, NAN if it doesn't. Reset masks for next time. */ for (idx = 0; idx < pwf->nord; idx++) { midx = idx/32; dataidx = idx%32; if (psmWfmBuffer->doneMask[midx] & (1 << dataidx)) *(((double *)pwf->bptr) + idx) = psmWfmBuffer->data[idx]; else *(((double *)pwf->bptr) + idx) = PSM_NAN; } psmWfmBuffer->empty = 1; memset(psmWfmBuffer->doneMask, 0, sizeof(psmWfmBuffer->doneMask)); /* Add timestamp to the end */ pwf->nord += 2; *(((double *)pwf->bptr) + idx) = (double)psmWfmBuffer->time.secPastEpoch; idx++; *(((double *)pwf->bptr) + idx) = (double)psmWfmBuffer->time.nsec; epicsMutexUnlock(psmWfmBuffer->mutex); return 0; } static long write_wf_data(double timeDiffMax, long idx, double value, epicsTimeStamp *valueTime_p) { long midx, dataidx; if ((!psmWfmBuffer) || (idx >= PSM_PARAM_MAX) || (idx < 0)) return -1; midx = idx/32; dataidx = idx%32; epicsMutexLock(psmWfmBuffer->mutex); /* Clear buffer if we are on the next time slot or the buffer has been read. */ if (epicsTimeDiffInSeconds(valueTime_p, &(psmWfmBuffer->time)) > timeDiffMax) psmWfmBuffer->empty = 1; if (psmWfmBuffer->empty) { psmWfmBuffer->empty = 0; psmWfmBuffer->time = *valueTime_p; memset(psmWfmBuffer->doneMask, 0, sizeof(psmWfmBuffer->doneMask)); } /* Write data - set bit that it's ready. */ psmWfmBuffer->data[idx] = value; psmWfmBuffer->doneMask[midx] |= (1 << dataidx); epicsMutexUnlock(psmWfmBuffer->mutex); return 0; } static long init_record_wftime(waveformRecord *pwf) { if (INST_IO != pwf->inp.type) { recGblRecordError(S_db_badField, (void*)pwf, "devpsmWfTime (init_record) Illegal INP field"); return S_db_badField; } if ((pwf->nelm < 2) || (DBF_ULONG != pwf->ftvl)) { recGblRecordError(S_db_badField, (void*)pwf, "devpsmWfTime (init_record) Illegal NELM or FTVL field"); return S_db_badField; } return 0; } static long read_wftime(waveformRecord *pwf) { pwf->nord = 2; recGblGetTimeStamp(pwf); *( (epicsUInt32 *)pwf->bptr) = pwf->time.secPastEpoch; *(((epicsUInt32 *)pwf->bptr) + 1) = pwf->time.nsec; return 0; } typedef struct { long number; DEVSUPFUN report; DEVSUPFUN init; DEVSUPFUN init_record_wf; DEVSUPFUN get_ioint_info_wf; DEVSUPFUN read_wf; } devpsmWf_ts; devpsmWf_ts devpsmWf = { 5, 0, 0, init_record_wf, 0, read_wf }; devpsmWf_ts devpsmWftime = { 5, 0, 0, init_record_wftime, 0, read_wftime }; epicsExportAddress(dset, devpsmWf); epicsExportAddress(dset, devpsmWftime); long subpsmWfm(struct subRecord *psub) { /* * Convert input waveform from counts to voltage. Also, provide a * waveform with operating limits. * Convert voltage waveform from voltage to EGU using a polynomial. * John Schmerge writes: * The input to the ADC is the channel input voltage minus the offset. * So a positive signal should use a 1V(10012), 1.1V(10014) or 2V(812) * offset to maximize the range of the digitizer and a negative signal a * -1V(10012), -1.1V(10014), or -2V(812) offset. Thus the real conversion * should be: * * V = (Voltage Range)*ADC/(Counts Range) - Full Scale Voltage + Voffset * * This way the voltage will not be affected by the offset since both the * ADC counts will decrease and the offset will increase to cancel the * change. * * Inputs: * A = Polynomial coefficient 0 (a0, offset if linear) * B = Polynomial coefficient 1 (a1, slope if linear) * C = Polynomial coefficient 2 (a2) * D = Polynomial coefficient 3 (a3) * E = Spare * F = Count-to-Volt Divisor (count/V), * (2048 for 12 bits, 2.0V range, VTR10012) * (8192 for 14 bits, 2.2V range, VTR10014) * (1024 for 12 bits, 4.0V range, VTR812) * (32768 for 16 bits, 6.0V range, SIS3302) * G = Calibrated Voltage Offset (V) * H = Full Scale Voltage/2 (V) * (1.0V, VTR10012) * (1.1V, VTR10014) * (2.0V, VTR812) * (2.5V, SIS3302) * INPI = Pointer to count array * * Outputs: * INPJ = Pointer to Operating limits array * INPK = Pointer to voltage array * INPL = Pointer to EGU array (optional) * VAL = Number of Values in the Array */ DBADDR *cntAddr = dbGetPdbAddrFromLink(&psub->inpi); DBADDR *vltAddr = dbGetPdbAddrFromLink(&psub->inpk); DBADDR *eguAddr = dbGetPdbAddrFromLink(&psub->inpl); short *cntP; double *vltP; double *eguP; double *polyP; double yval[2]; double voltOffset = psub->g - psub->h; long cntNord, vltNord, eguNord, offset; rset *prset; /* * inpi, inpj, inpk and inpl must be DB links and must be the proper type. */ if ((!cntAddr) || (!vltAddr) || (!eguAddr)) return -1; dbScanLock(cntAddr->precord); if ((cntAddr->field_type == DBF_SHORT) && ((prset = dbGetRset(cntAddr)))) { cntP = (short *)cntAddr->pfield; if (!cntP) cntNord = 0; else (*prset->get_array_info)(cntAddr, &cntNord, &offset); } else { cntP = 0; cntNord = 0; } dbScanLock(vltAddr->precord); if ((vltAddr->field_type == DBF_DOUBLE) && ((prset = dbGetRset(vltAddr)))) { vltP = (double *)vltAddr->pfield; if ((!vltP) || (vltAddr->no_elements < cntNord)) cntNord = 0; (*prset->get_array_info)(vltAddr, &vltNord, &offset); if (vltP && (vltNord > cntNord)) memset(vltP, 0, sizeof(double) * vltNord); (*prset->put_array_info)(vltAddr, cntNord); } else { vltP = 0; cntNord = 0; } dbScanLock(eguAddr->precord); if ((eguAddr->field_type == DBF_DOUBLE) && ((prset = dbGetRset(eguAddr)))) { eguP = (double *)eguAddr->pfield; if ((!eguP) || (eguAddr->no_elements < cntNord)) cntNord = 0; (*prset->get_array_info)(eguAddr, &eguNord, &offset); if (eguP && (eguNord > cntNord)) memset(eguP, 0, sizeof(double) * eguNord); (*prset->put_array_info)(eguAddr, cntNord); } else { eguP = 0; cntNord = 0; } psub->val = (double)cntNord; yval[0] = -1E20; yval[1] = 1E20; for (; cntNord > 0; cntNord--, cntP++, vltP++, eguP++) { *vltP = (*cntP/psub->f) + voltOffset; *eguP = 0.0; for (polyP = &psub->d, offset = 4; offset > 0; polyP--, offset--) { *eguP = (*eguP * *vltP) + *polyP; } if (*eguP > yval[0]) yval[0] = *eguP; if (*eguP < yval[1]) yval[1] = *eguP; } dbScanUnlock(cntAddr->precord); dbScanUnlock(vltAddr->precord); dbScanUnlock(eguAddr->precord); if (psub->val > 0.0) { dbPutLink(&psub->inpj, DBR_DOUBLE, yval, 2); return 0; } return -1; } /* * Initialization for Sinusoidal Signal Processing taken from * signalProcessing.c copyright 2005 JJSebek SSRL/SLAC * Jim Sebek writes: * signalProcessing contains code for the signal processing algorithm to * recover the DC and 10Hz components from the Danfysik signal monitoring * the output of the White Circuit. * The assumption of the algorithm is that the signal is composed of primarily * two frequencies, DC and 10Hz. It calculates the projection of these two * tones then inverts a matrix to obtain the offset (DC component), 10Hz * amplitude, and 10Hz phase. */ static long subpsmWfmProcCoeffInit(double nord, double ncycle, subpsmSinStruct *psin) { long idx; double phi; double cos1; double sin1; double cos2; double sin2; double f1; double f2; double det; if ((nord < 2) || (ncycle < 2)) return -1; if (nord > psin->nord) { if (psin->cosPhi) free(psin->cosPhi); if (psin->sinPhi) free(psin->sinPhi); psin->nord = 0; psin->cosPhi = calloc(nord, sizeof(double)); psin->sinPhi = calloc(nord, sizeof(double)); if ((!psin->cosPhi) || (!psin->sinPhi)) return -1; } psin->nord = nord; psin->ncycle = ncycle; psin->rad2deg = 180.0/SIN_PI; psin->phiFactor = 2*SIN_PI/ncycle; /* * Set cosine and sine arrays. */ for (idx = 0; idx < nord; idx++) { psin->cosPhi[idx]=cos(idx*psin->phiFactor); psin->sinPhi[idx]=sin(idx*psin->phiFactor); } phi=SIN_PI*(nord-1)/ncycle; cos1=cos(phi); sin1=sin(phi); cos2=cos(2*phi); sin2=sin(2*phi); f1=sin(SIN_PI*nord/ncycle)/(nord*sin(SIN_PI/ncycle)); f2=sin(2*SIN_PI*nord/ncycle)/(nord*sin(2*SIN_PI/ncycle)); det=nord*(1 + (2*f1*f1*f2) - (f2*f2) - (2*f1*f1)); psin->cR[0*SIN_DIM + 0]=((1-f1*f1)+cos2*(f1*f1-f2))/det; psin->cR[0*SIN_DIM + 1]=cos1*f1*(f2-1)/det; psin->cR[0*SIN_DIM + 2]=sin2*(f1*f1-f2)/det; psin->cR[1*SIN_DIM + 0]=2*cos1*f1*(f2-1)/det; psin->cR[1*SIN_DIM + 1]=(1-f2*f2)/det; psin->cR[1*SIN_DIM + 2]=2*sin1*f1*(f2-1)/det; psin->cR[2*SIN_DIM + 0]=((1-f1*f1)+cos2*(f1*f1-f2))/det; psin->cR[2*SIN_DIM + 1]=cos1*f1*(f2-1)/det; psin->cR[2*SIN_DIM + 2]=sin2*(f1*f1-f2)/det; psin->cI[0*SIN_DIM + 0]=-sin2*(f1*f1-f2)/det; psin->cI[0*SIN_DIM + 1]=-sin1*f1*(f2-1)/det; psin->cI[0*SIN_DIM + 2]=-((1-f1*f1)-cos2*(f1*f1-f2))/det; psin->cI[1*SIN_DIM + 0]=0.; psin->cI[1*SIN_DIM + 1]=0.; psin->cI[1*SIN_DIM + 2]=0.; psin->cI[2*SIN_DIM + 0]=-psin->cI[0*SIN_DIM + 0]; psin->cI[2*SIN_DIM + 1]=-psin->cI[0*SIN_DIM + 1]; psin->cI[2*SIN_DIM + 2]=-psin->cI[0*SIN_DIM + 2]; return 0; } long subpsmWfmProcInit(struct sSubRecord *psub) { /* * Initialize space used by sinusoidal processing. */ psub->dpvt = calloc(1, sizeof(subpsmSinStruct)); if (!psub->dpvt) return -1; if (subpsmWfmProcCoeffInit(psub->p, psub->p, (subpsmSinStruct *)psub->dpvt)) return -1; return 0; } long subpsmWfmProc(struct sSubRecord *psub) { /* * Calculate various parameters from the converted waveform above. Use * start and stop index entered by user. The equations come from: * http://www.slac.stanford.edu/~spear/epics/app/injector/2007_Run_PSM.xls * by John Schmerge. * Optional sinusoidal processing is also done. The equations come from: * http://www.slac.stanford.edu/~spear/epics/app/injector/whiteCircuitSignalProcessing.pdf * by Jim Sebek. * * Inputs: * INPA = Pointer to volt or EGU array * B = One index into the above array (0 to max data pts) * C = Another index into the above array (0 to max data pts) * D = Delta time between values (usec or msec) * E = Integral Conversion * N = Number of Samples for Pulse Amplitude calculation * O = Chi-Squared Maximum (0 = no sinusoidal processing) * P = Number of clock cycles in fundamental period * (~2900 or ~1000 for Booster Waveforms) * Y = Threshold on pulse amplitude at the centroid. * Outputs: * F = Minimum * G = Maximum * H = Integral * I = Sum * J, K set to zero if centroid cannot be determined or pulse amplitude * is too low: * J = Time Centroid (usec or msec) * K = Time Width (usec or msec) * L = Average * M = Standard Deviation on Average * Q and R set to zero when the fit is bad: * Q = Sine Wave DC Offset * R = Sine Wave Amplitude * S is only updated when there is a good fit: * S = Sine Wave Phase (degrees) * T = Chi-Squared distribution on the sine wave fit * Set to INPO when bad fit. * U,V,W set to zero if centroid cannot be determined or pulse amplitude * is too low: * U = Leading Edge * V = Trailing Edge * W = Pulse Amplitude at the Centroid * X = RMS of Average * Z = Last value in the selected range * VAL = Number of values used in above calcs */ DBADDR *inpAddr = dbGetPdbAddrFromLink(&psub->inpa); subpsmSinStruct *psin = (subpsmSinStruct *)psub->dpvt; double *inpP, *valP; double y[SIN_DIM]; double yR[SIN_DIM]; double yI[SIN_DIM]; double calc, phase; double centroid, timeWidth, leadingEdge, trailingEdge; long inpNord, nord, offset, idx, jdx, status, doCentroid; long nsample, ninitial, nfinal, ncentroid, nAmpl; rset *prset; /* * inpa must be a DB link and must be the proper type. */ if ((!inpAddr) || (!psin)) return -1; dbScanLock(inpAddr->precord); if ((inpAddr->field_type == DBF_DOUBLE) && ((prset = dbGetRset(inpAddr)))) { inpP = (double *)inpAddr->pfield; if (!inpP) inpNord = 0; else (*prset->get_array_info)(inpAddr, &inpNord, &offset); } else { inpP = 0; inpNord = 0; } /* * Check if the specified range is invalid or there are no values * in the specified range. */ if (psub->b > psub->c) { ninitial = psub->c + 0.1; nfinal = psub->b + 0.1; } else { ninitial = psub->b + 0.1; nfinal = psub->c + 0.1; } if (ninitial < 0 ) ninitial = 0; if (nfinal >= inpNord) nfinal = inpNord - 1; nord = nfinal - ninitial + 1; if (nord < 2) { dbScanUnlock(inpAddr->precord); return -1; } /* * Initialize processed values. */ psub->z = inpP[nfinal]; /* last value in the range */ psub->f = inpP[ninitial]; /* minimum */ psub->g = psub->f; /* maximum */ psub->h = (psub->f + inpP[nfinal])/2; /* integration */ psub->i = 0.0; /* sum(data) */ centroid = 0.0; /* sum(offset*data) */ timeWidth = 0.0; /* sum(offset*offset*data) */ psub->m = 0.0; /* sum(data*data) */ psub->w = 0.0; /* sum(data at centroid) */ psub->val = (double)nord; /* # points */ /* * Initialize sinusoidal processing arrays used to find projections on DC * and 10Hz vectors. Some initialization only * needs to be done when the number of values or the number of cycles * has changed. */ if (psub->o > 0.5) { if ((psub->val != psin->nord) || (psub->p != psin->ncycle)) { status = (double)subpsmWfmProcCoeffInit(psub->val, psub->p, psin); } else { status = 0; } for(idx = 0; idx < SIN_DIM; idx++) { y[idx] = 0.0; yR[idx] = 0.0; yI[idx] = 0.0; } } else { status = -1; } /* * Go through each data point and update min, max, and sums */ for (idx = 0, jdx = 1, valP = inpP + ninitial; idx < nord; idx++, jdx++, valP++) { if (*valP < psub->f) psub->f = *valP; /* minimum */ if (*valP > psub->g) psub->g = *valP; /* maximum */ calc = jdx * *valP; /* intermediate value */ psub->i += *valP; /* sum(data) */ centroid += calc; /* sum(offset*data) */ timeWidth += jdx * calc; /* sum(offset*offset*data) */ psub->m += *valP * *valP; /* sum(data*data) */ if (!status) { y[0] += psin->cosPhi[idx] * *valP; y[2] += psin->sinPhi[idx] * *valP; } } /* * Finish sinusoidal processing - invert projections to obtain amplitude * and phase vector */ if (!status) { y[1] = psub->i; for (idx = 0; idx < SIN_DIM; idx++) { for (jdx = 0; jdx < SIN_DIM; jdx++) { yR[idx] += psin->cR[(idx*SIN_DIM) + jdx] * y[jdx]; yI[idx] += psin->cI[(idx*SIN_DIM) + jdx] * y[jdx]; } } /* * extract DC and phase/ampl information from vector. */ calc = (yR[0]*yR[2]) - (yI[0]*yI[2]); psub->q = yR[1]; /* DC Offset */ if (calc >= 0.0) { psub->r = 2.0 * sqrt(calc); /* Ampl */ phase = atan2(yI[0], yR[0]); /* Phase in radians */ psub->t = 0.0; /* Initialize chi-squared distribution */ } else { status = -1; phase = 0; } } else { psub->q = psub->r = psub->s = psub->t = phase = 0.0; } /* * Post processing - find chi-squared on sine wave fit, leading and * trailing edges, and pulse amplitude at the centroid. */ nsample = psub->n + 0.1; if ((psub->i < 0) && (psub->i > -1E-15)) psub->i = -1E-15; else if ((psub->i >= 0) && (psub->i < +1E-15)) psub->i = +1E-15; centroid /= psub->i; ncentroid = centroid - 0.5; /* integer centroid idx */ if ((ncentroid > 0) && (ncentroid < (nord-1))) doCentroid = 1; else doCentroid = 0; leadingEdge = 0.0; trailingEdge = 0.0; nAmpl = 0; for (idx = 0, valP = inpP + ninitial; idx < nord; idx++, valP++) { if (doCentroid) { if (idx > 0) { if (idx <= ncentroid) leadingEdge += *valP; else if (idx > ncentroid) trailingEdge += *valP; } if ((idx >= (ncentroid - nsample)) && (idx <= (ncentroid + nsample))) { psub->w += *valP; nAmpl++; } } if (!status) { calc = phase + (psin->phiFactor*idx); calc = psub->q + (psub->r * cos(calc)); calc = *valP - calc; psub->t += calc * calc; } } /* Leading and trailing edges */ if (doCentroid) { leadingEdge = ((ncentroid+1) * inpP[ncentroid+ninitial]) - inpP[ninitial] - leadingEdge; calc = inpP[ncentroid+ninitial] - inpP[ninitial]; if ((calc < 0) && (calc > -1E-15)) calc = -1E-15; else if ((calc >= 0) && (calc < +1E-15)) calc = +1E-15; leadingEdge /= calc; trailingEdge = (nord * inpP[nfinal]) - ((ncentroid+1) * inpP[ncentroid+ninitial]) - trailingEdge; calc = inpP[nfinal] - inpP[ncentroid+ninitial]; if ((calc < 0) && (calc > -1E-15)) calc = -1E-15; else if ((calc >= 0) && (calc < +1E-15)) calc = +1E-15; trailingEdge /= calc; if (nAmpl > 0) psub->w /= nAmpl; /* Amplitude around centroid */ else psub->w = 0.0; } dbScanUnlock(inpAddr->precord); /* * Divide resultant sums and convert to desired units. */ psub->h = (psub->i - psub->h) * psub->d * psub->e; /* integration */ psub->l = psub->i/psub->val; /* average */ psub->x = sqrt(psub->m/psub->val); /* average RMS */ psub->m = psub->m - ((psub->i * psub->i)/psub->val); if (psub->m > 0) /* standard deviation */ psub->m = sqrt(psub->m/(psub->val - 1.0)); else psub->m = 0.0; /* Only update centroid values if amplitude is above threshold */ if (doCentroid && (((psub->y > 0) && (psub->w > psub->y)) || ((psub->y < 0) && (psub->w < psub->y)) || (psub->y == 0))) { psub->k = timeWidth/psub->i; /* divide by sum */ psub->k -= centroid * centroid; /* intermediate */ if (psub->k >= 0.0) psub->k = sqrt(psub->k); /* time width (# samples) */ else psub->k = 0.0; psub->j = centroid + ninitial - 1; /* centroid - adjusted */ psub->u = leadingEdge + ninitial; /* leading edge - adjusted */ psub->v = trailingEdge + ninitial; /* trailing edge - adjusted */ if (psub->j < ninitial) psub->j = ninitial; else if (psub->j > nfinal ) psub->j = nfinal; if (psub->u < ninitial) psub->u = ninitial; else if (psub->u > nfinal ) psub->u = nfinal; if (psub->v < ninitial) psub->v = ninitial; else if (psub->v > nfinal ) psub->v = nfinal; psub->k *= psub->d; /* time width (time units)*/ psub->j *= psub->d; /* centroid (time units)*/ psub->u *= psub->d; /* leading edge (time units)*/ psub->v *= psub->d; /* trailing edge (time units)*/ } else { psub->j = 0.0; psub->k = 0.0; psub->u = 0.0; psub->v = 0.0; psub->w = 0.0; } if (!status) { if (psub->r < +1E-15) psub->r = +1E-15; /* clamp small value */ psub->t /= psub->r * psub->r; /* chi-squared distribution */ if (psub->t >= psub->o) { /* fit no good, set zeros */ psub->r = 0.0; psub->t = psub->o; status = -1; } else { psub->s = phase * psin->rad2deg; /* phase in degrees */ } } else { psub->r = 0.0; psub->t = psub->o; } return 0; } long subpsmWfmProcTest(struct subRecord *psub) { /* * Fill in a waveform used to test subpsmWfmProc. * This test data yields: * AC = 717.5061, DC = 2524.2963, Phase = -2.6315 radians (-150.7744 deg) * * Inputs: None * * Outputs: * INPL = Pointer to test array * VAL = Number of Values in the Array */ DBADDR *inpAddr = dbGetPdbAddrFromLink(&psub->inpl); double *inpP; long idx; static long nord = 1024; rset *prset; static double testData[1024] = {1899,1899,1899,1899,1901,1901,1901,1903,1903,1903, 1905,1905,1906,1907,1909,1908,1909,1910,1912,1913, 1912,1914,1914,1915,1916,1917,1917,1918,1919,1920, 1922,1922,1924,1923,1925,1925,1926,1927,1927,1929, 1929,1931,1930,1933,1935,1936,1936,1937,1939,1938, 1939,1940,1944,1943,1943,1945,1945,1947,1948,1949, 1951,1951,1952,1953,1953,1954,1954,1956,1958,1959, 1958,1960,1962,1961,1963,1963,1964,1966,1967,1967, 1970,1969,1971,1973,1973,1973,1975,1976,1976,1978, 1978,1980,1982,1982,1983,1984,1984,1985,1986,1987, 1989,1989,1991,1993,1993,1995,1995,1996,1997,1998, 1999,2000,2001,2003,2004,2004,2006,2007,2009,2009, 2009,2011,2011,2013,2015,2015,2017,2018,2019,2020, 2022,2022,2022,2024,2025,2026,2027,2028,2029,2031, 2033,2033,2034,2035,2035,2036,2040,2040,2041,2043, 2044,2044,2047,2047,2048,2049,2050,2051,2053,2053, 2055,2056,2057,2059,2060,2061,2061,2063,2064,2066, 2066,2067,2069,2070,2072,2072,2074,2075,2076,2078, 2079,2080,2082,2082,2083,2085,2086,2088,2089,2090, 2091,2092,2094,2095,2096,2097,2098,2100,2101,2103, 2104,2105,2107,2106,2107,2110,2111,2112,2114,2115, 2117,2117,2119,2120,2121,2122,2124,2124,2126,2127, 2129,2130,2131,2133,2135,2135,2137,2137,2139,2140, 2142,2142,2143,2145,2146,2149,2150,2151,2152,2153, 2156,2155,2158,2158,2159,2162,2162,2164,2166,2167, 2167,2169,2171,2172,2173,2174,2175,2177,2179,2179, 2182,2183,2185,2186,2187,2189,2190,2192,2192,2193, 2196,2197,2198,2200,2201,2202,2205,2205,2207,2208, 2210,2212,2212,2214,2214,2216,2218,2220,2220,2221, 2223,2225,2226,2228,2229,2231,2232,2234,2235,2237, 2238,2239,2241,2242,2243,2246,2246,2249,2249,2251, 2253,2254,2256,2256,2258,2259,2260,2263,2263,2265, 2267,2268,2269,2270,2271,2274,2275,2277,2278,2280, 2280,2282,2285,2286,2286,2287,2290,2291,2292,2294, 2295,2298,2298,2300,2302,2302,2305,2307,2307,2309, 2310,2311,2314,2315,2316,2318,2319,2320,2321,2324, 2326,2328,2328,2329,2331,2332,2334,2336,2337,2339, 2340,2341,2344,2345,2347,2345,2349,2350,2352,2353, 2355,2355,2359,2360,2361,2361,2363,2365,2368,2369, 2370,2372,2372,2375,2377,2378,2379,2381,2383,2384, 2386,2386,2388,2390,2391,2394,2395,2397,2397,2400, 2400,2403,2404,2406,2407,2409,2409,2412,2413,2415, 2415,2417,2418,2420,2422,2423,2424,2425,2428,2430, 2430,2432,2434,2435,2437,2438,2440,2443,2445,2446, 2446,2449,2449,2453,2454,2457,2456,2458,2460,2462, 2463,2465,2466,2468,2469,2471,2473,2474,2475,2477, 2479,2479,2482,2482,2485,2486,2487,2490,2491,2493, 2494,2495,2497,2499,2499,2502,2502,2505,2506,2507, 2510,2509,2512,2513,2516,2517,2519,2521,2522,2523, 2525,2527,2528,2530,2532,2533,2535,2536,2537,2537, 2540,2542,2543,2545,2546,2547,2551,2552,2553,2554, 2556,2557,2559,2561,2561,2563,2566,2566,2568,2570, 2571,2573,2574,2576,2577,2580,2580,2582,2584,2585, 2586,2588,2590,2592,2594,2594,2596,2597,2600,2601, 2602,2604,2606,2606,2608,2610,2612,2613,2615,2617, 2617,2620,2621,2622,2623,2625,2628,2629,2630,2631, 2633,2636,2637,2639,2639,2642,2642,2644,2646,2647, 2650,2650,2651,2654,2654,2656,2658,2660,2660,2662, 2664,2664,2666,2668,2669,2671,2674,2673,2676,2676, 2679,2679,2681,2684,2684,2687,2687,2690,2690,2692, 2694,2695,2697,2699,2701,2701,2704,2706,2707,2708, 2710,2711,2714,2715,2714,2719,2719,2721,2721,2724, 2725,2727,2729,2729,2731,2732,2734,2735,2736,2738, 2740,2741,2742,2744,2745,2747,2748,2750,2751,2753, 2754,2756,2757,2759,2761,2762,2765,2765,2767,2767, 2769,2770,2773,2773,2775,2776,2778,2780,2782,2783, 2784,2787,2787,2788,2790,2792,2794,2793,2796,2796, 2799,2800,2802,2803,2805,2805,2806,2808,2810,2810, 2812,2813,2816,2817,2818,2820,2822,2822,2824,2825, 2827,2827,2829,2830,2833,2833,2834,2836,2838,2839, 2841,2842,2844,2844,2845,2849,2849,2850,2852,2853, 2854,2856,2857,2858,2861,2861,2864,2863,2867,2867, 2868,2869,2869,2873,2874,2874,2876,2876,2878,2879, 2881,2882,2883,2885,2887,2888,2888,2890,2891,2892, 2895,2894,2897,2898,2900,2900,2901,2904,2905,2907, 2907,2908,2910,2912,2914,2914,2916,2916,2918,2919, 2920,2922,2923,2925,2925,2927,2928,2928,2931,2932, 2934,2935,2937,2938,2939,2942,2941,2943,2945,2945, 2946,2947,2949,2950,2952,2953,2955,2956,2957,2959, 2961,2962,2962,2964,2965,2965,2967,2967,2970,2971, 2972,2973,2975,2975,2976,2979,2980,2981,2980,2983, 2983,2986,2986,2988,2990,2989,2992,2992,2994,2995, 2997,2997,2998,3000,3001,3003,3003,3004,3005,3007, 3006,3009,3010,3010,3012,3013,3015,3015,3017,3018, 3019,3020,3020,3021,3024,3025,3025,3027,3028,3030, 3029,3031,3032,3032,3034,3036,3037,3037,3039,3040, 3041,3041,3043,3043,3044,3047,3047,3049,3048,3051, 3051,3054,3053,3055,3055,3057,3058,3058,3061,3060, 3062,3063,3065,3065,3066,3067,3068,3069,3070,3071, 3071,3075,3074,3077,3074,3077,3077,3079,3080,3081, 3082,3084,3084,3085,3085,3085,3086,3088,3089,3090, 3090,3093,3093,3094,3094,3096,3097,3098,3098,3099, 3099,3102,3102,3103,3104,3105,3107,3107,3108,3108, 3110,3110,3112,3112,3113,3113,3116,3116,3116,3117, 3117,3120,3120,3122,3121,3122,3123,3123,3125,3127, 3128,3127,3129,3129,3131,3131,3131,3133,3133,3135, 3135,3137,3136,3137,3138,3139,3140,3140,3142,3142, 3143,3144,3145,3146,3147,3147,3148,3149,3148,3150, 3150,3152,3153,3154,3154,3155,3157,3155,3156,3156, 3157,3159,3158,3160,3160,3162,3161,3164,3163,3165, 3165,3165,3167,3167,3168,3167,3170,3170,3171,3170, 3171,3173,3173,3173,3174,3175,3176,3178,3177,3177, 3180,3179,3180,3180 }; if (!inpAddr) return -1; dbScanLock(inpAddr->precord); if ((inpAddr->field_type == DBF_DOUBLE) && ((prset = dbGetRset(inpAddr)))) { inpP = (double *)inpAddr->pfield; if ((!inpP) || (inpAddr->no_elements < nord)) nord = 0; (*prset->put_array_info)(inpAddr, nord); if (inpAddr->no_elements > nord) memset(inpP, 0, sizeof(double) * inpAddr->no_elements); } else { inpP = 0; nord = 0; } psub->val = (double)nord; for (idx = 0; idx < nord; idx++, inpP++) { *inpP = testData[idx]; } dbScanUnlock(inpAddr->precord); if (psub->val > 0.0) return 0; else return -1; } long subpsmTime(struct subRecord *psub) { /* * Find time associated with the selected indices into the data waveform. * Fill in X arrays used for display of range lines on the * pulse signal waveform display. * * Inputs: * A = Index 1 (0 to max data points-1) * B = Index 2 (0 to max data points-1) * C = Spare * D = Delta time between values (usec) * * Outputs: * I = Time 1 (usec) * J = Time 2 (usec) * INPK = Pointer to array of time 1 values * INPL = Pointer to array of time 2 values * VAL = Not used */ double xval[2]; psub->i = psub->a * psub->d; xval[0] = xval[1] = psub->i; dbPutLink(&psub->inpk, DBR_DOUBLE, xval, 2); psub->j = psub->b * psub->d; xval[0] = xval[1] = psub->j; dbPutLink(&psub->inpl, DBR_DOUBLE, xval, 2); return 0; } long subpsmTimeArray(struct subRecord *psub) { /* * Fill in line used to plot time in a 2-d plot. * * Inputs: * A = Delta time between values (usec) * B = Max number of data points * * Outputs: * INPL = Pointer to array of time values * VAL = Number of Values in the Array */ DBADDR *timeAddr = dbGetPdbAddrFromLink(&psub->inpl); double *timeP; double inctime; long nord = (long)psub->b; rset *prset; if (!timeAddr) return -1; dbScanLock(timeAddr->precord); if ((timeAddr->field_type == DBF_DOUBLE) && ((prset = dbGetRset(timeAddr)))) { timeP = (double *)timeAddr->pfield; if ((!timeP) || (timeAddr->no_elements < nord)) nord = 0; (*prset->put_array_info)(timeAddr, nord); if (timeAddr->no_elements > nord) memset(timeP, 0, sizeof(double) * timeAddr->no_elements); } else { timeP = 0; nord = 0; } psub->val = (double)nord; for (inctime = 0.0; nord > 0; nord--, timeP++) { *timeP = inctime; inctime += psub->a; } dbScanUnlock(timeAddr->precord); if (psub->val > 0.0) return 0; else return -1; } long subpsmSelect(struct subRecord *psub) { /* * Select value from one of two ranges and update data buffer. * * Inputs: * A = Appropriate field from the Range A Update record * B = Appropriate field from the Range B Update record * C = Selected range (0=A, 1=B) * D = Range A Update record severity * E = Range B Update record severity * F = Allowable Time Diff between Data Buffer and Value * G = Data Buffer Index * Input/Outputs: * H,I,J,K = Spare * L = Selected Severity * VAL = Selected Value */ epicsTimeStamp newTimeStamp; /* Choose the value to use. */ if (psub->c > 0.5) { psub->val = psub->b; psub->l = psub->e; dbGetTimeStamp(&psub->inpb, &newTimeStamp); } else { psub->val = psub->a; psub->l = psub->d; dbGetTimeStamp(&psub->inpa, &newTimeStamp); } /* Update data buffer. */ write_wf_data(psub->f, (long)psub->g, psub->val, &newTimeStamp); return 0; } long subpsmAverageProc(struct subRecord *psub) { /* * Update sums, counter, minimum, and maximum used in the time average * and standard deviation calculation. Reset sums and counter on demand. * Check against limits for breakdown - update breakdown counter and post * an event when there is a breakdown. * Reset breakdown counter on demand. * * Inputs: * A = Value * B = Severity * C = Breakdown Event (<0 = no breakdown checking) * D = Breakdown Counter Low Limit * E = Breakdown Counter High Limit * F = Minumum Value below which value is no good * Input/Outputs: * G = Minimum Value so Far * H = Maximum Value so Far * I = Breakdown Counter so Far - set to 0 to reset * J = Average Sum so Far * K = # of Values in Average so Far - set to 0 to reset * L = Value-Squared Sum so Far * VAL = Input Value */ /* Reset sums when counter is reset. */ if (psub->k < 0.5) { psub->g = 0; psub->h = 0; psub->j = 0; psub->k = 0; psub->l = 0; } /* Do nothing if severity is not invalid - set this record invalid. */ if (psub->b > 2.5) return -1; psub->val = psub->a; /* Do nothing if the value is less than the threshold - set this record invalid. */ if (psub->a < psub->f) return -1; /* Find min and max */ if (psub->k < 0.5) { psub->g = psub->val; psub->h = psub->val; } else { if (psub->val < psub->g) psub->g = psub->val; if (psub->val > psub->h) psub->h = psub->val; } /* Increment sums and counter. */ psub->j += psub->val; psub->l += (psub->val * psub->val); psub->k++; /* Check for breakdown. */ if ((psub->c >= 0.0) && ((psub->val >= psub->e) || (psub->val <= psub->d))) { /* Rollover breakdown counter when count gets too high. */ if (psub->i > 4000000000.0) psub->i = 1; else psub->i++; post_event((int)psub->c); } return 0; } long subpsmAverage(struct subRecord *psub) { /* * Do time average and standard deviation. * * Inputs: * A = # of times counter (K) is zero before setting record * to invalid. If <0, set value to 0 and don't set record * to invalid. * B = Spare * G = Minimum (not used, provides a snapshot at lower rate) * H = Maximum (not used, provides a snapshot at lower rate) * I = Breakdown Counter (not used, provides a snapshot at lower rate) * J = Average Sum * K = # of Values in Average * L = Value-Squared Sum * Outputs: * C = RMS - future * D = Standard Deviation on Average * E = Zero counter flag (0 = last count wasn't zero, >0 last count(s) was zero) * F = Intermediate value for standard deviation calculation * VAL = Average */ /* * Leave previous values as-is if no sums have * been done since the last time this record processed. */ if (psub->k < 0.5) { if (psub->a < 0.0) { psub->val = 0; psub->d = 0.0; psub->e = 0.0; psub->f = 0.0; return 0; } if (psub->e < psub->a) { psub->e++; return 0; } return -1; } /* Find average, RMS, and standard deviation from input sums. */ psub->e = 0.0; psub->val = psub->j/psub->k; /* average */ #if 0 psub->c = sqrt(psub->l/psub->k); /* average RMS */ #endif psub->f = psub->l - ((psub->j * psub->j)/psub->k); if ((psub->f > 0.0) && (psub->k > 1.5)) psub->d = sqrt(psub->f/(psub->k - 1.0)); /* standard deviation */ else psub->d = 0.0; return 0; } long subpsmAverageRun(struct subRecord *psub) { /* * Do average and standard deviation of an array of values. * * Inputs: * A = Array of last values * B = # values to average * E = Minumum Value below which value is no good * I = Validity flag: * 0 = Valid if # values in average is zero. * >0 = Invalid if # values in average is zero. * Outputs: * C = RMS - future * D = Standard Deviation on Average * F = Intermediate value for standard deviation calculation * G = Minimum * H = Maximum * J = Average Sum * K = # of Values in Average * L = Value-Squared Sum * VAL = Average */ DBADDR *yAddr = dbGetPdbAddrFromLink(&psub->inpa); double *py; long nord, nelm, offset, idx; long nsam = (long)(psub->b + 0.5); rset *prset; psub->c = 0.0; psub->d = 0.0; psub->f = 0.0; psub->g = 0.0; psub->h = 0.0; psub->j = 0.0; psub->k = 0.0; psub->l = 0.0; psub->val = 0.0; if (!yAddr) return -1; dbScanLock(yAddr->precord); if ((yAddr->field_type == DBF_DOUBLE) && ((prset = dbGetRset(yAddr)))) { (*prset->get_array_info)(yAddr, &nord, &offset); py = (double *)yAddr->pfield; nelm = yAddr->no_elements; } else { py = 0; nord = 0; nelm = 0; } if (nord > 0) { /* * Start with the most current value. */ if (offset < 1) offset = nord; offset--; if (nsam > nord) nsam = nord; for(idx = 0; idx < nsam; idx++, offset--) { /* Go to end of circular buffer when ready. */ if (offset < 0) offset = nelm-1; /* Value must not be less than the threshold */ if (py[offset] >= psub->e) { /* Find min and max */ if (psub->k < 0.5) { psub->g = py[offset]; psub->h = py[offset]; } else { if (py[offset] < psub->g) psub->g = py[offset]; if (py[offset] > psub->h) psub->h = py[offset]; } /* Increment # in average and sum */ psub->k++; psub->j += py[offset]; psub->l += (py[offset] * py[offset]); } } } dbScanUnlock(yAddr->precord); if (psub->k > 0.5) { /* Find average, RMS, and standard deviation from sums. */ psub->val = psub->j/psub->k; /* average */ #if 0 psub->c = sqrt(psub->l/psub->k); /* average RMS */ #endif psub->f = psub->l - ((psub->j * psub->j)/psub->k); if ((psub->f > 0.0) && (psub->k > 1.5)) psub->d = sqrt(psub->f/(psub->k - 1.0)); /* standard deviation */ else psub->d = 0.0; return 0; } if (psub->i > 0.5) return -1; return 0; } long subpsmIQ(struct subRecord *psub) { /* * Calculate I and Q values based on an input amplitude and phase. * Inputs: A = Amplitude * B = Phase (degrees) * Outputs: G = Phase (radians) * K = I * L = Q * VAL = Amplitude */ psub->val = psub->a; /* Find phase in radians. */ psub->g = psub->b * SIN_PI/180.0; /* Calculate I and Q. */ psub->k = psub->val * cos(psub->g); psub->l = psub->val * sin(psub->g); return(0); } long subpsmCharge(struct sSubRecord *psub) { /* * Sum input (linac ACM or BTS ACM), output (BTS ACM or SPEAR DCCT), * and lost charge for a fill charge. * Also, force the injection efficiency calculation when there * is enough data. All calculations are done whenever a * new output charge is available (currently every second). * * The start of a fill is determined when the chopper goes on, stopper * (ie, LTB stopper or PR2) is out, * and the first >0 input charge is detected. The end of a fill is * when the chopper goes off or stopper is in, and both the output and input * charge show no more charge. * * Injection efficiency is calculated during the fill only after * enough charge values have been received. Unfortunately, the * BTS ACM charge and DCCT charge are not synchronized. * * Inputs: * A = Input Integrated Charge * B = Output Integrated Charge * C = Chopper Status (0=disabled, 1=enabled) * D = Stopper State (0=Out, 1=In) * E = # of Values used in Injection Efficiency calculation * (1, 2, 3, or 4) * F = Count before a fill is finished * I = # 10Hz Pulses in the Input Charge Integration * Input/Outputs: * G = Input Integrated Charge for the Fill * H = Output Integrated Charge for the Fill * J = # 10Hz Pulses in the Input Integrated Charge for the Fill * K = Lost Charge for the Fill * L = Count for the Injection Efficiency Calculation * M = Fill Off Count * S = Input Integrated Charge from last Update * T = Output Integrated Charge from last Update * U = Input Integrated Charge from 2 Updates ago * V = Output Integrated Charge from 2 Updates ago * W = Input Integrated Charge from 3 Updates ago * X = Output Integrated Charge from 3 Updates ago * VAL = Fill Status (0=Off, 1=On, 2=On-and-Waiting-for-Charge * Outputs: * N = Update Fill and Inj Efficiency Values: * 0 = no update, * 0x20 = update injection efficiency only, * 0x3FF = update everything * O = Smooth Injection Efficiency (1=smooth, 0=don't smooth) * P = Input Charge Sum for Injection Efficiency Calc * Q = Output Charge Sum for Injection Efficiency Calc * R = Lost Charge for this Cycle */ /* Don't update fill values until fill goes off */ psub->n = 0.0; /* Don't do injection efficiency smoothing */ psub->o = 0.0; /* No lost charge this cycle. */ psub->r = 0.0; /* Check inputs */ if (psub->e < 0.5) psub->e = 1.0; if (psub->f < 0.5) psub->f = 1.0; /* Fill starts when chopper is on and stopper is out. */ if ((psub->c > 0.5) && (psub->d < 0.5) && (psub->val < 0.5)) { psub->val = 2.0; psub->m = psub->f; } /* If fill is not off, update fill values. */ if (psub->val > 0.5) { /* Check for start of fill */ if ((psub->val > 1.5) && (psub->a > 0.0)) { psub->val = 1.0; psub->l = 0.0; psub->m = psub->f; psub->g = psub->h = psub->j = psub->k = 0.0; psub->s = psub->t = psub->u = psub->v = psub->w = psub->x = 0.0; } /* If the chopper is off or stopper is in now, check for end of fill. */ if ((psub->c < 0.5) || (psub->d > 0.5)) { if (psub->m > 0.5) psub->m--; /* If there is no more charge or we've waited long enough, * the fill is finished. Calculate lost charge and * force update of fill charge values. Injection efficiency * now uses the fill values. */ if (((psub->a <= 0.0) && (psub->b <= 0.0)) || (psub->m < 0.5)) { if ((psub->val > 0.5) && (psub->val < 1.5)) { psub->n = 0x3FF; psub->k = psub->g - psub->h; if (psub->k < 0.0) psub->k = 0.0; psub->p = psub->g; psub->q = psub->h; } psub->val = 0.0; } } else { psub->m = psub->f; } /* If fill is on, update integrated fill values */ if ((psub->val > 0.5) && (psub->val < 1.5)) { if (psub->a > 0.0) { psub->g += psub->a; psub->j += psub->i; psub->r = psub->a; } if (psub->b > 0.0) { psub->h += psub->b; psub->r -= psub->b; } /* Update values for injection efficiency if there is input charge */ if (psub->a > 0.0) { if (psub->l <= psub->e) { psub->l++; } else { psub->o = 1.0; /* Do smoothing now */ } /* Are there enough values for a decent inj eff calc */ if (psub->l > psub->e) { psub->n = 0x20; /* Integrate the desired number of values */ if (psub->l > 0.5) { if (psub->a > 0.0) psub->p = psub->a; else psub->p = 0.0; if (psub->b > 0.0) psub->q = psub->b; else psub->q = 0.0; } if (psub->l > 1.5) { if (psub->s > 0.0) psub->p += psub->s; if (psub->t > 0.0) psub->q += psub->t; } if (psub->l > 2.5) { if (psub->u > 0.0) psub->p += psub->u; if (psub->v > 0.0) psub->q += psub->v; } if (psub->l > 3.5) { if (psub->w > 0.0) psub->p += psub->w; if (psub->x > 0.0) psub->q += psub->x; } } /* Update previous values for next calc */ psub->w = psub->u; psub->x = psub->v; psub->u = psub->s; psub->v = psub->t; psub->s = psub->a; psub->t = psub->b; } /* Inj Eff update */ } /* Fill update */ } /* Fill not off */ return 0; } long subpsmChargeSum(struct subRecord *psub) { /* * This routine runs at 10hz after data acquistion of charge is done. * Sum input charge and increment counter when charge is non-zero. * Reset sum and counter when requested (every second). * Inputs: * A = Input Charge for One Pulse * B = Input Charge Severity * D = Value above which the charge is summed. * Input/Outputs: * C = Reset Counter and Sum (1=reset,0=no reset) * L = Count of non-zero charge since last reset * VAL = Charge Sum since last reset */ /* Check for a reset. */ if (psub->c > 0.5) { psub->l = 0.0; psub->val = 0.0; psub->c = 0.0; } /* Sum if we have good charge above a threshold. */ if ((psub->a > psub->d) && (psub->b < 2.5)) { psub->l++; psub->val += psub->a; } return 0; } #ifdef __rtems__ extern void NTPTime_Init(int priority); /* NTPTime_Init */ static const iocshArg NTPTime_InitArg0 = { "priority", iocshArgArgv}; static const iocshArg * const NTPTime_InitArgs[1] = { &NTPTime_InitArg0}; static const iocshFuncDef NTPTime_InitFuncDef = {"NTPTime_Init",1,NTPTime_InitArgs}; static void NTPTime_InitCallFunc(const iocshArgBuf *args) { NTPTime_Init(args[0].ival); } static void NTPTime_InitRegistrar(void) { iocshRegister(&NTPTime_InitFuncDef, NTPTime_InitCallFunc); } epicsExportRegistrar(NTPTime_InitRegistrar); #endif epicsRegisterFunction(subpsmWfm); epicsRegisterFunction(subpsmWfmProcInit); epicsRegisterFunction(subpsmWfmProc); epicsRegisterFunction(subpsmWfmProcTest); epicsRegisterFunction(subpsmTime); epicsRegisterFunction(subpsmTimeArray); epicsRegisterFunction(subpsmSelect); epicsRegisterFunction(subpsmAverageProc); epicsRegisterFunction(subpsmAverage); epicsRegisterFunction(subpsmAverageRun); epicsRegisterFunction(subpsmIQ); epicsRegisterFunction(subpsmCharge); epicsRegisterFunction(subpsmChargeSum);