/*
 *        RFPhasecontrol
 *        Auth: KHKIM
 *        
 *
 *
*/

program RFPhaseControl

%{
#include <math.h>
#include <epicsPrint.h>
#define TRUE   1
#define FLASE  0
#define M_PI           3.14159265358979323846  /* pi */
#define DEBUG_
#define DEBUG_MONPV
#define DEBUG_STATUS_
#define PERFORM_FEEDBACK
}%

double pvs[50];
assign pvs to {
                 "SIOC:SYS0:ML00:AO057",   /* index 0 */ /* phase control running: counter */   
                 "SIOC:SYS0:ML00:AO058",   /* index 1 */ /* fancy phase control ON/OFF  */
                 "SIOC:SYS0:ML00:AO059",   /* index 2 */ /* L2 Energy */
                 "SIOC:SYS0:ML00:AO060",   /* index 3 */ /* BC1 phase delay */
                 "SIOC:SYS0:ML00:AO061",   /* index 4 */ /* L2 phase control */
                 "SIOC:SYS0:ML00:AO062",   /* index 5 */ /* L2 phase offset */
                 "SIOC:SYS0:ML00:AO063",   /* index 6 */ /* BC2 phase delay */
                 "SIOC:SYS0:ML00:AO064",   /* index 7 */ /* L3 phase control */
                 "SIOC:SYS0:ML00:AO065",   /* index 8 */ /* L3 phase offset */
                 "LLRF:IN20:RH:REF_2_PDES", /* index 9 */ 
                 "LLRF:IN20:RH:L2_PDES",   /* index 10 */
                 "ACCL:LI24:100:KLY_POC",  /* index 11 */
                 "ACCL:LI24:300:KLY_POC",  /* index 12 */ /* swap 24-2 and 24-3 */
                 "ACCL:LI24:200:KLY_POC",  /* index 13 */  /* swap 24-2 and 24-3 */
                 "TCAV:LI24:800:TC3_PDES", /* index 14 */
                 "TCAV:LI24:800:TC3_0_POC", /* index 15 */
                 "TCAV:LI24:800:TC3_1_POC", /* index 16 */
                 "LLRF:IN20:RH:REF_1_S_PA.HIHI", /* index 17 */
                 "LLRF:IN20:RH:REF_1_S_PA.HIGH", /* index 18 */
                 "LLRF:IN20:RH:REF_1_S_PA.LOW",  /* index 19 */
                 "LLRF:IN20:RH:REF_1_S_PA.LOLO", /* index 20 */
                 "SIOC:SYS0:ML00:AO066",    /* index 21 */ /* phase control busy */
                 "SIOC:SYS0:ML00:AO278",    /* index 22 */ /* L2 nofb flat total energy */
                 "SIOC:SYS0:ML00:AO067",    /* index 23 */ /* L2 Fudge input */
                 "SIOC:SYS0:ML00:AO080",    /* index 24 */ /* BCH phase */
                 "LASR:IN20:1:LSR_0_POC",   /* index 25 */
                 "GUN:IN20:1:GN1_0_POC",    /* index 26 */
                 "GUN:IN20:1:GN1_1_POC",    /* index 27 */
                 "GUN:IN20:1:GN1_2_POC",    /* index 28 */
                 "GUN:IN20:1:GN1_3_POC",    /* index 29 */
                 "ACCL:IN20:300:L0A_0_POC", /* index 30 */
                 "ACCL:IN20:300:L0A_1_POC", /* index 31 */
                 "ACCL:IN20:400:L0B_2_POC", /* index 32 */
                 "ACCL:IN20:400:L0B_3_POC", /* index 33 */
                 "SIOC:SYS0:ML00:AO075",    /* index 34 */ /* L3 energy */
                 "SIOC:SYS0:ML00:AO079",    /* index 35 */ /* L3 Fudge input*/
                 "SIOC:SYS0:ML00:AO279",    /* index 36 */ /* L3 nofb flat totoal energy */
                 "SIOC:SYS0:ML00:AO280",    /* index 37 */ /* S29 flat total energy */
                 "SIOC:SYS0:ML00:AO281",    /* index 38 */ /* S30 flat total energy */
                 "ACCL:LI29:0:KLY_POC",     /* index 39 */
                 "ACCL:LI30:0:KLY_POC",     /* index 40 */
                 "ACCL:LI29:0:KLY_PDES",    /* index 41 */
                 "ACCL:LI30:0:KLY_PDES",    /* index 42 */
                 "SIOC:SYS0:ML00:AO017",    /* index 43 */ /* phase scan gui running */
                 "SIOC:SYS0:ML00:AO092",    /* index 44 */ /* L2 actuator strength */
                 "SIOC:SYS0:ML00:AO093",    /* index 45 */ /* L3 actuator strength */
                 "SIOC:SYS0:ML00:AO094",    /* index 46 */ /* S29 phase */
                 "SIOC:SYS0:ML00:AO095",    /* index 47 */ /* S30 phase */
                 "SIOC:SYS0:ML00:AO096",    /* index 48 */ /* 24-1 phase */
                 "SIOC:SYS0:ML00:AO097"     /* index 49 */ /* 24-2 phase */
              };
