
FUNCTION_BLOCK FB20
VAR_TEMP
    // variables temporaires

END_VAR
VAR
    TR: ARRAY[0..10] OF BOOL; //Transition
    modeAutoOff: BOOL; //varialbe pour sortir du mode auto
    flag_S5, flag_S6, flag_startGeneral, flag_stopGeneral, flag_HMI_Z8_marche, flag_HMI_Z8_arret: BOOL; // Flag 
    cdSortieAuto, CdSortieManu, cdSortieMaintenance: BOOL;
    tOnDCY, timerOutDCY: BOOL;
    tOn, tOn2: S5TIME;
    tOntempo_bin, tOn2tempo_bin: WORD;
    
END_VAR

// liste transition:
TR[0]:= "G7_Gestion_General".etape[0] AND (("HMI_DB".HMI_Z8_marche AND NOT flag_HMI_Z8_marche) OR (startGeneral AND NOT flag_startGeneral)) ; // TR 0 vers 1
TR[1]:= "G7_Gestion_General".etape[1] AND (s6 OR "HMI_DB".HMI_Z8_arret OR stopGeneral); //TR 1 vers 2
TR[2]:= "G7_Gestion_General".etape[2] AND CdSortieAuto; //TR 2 vers 0
TR[3]:= "G7_Gestion_General".etape[2] AND ((S5 AND NOT Flag_S5) OR ("HMI_DB".HMI_Z8_marche AND NOT flag_HMI_Z8_marche) OR (startGeneral AND NOT flag_startGeneral)); //TR 2 vers 1
TR[4]:= "G7_Gestion_General".etape[0] AND "HMI_DB".HMI_Z8_maintenance AND NOT TR[0]; // TR 0 vers 3
TR[5]:= "G7_Gestion_General".etape[3] AND NOT "HMI_DB".HMI_Z8_maintenance AND cdSortieMaintenance; // TR 3 vers 0
TR[6]:= "G7_Gestion_General".etape[0] AND "HMI_DB".HMI_Z8_Manu AND NOT TR[0] AND NOT TR[4]; // TR 0 vers 4
TR[7]:= "G7_Gestion_General".etape[4] AND NOT "HMI_DB".HMI_Z8_Manu AND cdSortieManu; // TR 4 vers 0
TR[8]:= "G7_Gestion_General".etape[0] AND tOnDCY; // TR 0 vers 5   
TR[9]:= "G7_Gestion_General".etape[5] AND ((S5 AND NOT Flag_S5) OR ("HMI_DB".HMI_Z8_marche AND NOT flag_HMI_Z8_marche) OR (startGeneral AND NOT flag_startGeneral)) ; // TR 5 vers 1
TR[10]:= "G7_Gestion_General".etape[5] AND (s6 OR "HMI_DB".HMI_Z8_arret OR stopGeneral OR timerOutDCY); // TR 5 vers 0

// liste etape
IF TR[0] THEN // 0 vers 1
    "G7_Gestion_General".etape[0]:= false;
    "G7_Gestion_General".etape[1]:= true;
    END_IF;
IF TR[1] THEN // 1 vers 2
    "G7_Gestion_General".etape[1]:= false;
    "G7_Gestion_General".etape[2]:= true;
    END_IF;
IF TR[2] THEN // 2 vers 0
    "G7_Gestion_General".etape[2]:= False;
    "G7_Gestion_General".etape[0]:= true;
END_IF;
IF TR[3] THEN // 2 vers 1
    "G7_Gestion_General".etape[2]:= false;
    "G7_Gestion_General".etape[1]:= true;
END_IF;
IF TR[4] THEN // 0 vers 3
    "G7_Gestion_General".etape[0]:= false;
    "G7_Gestion_General".etape[3]:= true;
END_IF;
IF TR[5] THEN // 3 vers 0
    "G7_Gestion_General".etape[3]:= false;
    "G7_Gestion_General".etape[0]:= true;
END_IF;
IF TR[6] THEN // 0 vers 4
    "G7_Gestion_General".etape[0]:= false;
    "G7_Gestion_General".etape[4]:= true;
END_IF;
IF TR[7] THEN // 4 vers 0
    "G7_Gestion_General".etape[4]:= false;
    "G7_Gestion_General".etape[0]:= true;
