/*
 * file:                extractMotors.stt
 * purpose:             LCLS State Machine for Dechirper motion control coordination
 * created:             22-Jul-2015
 * property of:         SLAC & Cosylab USA
 *
 * revision history:
 *   22-Jul-2014        Ziga Oven             initial version
 *
 */

program extractMotors
    option +d; /* debug */
    option +c; /* connect to PVs option */
    option +r; /* reentrant */

/* PV variables */

  /* various status and debugging PVs */
  int EXTRACT;        assign EXTRACT         to "{P}:EXTRACT";            monitor EXTRACT;
  int EXTR_DEBUG;     assign EXTR_DEBUG      to "{P}:EXTR_DEBUG";         monitor EXTR_DEBUG;
  int EXTR_BREAK;     assign EXTR_BREAK      to "{P}:EXTR_BREAK";         monitor EXTR_BREAK;
  int EXTR_GAP_UP;    assign EXTR_GAP_UP     to "{P}:EXTR_GAP_UP.PROC";   monitor EXTR_GAP_UP;
  int SNL_ACTION;     assign SNL_ACTION      to "{P}:SNL_ACTION";         monitor SNL_ACTION;

  /* motor PVs */
  int j1_mc_DMOV;     assign j1_mc_DMOV      to "{P}:{J1}:CSP.DMOV";      monitor j1_mc_DMOV;
  int j2_mc_DMOV;     assign j2_mc_DMOV      to "{P}:{J2}:CSP.DMOV";      monitor j2_mc_DMOV;
  int j1_mt_DMOV;     assign j1_mt_DMOV      to "{P}:{J1}:TRM.DMOV";      monitor j1_mt_DMOV;
  int j2_mt_DMOV;     assign j2_mt_DMOV      to "{P}:{J2}:TRM.DMOV";      monitor j2_mt_DMOV;
  int j1_mc_STOP;     assign j1_mc_STOP      to "{P}:{J1}:CSP.STOP";      monitor j1_mc_STOP;
  int j2_mc_STOP;     assign j2_mc_STOP      to "{P}:{J2}:CSP.STOP";      monitor j2_mc_STOP;
  int j1_mt_STOP;     assign j1_mt_STOP      to "{P}:{J1}:TRM.STOP";      monitor j1_mt_STOP;
  int j2_mt_STOP;     assign j2_mt_STOP      to "{P}:{J2}:TRM.STOP";      monitor j2_mt_STOP;
  int j1_mc_CNEN;     assign j1_mc_CNEN      to "{P}:{J1}:CSP.CNEN";      monitor j1_mc_CNEN;
  int j2_mc_CNEN;     assign j2_mc_CNEN      to "{P}:{J2}:CSP.CNEN";      monitor j2_mc_CNEN;
  int j1_mt_CNEN;     assign j1_mt_CNEN      to "{P}:{J1}:TRM.CNEN";      monitor j1_mt_CNEN;
  int j2_mt_CNEN;     assign j2_mt_CNEN      to "{P}:{J2}:TRM.CNEN";      monitor j2_mt_CNEN;
  double j1_mc;       assign j1_mc           to "{P}:{J1}:CSP.VAL";       monitor j1_mc;
  double j2_mc;       assign j2_mc           to "{P}:{J2}:CSP.VAL";       monitor j2_mc;
  double j1_mt;       assign j1_mt           to "{P}:{J1}:TRM.VAL";       monitor j1_mt;
  double j2_mt;       assign j2_mt           to "{P}:{J2}:TRM.VAL";       monitor j2_mt;
  /* out positions stored in database */
  double j1_mc_out;   assign j1_mc_out       to "{P}:{J1}:CSP:OUT_POS";   monitor j1_mc_out;
  double j2_mc_out;   assign j2_mc_out       to "{P}:{J2}:CSP:OUT_POS";   monitor j2_mc_out;
  double j1_mt_out;   assign j1_mt_out       to "{P}:{J1}:TRM:OUT_POS";   monitor j1_mt_out;
  double j2_mt_out;   assign j2_mt_out       to "{P}:{J2}:TRM:OUT_POS";   monitor j2_mt_out;


/*******************************************************************************************/