monitor pvs;

double s29PV;
assign s29PV   to "ACCL:LI29:0:KLY_PDES";
double s30PV;
assign s30PV   to "ACCL:LI30:0:KLY_PDES";
double klys1PV;
assign klys1PV to "ACCL:LI24:100:KLY_PDES";
double klys2PV;
assign klys2PV to "ACCL:LI24:300:KLY_PDES";  /* switch to 24-3 from 24-2 */

evflag   mon_flag;
/* 
   When, the state set GenMonEvent is activate,
   Please, comment out the following sync statement.
*/
 
sync pvs mon_flag; 


evflag drive_flag;
evflag done_flag;

double data[50];
double oldData[50];
char   chgFlag[50];

unsigned int missing_counter;
unsigned int missing_counter_old;
unsigned int missing_rate;
unsigned int event_counter;
unsigned int event_counter_old;
unsigned int event_rate;
unsigned int execution_counter;
unsigned int execution_counter_old;
unsigned int execution_rate;


assign   missing_rate    to     "{IOC}:MISSING_RATE";
assign   event_rate      to     "{IOC}:EVENT_RATE";
assign   execution_rate  to     "{IOC}:EXECUTION_RATE";


double pdes_ref;       assign pdes_ref    to  "{IOC}:PDES:REF";
double pdes_l2;        assign pdes_l2     to  "{IOC}:PDES:L2";
double phase24_1;      assign phase24_1   to  "{IOC}:PHASE:24_1";
double phase24_2;      assign phase24_2   to  "{IOC}:PHASE:24_2";
double phase_s29;      assign phase_s29   to  "{IOC}:PHASE:S29";
double phase_s30;      assign phase_s30   to  "{IOC}:PHASE:S30";
double poc_24_1;       assign poc_24_1    to  "{IOC}:POC:24_1";
double poc_24_2;       assign poc_24_2    to  "{IOC}:POC:24_2";
double poc_24_3;       assign poc_24_3    to  "{IOC}:POC:24_3";
double poc_tcav3_0;    assign poc_tcav3_0 to  "{IOC}:POC:TCAV3_0";
double poc_tcav3_1;    assign poc_tcav3_1 to  "{IOC}:POC:TCAV3_1";
double poc_s29;        assign poc_s29     to  "{IOC}:POC:S29";
double poc_s30;        assign poc_s30     to  "{IOC}:POC:S30";

unsigned int kly_selector;
assign       kly_selector   to "{IOC}:KLY_SELECTOR";
monitor      kly_selector;
evflag       kly_selector_flag;
sync         kly_selector      kly_selector_flag;


%{

void initData(void)
{
    int i;
    for(i=0; i<50; i++) {
        oldData[i] = 0.;
        data[i] = 0.;
        chgFlag[i] = 0;
    }
    missing_counter = 0;
    missing_counter_old = 0;
    missing_rate = 0;
    event_counter   = 0;
    event_counter_old = 0;
    event_rate =0;
    execution_counter = 0;
    execution_counter_old =0;
    execution_rate = 0;
}

void loadData(void)
{
    int i;
  
    for(i=0;i<50;i++) {
        oldData[i] = data[i];
        data[i]    = pvs[i];
        if(oldData[i] != data[i]) chgFlag[i] = TRUE;
        else                      chgFlag[i] = FALSE;
    }
}

void set_feedback_klystrons(double phase12,
                            double eGain,
                            double stationEnergy,
                            double *klys1Phase,
                            double *klys2Phase)
{
    double ex, ph;
    if(fabs(eGain) > 2.*stationEnergy) {
        if(eGain>0.) eGain = 2.*stationEnergy;
        else         eGain = -2.*stationEnergy;
    }

    ex = eGain/stationEnergy/2.;
    ph = 180./M_PI * acos(ex);

    *klys1Phase = phase12 + ph;
    *klys2Phase = phase12 - ph;
}

void set_feedback_stations(double phase13,
                           double eGain,
                           double stationEnergy1,
                           double stationEnergy2,
                           double *s29Phase,
                           double *s30Phase)
{
    double ex, ph;
    if(fabs(eGain) > (stationEnergy1+stationEnergy2)) {
        if(eGain>0.) eGain = stationEnergy1+stationEnergy2;
        else         eGain = -1.*(stationEnergy1+stationEnergy2);
    }

    ex = eGain/(stationEnergy1+stationEnergy2);
    ph = 180./M_PI * acos(ex);

    *s29Phase = phase13 - ph;
    *s30Phase = phase13 + ph;
}

}%


