FUNCTION_BLOCK "FB_Lvl1AdditionalRouting"
{ S7_Optimized_Access := 'TRUE' }
VERSION : 0.1
   VAR_INPUT 
      udt_IN_MES : "UDT_Payload_CReqM";
      x_CarrierLoaded : Bool;
      xF2HEmptyCarrierNeeded : Bool;
      xAMREmptyCarrierNeeded : Bool;
   END_VAR

   VAR_IN_OUT 
      arCarrier : Array[*] of "stCarrier";
   END_VAR

   VAR 
      nSequence : Int;
      nBarcodeSave : DInt;
   END_VAR

   VAR_TEMP 
      nTempUpperBound : DInt;
      nTempLowerBound : DInt;
   END_VAR

   VAR CONSTANT 
      c000Wait : Int := 0;
      c002SaveBarcode : Int := 2;
      c010AnalyzeCarrier : Int := 10;
      c020SetDestination : Int := 20;
      c030WaitCarrierGone : Int := 30;
      c120AnalyzeEmptyNeeded : Int := 120;
   END_VAR


BEGIN
	
	REGION Info
	(* Company: Schönenberger Systeme GmbH / (C) Copyright (2022). All rights reserved.
	-----------------------------------------------------------------------------------------
	Title: FB_Lvl1AdditionalRouting
	Comment/Function: Desitnation for Carrier in lvl 1
	Library/Family: -
	Author: department: "Automatisierung" ; person in charge / contact: C.Nottelmann (Automated Intelligence)
	Target System: CPU 1518F-4 PN/DP
	Engineering: TIA Portal V20
	Restrictions: -
	Requirements: -
	-----------------------------------------------------------------------------------------
	Change log table:
	Version     | Date       | Signature (Safety) | Expert in charge | Changes applied
	------------|------------|--------------------|------------------|-----------------------
	001.000.000 | 2025-10-16 | -                  |                  | Header added on 16.10.2025
	                                                                   
	*)
	END_REGION
	
	REGION
	    
	    //Scan Result von UH05 erforderlich
	    
	    
	END_REGION
	
	REGION Check Array Size
	    
	    #nTempLowerBound := LOWER_BOUND(ARR := #arCarrier, DIM := 1);
	    #nTempUpperBound := UPPER_BOUND(ARR := #arCarrier, DIM := 1);
	    
	    
	END_REGION
	
	
	
	REGION Sequence
	    
	    CASE #nSequence OF
	        #c000Wait:
	            //Action
	            
	            
	            //Transition
	            IF #arCarrier[#nTempLowerBound].nBarcode >0
	                AND #arCarrier[#nTempLowerBound].nBarcode <> #nBarcodeSave
	            THEN
	                #nSequence := #c002SaveBarcode;
	            END_IF;
	            
	        #c002SaveBarcode:
	            //Action
	            #nBarcodeSave := #arCarrier[#nTempLowerBound].nBarcode;
	            
	            //Transition
	            #nSequence := #c010AnalyzeCarrier;
	            
	        #c010AnalyzeCarrier:
	            //Action
	            IF #x_CarrierLoaded AND #arCarrier[#nTempLowerBound].bStatus.%X0
	            THEN
	                #arCarrier[#nTempLowerBound].bStatus.%X1 := True;
	            END_IF;
	            
	            //Transition
	            IF #arCarrier[#nTempLowerBound].bStatus.%X1
	            THEN
	                #nSequence := #c020SetDestination;
	            ELSIF NOT #arCarrier[#nTempLowerBound].bStatus.%X1
	            THEN
	                #nSequence := #c120AnalyzeEmptyNeeded;
	            ELSIF NOT #arCarrier[#nTempLowerBound].bStatus.%X0
	            THEN
	                #nSequence := #c000Wait;
	            END_IF;
	            
	           //Wenn Loaded -> Destination von MES
	           //Wenn nicht Loaded -> Prüfen über Schnittstelle ob F2H oder AMR Carrier braucht
	           //Wenn nicht Ziel Jam1110_1
	           //
	           
	        #c020SetDestination:
	            //Action
	            IF #arCarrier[#nTempLowerBound].nBarcode = #udt_IN_MES.nBarcode
	            THEN
	                #arCarrier[#nTempLowerBound].nDestination := DINT_TO_INT( #udt_IN_MES.nDestination);
	            END_IF;
	            
	            //Transition
	            IF #arCarrier[#nTempLowerBound].nDestination > 0
	            THEN
	                #nSequence := #c030WaitCarrierGone;
	            ELSIF NOT #arCarrier[#nTempLowerBound].bStatus.%X0
	            THEN
	                #nSequence := #c000Wait;
	            END_IF;
	            
	        #c030WaitCarrierGone:
	            //Action
	            
	            
	            //Transition
	            IF NOT #arCarrier[#nTempLowerBound].bStatus.%X0
	                OR #arCarrier[#nTempLowerBound].nBarcode <> #nBarcodeSave
	            THEN
	                #nSequence := #c000Wait;
	            END_IF;
	            
	        #c120AnalyzeEmptyNeeded:
	            //Action
	            IF #xAMREmptyCarrierNeeded THEN
	                #arCarrier[#nTempLowerBound].bStatus.%X8 := True;
	            ELSIF #xF2HEmptyCarrierNeeded THEN
	                #arCarrier[#nTempLowerBound].bStatus.%X9 := True;
	            END_IF;
	            
	            //Transition
	            IF #arCarrier[#nTempLowerBound].bStatus.%X8 OR
	                #arCarrier[#nTempLowerBound].bStatus.%X9
	            THEN
	                #nSequence := #c030WaitCarrierGone;
	            ELSIF  NOT #arCarrier[#nTempLowerBound].bStatus.%X0
	                OR #arCarrier[#nTempLowerBound].nBarcode <> #nBarcodeSave
	            THEN
	                #nSequence := #c000Wait;
	            END_IF;
	            
	                
	                
	                
	            
	            
	            
	    END_CASE;
	    
	    
	    
	    
	END_REGION
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
END_FUNCTION_BLOCK

