2.8 Stage Machine Concept


In above example, you can find "case 17:" does not doing time count, but checks state of timer and when state of timer is "IDLE", it changes state of timer "START" and turns on output then waiting state of timer changes to "FINISH". After state of timer changes to "FINISH", the "case 17" turns off output, sends plc.mCode.finish and changes state of timer backs to "IDLE".


StageMachine concept: StageMachine usually IDLE state, you must wake up before they begin execution, the implementation process will enter the FINISH. The awakener then ordered it to enter the IDLE state, releasing the StageMachine for use by other programs. The use of StageMachine writing PLC, the PLC clear, easy to maintain the advantages, it is recommended should be used.


We will write a sub function for many functions to call it, it can save program size and easy to handle.



It is same concept in PLC procedure, we write a StageMachine for PLC procedure to call it. The different of sub function and StageMachine is sub function is outside of function, but StageMachine is in plcRun function. A stage machine is wake up by caller and get in idle by caller, the other caller can not using this StageMachine when it is not in idle.


Example: The spindle will turn on by machine panel and M03, and these two procedures use same StageMachine to process spindle turns on.

void PLCAPI plcRun(struct Status & sts, struct PlcBlock &plc){

if(sts.state0.bManual){ //system in manual mode

 if(piM03 && m03StageMachine.flag == IDLE){

  m03StageMachine.flag = START;

 }

 if(m03StageMachine.flag == FINISH)

  m03StageMachine.flag = IDLE;

}

if(plc.mCode.flag){ //decode M code

 switch(plc.mCode.data){

  case 3: //M03

   if(m03StageMachine.flag == IDLE)

    m03StageMachine.flag = START;

   if(m03StageMachine.flag == FINISH){

    m03StageMachine.flag = IDLE;

    plc.mCode.finish = 1;

   }

   break;

 }

}

// stage machine of M03

if(m03StageMachine.flag != IDLE){

 switch(m03StageMachine.flag){

  case START:

   oM03 =1;

   m03StageMachine.flag = STEP_1;

   break;

  case STEP_1:

   if(iSpeedReach)

    m03StageMachine.flag = FINISH;

   break;

 }

}

}