{
    S7_Author := "ABH";
    S7_BlockNumber := "15184";
    S7_BlockTitle := "MLC_3iB";
    S7_Family := "ZeusControl";
    S7_Optimized := "TRUE";
    S7_PreferredLanguage := "LAD";
    S7_Version := "1.0"
}
FUNCTION_BLOCK "FB15184_ZC_FERM_FIN_PREP"
    VAR_INPUT
        { S7_MLC := "MLC_4B4" }
        NombreSecuencia : String := 'HOMOGENIZAR';
        { S7_MLC := "MLC_4du" }
        Pulso1Seg : Bool;
        { S7_MLC := "MLC_3JS" }
        Ack : Bool;
        { S7_MLC := "MLC_4Jb" }
        OrdenMarcha : Bool;
        { S7_MLC := "MLC_55u" }
        EstadoEnMarcha : Bool;
        { S7_MLC := "MLC_3dF" }
        EnEtapa : Bool;
        { S7_MLC := "MLC_4DW" }
        OrdenPausa : Bool;
        EstadoBocaHombre : Bool;
        { S7_MLC := "MLC_3yL" }
        TT_Tanque : Real;
        { S7_MLC := "MLC_3dt" }
        LT_Tanque : Real;
        Parametros : _.UDT_ZC_RECETA_BATCH_ETAPA := (
            { S7_MLC := "MLC_ZK" }
            TipoEtapa := (),
            { S7_MLC := "MLC_3N9" }
            P_Real_1 := (),
            { S7_MLC := "MLC_rL" }
            P_Real_2 := (),
            { S7_MLC := "MLC_3LR" }
            P_Real_3 := (),
            { S7_MLC := "MLC_38U" }
            P_Real_4 := (),
            { S7_MLC := "MLC_3nE" }
            P_Real_5 := (),
            { S7_MLC := "MLC_4jc" }
            P_Real_6 := (),
            { S7_MLC := "MLC_3b2" }
            P_Real_7 := (),
            { S7_MLC := "MLC_T2" }
            P_Real_8 := (),
            { S7_MLC := "MLC_56f" }
            P_Real_9 := (),
            { S7_MLC := "MLC_3cf" }
            P_Real_10 := (),
            { S7_MLC := "MLC_is" }
            P_Real_11 := (),
            { S7_MLC := "MLC_4L2" }
            P_Real_12 := (),
            { S7_MLC := "MLC_3qj" }
            P_Real_13 := (),
            { S7_MLC := "MLC_3uM" }
            P_Real_14 := (),
            { S7_MLC := "MLC_nA" }
            P_Real_15 := (),
            { S7_MLC := "MLC_46R" }
            P_Real_16 := (),
            { S7_MLC := "MLC_53V" }
            P_Real_17 := (),
            { S7_MLC := "MLC_4dP" }
            P_Int_1 := (),
            { S7_MLC := "MLC_dK" }
            P_Int_2 := (),
            { S7_MLC := "MLC_4ra" }
            P_Int_3 := (),
            { S7_MLC := "MLC_3We" }
            P_Int_4 := (),
            { S7_MLC := "MLC_W3" }
            P_Int_5 := (),
            { S7_MLC := "MLC_37d" }
            P_Int_6 := (),
            { S7_MLC := "MLC_32n" }
            P_Int_7 := (),
            { S7_MLC := "MLC_yg" }
            P_Int_8 := (),
            { S7_MLC := "MLC_42x" }
            P_Int_9 := (),
            { S7_MLC := "MLC_5CD" }
            P_Int_10 := (),
            { S7_MLC := "MLC_3h9" }
            P_Int_11 := (),
            { S7_MLC := "MLC_3Gn" }
            P_Int_12 := (),
            { S7_MLC := "MLC_3ZK" }
            P_Int_13 := (),
            { S7_MLC := "MLC_Yn" }
            P_Int_14 := (),
            { S7_MLC := "MLC_3J3" }
            P_Int_15 := (),
            { S7_MLC := "MLC_49q" }
            P_String_1 := (),
            { S7_MLC := "MLC_42y" }
            P_String_2 := (),
            { S7_MLC := "MLC_4ZF" }
            P_String_3 := (),
            { S7_MLC := "MLC_3AM" }
            P_String_4 := (),
            { S7_MLC := "MLC_48a" }
            P_String_5 := (),
            { S7_MLC := "MLC_4kJ" }
            P_String_6 := (),
            { S7_MLC := "MLC_Pb" }
            P_String_7 := (),
            { S7_MLC := "MLC_3wX" }
            P_String_8 := (),
            { S7_MLC := "MLC_N6" }
            P_String_9 := (),
            { S7_MLC := "MLC_3fH" }
            P_String_10 := ()
        );
    END_VAR
    VAR_OUTPUT
        MarchaAgitador : Bool;
        VelocidadAgitador : Real;
        { S7_MLC := "MLC_3FM" }
        OrdenCalentar : Bool;
        { S7_MLC := "MLC_3dP" }
        OrdenEnfriar : Bool;
        { S7_MLC := "MLC_4qy" }
        SP_CalEnf : Real;
        VisibilidadCalEnf : Bool;
        { S7_MLC := "MLC_ie" }
        SecuenciaIniciada : Bool;
        { S7_MLC := "MLC_iG" }
        FinSecuencia : Bool;
    END_VAR
    VAR RETAIN
        { S7_MLC := "MLC_4jN" }
        s_EtapaActual : Int;
        { S7_MLC := "MLC_3BG" }
        s_TextoEtapa : String[100];
        s_Pausa_M : Bool;
        s_Pausa_V : Bool;
        s_Continuar : Bool;
    END_VAR
    VAR
        {
            S7_Access := "ReadOnly := External";
            S7_Visibility := "Hidden := External"
        }
        s_Pulsos : Array[0..15] of Bool := [
            { S7_MLC := "MLC_4uQ" }
        (),
            { S7_MLC := "MLC_4w8" }
        (),
            { S7_MLC := "MLC_34H" }
        (),
            { S7_MLC := "MLC_3Bb" }
        (),
            { S7_MLC := "MLC_mm" }
        (),
            { S7_MLC := "MLC_MW" }
        (),
            { S7_MLC := "MLC_3ry" }
        (),
            { S7_MLC := "MLC_3Vb" }
        (),
            { S7_MLC := "MLC_35A" }
        (),
            6(()),
            { S7_MLC := "MLC_3s9" }
        ()
        ];
    END_VAR
    VAR RETAIN
        { S7_MLC := "MLC_ae" }
        s_TiempoEtapa : STRUCT
            { S7_MLC := "MLC_nk" }
            Activar : Bool;
            { S7_MLC := "MLC_ip" }
            Reset : Bool;
            { S7_MLC := "MLC_3fp" }
            Actual : Int;
            { S7_MLC := "MLC_599" }
            SP : Int;
            { S7_MLC := "MLC_gE" }
            Restante : Int;
        END_STRUCT;
    END_VAR
    VAR
        {
            S7_Access := "ReadOnly := External";
            S7_Setpoint := "False";
            S7_Visibility := "Hidden := External"
        }
        s_TON : Array[1..#NUM_MAX_TON] of _.UDT_ZC_DISP_TON := [
            { S7_MLC := "MLC_3nR" }
        (),
            { S7_MLC := "MLC_3Xz" }
        ()
        ];
        s_FF : Array[1..#NUM_MAX_FF] of _.UDT_ZC_DISP_FF;
        {
            S7_Access := "ReadOnly := External";
            S7_Visibility := "Hidden := External"
        }
        s_OrdenCalentar : Bool;
        {
            S7_Access := "ReadOnly := External";
            S7_Visibility := "Hidden := External"
        }
        s_OrdenEnfriar : Bool;
    END_VAR
    VAR_TEMP
        for_i : Int;
        t_TempHistSuperior : Real;
        t_TempHistInferior : Real;
        t_x0 : Real;
        t_x1 : Real;
        t_x : Real;
        t_y0 : Real;
        t_y1 : Real;
    END_VAR
    VAR CONSTANT
        SiempreON : Bool := TRUE;
        SiempreOFF : Bool := FALSE;
        NUM_MAX_TON : Int := 2;
        NUM_MAX_FF : Int := 1;
    END_VAR

    {
        S7_Language := "SCL";
        S7_NetworkTitle := "MLC_4VP"
    }
    NETWORK
        (*
        ============================================================================
        ZEUS CONTROL, S.A.
        (c)Copyright (2024) All Rights Reserved
        -----------------------------------------------------------------------------
        
        Software:       TIA Portal 18
        Restricciones:  PLC serie 1200/1500
        
        
        Nombre:         FB15184_ZC_FERM_FIN_PREP
        Descripcion:    Bloque de gestión de fase homogenizar
        
        Dependencias:   
            FC:         -
            FB:         -
            DB:         -
            UDT:        -
                     
        
        Change log:
        
            Version     Fecha       Tecnico a cargo     Descripción
            
            01.00.00    08.07.2025  (ABH)               Primera versión
            
         ============================================================================
        *)
    END_NETWORK
    {
        S7_Language := "LAD";
        S7_NetworkTitle := "MLC_vx"
    }
    NETWORK
        RUNG wire#powerrail
        END_RUNG
    END_NETWORK
    {
        S7_Language := "LAD";
        S7_NetworkTitle := "MLC_4Q6"
    }
    NETWORK
        RUNG wire#powerrail
            Add(
                in1 := #Parametros.P_Real_6,
                in2 := #Parametros.P_Real_7,
                out => #t_TempHistSuperior
            )
            Sub(
                in1 := #Parametros.P_Real_6,
                in2 := #Parametros.P_Real_7,
                out => #t_TempHistInferior
            )
        END_RUNG
    END_NETWORK
    {
        S7_Language := "LAD";
        S7_NetworkTitle := "MLC_4Tp"
    }
    NETWORK
        RUNG wire#powerrail
        END_RUNG
    END_NETWORK
    {
        S7_Language := "LAD";
        S7_NetworkTitle := "MLC_5CB"
    }
    NETWORK
        RUNG wire#powerrail
            Contact( #SiempreON )
            wire#w1
            Contact( #OrdenPausa )
            wire#w2
            S_Coil( #s_Pausa_M )
        END_RUNG
        RUNG wire#w1
            I_Contact( #EstadoEnMarcha )
        END_RUNG wire#w2
        RUNG wire#w1
            Contact( #s_Pausa_M )
            wire#w3
            Move(
                in := 3,
                out1 => #s_TON[1].SP
            )
            Coil( #s_TON[1].IN )
        END_RUNG
        RUNG wire#w3
            Contact( #s_TON[1].Q )
            I_Contact( #s_Continuar )
            S_Coil( #s_Pausa_V )
        END_RUNG
        RUNG wire#w1
            I_Contact( #OrdenPausa )
            Contact( #EstadoEnMarcha )
            wire#w4
            R_Coil( #s_Pausa_V )
        END_RUNG
        RUNG wire#w4
            S_Coil( #s_Continuar )
        END_RUNG
        RUNG wire#w1
            Contact( #s_Continuar )
            wire#w5
            Move(
                in := 3,
                out1 => #s_TON[2].SP
            )
            Coil( #s_TON[2].IN )
        END_RUNG
        RUNG wire#w5
            Contact( #s_TON[2].Q )
            wire#w6
            R_Coil( #s_Pausa_M )
        END_RUNG
        RUNG wire#w6
            R_Coil( #s_Continuar )
        END_RUNG
        RUNG wire#w1
            { S7_Templates := "SrcType := Int" }
            EQ_Contact(
                in1 := #s_EtapaActual,
                in2 := 0
            )
            wire#w7
            R_Coil( #s_Pausa_M )
        END_RUNG
        RUNG wire#w7
            R_Coil( #s_Pausa_V )
        END_RUNG
        RUNG wire#w7
            R_Coil( #s_Continuar )
        END_RUNG
    END_NETWORK
    {
        S7_Language := "LAD";
        S7_NetworkTitle := "MLC_3Td"
    }
    NETWORK
        RUNG wire#powerrail
            I_Contact( #EnEtapa )
            Move(
                in := 0,
                out1 => #s_EtapaActual
            )
        END_RUNG
    END_NETWORK
    {
        S7_Language := "SCL";
        S7_NetworkTitle := "MLC_3ED"
    }
    NETWORK
        //  ================================================================
        //  TEXTO ETAPA
        CASE #s_EtapaActual OF
            0:
                #s_TextoEtapa := CONCAT_STRING(IN1:= #NombreSecuencia,IN2:= ' - REPOSO');
                
               
            140:
                #s_TextoEtapa := CONCAT_STRING(IN1 := #NombreSecuencia, IN2 := ' - FIN PREPARACION');
                
            999:
                #s_TextoEtapa := CONCAT_STRING(IN1 := #NombreSecuencia, IN2 := ' - FIN DE SECUENCIA');
                
            ELSE
                #s_TextoEtapa := CONCAT_STRING(IN1 := #NombreSecuencia, IN2 := ' - FUERA DE ETAPA');
        END_CASE;
        
    END_NETWORK
    {
        S7_Language := "LAD";
        S7_NetworkTitle := "MLC_XS"
    }
    NETWORK
        RUNG wire#powerrail
        END_RUNG
    END_NETWORK
    {
        S7_Language := "LAD";
        S7_NetworkTitle := "MLC_vu"
    }
    NETWORK
        RUNG wire#powerrail
            { S7_Templates := "SrcType := Int" }
            EQ_Contact(
                in1 := #s_EtapaActual,
                in2 := 0
            )
            Contact( #EnEtapa )
            Contact( #OrdenMarcha )
            Move(
                in := 140,
                out1 => #s_EtapaActual
            )
            Move(
                in := 0,
                out1 => #s_TiempoEtapa.Actual
            )
        END_RUNG
    END_NETWORK
    {
        S7_Language := "LAD";
        S7_NetworkTitle := "MLC_3yM"
    }
    NETWORK
        RUNG wire#powerrail
            { S7_Templates := "SrcType := Int" }
            EQ_Contact(
                in1 := #s_EtapaActual,
                in2 := 140
            )
            wire#w1
            P_Trig( #s_Pulsos[3] )
            Move(
                in := #Parametros.P_Int_1,
                out1 => #s_TiempoEtapa.SP
            )
        END_RUNG
        RUNG wire#w1
            Contact( #SiempreOFF )
            { S7_Templates := "SrcType := Int" }
            GE_Contact(
                in1 := #s_TiempoEtapa.Actual,
                in2 := #s_TiempoEtapa.SP
            )
            Move(
                in := 999,
                out1 => #s_EtapaActual
            )
            Move(
                in := 0,
                out1 => #s_TiempoEtapa.Actual
            )
        END_RUNG
    END_NETWORK
    {
        S7_Language := "LAD";
        S7_NetworkTitle := "MLC_mY"
    }
    NETWORK
        RUNG wire#powerrail
            { S7_Templates := "SrcType := Int" }
            EQ_Contact(
                in1 := #s_EtapaActual,
                in2 := 999
            )
            wire#w1
            P_Trig( #s_Pulsos[15] )
            Move(
                in := 3,
                out1 => #s_TiempoEtapa.SP
            )
        END_RUNG
        RUNG wire#w1
            I_Contact( #EnEtapa )
            S_Coil( #s_TiempoEtapa.Activar )
        END_RUNG
        RUNG wire#w1
            { S7_Templates := "SrcType := Int" }
            GE_Contact(
                in1 := #s_TiempoEtapa.Actual,
                in2 := #s_TiempoEtapa.SP
            )
            Move(
                in := 0,
                out1 => #s_EtapaActual
            )
            Move(
                in := 0,
                out1 => #s_TiempoEtapa.Actual
            )
        END_RUNG
    END_NETWORK
    {
        S7_Language := "LAD";
        S7_NetworkTitle := "MLC_3Bx"
    }
    NETWORK
        RUNG wire#powerrail
        END_RUNG
    END_NETWORK
    {
        S7_Language := "LAD";
        S7_NetworkTitle := "MLC_4wF"
    }
    NETWORK
        RUNG wire#powerrail
            Contact( #s_TiempoEtapa.Activar )
            wire#w1
            Contact( #EstadoEnMarcha )
            I_Contact( #s_Pausa_M )
            Contact( #Pulso1Seg )
            { S7_Templates := "SrcType := Int" }
            LT_Contact(
                in1 := #s_TiempoEtapa.Actual,
                in2 := #s_TiempoEtapa.SP
            )
            { S7_Templates := "DestType := Int" }
            Inc( #s_TiempoEtapa.Actual )
        END_RUNG
        RUNG wire#w1
            Contact( #s_TiempoEtapa.Reset )
            Move(
                in := 0,
                out1 => #s_TiempoEtapa.Actual
            )
        END_RUNG
        RUNG wire#w1
            R_Coil( #s_TiempoEtapa.Activar )
        END_RUNG
        RUNG wire#powerrail
            Sub(
                in1 := #s_TiempoEtapa.SP,
                in2 := #s_TiempoEtapa.Actual,
                out => #s_TiempoEtapa.Restante
            )
            { S7_Templates := "SrcType := Int" }
            LE_Contact(
                in1 := #s_TiempoEtapa.Restante,
                in2 := 0
            )
            Move(
                in := 0,
                out1 => #s_TiempoEtapa.Restante
            )
        END_RUNG
    END_NETWORK
    {
        S7_Language := "SCL";
        S7_NetworkTitle := "MLC_dV"
    }
    NETWORK
        
        //  ============================================================================
        //  TON
        FOR #for_i := 1 TO #NUM_MAX_TON DO
            "FC2928_ZC_TON_INDV"(Pulso1seg := #Pulso1Seg,
                                 TON := #s_TON[#for_i]);
        END_FOR;
        
        //  ============================================================================
        //  FF
        FOR #for_i := 1 TO #NUM_MAX_FF DO
            "FC2926_ZC_FF_INDV"(Pulso1Seg := #Pulso1Seg,
                                FF := #s_FF[#for_i]);
        END_FOR;
        
    END_NETWORK
    {
        S7_Language := "LAD";
        S7_NetworkTitle := "MLC_Td"
    }
    NETWORK
        RUNG wire#powerrail
        END_RUNG
    END_NETWORK
    {
        S7_Language := "LAD";
        S7_NetworkTitle := "MLC_4xq"
    }
    NETWORK
        RUNG wire#powerrail
            { S7_Templates := "SrcType := Int" }
            EQ_Contact(
                in1 := #Parametros.P_Int_15,
                in2 := 1
            )
            { S7_Templates := "SrcType := Int" }
            GE_Contact(
                in1 := #s_EtapaActual,
                in2 := 140
            )
            { S7_Templates := "SrcType := Int" }
            LE_Contact(
                in1 := #s_EtapaActual,
                in2 := 150
            )
            I_Contact( #s_Pausa_M )
            wire#w1
            { S7_Templates := "SrcType := Int" }
            EQ_Contact(
                in1 := #Parametros.P_Int_7,
                in2 := 0
            )
            wire#w2
            Move(
                in := #Parametros.P_Real_6,
                out1 => #SP_CalEnf
            )
            wire#w3
            { S7_Templates := "SrcType := Real" }
            LE_Contact(
                in1 := #TT_Tanque,
                in2 := #SP_CalEnf
            )
            S_Coil( #s_OrdenCalentar )
        END_RUNG
        RUNG wire#w3
            { S7_Templates := "SrcType := Real" }
            GE_Contact(
                in1 := #TT_Tanque,
                in2 := #t_TempHistSuperior
            )
            R_Coil( #s_OrdenCalentar )
        END_RUNG
        RUNG wire#w2
            R_Coil( #s_OrdenEnfriar )
        END_RUNG
        RUNG wire#w1
            { S7_Templates := "SrcType := Int" }
            EQ_Contact(
                in1 := #Parametros.P_Int_7,
                in2 := 1
            )
            wire#w4
            Move(
                in := #Parametros.P_Real_6,
                out1 => #SP_CalEnf
            )
            wire#w5
            { S7_Templates := "SrcType := Real" }
            GE_Contact(
                in1 := #TT_Tanque,
                in2 := #SP_CalEnf
            )
            S_Coil( #s_OrdenEnfriar )
        END_RUNG
        RUNG wire#w5
            { S7_Templates := "SrcType := Real" }
            LE_Contact(
                in1 := #TT_Tanque,
                in2 := #t_TempHistInferior
            )
            R_Coil( #s_OrdenEnfriar )
        END_RUNG
        RUNG wire#w4
            R_Coil( #s_OrdenCalentar )
        END_RUNG
        RUNG wire#w1
            Coil( #VisibilidadCalEnf )
        END_RUNG
        RUNG wire#w1
            Contact( #s_OrdenCalentar )
            Coil( #OrdenCalentar )
        END_RUNG
        RUNG wire#w1
            Contact( #s_OrdenEnfriar )
            Coil( #OrdenEnfriar )
        END_RUNG
        RUNG wire#powerrail
            { S7_Templates := "SrcType := Int" }
            NE_Contact(
                in1 := #s_EtapaActual,
                in2 := 140
            )
            { S7_Templates := "SrcType := Int" }
            NE_Contact(
                in1 := #s_EtapaActual,
                in2 := 150
            )
            R_Coil( #s_OrdenEnfriar )
            R_Coil( #s_OrdenCalentar )
        END_RUNG
    END_NETWORK
    {
        S7_Language := "LAD";
        S7_NetworkTitle := "MLC_4jx"
    }
    NETWORK
        RUNG wire#powerrail
            { S7_Templates := "SrcType := Int" }
            EQ_Contact(
                in1 := #s_EtapaActual,
                in2 := 140
            )
            { S7_Templates := "SrcType := Int" }
            EQ_Contact(
                in1 := #Parametros.P_Int_2,
                in2 := 0
            )
            I_Contact( #s_Pausa_M )
            Contact( #EstadoBocaHombre )
            { S7_Templates := "SrcType := Real" }
            GT_Contact(
                in1 := #LT_Tanque,
                in2 := #Parametros.P_Real_2
            )
            { S7_Templates := "SrcType := Real" }
            LT_Contact(
                in1 := #LT_Tanque,
                in2 := #Parametros.P_Real_3
            )
            wire#w1
            { S7_Templates := "SrcType := Int" }
            EQ_Contact(
                in1 := #Parametros.P_Int_6,
                in2 := 0
            )
            Move(
                in := #Parametros.P_Int_4,
                out1 => #s_FF[1].Config_SetPointTiempoFlip
            )
            Move(
                in := #Parametros.P_Int_3,
                out1 => #s_FF[1].Config_SetPointTiempoFlop
            )
            Coil( #s_FF[1].Orden_Activar )
        END_RUNG
        RUNG wire#w1
            { S7_Templates := "SrcType := Int" }
            EQ_Contact(
                in1 := #Parametros.P_Int_6,
                in2 := 1
            )
            wire#w2
            Coil( #MarchaAgitador )
        END_RUNG
        RUNG wire#w1
            Contact( #s_FF[1].Estado_Flip )
        END_RUNG wire#w2
    END_NETWORK
    {
        S7_Language := "SCL";
        S7_NetworkTitle := "MLC_56S"
    }
    NETWORK
        
        IF #Parametros.P_Int_5 = 1 THEN
            // =============================================================================
            //VELOCIDAD FIJA
            #VelocidadAgitador := #Parametros.P_Real_1;
        ELSE
            // =============================================================================
            //VELOCIDAD POR NIVEL
            #t_x0 := #Parametros.P_Real_2;
            #t_x1 := #Parametros.P_Real_3;
            #t_x := #LT_Tanque;
            #t_y0 := #Parametros.P_Real_4;
            #t_y1 := #Parametros.P_Real_5;
            #VelocidadAgitador := #t_y0 + (#t_y1 - #t_y0) * ((#t_x - #t_x0) / (#t_x1 - #t_x0));
            IF #VelocidadAgitador > #t_y1 THEN
                #VelocidadAgitador := #t_y1;
            END_IF;
            IF #VelocidadAgitador < #t_y0 THEN
                #VelocidadAgitador := #t_y0;
            END_IF;
        END_IF;
                                        
    END_NETWORK
    {
        S7_Language := "LAD";
        S7_NetworkTitle := "MLC_4Nk"
    }
    NETWORK
        RUNG wire#powerrail
            { S7_Templates := "SrcType := Int" }
            GT_Contact(
                in1 := #s_EtapaActual,
                in2 := 0
            )
            Coil( #SecuenciaIniciada )
        END_RUNG
    END_NETWORK
    {
        S7_Language := "LAD";
        S7_NetworkTitle := "MLC_3rE"
    }
    NETWORK
        RUNG wire#powerrail
            { S7_Templates := "SrcType := Int" }
            EQ_Contact(
                in1 := #s_EtapaActual,
                in2 := 999
            )
            Coil( #FinSecuencia )
        END_RUNG
    END_NETWORK
END_FUNCTION_BLOCK
