/*
 * file:                timeout.stt
 * purpose:             45 DEG Fast Wire Scanner timeout state machine
 * created:             19-Dec-2013
 * property of:         Stanford Linear Accelerator Center
 *
 * revision history:
 *   28-Feb-2024        Namrata Balakrishnan   Remove Linmot from message text
 *   22-Nov-2017        Ziga Oven              Updated the timeout routine with messages and modified
 *                                             behaviour on motion complete or when timeout disabled
 *   15-Nov-2017        Ziga Oven              Rewritten initialize routine with debugging messages
 *   21-May-2016        Kristi Luchini         during initialize, check for motor error and clear 
 *                                             before turning on motor.
 *   28-May-2014        Mitch D'Ewart          initial version
 */

/*
ex motor=WIRE:LTU1:776
*/

program timeout

option +c;  /* All connections must be made before execution begins */
option +d;  /* Turn on runtime debug messages */
option +r;  /* All declarations private to this sequence */
option -a;  /* All pvGets must be synchronous */

/* Including time for timestamping the output */
%%#include <time.h>
/* Method for output the message with event timestamp */
%%static void outputMessage(char* deviceName, char* message);

/* PVs from motor record */
double val;             assign val to "{motor}:MOTR.VAL";           
double rbv;             assign rbv to "{motor}:MOTR.RBV";                  monitor rbv;
short doneMoving;       assign doneMoving to "{motor}:MOTR.DMOV";          monitor doneMoving;
short closedLoop;       assign closedLoop to "{motor}:MOTR.CNEN";   monitor closedLoop;
short closedLoopCmd;    assign closedLoopCmd to "{motor}:MOTR.CNEN";       monitor closedLoopCmd;
short lLimit;           assign lLimit to "{motor}:MOTR.LLS";               monitor lLimit;

double retractPos;      assign retractPos to "{motor}:MOTR_RETRACT_POS";   monitor retractPos;   /* Motor retract position = 2000um  */
short timeoutEn;        assign timeoutEn to "{motor}:MOTR_TIMEOUTEN";      monitor timeoutEn;    /* Enable automatic timeout */
double timeOut;         assign timeOut to "{motor}:MOTR_TIMEOUT";          monitor timeOut;      /* Timeout to turn motor off in seconds */
double longTimeOut;     assign longTimeOut to "{motor}:MOTR_LTIMEOUT";     monitor longTimeOut;  /* Timeout to turn motor off in seconds */
short retract;          assign retract to "{motor}:MOTR_RETRACT";          monitor retract;      /* Used to retract motor and turn off closed loop */
short DEBUG_T;          assign DEBUG_T to "{motor}:MOTR_TIMEOUT_DBG";      monitor DEBUG_T;      /* Timeout sequence debug flag */


/* Internal variables */
char* deviceName;

