/*
 * file:                moveMotors.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 moveMotors
    option +d; /* debug */
    option +c; /* connect to PVs option */
    option +r; /* reentrant */

%%#include <time.h>

%%#define J1_MC_POS 0x01    /* 1    */
%%#define J1_MC_NEG 0x02    /* 2    */
%%#define J1_MT_POS 0x04    /* 4    */
%%#define J1_MT_NEG 0x08    /* 8    */
%%#define J2_MC_POS 0x10    /* 16   */
%%#define J2_MC_NEG 0x20    /* 32   */
%%#define J2_MT_POS 0x40    /* 64   */
%%#define J2_MT_NEG 0x80    /* 128  */
%%#define POS_LS 0x0004     /* 4    */
%%#define NEG_LS 0x2000     /* 8192 */


/* Internal variables */
int sequence; 
int move_last_motor;
int move_another_motor;
int move_other_jaw;
int norm_factor=500;
int counter;
int moveOffLS; /* 1=J1_CSP, 2=J2_CSP, 3=J1_TRM, 4=J2_TRM */
/* normalized current motor position (4 decimal places) */
double j1_mc_c_norm;
double j2_mc_c_norm;
double j1_mt_c_norm;
double j2_mt_c_norm;
/* normalized current motor position readback (4 decimal places) */
double j1_mc_r_norm;
double j2_mc_r_norm;
double j1_mt_r_norm;
double j2_mt_r_norm;
/* normalized new motor position (4 decimal places) */
double j1_mc_d_norm;
double j2_mc_d_norm;
double j1_mt_d_norm;
double j2_mt_d_norm;
/* normalized gap values */
double us_gap_c;
double ds_gap_c;
double us_gap_n;
double ds_gap_n;


/* PV variables */

/* various status and debugging PVs */
int SNL_ACTION;     assign SNL_ACTION      to "{P}:SNL_ACTION";         monitor SNL_ACTION;
int SNL_DEBUG;      assign SNL_DEBUG       to "{P}:SNL_DEBUG";          monitor SNL_DEBUG;
int SNL_BREAK;      assign SNL_BREAK       to "{P}:SNL_BREAK";          monitor SNL_BREAK;
int N_GAP_STAT;     assign N_GAP_STAT      to "{P}:F_GAP_STAT";         monitor N_GAP_STAT;
int GAP_UP_P;       assign GAP_UP_P        to "{P}:F_GAP_UPDATE.PROC";  monitor GAP_UP_P;
int MSS_SWITCH;     assign MSS_SWITCH      to "{P}:MSS_SWITCH";         monitor MSS_SWITCH;
int EXTRACT;        assign EXTRACT         to "{P}:EXTRACT";            monitor EXTRACT;

/* current and new value for gap */
double US_GAP;      assign US_GAP          to "{P}:US_GW_RBK";          monitor US_GAP;
double DS_GAP;      assign DS_GAP          to "{P}:DS_GW_RBK";          monitor DS_GAP;
double N_US_GAP;    assign N_US_GAP        to "{P}:F_US_G";             monitor N_US_GAP;
double N_DS_GAP;    assign N_DS_GAP        to "{P}:F_DS_G";             monitor N_DS_GAP;