ss KlySelectControl
{
    state monSelector
    {
        when(kly_selector == 0) {} state selOneTwo    
        when(kly_selector == 1) {} state selTwoThree 
        when(kly_selector == 2) {} state selOneThree
    }

    state selOneTwo
    {
       entry { 
           pvAssign(pvs[11], "ACCL:LI24:100:KLY_POC"); pvMonitor(pvs[11]);
           pvAssign(pvs[12], "ACCL:LI24:200:KLY_POC"); pvMonitor(pvs[12]);
           pvAssign(pvs[13], "ACCL:LI24:300:KLY_POC"); pvMonitor(pvs[13]);

           pvAssign(klys1PV, "ACCL:LI24:100:KLY_PDES");
           pvAssign(klys2PV, "ACCL:LI24:200:KLY_PDES");
       
       }

        when(efTestAndClear(kly_selector_flag)) {} state monSelector
    }

    state selTwoThree
    {
       entry { 
           pvAssign(pvs[11], "ACCL:LI24:200:KLY_POC"); pvMonitor(pvs[11]); 
           pvAssign(pvs[12], "ACCL:LI24:300:KLY_POC"); pvMonitor(pvs[12]);
           pvAssign(pvs[13], "ACCL:LI24:100:KLY_POC"); pvMonitor(pvs[13]);

           pvAssign(klys1PV, "ACCL:LI24:200:KLY_PDES"); 
           pvAssign(klys2PV, "ACCL:LI24:300:KLY_PDES"); 

       }

        when(efTestAndClear(kly_selector_flag)) {} state monSelector
    }

    state selOneThree
    {
       entry { 
           pvAssign(pvs[11], "ACCL:LI24:100:KLY_POC"); pvMonitor(pvs[11]);
           pvAssign(pvs[12], "ACCL:LI24:300:KLY_POC"); pvMonitor(pvs[12]);
           pvAssign(pvs[13], "ACCL:LI24:200:KLY_POC"); pvMonitor(pvs[13]);

           pvAssign(klys1PV, "ACCL:LI24:100:KLY_PDES");
           pvAssign(klys2PV, "ACCL:LI24:300:KLY_PDES"); 

       }

        when(efTestAndClear(kly_selector_flag)) {} state monSelector

    }
}


ss MonPhaseControl
{
    state initMon
    {
        when(TRUE) {
            %% initData();
            %% loadData();
            %% loadData();
        } state monPhaseControl
    }
    state monPhaseControl
    {
        when(efTestAndClear(mon_flag)) {
            %% event_counter++;
            %% loadData();
            efSet(drive_flag);
        } state waitMon
    }

    state waitMon
    {
        when(efTestAndClear(done_flag)) {
            %% execution_counter ++;
        } state monPhaseControl

        when(efTestAndClear(mon_flag)) {
            %% event_counter ++;
            %% missing_counter ++;
        } state exceptionPhaseControl

    }

    state exceptionPhaseControl
    {
        when(TRUE) {
            /*
            epicsPrintf("SNL Monitoring Thread detects exception: counter %d\n", missing_counter);
            */
        } state waitMon
    }
}

ss EstEventRate
{
    state eventRate
    {
        when(delay(1.)) {
            %% event_rate = event_counter - event_counter_old;
            %% event_counter_old = event_counter;
            %% missing_rate = missing_counter - missing_counter_old;
            %% missing_counter_old = missing_counter;
            %% execution_rate = execution_counter - execution_counter_old;
            %% execution_counter_old = execution_counter;

            pvPut(event_rate);
            pvPut(missing_rate);
            pvPut(execution_rate);

            %% #ifdef DEBUG_STATUS
            %% epicsPrintf("------------------------------------\n");
            %% epicsPrintf("Feedback Event Rate: %ld\n", event_rate);
            %% epicsPrintf("Execution Rate:      %ld\n", execution_rate);
            %% epicsPrintf("Event Missing Rate:  %ld\n", missing_rate);
            %% epicsPrintf("------------------------------------\n\n");
            %% #endif

            %{ {
               unsigned watchdog = data[0];
               watchdog++;
               data[0] = (double) watchdog;
               pvs[0] = data[0];
              } }%
             pvPut(pvs[0]);

              

        } state eventRate
    }
}

