%%%
  VERSION:1
  LANGUAGE:ENGLISH
%%%

MODULE PAL_COM(SYSMODULE)
  !######################################################
  !# PalletWare module PAL_COM				#
  !# This module holds the communication routines to 	#
  !# PLC and if used external MMI.			#
  !#							#
  !#							#
  !######################################################
  
  !######################################################
  !#							#
  !# 		G  L O B A L   D A T A	  		#
  !#							#
  !######################################################
  !Alias type for message number
  !Do NOT declare anything above this definition
  ALIAS num msgnum;
  
  CONST string pal_com_rev:="$Revision: 1.4 $";
  
  !Message codes for communication with external MMI
  CONST msgnum MSG_SEL_CONTEXT:=1;
  CONST msgnum MSG_SEL_REQ_OPT:=2;
  CONST msgnum MSG_SEL_ERR_OPT:=3;
  CONST msgnum MSG_SEL_PALCYC:=4;
  CONST msgnum MSG_EDIT_MORE:=5;
  CONST msgnum MSG_SEL_ACK_OPT:=6;
  CONST msgnum MSG_CHK_TOOL:=7;
  
  !Message codes for communication with PLC
  CONST msgnum MSG_REQ_BOXES:=10;
  CONST msgnum MSG_START_OUTF:=11;
  CONST msgnum MSG_INIT_PLC:=12;
  CONST msgnum MSG_CELL_CONFIG:=13;
  CONST msgnum MSG_REQ_EXT_PAL:=14;
  CONST msgnum MSG_RESET_PLC:=15;
  
  !######################################################
  !#							#
  !# 		L O C A L   D A T A	  		#
  !#							#
  !######################################################
  LOCAL CONST num timeout_mmi:=5;
  LOCAL CONST num timeout_plc:=5;
  
  !######################################################
  !#							#
  !# 		P U B L I C   R O U T I N E S  		#
  !#							#
  !######################################################
  
  !------------------------------------------------------
  !	name:	PalRequestBoxes
  !	descr:	Order boxes via the PLC.
  !		It uses NextInFeederInfo to get data of
  !		a certain cycle. It uses SendMsgToPlc in 
  !		order to communicate with the PLC.
  !		The predefined message code MSG_REQ_BOXES
  !		is used.		
  !	arg:	optional argument pick of type switch
  !		context of type num
  !		cycle number (ncycle) of type num	
  !------------------------------------------------------
  PROC PalRequestBoxes(
    \switch pick,
    num context,
    num ncycle)

    VAR num defeed;
    VAR num nextnbprod;
    VAR num nextprodtype;
    VAR bool ltol;
    VAR num reply;
    
    IF ncycle<>0 THEN
      PrevNbProd{context}:=CurNbProd{context};
      PrevOrient{context}:=CurOrient{context};
      NextInFeederInfo context,ncycle,defeed,nextnbprod,nextprodtype,ltol;
      CurNbProd{context}:=nextnbprod;
      IF ltol THEN
        CurOrient{context}:=1;
      ELSE
        CurOrient{context}:=0;
      ENDIF
    ELSE
      defeed:=context;
      CurNbProd{context}:=PrevNbProd{context};
      CurOrient{context}:=PrevOrient{context};
    ENDIF
    
    SendMsgToPlc MSG_REQ_BOXES
      \param1:=defeed
      \param2:=CurNbProd{context}
      \param3:=CurOrient{context};
  ERROR
    RAISE;
  ENDPROC


  !------------------------------------------------------
  !	name:	PalRequestExtLay
  !	descr:	Order an external layer.
  !		This function is invoked if the layer is
  !		of type EXTERNAL. It can invoke different 
  !		subroutines depending on layer number etc.
  !		The routine shall return TRUE if 
  !		everything is ok.
  !	arg:	context of type num
  !	ret:	status of type bool	
  !------------------------------------------------------
  FUNC bool PalRequestExtLay(
    num context)
    
    VAR num station;
    
    station:=GetDefPalStn(context);
    
    IF GetCurLayer(context)<=1 THEN
      !Request pallet from external equipment
      RETURN PalRequestExtPal(station);
      !Test has to be done later to check 
      !that the pallet is in position
    ELSE
      !Invoke another routine for other type of layer
    ENDIF
  ERROR
    RAISE;
  ENDFUNC

  !------------------------------------------------------
  !	name:	PalStartOutfeed
  !	descr:	Order outfeeder start via the PLC.
  !		It uses SendMsgToPlc in order to 
  !		communicate with the PLC. The predefined 
  !		message code MSG_START_OUTF is used.
  !	arg:	outfeeder station of type num 	
  !------------------------------------------------------
  PROC PalStartOutfeed(
    num station)
  
    SendMsgToPlc MSG_START_OUTF\param1:=station-(NB_FEEDER_STA+NB_STACK_STA);
  ERROR
    RAISE;
  ENDPROC
  
  !------------------------------------------------------
  !	name:	PalResetPlc
  !	descr:	Order reset of the PLC.
  !		It uses SendMsgToPlc in order to 
  !		communicate with the PLC. The predefined 
  !		message code MSG_RESET_PLC is used.
  !	arg:	none 	
  !------------------------------------------------------
  PROC PalResetPlc()
  
    SendMsgToPlc MSG_RESET_PLC;
  ERROR
    RAISE;
  ENDPROC
  
  !------------------------------------------------------
  !	name:	EventRestart
  !	descr:	The routine is executed at the 
  !		RESTART event
  !	arg:	none 	
  !------------------------------------------------------
  PROC EventRestart()
    VAR num dialoganswer;
    
    Set Do_Synch;
    
    IF GOutput(goRobotStatus)=STS_IN_PALCYC THEN
      TPShow TP_PROGRAM;
      !Find action when startup 
      dialoganswer:=PalGetErrChoice();
      TPShow TP_SCREENVIEWER;
      !Handle operators choice
      PalHandleErrAns dialoganswer;
    ENDIF
    !Connected in palsysrp:
    !ShRestartPal;
    IF GetCurLayer(GetCurContext())=1 THEN
      IF reinit_search THEN
        RedoFromStart GetCurContext();
        RAISE PAL_RESTART_PROG;
      ENDIF
    ENDIF
  ENDPROC

  
  !------------------------------------------------------
  !	name:	SendMsgToMmi
  !	descr:	Communicates with external MMI via
  !		digital input and output. It displays
  !		a message on a group output to be read
  !		by an external MMI.
  !		It performs a handshake procedure.
  !	arg:	message code of type num	
  !------------------------------------------------------
  PROC SendMsgToMmi(
    num messagecode)
    
    VAR bool timeout:=FALSE;
    
    WaitDI diMmiComAck,0\MaxTime:=timeout_mmi\TimeFlag:=timeout;
    SetGO goMsgToMmi,messagecode;
    SetDO doOkToSendMmiMsg,1;
    WaitDI diMmiComAck,1\MaxTime:=timeout_mmi\TimeFlag:=timeout;
    SetDO doOkToSendMmiMsg,0;
    IF timeout PalReportError ERR_MMI_COM \PAL_FATAL_ERROR;
  ERROR
    RAISE;
  ENDPROC 
  
  !------------------------------------------------------
  !	name:	ReadMsgFromMmi
  !	descr:	Communicates with external MMI via
  !		digital input and output. It reads a
  !		group input where a message is setup
  !		by an external MMI.
  !		It performs a handshake procedure.		
  !	arg:	answer of type INOUT num	
  !------------------------------------------------------ 
  PROC ReadMsgFromMmi(
    INOUT num answer)
    
    VAR bool timeout:=FALSE;
    
    WaitDI diMmiComAck,0\MaxTime:=timeout_mmi\TimeFlag:=timeout;
    SetDO doOkToReadMmiMsg,1;
    WaitDI diMmiComAck,1;
    answer:=giMsgFromMmi;
    SetDO doOkToReadMmiMsg,0;
    IF timeout PalReportError ERR_MMI_COM \PAL_FATAL_ERROR;
  ERROR
    RAISE;
  ENDPROC
     
  !------------------------------------------------------
  !	name:	SendMsgToPlc
  !	descr:	Communicates with a PLC via
  !		digital input and output. It displays
  !		a message on a group output to be read
  !		by the PLC.
  !		It performs a handshake procedure.
  !	arg:	message code of type num
  !		parameter 1 of type num, optional arg
  !		parameter 2 of type num, optional arg
  !		parameter 3 of type num, optional arg	
  !------------------------------------------------------
  PROC SendMsgToPlc(
    num messagecode,
    \num param1,
    \num param2,
    \num param3)
    
    VAR bool timeout:=FALSE;
    
    WaitDI diPlcComAck,0\MaxTime:=timeout_plc\TimeFlag:=timeout;
    SetGO goMsgToPlc,messagecode;
    IF Present(param1) THEN
      SetGO goParam1ToPlc, param1;
    ELSE
      SetGO goParam1ToPlc,0;
    ENDIF
    IF Present(param2) THEN
      SetGO goParam2ToPlc, param2;
    ELSE
      SetGO goParam2ToPlc,0;
    ENDIF
    IF Present(param3) THEN
      SetGO goParam3ToPlc, param3;
    ELSE
      SetGO goParam3ToPlc,0;
    ENDIF
    SetDO doOkToSendPlcMsg,1;
    WaitDI diPlcComAck,1\MaxTime:=timeout_plc\TimeFlag:=timeout;
    SetDO doOkToSendPlcMsg,0;
    IF timeout PalReportError ERR_PLC_COM \PAL_FATAL_ERROR;
  ERROR
    RAISE;
  ENDPROC 
  
  !------------------------------------------------------
  !	name:	ReadMsgFromPlc
  !	descr:	Communicates with a PLC via
  !		digital input and output. It reads a
  !		group input where a message is setup
  !		by the PLC.
  !		It performs a handshake procedure.		
  !	arg:	answer of type INOUT num	
  !------------------------------------------------------ 
  
  
  PROC ReadMsgFromPlc(
    INOUT num answer)
    
    VAR bool timeout:=FALSE;
    
    WaitDI diPlcComAck,0\MaxTime:=timeout_plc\TimeFlag:=timeout;
    SetDO doOkToReadPlcMsg,1;
    WaitDI diPlcComAck,1;
    answer:=giMsgFromPlc;
    SetDO doOkToReadPlcMsg,0;
    IF timeout PalReportError ERR_PLC_COM \PAL_FATAL_ERROR;
  ERROR
    RAISE;
  ENDPROC

  FUNC bool PalFeederReady(
    num station)
    
    RETURN BitCheck(giFeederReady,station);
  ERROR
    RAISE;
  ENDFUNC
  
  FUNC bool PalletApproach(
    num station)
   
    RETURN BitCheck(giPalStatAppr,station-(NB_FEEDER_STA+NB_STACK_STA));
 ERROR
   RAISE;
 ENDFUNC
 
 FUNC bool PalFeederSaturat(
   num station)
   
   RETURN BitCheck(giFeederSaturat,station);
 ENDFUNC
  
 FUNC bool PalletPlace(
   num station)
   
   RETURN BitCheck(giPalStatReady,station-(NB_FEEDER_STA+NB_STACK_STA));
 ERROR
   RAISE;
 ENDFUNC
  !######################################################
  !#							#
  !# 		L O C A L    R O U T I N E S  		#
  !#							#
  !######################################################
  
  !------------------------------------------------------
  !	name:	PalRequestExtPal
  !	descr:	Order an external pallet via the PLC.
  !		It uses SendMsgToPlc in order to 
  !		communicate with the PLC.
  !		The predefined message code 
  !		MSG_REQ_EXT_PAL is used.		
  !	arg:	station of type num
  !	ret:	status of type bool	
  !------------------------------------------------------
  LOCAL FUNC bool PalRequestExtPal(
    num station)
    
    !Request pallet from external equipment
    SendMsgToPlc MSG_REQ_EXT_PAL
      \param1:=station-(NB_FEEDER_STA+NB_STACK_STA);
    !Return TRUE if all ok
    RETURN TRUE;
  ERROR
    RAISE;
  ENDFUNC
  
ENDMODULE