/* 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;
int j1_mc_MSTA;     assign j1_mc_MSTA      to "{P}:{J1}:CSP.MSTA";      monitor j1_mc_MSTA;
int j2_mc_MSTA;     assign j2_mc_MSTA      to "{P}:{J2}:CSP.MSTA";      monitor j2_mc_MSTA;
int j1_mt_MSTA;     assign j1_mt_MSTA      to "{P}:{J1}:TRM.MSTA";      monitor j1_mt_MSTA;
int j2_mt_MSTA;     assign j2_mt_MSTA      to "{P}:{J2}:TRM.MSTA";      monitor j2_mt_MSTA;
int j1_mc_DIR;      assign j1_mc_DIR       to "{P}:{J1}:CSP.DIR";       monitor j1_mc_DIR;
int j2_mc_DIR;      assign j2_mc_DIR       to "{P}:{J2}:CSP.DIR";       monitor j2_mc_DIR;
int j1_mt_DIR;      assign j1_mt_DIR       to "{P}:{J1}:TRM.DIR";       monitor j1_mt_DIR;
int j2_mt_DIR;      assign j2_mt_DIR       to "{P}:{J2}:TRM.DIR";       monitor j2_mt_DIR;
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;
double j1_mc_RBV;   assign j1_mc_RBV       to "{P}:{J1}:CSP.RBV";       monitor j1_mc_RBV;
double j2_mc_RBV;   assign j2_mc_RBV       to "{P}:{J2}:CSP.RBV";       monitor j2_mc_RBV;
double j1_mt_RBV;   assign j1_mt_RBV       to "{P}:{J1}:TRM.RBV";       monitor j1_mt_RBV;
double j2_mt_RBV;   assign j2_mt_RBV       to "{P}:{J2}:TRM.RBV";       monitor j2_mt_RBV;
double j1_mc_RLV;   assign j1_mc_RLV       to "{P}:{J1}:CSP.RLV";       monitor j1_mc_RLV;
double j2_mc_RLV;   assign j2_mc_RLV       to "{P}:{J2}:CSP.RLV";       monitor j2_mc_RLV;
double j1_mt_RLV;   assign j1_mt_RLV       to "{P}:{J1}:TRM.RLV";       monitor j1_mt_RLV;
double j2_mt_RLV;   assign j2_mt_RLV       to "{P}:{J2}:TRM.RLV";       monitor j2_mt_RLV;


/* new motor position PVs */
double j1_mc_dest;  assign j1_mc_dest      to "{P}:{J1}:MTR_CSP_DEST";  monitor j1_mc_dest;
double j2_mc_dest;  assign j2_mc_dest      to "{P}:{J2}:MTR_CSP_DEST";  monitor j2_mc_dest;
double j1_mt_dest;  assign j1_mt_dest      to "{P}:{J1}:MTR_TRM_DEST";  monitor j1_mt_dest;
double j2_mt_dest;  assign j2_mt_dest      to "{P}:{J2}:MTR_TRM_DEST";  monitor j2_mt_dest;


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