/* State set GenMonEvent only for debuuging. 
   When this is activated, please remove the sync statement for the mon_flag. */
/*
ss GenMonEvent
{
    state genMonEvent
    {
        when(delay(.2)) {
            efSet(mon_flag);
        } state genMonEvent
    }
}
*/



ss phasControl
{
    state phaseControl
    {
        when(efTestAndClear(drive_flag)) {
              %% double result    = 0.;
              %% double mdl_scale = -1./6.;
              %% double l2_scale  = 1.;
              %% double tcav3_scale = 1.;
              %% double fb_station_scale = 1.;
              %% double S2930_station_scale = 1.;
              %% double injector_scale = 1.;
              %% double station_energy = 240.;
              %% double s21_2856_scale = -1;
              %% double requested_fb;
              %% double ph24_1_u;
              %% double ph24_2_u;
              %% double ph24_1;
              %% double ph24_2;

              %% double change;
              %% double new_phase;
              %% double new_l2_phase;
              %% double new_tcav3_phase_in;
              %% double new_tcav3_phase_out;
              %% double s21HH;
              %% double s21H;
              %% double s21L;
              %% double s21LL;

              %% double pchange;
              %% double ofchange;
              %% double fb_off[5];

              %% double S29_new;
              %% double S30_new;
              %% double requested_fb3;
              %% double S29_u;
              %% double S30_u;
              %% double S29;
              %% double S30;

              %% int i;
              %% double newPhase[10];

              if(chgFlag[1] || chgFlag[2] || chgFlag[22] || chgFlag[23] || chgFlag[43]) {
                 %% requested_fb = data[2] - data[22] * data[23];
                 %% if((int)data[1] && ~(int)data[43]) {
                 %%    set_feedback_klystrons(data[4], requested_fb, station_energy,
                 %%                           &ph24_1_u, &ph24_2_u);
                 %%      ph24_1 = ph24_1_u * fb_station_scale;
                 %%      ph24_2 = ph24_2_u * fb_station_scale;
                 %%
                 %% /*-----------------------------------------*/
                 %% #ifdef PERFORM_FEEDBACK
                         klys1PV = ph24_1;   pvPut(klys1PV);
                         klys2PV = ph24_2;   pvPut(klys2PV);
                         pvs[48] = ph24_1_u; pvPut(pvs[48]);
                         pvs[49] = ph24_2_u; pvPut(pvs[49]);
                 %% #endif
                 
                 %% #ifdef DEBUG_MONPV
                         phase24_1 = ph24_1_u;
                         phase24_2 = ph24_2_u;
                         pvPut(phase24_1);
                         pvPut(phase24_2);
                 %% #endif

                 %% #ifdef DEBUG
                         pvGet(klys1PV);
                         pvGet(klys2PV);
                         epicsPrintf("-------------------\n");
                         epicsPrintf("L2 Amplitude Change\n");
                         epicsPrintf("-------------------\n");
                         epicsPrintf("ph24_1(klys1PV): %lf  : %lf\n", ph24_1, klys1PV);
                         epicsPrintf("ph24_2(klys2PV): %lf  : %lf\n", ph24_2, klys2PV);
                         epicsPrintf("ph24_1_u:        %lf  : %lf\n", ph24_1_u, pvs[48]);
                         epicsPrintf("ph24_2_u:        %lf  : %lf\n", ph24_2_u, pvs[49]);
                         epicsPrintf("-------------------\n\n");
                 %% #endif
                 %% } 
              }


              if(chgFlag[3]) {
                 %% change              = -1. * (data[3] - oldData[3]);
                 %% new_phase           = data[9] - change * mdl_scale;
                 %% new_l2_phase        = data[10] + change *l2_scale;
                 %% new_tcav3_phase_in  = data[15] + change * tcav3_scale;
                 %% new_tcav3_phase_out = data[16] + change * tcav3_scale;
                 %% s21HH = data[17] + change * s21_2856_scale;
                 %% s21H  = data[18] + change * s21_2856_scale;
                 %% s21L  = data[19] + change * s21_2856_scale;
                 %% s21LL = data[20] + change * s21_2856_scale;
                 %%
                 %% /* ----------------------------------------------- */
                 %% #ifdef PERFORM_FEEDBACK
                 pvs[9]  = new_phase;             pvPut(pvs[9]);
                 pvs[10] = new_l2_phase;          pvPut(pvs[10]);
                 pvs[15] = new_tcav3_phase_in;    pvPut(pvs[15]);
                 pvs[16] = new_tcav3_phase_out;   pvPut(pvs[16]);
                 pvs[17] = s21HH;                 pvPut(pvs[17]);
                 pvs[18] = s21H;                  pvPut(pvs[18]);
                 pvs[19] = s21L;                  pvPut(pvs[19]);
                 pvs[20] = s21LL;                 pvPut(pvs[20]);
                 %% #endif
      
                 %% #ifdef DEBUG_MONPV
                 pdes_ref = new_phase;
                 pdes_l2       = new_l2_phase;
                 poc_tcav3_0 = new_tcav3_phase_in;
                 poc_tcav3_1 = new_tcav3_phase_out;
                 pvPut(pdes_ref);
                 pvPut(pdes_l2);
                 pvPut(poc_tcav3_0);
                 pvPut(poc_tcav3_1);
                 %% #endif


                 %% #ifdef DEBUG
                 epicsPrintf("---------------------------------------------------------------\n");
                 epicsPrintf("BC1 Phase Delay, move MDL and L2 phase shifter, fix tacv3 phase\n");
                 epicsPrintf("---------------------------------------------------------------\n");
                 epicsPrintf("new_phase:           %lf   : %lf\n", new_phase, pvs[9]);
                 epicsPrintf("new_l2_phase:        %lf   : %lf\n", new_l2_phase, pvs[10]);
                 epicsPrintf("new_tcav3_phase_in:  %lf   : %lf\n", new_tcav3_phase_in, pvs[15]);
                 epicsPrintf("new_tcav3_phase_out: %lf   : %lf\n", new_tcav3_phase_out, pvs[16]);
                 epicsPrintf("s21HH:               %lf   : %lf\n", s21HH, pvs[17]);
                 epicsPrintf("s21H:                %lf   : %lf\n", s21H, pvs[18]);
                 epicsPrintf("s21L:                %lf   : %lf\n", s21L, pvs[19]);
                 epicsPrintf("s21LL:               %lf   : %lf\n", s21LL, pvs[20]);
                 epicsPrintf("---------------------------------------------------------------\n\n");
                 %% #endif
              } 


              if(chgFlag[4] || chgFlag[5]) {
                 %% pchange = data[4] - oldData[4];
                 %% ofchange = data[5] - oldData[5];
                 %% change = pchange + ofchange;
                 %% new_phase = data[10] + change * l2_scale;
                 %% s21HH = data[17] + change * s21_2856_scale;
                 %% s21H  = data[18] + change * s21_2856_scale;
                 %% s21L  = data[19] + change * s21_2856_scale;
                 %% s21LL = data[20] + change * s21_2856_scale;
                 %% fb_off[1] = data[11] - change * fb_station_scale;
                 %% fb_off[2] = data[12] - change * fb_station_scale;
                 %% fb_off[3] = data[13] - change * fb_station_scale;

                 %% if((pchange != 0.) && (int)data[1] && ~(int)data[43]) {
                 %%     requested_fb = data[2] - data[22] * data[23];
                 %%     set_feedback_klystrons(data[4], requested_fb, station_energy,
                 %%                            &ph24_1_u, &ph24_2_u);
                 %%     ph24_1 = ph24_1_u * fb_station_scale;
                 %%     ph24_2 = ph24_2_u * fb_station_scale;
                 %%
                 %% /* ---------------------------------------------- */
                 %% #ifdef PERFORM_FEEDBACK
                        pvs[10] = new_phase;    pvPut(pvs[10]);
                        pvs[11] = fb_off[1];    pvPut(pvs[11]);
                        pvs[12] = fb_off[2];    pvPut(pvs[12]);
                        pvs[13] = fb_off[3];    pvPut(pvs[13]);
                        pvs[17] = s21HH;        pvPut(pvs[17]);
                        pvs[18] = s21H;         pvPut(pvs[18]);
                        pvs[19] = s21L;         pvPut(pvs[19]);
                        pvs[20] = s21LL;        pvPut(pvs[20]);
                        klys1PV = ph24_1;       pvPut(klys1PV);
                        klys2PV = ph24_2;       pvPut(klys2PV);
                        pvs[48] = ph24_1_u;     pvPut(pvs[48]);
                        pvs[49] = ph24_2_u;     pvPut(pvs[49]);
                 %% #endif

                 %% #ifdef DEBUG_MONPV
                        pdes_l2             = new_phase;
                        poc_24_1 = fb_off[1];
                        poc_24_2 = fb_off[2];
                        poc_24_3 = fb_off[3];
                        phase24_1            = ph24_1_u;
                        phase24_2            = ph24_2_u;
                        pvPut(pdes_l2);
                        pvPut(poc_24_1);
                        pvPut(poc_24_2);
                        pvPut(poc_24_3);
                        pvPut(phase24_1);
                        pvPut(phase24_2);
                 %% #endif

                 %% #ifdef DEBUG
                        pvGet(klys1PV);
                        pvGet(klys2PV);
                        epicsPrintf("-------------------------------\n");
                        epicsPrintf("L2 Phase or phase offset change\n");
                        epicsPrintf("-------------------------------\n");
                        epicsPrintf("new_phase: %lf   : %lf\n", new_phase, pvs[10]);
                        epicsPrintf("fb_off[1]: %lf   : %lf\n", fb_off[1], pvs[11]);
                        epicsPrintf("fb_off[2]: %lf   : %lf\n", fb_off[2], pvs[12]);
                        epicsPrintf("fb_off[3]: %lf   : %lf\n", fb_off[3], pvs[13]);
                        epicsPrintf("s21HH:     %lf   : %lf\n", s21HH,     pvs[17]);
                        epicsPrintf("s21H:      %lf   : %lf\n", s21H,      pvs[18]);
                        epicsPrintf("s21L:      %lf   : %lf\n", s21L,      pvs[19]);
                        epicsPrintf("s21LL:     %lf   : %lf\n", s21LL,     pvs[20]);
                        epicsPrintf("ph24_1:    %lf   : %lf\n", ph24_1,    klys1PV);
                        epicsPrintf("ph24_2:    %lf   : %lf\n", ph24_2,    klys2PV);
                        epicsPrintf("ph24_1_u:  %lf   : %lf\n", ph24_1_u,  pvs[48]);
                        epicsPrintf("ph24_2_u:  %lf   : %lf\n", ph24_2_u,  pvs[49]);
                        epicsPrintf("-------------------------------\n\n");
                 %% #endif
                 %% } else {
                 %% #ifdef PERFORM_FEEDBACK
                        pvs[10] = new_phase;    pvPut(pvs[10]);
                        pvs[11] = fb_off[1];    pvPut(pvs[11]);
                        pvs[12] = fb_off[2];    pvPut(pvs[12]);
                        pvs[13] = fb_off[3];    pvPut(pvs[13]);
                        pvs[17] = s21HH;        pvPut(pvs[17]);
                        pvs[18] = s21H;         pvPut(pvs[18]);
                        pvs[19] = s21L;         pvPut(pvs[19]);
                        pvs[20] = s21LL;        pvPut(pvs[20]);
                 %% #endif

                 %% #ifdef DEBUG_MONPV
                        pdes_l2             = new_phase;
                        poc_24_1 = fb_off[1];
                        poc_24_2 = fb_off[2];
                        poc_24_3 = fb_off[3];                       
                        pvPut(pdes_l2);
                        pvPut(poc_24_1);
                        pvPut(poc_24_2);
                        pvPut(poc_24_3);
                 %% #endif

                 %% #ifdef DEBUG
                        epicsPrintf("-------------------------------\n");
                        epicsPrintf("L2 Phase or phase offset change\n");
                        epicsPrintf("-------------------------------\n");
                        epicsPrintf("new_phase: %lf   : %lf\n", new_phase, pvs[10]);
                        epicsPrintf("fb_off[1]: %lf   : %lf\n", fb_off[1], pvs[11]);
                        epicsPrintf("fb_off[2]: %lf   : %lf\n", fb_off[2], pvs[12]);
                        epicsPrintf("fb_off[3]: %lf   : %lf\n", fb_off[3], pvs[13]);
                        epicsPrintf("s21HH:     %lf   : %lf\n", s21HH,     pvs[17]);
                        epicsPrintf("s21H:      %lf   : %lf\n", s21H,      pvs[18]);
                        epicsPrintf("s21L:      %lf   : %lf\n", s21L,      pvs[19]);     
                        epicsPrintf("s21LL:     %lf   : %lf\n", s21LL,     pvs[20]);
                        epicsPrintf("-------------------------------\n");
                 %% #endif
                 %% }
 
              }

              if(chgFlag[6]) {
                 %% change              = -1. * (data[6] - oldData[6]);
                 %% new_phase           = data[9] - change *mdl_scale;
                 %% new_tcav3_phase_in  = data[15] + change * tcav3_scale;
                 %% new_tcav3_phase_out = data[16] + change * tcav3_scale;
                 %%
                 %% /* --------------------------------------------------- */
                 %% #ifdef PERFORM_FEEDBACK
                    pvs[9]  = new_phase;             pvPut(pvs[9]);
                    pvs[15] = new_tcav3_phase_in;    pvPut(pvs[15]);
                    pvs[16] = new_tcav3_phase_out;   pvPut(pvs[16]);
                 %% #endif

                 %% #ifdef DEBUG_MONPV
                         pdes_l2      = new_phase;
                         poc_tcav3_0  = new_tcav3_phase_in;
                         poc_tcav3_1  = new_tcav3_phase_out;
                         pvPut(pdes_l2);
                         pvPut(poc_tcav3_0);
                         pvPut(poc_tcav3_1);
                 %% #endif

                 %% #ifdef DEBUG
                    epicsPrintf("----------------------\n");
                    epicsPrintf("Change BC2 phase delay\n");
                    epicsPrintf("----------------------\n");
                    epicsPrintf("new_phase:           %lf   : %lf\n", new_phase, pvs[9]);
                    epicsPrintf("new_tcav3_phase_in:  %lf   : %lf\n", new_tcav3_phase_in, pvs[15]);
                    epicsPrintf("new_tcav3_phase_out: %lf   : %lf\n", new_tcav3_phase_out, pvs[16]);
                    epicsPrintf("----------------------\n\n");
                 %% #endif
              }


              if(chgFlag[7] || chgFlag[8]) {
                 %% pchange = data[7] - oldData[7];
                 %% ofchange = data[8] - oldData[8];
                 %% change = pchange + ofchange;
                 %% new_phase = data[9] - change * mdl_scale;
                 %% S29_new = data[39] - change * S2930_station_scale;
                 %% S30_new = data[40] - change * S2930_station_scale;
                 %% if((pchange != 0.) && (int)data[1] && ~(int)data[43]) {
                 %%     requested_fb3 = data[34] - data[35] * data[36];
                 %%     set_feedback_stations(data[7], requested_fb3, data[37], data[38],
                 %%                           &S29_u, &S30_u);
                 %%     S29 = S29_u * S2930_station_scale;
                 %%     S30 = S30_u * S2930_station_scale;
                 %%
                 %%  /* ------------------------------------------- */

                 %% #ifdef PERFORM_FEEDBACK
                        pvs[9]  = new_phase;    pvPut(pvs[9]);
                        pvs[39] = S29_new;      pvPut(pvs[39]);
                        pvs[40] = S30_new;      pvPut(pvs[40]);
                        s29PV   = S29;          pvPut(s29PV);
                        s30PV   = S30;          pvPut(s30PV);
                        pvs[46] = S29_u;        pvPut(pvs[46]);
                        pvs[47] = S30_u;        pvPut(pvs[47]);
                 %% #endif

                 %% #ifdef DEBUG_MONPV
                        pdes_l2 = new_phase;
                        poc_s29 = S29_new;
                        poc_s30 = S30_new;
                        phase_s29 = S29_u;
                        phase_s30 = S30_u;
                        pvPut(pdes_l2);
                        pvPut(poc_s29);
                        pvPut(poc_s30);
                        pvPut(phase_s29);
                        pvPut(phase_s30);
                 %% #endif

                 %% #ifdef DEBUG
                        pvGet(s29PV);
                        pvGet(s30PV);
                        epicsPrintf("-------------------------\n");
                        epicsPrintf("L3 phase or offset change\n");
                        epicsPrintf("-------------------------\n");
                        epicsPrintf("new_phase: %lf   : %lf\n", new_phase, pvs[9]);
                        epicsPrintf("S29_new:   %lf   : %lf\n", S29_new, pvs[39]);
                        epicsPrintf("S30_new:   %lf   : %lf\n", S30_new, pvs[40]);
                        epicsPrintf("S29:       %lf   : %lf\n", S29, s29PV);
                        epicsPrintf("S30:       %lf   : %lf\n", S30, s30PV);
                        epicsPrintf("S29_u:     %lf   : %lf\n", S29_u, pvs[46]);
                        epicsPrintf("S30_u:     %lf   : %lf\n", S30_u, pvs[47]);
                        epicsPrintf("-------------------------\n\n");
                 %% #endif
                             
                 %% } else {
                 %% #ifdef PERFORM_FEEDBACK
                        pvs[9]  = new_phase;    pvPut(pvs[9]);
                        pvs[39] = S29_new;      pvPut(pvs[39]);
                        pvs[40] = S30_new;      pvPut(pvs[40]);
                 %% #endif

                 %% #ifdef DEBUG_MONPV
                        pdes_l2 = new_phase;
                        phase_s29 = S29_new;
                        phase_s30 = S30_new;
                        pvPut(pdes_l2);
                        pvPut(phase_s29);
                        pvPut(phase_s30);
                 %% #endif

                 %% #ifdef DEBUG
                        epicsPrintf("-------------------------\n");
                        epicsPrintf("L3 phase or offset change\n");
                        epicsPrintf("-------------------------\n");
                        epicsPrintf("new_phase: %lf   : %lf\n", new_phase, pvs[9]);
                        epicsPrintf("S29_new:   %lf   : %lf\n", S29_new, pvs[39]);
                        epicsPrintf("S30_new:   %lf   : %lf\n", S30_new, pvs[40]);
                        epicsPrintf("-------------------------\n\n");
                 %% #endif
                 %% }
              }

             
              if(chgFlag[24]) {
                 %% #ifdef DEUG
                    epicsPrintf("---------------------------------\n");
                    epicsPrintf("Laser Heater chicain phase change\n");
                    epicsPrintf("---------------------------------\n");
                 %% #endif

                 %% change = data[24] - oldData[24];
                 %% for(i=25; i<=33; i++) {
                 %%     newPhase[i-25] = data[i] + change * injector_scale;

                 %%     #ifdef PERFORM_FEEDBACK
                        pvs[i] = newPhase[i-25];  pvPut(pvs[i]); 
                 %%     #endif

                 %%     #ifdef DEBUG
                        epicsPrintf("newPhase[%d]: %lf   : %lf\n", i-25, newPhase[i-25], pvs[i]);
                 %%     #endif

                 %% }

                 %% #ifdef DEBUG
                    epicsPrintf("---------------------------------\n\n");
                 %% #endif 
              }


              if(chgFlag[1] || chgFlag[34] || chgFlag[35] || chgFlag[36] 
                 || chgFlag[37] || chgFlag[38] || chgFlag[43] ) {
                 %% requested_fb3 = data[34] - data[35] * data[36];
                 %% set_feedback_stations(data[7], requested_fb3, data[37], data[38],
                 %%                       &S29_u, &S30_u);
                 %% S29 = S29_u * S2930_station_scale;
                 %% S30 = S30_u * S2930_station_scale;
                 %%
                 %%  /* ------------------------------------ */
                
                 %% #ifdef PERFORM_FEEDBACK 
                    s29PV   = S29;      pvPut(s29PV);
                    s30PV   = S30;      pvPut(s30PV);
                    pvs[46] = S29_u;    pvPut(pvs[46]);
                    pvs[47] = S30_u;    pvPut(pvs[47]);
                 %% #endif


                 %% #ifdef DEBUG_MONPV
                    phase_s29 = S29_u;
                    phase_s30 = S30_u;
                    pvPut(phase_s29);
                    pvPut(phase_s30);
                 %% #endif

                 %% #ifdef DEBUG
                    pvGet(s29PV);
                    pvGet(s30PV);
                    epicsPrintf("-------------------\n");
                    epicsPrintf("Change in L3 energy\n");
                    epicsPrintf("-------------------\n");
                    epicsPrintf("S29  : %lf   : %lf\n", S29, s29PV);
                    epicsPrintf("S30  : %lf   : %lf\n", S30, s30PV);
                    epicsPrintf("S29_u: %lf   : %lf\n", S29_u, pvs[46]);
                    epicsPrintf("S30_u: %lf   : %lf\n", S30_u, pvs[47]);
                    epicsPrintf("-------------------\n\n");
                 %% #endif
                   
              }

        } state donePhaseControl
    }

    state donePhaseControl
    {
        when(TRUE) {
            efSet(done_flag);
        } state phaseControl
    }
}
