// Block Type: FC
// Block Name (Original): FC TT Devices
// Block Number: 380
// Original Network Languages: SCL, STL, LAD

FUNCTION "FC_TT_Devices" : Void
{ S7_Optimized_Access := 'TRUE' }
VERSION : 0.1

VAR_TEMP
  YLBR1 : Bool;
  YLBR2 : Bool;
  YLBR3 : Bool;
  YLBR4 : Bool;
  YLBR5 : Bool;
  SystemReady : Bool;
  ConveyorsRunning : Bool;
  Filler_Run : Bool;
  Labeller_Run : Bool;
  Line_Empty : Bool;
  Reset_Push_Button : Bool;
  Line_Empty : Bool;
  DUMMY : Bool;
  LEVEL_OK : Bool;
  Temp : Bool;
  Ap_HighLim : Real;
  Ap_LowLim : Real;
  Apoyo_Rechazo : Bool;
END_VAR

  #_0 : Bool; // Auto-generated temporary
  #_1500ms : Bool; // Auto-generated temporary
BEGIN

  // Network 1: EMD - Body Guide (Original Language: LAD)

  "DB TTOP - Body Guide EMD"(Number_Zone := 4, Pressure_Control_Zone_1 := "P1-M37" AND NOT "DB TT Alarm"."A371_04_0", Pressure_Control_Zone_2 := "P1-M39" AND NOT "DB TT Alarm"."A371_04_1", Pressure_Control_Zone_3 := "P1-M41" AND NOT "DB TT Alarm"."A371_04_2", Pressure_Control_Zone_4 := "P1-M42" AND NOT "DB TT Alarm"."A371_04_3", Pressure_Control_Zone_5 := FALSE);

  // Network 2: Elevator Door Management - UpStair (Original Language: LAD)

  "DB Door Control - Lifter - UpStair"(BUTTON_REQUEST := "E7.2", CLK_FAST := "M1.3", CLK_SLOW := "M1.5", DOOR_OPEN := "E7.6" OR "E7.7" OR NOT "KS165-1C", FBK_MACHINE_OFF := Eq("DB TT Motor 35"."Manage"."STATUS_VFD_ACT_Speed_Hz", 0));

  // Network 3: Elevator Door Management - DownStair (Original Language: LAD)

  "DB Door Control - Lifter - DownStair"(BUTTON_REQUEST := "E7.0", CLK_FAST := "M1.3", CLK_SLOW := "M1.5", DOOR_OPEN := "E7.4" OR "E7.5" OR NOT "KS165-1C", FBK_MACHINE_OFF := Eq("DB TT Motor 35"."Manage"."STATUS_VFD_ACT_Speed_Hz", 0));

  // Network 4: Elevator - Right/ Left  Control breaking chains (Original Language: LAD)

  "Timer_Chain_Right"(IN := NOT "S1-M35", PT := S5T#1500ms); // TODO: Declarar "Timer_Chain_Right" : TON;

  "Timer_Chain_Left"(IN := NOT "S2-M35", PT := S5T#1500ms); // TODO: Declarar "Timer_Chain_Left" : TON;

  // Network 5: Energy Saving - Line Empty (Original Language: STL)

  // --- BEGIN STL Network 5 ---
  ```stl 
     	A	"B1-M31"
     	A	"B1-M32"
     	A	"B2-M32"
     	A	"B1-M34"
     	A	"B2-M34"
     	A	"B1-M35"
     	A	"B2-M35"
     	A	"B4-M35"
     	A	"B5-M35"
     	A	"B1-M36"
     	A	"B1-M37"
     	A	"B2-M37"
     	A	"B1-M38"
     	A	"B2-M38"
     	A	"B1-M39"
     	A	"B2-M39"
     	A	"B1-M40"
     	A	"B2-M40"
     	A	"B1-M41"
     	A	"B2-M41"
     	A	"B1-M42"
     	A	"B2-M42"
     	=	"Line Empty"
  ``` 
  // --- END STL Network 5 ---

  // Network 6: Energy Saving (Original Language: LAD)

  "DB TTOP - Energy Saving"(DownStream_Mac_Req := "M0.1" OR ("DB Signal DownStream Machine - TL25_Q2"."IN_DIG_Signal_01" AND "M0.0"), Sensor_on_the_line := "Line Empty", UpStream_Mac_Empty := "DB TT Run"."TT_Run"."OUT_Cycle_ON" AND (("ComSV"."TL25_ReadFromSv"."FromSV"."STW"."X00" AND "ComSV"."TL25_ReadFromSv"."FromSV"."Data From TL28"."Run") OR (NOT "ComSV"."TL25_ReadFromSv"."FromSV"."STW"."X00" AND NOT "ComSV"."TL25_ReadFromSv"."FromSV"."STW"."X13" AND NOT "ComSV"."TL25_ReadFromSv"."FromSV"."STW"."X14")));

  // Network 7: Bottle Counter M153_154 (Original Language: LAD)

  "DB Bottle Counter M153_154"(Conveyor_Running := "DB TT Motor 41"."Manage"."STATUS_VFD_Run_FWD" AND "DB TT Motor 42"."Manage"."STATUS_VFD_Run_FWD", PH_Inlet := "Count 1 M42", PH_Outlet := "Count 2 M42", Reset_Button_Allarm := "SH331-1B", Reset_Counter_Henkel := "ComSV"."TL25_WriteToSv"."FromHenkel"."STW"."di0.x"[1]);

  // Network 8: Bottle Counter M37 (Original Language: LAD)

  "DB Bottle Counter_M37"(Conveyor_Running := "DB TT Motor 37"."Manage"."STATUS_VFD_Run_FWD", PH_Inlet := "Count 1 M35", PH_Outlet := "Count 2 M35", Reset_Button_Allarm := "SH331-1B", Reset_Counter_Henkel := "ComSV"."TL25_WriteToSv"."FromHenkel"."STW"."di0.x"[1]);

  IF "M0.1" THEN
    "DB TT Alarm"."A371_03_3" := FALSE;
  END_IF;

  // Network 9:  (Original Language: LAD)

  IF "Tag_27" > 5 THEN
    "Tag_27" := 0;
  END_IF;

  "Apoyo_Rechazo" := "B5-M35" OR ("M0.0" AND Eq("Tag_27", 5));

  // Network 10: Ejector Bottle (Original Language: STL)

  // --- BEGIN STL Network 10 ---
  ```stl 
     	CALL	"DB Ejector Bottle"
  ``` 
  // --- END STL Network 10 ---

  // Network 11: Photocell Teach (Original Language: LAD)

  "Delay Photocell Teach"(IN := "DB General"."X203_0", PT := S5T#0.8s); // TODO: Declarar "Delay Photocell Teach" : TP;

  "A8.0" := "Delay Photocell Teach".Q;

  "DB General"."X203_1" := "Delay Photocell Teach".Q;

  IF "Delay Photocell Teach".Q THEN
    "DB General"."X203_0" := FALSE;
  END_IF;

  // Network 12: Elevator - Guide (Original Language: STL)

  // --- BEGIN STL Network 12 ---
  ```stl 
     	CALL	"DB Guide Lifter - Lowerator"
  ``` 
  // --- END STL Network 12 ---

  // Network 13: Elevator - Lube (Original Language: STL)

  // --- BEGIN STL Network 13 ---
  ```stl 
     
     	CALL	"DB lube M35"
  ``` 
  // --- END STL Network 13 ---

  // Network 14: Camera Ejector Bottle (Original Language: STL)
  //   "E38.1"

  // --- BEGIN STL Network 14 ---
  ```stl 
     	CALL	"DB Camera Ejector Bottle"
  ``` 
  // --- END STL Network 14 ---

  // Network 15:  (Original Language: LAD)

  // Network 15 did not produce printable SCL code.

  // Network 16: Elevator -  SV Reject Y1-M35 (Original Language: LAD)

  "Y1-M35" := "Ejector_Tower" OR "Ejector_Camera";

  // Network 17: Signal for changeover done (Original Language: LAD)

  "Ap_EMD_InPsosition" := Eq("DB TTOP - Body Guide EMD"."Counter_Position", "DB TTOP - Body Guide EMD"."Position_Selection");

  "Ap_HighLim" := ("DB Guide Lifter - Lowerator"."Position Selection") + ("DB Guide Lifter - Lowerator"."Range Position");

  "Ap_LowLim" := ("DB Guide Lifter - Lowerator"."Position Selection") - ("DB Guide Lifter - Lowerator"."Range Position");

  "Ap_LifterGuideInPositon" := "DB Guide Lifter - Lowerator"."Actual Position" >= "Ap_LowLim";

  // Network 18: INTERLOCK EMD CHANGEOVER (Original Language: SCL)

  "ComSV".TL25_WriteToSv.ToSV.STW.QE1.X13 := "ComSV".TL25_ReadFromSv.FromSV.STW.X06;
  "ComSV".TL25_WriteToSv.ToSV.STW.QE1.X14 := "ComSV".TL25_ReadFromSv.FromSV.STW.X07;
  "ComSV".TL25_WriteToSv.ToSV.STW.QE1.X15 := "ComSV".TL25_ReadFromSv.FromSV.STW.X08;
  "ComSV".TL25_WriteToSv.ToSV.STW.QE1.X16 := "ComSV".TL25_ReadFromSv.FromSV.STW.X09;

END_FUNCTION
