#include #include #include #include /* for LOW_ALARM,NO_ALARM,HIGH_ALARM,MINOR_ALARM */ #include #include /* for atan2 */ #include /* used in PactFast */ #include /* for DBADDR */ #include /* for dbGetPdbAddrFromLink indirectly from dbAccessDefs.h */ #include /* for dbScanLock */ #include /* for DBF_DOUBLE */ #include /* for struct rset */ #ifdef DEBUG_PRINT #include /* for DEBUGPRINT */ #endif #include "llrfMacros.h" #include "drvLlrf.h" #define PI (3.14159) #define CLOSE_TO_ZERO (0.000001) /* 11/21/8 Writing errors (level 2) has become too invasive to the processor w.r.t. processing the incoming data streams. Set all these to 0 */ #ifdef DEBUG_PRINT int dbSubLlrfFlag = 0; int dbSubLlrfAsetFlag = 0; int dbSubLlrfPsetFlag = 0; int dbSubLlrfMDLPsetFlag = 0; int dbSubLlrfLaserPsetFlag = 0; int dbSubLlrfLaserPacPsetFlag = 0; int dbSubLlrfPactFlag = 0; int dbSubLlrfPactL1XFlag = 0; int dbSubLlrfPactFastFlag = 0; int dbSubLlrfGunFlag = 0; int dbSubLlrfPabsorbFlag = 0; int dbSubLlrfAabsorbFlag = 0; int dbSubLlrfPIDFlag = 0; int dbSubLlrfPID2Flag = 0; int dbSubLlrfMsrcPhaseFlag = 0; int dbSubLlrfPdesFlag = 0; int dbSubLlrfPsetZeroFlag = 0; int dbSubLlrfL1XPsetFlag = 0; int dbSubLlrfAdjPacFlag = 0; #endif int laser_fdbk_iter=0; static long dbSubLlrfInit(subRecord *precord) { #ifdef DEBUG_PRINT DEBUGPRINT(DP_INFO, dbSubLlrfFlag, ("Record %s called dbSubLlrfInit(%p)\n", precord->name, (void*) precord)); #endif return OK; } /* Combines the call to atan2, the _$(CH)_PACT1 calc to convert from radians to degrees, the $(CH)_PACT2 calc to bring phase >180 back between -180 and 180 and finally the $(CH)_PACT calc to bring phase <180 back between -180 and 180. Notes about ATAN2, chosen for its knowledge of quadrant. Ref: http://mathforum.org/library/drmath/view/55417.html if y > 0 then if x > 0 then tc1 = arctan(y/x) if x < 0 then tc1 = 180 - arctan(-y/x) if x = 0 then tc1 = 90 if y < 0 then if x > 0 then tc1 = -arctan(-y/x) if x < 0 then tc1 = arctan(y/x)-180 if x = 0 then tc1 = 270 if y = 0 then if x > 0 then tc1 = 0 if x < 0 then tc1 = 180 if x = 0 then [the 2 points are the same] inputs: INPA=$(PRIM):$(LOCA):$(UNIT):$(STN)_i_QACT, i=0-3 INPB=$(PRIM):$(LOCA):$(UNIT):$(STN)_i_IACT, i=0-3 INPC=$(PRIM):$(LOCA):$(UNIT):$(STN)_i_POC, i=0-3 flnks: the power PV, "AACT" */ static long dbSubLlrfPact(subRecord *precord) { double y; double x; double phase; int quadrant = 0; double phase_offset_correction; /* Assign subroutine inputs to y and x */ y=precord->a; x=precord->b; phase_offset_correction=precord->c; /* Determine the quadrant. Extra. Only for checking */ if (y>0) { if (x>0) { quadrant = 1; } else { if (x<0) { quadrant = 2; } else { if (x==0) { quadrant = 12; /* marker for boundary between I and II*/ } } } } else { if (y<0) { if (x>0) { quadrant = 4; } else { if (x<0) { quadrant = 3; } else { if (x==0) { quadrant = 34; /* marker for boundary between III and IV*/ } } } } else { if (y==0) { if (x>0) { quadrant = 41; /* marker for boundary between IV and I*/ } else { if (x<0) { quadrant = 23; /* marker for boundary between II and III*/ } else { if (x==0) { quadrant = 0; /* marker for the origin (0,0) */ } } } } } } phase = atan2(y,x); /* Convert radians to degrees */ phase = phase/PI*180.; #ifdef DEBUG_PRINT DEBUGPRINT(DP_INFO, dbSubLlrfPactFlag, ("dbSubLlrfPact: Record %s with inputs Q,I=(%f,%f) has phase=%f degrees. Quadrant=%d\n", precord->name, y, x, phase, quadrant )); #endif phase += phase_offset_correction; #ifdef DEBUG_PRINT DEBUGPRINT(DP_INFO, dbSubLlrfPactFlag, ("dbSubLlrfPact: Record %s adding correction of %f degrees\n", precord->name, phase_offset_correction)); #endif /* Ensure phase < 180 degrees */ if (phase > 180.) { phase -= 360.; #ifdef DEBUG_PRINT DEBUGPRINT(DP_INFO, dbSubLlrfPactFlag, ("dbSubLlrfPact: Record %s phase exceeds 180 degrees. Subtract 360.\n", precord->name)); #endif } /* Ensure phase > -180 degrees */ if (phase < (-180.)) { phase += 360.; #ifdef DEBUG_PRINT DEBUGPRINT(DP_INFO, dbSubLlrfPactFlag, ("dbSubLlrfPact: Record %s phase less than -180 degrees. Add 360.\n", precord->name)); #endif } precord->val = phase; #ifdef DEBUG_PRINT DEBUGPRINT(DP_INFO, dbSubLlrfPactFlag, ("dbSubLlrfPact: Record %s has phase=%f degrees.\n", precord->name, precord->val)); #endif return OK; } static long dbSubLlrfL1XPact(subRecord *precord) { double y; double x; double phase; int quadrant = 0; /* Assign subroutine inputs to y and x */ y=precord->a; x=precord->b; /* Determine the quadrant. Extra. Only for checking */ if (y>0) { if (x>0) { quadrant = 1; } else { if (x<0) { quadrant = 2; } else { if (x==0) { quadrant = 12; /* marker for boundary between I and II*/ } } } } else { if (y<0) { if (x>0) { quadrant = 4; } else { if (x<0) { quadrant = 3; } else { if (x==0) { quadrant = 34; /* marker for boundary between III and IV*/ } } } } else { if (y==0) { if (x>0) { quadrant = 41; /* marker for boundary between IV and I*/ } else { if (x<0) { quadrant = 23; /* marker for boundary between II and III*/ } else { if (x==0) { quadrant = 0; /* marker for the origin (0,0) */ } } } } } } phase = atan2(y,x); /* Convert radians to degrees */ phase = phase/PI*180.; #ifdef DEBUG_PRINT DEBUGPRINT(DP_INFO, dbSubLlrfPactL1XFlag, ("dbSubLlrfL1XPact: Record %s with inputs Q,I=(%f,%f) has phase=%f degrees. Quadrant=%d\n", precord->name, y, x, phase, quadrant )); #endif /* Ensure phase < 180 degrees */ if (phase > 180.) { phase -= 360.; #ifdef DEBUG_PRINT DEBUGPRINT(DP_INFO, dbSubLlrfPactL1XFlag, ("dbSubLlrfL1XPact: Record %s phase exceeds 180 degrees. Subtract 360.\n", precord->name)); #endif } /* Ensure phase > -180 degrees */ if (phase < (-180.)) { phase += 360.; #ifdef DEBUG_PRINT DEBUGPRINT(DP_INFO, dbSubLlrfPactL1XFlag, ("dbSubLlrfL1XPact: Record %s phase less than -180 degrees. Add 360.\n", precord->name)); #endif } precord->val = phase; #ifdef DEBUG_PRINT DEBUGPRINT(DP_INFO, dbSubLlrfPactL1XFlag, ("dbSubLlrfL1XPact: Record %s has phase=%f degrees.\n", precord->name, precord->val)); #endif return OK; } /* Calculate the phase as the y-intercept of the line given by the two phases and their midpoints in x */ static long dbSubLlrfPactFast(subRecord *precord) { double phase_offset_correction; double phase0; double phase1; double b; double slope; double deltax; double x_offset; DBADDR *inpAddr = dbGetPdbAddrFromLink(&precord->inpa); int inpNord, offset; struct rset *prset; double *inpP; /* New. to match what is getting passing in, in the array */ double channel=-1; double I0=0; double Q0=0; double X0=0; double I1=0; double Q1=0; double X1=0; /* * inpa must be a DB link and must be the proper type. */ if (!inpAddr) return ERROR; dbScanLock(inpAddr->precord); /* Tried to pass this test by declaring the VME copy of the waveform to be of type DOUBLE, even though it's a LONG on the PAD */ 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); channel=*inpP; I0=*(inpP+1); Q0=*(inpP+2); X0=*(inpP+3); I1=*(inpP+4); Q1=*(inpP+5); X1=*(inpP+6); } } else { inpP = 0; inpNord = 0; dbScanUnlock(inpAddr->precord); /* do before any return */ #ifdef DEBUG_PRINT DEBUGPRINT(DP_ERROR, dbSubLlrfPactFastFlag, ("dbSubLlrfPactFast: Record %s problem with INPA\n", precord->name)); #endif return ERROR; } dbScanUnlock(inpAddr->precord); /* Get the phase offset before going any further. It's in INPB */ /* NB: this correction is ONE PER CHANNEL, not per sample. */ phase_offset_correction = precord->b; /* See what we got */ #ifdef DEBUG_PRINT DEBUGPRINT(DP_INFO, dbSubLlrfPactFastFlag, ("dbSubLlrfPactFast: Record %s has sample0 Q,I=(%f,%f) and phase_offset_correction %f \n", precord->name, Q0, I0, phase_offset_correction)); #endif /**************************************************************/ /* Now start calculating for sample 0 */ /* Convert I0 and Q0 to phase */ /* y is Q; x is I */ /* Bring in code from dbSubLlrfPact */ /* delete the quadrant check stuff, tho */ phase0 = atan2(Q0,I0); /* Convert radians to degrees */ phase0 = phase0/PI*180.; #ifdef DEBUG_PRINT DEBUGPRINT(DP_INFO, dbSubLlrfPactFastFlag, ("dbSubLlrfPactFast: Record %s with inputs I0,Q0=(%f,%f) has phase0=%f degrees\n", precord->name, I0, Q0, phase0)); #endif phase0 += phase_offset_correction; /* Ensure phase < 180 degrees */ if (phase0 > 180.) { phase0 -= 360.; } /* Ensure phase > -180 degrees */ if (phase0 < (-180.)) { phase0 += 360.; } #ifdef DEBUG_PRINT DEBUGPRINT(DP_INFO, dbSubLlrfPactFastFlag, ("dbSubLlrfPactFast: Record %s (after offset and range limiting) phase0=%f degrees.\n", precord->name, phase0)); #endif /* End of import from dbSubLlrfPact */ /* Write stuff out so it can be displayed */ precord->d = X0; precord->e = phase0; /**************************************************************/ /* Now start calculating for sample 1 */ /* Convert I1 and Q1 to phase */ /* y is Q; x is I */ /* delete the quadrant check stuff, tho */ phase1 = atan2(Q1,I1); /* Convert radians to degrees */ phase1 = phase1/PI*180.; #ifdef DEBUG_PRINT DEBUGPRINT(DP_DEBUG, dbSubLlrfPactFastFlag, ("dbSubLlrfPactFast: Record %s with inputs I1,Q1=(%f,%f) has phase1=%f degrees\n", precord->name, I1, Q1, phase1)); #endif /* Note: phase_offset_correction is the SAME value for each sample, since the offset is for the channel */ phase1 += phase_offset_correction; /* Ensure phase < 180 degrees */ if (phase1 > 180.) { phase1 -= 360.; } /* Ensure phase > -180 degrees */ if (phase1 < (-180.)) { phase1 += 360.; } #ifdef DEBUG_PRINT DEBUGPRINT(DP_INFO, dbSubLlrfPactFlag, ("dbSubLlrfPactFast: Record %s (after offset and range limiting) phase1=%f degrees.\n", precord->name, phase1)); #endif /* Write stuff out so it can be displayed */ precord->f = X1; precord->g = phase1; #ifdef DEBUG_PRINT DEBUGPRINT(DP_INFO, dbSubLlrfPactFastFlag, ("dbSubLlrfPactFast: Record %s phase0=%g phase1=%g X0=%g X1=%g.\n", precord->name, phase0, phase1, X0, X1)); #endif /* Calculate slope */ deltax = X1 - X0; if ((deltax) < 0.0001 ) { /* line approaches vertical. Return the phase of the first sample instead of computing equation to line. */ precord->val = phase0; #ifdef DEBUG_PRINT DEBUGPRINT(DP_INFO, dbSubLlrfPactFastFlag, ("dbSubLlrfPactFast: Record %s line approaching vertical, use phase0=%g and skip eqn to line.\n", precord->name, precord->val)); #endif } else { x_offset = precord->c; slope = (phase1 - phase0)/deltax; precord->h = slope; b = phase0 - (slope * X0); precord->i = b; precord->val = slope * x_offset + b; #ifdef DEBUG_PRINT DEBUGPRINT(DP_INFO, dbSubLlrfPactFastFlag, ("dbSubLlrfPactFast: Record %s %g = %g * %g + %g.\n", precord->name, precord->val, slope, x_offset, b)); #endif } /* compute amplitude of sample0 */ precord->j = I0*I0 + Q0*Q0; #ifdef DEBUG_PRINT DEBUGPRINT(DP_INFO, dbSubLlrfPactFastFlag, ("dbSubLlrfPactFast: Record %s ampl0 is: %g\n", precord->name, precord->k)); #endif return OK; /* By this point, val is the actual phase at x_offset, i is phase0 j is phase1 k is ampl0 l is y-intercept */ } static long dbSubLlrfAset(subRecord *precord) { double desired_ampl; double actual_avg_ampl; double corrected_ampl; double corrected_ampl_upper_limit; double corrected_ampl_lower_limit; double ampl_setpt; double previous_ampl_setpt; long ampl_feedback_on; double ampl_smoothing_factor; double ampl_setpt_upper_limit; double ampl_setpt_lower_limit; double ampl_minimum_threshold; desired_ampl = precord->a; actual_avg_ampl = precord->b; corrected_ampl_upper_limit = precord->c; corrected_ampl_lower_limit = precord->d; previous_ampl_setpt = precord->e; ampl_feedback_on = precord->f; ampl_smoothing_factor = precord->g; ampl_setpt_upper_limit = precord->h; ampl_setpt_lower_limit = precord->i; ampl_minimum_threshold = precord->j; #ifdef DEBUG_PRINT DEBUGPRINT(DP_INFO, dbSubLlrfAsetFlag, ("dbSubLlrfAset: Record %s with inputs ADES=%f, AAVG=%f, ACOR_MAX=%f, ACOR_MIN=%f, PRV_ASET=%f, FB_ON=%ld, A_SMOOTH=%f, PWR_THLD=%f\n", precord->name, desired_ampl, actual_avg_ampl, corrected_ampl_upper_limit, corrected_ampl_lower_limit, previous_ampl_setpt, ampl_feedback_on, ampl_smoothing_factor, ampl_minimum_threshold)); #endif /* avoid divide by zero */ if (actual_avg_ampl > CLOSE_TO_ZERO ) { /* MegaVolts */ corrected_ampl = desired_ampl/actual_avg_ampl; #ifdef DEBUG_PRINT DEBUGPRINT(DP_INFO, dbSubLlrfAsetFlag, ("dbSubLlrfAset: Record %s corrected_ampl (MV) is %f\n", precord->name, corrected_ampl )); #endif } else { #ifdef DEBUG_PRINT DEBUGPRINT(DP_INFO, dbSubLlrfAsetFlag, ("dbSubLlrfAset: Record %s setting corrected_ampl to 1 to avoid divide by zero\n", precord->name )); #endif corrected_ampl = 1.0; /* BETTER WAY??? */ } /* clamp the magnitude of the corrected, only-ever-positive, amplitude */ if (corrected_ampl > corrected_ampl_upper_limit) { corrected_ampl = corrected_ampl_upper_limit; #ifdef DEBUG_PRINT DEBUGPRINT(DP_INFO, dbSubLlrfAsetFlag, ("dbSubLlrfAset: Record %s limiting corrected_ampl of %f to upper limit %f\n", precord->name, corrected_ampl, corrected_ampl_upper_limit)); #endif } if (corrected_ampl < corrected_ampl_lower_limit) { corrected_ampl = corrected_ampl_lower_limit; #ifdef DEBUG_PRINT DEBUGPRINT(DP_INFO, dbSubLlrfAsetFlag, ("dbSubLlrfAset: Record %s limiting corrected_ampl of %f to lower limit %f\n", precord->name, corrected_ampl, corrected_ampl_lower_limit)); #endif } if (ampl_feedback_on) { /* Is actual amplitude above the minimum threshold to feedback on? */ if (actual_avg_ampl > ampl_minimum_threshold) { ampl_setpt = ((ampl_smoothing_factor*(corrected_ampl - 1.0)+1)*previous_ampl_setpt); #ifdef DEBUG_PRINT DEBUGPRINT(DP_INFO, dbSubLlrfAsetFlag, ("dbSubLlrfAset: Record %s phase feedback is on and ampl:%f > thresh: %f.\n", precord->name, actual_avg_ampl, ampl_minimum_threshold)); DEBUGPRINT(DP_INFO, dbSubLlrfAsetFlag, ("dbSubLlrfAset: Record %s new setpt: %f calculated from smooth: %f * corrected ampl: %f + prev corr: %f\n", precord->name, ampl_setpt, ampl_smoothing_factor, corrected_ampl, previous_ampl_setpt)); #endif } else { ampl_setpt = previous_ampl_setpt; #ifdef DEBUG_PRINT DEBUGPRINT(DP_INFO, dbSubLlrfAsetFlag, ("dbSubLlrfAset: Record %s ampl is below minimum threshold %f required for feedback. Reuse previous setpt: %f\n", precord->name, ampl_minimum_threshold, ampl_setpt)); #endif } } else { ampl_setpt = previous_ampl_setpt; #ifdef DEBUG_PRINT DEBUGPRINT(DP_INFO, dbSubLlrfAsetFlag, ("dbSubLlrfAset: Record %s ampl feedback is off. Reuse previous setpt: %f\n", precord->name, ampl_setpt)); #endif } /* Clamp the range of the setpoint to limits */ if (ampl_setpt > ampl_setpt_upper_limit) { #ifdef DEBUG_PRINT DEBUGPRINT(DP_INFO, dbSubLlrfAsetFlag, ("dbSubLlrfAset: Record %s limiting ampl_setpt of %f to upper limit %f\n", precord->name, ampl_setpt, ampl_setpt_upper_limit)); #endif ampl_setpt = ampl_setpt_upper_limit; } if (ampl_setpt < ampl_setpt_lower_limit) { #ifdef DEBUG_PRINT DEBUGPRINT(DP_INFO, dbSubLlrfAsetFlag, ("dbSubLlrfAset: Record %s limiting ampl_setpt of %f to lower limit %f\n", precord->name, ampl_setpt, ampl_setpt_lower_limit)); #endif ampl_setpt = ampl_setpt_lower_limit; } precord->val = ampl_setpt; return (0); } static long dbSubLlrfPset(subRecord *precord) { double desired_phase; double actual_avg_phase; double corrected_phase; double corrected_phase_upper_limit; double corrected_phase_lower_limit; double phase_setpt; double previous_phase_setpt; long phase_feedback_on; double phase_smoothing_factor; double ampl_minimum_threshold; double actual_avg_ampl; previous_phase_setpt = precord->e; phase_feedback_on = precord->f; if (!phase_feedback_on) { phase_setpt = previous_phase_setpt; #ifdef DEBUG_PRINT DEBUGPRINT(DP_INFO, dbSubLlrfPsetFlag, ("dbSubLlrfPset: Record %s phase feedback is off. Reuse previous phase setpt: %f\n", precord->name, phase_setpt)); #endif } else { /* phase feedback on */ desired_phase = precord->a; actual_avg_phase = precord->b; corrected_phase_upper_limit = precord->c; corrected_phase_lower_limit = precord->d; phase_smoothing_factor = precord->g; ampl_minimum_threshold = precord->j; actual_avg_ampl = precord->k; /* Determine the required phase correction. This can be +ve or -ve. */ corrected_phase = (desired_phase - actual_avg_phase); #ifdef DEBUG_PRINT DEBUGPRINT(DP_INFO, dbSubLlrfPsetFlag, ("dbSubLlrfPset: Record %s with PDES=%f, PAVG=%f, PCOR_MAX=%f, PCOR_MIN=%f\n", precord->name, desired_phase,actual_avg_phase, corrected_phase_upper_limit, corrected_phase_lower_limit)); DEBUGPRINT(DP_INFO, dbSubLlrfPsetFlag, ("dbSubLlrfPset: Record %s with PRV_PSET=%f, FB_ON=%ld, P_SMOOTH=%f, PWR_THLD=%f, PWR_ACT=%f\n", precord->name, previous_phase_setpt, phase_feedback_on, phase_smoothing_factor, ampl_minimum_threshold, actual_avg_ampl)); #endif if (corrected_phase < 0.) { /* Correction is negative. PMIN and PMAX trade places, essentially */ if (corrected_phase < (-1.)*corrected_phase_upper_limit) { #ifdef DEBUG_PRINT DEBUGPRINT(DP_INFO, dbSubLlrfPsetFlag, ("dbSubLlrfPset: Record %s limiting corrected_phase %f to negative PCORMAX %f\n", precord->name, corrected_phase, (-1.)*corrected_phase_upper_limit)); #endif corrected_phase = (-1.)*corrected_phase_upper_limit; } if (corrected_phase > (-1.)*corrected_phase_lower_limit) { #ifdef DEBUG_PRINT DEBUGPRINT(DP_INFO, dbSubLlrfPsetFlag, ("dbSubLlrfPset: Record %s limiting corrected_phase of %f to 0\n", precord->name, corrected_phase)); #endif corrected_phase = 0.; } } else { /* Correction is zero or positive */ if (corrected_phase < corrected_phase_lower_limit) { #ifdef DEBUG_PRINT DEBUGPRINT(DP_INFO, dbSubLlrfPsetFlag, ("dbSubLlrfPset: Record %s limiting corrected_phase of %f to 0\n", precord->name, corrected_phase)); #endif corrected_phase = 0.; } if (corrected_phase > corrected_phase_upper_limit) { #ifdef DEBUG_PRINT DEBUGPRINT(DP_INFO, dbSubLlrfPsetFlag, ("dbSubLlrfPset: Record %s limiting corr phase of %f to PCORMAX %f\n", precord->name, corrected_phase, corrected_phase_upper_limit)); #endif corrected_phase = corrected_phase_upper_limit; } } /* Is actual amplitude above the minimum threshold? */ if (actual_avg_ampl > ampl_minimum_threshold) { phase_setpt = phase_smoothing_factor * corrected_phase + previous_phase_setpt; #ifdef DEBUG_PRINT DEBUGPRINT(DP_INFO, dbSubLlrfPsetFlag, ("dbSubLlrfPset: Record %s phase feedback is on and ampl:%f > thresh: %f.\n", precord->name, actual_avg_ampl, ampl_minimum_threshold)); DEBUGPRINT(DP_INFO, dbSubLlrfPsetFlag, ("%s: des: %f act: %f. new setpt: %f calced from %f * %f + prev: %f\n", precord->name, desired_phase, actual_avg_phase, phase_setpt, phase_smoothing_factor, corrected_phase, previous_phase_setpt)); #endif } else { phase_setpt = previous_phase_setpt; #ifdef DEBUG_PRINT DEBUGPRINT(DP_INFO, dbSubLlrfPsetFlag, ("dbSubLlrfPset: Record %s ampl is below minimum threshold %f required for feedback. Reuse previous phase setpt: %f\n", precord->name, ampl_minimum_threshold, phase_setpt)); #endif } } if (phase_setpt >= 360.) { phase_setpt -= 360.; } if (phase_setpt < -360.) { phase_setpt += 360.; } precord->val = phase_setpt; /* set up for next time: */ precord->e = previous_phase_setpt; #ifdef DEBUG_PRINT DEBUGPRINT(DP_INFO, dbSubLlrfFlag, ("dbSubLlrfPset: Record %s has phase=%f degrees.\n", precord->name, precord->val)); #endif return OK; } /* */ static long dbSubLlrfMDLPset(subRecord *precord) { double desired_phase; double error; double corrected_phase_upper_limit; double phase_setpt; double previous_phase_setpt; /* First, ensure valid step size; i.e. greater than zero */ corrected_phase_upper_limit = precord->b; if (corrected_phase_upper_limit < CLOSE_TO_ZERO) { /* no way to converge! */ #ifdef DEBUG_PRINT DEBUGPRINT(DP_ERROR, dbSubLlrfMDLPsetFlag, ("dbSubLlrfMDLPset: Record %s: max step is %g. No way to converge.\n", precord->name, corrected_phase_upper_limit)); #endif return(ERROR); } /* Step size is ok. Calculate a setpoint... */ desired_phase = precord->a; previous_phase_setpt = precord->c; /* Determine the amount that the desired phase differs from the last desired phase. This can be +ve or -ve. */ error = (desired_phase - previous_phase_setpt); #ifdef DEBUG_PRINT DEBUGPRINT(DP_INFO, dbSubLlrfMDLPsetFlag, ("dbSubLlrfMDLPset: Record %s: des: %f. prv pset: %f. error: %f. max step: %f.\n", precord->name, desired_phase, previous_phase_setpt, error, corrected_phase_upper_limit)); #endif if ((error > CLOSE_TO_ZERO) || (error < (-1)*CLOSE_TO_ZERO)) { /* there is work to do */ if (error < 0.) { /* Case where desired_phase < actual phase. Setpoint will be lower than actual phase by either the error, or if that is too big, then by the max correction allowed. Check if error exceeds bounds for a single step */ if (error < (-1.)*corrected_phase_upper_limit) { /* It exceeds, so clamp */ phase_setpt = previous_phase_setpt - corrected_phase_upper_limit; } else { /* Single step to desired is possible */ phase_setpt = previous_phase_setpt + error; /* error already has -ve sign */ } } else { /* Case where desired_phase > actual phase Setpoint will be higher than actual phase by either the error, or if that is too big, then by the max correction allowed. Check if error exceeds bounds for a single step */ if (error > corrected_phase_upper_limit) { /* It exceeds, so clamp */ phase_setpt = previous_phase_setpt + corrected_phase_upper_limit; } else { /* Single step to desired is possible */ phase_setpt = previous_phase_setpt + error; } } #ifdef DEBUG_PRINT DEBUGPRINT(DP_INFO, dbSubLlrfMDLPsetFlag, ("dbSubLlrfMDLPset: Record %s: new setpoint: %f\n", precord->name, phase_setpt)); #endif if (phase_setpt > 180.) { /* 5/5/8 took out the equals sign because, for a pdes of exactly 180, this if was passing and phase setpt was getting set to -180!!!! */ phase_setpt -= 360.; #ifdef DEBUG_PRINT DEBUGPRINT(DP_INFO, dbSubLlrfMDLPsetFlag, ("dbSubLlrfMDLPset: Record %s: wrapping new setpoint to: %f\n", precord->name, phase_setpt)); #endif } if (phase_setpt < -180.) { phase_setpt += 360.; #ifdef DEBUG_PRINT DEBUGPRINT(DP_INFO, dbSubLlrfMDLPsetFlag, ("dbSubLlrfMDLPset: Record %s: wrapping new setpoint to: %f\n", precord->name, phase_setpt)); #endif } } else { /* no work to do */ phase_setpt = previous_phase_setpt; #ifdef DEBUG_PRINT DEBUGPRINT(DP_INFO, dbSubLlrfMDLPsetFlag, ("dbSubLlrfMDLPset: Record %s error is small (%f). Setpt = previous setpt.\n", precord->name, error)); #endif } precord->val = phase_setpt; /* set up for next time: */ precord->c = previous_phase_setpt; #ifdef DEBUG_PRINT DEBUGPRINT(DP_INFO, dbSubLlrfMDLPsetFlag, ("dbSubLlrfMDLPset: Record %s phase setpoint=%f degrees.\n", precord->name, precord->val)); #endif return(OK); } /* Look at the average of the gun body temperature and compare to the range defining "on resonance" and set val accordingly. Then use val as input to an mbbo so that meaningful string can be displayed Problem: this record can't return a '1' without becoming invalid and not executing any more. I was hoping to set 1 as val and trigger the mbbi that gun is off resonance... */ static long dbSubLlrfGunRes(subRecord *precord) { int gunBodyInside; int gunBodyOutside; int lowTemperature; int highTemperature; int gunBodyAverage; gunBodyInside = precord->a; gunBodyOutside = precord->b; highTemperature = precord->c; lowTemperature = precord->d; gunBodyAverage = (gunBodyInside+gunBodyOutside)/2; if (gunBodyAverage > highTemperature) { #ifdef DEBUG_PRINT DEBUGPRINT(DP_INFO, dbSubLlrfGunFlag, ("dbSubLlrfGunRes: Record %s average gun body temperature %d degC exceeds max %d \n", precord->name, gunBodyAverage, highTemperature)); #endif /*precord->stat = HIGH_ALARM; precord->sevr = MINOR_ALARM; */ } else { if (gunBodyAverage < lowTemperature) { #ifdef DEBUG_PRINT DEBUGPRINT(DP_INFO, dbSubLlrfGunFlag, ("dbSubLlrfGunRes: Record %s average gun body temperature %d degC under min %d \n", precord->name, gunBodyAverage, lowTemperature)); #endif /*precord->stat = LOW_ALARM; precord->sevr = MINOR_ALARM;*/ } else { #ifdef DEBUG_PRINT DEBUGPRINT(DP_INFO, dbSubLlrfGunFlag, ("dbSubLlrfGunRes: Record %s average gun body temperature %d degC on resonance\n", precord->name, gunBodyAverage)); #endif /*precord->stat = NO_ALARM; precord->sevr = NO_ALARM;*/ } } return 0; } static long dbSubLlrfPabsorb(subRecord *precord) { double y; double x; double phase; int quadrant = 0; int feedback_pending; int feedback_status; double setpoint; int setpoint_enable; /* Assign subroutine inputs to y and x */ feedback_pending=(int) precord->a; y=precord->b; x=precord->c; feedback_status=(int)precord->d; setpoint=precord->e; setpoint_enable=(int)precord->f; if (feedback_pending == 1) { /* absorb current I and Q settings as starting point for desired phase */ #ifdef DEBUG_PRINT DEBUGPRINT(DP_INFO, dbSubLlrfPabsorbFlag, ("dbSubLlrfPabsorb: Record %s feedback requested. Absorb necessary.\n", precord->name)); #endif /* Determine the quadrant. Extra. Only for checking */ if (y>0) { if (x>0) { quadrant = 1; } else { if (x<0) { quadrant = 2; } else { if (x==0) { quadrant = 12; /* marker for boundary between I and II*/ } } } } else { if (y<0) { if (x>0) { quadrant = 4; } else { if (x<0) { quadrant = 3; } else { if (x==0) { quadrant = 34; /* marker for boundary between III and IV*/ } } } } else { if (y==0) { if (x>0) { quadrant = 41; /* marker for boundary between IV and I*/ } else { if (x<0) { quadrant = 23; /* marker for boundary between II and III*/ } else { if (x==0) { quadrant = 0; /* marker for the origin (0,0) */ } } } } } } phase = atan2(y,x); /* Convert radians to degrees */ phase = phase/PI*180.; #ifdef DEBUG_PRINT DEBUGPRINT(DP_INFO, dbSubLlrfPabsorbFlag, ("dbSubLlrfPabsorb: Record %s with inputs I,Q=(%f,%f) has phase=%f degrees. Quadrant=%d\n", precord->name, x, y, phase, quadrant )); #endif /* Ensure phase < 180 degrees */ if (phase > 180.) { phase -= 360.; } /* Ensure phase > -180 degrees */ if (phase < (-180.)) { phase += 360.; } precord->val = phase; /* VAL NOT USED FOR ANYTHING except sanity check. */ /* Actually, the value of the subroutine doesn't matter. Want to put phas into value of PSET record. Need to process PSET? */ setpoint=phase; precord->e=phase; /* set PHAS_FB binary output val to ON. Need to process PHAS_FB? */ feedback_status=1; /* this only sets local var, not 'D' in the record! */ precord->d=(double)1; /* must write to 'D' directly */ /* enable PSET now that it has the correct value */ setpoint_enable = 0; /* this only sets local var, not 'E' in the record! */ precord->f =(double)0; #ifdef DEBUG_PRINT DEBUGPRINT(DP_INFO, dbSubLlrfPabsorbFlag, ("dbSubLlrfPabsorb: Record %s has phase=%g degrees.\n", precord->name, precord->e)); DEBUGPRINT(DP_INFO, dbSubLlrfPabsorbFlag, ("dbSubLlrfPabsorb: Record %s putting phase=%g into PSET and writing %g to its DISA.\n", precord->name, precord->e, precord->f)); DEBUGPRINT(DP_INFO, dbSubLlrfPabsorbFlag, ("dbSubLlrfPabsorb: Record %s setting feedback_status to %g\n", precord->name, precord->d)); #endif } else { #ifdef DEBUG_PRINT DEBUGPRINT(DP_INFO, dbSubLlrfPabsorbFlag, ("dbSubLlrfPabsorb: Record %s feedback request turned off. No absorb. Turn feedback off.\n", precord->name)); #endif precord->d=(double)0; /* turn feedback off */ } return OK; } static long dbSubLlrfAabsorb(subRecord *precord) { double y; double x; double ampl; long feedback_pending; long feedback_status; double setpoint; long setpoint_enable; /* Assign subroutine inputs to y and x */ feedback_pending=precord->a; y=precord->b; x=precord->c; feedback_status=precord->d; setpoint=precord->e; setpoint_enable=precord->f; /* Feedback is pending. Before we turn it on, set up the setpoint from current I and Q settings */ if (feedback_pending == 1) { ampl = sqrt((y*y) + (x*x)); /* Normalize? */ #ifdef DEBUG_PRINT DEBUGPRINT(DP_INFO, dbSubLlrfAabsorbFlag, ("dbSubLlrfAabsorb: Record %s with inputs I,Q=(%f,%f) has ampl=%f degrees\n", precord->name, x, y, ampl)); #endif /* Ensure ampl in range... */ precord->val = ampl; /* Actually, the value of the subroutine doesn't matter. Want to put ampl into value of ASET record. Need to process ASET? */ setpoint=ampl; /* set AMPL_FB binary output val to ON. Need to process AMPL_FB? */ feedback_status=1; /* enable ASET now that it has the correct value */ setpoint_enable = 0; #ifdef DEBUG_PRINT DEBUGPRINT(DP_INFO, dbSubLlrfAabsorbFlag, ("dbSubLlrfAabsorb: Record %s has ampl=%f (units).\n", precord->name, precord->val)); DEBUGPRINT(DP_INFO, dbSubLlrfAabsorbFlag, ("dbSubLlrfAabsorb: Record %s putting ampl=%f into ASET.\n", precord->name, precord->e)); DEBUGPRINT(DP_INFO, dbSubLlrfAabsorbFlag, ("dbSubLlrfAabsorb: Record %s setting feedback_status ON\n", precord->name)); #endif } else { #ifdef DEBUG_PRINT DEBUGPRINT(DP_INFO, dbSubLlrfAabsorbFlag, ("dbSubLlrfAabsorb: Record %s feedback pending is off. No absorb necessary.\n", precord->name)); DEBUGPRINT(DP_INFO, dbSubLlrfAabsorbFlag, ("dbSubLlrfAabsorb: Record %s setting feedback_status OFF\n", precord->name)); #endif /* set AMPL_FB binary output val to OFF. Need to process it? */ feedback_status=0; } return OK; } static long dbSubLlrfPID(subRecord *precord) { double ki; double kp; double readout; double setpoint; double upper_limit; double lower_limit; long pid_control; /* indicates whether PID loop is on or off */ long pid_control_previous; /* indication whether feedback was on or off last time */ double p; /* local variable to hold the proportional term for this iteration */ double i; /* local variable to hold the integral term for this iteration only. It's added to pid_sum */ double new_calc; /* local variable to hold the new setpoint for this iteration */ double rate = 1.; /* Later: parse from SCAN field*/ static double offset; /* Later: can't get rid of this static unless an INP is freed up */ double pid_sum; /* used to hold the cumulative sum from the previous iterations and gets added to 'i' term for this iteration */ double pid_sum_upper_limit; double pid_sum_lower_limit; /* long plc_accepting_pid_setpoint; OMSL field of low conductivity water temperature setpoint */ static long first_process_after_reboot=1; readout = precord->a; setpoint = precord->b; kp = precord->d; ki = precord->e; upper_limit = precord->f; lower_limit = precord->g; pid_sum = precord->h; /* integral is stored in this field, zeroed each time PID is turned off */ pid_control = (long) precord->i; pid_sum_upper_limit = precord->j; pid_sum_lower_limit = precord->k; pid_control_previous = precord->l; /*plc_accepting_pid_setpoint = precord->m; there are only 'l' fields! */ /* Avoid sending a 0 to low conductivity water temperature at startup */ if (first_process_after_reboot) { first_process_after_reboot = 0; precord->val = precord->c; printf("initializing _TEMP_PID to starting value %g\n", precord->c); return OK; } if (pid_control == 1) { /* PID loop is ON */ if (1 /* plc_accepting_pid_setpoint */) { /* It ALWAYS goes here until a spare INP is freed up for the OMSL field */ /* OMSL field of RF gun low conductivity water temperature setpoint will accept values from this subroutine PV */ if (pid_control_previous == 0 ) { /* PID loop was OFF last iteration. Do some setup for this invocation of the loop Use the value of what we want to control as the offset for the new setpoint. This offset will be constant for the entire time feedback is ON. */ offset = precord->c; #ifdef DEBUG_PRINT DEBUGPRINT(DP_INFO, dbSubLlrfPIDFlag, ("dbSubLlrfPID: Record %s PID control turned ON. Set offset to %g\n", precord->name, offset)); #endif /* precord->n = offset; do this (if enough fields to get rid of static offset */ } p = kp * (setpoint - readout); /* use another INP field to hold 'p' so that can display on GUI precord->o = p; there are only 'l' fields! */ #ifdef DEBUG_PRINT DEBUGPRINT(DP_INFO, dbSubLlrfPIDFlag, ("dbSubLlrfPID: Record %s P: %g * (%g - %g) = %g\n", precord->name, kp, setpoint, readout, p)); #endif #ifdef DEBUG_PRINT DEBUGPRINT(DP_INFO, dbSubLlrfPIDFlag, ("dbSubLlrfPID: Record %s I: %g + ", precord->name, pid_sum)); #endif i = pid_sum + ki * (setpoint - readout)/rate; /* Limit size of i-term */ if (i < pid_sum_lower_limit) { i = pid_sum_lower_limit; } else { if (i > pid_sum_upper_limit) { i = pid_sum_upper_limit; } } #ifdef DEBUG_PRINT DEBUGPRINT(DP_INFO, dbSubLlrfPIDFlag, ("%g * (%g - %g)/%g = %g\n", ki, setpoint, readout, rate, i)); #endif new_calc = p + i + offset; #ifdef DEBUG_PRINT DEBUGPRINT(DP_INFO, dbSubLlrfPIDFlag, ("dbSubLlrfPID: Record %s New calc: %g + %g + %g) = %g\n", precord->name, p, i, offset, new_calc)); #endif if (new_calc > upper_limit) { new_calc = upper_limit; #ifdef DEBUG_PRINT DEBUGPRINT(DP_INFO, dbSubLlrfPIDFlag, ("dbSubLlrfPID: Record %s New calc %g constrained to max: %g\n", precord->name, new_calc, upper_limit)); #endif } else { if (new_calc < lower_limit) { new_calc = lower_limit; #ifdef DEBUG_PRINT DEBUGPRINT(DP_INFO, dbSubLlrfPIDFlag, ("dbSubLlrfPID: Record %s New calc %g constrained to min: %g\n", precord->name, new_calc, lower_limit)); #endif } } /* Write out the cumulative integral (includes pid_sum) to 'h' for next time */ precord->h = i; #ifdef DEBUG_PRINT DEBUGPRINT(DP_INFO, dbSubLlrfPIDFlag, ("dbSubLlrfPID: Record %s assigning latest sum term, %g, to H\n", precord->name, precord->h)); #endif precord->val = new_calc; #ifdef DEBUG_PRINT DEBUGPRINT(DP_INFO, dbSubLlrfPIDFlag, ("dbSubLlrfPID: Record %s New _LCW_SP is: %g\n", precord->name, precord->val)); #endif } else { /* It can never get here until a spare INP is freed up for the OMSL field */ /* There is a problem: PID control is ON but low conductivity water temperature setpoint OMSL field is set to "supervisory" so no vals can get through */ #ifdef DEBUG_PRINT DEBUGPRINT(DP_ERROR, dbSubLlrfPIDFlag, ("dbSubLlrfPID: Record %s unable to write new LCW_SP because its OMSL field is supervisory\n", precord->name)); #endif } } else { /* PID loop is OFF */ if (pid_control_previous == 1 ) { /* PID loop was ON last iteration */ #ifdef DEBUG_PRINT DEBUGPRINT(DP_INFO, dbSubLlrfPIDFlag, ("dbSubLlrfPID: Record %s PID control turned OFF. Zeroing sum\n", precord->name)); #endif precord->h = 0; /* zero the sum when feedback turned off */ } /* Note: precord->val is not set when feedback is off. The record is still processing at 1 Hz, but without a new val, no monitor is posted and so softIoc doesn't get any new val */ } /* Write pid_control into field that will be read as pid_control_previous next iteration */ precord->l = (double) pid_control; #ifdef DEBUG_PRINT DEBUGPRINT(DP_INFO, dbSubLlrfPIDFlag, ("dbSubLlrfPID: Record %s writing PID control %g to L\n", precord->name, precord->l)); #endif return OK; } static long dbSubLlrfPID2(subRecord *precord) { double ki; double kp; double readout; double setpoint; double upper_limit; double lower_limit; long pid_control; /* indicates whether PID loop is on or off */ long pid_control_previous; /* indication whether feedback was on or off last time */ double p; /* local variable to hold the proportional term for this iteration */ double i; /* local variable to hold the integral term for this iteration only. It's added to pid_sum */ double new_calc; /* local variable to hold the new setpoint for this iteration */ double rate = 1.; /* Later: parse from SCAN field*/ static double offset; /* Later: can't get rid of this static unless an INP is freed up */ double pid_sum; /* used to hold the cumulative sum from the previous iterations and gets added to 'i' term for this iteration */ double pid_sum_upper_limit; double pid_sum_lower_limit; /* long plc_accepting_pid_setpoint; OMSL field of low conductivity water temperature setpoint */ static long first_process_after_reboot=1; readout = precord->a; setpoint = precord->b; kp = precord->d; ki = precord->e; upper_limit = precord->f; lower_limit = precord->g; pid_sum = precord->h; /* integral is stored in this field, zeroed each time PID2 is turned off */ pid_control = (long) precord->i; pid_sum_upper_limit = precord->j; pid_sum_lower_limit = precord->k; pid_control_previous = precord->l; /*plc_accepting_pid_setpoint = precord->m; there are only 'l' fields! */ /* Avoid sending a 0 to low conductivity water temperature at startup */ if (first_process_after_reboot) { first_process_after_reboot = 0; precord->val = precord->c; printf("initializing _PHAS_PID to starting value %g\n", precord->c); return OK; } if (pid_control == 1) { /* PID2 loop is ON */ if (1 /* plc_accepting_pid_setpoint */) { /* It ALWAYS goes here until a spare INP is freed up for the OMSL field */ /* OMSL field of RF gun low conductivity water temperature setpoint will accept values from this subroutine PV */ if (pid_control_previous == 0 ) { /* PID2 loop was OFF last iteration. Do some setup for this invocation of the loop Use the value of what we want to control as the offset for the new setpoint. This offset will be constant for the entire time feedback is ON. */ offset = precord->c; #ifdef DEBUG_PRINT DEBUGPRINT(DP_INFO, dbSubLlrfPID2Flag, ("dbSubLlrfPID2: Record %s PID2 control turned ON. Set offset to %g\n", precord->name, offset)); #endif /* precord->n = offset; do this (if enough fields to get rid of static offset */ } p = kp * (setpoint - readout); /* use another INP field to hold 'p' so that can display on GUI precord->o = p; there are only 'l' fields! */ #ifdef DEBUG_PRINT DEBUGPRINT(DP_INFO, dbSubLlrfPID2Flag, ("dbSubLlrfPID2: Record %s P: %g * (%g - %g) = %g\n", precord->name, kp, setpoint, readout, p)); #endif #ifdef DEBUG_PRINT DEBUGPRINT(DP_INFO, dbSubLlrfPID2Flag, ("dbSubLlrfPID2: Record %s I: %g + ", precord->name, pid_sum)); #endif i = pid_sum + ki * (setpoint - readout)/rate; /* Limit size of i-term */ if (i < pid_sum_lower_limit) { i = pid_sum_lower_limit; } else { if (i > pid_sum_upper_limit) { i = pid_sum_upper_limit; } } #ifdef DEBUG_PRINT DEBUGPRINT(DP_INFO, dbSubLlrfPID2Flag, ("%g * (%g - %g)/%g = %g\n", ki, setpoint, readout, rate, i)); #endif new_calc = p + i + offset; #ifdef DEBUG_PRINT DEBUGPRINT(DP_INFO, dbSubLlrfPID2Flag, ("dbSubLlrfPID2: Record %s New calc: %g + %g + %g) = %g\n", precord->name, p, i, offset, new_calc)); #endif if (new_calc > upper_limit) { new_calc = upper_limit; #ifdef DEBUG_PRINT DEBUGPRINT(DP_INFO, dbSubLlrfPID2Flag, ("dbSubLlrfPID2: Record %s New calc %g constrained to max: %g\n", precord->name, new_calc, upper_limit)); #endif } else { if (new_calc < lower_limit) { new_calc = lower_limit; #ifdef DEBUG_PRINT DEBUGPRINT(DP_INFO, dbSubLlrfPID2Flag, ("dbSubLlrfPID2: Record %s New calc %g constrained to min: %g\n", precord->name, new_calc, lower_limit)); #endif } } /* Write out the cumulative integral (includes pid_sum) to 'h' for next time */ precord->h = i; #ifdef DEBUG_PRINT DEBUGPRINT(DP_INFO, dbSubLlrfPID2Flag, ("dbSubLlrfPID2: Record %s assigning latest sum term, %g, to H\n", precord->name, precord->h)); #endif precord->val = new_calc; #ifdef DEBUG_PRINT DEBUGPRINT(DP_INFO, dbSubLlrfPID2Flag, ("dbSubLlrfPID2: Record %s New _LCW_SP would be set to: %g if PID2 was used\n", precord->name, precord->val)); #endif } else { /* It can never get here until a spare INP is freed up for the OMSL field */ /* There is a problem: PID2 control is ON but low conductivity water temperature setpoint OMSL field is set to "supervisory" so no vals can get through */ #ifdef DEBUG_PRINT DEBUGPRINT(DP_ERROR, dbSubLlrfPID2Flag, ("dbSubLlrfPID2: Record %s unable to write new LCW_SP because its OMSL field is supervisory\n", precord->name)); #endif } } else { /* PID2 loop is OFF */ if (pid_control_previous == 1 ) { /* PID2 loop was ON last iteration */ #ifdef DEBUG_PRINT DEBUGPRINT(DP_INFO, dbSubLlrfPID2Flag, ("dbSubLlrfPID2: Record %s PID2 control turned OFF. Zeroing sum\n", precord->name)); #endif precord->h = 0; /* zero the sum when feedback turned off */ } /* Note: precord->val is not set when feedback is off. The record is still processing at 1 Hz, but without a new val, no monitor is posted and so softIoc doesn't get any new val */ } /* Write pid_control into field that will be read as pid_control_previous next iteration */ precord->l = (double) pid_control; #ifdef DEBUG_PRINT DEBUGPRINT(DP_INFO, dbSubLlrfPID2Flag, ("dbSubLlrfPID2: Record %s writing PID2 control %g to L\n", precord->name, precord->l)); #endif return OK; } static long dbSubLlrfL1XPset(subRecord *precord) { double phase_error; double corrected_phase; double corrected_phase_upper_limit; double corrected_phase_lower_limit; double phase_setpt; double previous_phase_setpt; long phase_feedback_on; double phase_smoothing_factor; double ampl_minimum_threshold; double actual_avg_ampl; previous_phase_setpt = precord->d; phase_feedback_on = precord->e; if (!phase_feedback_on) { phase_setpt = previous_phase_setpt; #ifdef DEBUG_PRINT DEBUGPRINT(DP_INFO, dbSubLlrfL1XPsetFlag, ("dbSubLlrfL1XPset: Record %s phase feedback is off. Reuse previous phase setpt: %f\n", precord->name, phase_setpt)); #endif } else { /* phase feedback on */ phase_error = -1. * (precord->a); corrected_phase_upper_limit = precord->b; corrected_phase_lower_limit = precord->c; phase_smoothing_factor = precord->f; ampl_minimum_threshold = precord->g; actual_avg_ampl = precord->h; /* Determine the required phase correction. This can be +ve or -ve. */ corrected_phase = phase_error; #ifdef DEBUG_PRINT DEBUGPRINT(DP_INFO, dbSubLlrfL1XPsetFlag, ("dbSubLlrfL1XPset: Record %s with ERROR=%f, PCOR_MAX=%f, PCOR_MIN=%f\n", precord->name, phase_error, corrected_phase_upper_limit, corrected_phase_lower_limit)); DEBUGPRINT(DP_INFO, dbSubLlrfL1XPsetFlag, ("dbSubLlrfL1XPset: Record %s with PRV_PSET=%f, FB_ON=%ld, P_SMOOTH=%f, PWR_THLD=%f, PWR_ACT=%f\n", precord->name, previous_phase_setpt, phase_feedback_on, phase_smoothing_factor, ampl_minimum_threshold, actual_avg_ampl)); #endif if (corrected_phase < 0.) { /* Correction is negative. PMIN and PMAX trade places, essentially */ if (corrected_phase < (-1.)*corrected_phase_upper_limit) { #ifdef DEBUG_PRINT DEBUGPRINT(DP_INFO, dbSubLlrfL1XPsetFlag, ("dbSubLlrfL1XPset: Record %s limiting corrected_phase %f to negative PCORMAX %f\n", precord->name, corrected_phase, (-1.)*corrected_phase_upper_limit)); #endif corrected_phase = (-1.)*corrected_phase_upper_limit; } if (corrected_phase > (-1.)*corrected_phase_lower_limit) { #ifdef DEBUG_PRINT DEBUGPRINT(DP_INFO, dbSubLlrfL1XPsetFlag, ("dbSubLlrfL1XPset: Record %s limiting corrected_phase of %f to 0\n", precord->name, corrected_phase)); #endif corrected_phase = 0.; } } else { /* Correction is zero or positive */ if (corrected_phase < corrected_phase_lower_limit) { #ifdef DEBUG_PRINT DEBUGPRINT(DP_INFO, dbSubLlrfL1XPsetFlag, ("dbSubLlrfL1XPset: Record %s limiting corrected_phase of %f to 0\n", precord->name, corrected_phase)); #endif corrected_phase = 0.; } if (corrected_phase > corrected_phase_upper_limit) { #ifdef DEBUG_PRINT DEBUGPRINT(DP_INFO, dbSubLlrfL1XPsetFlag, ("dbSubLlrfL1XPset: Record %s limiting corr phase of %f to PCORMAX %f\n", precord->name, corrected_phase, corrected_phase_upper_limit)); #endif corrected_phase = corrected_phase_upper_limit; } } /* Is actual amplitude above the minimum threshold? */ if (actual_avg_ampl > ampl_minimum_threshold) { phase_setpt = phase_smoothing_factor * corrected_phase + previous_phase_setpt; #ifdef DEBUG_PRINT DEBUGPRINT(DP_INFO, dbSubLlrfL1XPsetFlag, ("dbSubLlrfL1XPset: Record %s phase feedback is on and ampl:%f > thresh: %f.\n", precord->name, actual_avg_ampl, ampl_minimum_threshold)); DEBUGPRINT(DP_INFO, dbSubLlrfL1XPsetFlag, ("%s: error: %f. new setpt: %f calced from %f * %f + prev: %f\n", precord->name, phase_error, phase_setpt, phase_smoothing_factor, corrected_phase, previous_phase_setpt)); #endif } else { phase_setpt = previous_phase_setpt; #ifdef DEBUG_PRINT DEBUGPRINT(DP_INFO, dbSubLlrfL1XPsetFlag, ("dbSubLlrfL1XPset: Record %s ampl is below minimum threshold %f required for feedback. Reuse previous phase setpt: %f\n", precord->name, ampl_minimum_threshold, phase_setpt)); #endif } } if (phase_setpt >= 180.) { phase_setpt -= 360.; } if (phase_setpt < -180.) { phase_setpt += 360.; } precord->val = phase_setpt; /* set up for next time: */ precord->d = previous_phase_setpt; #ifdef DEBUG_PRINT DEBUGPRINT(DP_INFO, dbSubLlrfL1XPsetFlag, ("dbSubLlrfL1XPset: Record %s has phase=%f degrees.\n", precord->name, precord->val)); #endif return OK; } static long dbSubLlrfSetMinPowerThreshold(subRecord *precord) { #ifdef DEBUG_PRINT DEBUGPRINT(DP_INFO, dbSubLlrfFlag, ("dbSubLlrfSetMinPowerThreshold: Record %s has station: %d and threshold: %g\n", precord->name, (int) precord->a, precord->val)); #endif llrfSetMinPowerThreshold((int) precord->a, precord->val); return OK; } #ifdef NOT_YET /* 10/7/2008 new */ static long dbSubLlrfAdjustPac(subRecord *precord) { double phase_setpt; double desired_ampl; double i_scale; double q_scale; long pac_card; long pac_data_kind; long pac_write_order; short i_setpt; short q_setpt; int rc; desired_ampl = precord->a; phase_setpt = precord->b; i_scale = precord->c; q_scale = precord->d; pac_card = (long) precord->e; pac_data_kind = (long) precord->f; pac_write_order = (long) precord->g; /* compute values to put in udp broadcast to PAC */ i_setpt = (short) (desired_ampl * cos(phase_setpt * PI/180.) * i_scale); q_setpt = (short) (desired_ampl * sin(phase_setpt * PI/180) * q_scale); rc = llrfPacSend(i_setpt, q_setpt, pac_card, pac_data_kind, pac_write_order); #ifdef DEBUG_PRINT DEBUGPRINT(DP_INFO, dbSubLlrfAdjPacFlag, ("dbSubLlrfAdjustPac: Record %s has phas: %g, ampl: %g, iscl: %g, qscl: %g\n", precord->name, phase_setpt, desired_ampl, i_scale, q_scale)); DEBUGPRINT(DP_INFO, dbSubLlrfAdjPacFlag, ("PAC: %ld, kind: %ld, order: %ld\n", (long) pac_card, (long) pac_data_kind, (long) pac_write_order)); DEBUGPRINT(DP_INFO, dbSubLlrfAdjPacFlag, ("i setpt: %hd, q setpt: %hd\n", i_setpt, q_setpt)); DEBUGPRINT(DP_INFO, dbSubLlrfAdjPacFlag, ("rc: %ld\n", rc)); #endif return (i_setpt<<16) | (q_setpt & 0xffff); } #endif /* Register these symbols for use by IOC code:/ */ epicsRegisterFunction(dbSubLlrfInit); epicsRegisterFunction(dbSubLlrfPact); epicsRegisterFunction(dbSubLlrfPactFast); epicsRegisterFunction(dbSubLlrfL1XPact); epicsRegisterFunction(dbSubLlrfPset); epicsRegisterFunction(dbSubLlrfMDLPset); epicsRegisterFunction(dbSubLlrfL1XPset); epicsRegisterFunction(dbSubLlrfAset); epicsRegisterFunction(dbSubLlrfGunRes); epicsRegisterFunction(dbSubLlrfPabsorb); epicsRegisterFunction(dbSubLlrfAabsorb); epicsRegisterFunction(dbSubLlrfPID); epicsRegisterFunction(dbSubLlrfPID2); epicsRegisterFunction(dbSubLlrfSetMinPowerThreshold); #ifdef NOT_YET epicsRegisterFunction(dbSubLlrfAdjustPac); #endif