END_IF;
IF TR[8] THEN // 0 vers 5
    "G7_Gestion_General".etape[0]:= false;
    "G7_Gestion_General".etape[5]:= true;
END_IF;
IF TR[9] THEN // 5 vers 1
    "G7_Gestion_General".etape[5]:= false;
    "G7_Gestion_General".etape[1]:= true;
END_IF;
IF TR[10] THEN // 5 vers 0
    "G7_Gestion_General".etape[5]:= false;
    "G7_Gestion_General".etape[0]:= true;
END_IF;


//prog
//assure une init correct du système lors d'un redémarage
IF "G7_Gestion_General".etape[0] AND "G7_Section0DB".init < 1 THEN
    "G7_Section0DB".init:= 2;
END_IF;

//resetG7 auto quand on quitte le mode auto
IF TR[2] THEN
    ResetG7Auto();
END_IF;

// appel des sous-programes 
IF "G7_Gestion_General".etape[1] OR "G7_Gestion_General".etape[2] THEN // appel les G7 du mode auto
    G7_tapis.G7_tapisDB();
    G7_section0.G7_Section0DB();
    G7_section1.G7_section1DB();
    G7_section2.G7_section2DB();
    G7_section3.G7_section3DB();
    G7_section4.G7_section4DB();
    G7_section5.G7_section5DB();
END_IF;
IF "G7_Gestion_General".etape[4] THEN // appel le mode manu
    ModeManu.ModeManuDB();
END_IF;
IF "G7_Gestion_General".etape[3] THEN // appel le mode manu
    ModeMaint.ModeMaintDB();
END_IF;

//cd de sortie d'étape de G7
cdSortieAuto:= "G7_GestionSection".tapis[0];
CdSortieManu:= not ("GestionManu".ManuSection0 OR "GestionManu".ManuSection1 OR "GestionManu".manuSection2 OR "GestionManu".manuSection3 OR "GestionManu".manuSection4 OR "GestionManu".manuSection5
                        OR "GestionManu".manuTapis OR "GestionManu".ChargementZ7 OR "GestionManu".DechargementZ9 OR "GestionManu".DechargementZ10);
cdSortieMaintenance:= NOT ("GestionMaintenance".MaintSection0 OR "GestionMaintenance".MaintSection1 OR "GestionMaintenance".MaintSection2 OR "GestionMaintenance".MaintSection3 
                        or "GestionMaintenance".MaintSection4 or "GestionMaintenance".MaintSection5
                        OR "GestionMaintenance".MaintTapis OR "GestionMaintenance".MaintChargementZ7 OR "GestionMaintenance".MaintChargementZ9 OR "GestionMaintenance".MaintChargementZ10);

//mode pour affichage hmi
"HMI_DB".optionAutoDisp:= "G7_Gestion_General".etape[0] OR "G7_Gestion_General".etape[1] OR "G7_Gestion_General".etape[2] OR "G7_Gestion_General".etape[5];
"HMI_DB".optionManuDisp:= "G7_Gestion_General".etape[0] OR "G7_Gestion_General".etape[4];
"HMI_DB".optionMaintenanceDisp:= "G7_Gestion_General".etape[0] OR "G7_Gestion_General".etape[3];
"HMI_DB".optionBoutonManuMaint:= "G7_Gestion_General".etape[3] OR "G7_Gestion_General".etape[4];
"HMI_DB".optionBoutonMaint:= "G7_Gestion_General".etape[3] AND "G7_AUDB".step[1];

//gestion tempo
tOn:= S_ODT (T_NO:= T6, S:=S5, TV:=T#3s, R:=FALSE, BI:=tOntempo_bin,Q:=tOnDCY) ;
tOn2:= S_ODT (T_NO:= T7, S:="G7_Gestion_General".etape[5], TV:=T#30s, R:=FALSE, BI:=tOn2tempo_bin,Q:=timerOutDCY) ;
   
// front montant
Flag_S5:= S5;
Flag_S6:= S6;
flag_startGeneral := startGeneral;
flag_HMI_Z8_marche:= "HMI_DB".HMI_Z8_marche;
flag_HMI_Z8_arret:= "HMI_DB".HMI_Z8_arret;
END_FUNCTION_BLOCK
