(* Código SCL generado desde LAD TwinCAT *)
(* Convertidor mejorado con SymPy - Estructura DNF preferida *)
(* Path original: \/TASK1_PID\/PID_Controllers\/Filling_Valve_PID *)

PROGRAM _Filling_Head_PID_Ctrl
VAR_OUTPUT
    EN_Out					: BOOL ;
END_VAR

VAR
    mFillerProdSlew			: REAL ;
    mFillerEstSlew			: SlewLimit ;
    mKp_Loss				: REAL ;
    mR_KP					: REAL ;
    mR_TI					: REAL ;
    mR_TD					: REAL ;
    _Hold_int_PID			: BOOL ;
    _Init_int_PID			: BOOL ;
    mManual_Value			: REAL ;
    mReal_FF_value		: REAL ;
    mReal_Max_FF			: REAL ;
    mLMN_FAC				: REAL ;
    mDeadBand				: REAL ;
    mInit_Int_PID			: REAL ;
    mHr_Hs					: REAL ;
    mFillingHead_PID		: FB41_PIDController;
    mI_Sel_FillHead			: BOOL ;
    mP_Sel_FillHead		: BOOL ;
    mD_Sel_FillHead		: BOOL ;
    mPID_FillHead_OUT		: REAL ;
    mPIDFillHeadProp		: REAL ;
    mPIDFillHeadInt			: REAL ;
    mPIDFillHeadDer		: REAL ;
    mP3_Freq_Flted			: LowPassFilter ;
    mDiffTransducer_Flted	: LowPassFilter ;
    mFillValve1Press_Flted	: LowPassFilter ;
    mFillingHead_KP_PID	: FB41_PIDController;
    mFillHead_PID_En		: BOOL ;
    mI_Sel_FillHead_KP		: BOOL ;
    mP_Sel_FillHead_KP	: BOOL ;
    mD_Sel_FillHead_KP	: BOOL ;
    mPID_FillHead_KP_OUT	: REAL ;
    mPIDFillHead_KP_Prop	: REAL ;
    mPIDFillHead_KP_Int	: REAL ;
    mPIDFillHead_KP_Der	: REAL ;
    _Hold_int_KP_PID		: BOOL ;
    _Init_int_KP_PID		: BOOL ;
    mManual_KP_Value		: REAL ;
    mReal_KP_FF_value	: REAL ;
    mDead_KP_Band		: REAL ;
    mInit_Int_KP_PID		: REAL ;
    mFillingHeadPIDStat		: StatisticalAnalisys ;
    mHeadKP_SlewLimit		: SlewLimit ;
    mHead_KP_Temp		: REAL ;
    mOutWordToVFC		: WORD ;
    mRealTemp				: REAL ;
    mAnalogInTemp			: Peripherial ;
    mReset_Int_ONS		: R_TRIG ;
    mReset_Int				: BOOL ;
    mEn_ProdPressLoss		: TON ;
    mStartFillTON			: TON ;
    mStopFillTP				: TP ;
    mStopFillingN_ONS		: F_TRIG ;
    mFillerFilling				: BOOL ;
    mStopFilling				: BOOL;
    mDummy				: BOOL ;
    mDiffSensFaultEn: TON;
END_VAR


