/*
 * file:                LHUtrimtest.stt
 * purpose:             Motor move request and readback when a go command is initiated.
 * created:             10-Sep-2018
 * property of:         Stanford Linear Accelerator Center
 *
 * revision history:
 *   18-May-2021        Sarah Vo               Added correct state to make motor readback match LVDT readback
 *   08-Apr-2020        Namrata Balakrishnan   Make it generic  
 *   10-Sep-2018        Namrata Balakrishnan   initial version. 
 *-----------------------------------------------------------------
 * Inputs: 
 * motor: Motor PV prefix eg: USEG:HTR:646 
 * motordes: PV the holds the requested motor position eg: USEG:HTR:646:XDES
 * 
 * States : TRIM -> WAIT -> ENABLE -> START TO MOVE -> MOVING -> DISABLE -> CORRECT -> WAIT
 * TRIM : Initializes sequence and obtains motor Name.
 * WAIT : Waits for trim to be selected
 * ENABLE : Enable motor torque. This is such that we dont keep the motor on and cause noise.
 * START TO MOVE : Wait for enable to be done. Then trigger move command
 * MOVING : Wait for move to be complete.
 * DISABLE : Disable motor torque after completion of move.
 * CORRECT : Updates motor readback to match LVDT readback.
 *------------------------------------------------------------------
*/

program LHUtrimtest

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 record*/
short trimCmd;          assign trimCmd to "{motor}:TRIM";                 monitor trimCmd;      /* Trigger trim sequence */
short movedSts;         assign movedSts to "{motor}:MOTR.DMOV";           monitor movedSts;     /* Flag that gets set after motor done move */
short trimSts;          assign trimSts to "{motor}:TRIMSTS";              monitor trimSts;      /* Flag indicating the status of the trim sequence */
double moveDes;         assign moveDes to "{motordes}";                   monitor moveDes;      /* Pv storing desired motor position */
short correctCmd;       assign correctCmd to "{motor}:CORRECT";           monitor correctCmd;   /* Trigger correction sequence */
short correctSts;       assign correctSts to "{motor}:CORRECT_STS";       monitor correctSts;   /* Flag indicating the status of the correct seq */

double moveValCmd;      assign moveValCmd to "{motor}:MOTR.VAL";                                /* Command to move the motor */
short enableCmd;        assign enableCmd to "{motor}:MOTR.CNEN";                                /* Command to turn on torque */

char* deviceName;

short DEBUG_T;          assign DEBUG_T to "{motor}:TRIM_DBG";             monitor DEBUG_T;      /* Trim sequence debug flag */

/* Start trim sequence for motor*/
ss motorTrim {
  state trim {
    entry {
      if (DEBUG_T) outputMessage(deviceName, "Entering Trim sequence program");
    }

    when(){
      deviceName = macValueGet("motor");
    } state wait
  }       
  
  state wait {
    entry {
      if (DEBUG_T) outputMessage(deviceName, "Entering wait state");
    }

    when(trimCmd == 1){
      trimSts=0;
      pvPut(trimSts,SYNC);
      if (DEBUG_T) outputMessage(deviceName, "Trim to begin.");
    } state enable
  }
  
  state enable {
    entry {
      if (DEBUG_T) outputMessage(deviceName, "Entering enable state");
    }

    when(){
      enableCmd = 1;
      pvPut(enableCmd,SYNC);
      if (DEBUG_T) outputMessage(deviceName, "Motor enabled");
    } state startToMove
  }
  
  state startToMove {
    entry {
      if (DEBUG_T) outputMessage(deviceName, "Entering StartToMove state");
    }
    
    when(trimCmd == 0){
      if (DEBUG_T) outputMessage(deviceName, "Trim abort. Heading back to disable");
    } state disable
    
    when(delay(1) && (trimCmd == 1)){
      moveValCmd = moveDes;
      pvPut(moveValCmd,SYNC);
      if (DEBUG_T) outputMessage(deviceName, "Motor move requested");
    } state moving
  }
 
  state moving {
    entry {
      if (DEBUG_T) outputMessage(deviceName, "Entering moving state");
    }
    
    when(trimCmd == 0){
      if (DEBUG_T) outputMessage(deviceName, "Trim abort. Heading back to disable");
    } state disable
   
    when(delay(1) && (movedSts == 1)){
    } state disable
  }
  
  state disable {
    entry {
      if (DEBUG_T) outputMessage(deviceName, "Entering disable state");
    }

    when(){
      enableCmd = 0;
      pvPut(enableCmd,SYNC);
      trimCmd = 0;
      pvPut(trimCmd,SYNC);
      if (DEBUG_T) outputMessage(deviceName, "Motor disabled.");
    } state correct
  }

  state correct {
    entry {
      if (DEBUG_T) outputMessage(deviceName, "Entering correct state");
    }

    when(delay(5)){
      correctSts = 0;
      pvPut(correctSts,SYNC);
      correctCmd = 1;
      pvPut(correctCmd,SYNC);
      if (DEBUG_T) outputMessage(deviceName, "Motor readback corrected");
      correctSts = 1;
      pvPut(correctSts,SYNC);
      trimSts = 1;
      pvPut(trimSts,SYNC);
      if (DEBUG_T) outputMessage(deviceName, "Trim on standby.");
    } 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:(trimMotor) %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;
}

}%

