/**
 *
 * Project       : ISTTOK slow Control Sequencer
 * Docs: https://www-csr.bessy.de/control/SoftDist/sequencer/
 *
 * File          : $Id$
 * Description   : Pulse Sequence State Machine
 *
 * Author(s)     : Bernardo Carvalho (IPFN-IST)
 *               : Paulo Fortuna Carvalho (IPFN-IST)
 * Copyright (c) : (IPFN-IST)
 *
 * Created 5-Mar-2013
 * Revised 07-May-2021
 * Modified 18-Nov-2019
 *
 **/
//%% #include  "errlog.h"

program IsttokSeqExec ("output=logFile.txt")
%{#include "errlog.h"}%
%%#include <fcntl.h>    /* for exit() */
%%#include <stdio.h>    /* for exit() */
%%#include <stdlib.h>    /* for exit() */
/* CONSTANT DECLARATION*/
/*Relays and INPUT PVs on Valleman Board have negative Logic!*/
/*=================== declarations =========================*/

int fd; /* file descriptor for logging */
char *pmac; /* used to access program macros */

short PCF_RELAY_ON = 0;
short PCF_RELAY_OFF= 1;
short PCF_INPUT_ON = 0;
short PCF_INPUT_OFF= 1;
short PCF_PULSE_ON = 1;
short SEEED_RELAY_OFF= 0;
short SEEED_RELAY_ON = 1;
short SEEED_RELAY_PULSE = 1;

short STRST_NonStarting = 0;
short STRST_Starting1 = 1;
short STRST_Starting2 = 2;
short STRST_Starting3 = 3;
short STRST_Starting4 = 4;
short STRST_StartingEnd = 5;

short CLNST_NonCleaning = 0;
short CLNST_Cleaning1 = 1;
short CLNST_Cleaning2 = 2;
short CLNST_Cleaning3 = 3;
short CLNST_Cleaning4 = 4;
short CLNST_Cleaning5 = 5;
short CLNST_Cleaning6 = 6;
short CLNST_Manual    = 7;

short WSHTST_NonShoting = 0;
short WSHTST_WaitTrg  = 1;
short WSHTST_Shooting2 = 2;
short WSHTST_Shooting3 = 3;
short WSHTST_Shooting4 = 4;
short WSHTST_Shooting5 = 5;
short WSHTST_Shooting6 = 6;
short WSHTST_Shooting7 = 7;
short WSHTST_Shooting8 = 8;
short WSHTST_SShot = 9;
short WSHTST_Shooting10 = 10;
short WSHTST_Shooting11 = 11;
short WSHTST_Shooting12 = 12;
short WSHTST_ShotEnd = 13;

short  STPST_NonStopping = 0;
short  STPST_Stopping1 = 1;
short  STPST_Stopping2 = 2;
short  STPST_Stopping3 = 3;
short  STPST_StoppingEnd = 4;
//short  STPST_Stopping5 = 5;

short POS_Stopped = 0;
short POS_Starting = 1;
short POS_Idle  =2;
short POS_Clean =3;
short POS_WaitShot=4;
short POS_Stopping=5;
short POS_Emergency=6;
short POS_IdleLimbo=8;
//short POS_UPSEmergency=7;

short ON=1;
short OFF=0;
short START=1;
short STOP=0;
short CLEAN=0;
short SHOT=1;
short NO_ALARM=0;
short MINOR=1;
short MAJOR=2;
short INVALID=3;

/* PV variables  */
short IsttokOPSTATE;
assign IsttokOPSTATE to "ISTTOK:central:OPSTATE";
monitor IsttokOPSTATE;

int PulseNumber;
assign PulseNumber to "ISTTOK:central:PULSE-NUMBER";
monitor PulseNumber;

short IsttokLastOPSTATE;
assign IsttokLastOPSTATE to "ISTTOK:central:LASTOPSTATE";
monitor IsttokLastOPSTATE;

short IsttokOPREQ;
assign IsttokOPREQ to "ISTTOK:central:OPREQ";
monitor IsttokOPREQ;

short IsttokProcMode;
assign IsttokProcMode to  "ISTTOK:central:PROCESS-MODE";
monitor IsttokProcMode;

short IsttokProcReq;
assign IsttokProcReq  to "ISTTOK:central:PROCESS-REQ";
monitor IsttokProcReq;

short STARTINGSTATE;
assign STARTINGSTATE to "ISTTOK:central:STARTINGSTATE";
monitor STARTINGSTATE;

short CLEANINGSTATE;
assign CLEANINGSTATE to "ISTTOK:central:CLEANINGSTATE";
monitor CLEANINGSTATE;

short CLEANING_AUTOMODE;
assign CLEANING_AUTOMODE to "ISTTOK:central:CLEANINGMANMODE";
monitor CLEANING_AUTOMODE;

short WSHOTINGSTATE;
assign WSHOTINGSTATE to "ISTTOK:central:WSHOTINGSTATE";

short STOPPINGSTATE;
assign STOPPINGSTATE to "ISTTOK:central:STOPPINGSTATE";
monitor STOPPINGSTATE;

short IsttokRPump1_Motor;
assign IsttokRPump1_Motor to "ISTTOK:central:RPump1-Motor";

short IsttokRPump1_Valve;
assign IsttokRPump1_Valve to "ISTTOK:central:RPump1-Valve";

short IsttokRPump2_Motor;
assign IsttokRPump2_Motor to "ISTTOK:central:RPump2-Motor";

short IsttokRPump2_Valve;
assign IsttokRPump2_Valve to "ISTTOK:central:RPump2-Valve";
/* No use for these, Controller does not respond
short IsttokTMPump1_ControllerOff;
assign IsttokTMPump1_ControllerOff to "ISTTOK:central:TMPump1-ControllerOff";

short IsttokTMPump1_ControllerOn;
assign IsttokTMPump1_ControllerOn to "ISTTOK:central:TMPump1-ControllerOn";
*/

short IsttokTMPump1_MotorOff;
assign IsttokTMPump1_MotorOff to "ISTTOK:central:TMPump1-MotorOff";

short IsttokTMPump1_MotorOn;
assign IsttokTMPump1_MotorOn to "ISTTOK:central:TMPump1-MotorOn";

/*short IsttokTMPump2_Motor;*/
/*assign IsttokTMPump2_Motor to "ISTTOK:central:TMPump2-Motor";*/

short TMPump1NormalOperation;
assign TMPump1NormalOperation to "ISTTOK:central:TMPump1-NormalOperation";
monitor TMPump1NormalOperation;

short IsttokTMPump1_Power;
assign IsttokTMPump1_Power to "ISTTOK:central:TMPump1-Power";
monitor IsttokTMPump1_Power;

short IsttokTMPump1_Emergency;
assign IsttokTMPump1_Emergency to "ISTTOK:central:TMPump1-Emergency";
monitor IsttokTMPump1_Emergency;

short IsttokTMPump1_ManualValve;
assign IsttokTMPump1_ManualValve to "ISTTOK:central:TMPump1-ManualValve";
monitor IsttokTMPump1_ManualValve;

/*
short IsttokTMPump2_Emergency;
assign IsttokTMPump2_Emergency to "ISTTOK:central:TMPump2-Emergency";
monitor IsttokTMPump2_Emergency;
*/

float IsttokTMPump1_Pressure;
assign IsttokTMPump1_Pressure to "ISTTOK:central:TMPump1-PressureAdmission";
monitor IsttokTMPump1_Pressure;

float IsttokRPump1_Pressure;
/*assign IsttokRPump1_Pressure to "ISTTOK:vacuum:RPump1-Pressure";*/
assign IsttokRPump1_Pressure to "ISTTOK:central:RPump1-Pressure";
monitor IsttokRPump1_Pressure;

/*
float IsttokRPump2_Pressure;
assign IsttokRPump2_Pressure to "ISTTOK:central:RPump2-Pressure";
monitor IsttokRPump2_Pressure;
*/

float IsttokVVesselTemperature;
assign IsttokVVesselTemperature to "ISTTOK:temperature:VVessel-Temperature";
monitor IsttokVVesselTemperature;

short IsttokBuzzer;
assign IsttokBuzzer to "ISTTOK:central:Buzzer";

short IsttokLab_WarningLight;
assign IsttokLab_WarningLight to "ISTTOK:central:Lab-WarningLight";

short IsttokGasIS_Valves;
assign IsttokGasIS_Valves to "ISTTOK:central:GasIS-Valves";

short IsttokVVessel_Filament;
assign IsttokVVessel_Filament to "ISTTOK:central:VVessel-Filament";

short IsttokCapBank_Charge;
assign IsttokCapBank_Charge to "ISTTOK:central:CapBank-Charge";

short IsttokCapBank_Discharge;
assign IsttokCapBank_Discharge to "ISTTOK:central:CapBank-Discharge";

short IsttokClean_TorContactor;
assign IsttokClean_TorContactor to "ISTTOK:central:Clean-TorContactor";

short IsttokClean_TorPneuBreaker;
assign IsttokClean_TorPneuBreaker to "ISTTOK:central:Clean-TorPneuBreaker";

short Isttok_TorPSTrigger;
assign Isttok_TorPSTrigger to "ISTTOK:central:Shot-TorPSTrigger";
monitor Isttok_TorPSTrigger;

short Isttok_TorPSDisable;
assign Isttok_TorPSDisable to "ISTTOK:central:Shot-TorPSDisable";

short IsttokClean_PrimContactor;
assign IsttokClean_PrimContactor to "ISTTOK:central:Clean-PrimContactor";

short IsttokClean_PrimPneuBreaker;
assign IsttokClean_PrimPneuBreaker to "ISTTOK:central:Clean-PrimPneuBreaker";

short IsttokElco_Enable;
assign IsttokElco_Enable to "ISTTOK:central:ELCO-Enable";

short IsttokWaitTimer;
assign IsttokWaitTimer to "ISTTOK:central:COUNTDOWN";
monitor IsttokWaitTimer;

//string IsttokWaitTimerScan;
//assign IsttokWaitTimerScan to "ISTTOK:central:COUNTDOWN.SCAN";

short IsttokWaitTimerDisa;
assign IsttokWaitTimerDisa to "ISTTOK:central:COUNTDOWN.DISA";

short IsttokEmergency;
assign IsttokEmergency to "ISTTOK:central:Emergency";
monitor IsttokEmergency;

short IsttokTTSystem_tzero;
assign IsttokTTSystem_tzero to  "ISTTOK:central:TTSystem-tzero";
monitor IsttokTTSystem_tzero;

short IsttokupsBatteryON;
assign IsttokupsBatteryON  "{unit}:central:UPS-OnBattery";
monitor IsttokupsBatteryON;

short IsttokUpsShutdown;
assign IsttokUpsShutdown  "ISTTOK:central:UPS-Shutdown";
monitor IsttokUpsShutdown;

//string tmp1_stat;

/* Trace message record limited to 40 characters */
string msg;
assign msg to "ISTTOK:central:TraceMessage.VAL";
//monitor msg;

string sendMail;
assign sendMail to "ISTTOK:central:sendmail";

/* Main Pulse State Set */
ss PulseSequence {
/*********** State: init ****************** */
    state init {
    entry {
            fd = -1;
            /* Use parameter to define logging file */

            pmac = macValueGet("output");
            if (pmac == 0 || pmac[0] == 0) {
                printf("No macro defined for \"output\"\n");
                fd = 1;
            }
            else {
                fd = open(pmac, (O_CREAT | O_WRONLY), 0664);
/*                if (fd == ERROR) { */
                if (fd == -1) {
                    printf("Can't open %s\n", pmac);
                    exit (-1);
                }
            }
            dprintf(fd, "Starting program\n");
            strcpy(msg, "INIT: Sequence Entry");
            pvPut(msg);
            errlogSevPrintf(NO_ALARM, "%s\n",msg);

            strcpy(msg, "Initializing...");
            pvPut(msg);
            errlogSevPrintf(NO_ALARM, "%s\n",msg);

            IsttokWaitTimer = 160;
            pvPut(IsttokWaitTimer);

//            IsttokProcMode = OFF; // UPON POWER-DOWN OR FAILURE PREVENT TO ENTER WAITSHOT ON POWER-UP!
//            pvPut(IsttokProcMode);

            IsttokClean_PrimContactor = SEEED_RELAY_OFF;
            pvPut(IsttokClean_PrimContactor);

            IsttokClean_PrimPneuBreaker = SEEED_RELAY_OFF;
            pvPut(IsttokClean_PrimPneuBreaker);

            IsttokBuzzer = SEEED_RELAY_OFF;
            pvPut(IsttokBuzzer);

            IsttokCapBank_Charge = SEEED_RELAY_OFF;
            pvPut(IsttokCapBank_Charge);

            IsttokCapBank_Discharge = SEEED_RELAY_OFF;
            pvPut(IsttokCapBank_Discharge);

            IsttokVVessel_Filament = PCF_RELAY_OFF;
            pvPut(IsttokVVessel_Filament);

            IsttokElco_Enable= PCF_RELAY_OFF;
            pvPut(IsttokElco_Enable);

            IsttokGasIS_Valves = SEEED_RELAY_OFF;
            pvPut(IsttokGasIS_Valves);

            IsttokLab_WarningLight = SEEED_RELAY_OFF;
            pvPut(IsttokLab_WarningLight);

            IsttokClean_TorPneuBreaker = SEEED_RELAY_OFF;
            pvPut(IsttokClean_TorPneuBreaker);

            IsttokClean_TorContactor = SEEED_RELAY_OFF;
            pvPut(IsttokClean_TorContactor);

            Isttok_TorPSDisable = SEEED_RELAY_OFF;
            pvPut(Isttok_TorPSDisable);

            Isttok_TorPSTrigger= OFF;
            pvPut(Isttok_TorPSTrigger);
/*
            IsttokTMPump1_ControllerOff = OFF;
            pvPut(IsttokTMPump1_ControllerOff);

            IsttokTMPump1_ControllerOn = OFF;
            pvPut(IsttokTMPump1_ControllerOn);
*/
            IsttokTMPump1_MotorOff = OFF;
            pvPut(IsttokTMPump1_MotorOff);

            IsttokTMPump1_MotorOn = OFF;
            pvPut(IsttokTMPump1_MotorOn);

          //  IsttokTMPump2_Motor = PCF_RELAY_OFF;
            //pvPut(IsttokTMPump2_Motor);

            if ((IsttokTMPump1_Power == PCF_INPUT_OFF)) {
                strcpy(msg, "TMP1 Controller is OFF");
                pvPut(msg);
                errlogSevPrintf(NO_ALARM, "%s\n",msg);
            } else{
                strcpy(msg, "TMP1 Controller is ON");
                pvPut(msg);
                errlogSevPrintf(NO_ALARM, "%s\n",msg);
            }
    }
        when (delay(5) && IsttokOPREQ == STOP)  {
            strcpy(msg, "State to Stopping");
            pvPut(msg);
            errlogSevPrintf(MINOR, "%s\n",msg);
            /* State change to Stopping */
//            IsttokOPSTATE = POS_Stopping;
//            pvPut(IsttokOPSTATE);
        } state Stopping

        when (delay(5) && IsttokOPREQ == START ) {
            strcpy(msg, "State to Starting");
            pvPut(msg);
            errlogSevPrintf(NO_ALARM, "%s\n",msg);
       /* State change to Starting */
//            IsttokOPSTATE = POS_Starting;
//            pvPut(IsttokOPSTATE);
        } state Starting
    }

/*********** State: Stopped ****************** */
    state Stopped {
    entry{
            IsttokLastOPSTATE = POS_Stopped;
            pvPut(IsttokLastOPSTATE);
    }
        when(IsttokEmergency == ON){
            strcpy(msg, "Emergency from Stopped");
            pvPut(msg);
            errlogSevPrintf(NO_ALARM, "%s\n",msg);
            /* Pulse state change to Emergency*/
            IsttokOPSTATE = POS_Stopped;
            pvPut(IsttokOPSTATE);
        } state Emergency

        when ((IsttokOPREQ == START) && (IsttokupsBatteryON != ON)) {
//              &&  (pvStatus(IsttokRPump1_Pressure) == pvStatOK) && (pvStatus(IsttokTMPump1_Emergency) == pvStatOK)) {
            strcpy(msg, "State to Starting");
            pvPut(msg);
            errlogSevPrintf(NO_ALARM, "%s\n",msg);
            /* Pulse state change to Starting */
        } state Starting
    }

/********  State: Starting ***********************/
    state Starting {
        entry{
            IsttokOPSTATE = POS_Starting;
            pvPut(IsttokOPSTATE);
        }
        when(IsttokEmergency == ON) {
            strcpy(msg, "Starting: ->Emergency");
            pvPut(msg);
            errlogSevPrintf(MAJOR, "%s\n",msg);
            /* Pulse state change to Emergency*/
        } state Emergency
        when (IsttokupsBatteryON == ON) {
            strcpy(msg, "Starting: Power UPS OB, Starting to Stopping");
            pvPut(msg);
            errlogSevPrintf(MAJOR, "%s\n",msg);
            IsttokOPREQ = STOP;
            pvPut(IsttokOPREQ);
        } state Stopping
        when (IsttokOPREQ == STOP) {
            strcpy(msg, "Starting: State to Stopping");
            pvPut(msg);
            errlogSevPrintf(MINOR, "%s\n",msg);
            /* Pulse state change to Stopping */
        } state Stopping

        when (STARTINGSTATE == STRST_StartingEnd ) {
            strcpy(msg, "Starting: State to Idle");
            pvPut(msg);
            errlogSevPrintf(NO_ALARM, "%s\n",msg);
            /* Pulse state change to Idle*/
        } state Idle
    }

/********** State: Idle ****************************/
    state Idle {
        entry {
            IsttokOPSTATE = POS_Idle;
            pvPut(IsttokOPSTATE);
            IsttokLastOPSTATE = POS_Idle;
            pvPut(IsttokLastOPSTATE);
            Isttok_TorPSDisable = SEEED_RELAY_OFF;
            pvPut(Isttok_TorPSDisable);
            IsttokProcReq = STOP;
            pvPut(IsttokProcReq);
        }
        when (IsttokUpsShutdown == ON) {
            strcpy(msg, "Idle: UPS Power Emergency Shutdown. -> Stopping");
            pvPut(msg);
            errlogSevPrintf(MAJOR, "%s\n",msg);
            IsttokOPREQ = STOP;
            pvPut(IsttokOPREQ);
        } state Stopping

        when(IsttokEmergency == ON){
            strcpy(msg, "Idle: ->Emergency");
            pvPut(msg);
            errlogSevPrintf(MAJOR, "%s\n",msg);
            /* Pulse state change to Emergency*/
        } state Emergency

        when (delay(5.0) && (pvStatus(IsttokRPump1_Pressure) != pvStatOK) ||  (pvStatus(IsttokTMPump1_Emergency) != pvStatOK) ) {
    //     pvStatus(IsttokTMPump1_Emergency) != pvStatOK || pvStatus(IsttokTMPump2_Emergency) != pvStatOK) {
            /* Vacuum emergency: Pulse state change to Stopping */
            /*IsttokOPREQ = STOP; State should recover from */
            /*pvPut(IsttokOPREQ);*/
            //strcpy(sendMail, "Vacuum Emergency while Idle");
            strcpy(msg, "Idle: -> IdleLimbo");
            pvPut(msg);
            errlogSevPrintf(MINOR, "%s\n", msg);
            //sprintf(sendMail, "Vacuum Emergency while Idle, Rpump1 Pressure:%1.1e, TMP1 Adm. Pressure:%4.2e, TMP1 Emergency:%d",
            //        IsttokRPump1_Pressure, IsttokTMPump1_Pressure, IsttokTMPump1_Emergency);
            //pvPut(sendMail);
        } state IdleLimbo

        when (IsttokOPREQ==STOP || IsttokTMPump1_ManualValve == OFF) {
            /*Just to be sure */
//            IsttokProcReq = STOP;
//            pvPut(IsttokProcReq);

            strcpy(msg, "Idle: State to Stopping");
            pvPut(msg);
            errlogSevPrintf(MINOR, "%s\n",msg);
            /* Pulse state change to Stopping */
        } state Stopping

        when (IsttokProcReq==START && IsttokProcMode==CLEAN ) {
            strcpy(msg, "Idle: State to Clean");
            pvPut(msg);
            errlogSevPrintf(NO_ALARM, "%s\n",msg);
            /* Pulse state change to Clean */
        } state Clean

        when (IsttokProcReq==START && IsttokProcMode==SHOT ) {
            strcpy(msg, "Idle: State to WaitShot");
            pvPut(msg);
            errlogSevPrintf(NO_ALARM, "%s\n",msg);
            /* Pulse state change to WaitShot */
        } state WaitShot
    }

/**          State: IdleLimbo                       ***/
    state IdleLimbo {
        entry {
            IsttokOPSTATE = POS_IdleLimbo;
            pvPut(IsttokOPSTATE);
            IsttokLastOPSTATE = POS_IdleLimbo;
            pvPut(IsttokLastOPSTATE);
        }
        when (IsttokUpsShutdown == ON) {
            strcpy(msg, "IdleLimbo: Power Emergency. Stopping");
            pvPut(msg);
            errlogSevPrintf(MAJOR, "%s\n",msg);
            IsttokOPREQ = STOP;
            pvPut(IsttokOPREQ);
        } state Stopping

        when(IsttokEmergency == ON){
            strcpy(msg, "IdleLimbo: Emergency");
            pvPut(msg);
            errlogSevPrintf(MAJOR, "%s\n",msg);
            /* Pulse state change to Emergency*/
        } state Emergency

        when ( delay(10.0) && (pvStatus(IsttokRPump1_Pressure) == pvStatOK) &&  (pvStatus(IsttokTMPump1_Emergency) == pvStatOK) ) {
    //     pvStatus(IsttokTMPump1_Emergency) != pvStatOK || pvStatus(IsttokTMPump2_Emergency) != pvStatOK) {
            strcpy(msg, "IdleLimbo: Returning to Idle.");
            pvPut(msg);
            errlogSevPrintf(MINOR, "%s\n", msg);
        } state Idle

        when ( delay(20.0) && (pvStatus(IsttokRPump1_Pressure) != pvStatOK) ||  (pvStatus(IsttokTMPump1_Emergency) != pvStatOK) ) {
    //     pvStatus(IsttokTMPump1_Emergency) != pvStatOK || pvStatus(IsttokTMPump2_Emergency) != pvStatOK) {
            /* Vacuum emergency: Pulse state change to Stopping */
            IsttokOPREQ = STOP;
            pvPut(IsttokOPREQ);
            //strcpy(sendMail, "Vacuum Emergency while Idle");
            strcpy(msg, "IdleLimbo: Stopping");
            pvPut(msg);
            errlogSevPrintf(MINOR, "%s\n", msg);
            sprintf(sendMail, "Vacuum Emergency while Idle, Rpump1 Pressure:%1.1e, TMP1 Adm. Pressure:%4.2e, TMP1 Emergency:%d",
                    IsttokRPump1_Pressure, IsttokTMPump1_Pressure, IsttokTMPump1_Emergency);
            pvPut(sendMail);
        } state Stopping
    }

/*******  State: Clean ***************************/
    state Clean {
    entry{
            IsttokOPSTATE = POS_Clean;
            pvPut(IsttokOPSTATE);
    }
        when ((IsttokProcReq==STOP) && (CLEANINGSTATE == CLNST_NonCleaning))  {
            strcpy(msg, "Clean: State to Idle");
            pvPut(msg);
            errlogSevPrintf(NO_ALARM, "%s\n",msg);
        } state Idle

        when (IsttokOPREQ == STOP) {} state Stopping

        when (IsttokupsBatteryON == ON) {
            strcpy(msg, "Clean: Power UPS Battery ON, -> Idle");
            pvPut(msg);
            errlogSevPrintf(MAJOR, "%s\n",msg);
            IsttokOPREQ = STOP;
            pvPut(IsttokOPREQ);
       /* State change to UPSEmergency */
            /*IsttokOPSTATE = POS_Idle;*/
            /*pvPut(IsttokOPSTATE);*/
        } state Idle

        when ( pvStatus(IsttokTMPump1_Emergency) != pvStatOK)  {
        //when ( pvStatus(IsttokTMPump1_Emergency) != pvStatOK ||
    //       pvStatus(IsttokTMPump2_Emergency) != pvStatOK)  {
            /* Vacuum emergency: Pulse state change to Stopping */
            IsttokOPREQ = STOP;
            pvPut(IsttokOPREQ);
            strcpy(msg, "Clean: State to Stopping");
            pvPut(msg);
            errlogSevPrintf(MAJOR, "%s\n",msg);
        } state Stopping

        when(IsttokEmergency == ON){
            strcpy(msg, "Clean: Emergency from Clean");
            pvPut(msg);
            errlogSevPrintf(MAJOR, "%s\n",msg);
            /* Pulse state change to Emergency*/
        } state Emergency
    }

/******** State: WaitShot *************************/
    state WaitShot {
    entry{
            strcpy(msg, "Toroidal Shot Enable");
            pvPut(msg);
            errlogSevPrintf(NO_ALARM, "%s\n",msg);
/*
            Isttok_TorPSDisable = SEEED_RELAY_ON;
            pvPut(Isttok_TorPSDisable);
*/
            IsttokOPSTATE = POS_WaitShot;
            pvPut(IsttokOPSTATE);
    }
        when (IsttokupsBatteryON == ON) {
            strcpy(msg, "Power UPS OB, WaitShot to Idle");
            pvPut(msg);
            errlogSevPrintf(MAJOR, "%s\n",msg);
            IsttokOPREQ = STOP;
            pvPut(IsttokOPREQ);
       /* State change to UPSEmergency */
            /*IsttokOPSTATE = POS_Idle;*/
            /*pvPut(IsttokOPSTATE);*/
        } state Idle

        when(IsttokEmergency == ON){
            strcpy(msg, "Emergency from WaitShot");
            pvPut(msg);
            errlogSevPrintf(MAJOR, "%s\n",msg);
            /* Pulse state change to Emergency*/
        } state Emergency
        when (IsttokProcReq==STOP )  {
            strcpy(msg, "State to Idle");
            pvPut(msg);
            errlogSevPrintf(MINOR, "%s\n",msg);
        } state Idle

        when (IsttokOPREQ == STOP) {
            /* Pulse state change to Stopping */
        } state Stopping

        when ( pvStatus(IsttokTMPump1_Emergency) != pvStatOK)  {
        //when ( pvStatus(IsttokTMPump1_Emergency) != pvStatOK ||
    //       pvStatus(IsttokTMPump2_Emergency) != pvStatOK)  {
            /* Vacuum emergency: Pulse state change to Stopping */
            IsttokOPREQ = STOP;
            pvPut(IsttokOPREQ);
            strcpy(msg, "State to Stopping");
            pvPut(msg);
            errlogSevPrintf(MAJOR, "%s\n",msg);
        } state Stopping
    }

    /* ************** State: Stopping *********** */
    state Stopping {
    entry{
            IsttokOPSTATE = POS_Stopping;
            pvPut(IsttokOPSTATE);
            /*Just to be sure */
            Isttok_TorPSDisable = SEEED_RELAY_OFF;
            pvPut(Isttok_TorPSDisable);

            IsttokCapBank_Charge = SEEED_RELAY_OFF;
            pvPut(IsttokCapBank_Charge);

            IsttokCapBank_Discharge = SEEED_RELAY_OFF;
            pvPut(IsttokCapBank_Discharge);

            IsttokGasIS_Valves = SEEED_RELAY_OFF;
            pvPut(IsttokGasIS_Valves);

            IsttokVVessel_Filament = PCF_RELAY_OFF;
            pvPut(IsttokVVessel_Filament);

            IsttokProcReq = STOP;
            pvPut(IsttokProcReq);

            IsttokLastOPSTATE = POS_Stopping;
            pvPut(IsttokLastOPSTATE);
    }

        when (delay(5.0) && (IsttokOPREQ == START) && (IsttokupsBatteryON != ON) ) {
    //&&         (pvStatus(IsttokRPump1_Pressure) == pvStatOK) && (pvStatus(IsttokTMPump1_Emergency) == pvStatOK)) {
            strcpy(msg, "State Stopping to Starting");
            pvPut(msg);
            errlogSevPrintf(NO_ALARM, "%s\n",msg);
       /* State change to Starting */
            IsttokOPSTATE = POS_Starting;
            pvPut(IsttokOPSTATE);
        } state Starting

        when (STOPPINGSTATE == STPST_StoppingEnd) {
            IsttokOPSTATE = POS_Stopped;
            pvPut(IsttokOPSTATE);
        } state Stopped
    }

    /************* State: Emergency ******************/
    state Emergency {
    entry {
            strcpy(msg, "Emergency Toroidal Shot Disable");
            pvPut(msg);
            errlogSevPrintf(MAJOR, "%s\n",msg);
            Isttok_TorPSDisable = SEEED_RELAY_ON;
            pvPut(Isttok_TorPSDisable);

            strcpy(msg, "EMERGENCY: CAPBANK Charge OFF");
            pvPut(msg);
            errlogSevPrintf(NO_ALARM, "%s\n",msg);
            IsttokCapBank_Charge = SEEED_RELAY_OFF;
            pvPut(IsttokCapBank_Charge);

            strcpy(msg, "EMERGENCY: CAPBANK CrowBar Close");
            pvPut(msg);
            errlogSevPrintf(NO_ALARM, "%s\n",msg);
            IsttokCapBank_Discharge = SEEED_RELAY_OFF;
            pvPut(IsttokCapBank_Discharge);

            strcpy(msg, "Prim_Pneu_Breaker OFF");
            pvPut(msg);
            IsttokClean_PrimPneuBreaker = SEEED_RELAY_OFF;
            pvPut(IsttokClean_PrimPneuBreaker);

            strcpy(msg, "Tor_Pneu_Breaker OFF");
            pvPut(msg);
            IsttokClean_TorPneuBreaker = SEEED_RELAY_OFF;
            pvPut(IsttokClean_TorPneuBreaker);

            strcpy(msg, "EMERGENCY: GIS Valves CLOSE");
            pvPut(msg);
            errlogSevPrintf(NO_ALARM, "%s\n",msg);
            IsttokGasIS_Valves = SEEED_RELAY_OFF;
            pvPut(IsttokGasIS_Valves);

            strcpy(msg, "EMERGENCY: Filament OFF");
            pvPut(msg);
            errlogSevPrintf(MAJOR, "%s\n",msg);
            IsttokVVessel_Filament = PCF_RELAY_OFF;
            pvPut(IsttokVVessel_Filament);
            
            IsttokElco_Enable= PCF_RELAY_OFF;
            pvPut(IsttokElco_Enable);

        }
        when (delay(1.0) && (IsttokEmergency == OFF) && (IsttokOPSTATE == POS_WaitShot || IsttokOPSTATE==POS_Clean) ){
            /* Pulse state change to Idle */
            strcpy(msg, "State to Idle");
            pvPut(msg);
            errlogSevPrintf(NO_ALARM, "%s\n",msg);
            IsttokProcReq = STOP;
            pvPut(IsttokProcReq);
            IsttokOPSTATE = POS_Idle;
            pvPut(IsttokOPSTATE);
        } state Idle
        when (delay(1.0) && (IsttokEmergency == OFF) && (IsttokOPSTATE == POS_Idle)){
            /* Pulse state change to Idle */
            strcpy(msg, "State to Idle");
            pvPut(msg);
            errlogSevPrintf(NO_ALARM, "%s\n",msg);
            IsttokOPSTATE = POS_Idle;
            pvPut(IsttokOPSTATE);
        } state Idle
        when (delay(1.0) && (IsttokEmergency == OFF) && (IsttokOPSTATE == POS_Starting)){
           // TODO Check these 2 transictions
            strcpy(msg, "State to Stopped");
            pvPut(msg);
            errlogSevPrintf(NO_ALARM, "%s\n",msg);
            IsttokOPREQ = STOP;
            pvPut(IsttokOPREQ);
            /* Pulse state change to Stopped */
            IsttokOPSTATE = POS_Stopped;
            pvPut(IsttokOPSTATE);
        } state Stopped

        when (delay(1.0) && (IsttokEmergency == OFF) && (IsttokOPSTATE == POS_Stopped)){

            strcpy(msg, "State to Stopped");
            pvPut(msg);
            errlogSevPrintf(NO_ALARM, "%s\n",msg);
            /* Pulse state change to Stopped */
            IsttokOPSTATE = POS_Stopped;
            pvPut(IsttokOPSTATE);
        } state Stopped
    }
    
    /***** State: UpsEmergency *****/
/*
 *    state UpsEmergency {
 *    entry {
 *            strcpy(msg, "Emergency Toroidal Shot Disable");
 *            errlogSevPrintf(MAJOR, "%s\n",msg);
 *            Isttok_TorPSDisable = PCF_RELAY_OFF;
 *            pvPut(Isttok_TorPSDisable);
 *
 *            strcpy(msg, "EMERGENCY: CAPBANK Charge OFF");
 *            errlogSevPrintf(NO_ALARM, "%s\n",msg);
 *            IsttokCapBank_Charge = PCF_RELAY_OFF;
 *            pvPut(IsttokCapBank_Charge);
 *
 *            strcpy(msg, "EMERGENCY: CAPBANK CrowBar Close");
 *            errlogSevPrintf(NO_ALARM, "%s\n",msg);
 *            IsttokCapBank_Discharge = SEEED_RELAY_OFF;
 *            pvPut(IsttokCapBank_Discharge);
 *
 *            strcpy(msg, "EMERGENCY: GIS Valves CLOSE");
 *            errlogSevPrintf(NO_ALARM, "%s\n",msg);
 *            IsttokGasIS_Valves = SEEED_RELAY_OFF;
 *            pvPut(IsttokGasIS_Valves);
 *
 *            strcpy(msg, "UPS EMERGENCY: Filament OFF");
 *            pvPut(msg);
 *            errlogSevPrintf(MAJOR, "%s\n",msg);
 *            IsttokVVessel_Filament = PCF_RELAY_OFF;
 *            pvPut(IsttokVVessel_Filament);
 *    }
 *        when (IsttokupsBatteryON == ON) {
 *            [> Pulse state change to Stopping <]
 *            strcpy(msg, "State to Stopping");
 *            pvPut(msg);
 *            errlogSevPrintf(NO_ALARM, "%s\n",msg);
 *            IsttokProcReq = STOP;
 *            pvPut(IsttokProcReq);
 *            [>IsttokOPSTATE = POS_Stopping;<]
 *            [>pvPut(IsttokOPSTATE);<]
 *        } state Stopping
 *        when (delay(6.0) && (IsttokupsBatteryON == OFF) && (IsttokOPSTATE == POS_Idle)){
 *            [> Pulse state change to Idle <]
 *            strcpy(msg, "State to Idle");
 *            pvPut(msg);
 *            errlogSevPrintf(NO_ALARM, "%s\n",msg);
 *            IsttokOPSTATE = POS_Idle;
 *            pvPut(IsttokOPSTATE);
 y
 *    }
 */
}

/*********** SUB-State: StartingSequence Set ************/
ss StartingSequence {
    state NonStarting {
        entry {
            STARTINGSTATE = STRST_NonStarting;
            pvPut(STARTINGSTATE);
        }
        when (delay(2.0) && IsttokOPSTATE == POS_Starting) {
            strcpy(msg, "Rotary Pump1 ON");
            pvPut(msg);
            errlogSevPrintf(NO_ALARM, "%s\n",msg);
            IsttokRPump1_Motor = PCF_RELAY_OFF;
            pvPut(IsttokRPump1_Motor);
//            strcpy(msg, "Rotary Pump2 ON");
//            pvPut(msg);
//            errlogSevPrintf(NO_ALARM, "%s\n",msg);
//            IsttokRPump2_Motor = PCF_RELAY_OFF;
//            pvPut(IsttokRPump2_Motor);
            STARTINGSTATE = STRST_Starting1;
            pvPut(STARTINGSTATE);
        } state Starting1
    }

    state Starting1 {
        when (delay(1.0) && IsttokOPSTATE != POS_Starting ) {
            strcpy(msg, "Starting1 Out");
            pvPut(msg);
            errlogSevPrintf(NO_ALARM, "%s\n",msg);
        } state NonStarting
        when (delay(5.0)) {
            strcpy(msg, "Rotary Valve1 OPEN");
            pvPut(msg);
            errlogSevPrintf(NO_ALARM, "%s\n",msg);
            IsttokRPump1_Valve = PCF_RELAY_OFF;
            pvPut(IsttokRPump1_Valve);
//            strcpy(msg, "Rotary Valve2 OPEN");
//            pvPut(msg);
//            errlogSevPrintf(NO_ALARM, "%s\n",msg);
//            IsttokRPump2_Valve = PCF_RELAY_OFF;
//            pvPut(IsttokRPump2_Valve);
            STARTINGSTATE = STRST_Starting2;
            pvPut(STARTINGSTATE);
        } state Starting2
    }

    state Starting2 {
        when (delay(1.0) && IsttokOPSTATE != POS_Starting ) {
            strcpy(msg, "Starting2 Out");
            pvPut(msg);
            errlogSevPrintf(NO_ALARM, "%s\n",msg);
            STARTINGSTATE = STRST_NonStarting;
            pvPut(STARTINGSTATE);
        } state NonStarting

        when (delay(2.0) && IsttokTMPump1_ManualValve == ON) {
            strcpy(msg, "Manual Valve Open Checked");
            pvPut(msg);
            errlogSevPrintf(NO_ALARM, "%s\n",msg);
            STARTINGSTATE = STRST_Starting3;
            pvPut(STARTINGSTATE);
        } state Starting3
    }

    state Starting3 {
        when (delay(1.0) && IsttokOPSTATE != POS_Starting) {
            strcpy(msg, "Starting3 Out");
            pvPut(msg);
            errlogSevPrintf(NO_ALARM, "%s\n",msg);
            STARTINGSTATE = STRST_NonStarting;
            pvPut(STARTINGSTATE);
        } state NonStarting
        //when ( (pvStatus(IsttokRPump1_Pressure) == pvStatOK) && (pvStatus(IsttokRPump2_Pressure) == pvStatOK)) {
        when (delay(2.0) &&  pvStatus(IsttokRPump1_Pressure) == pvStatOK) {

                strcpy(msg, "TMP1 MotorOn ON" );
                pvPut(msg);
                errlogSevPrintf(NO_ALARM, "%s\n",msg);
                IsttokTMPump1_MotorOn = ON;
                pvPut(IsttokTMPump1_MotorOn);
//                strcpy(msg, "TMP2 Motor ON" );
//                pvPut(msg);
//                errlogSevPrintf(NO_ALARM, "%s\n",msg);
//                IsttokTMPump2_Motor = PCF_RELAY_ON;
//                pvPut(IsttokTMPump2_Motor);
                STARTINGSTATE = STRST_Starting4;
                pvPut(STARTINGSTATE);
        } state Starting4
    }

    state Starting4 {
        when (delay(1.0) && IsttokOPSTATE != POS_Starting ) {
            strcpy(msg, "Starting4 Out");
            pvPut(msg);
            errlogSevPrintf(NO_ALARM, "%s\n",msg);
            //IsttokSTARTINGSTATE = STRST_NonStarting;
            //pvPut(IsttokSTARTINGSTATE);
        } state NonStarting
        when (delay(2.0) && TMPump1NormalOperation == PCF_INPUT_ON) {   // TODO insert TMP2 Test
            strcpy(msg, "TMP1 operating Normal");
            pvPut(msg);
            errlogSevPrintf(NO_ALARM, "%s\n",msg);
            STARTINGSTATE = STRST_StartingEnd;
            pvPut(STARTINGSTATE);
        } state StartingEnd
    }

    state StartingEnd {
        when (delay(1.0) && IsttokOPSTATE != POS_Starting ) {
            STARTINGSTATE = STRST_NonStarting;
            pvPut(STARTINGSTATE);
        } state NonStarting
    }
}

/*********** SUB-State: CleaningSequence Set ************/
ss CleaningSequence{
    state NonCleaning {
    entry{
            strcpy(msg, "GIS Valves CLOSE");
            pvPut(msg);
            IsttokGasIS_Valves = SEEED_RELAY_OFF;
            pvPut(IsttokGasIS_Valves);

            strcpy(msg, "Filament OFF");
            pvPut(msg);
            IsttokVVessel_Filament = PCF_RELAY_OFF;
            pvPut(IsttokVVessel_Filament);

            strcpy(msg, "Primary_CT OFF");
            pvPut(msg);
            IsttokClean_PrimContactor = SEEED_RELAY_OFF;
            pvPut(IsttokClean_PrimContactor);

            strcpy(msg, "Toroidal_BK OPEN");
            pvPut(msg);
            IsttokClean_TorPneuBreaker = SEEED_RELAY_OFF;
            pvPut(IsttokClean_TorPneuBreaker);

            strcpy(msg, "Toroidal_CT OFF");
            pvPut(msg);
            IsttokClean_TorContactor = SEEED_RELAY_OFF;
            pvPut(IsttokClean_TorContactor);

            strcpy(msg, "Prim_Pneu_Breaker OFF");
            pvPut(msg);
            IsttokClean_PrimPneuBreaker = SEEED_RELAY_OFF;
            pvPut(IsttokClean_PrimPneuBreaker);

            CLEANINGSTATE = CLNST_NonCleaning;
            pvPut(CLEANINGSTATE);

            errlogSevPrintf(NO_ALARM, "%s\n",msg);
            strcpy(msg, "Entry Cleanning State Mach");
            pvPut(msg);
            errlogSevPrintf(NO_ALARM, "%s\n",msg);

        }
        when(delay(1.0) && CLEANING_AUTOMODE == 0 && IsttokOPSTATE == POS_Clean){
            strcpy(msg, "Entry on Manual Cleaning...");

            CLEANINGSTATE = CLNST_Manual;
            pvPut(CLEANINGSTATE);
        } state CleaningManual

        when(delay(1.0) && CLEANING_AUTOMODE == 1 && IsttokOPSTATE == POS_Clean){
            strcpy(msg, "Initiating Auto Cleaning...");
            pvPut(msg);
            strcpy(msg, "GIS Valves OPEN");
            pvPut(msg);
            errlogSevPrintf(NO_ALARM, "%s\n",msg);
            IsttokGasIS_Valves = SEEED_RELAY_ON;
            pvPut(IsttokGasIS_Valves);
            IsttokClean_PrimPneuBreaker = SEEED_RELAY_ON;
            pvPut(IsttokClean_PrimPneuBreaker);

            CLEANINGSTATE = CLNST_Cleaning1;
            pvPut(CLEANINGSTATE);
        } state Cleaning1
    }
    state CleaningManual {
        when (IsttokProcReq==STOP) {
        } state NonCleaning
    }
    state Cleaning1 {
        when (delay(1.0) && IsttokProcReq==STOP) {
            // Reversing Cleaning
            strcpy(msg, "GIS Valves CLOSE");
            pvPut(msg);
            errlogSevPrintf(NO_ALARM, "%s\n",msg);
            IsttokClean_PrimPneuBreaker = SEEED_RELAY_OFF;
            pvPut(IsttokClean_PrimPneuBreaker);
            IsttokGasIS_Valves = SEEED_RELAY_OFF;
            pvPut(IsttokGasIS_Valves);
        } state NonCleaning

        when (delay(2.0) && CLEANING_AUTOMODE == 1 && IsttokOPSTATE == POS_Clean ) {
            strcpy(msg, "Filament ON");
            pvPut(msg);
            errlogSevPrintf(NO_ALARM, "%s\n",msg);
            IsttokVVessel_Filament = PCF_RELAY_ON;
            pvPut(IsttokVVessel_Filament);
            CLEANINGSTATE = CLNST_Cleaning2;
            pvPut(CLEANINGSTATE);
        } state Cleaning2
    }
    state Cleaning2 {
        when (delay(1.0) && IsttokProcReq==STOP) {
            // Reversing Cleaning
            strcpy(msg, "Filament OFF");
            pvPut(msg);
            errlogSevPrintf(NO_ALARM, "%s\n",msg);
            IsttokVVessel_Filament = PCF_RELAY_OFF;
            pvPut(IsttokVVessel_Filament);
            CLEANINGSTATE = CLNST_Cleaning1;
            pvPut(CLEANINGSTATE);
        } state Cleaning1
        when (delay(2.0) && CLEANING_AUTOMODE == 1 && IsttokOPSTATE == POS_Clean) {
            strcpy(msg, "Primary-CT ON");
            pvPut(msg);
            errlogSevPrintf(NO_ALARM, "%s\n",msg);
            IsttokClean_PrimContactor = SEEED_RELAY_ON;
            pvPut(IsttokClean_PrimContactor);
            CLEANINGSTATE = CLNST_Cleaning3;
            pvPut(CLEANINGSTATE);
        } state Cleaning3
    }
    state Cleaning3 {
        when(delay(1.0) && IsttokProcReq==STOP){
            strcpy(msg, "Primary-CT OFF");
            pvPut(msg);
            errlogSevPrintf(NO_ALARM, "%s\n",msg);
            IsttokClean_PrimContactor = SEEED_RELAY_OFF;
            pvPut(IsttokClean_PrimContactor);
            CLEANINGSTATE = CLNST_Cleaning2;
            pvPut(CLEANINGSTATE);
        } state Cleaning2
        when(delay(2.0) && CLEANING_AUTOMODE == 1 && IsttokOPSTATE == POS_Clean){
            strcpy(msg, "Toroidal-BK CLOSE");
            pvPut(msg);
            errlogSevPrintf(NO_ALARM, "%s\n",msg);
            IsttokClean_TorPneuBreaker = SEEED_RELAY_ON;
            pvPut(IsttokClean_TorPneuBreaker);
            CLEANINGSTATE = CLNST_Cleaning4;
            pvPut(CLEANINGSTATE);
        } state Cleaning4

    }
    state Cleaning4 {
        when(delay (1.0) && IsttokProcReq==STOP){
            strcpy(msg, "Toroidal-BK OPEN");
            pvPut(msg);
            errlogSevPrintf(NO_ALARM, "%s\n",msg);
            IsttokClean_TorPneuBreaker = SEEED_RELAY_OFF;
            pvPut(IsttokClean_TorPneuBreaker);
            CLEANINGSTATE = CLNST_Cleaning3;
            pvPut(CLEANINGSTATE);
        } state Cleaning3
        when (delay(2.0) && CLEANING_AUTOMODE == 1 && IsttokOPSTATE == POS_Clean){
            strcpy(msg, "Toroidal-CT ON");
            pvPut(msg);
            errlogSevPrintf(NO_ALARM, "%s\n",msg);
            IsttokClean_TorContactor = SEEED_RELAY_ON;
            pvPut(IsttokClean_TorContactor);
            CLEANINGSTATE = CLNST_Cleaning5;
            pvPut(CLEANINGSTATE);
        } state Cleaning5

    }
    state Cleaning5 {
        when (IsttokProcReq==STOP) {
//        } state Idle
 //       when(delay(1.0) && IsttokOPSTATE != POS_Clean){
            strcpy(msg, "Toroidal-CT OFF");
            pvPut(msg);
            errlogSevPrintf(NO_ALARM, "%s\n",msg);
            IsttokClean_TorContactor = SEEED_RELAY_OFF;
            pvPut(IsttokClean_TorContactor);
/* Close GIS to let Pressure down */
            IsttokGasIS_Valves = SEEED_RELAY_OFF;
            pvPut(IsttokGasIS_Valves);
            CLEANINGSTATE = CLNST_Cleaning4;
            pvPut(CLEANINGSTATE);
        } state Cleaning4

        when (pvSeverity(IsttokVVesselTemperature) >= pvSevrMAJOR) {
            // Temperature Limit Pulse state change to Idle and close clean PVs
            IsttokProcReq = STOP;
            pvPut(IsttokProcReq);
            strcpy(msg, "Temperature Limit-> State to Idle");
            pvPut(msg);
            errlogSevPrintf(MINOR, "%s\n",msg);
        } state Cleaning5
    }
}

/*********** SUB-State: ShotSequence Set ************/
ss ShotSequence {
    state NonShoting {
    entry {
            WSHOTINGSTATE = WSHTST_NonShoting;
            pvPut(WSHOTINGSTATE);
            IsttokWaitTimerDisa = 1;
            pvPut(IsttokWaitTimerDisa);
//            strcpy(IsttokWaitTimerScan, "Passive");
//            pvPut(IsttokWaitTimerScan);
    }
        when( IsttokOPSTATE == POS_WaitShot ){
            //strcpy(msg, "Waiting for Trigger...");
           // pvPut(msg);
            //errlogSevPrintf(NO_ALARM, "%s\n",msg);
            IsttokWaitTimer = 0;
            pvPut(IsttokWaitTimer);
            // Enable Counting
            IsttokWaitTimerDisa = 0;
            pvPut(IsttokWaitTimerDisa);
//            strcpy(IsttokWaitTimerScan, "1 second");
//            pvPut(IsttokWaitTimerScan);
            WSHOTINGSTATE = WSHTST_ShotEnd;
            pvPut(WSHOTINGSTATE);
            //WSHOTINGSTATE = WSHTST_WaitTrg;
            //pvPut(WSHOTINGSTATE);
        } state ShotEnd
    }
    state ShotWaitTrg {
        when ((IsttokWaitTimer > 1800) || (IsttokOPSTATE != POS_WaitShot)) {
            strcpy(msg, "Shooting Out");
            pvPut(msg);
            errlogSevPrintf(NO_ALARM, "%s\n",msg);
            IsttokProcReq = STOP;
            pvPut(IsttokProcReq);
            WSHOTINGSTATE = WSHTST_NonShoting;
            pvPut(WSHOTINGSTATE);
        } state NonShoting

        when( IsttokTTSystem_tzero == 0 ){
            strcpy(msg, "Trigger received...");
            pvPut(msg);
            IsttokElco_Enable= PCF_RELAY_ON;
            pvPut(IsttokElco_Enable);

            IsttokWaitTimer = -70;
            pvPut(IsttokWaitTimer);
            WSHOTINGSTATE = WSHTST_Shooting2;
            pvPut(WSHOTINGSTATE);
            PulseNumber +=1;
            pvPut(PulseNumber);
        } state Shooting2
    }
    state Shooting2 {
        when (IsttokOPSTATE != POS_WaitShot) {
            strcpy(msg, "Shooting 2 Out");
            pvPut(msg);
            errlogSevPrintf(NO_ALARM, "%s\n",msg);
        } state ShotWaitTrg
        when ((IsttokOPSTATE == POS_WaitShot) && (IsttokWaitTimer == -61)) {
            strcpy(msg, "Buzzer 1-Sound");
            pvPut(msg);
            errlogSevPrintf(NO_ALARM, "%s\n",msg);
            IsttokBuzzer = SEEED_RELAY_PULSE;
            pvPut(IsttokBuzzer);
            strcpy(msg, "CAPBANK Crowbar Open");
            pvPut(msg);
            errlogSevPrintf(NO_ALARM, "%s\n",msg);
            IsttokCapBank_Discharge = SEEED_RELAY_ON;
            pvPut(IsttokCapBank_Discharge);
            strcpy(msg, "Lights ON");
            pvPut(msg);
            errlogSevPrintf(NO_ALARM, "%s\n",msg);
            IsttokLab_WarningLight = SEEED_RELAY_ON;
            pvPut(IsttokLab_WarningLight);
            WSHOTINGSTATE = WSHTST_Shooting3;
            pvPut(WSHOTINGSTATE);
        } state Shooting3
    }
    state Shooting3 {
        when (IsttokOPSTATE != POS_WaitShot) {
            strcpy(msg, "Shooting3 Out");
            pvPut(msg);
            errlogSevPrintf(NO_ALARM, "%s\n",msg);
        } state ShotWaitTrg
        when (IsttokWaitTimer == -60) {
            strcpy(msg, "GIS Valves OPEN");
            pvPut(msg);
            errlogSevPrintf(NO_ALARM, "%s\n",msg);
            IsttokGasIS_Valves = SEEED_RELAY_ON;
            pvPut(IsttokGasIS_Valves);
            WSHOTINGSTATE = WSHTST_Shooting4;
            pvPut(WSHOTINGSTATE);
        } state Shooting4
    }
    state Shooting4 {
        when (IsttokOPSTATE != POS_WaitShot ) {
            strcpy(msg, "Shooting4 Out");
            pvPut(msg);
            errlogSevPrintf(NO_ALARM, "%s\n",msg);
            WSHOTINGSTATE = WSHTST_NonShoting;
            pvPut(WSHOTINGSTATE);
        } state NonShoting
        when (IsttokWaitTimer == -59) {
            WSHOTINGSTATE = WSHTST_Shooting5;
            pvPut(WSHOTINGSTATE);
        } state Shooting5
    }
    state Shooting5 {
        when (IsttokOPSTATE != POS_WaitShot) {
            strcpy(msg, "Shooting5 Out");
            pvPut(msg);
            errlogSevPrintf(NO_ALARM, "%s\n",msg);
            WSHOTINGSTATE = WSHTST_NonShoting;
            pvPut(WSHOTINGSTATE);
        } state NonShoting
        when (IsttokWaitTimer == -46) {
            strcpy(msg, "CAPBANK Charge ON" );
            pvPut(msg);
            errlogSevPrintf(NO_ALARM, "%s\n",msg);
            IsttokCapBank_Charge = SEEED_RELAY_ON;
            pvPut(IsttokCapBank_Charge);
            WSHOTINGSTATE = WSHTST_Shooting6;
            pvPut(WSHOTINGSTATE);
        } state Shooting6
    }
    state Shooting6 {
        when (IsttokOPSTATE != POS_WaitShot) {
            strcpy(msg, "Shooting6 Out");
            pvPut(msg);
            errlogSevPrintf(MINOR, "%s\n",msg);
            WSHOTINGSTATE = WSHTST_NonShoting;
            pvPut(WSHOTINGSTATE);
        } state NonShoting
        when (IsttokWaitTimer == -16) {
            strcpy(msg, "CAPBANK Charge OFF" );
            pvPut(msg);
            errlogSevPrintf(NO_ALARM, "%s\n", msg);
            IsttokCapBank_Charge = SEEED_RELAY_OFF;
            pvPut(IsttokCapBank_Charge);
            WSHOTINGSTATE = WSHTST_Shooting7;
            pvPut(WSHOTINGSTATE);
        } state Shooting7
    }
    state Shooting7 {
        when (IsttokOPSTATE != POS_WaitShot) {
            strcpy(msg, "Shooting7 Out");
            pvPut(msg);
            errlogSevPrintf(MINOR, "%s\n",msg);
            WSHOTINGSTATE = WSHTST_NonShoting;
            pvPut(WSHOTINGSTATE);
        } state NonShoting
        when (IsttokWaitTimer == -11) {
            strcpy(msg, "Buzzer 2-Sound" );
            pvPut(msg);
            errlogSevPrintf(NO_ALARM, "%s\n", msg);
            IsttokElco_Enable= PCF_RELAY_OFF;
            pvPut(IsttokElco_Enable);

            IsttokBuzzer = SEEED_RELAY_PULSE;
            pvPut(IsttokBuzzer);
            strcpy(msg, "Filament ON");
            pvPut(msg);
            errlogSevPrintf(NO_ALARM, "%s\n",msg);
            IsttokVVessel_Filament = PCF_RELAY_ON;
            pvPut(IsttokVVessel_Filament);
            WSHOTINGSTATE = WSHTST_Shooting8;
            pvPut(WSHOTINGSTATE);
       } state Shooting8
    }
    state Shooting8 {
        when (IsttokOPSTATE != POS_WaitShot) {
            strcpy(msg, "Shooting8 Out");
            pvPut(msg);
            errlogSevPrintf(MINOR, "%s\n",msg);
            WSHOTINGSTATE = WSHTST_NonShoting;
            pvPut(WSHOTINGSTATE);
        } state NonShoting
        when (IsttokWaitTimer == -4) {
            strcpy(msg, "Toroidal PS Trigger");
            pvPut(msg);
            errlogSevPrintf(NO_ALARM, "%s\n",msg);
//          PS Trigger Pulse
            Isttok_TorPSTrigger = ON;
            pvPut(Isttok_TorPSTrigger);
            WSHOTINGSTATE = WSHTST_SShot;
            pvPut(WSHOTINGSTATE);
       } state SShot
    }
    state SShot {
        when (IsttokOPSTATE != POS_WaitShot) {
            strcpy(msg, "SShot Out");
            pvPut(msg);
            errlogSevPrintf(MINOR, "%s\n",msg);
            WSHOTINGSTATE = WSHTST_NonShoting;
            pvPut(WSHOTINGSTATE);
        } state NonShoting
        when (IsttokWaitTimer == -2) {
            strcpy(msg, "Ready for SHOT" );
            pvPut(msg);
            errlogSevPrintf(NO_ALARM, "%s\n", msg);
            WSHOTINGSTATE = WSHTST_Shooting10;
            pvPut(WSHOTINGSTATE);
       } state Shooting10
    }
    /* Does nothing    */
    state Shooting10 {
        when (IsttokOPSTATE != POS_WaitShot) {
            strcpy(msg, "Shooting10 Out");
            pvPut(msg);
            errlogSevPrintf(MINOR, "%s\n",msg);
            WSHOTINGSTATE = WSHTST_NonShoting;
            pvPut(WSHOTINGSTATE);
        } state NonShoting
        when (IsttokWaitTimer == 1) {
            WSHOTINGSTATE = WSHTST_Shooting11;
            pvPut(WSHOTINGSTATE);
       } state Shooting11
    }
    state Shooting11 {
        when (IsttokOPSTATE != POS_WaitShot) {
            strcpy(msg, "Shooting11 Out");
            pvPut(msg);
            errlogSevPrintf(MINOR, "%s\n",msg);
            WSHOTINGSTATE = WSHTST_NonShoting;
            pvPut(WSHOTINGSTATE);
        } state NonShoting
        when (IsttokWaitTimer == 2) {
            strcpy(msg, "CAPBANK CrowBar Close" );
            pvPut(msg);
            errlogSevPrintf(NO_ALARM, "%s\n", msg);
            IsttokCapBank_Discharge = SEEED_RELAY_OFF;
            pvPut(IsttokCapBank_Discharge);
            strcpy(msg, "GIS Valves CLOSE" );
            pvPut(msg);
            errlogSevPrintf(NO_ALARM, "%s\n", msg);
            IsttokGasIS_Valves = SEEED_RELAY_OFF;
            pvPut(IsttokGasIS_Valves);
            strcpy(msg, "Filament OFF" );
            pvPut(msg);
            errlogSevPrintf(NO_ALARM, "%s\n", msg);
            IsttokVVessel_Filament = 1;
            pvPut(IsttokVVessel_Filament);
            WSHOTINGSTATE = WSHTST_Shooting12;
            pvPut(WSHOTINGSTATE);
        } state Shooting12
    }
    state Shooting12 {
        when (IsttokOPSTATE != POS_WaitShot) {
            strcpy(msg, "Shooting6 Out");
            pvPut(msg);
            errlogSevPrintf(MINOR, "%s\n",msg);
            WSHOTINGSTATE = WSHTST_NonShoting;
            pvPut(WSHOTINGSTATE);
        } state NonShoting
        when (IsttokWaitTimer == 4) {
            strcpy(msg, "Lights OFF and Recovering..." );
            pvPut(msg);
            errlogSevPrintf(NO_ALARM, "%s\n", msg);
            IsttokLab_WarningLight = SEEED_RELAY_OFF;
            pvPut(IsttokLab_WarningLight);
            WSHOTINGSTATE = WSHTST_ShotEnd;
            pvPut(WSHOTINGSTATE);
        } state ShotEnd
    }
    state ShotEnd {
        when (IsttokWaitTimer > 160 || IsttokOPSTATE != POS_WaitShot) {
            strcpy(msg, "Terminated");
            pvPut(msg);
            errlogSevPrintf(NO_ALARM, "%s\n", msg);
            WSHOTINGSTATE = WSHTST_WaitTrg;
            pvPut(WSHOTINGSTATE);
        } state ShotWaitTrg
    }
}

/*********** SUB-State: StoppingSequence Set ************/
ss StoppingSequence {
    state NonStopping {
    entry {
            STOPPINGSTATE = STPST_NonStopping;
            pvPut(STOPPINGSTATE);
        }
        when (IsttokOPSTATE == POS_Stopping) {
            strcpy(msg, "Stopping Mode: Stopping1");
            pvPut(msg);
            errlogSevPrintf(NO_ALARM, "%s\n",msg);

            IsttokClean_PrimPneuBreaker = SEEED_RELAY_OFF;
            pvPut(IsttokClean_PrimPneuBreaker);

            IsttokTMPump1_MotorOff = ON;
            pvPut(IsttokTMPump1_MotorOff);

            IsttokClean_TorPneuBreaker = SEEED_RELAY_OFF;
            pvPut(IsttokClean_TorPneuBreaker);

 //     IsttokTMPump2_Motor = PCF_RELAY_OFF;
 //           pvPut(IsttokTMPump2_Motor);
            STOPPINGSTATE = STPST_Stopping1;
            pvPut(STOPPINGSTATE);
        } state Stopping1
    }
   state Stopping1 {
        when (IsttokOPSTATE != POS_Stopping ) { } state NonStopping
        when (TMPump1NormalOperation == PCF_INPUT_OFF) {   // TODO insert TMP2 Test
            strcpy(msg, "TMP1 Stopping");
            pvPut(msg);
            errlogSevPrintf(NO_ALARM, "%s\n",msg);
            STOPPINGSTATE = STPST_Stopping2;
            pvPut(STOPPINGSTATE);
    } state Stopping2
   }
   state Stopping2 {
      when (IsttokOPSTATE != POS_Stopping ) { } state NonStopping
      when (delay(5.0) && (IsttokTMPump1_ManualValve == OFF)) {
            strcpy(msg, "Manual Valve Closed Checked");
            pvPut(msg);
            errlogSevPrintf(NO_ALARM, "%s\n",msg);
//            IsttokRPump2_Motor = PCF_RELAY_ON;
//            pvPut(IsttokRPump2_Motor);
            IsttokRPump1_Valve = PCF_RELAY_ON; // "NO" relay
            pvPut(IsttokRPump1_Valve);
            strcpy(msg, "Rotary Valve 1  CLOSE");
            pvPut(msg);
            errlogSevPrintf(NO_ALARM, "%s\n",msg);
            STOPPINGSTATE = STPST_Stopping3;
            pvPut(STOPPINGSTATE);
        } state Stopping3
    }
   state Stopping3 {
        when (IsttokOPSTATE != POS_Stopping ) { } state NonStopping
        when (delay(5)) {
 //           IsttokRPump2_Valve = PCF_RELAY_ON;
 //           pvPut(IsttokRPump2_Valve);
            IsttokRPump1_Motor = PCF_RELAY_ON;   // "NO" Relay
            pvPut(IsttokRPump1_Motor);
            strcpy(msg, "Rotary Pump 1  OFF");
            pvPut(msg);
            errlogSevPrintf(NO_ALARM, "%s\n",msg);
            STOPPINGSTATE = STPST_StoppingEnd;
            pvPut(STOPPINGSTATE);
        } state StoppingEnd
   }
   state StoppingEnd {
        when (IsttokOPSTATE != POS_Stopping ) { } state NonStopping
   }
}
/* Exit procedure - close the log file 
exit {
  printf("close fd=%d\n", fd);
  if ((fd > 0) && (fd != ioGlobalStdGet(1)) )
    close(fd);
  fd = -1;
}
*/

/**** END PROGRAM ***/

/*        when (IsttokWaitTimer > 159) {
            strcpy(msg, "Toroidal Field Recovered" );
            pvPut(msg);
            errlogSevPrintf(NO_ALARM, "%s\n", msg);
            WSHOTINGSTATE = WSHTST_Shooting12;
            pvPut(WSHOTINGSTATE);
        } state Shooting12
    state Shooting12 {
        when ((IsttokOPSTATE != POS_WaitShot ) && (IsttokWaitTimer == 160) ) {
            strcpy(msg, "Shooting12 Out");
            pvPut(msg);
            errlogSevPrintf(NO_ALARM, "%s\n", msg);
            WSHOTINGSTATE = WSHTST_NonShoting;
            pvPut(WSHOTINGSTATE);
        } state NonShoting
        when (IsttokWaitTimer >= 160) {
            strcpy(msg, "Terminated");
            pvPut(msg);
            errlogSevPrintf(NO_ALARM, "%s\n", msg);
//            strcpy(IsttokWaitTimerScan, "Passive");
//            pvPut(IsttokWaitTimerScan);
//            IsttokWaitTimer = 0;
//            pvPut(IsttokWaitTimer);
        } state ShotWaitTrg
    }



    entry{
            IsttokRPump1_Motor = PCF_RELAY_ON;
            pvPut(IsttokRPump1_Motor);
            strcpy(msg, "Rotary Pumps 1/2 OFF");
            pvPut(msg);
            errlogSevPrintf(NO_ALARM, "%s\n",msg);
            IsttokRPump2_Motor = PCF_RELAY_ON;
            pvPut(IsttokRPump2_Motor);
            //IsttokLastOPSTATE = POS_Idle;
            //pvPut(IsttokLastOPSTATE);
    }
//        } state Stopping

*/

// vim: set filetype=c ts=4 sw=4 sts=4 sr et :
// Use: SyntasticToggleMode
