
FUNCTION_BLOCK FB30

VAR_TEMP
    posTemp: byte;

END_VAR
VAR
    step: ARRAY[0..5] OF BOOL;
    TR: ARRAY[0..7] OF BOOL;
    cdQuitter: BOOL; // cd pour quitter mode Manu
    cdUtilisable: BOOL;
    flag_HMI_Z10_GoManu: BOOL;
    travailEnCours, demandePalette: BOOL;
END_VAR

    //transition
    TR[0]:= step[0] AND "HMI_DB".HMI_Z10_GoManu AND NOT flag_HMI_Z10_GoManu AND cdUtilisable; // 0 vers 1
    TR[1]:= step[1] AND X_Move_End AND Z_Move_End; // 1 vers 0
    TR[2]:= step[0] AND "HMI_DB".HMI_Z10_Z_plateauOffset AND NOT "Global".UpperPosition 
            OR step[0] AND NOT "HMI_DB".HMI_Z10_Z_plateauOffset AND "Global".UpperPosition AND cdUtilisable; // 0 vers 2
    TR[3]:= step[2] AND "G7_PrendreDeposer_DB".step[5]; // 2 vers 3
    TR[4]:= step[3] ; // 3 vers 0
        
    //etape
    IF TR[0] THEN // 0 vers 1
        step[0]:= false;
        step[1]:= true;
    END_IF;
    IF TR[1] THEN // 1 vers 0
        step[1]:= false;
        step[0]:= true;
    END_IF;
    IF TR[2] THEN // 0 vers 2
        step[0]:= false;
        step[2]:= true;
    END_IF;
    IF TR[3] THEN // 2 vers 3
        step[2]:= false;
        step[3]:= true;
    END_IF;
    IF TR[4] THEN // 3 vers 0
        step[3]:= false;
        step[0]:= true;
    END_IF;
    
    //prog
    IF step[1] THEN
        posTemp:= INT_TO_BYTE("HMI_DB".HMI_Z10_PosManu);
        IF posTemp = 0 THEN 
            Place_x:= 17;
            Place_Z:= 17; 
        ELSE
            Place_x:= posTemp;
            Place_Z:= posTemp;
        END_IF;
    END_IF;
    IF step[3] THEN
        "HMI_DB".HMI_Z10_Z_plateauOffset:= "Global".UpperPosition;
    END_IF;
    
    //gestion DB place
    IF "G7_PrendreDeposer_DB".TR[1] AND BYTE_TO_INT(Place_x) < 17 AND BYTE_TO_INT(Place_x) > 0 THEN
        "GestionStock_DB".stock[BYTE_TO_INT(Place_x)] := false;
    END_IF;
    IF "G7_PrendreDeposer_DB".TR[2] AND BYTE_TO_INT(Place_x) < 17 AND BYTE_TO_INT(Place_x) > 0 THEN
        "GestionStock_DB".stock[BYTE_TO_INT(Place_x)] := true;
    END_IF;

    
    IF "HMI_DB".HMI_Z10_PosManu < 17 AND "HMI_DB".HMI_Z10_PosManu > 0 THEN
        "HMI_DB".HMI_Z10_tempsAPosManu := "GestionStock_DB".stockTimer["HMI_DB".HMI_Z10_PosManu];
        "HMI_DB".HMI_Z10_etatDecantManu := "GestionStock_DB".stockEtat["HMI_DB".HMI_Z10_PosManu];
    ELSE 
        "HMI_DB".HMI_Z10_tempsAPosManu:= 0;
        "HMI_DB".HMI_Z10_etatDecantManu :=0;
    END_IF;
        
    //gestion    
    cdQuitter:= step[0] AND NOT "Global".UpperPosition AND NOT "HMI_DB".HMI_Z10_mouvementEnCours AND NOT "HMI_DB".HMI_Z10_demandePalette;
    "HMI_DB".HMI_Z10_cdQuitterManu:= cdQuitter;
    travailEnCours:= "HMI_DB".HMI_Z10_mouvementEnCours;
    demandePalette:= "HMI_DB".HMI_Z10_demandePalette;
    cdUtilisable:= "G7_ResetPos_DB".step[0] and "TeachPos_DB".step[0];

    //front
    flag_HMI_Z10_GoManu:= "HMI_DB".HMI_Z10_GoManu;
END_FUNCTION_BLOCK
