/*============================================================================= Name: fbck.c Abs: This file contains all subroutine support for the longitudinal feedback simulator 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: -----------------------------------------------------------------------------*/ /*#include "copyright_SLAC.h" SLAC copyright comments */ /*----------------------------------------------------------------------------- Mod: (newest to oldest) =============================================================================*/ /* c includes */ #include /* memcmp */ #include /* sqrt,sin */ #include /* rand */ #include /* for struct subRecord */ /*#include for struct sSubRecord in site area */ #include /* for epicsExport */ #include /* for epicsRegisterFunction */ #include /* epicsTimeStamp */ #include /* dbGetTimeStamp */ #include /* NO_ALARM, INVALID_ALARM */ #define NMATRIX 36 /* define number of elements in the matrix */ #define NHST 100 /* define number of elements in the averaging buffers */ static int fbckInit(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: longXDL1Calc Abs: longitudinal machine simulation. this is the matrix multiplication O = MC where M is a 6x6 matrix and C is a 6x1 column vector representing the rf amp and phase actuators. O is the output values (6x1 column) E0, E1, I1, E2, I2, E3 Once the energy is calculated, then a dispersion factor is applied per BPM. This is for the BPM used for the DL1 feedback loop ONLY AGAIN: THIS SUBROUTINE CALCULATES BPM-X for the DL1 loop only Args: Type Name Access Description ------------------- ----------- ---------- ---------------------------- subRecord * psub read point to subroutine record Rem: Subroutine for PRIM:LOCA:UNIT:STATUS Side: INPA - DEV:MATRIXWFM where DEV=LONGSIM:XX06:1 INPB - DSPR dispersion value for the BPM used in DL1 loop INPD - LLRF:XX06:801:AMP INPE - LLRF:XX06:101:AMP INPF - LLRF:XX06:101:PHASE INPG - LLRF:XX06:2:PHASEA INPH - LLRF:XX06:2:PHASEB INPI - LLRF:XX06:3:PHASE OUT VAL = X for DL1 Ret: 0 ==============================================================================*/ static long longXDL1Calc(subRecord *psub) { DBADDR *matrixAddr = dbGetPdbAddrFromLink(&psub->inpa); double *matrix_p; double temp_val; /*printf("starting longCalc\n");*/ /* check for error conditions, no input, no waveform ptr, not enough elements */ if (!matrixAddr) return -1; /* printf("matrix addr is good\n");*/ matrix_p = (double *)matrixAddr->pfield; if ((!matrix_p) || (matrixAddr->no_elements < NMATRIX)) return -1; /*printf("matrix pointer and size are good\n");*/ /* now calculate e0 */ printf("c1=%e, c2=%e, c3=%e, c4=%e\n", matrix_p[0],matrix_p[1], matrix_p[2],matrix_p[3]); temp_val = matrix_p[0]*psub->d + matrix_p[1]*psub->e + matrix_p[2]*psub->f; psub->val = temp_val + matrix_p[3]*psub->g + matrix_p[4]*psub->h + matrix_p[5]*psub->i; /* and multiply by dispersion factor */ psub->val = psub->b*psub->val; return 0; } /*============================================================================= Name: longXBC1Calc Abs: longitudinal machine simulation. this is the matrix multiplication O = MC where M is a 6x6 matrix and C is a 6x1 column vector representing the rf amp and phase actuators. O is the output values (6x1 column) E0, E1, I1, E2, I2, E3 Once the energy is calculated, then a dispersion factor is applied per BPM. This is for the BPM used for the BC1 feedback energy ONLY AGAIN: THIS SUBROUTINE CALCULATES BPM-X for the BC1 energy only Args: Type Name Access Description ------------------- ----------- ---------- ---------------------------- subRecord * psub read point to subroutine record Rem: Subroutine for PRIM:LOCA:UNIT:STATUS Side: INPA - DEV:MATRIXWFM where DEV=LONGSIM:XX06:1 INPB - DSPR dispersion value for the BPM used in BC1 loop INPD - LLRF:XX06:801:AMP INPE - LLRF:XX06:101:AMP INPF - LLRF:XX06:101:PHASE INPG - LLRF:XX06:2:PHASEA INPH - LLRF:XX06:2:PHASEB INPI - LLRF:XX06:3:PHASE OUT VAL = E1 Ret: 0 ==============================================================================*/ static long longXBC1Calc(subRecord *psub) { DBADDR *matrixAddr = dbGetPdbAddrFromLink(&psub->inpa); double *matrix_p; double temp_val; /*printf("starting longCalc\n");*/ /* check for error conditions, no input, no waveform ptr, not enough elements */ if (!matrixAddr) return -1; /* printf("matrix addr is good\n");*/ matrix_p = (double *)matrixAddr->pfield; if ((!matrix_p) || (matrixAddr->no_elements < NMATRIX)) return -1; /*printf("matrix pointer and size are good\n");*/ /* now calculate */ temp_val = matrix_p[6]*psub->d + matrix_p[7]*psub->e + matrix_p[8]*psub->f; /* e1 */ psub->val = temp_val + matrix_p[9]*psub->g + matrix_p[10]*psub->h + matrix_p[11]*psub->i; /* and multiply by dispersion factor */ psub->val = psub->b*psub->val; return 0; } /*============================================================================= Name: longI1Calc Abs: longitudinal machine simulation. this is the matrix multiplication O = MC where M is a 6x6 matrix and C is a 6x1 column vector representing the rf amp and phase actuators. O is the output values (6x1 column) E0, E1, I1, E2, I2, E3 THIS SUBROUTINE CALCULATES I1 only Args: Type Name Access Description ------------------- ----------- ---------- ---------------------------- subRecord * psub read point to subroutine record Rem: Subroutine for PRIM:LOCA:UNIT:STATUS Side: INPA - DEV:MATRIXWFM where DEV=LONGSIM:XX06:1 INPD - LLRF:XX06:801:DELTA_AMP INPE - LLRF:XX06:101:DELTA_AMP INPF - LLRF:XX06:101:DELTA_PHASE INPG - LLRF:XX06:2:PHASEA INPH - LLRF:XX06:2:PHASEB INPI - LLRF:XX06:3:PHASE OUT VAL = I1 Ret: 0 NOTE: dmf added offset of 8e6 to represent real BLEN, which never reads 0 ==============================================================================*/ static long longI1Calc(subRecord *psub) { DBADDR *matrixAddr = dbGetPdbAddrFromLink(&psub->inpa); double *matrix_p; double temp_val; /*printf("starting longCalc\n");*/ /* check for error conditions, no input, no waveform ptr, not enough elements */ if (!matrixAddr) return -1; /* printf("matrix addr is good\n");*/ matrix_p = (double *)matrixAddr->pfield; if ((!matrix_p) || (matrixAddr->no_elements < NMATRIX)) return -1; /*printf("matrix pointer and size are good\n");*/ /* now calculate */ temp_val = (matrix_p[12]*psub->d)+(matrix_p[13]*psub->e)+(matrix_p[14]*psub->f)+8.0e6; /*i1*/ psub->val = temp_val + matrix_p[15]*psub->g + matrix_p[16]*psub->h + matrix_p[17]*psub->i; return 0; } /*============================================================================= Name: longXBC2Calc Abs: longitudinal machine simulation. this is the matrix multiplication O = MC where M is a 6x6 matrix and C is a 6x1 column vector representing the rf amp and phase actuators. O is the output values (6x1 column) E0, E1, I1, E2, I2, E3 Once the energy is calculated, then a dispersion factor is applied per BPM. This is for the BPM used for the BC2 energy ONLY AGAIN: THIS SUBROUTINE CALCULATES BPM-X for the BC2 energy only Args: Type Name Access Description ------------------- ----------- ---------- ---------------------------- subRecord * psub read point to subroutine record Rem: Subroutine for PRIM:LOCA:UNIT:STATUS Side: INPA - DEV:MATRIXWFM where DEV=LONGSIM:XX06:1 INPB - DSPR dispersion value for the BPM used in BC2 loop INPD - LLRF:XX06:801:AMP INPE - LLRF:XX06:101:AMP INPF - LLRF:XX06:101:PHASE INPG - LLRF:XX06:2:PHASEA INPH - LLRF:XX06:2:PHASEB INPI - LLRF:XX06:3:PHASE OUT VAL = E2 Ret: 0 ==============================================================================*/ static long longXBC2Calc(subRecord *psub) { DBADDR *matrixAddr = dbGetPdbAddrFromLink(&psub->inpa); double *matrix_p; double temp_val; /*printf("starting longCalc\n");*/ /* check for error conditions, no input, no waveform ptr, not enough elements */ if (!matrixAddr) return -1; /* printf("matrix addr is good\n");*/ matrix_p = (double *)matrixAddr->pfield; if ((!matrix_p) || (matrixAddr->no_elements < NMATRIX)) return -1; /*printf("matrix pointer and size are good\n");*/ /* now calculate */ temp_val = matrix_p[18]*psub->d + matrix_p[19]*psub->e + matrix_p[20]*psub->f; /* e1 */ psub->val = temp_val + matrix_p[21]*psub->g + matrix_p[22]*psub->h + matrix_p[23]*psub->i; /* and multiply by dispersion factor */ psub->val = psub->b*psub->val; return 0; } /*============================================================================= Name: longI2Calc Abs: longitudinal machine simulation. this is the matrix multiplication O = MC where M is a 6x6 matrix and C is a 6x1 column vector representing the rf amp and phase actuators. O is the output values (6x1 column) E0, E1, I1, E2, I2, E3 THIS SUBROUTINE CALCULATES I1 only Args: Type Name Access Description ------------------- ----------- ---------- ---------------------------- subRecord * psub read point to subroutine record Rem: Subroutine for PRIM:LOCA:UNIT:STATUS Side: INPA - DEV:MATRIXWFM where DEV=LONGSIM:XX06:1 INPD - LLRF:XX06:801:DELTA_AMP INPE - LLRF:XX06:101:DELTA_AMP INPF - LLRF:XX06:101:DELTA_PHASE INPG - LLRF:XX06:2:PHASEA INPH - LLRF:XX06:2:PHASEB INPI - LLRF:XX06:3:PHASE OUT VAL = I2 Ret: 0 NOTE: dmf added offset of 8e6 to represent real BLEN, which never reads 0 ==============================================================================*/ static long longI2Calc(subRecord *psub) { DBADDR *matrixAddr = dbGetPdbAddrFromLink(&psub->inpa); double *matrix_p; double temp_val; /*printf("starting longCalc\n");*/ /* check for error conditions, no input, no waveform ptr, not enough elements */ if (!matrixAddr) return -1; /* printf("matrix addr is good\n");*/ matrix_p = (double *)matrixAddr->pfield; if ((!matrix_p) || (matrixAddr->no_elements < NMATRIX)) return -1; /*printf("matrix pointer and size are good\n");*/ /* now calculate */ temp_val = (matrix_p[24]*psub->d)+(matrix_p[25]*psub->e)+(matrix_p[26]*psub->f)+8.0e6; psub->val = temp_val + matrix_p[27]*psub->g + matrix_p[28]*psub->h + matrix_p[29]*psub->i; return 0; } /*============================================================================= Name: longXDL2Calc Abs: longitudinal machine simulation. this is the matrix multiplication O = MC where M is a 6x6 matrix and C is a 6x1 column vector representing the rf amp and phase actuators. O is the output values (6x1 column) E0, E1, I1, E2, I2, E3 Once the energy is calculated, then a dispersion factor is applied per BPM. This is for the BPM used for the DL2 energy ONLY AGAIN: THIS SUBROUTINE CALCULATES BPM-X for the DL2 energy only Args: Type Name Access Description ------------------- ----------- ---------- ---------------------------- subRecord * psub read point to subroutine record Rem: Subroutine for PRIM:LOCA:UNIT:STATUS Side: INPA - DEV:MATRIXWFM where DEV=LONGSIM:XX06:1 INPB - DSPR dispersion value for the BPM used in DL2 INPD - LLRF:XX06:801:AMP INPE - LLRF:XX06:101:AMP INPF - LLRF:XX06:101:PHASE INPG - LLRF:XX06:2:PHASEA INPH - LLRF:XX06:2:PHASEB INPI - LLRF:XX06:3:PHASE OUT VAL = X for DL2 Ret: 0 ==============================================================================*/ static long longXDL2Calc(subRecord *psub) { DBADDR *matrixAddr = dbGetPdbAddrFromLink(&psub->inpa); double *matrix_p; double temp_val; /*printf("starting longCalc\n");*/ /* check for error conditions, no input, no waveform ptr, not enough elements */ if (!matrixAddr) return -1; /* printf("matrix addr is good\n");*/ matrix_p = (double *)matrixAddr->pfield; if ((!matrix_p) || (matrixAddr->no_elements < NMATRIX)) return -1; /*printf("matrix pointer and size are good\n");*/ /* now calculate e0 */ temp_val = matrix_p[30]*psub->d + matrix_p[31]*psub->e + matrix_p[32]*psub->f; psub->val = temp_val + matrix_p[33]*psub->g + matrix_p[34]*psub->h + matrix_p[35]*psub->i; /* and multiply by dispersion factor */ psub->val = psub->b*psub->val; return 0; } /*============================================================================= Name: longNoiseCalc Abs: longitudinal machine simulation. Calculate the noise added to the actuator settings. this noise is added at a rate of 10Hz, so the sinusoids and drift will change at the rate of 10Hz. Args: Type Name Access Description ------------------- ----------- ---------- ---------------------------- subRecord * psub read point to subroutine record Rem: Subroutine for PRIM:LOCA:UNIT:NAMENOISE Side: A = the random noise signal from $PRIM:$LOCA:$UNIT:RAND B = $SMULT1 the gain multiplier for the 1st sinusoidal signal C = $FRQ1 the frequency of the 1st sinusoidal signal D = $SMULT2 the gain multiplier for the 2nd sinusoidal signal E = $FRQ2 the frequency of the 2nd sinusoidal signal F = $LMULT the gain multiplier for the linear drift signal G = $FREQ0 is const 10Hz INPH = $(FBCK):CNTR the incrementing counter to calc time value VAL = the resulting signal with all noise types added together (random, sine, drift, step) Ret: 0 ==============================================================================*/ static long longNoiseCalc(subRecord *psub) { double r; /* random signal */ double s1; /* 1st sinusoid */ double s2; /* 2nd sinusoid */ double drift; /* linear drift */ double t; /* time */ t = (psub->h)/10; /* t(j) at 10 Hz */ r = psub->a; /* random jitter */ s1 = psub->b*sin(2*3.1416*psub->c*t); /* sin(2pi*f*t) */ s2 = psub->d*sin(2*3.1416*psub->e*t); /* sin(2pi*f*t) */ drift = psub->f*t; /* linear drift */ /* printf("time = %lf, r=%lf, s1=%lf, s2=%lf, drift=%lf\n", t, r, s1, s2, drift); */ /* now calculate */ psub->val = r+s1+s2+drift; /* total calculated noise to be added to signal */ return 0; } /*============================================================================= Name: transNoiseCalc Abs: transverse machine simulation. Calculate the noise added to the actuator settings. this noise is added at a rate of 10Hz, so the sinusoids and drift will change at the rate of 10Hz. Args: Type Name Access Description ------------------- ----------- ---------- ---------------------------- subRecord * psub read point to subroutine record Rem: Subroutine for PRIM:LOCA:UNIT:NAMENOISE Side: A = the random noise signal from $PRIM:$LOCA:$UNIT:RAND B = $SMULT1 the gain multiplier for the 1st sinusoidal signal C = $FRQ1 the frequency of the 1st sinusoidal signal D = $SMULT2 the gain multiplier for the 2nd sinusoidal signal E = $FRQ2 the frequency of the 2nd sinusoidal signal F = $LMULT the gain multiplier for the linear drift signal G = $FREQ0 is const 10Hz INPH = $(FBCK):CNTR the incrementing counter to calc time value VAL = the resulting signal with all noise types added together (random, sine, drift, step) Ret: 0 ==============================================================================*/ static long transNoiseCalc(subRecord *psub) { double r; /* random signal */ double s1; /* 1st sinusoid */ double s2; /* 2nd sinusoid */ double drift; /* linear drift */ double t; /* time */ t = (psub->h)/10; /* t(j) at 10 Hz */ r = psub->a; /* random jitter */ s1 = psub->b*sin(2*3.1416*psub->c*t); /* sin(2pi*f*t) */ s2 = psub->d*sin(2*3.1416*psub->e*t); /* sin(2pi*f*t) */ drift = psub->f*t; /* linear drift */ /* printf("time = %lf, r=%lf, s1=%lf, s2=%lf, drift=%lf\n", t, r, s1, s2, drift); */ /* now calculate */ psub->val = r+s1+s2+drift; /* total calculated noise to be added to signal */ return 0; } epicsRegisterFunction(fbckInit); epicsRegisterFunction(longXDL1Calc); epicsRegisterFunction(longXBC1Calc); epicsRegisterFunction(longI1Calc); epicsRegisterFunction(longXBC2Calc); epicsRegisterFunction(longI2Calc); epicsRegisterFunction(longXDL2Calc); epicsRegisterFunction(longNoiseCalc); epicsRegisterFunction(transNoiseCalc);