(* === CÓDIGO PRINCIPAL === *)

    (* Código LAD convertido *)
    // Red 2
    // Llamada a función: _Filling_Head_PID_Ctrl.Read_Analog
    CALL _Filling_Head_PID_Ctrl.Read_Analog();
    mDummy := TRUE; // ACTION ejecutada

    // Red 3
    // Llamada a función: _Filling_Head_PID_Ctrl._KP_PID_Ctrl
    CALL _Filling_Head_PID_Ctrl._KP_PID_Ctrl();
    mDummy := TRUE; // ACTION ejecutada

    // Red 4
    // Llamada a función: _Filling_Head_PID_Ctrl.Calcolous
    CALL _Filling_Head_PID_Ctrl.Calcolous();
    mDummy := TRUE; // ACTION ejecutada

    // Red 5
    // Llamada a función: gProd_Flow
    mDummy := gProd_Flow();

    // Red 6
    // Llamada a función: mFillerEstSlew
    // Sin optimización SymPy
    IF mFillerEstSlew(_POSITIV) THEN
        mDummy := TRUE;
    ELSE
        mDummy := FALSE;
    END_IF;

    // Red 7
    // Llamada a función: mStopFillTP
    // Sin optimización SymPy
    IF mStopFillTP() THEN
        mDummy := TRUE;
    ELSE
        mDummy := FALSE;
    END_IF;

    // Red 1
    // Sin optimización SymPy
    IF NOT gAlwaysOff THEN
        mP_Sel_FillHead := TRUE;
    ELSE
        mP_Sel_FillHead := FALSE;
    END_IF;

    // Red 8
    // Sin optimización SymPy
    IF NOT gAlwaysOff THEN
        mI_Sel_FillHead := TRUE;
    ELSE
        mI_Sel_FillHead := FALSE;
    END_IF;

    // Red 9
    // Sin optimización SymPy
    IF NOT gAlwaysOff THEN
        mD_Sel_FillHead := TRUE;
    ELSE
        mD_Sel_FillHead := FALSE;
    END_IF;

    // Red 10
    // Sin optimización SymPy
    IF (gBlenderCIPMode OR gBlenderRinseMode) THEN
        g_Head_CIP_Rinse := TRUE;
    ELSE
        g_Head_CIP_Rinse := FALSE;
    END_IF;

    // Red 11
    // Sin optimización SymPy
    IF ((NOT gH_P3_ProductPump_Auto AND gH_P3_ProductPump_Manual) OR g_Head_Drain OR (gH_EV62_Status AND g_Head_CIP_Rinse) OR gP_PID_Head_Manual) THEN
        g_MAN_Head := TRUE;
    ELSE
        g_MAN_Head := FALSE;
    END_IF;

    // Red 12
    // Sin optimización SymPy
    IF ((((gP_PID_Head_Enable OR (NOT gProdPipeRunOut_Done AND gBlendFillStartUp_Done)) AND gInFillerEV46Enable AND NOT gFlrCO2ValveEV46Pos_Fault AND gInFillerEV62Enable AND NOT gProdValveEV62Pos_Fault AND NOT gDiffSensor_Analog_Fault) OR (g_Head_CIP_Rinse AND gBlenderRun_Latch) OR g_MAN_Head OR gP_PID_Head_Manual) AND gH_Blender_OPT_BlendFillSystem) THEN
        g_Head_PID_Enable := TRUE;
    ELSE
        g_Head_PID_Enable := FALSE;
    END_IF;

    // Red 14
    // Llamada a función: gHead_PID_Max_Freq
    mDummy := gHead_PID_Max_Freq();

    // Red 15
    // Llamada a función: gH_ProcessSetup_FillerDiameter
    mDummy := gH_ProcessSetup_FillerDiameter();

    // Red 16
    // Llamada a función: mReal_FF_value
    mDummy := mReal_FF_value();

    // Red 17
    // Llamada a función: mManual_Value
    mDummy := mManual_Value();

    // Red 18
    // Llamada a función: _Filling_Head_PID_Ctrl.Write_Analog
    CALL _Filling_Head_PID_Ctrl.Write_Analog();
    mDummy := TRUE; // ACTION ejecutada

    // Red 13
    CALL _Filling_Head_PID_Ctrl.Write_Analog();
    mDummy := TRUE; // ACTION ejecutada

END_PROGRAM

(* === SUBFUNCIONES (ACTIONs convertidas) === *)

PROCEDURE _Filling_Head_PID_Ctrl__KP_PID_Ctrl
(* Convertida desde ACTION *)

    (* Código LAD convertido a SCL *)
    // Red 1
    IF NOT gAlwaysOff THEN
        mI_Sel_FillHead_KP := TRUE;
    ELSE
        mI_Sel_FillHead_KP := FALSE;
    END_IF;

    // Red 2
    IF NOT gAlwaysOff THEN
        mP_Sel_FillHead_KP := TRUE;
    ELSE
        mP_Sel_FillHead_KP := FALSE;
    END_IF;

    // Red 3
    IF NOT gAlwaysOff THEN
        mD_Sel_FillHead_KP := TRUE;
    ELSE
        mD_Sel_FillHead_KP := FALSE;
    END_IF;

    // Red 4
    IF mFillHead_PID_En THEN
        mR_KP := TRUE;
    ELSE
        mR_KP := FALSE;
    END_IF;


END_PROCEDURE