ss move
{
/* ################################################################################################################# */
    state s_Init {
        entry {
            if(SNL_DEBUG)
                printf( "-> Entering s_Init state\n");
        }
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
        when() {
        }state s_wait
    }
/* ################################################################################################################# */
    state s_wait {
        entry {
            if(SNL_DEBUG)
                printf( "\n-> Entering s_wait state\n");
        }
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
        /* if  EXTRACT is active don't move motors*/
        when(SNL_ACTION && EXTRACT) {
            if(SNL_DEBUG)
                printf( "\t\t Can't commence move while extracting, clearing the ACTIVE flag and returning to wait state\n");
            SNL_ACTION = 0;
            pvPut(SNL_ACTION,SYNC);
        }state s_wait
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
        /* wait until action PV is triggered and all values are in range and EXTRACT is not active */
        when(SNL_ACTION && N_GAP_STAT && !(EXTRACT)) {
        }state s_checkChange
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
        when(SNL_ACTION && !(N_GAP_STAT) ) {
        }state s_stopEverything   
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
        when(SNL_BREAK){
        }state s_stopEverything   
    }
/* ################################################################################################################# */
    state s_checkChange {
        entry {
            if(SNL_DEBUG)
                printf( "\n-> Entering s_checkDest state\n");

             sequence = 0;
             move_last_motor = 0;
             move_another_motor = 0;
             move_other_jaw = 0;
             moveOffLS = 0;
             counter = 0;
        }
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */     
        when() {
            /* normalizing motor positions */
            j1_mc_r_norm = round(j1_mc_RBV*norm_factor)/norm_factor;
            j1_mc_d_norm = round(j1_mc_dest*norm_factor)/norm_factor;
            j2_mc_r_norm = round(j2_mc_RBV*norm_factor)/norm_factor;
            j2_mc_d_norm = round(j2_mc_dest*norm_factor)/norm_factor;
            j1_mt_r_norm = round(j1_mt_RBV*norm_factor)/norm_factor;
            j1_mt_d_norm = round(j1_mt_dest*norm_factor)/norm_factor;
            j2_mt_r_norm = round(j2_mt_RBV*norm_factor)/norm_factor;
            j2_mt_d_norm = round(j2_mt_dest*norm_factor)/norm_factor;
            /* normalizing gaps */
            us_gap_c = round(US_GAP*norm_factor)/norm_factor;
            ds_gap_c = round(DS_GAP*norm_factor)/norm_factor;
            us_gap_n = round(N_US_GAP*norm_factor)/norm_factor;
            ds_gap_n = round(N_DS_GAP*norm_factor)/norm_factor;

            if(j1_mc_r_norm < j1_mc_d_norm)  /* J1 CSP move to POS */
                sequence = sequence | J1_MC_POS; /* 0x01 */
            if(j1_mc_r_norm > j1_mc_d_norm)  /* J1 CSP move to NEG */
                sequence = sequence | J1_MC_NEG; /* 0x02 */
            if(j1_mt_r_norm < j1_mt_d_norm)  /* J1 TRM move to POS */
                sequence = sequence | J1_MT_POS; /* 0x04 */
            if(j1_mt_r_norm > j1_mt_d_norm)  /* J1 TRM move to NEG */
                sequence = sequence | J1_MT_NEG; /* 0x08 */
            if(j2_mc_r_norm < j2_mc_d_norm)  /* J2 CSP move to POS */
                sequence = sequence | J2_MC_POS; /* 0x10 */
            if(j2_mc_r_norm > j2_mc_d_norm)  /* J2 CSP move to NEG */
                sequence = sequence | J2_MC_NEG; /* 0x20 */
            if(j2_mt_r_norm < j2_mt_d_norm)  /* J2 TRM move to POS */
                sequence = sequence | J2_MT_POS; /* 0x40 */
            if(j2_mt_r_norm > j2_mt_d_norm)  /* J2 TRM move to NEG */
                sequence = sequence | J2_MT_NEG; /* 0x80 */

            if(SNL_DEBUG)
                printf("\tsequence: %d\n",sequence);
            /* update PVs for END GAP COORDINATE system  */
            GAP_UP_P = 1;
            pvPut(GAP_UP_P,SYNC);
        }state s_selectMove
    }
/* ################################################################################################################# */
    state s_selectMove {
        entry {
            if(SNL_DEBUG)
                printf( "-> Entering s_selectMove state\n");
        }
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
    /* No move */
        when( sequence == 0x00 ){
        }state s_stopEverything
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
    /* Only J1 CSP move */
        when( sequence & (J1_MC_POS|J1_MC_NEG) && !(sequence & (J1_MT_POS|J1_MT_NEG|J2_MC_POS|J2_MC_NEG|J2_MT_POS|J2_MT_NEG)) ){
            if(SNL_DEBUG)
                printf("\tChange only J1 CSP (doesn't matter which direction)\n");
             move_last_motor = 1;
        }state s_move_J1CSP
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
    /* J1 CSP & TRM move */
        when( sequence & (J1_MC_POS|J1_MC_NEG|J1_MT_POS|J1_MT_NEG) && !(sequence & (J2_MC_POS|J2_MC_NEG|J2_MT_POS|J2_MT_NEG)) ){
            if(SNL_DEBUG)
                printf("\tChange J1 CSP & TRM\n");
        }state s_move_J1
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
    /* Only J2 CSP move */
        when( sequence & (J2_MC_POS|J2_MC_NEG) && !(sequence & (J1_MC_POS|J1_MC_NEG|J1_MT_POS|J1_MT_NEG|J2_MT_POS|J2_MT_NEG)) ){
            if(SNL_DEBUG)
                printf("\tChange only J2 CSP (doesn't matter which direction)\n");
            move_last_motor = 1;
        }state s_move_J2CSP
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
    /* J2 CSP & TRM move */
        when( sequence & (J2_MC_POS|J2_MC_NEG|J2_MT_POS|J2_MT_NEG) && !(sequence & (J1_MC_POS|J1_MC_NEG|J1_MT_POS|J1_MT_NEG)) ){
            if(SNL_DEBUG)
                printf("\tChange J2 CSP & TRM\n");
        }state s_move_J2
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
    /* J1 & J2 CSP move */
        when( sequence & (J1_MC_POS|J1_MC_NEG|J2_MC_POS|J2_MC_NEG) && !(sequence & (J1_MT_POS|J1_MT_NEG|J2_MT_POS|J2_MT_NEG)) ){
            if(SNL_DEBUG)
                printf("\tChange J1 & J2 CSP\n");
        }state s_move_J1J2CSP
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
    /* J1 & J2 CSP & TRM move */
        when( sequence & (J1_MC_POS|J1_MC_NEG|J1_MT_POS|J1_MT_NEG |J2_MC_POS|J2_MC_NEG|J2_MT_POS|J2_MT_NEG) ){
            if(SNL_DEBUG)
                printf("\tChange J1 & J2 CSP & TRM\n");
        }state s_move_J1J2
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
		when(delay(10)){
            if(SNL_DEBUG)
                printf("\tTimeout\n");
        }state s_stopEverything
    }
/* ################################################################################################################# */
    state s_move_J1CSP {
        entry {
            if(SNL_DEBUG)
                printf( "-> Entering s_move_J1CSP state\n");
        }
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
        when( j1_mc_MSTA & (POS_LS|NEG_LS) ){ /* sitting on limit switch */
            if(SNL_DEBUG)
                printf( "\t moving off limit switch\n");
            moveOffLS = 1;
            j1_mc_CNEN = 1;
            pvPut(j1_mc_CNEN,SYNC);
            if(j1_mc_MSTA & POS_LS){ /* sitting on POS limit switch (according to MSTA) */
                if(j1_mc_DIR == 0) /* motorRecord Positive direction */
                    j1_mc_RLV = -0.1;
                if(j1_mc_DIR == 1) /* motorRecord Negative direction */
                    j1_mc_RLV = 0.1;
            }
            if(j1_mc_MSTA & NEG_LS){ /* sitting on NEG limit switch (according to MSTA) */
                if(j1_mc_DIR == 0) /* motorRecord Positive direction */
                    j1_mc_RLV = 0.1;
                if(j1_mc_DIR == 1) /* motorRecord Negative direction */
                    j1_mc_RLV = -0.1;
            }
            pvPut(j1_mc_RLV);
        }state s_move_offLimitSwitchStop
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
        when(){
            j1_mc_STOP = 1;
            pvPut(j1_mc_STOP,SYNC);
            j1_mc_CNEN = 1;
            pvPut(j1_mc_CNEN,SYNC);
            j1_mc = j1_mc_d_norm;
            pvPut(j1_mc);
        }state s_motorsMoving
    }
/* ################################################################################################################# */
    state s_move_J2CSP {
        entry {
            if(SNL_DEBUG)
                printf( "-> Entering s_move_J2CSP state\n");
        }
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
        when( j2_mc_MSTA & (POS_LS|NEG_LS) ){ /* sitting on limit switch */
            if(SNL_DEBUG)
                printf( "\t moving off limit switch\n");
            moveOffLS = 2;
            j2_mc_CNEN = 1;
            pvPut(j2_mc_CNEN,SYNC);
            if (j2_mc_MSTA & POS_LS){ /* sitting on POS limit switch (according to MSTA) */
                if(j2_mc_DIR == 0) /* motorRecord Positive direction */
                    j2_mc_RLV = -0.1;
                if(j2_mc_DIR == 1) /* motorRecord Negative direction */
                    j2_mc_RLV = 0.1;
            }
            if (j2_mc_MSTA & NEG_LS){ /* sitting on NEG limit switch (according to MSTA) */
                if(j2_mc_DIR == 0) /* motorRecord Positive direction */
                    j2_mc_RLV = 0.1;
                if(j2_mc_DIR == 1) /* motorRecord Negative direction */
                    j2_mc_RLV = -0.1;
            }
            pvPut(j2_mc_RLV);
        }state s_move_offLimitSwitchStop
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
        when(){
            j2_mc_STOP = 1;
            pvPut(j2_mc_STOP,SYNC);
            j2_mc_CNEN = 1;
            pvPut(j2_mc_CNEN,SYNC);
            j2_mc = j2_mc_d_norm;
            pvPut(j2_mc);
        }state s_motorsMoving
    }
/* ################################################################################################################# */
    state s_move_J1TRM {
        entry {
            if(SNL_DEBUG)
                printf( "-> Entering s_move_J1TRM state\n");
        }
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
        when( j1_mt_MSTA & (POS_LS|NEG_LS) ){ /* sitting on limit switch */
            if(SNL_DEBUG)
                printf( "\t moving off limit switch\n");
            moveOffLS = 3;
            j1_mt_CNEN = 1;
            pvPut(j1_mt_CNEN,SYNC);
            if (j1_mt_MSTA & POS_LS){ /* sitting on POS limit switch (according to MSTA) */
                if(j1_mt_DIR == 0) /* motorRecord Positive direction */
                    j1_mt_RLV = -0.1;
                if(j1_mt_DIR == 1) /* motorRecord Negative direction */
                    j1_mt_RLV = 0.1;
            }
            if (j1_mt_MSTA & NEG_LS){ /* sitting on NEG limit switch (according to MSTA) */
                if(j1_mt_DIR == 0) /* motorRecord Positive direction */
                    j1_mt_RLV = 0.1;
                if(j1_mt_DIR == 1) /* motorRecord Negative direction */
                    j1_mt_RLV = -0.1;
            }
            pvPut(j1_mt_RLV);
        }state s_move_offLimitSwitchStop
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
        when(){
            j1_mt_STOP = 1;
            pvPut(j1_mt_STOP,SYNC);
            j1_mt_CNEN = 1;
            pvPut(j1_mt_CNEN,SYNC);
            j1_mt = j1_mt_d_norm;
            pvPut(j1_mt);
        }state s_motorsMoving
    }
/* ################################################################################################################# */
    state s_move_J2TRM {
        entry {
            if(SNL_DEBUG)
                printf( "-> Entering s_move_J2TRM state\n");
        }
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
        when( j2_mt_MSTA & (POS_LS|NEG_LS) ){ /* sitting on limit switch */
            if(SNL_DEBUG)
                printf( "\t moving off limit switch\n");
            moveOffLS = 4;
            j2_mt_CNEN = 1;
            pvPut(j2_mt_CNEN,SYNC);
            if (j2_mt_MSTA & POS_LS){ /* sitting on POS limit switch (according to MSTA) */
                if(j2_mt_DIR == 0) /* motorRecord Positive direction */
                    j2_mt_RLV = -0.1;
                if(j2_mt_DIR == 1) /* motorRecord Negative direction */
                    j2_mt_RLV = 0.1;
            }
            if (j2_mt_MSTA & NEG_LS){ /* sitting on NEG limit switch (according to MSTA) */
                if(j2_mt_DIR == 0) /* motorRecord Positive direction */
                    j2_mt_RLV = 0.1;
                if(j2_mt_DIR == 1) /* motorRecord Negative direction */
                    j2_mt_RLV = -0.1;
            }
            pvPut(j2_mt_RLV);
        }state s_move_offLimitSwitchStop
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
        when(){
            j2_mt_STOP = 1;
            pvPut(j2_mt_STOP,SYNC);
            j2_mt_CNEN = 1;
            pvPut(j2_mt_CNEN,SYNC);
            j2_mt = j2_mt_d_norm;
            pvPut(j2_mt);
        }state s_motorsMoving
    }
/* ################################################################################################################# */
    state s_move_offLimitSwitchStop {
        entry {
            if(SNL_DEBUG)
                printf( "-> Entering s_move_offLimitSwitchStop state\n");
        }
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
        when(SNL_BREAK){
        }state s_stopEverything
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
        when(delay(0.3) && (moveOffLS == 1) && !(SNL_BREAK)){
            j1_mc_STOP = 1;
            pvPut(j1_mc_STOP,SYNC);
        }state s_move_J1CSP
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
        when(delay(0.3) && (moveOffLS == 2) && !(SNL_BREAK)){
            j2_mc_STOP = 1;
            pvPut(j2_mc_STOP,SYNC);
        }state s_move_J2CSP
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
        when(delay(0.3) && (moveOffLS == 3) && !(SNL_BREAK)){
            j1_mt_STOP = 1;
            pvPut(j1_mt_STOP,SYNC);
        }state s_move_J1TRM
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
        when(delay(0.3) && (moveOffLS == 4) && !(SNL_BREAK)){
            j2_mt_STOP = 1;
            pvPut(j2_mt_STOP,SYNC);
        }state s_move_J2TRM
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
		when(delay(10)){
            if(SNL_DEBUG)
                printf("\tTimeout\n");
        }state s_stopEverything
    }
/* ################################################################################################################# */
    state s_motorsMoving {
        entry {
            if(SNL_DEBUG)
                printf( "-> Entering s_motorsMoving state\n");
        }
        when(delay(0.5) && j1_mc_DMOV && j1_mt_DMOV && j2_mc_DMOV && j2_mt_DMOV){
            if(SNL_DEBUG){
                printf( "\tMotors done moving\n");
                printf("othrJaw: %d\t othrMotr: %d\n",move_other_jaw,move_another_motor);
            }
            counter = counter + 1; 
        }state s_whereToGo

        when(SNL_BREAK || (counter > 10) ){
        }state s_stopEverything         

    }
/* ################################################################################################################# */
    state s_stopEverything {
        entry {
            if(SNL_DEBUG)
                printf( "-> Entering s_stopEverything state\n");
        }
        when(){
            if(SNL_DEBUG)
                printf( "\tEnd of move or interrupt from user, stopping all motion, clearing ACTIVE flag and returning to wait 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);
            SNL_BREAK = 0;
            pvPut(SNL_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);
            /* update PVs for END GAP COORDINATE system  */
            SNL_ACTION = 0;
            pvPut(SNL_ACTION,SYNC);
        }state s_wait     
    }
/* ################################################################################################################# */
    state s_whereToGo {
        entry {
            if(SNL_DEBUG){
                printf( "-> Entering s_whereToGo state\n");
                printf("othrJaw: %d\t othrMotr: %d\n",move_other_jaw,move_another_motor);
            }
        }
        when( move_other_jaw || move_another_motor ){
            if(SNL_DEBUG)
                printf( "\tAnother move, check what to move next\n");
        }state s_selectMove

        when(move_last_motor){
            if(SNL_DEBUG)
                printf( "\tDone with motion, stopEveryhing and then go to state\n");
        }state s_stopEverything

        when(SNL_BREAK){
        }state s_stopEverything         
    }
/* ################################################################################################################# */
    state s_move_J1J2CSP {
        entry {
            if(SNL_DEBUG)
                printf( "-> Entering s_move_J1J2CSP state\n");
        }
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
    /* Opening gap */
        when( (sequence & J1_MC_POS) && (sequence & J2_MC_NEG) ){
            if(SNL_DEBUG)
                printf( "\tOpening both jaws at same time \n");
            /* order isn't important if moving in different directions, was just coded this way */
            j2_mc_CNEN = 1;
            pvPut(j2_mc_CNEN,SYNC);
            j2_mc = j2_mc_d_norm;
            pvPut(j2_mc);
            move_last_motor = 1;
        }state s_move_J1CSP
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
    /* Moving gap center in POS direction: fixed width of gap == scanning */
/*
        when( (sequence & J1_MC_POS) && (sequence & J2_MC_POS) && ((us_gap_c == us_gap_n) && (ds_gap_c == ds_gap_n)) ){
            if(SNL_DEBUG)
                printf( "\tMoving center POS: triggering J1 and starting J2 \n");
            j1_mc_CNEN = 1;
            pvPut(j1_mc_CNEN,SYNC);
            j1_mc = j1_mc_d_norm;
            pvPut(j1_mc);
            move_last_motor = 1;
        }state s_move_J2CSP
*/
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
    /* Moving gap center in NEG direction: fixed width of gap == scanning */
/*
        when( (sequence & J1_MC_NEG) && (sequence & J2_MC_NEG) && ((us_gap_c == us_gap_n) && (ds_gap_c == ds_gap_n)) ){
            if(SNL_DEBUG)
                printf( "\tMoving center NEG: triggering J2 and starting J1 \n");
            j2_mc_CNEN = 1;
            pvPut(j2_mc_CNEN,SYNC);
            j2_mc = j2_mc_d_norm;
            pvPut(j2_mc);
            move_last_motor = 1;
        }state s_move_J1CSP
*/
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
    /* Moving jaws in POS direction or closing gap */
        when( (sequence & (J1_MC_POS|J1_MC_NEG)) && (sequence & J2_MC_POS) && (move_another_motor == 0) ){
            if(SNL_DEBUG)
                printf( "\tPOS move or closing jaws: start with J1 \n");
            move_another_motor = 1;
        }state s_move_J1CSP

        when( (sequence & (J1_MC_POS|J1_MC_NEG)) && (sequence & J2_MC_POS) && (move_another_motor == 1) ){
            if(SNL_DEBUG)
                printf( "\tPOS move or closing jaws: continue with J2 \n");
            move_another_motor = 0;
            move_last_motor = 1;
        }state s_move_J2CSP
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
    /* Moving jaws NEG direction */
        when( (sequence & J1_MC_NEG) && (sequence & J2_MC_NEG) && (move_another_motor == 0) ){
            if(SNL_DEBUG)
                printf( "\tMoving jaws NEG: start with J2 \n");
            move_another_motor = 1;
        }state s_move_J2CSP

        when( (sequence & J1_MC_NEG) && (sequence & J2_MC_NEG) && (move_another_motor == 1) ){
            if(SNL_DEBUG)
                printf( "\tMoving jaws NEG: continue with J1 \n");
            move_another_motor = 0;
            move_last_motor = 1;
        }state s_move_J1CSP
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
    /* Debug exit on timeout */       
		when(delay(10)){
            if(SNL_DEBUG)
                printf("\tTimeout\n");
        }state s_stopEverything
    }
/* ################################################################################################################# */
    state s_move_J1 {
        entry {
            if(SNL_DEBUG){
                printf( "-> Entering s_move_J1 state\n");
                printf("othrJaw: %d\t othrMotr: %d\n",move_other_jaw,move_another_motor);
            }
        }
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
    /* CSP increasing, move CSP first */
        when( (sequence & J1_MC_POS) && (move_another_motor == 0) ){
            if(SNL_DEBUG)
                printf("\t\t\t\tMoving J1: start with CSP \n");
            move_another_motor = 1;
        }state s_move_J1CSP

        when( (sequence & J1_MC_POS) && (move_another_motor == 1) ){
            if(SNL_DEBUG)
                printf("\t\t\t\tMoving J1: continue with TRM \n");
            move_another_motor = 0;
            move_last_motor = 1;
        }state s_move_J1TRM
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
    /* CSP decreasing, move TRM first */
        when( (sequence & J1_MC_NEG) && (move_another_motor == 0) && !(MSS_SWITCH) ){
            if(SNL_DEBUG)
                printf("\t\t\t\tMoving J1: start with TRM \n");
            move_another_motor = 1;
        }state s_move_J1TRM

        when( (sequence & J1_MC_NEG) && (move_another_motor == 1) && !(MSS_SWITCH) ){
            if(SNL_DEBUG)
                printf("\t\t\t\tMoving J1: continue with CSP \n");
            move_another_motor = 0;
            move_last_motor = 1;
        }state s_move_J1CSP
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
    /* CSP at same spot, move TRM */
        when( (sequence & (J1_MT_POS|J1_MT_NEG)) && (move_another_motor == 0) && !(MSS_SWITCH) ){
            if(SNL_DEBUG)
                printf("\t\t\t\tMoving J1: move TRM only - 1st\n");
            move_another_motor = 1;
        }state s_move_J1TRM

        when( (sequence & (J1_MT_POS|J1_MT_NEG)) && (move_another_motor == 1) && !(MSS_SWITCH) ){
            if(SNL_DEBUG)
                printf("\t\t\t\tMoving J1: move TRM only - 2nd\n");
            move_another_motor = 0;
            move_last_motor = 1;
        }state s_move_J1TRM
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
    /* Debug exit on timeout */       
		when(delay(10)){
            if(SNL_DEBUG)
                printf("\tTimeout\n");
        }state s_stopEverything
    }
/* ################################################################################################################# */
    state s_move_J2 {
        entry {
            if(SNL_DEBUG){
                printf( "-> Entering s_move_J2 state\n");
                printf("othrJaw: %d\t othrMotr: %d\n",move_other_jaw,move_another_motor);
            }
        }
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
    /* CSP decreasing, move CSP first */
        when( (sequence & J2_MC_NEG) && (move_another_motor == 0) ){
            if(SNL_DEBUG)
                printf("\t\t\t\tMoving J2: start with CSP \n");
            move_another_motor = 1;
        }state s_move_J2CSP

        when( (sequence & J2_MC_NEG) && (move_another_motor == 1) ){
            if(SNL_DEBUG)
                printf("\t\t\t\tMoving J2: continue with TRM \n");
            move_another_motor = 0;
            move_last_motor = 1;
        }state s_move_J2TRM
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
    /* CSP increasing, move TRM first */
        when( (sequence & J2_MC_POS) && (move_another_motor == 0) && !(MSS_SWITCH) ){
            if(SNL_DEBUG)
                printf("\t\t\t\tMoving J2: start with TRM \n");
            move_another_motor = 1;
        }state s_move_J2TRM

        when( (sequence & J2_MC_POS) && (move_another_motor == 1) && !(MSS_SWITCH) ){
            if(SNL_DEBUG)
                printf("\t\t\t\tMoving J2: continue with CSP \n");
            move_another_motor = 0;
            move_last_motor = 1;
        }state s_move_J2CSP
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
    /* CSP at same spot, move TRM */
        when( (sequence & (J2_MT_POS|J2_MT_NEG)) && (move_another_motor == 0) && !(MSS_SWITCH) ){
            if(SNL_DEBUG)
                printf("\t\t\t\tMoving J2: move TRM only - 1st\n");
            move_another_motor = 1;
        }state s_move_J2TRM

        when( (sequence & (J2_MT_POS|J2_MT_NEG)) && (move_another_motor == 1) && !(MSS_SWITCH) ){
            if(SNL_DEBUG)
                printf("\t\t\t\tMoving J2: move TRM only - 2nd \n");
            move_another_motor = 0;
            move_last_motor = 1;
        }state s_move_J2TRM
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
    /* Debug exit on timeout */       
		when(delay(10)){
            if(SNL_DEBUG)
                printf("\tTimeout\n");
        }state s_stopEverything
    }
/* ################################################################################################################# */
    state s_move_J1J2 {
        entry {
            if(SNL_DEBUG)
                printf( "-> Entering s_move_J1J2 state\n");
        }
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
    /* move J2 first */
        when( (sequence & J1_MC_NEG) && (move_other_jaw == 0)){
            if(SNL_DEBUG)
                printf( "\tMove all 4 motors: start with J2\n");
            if( move_another_motor ==  1)
                move_other_jaw = 1;
        }state s_move_J2

        when( (sequence & J1_MC_NEG) && (move_other_jaw == 1)){
            if(SNL_DEBUG)
                printf( "\tMove all 4 motors: continue with J1\n");
            if( move_another_motor ==  1)
                move_other_jaw = 0;
        }state s_move_J1
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
    /* move J1 first */
        when( (sequence & J1_MC_POS) && (move_other_jaw == 0)){
            if(SNL_DEBUG)
                printf( "\tMove all 4 motors: start with J1\n");
            if( move_another_motor ==  1)
                move_other_jaw = 1;
        }state s_move_J1

        when( (sequence & J1_MC_POS) && (move_other_jaw == 1)){
            if(SNL_DEBUG)
                printf( "\tMove all 4 motors: continue with J2\n");
            if( move_another_motor ==  1)
                move_other_jaw = 0;
        }state s_move_J2
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
    /* move J1 CSP doesn't move, determine on J2 CSP -> move J1 first */
        when( (sequence & J2_MC_POS) && (move_other_jaw == 0)){
            if(SNL_DEBUG)
                printf( "\tMove max 3 motors: start with J1\n");
            if( move_another_motor ==  1)
                move_other_jaw = 1;
        }state s_move_J1

        when( (sequence & J2_MC_POS) && (move_other_jaw == 1)){
            if(SNL_DEBUG)
                printf( "\tMove max 3 motors: continue with J2\n");
            if( move_another_motor ==  1)
                move_other_jaw = 0;
        }state s_move_J2
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
    /* move J1 CSP doesn't move, determine on J2 CSP -> move J2 first */
        when( (sequence & J2_MC_NEG) && (move_other_jaw == 0)){
            if(SNL_DEBUG)
                printf( "\tMove max 3 motors: start with J2\n");
            if( move_another_motor ==  1)
                move_other_jaw = 1;
        }state s_move_J2

        when( (sequence & J2_MC_NEG) && (move_other_jaw == 1)){
            if(SNL_DEBUG)
                printf( "\tMove max 3 motors: continue with J1\n");
            if( move_another_motor ==  1)
                move_other_jaw = 0;
        }state s_move_J1
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
    /* move J1 CSP and J2 CSP don't move, determine on J1 TRM -> move J1 first*/
        when( (sequence & J1_MT_POS) && (move_other_jaw == 0)){
            if(SNL_DEBUG)
                printf( "\tMove only TRM motors: start with J1\n");
            if( move_another_motor ==  1)
                move_other_jaw = 1;
        }state s_move_J1

        when( (sequence & J1_MT_POS) && (move_other_jaw == 1)){
            if(SNL_DEBUG)
                printf( "\tMove only TRM motors: continue with J2\n");
            if( move_another_motor ==  1)
                move_other_jaw = 0;
        }state s_move_J2
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
    /* move J1 CSP and J2 CSP don't move, determine on J1 TRM -> move J2 first*/
        when( (sequence & J1_MT_NEG) && (move_other_jaw == 0)){
            if(SNL_DEBUG)
                printf( "\tMove only TRM motors: start with J2\n");
            if( move_another_motor ==  1)
                move_other_jaw = 1;
        }state s_move_J2

        when( (sequence & J1_MT_NEG) && (move_other_jaw == 1)){
            if(SNL_DEBUG)
                printf( "\tMove only TRM motors: continue with J1\n");
            if( move_another_motor ==  1)
                move_other_jaw = 0;
        }state s_move_J1
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
    /* Debug exit on timeout */       
		when(delay(10)){
            if(SNL_DEBUG)
                printf("\tTimeout\n");
        }state s_stopEverything
    }
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
}

/*
vim:filetype=cpp:
*/