ss timeoutSeq {
  state init {
    entry {
      if (DEBUG_T) outputMessage(deviceName, "Entering the Timeout sequence program");
/*      retract = 0;
      pvPut(retract, SYNC);*/
      deviceName = macValueGet("motor");
    } 
   
    when() {
      /* Always go to wait state*/
    } state wait
    
  }

  state disabled {
    entry {
      if (DEBUG_T) outputMessage(deviceName, "Entering the disable state");
    }
    
    when(timeoutEn) {
      /* If timeouts get enabled go to wait state */
      outputMessage(deviceName, "Automatic timeout enabled");
    } state wait
    
    /* If retract triggered move motor to home position and then go to wait state */
    when(retract == 1) {
      outputMessage(deviceName, "FWS retract triggered");
      val = retractPos;
      pvPut(val);
    } state wait
  }

  state wait {
    entry {
      if (DEBUG_T) outputMessage(deviceName, "Entering the wait state");
      pvGet(closedLoop, SYNC);
    }
    
    when((!timeoutEn) && (retract == 0)) {
      /* If timeout disabled on init go to disable state */
      outputMessage(deviceName, "Disabling automatic timeout");
    } state disabled

    when(closedLoop == 0) {
      /* If motor record is in open loop go to turnOff state */
    } state turnOff

    when((rbv <= (retractPos+50 ) || lLimit == 1) && doneMoving == 1) {
      /* If FWS is at home position or at lower limit switch and motor is stationary go to retracted state */
    } state retracted

    when((doneMoving == 1) && (retract == 0)) {
      /* If motor is stationary go to longTimeOut state */
    } state longTimeOut 
  }

  state retracted {
    entry {
      if (DEBUG_T) outputMessage(deviceName, "Entering the retracted state");
    }
    
    when(doneMoving == 0) {
      /* If motion is started go to wait state */
    } state wait

    when(delay(0.5) && closedLoop == 0) {
      if (DEBUG_T) outputMessage(deviceName, "motor record is in open loop go to turnOff state");
      /* If motor record is in open loop go to turnOff state */
    } state turnOff

    when(retract ==  1) {
      if (DEBUG_T) outputMessage(deviceName, "motor retract on");
      /* If motor retract on and motor has retracted and is in open loop go to turnOff state */
    } state turnOff
    
    when(delay(timeOut)) {
      if (DEBUG_T) outputMessage(deviceName, "Timeout period has finished");
      /* After time out periode turn go to turnOff state */
    } state turnOff 

    when((!timeoutEn) && (retract == 0)) {
      /* If timeout gets disabled go to disable state */
      outputMessage(deviceName, "Disabling automatic timeout");
    } state disabled
  }

  state longTimeOut {
    entry {
      if (DEBUG_T) outputMessage(deviceName, "Entering the longTimeOut state");
    }
    
    when(doneMoving == 0) {
      /* If motion started go to wait state */
    } state wait

    when(closedLoop == 0) {
      /* If motocted statr record is in open loop go to motorOff state */
    } state turnOff

    when(retract == 1) {
      /* Move motor into home position when triggerd with retract command */
      outputMessage(deviceName, "FWS retract triggered");
      val = retractPos;
      pvPut(val);
    } state wait
    
    when(delay(longTimeOut)) {
      /* Move motor into home position */
      outputMessage(deviceName, "Automatic FWS retract triggered after long timeout");
      retract = 1;
      pvPut(retract);
      val = retractPos;
      pvPut(val);
    } state wait
 
    when((!timeoutEn) && (retract == 0)) {
      /* If timeout gets disabled go to disable state */
      outputMessage(deviceName, "Disabling automatic timeout");
    } state disabled
 }

  state turnOff {
    entry {
      if (DEBUG_T) outputMessage(deviceName, "Entering the turnOff state");
    }
    
    when() {
      /* Set motor record in open loop */
      closedLoopCmd = 0;
      pvPut(closedLoopCmd, SYNC);
      /*Clear the retract flag*/
      retract = 0;
      pvPut(retract, SYNC);
      outputMessage(deviceName, "Linmot drive disabled, motor record in open loop, retract off");
    } state motorOff
  }

  /* Idle state, wait here for timeout to be disabled, or motor to be turned on */
  state motorOff {
    entry {
      if (DEBUG_T) outputMessage(deviceName, "Entering the motorOff state");
    }

    when(!timeoutEn) {
      /* If timeout gets disabled go to disable state */
      outputMessage(deviceName, "Disabling automatic timeout");
    } state disabled 
       
    when(delay(1) && retract == 1) {
    } state turnOff

    when(delay(0.5) && closedLoop == 1) {
     /* When motor record close loop is enabled go to wait state */
    } state wait
  }
}

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

%{

static void outputMessage(char* deviceName, char* message) {
  time_t t = time(NULL);
  struct tm tm = *localtime(&t);
  printf("%d-%.2d-%.2d %.2d:%.2d:%.2d: (Timeout) %s - %s\n", \
          tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec, \
          deviceName, message);
  return;
}

}%


/*
vim:filetype=cpp:
*/