PROCEDURE _Filling_Head_PID_Ctrl_Calcolous
(* Convertida desde ACTION *)

    (* Código ST original *)
    gHead_PID_Cycle := gPID_Cycle_Time ;
    mLMN_FAC := 1.0 ;
    mDeadBand := 0.0 ;

    mKp_Loss := gH_ProcessSetup_KProdLoss ;

    IF gH_ProcessSetup_BPFillingValvePID<>0 THEN
    (*	IF gHeadPIDCtrlOk THEN*)
    mHead_KP_Temp := 100 / gH_ProcessSetup_BPFillingValvePID ;
    (*	ELSE
    mHead_KP_Temp := 50 / gH_ProcessSetup_BPFillingValvePID ;
    END_IF*)
    END_IF
    mHeadKP_SlewLimit(i_InValue:=mHead_KP_Temp, i_SlewMax:=0.01, i_Cycle:=gHead_PID_Cycle, out:=mR_KP) ;

    mR_TI := gH_ProcessSetup_TIFillingValvePID ;
    mR_TD := gH_ProcessSetup_TDFillingValvePID ;

    mManual_Value := 0.0 ;
    IF NOT gH_P3_ProductPump_Auto AND gH_P3_ProductPump_Manual THEN
    mManual_Value := gH_P3_ProductPumpFC_Manual ;
    mLMN_FAC := 1.0 ;
    ELSIF g_MAN_Head AND NOT g_Head_CIP_Rinse THEN
    mManual_Value := gHead_PID_Manual_Value ;
    mLMN_FAC := 1.0 ;
    ELSIF g_Head_Drain THEN
    mManual_Value := 0.0 ;
    mLMN_FAC := 1.0 ;
    ELSIF g_Head_CIP_Rinse AND gBlenderRinseMode THEN
    mManual_Value :=gH_ProcessSetup_RinseProdPumpFreq ;
    mLMN_FAC := 1.0 ;
    ELSIF g_Head_CIP_Rinse THEN
    mManual_Value :=gH_ProcessSetup_CIPProdPumpFreq ;
    mLMN_FAC := 1.0 ;
    ELSIF g_Head_PID_Enable AND gH_P3_ProductPump_Auto THEN
    gH_P3_ProductPumpFC_Manual := mmH2O_TO_Freq(mPID_FillHead_OUT,gH_ProcessSetup_KProdPump) ;
    mManual_Value := 0.0 ;
    mLMN_FAC := 1.0 ;
    ELSE
    mManual_Value := 0.0 ;
    mLMN_FAC := 0.0 ;
    END_IF

END_PROCEDURE

PROCEDURE _Filling_Head_PID_Ctrl_PID_Monitor
(* Convertida desde ACTION *)

    (* Código LAD convertido a SCL *)
    // Red 2
    // Llamada a función: mFillingHeadPIDStat
    IF mFillingHeadPIDStat(_POSITIV) THEN
        mDummy := TRUE;
    ELSE
        mDummy := FALSE;
    END_IF;

    // Red 1
    IF gHeadPIDCtrlOk THEN
        gHeadPIDCtrlOk := TRUE;
    ELSE
        gHeadPIDCtrlOk := FALSE;
    END_IF;


END_PROCEDURE

PROCEDURE _Filling_Head_PID_Ctrl_Read_Analog
(* Convertida desde ACTION *)

    (* Código LAD convertido a SCL *)
    // Red 1
    IF NOT gH_Blender_OPT_Simulation THEN
        gDiffSensor_Analog_Fault := TRUE;
    ELSE
        gDiffSensor_Analog_Fault := FALSE;
    END_IF;


END_PROCEDURE

PROCEDURE _Filling_Head_PID_Ctrl_Reset_Integral
(* Convertida desde ACTION *)

    (* Código ST original *)
    mInit_Int_PID := 0.0 ;
    _Init_int_PID := TRUE ;

END_PROCEDURE

PROCEDURE _Filling_Head_PID_Ctrl_Write_Analog
(* Convertida desde ACTION *)

    (* Código LAD convertido a SCL *)
    // Red 1
    IF NOT gH_Blender_OPT_Simulation THEN
        gProduct_VFC_MainRefValue := TRUE;
    ELSE
        gProduct_VFC_MainRefValue := FALSE;
    END_IF;


END_PROCEDURE