ss extractMotors
{
/* ################################################################################################################# */
    state s_extractInit {
        entry {
            if(EXTR_DEBUG)
                printf( "=-=-=> Entering s_extractInit state\n");
        }
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
        when() {
        }state s_idle
    }
/* ################################################################################################################# */
    state s_idle {
        entry {
            if(EXTR_DEBUG)
                printf( "=-=-=> Entering s_idle state\n");
        }

        /* wait until extract PV is triggered and motion sequence is not active */
        when(EXTRACT && !(SNL_ACTION)) {
        }state s_extractMotors

        /* if motion sequence is active and extract PV is triggered, clear extract PV and do nothing */
        when(EXTRACT && SNL_ACTION) {
            if(EXTR_DEBUG)
                printf( "\t\t Sequence motion is active!\n\t\tExtract cancelled, stop motion first and then issue extract!\n");
            EXTRACT = 0;
            pvPut(EXTRACT,SYNC);
        }state s_idle


        when(EXTR_BREAK){
        }state s_stopExtract
    }
/* ################################################################################################################# */
    state s_extractMotors {
        entry {
            if(EXTR_DEBUG)
                printf( "=-=-=> Entering s_extractMotors state\n");
        }

        when(){
            if(EXTR_DEBUG)
                printf( "\tEnabling motors and starting extraction\n");
            j1_mc_CNEN = 1;
            pvPut(j1_mc_CNEN,SYNC);
            j2_mc_CNEN = 1;
            pvPut(j2_mc_CNEN,SYNC);
            j1_mt_CNEN = 1;
            pvPut(j1_mt_CNEN,SYNC);
            j2_mt_CNEN = 1;
            pvPut(j2_mt_CNEN,SYNC);
			j1_mc = j1_mc_out;
            pvPut(j1_mc);
			j2_mc = j2_mc_out;
            pvPut(j2_mc);
			j1_mt = j1_mt_out;
            pvPut(j1_mt);
			j2_mt = j2_mt_out;
            pvPut(j2_mt);
        }state s_motorsExtracting

        when(EXTR_BREAK){
        }state s_stopExtract

     /* Debug exit on timeout */       
		when(delay(10)){
            if(EXTR_DEBUG)
                printf("\tTimeout\n");
        }state s_idle
    }
/* ################################################################################################################# */
    state s_motorsExtracting {
        entry {
            if(EXTR_DEBUG)
                printf( "=-=-=> Entering s_motorsExtracting state\n");
        }

        when(delay(0.5) && j1_mc_DMOV && j1_mt_DMOV && j2_mc_DMOV && j2_mt_DMOV){
            if(EXTR_DEBUG)
                printf( "\tMotors done moving\n");
        }state s_stopExtract

        when(EXTR_BREAK){
        }state s_stopExtract

    }
/* ################################################################################################################# */
    state s_stopExtract {
        entry {
            if(EXTR_DEBUG)
                printf( "=-=-=> Entering s_stopExtract state\n");
        }
        when(){
            if(EXTR_DEBUG)
                printf( "\tEnd of extract or interrupt from user, stopping all motion, clearing EXTRACT flag and returning to idle state \n");
            j1_mc_STOP = 1;
            pvPut(j1_mc_STOP,SYNC);
            j2_mc_STOP = 1;
            pvPut(j2_mc_STOP,SYNC);
            j1_mt_STOP = 1;
            pvPut(j1_mt_STOP,SYNC);
            j2_mt_STOP = 1;
            pvPut(j2_mt_STOP,SYNC);
            EXTR_BREAK = 0;
            pvPut(EXTR_BREAK,SYNC);
            j1_mc_CNEN = 0;
            pvPut(j1_mc_CNEN);
            j2_mc_CNEN = 0;
            pvPut(j2_mc_CNEN);
            j1_mt_CNEN = 0;
            pvPut(j1_mt_CNEN);
            j2_mt_CNEN = 0;
            pvPut(j2_mt_CNEN);
            EXTR_GAP_UP = 1;
            pvPut(EXTR_GAP_UP,SYNC);
            EXTRACT = 0;
            pvPut(EXTRACT,SYNC);
        }state s_idle     

     /* Debug exit on timeout */       
		when(delay(10)){
            if(EXTR_DEBUG)
                printf("\tTimeout\n");
            EXTRACT = 0;
            pvPut(EXTRACT,SYNC);
        }state s_idle
    }
}

/*
vim:filetype=cpp:
*/
