
FUNCTION_BLOCK FB35

VAR_TEMP
    // variables temporaires

END_VAR
VAR    
    stepX: ARRAY[0..7] OF BOOL;
    TrX: ARRAY[0..10] OF BOOL;
    stepZ: ARRAY[0..7] OF BOOL;
    TrZ: ARRAY[0..10] OF BOOL;
    stepOffset: ARRAY[0..3] OF BOOL;
    TrOffset: ARRAY[0..4] OF BOOL;
    axeY: BOOL;
    cdQuitter: BOOL;
    gardeFou: BOOL;
    flag_offsetZ: BOOL;
    travailEnCours, demandePalette: BOOL;


END_VAR
        
    //transi axe X
    TrX[0]:= stepX[0] AND "HMI_DB".HMI_Z10_X_UP_quick AND NOT "HMI_DB".HMI_Z10_X_UP_slow AND NOT "HMI_DB".HMI_Z10_X_Down_slow AND NOT "HMI_DB".HMI_Z10_X_Down_quick AND gardeFou; // 0 vers 1
    TrX[1]:= stepX[0] AND NOT "HMI_DB".HMI_Z10_X_UP_quick AND "HMI_DB".HMI_Z10_X_UP_slow AND NOT "HMI_DB".HMI_Z10_X_Down_slow AND NOT "HMI_DB".HMI_Z10_X_Down_quick AND gardeFou; // 0 vers 2
    TrX[2]:= stepX[0] AND NOT "HMI_DB".HMI_Z10_X_UP_quick AND NOT "HMI_DB".HMI_Z10_X_UP_slow AND "HMI_DB".HMI_Z10_X_Down_slow AND not "HMI_DB".HMI_Z10_X_Down_quick AND gardeFou; // 0 vers 3
    TrX[3]:= stepX[0] AND NOT "HMI_DB".HMI_Z10_X_UP_quick AND NOT "HMI_DB".HMI_Z10_X_UP_slow AND NOT "HMI_DB".HMI_Z10_X_Down_slow AND "HMI_DB".HMI_Z10_X_Down_quick AND gardeFou; // 0 vers 4
    TrX[4]:= stepX[1] AND NOT "HMI_DB".HMI_Z10_X_UP_quick AND X_Jog_End; // 1 vers 5
    TrX[5]:= stepX[2] AND NOT "HMI_DB".HMI_Z10_X_UP_slow AND X_Jog_End; // 2 vers 5
    TrX[6]:= stepX[3] AND NOT "HMI_DB".HMI_Z10_X_Down_slow AND X_Jog_End; // 3 vers 5
    TrX[7]:= stepX[4] AND NOT "HMI_DB".HMI_Z10_X_Down_quick AND X_Jog_End; // 4 vers 5
    TrX[8]:= stepX[5] AND NOT X_Jog_End; // 5 vers 6
    TrX[9]:= stepX[6] AND X_Jog_End; // 6 vers 7
    TrX[10]:= stepX[7] AND NOT X_Jog_End; // 7 vers 0

    //etape
    IF TrX[0] THEN // 0 vers 1
        stepX[0]:= false;
        stepX[1]:= true;
    END_IF;
    IF TrX[1] THEN // 0 vers 2
        stepX[0]:= false;
        stepX[2]:= true;
    END_IF;
    IF TrX[2] THEN // 0 vers 3
        stepX[0]:= false;
        stepX[3]:= true;
    END_IF;
    IF TrX[3] THEN // 0 vers 4
        stepX[0]:= false;
        stepX[4]:= true;
    END_IF;
    IF TrX[4] THEN // 1 vers 5
        stepX[1]:= false;
        stepX[5]:= true;
    END_IF;
    IF TrX[5] THEN // 2 vers 5
        stepX[2]:= false;
        stepX[5]:= true;
    END_IF;
    IF TrX[6] THEN // 3 vers 5
        stepX[3]:= false;
        stepX[5]:= true;
    END_IF;
    IF TrX[7] THEN // 4 vers 5
        stepX[4]:= false;
        stepX[5]:= true;
    END_IF;
    IF TrX[8] THEN // 5 vers 6
        stepX[5]:= false;
        stepX[6]:= true;
    END_IF;
    IF TrX[9] THEN // 6 vers 7
        stepX[6]:= false;
        stepX[7]:= true;
    END_IF;
    IF TrX[10] THEN // 7 vers 0
        stepX[7]:= false;
        stepX[0]:= true;
    END_IF;


//transi axe Z
    TrZ[0]:= stepZ[0] AND "HMI_DB".HMI_Z10_Z_UP_quick AND NOT "HMI_DB".HMI_Z10_Z_UP_slow AND NOT "HMI_DB".HMI_Z10_Z_Down_slow AND NOT "HMI_DB".HMI_Z10_Z_Down_quick AND gardeFou; // 0 vers 1
    TrZ[1]:= stepZ[0] AND NOT "HMI_DB".HMI_Z10_Z_UP_quick AND "HMI_DB".HMI_Z10_Z_UP_slow AND NOT "HMI_DB".HMI_Z10_Z_Down_slow AND NOT "HMI_DB".HMI_Z10_Z_Down_quick AND gardeFou; // 0 vers 2
    TrZ[2]:= stepZ[0] AND NOT "HMI_DB".HMI_Z10_Z_UP_quick AND NOT "HMI_DB".HMI_Z10_Z_UP_slow AND "HMI_DB".HMI_Z10_Z_Down_slow AND not "HMI_DB".HMI_Z10_Z_Down_quick AND gardeFou; // 0 vers 3
    TrZ[3]:= stepZ[0] AND NOT "HMI_DB".HMI_Z10_Z_UP_quick AND NOT "HMI_DB".HMI_Z10_Z_UP_slow AND NOT "HMI_DB".HMI_Z10_Z_Down_slow AND "HMI_DB".HMI_Z10_Z_Down_quick AND gardeFou; // 0 vers 4
    TrZ[4]:= stepZ[1] AND NOT "HMI_DB".HMI_Z10_Z_UP_quick AND Z_Jog_End; // 1 vers 5
    TrZ[5]:= stepZ[2] AND NOT "HMI_DB".HMI_Z10_Z_UP_slow AND Z_Jog_End; // 2 vers 5
    TrZ[6]:= stepZ[3] AND NOT "HMI_DB".HMI_Z10_Z_Down_slow AND Z_Jog_End; // 3 vers 5
    TrZ[7]:= stepZ[4] AND NOT "HMI_DB".HMI_Z10_Z_Down_quick AND Z_Jog_End; // 4 vers 5
    TrZ[8]:= stepZ[5] AND NOT Z_Jog_End; // 5 vers 6
    TrZ[9]:= stepZ[6] AND Z_Jog_End; // 6 vers 7
    TrZ[10]:= stepZ[7] AND NOT Z_Jog_End; // 7 vers 0

    //etape
    IF TrZ[0] THEN // 0 vers 1
        stepZ[0]:= false;
        stepZ[1]:= true;
    END_IF;
    IF TrZ[1] THEN // 0 vers 2
        stepZ[0]:= false;
        stepZ[2]:= true;
    END_IF;
    IF TrZ[2] THEN // 0 vers 3
        stepZ[0]:= false;
        stepZ[3]:= true;
    END_IF;
    IF TrZ[3] THEN // 0 vers 4
        stepZ[0]:= false;
        stepZ[4]:= true;
    END_IF;
    IF TrZ[4] THEN // 1 vers 5
        stepZ[1]:= false;
        stepZ[5]:= true;
    END_IF;
    IF TrZ[5] THEN // 2 vers 5
        stepZ[2]:= false;
        stepZ[5]:= true;
    END_IF;
    IF TrZ[6] THEN // 3 vers 5
        stepZ[3]:= false;
        stepZ[5]:= true;
    END_IF;
    IF TrZ[7] THEN // 4 vers 5
        stepZ[4]:= false;
        stepZ[5]:= true;
    END_IF;
    IF TrZ[8] THEN // 5 vers 6
        stepZ[5]:= false;
        stepZ[6]:= true;
    END_IF;
    IF TrZ[9] THEN // 6 vers 7
        stepZ[6]:= false;
        stepZ[7]:= true;
    END_IF;
    IF TrZ[10] THEN // 7 vers 0
        stepZ[7]:= false;
        stepZ[0]:= true;
    END_IF;

    //transition offset Z
    TrOffset[0]:= stepOffset[0] AND "HMI_DB".HMI_Z10_Z_plateauOffset AND NOT flag_offsetZ AND NOT "Global".UpperPosition  AND gardeFou; // 0 vers 1
    TrOffset[1]:= stepOffset[0] AND NOT "HMI_DB".HMI_Z10_Z_plateauOffset AND flag_offsetZ AND "Global".UpperPosition  AND gardeFou; // 0 vers 2
    TrOffset[2]:= stepOffset[1] AND Z_MoveRel_End; // 1 vers 3
    TrOffset[3]:= stepOffset[2] AND Z_MoveRel_End; // 2 vers 3
    TrOffset[4]:= stepOffset[3] AND NOT Z_MoveRel_End; // 3 vers 0
    
    //etape
    IF TrOffset[0] THEN // 0 vers 1
        stepOffset[0]:= false;
        stepOffset[1]:= true;
    END_IF;
    IF TrOffset[1] THEN // 0 vers 2
        stepOffset[0]:= false;
        stepOffset[2]:= true;
    END_IF;
    IF TrOffset[2] THEN // 1 vers 3
        stepOffset[1]:= false;
        stepOffset[3]:= true;
    END_IF;
    IF TrOffset[3] THEN // 2 vers 3
        stepOffset[2]:= false;
        stepOffset[3]:= true;
    END_IF;
    IF TrOffset[4] THEN // 3 vers 0
        stepOffset[3]:= false;
        stepOffset[0]:= true;
    END_IF;

    //gestionHMI
    IF stepOffset[1] THEN
        "HMI_DB".HMI_Z10_Z_plateauOffset:= true;
        "Global".UpperPosition:= true;
    END_IF;
    IF stepOffset[2] THEN
        "HMI_DB".HMI_Z10_Z_plateauOffset:= false;
        "Global".UpperPosition:= false;
    END_IF;
    IF "HMI_DB".HMI_Z10_readyToSet0 THEN
        "PosListX".x[46]:= "Global".Position_X;
        "PosListZ".z[46]:= "Global".Position_Z;
    END_IF;
    IF "HMI_DB".HMI_Z10_initPos OR "HMI_DB".HMI_Z10_learnPos THEN
        "HMI_DB".HMI_Z10_Y_plateau:= false;
        "Global".UpperPosition:= false;
    END_IF;
               
    
    //prog
    gardeFou := stepX[0] AND stepZ[0] AND stepOffset[0] AND "G7_ResetPos_DB".step[0] AND "TeachPos_DB".step[0];
    axeY:= "HMI_DB".HMI_Z10_Y_plateau;
    cdQuitter:= NOT "HMI_DB".HMI_Z10_Z_plateauOffset AND NOT "HMI_DB".HMI_Z10_Y_plateau AND stepX[0] AND stepZ[0] AND stepOffset[0] AND NOT "Global".UpperPosition
                   AND NOT travailEnCours AND NOT demandePalette AND Y_rentre AND NOT Y_sorti AND "G7_ResetPos_DB".step[0] and "TeachPos_DB".step[0];
    "HMI_DB".HMI_Z10_cdQuitterMaint:= cdQuitter;
    "HMI_DB".HMI_Z10_readyToSet0:= "HMI_DB".HMI_Z10_Y_plateau AND stepX[0] AND stepZ[0] AND stepOffset[0] AND NOT "Global".UpperPosition; 
    travailEnCours:= "HMI_DB".HMI_Z10_mouvementEnCours;
    demandePalette:= "HMI_DB".HMI_Z10_demandePalette;

    //front montant
    flag_offsetZ:= "HMI_DB".HMI_Z10_Z_plateauOffset;

    
    
END_FUNCTION_BLOCK
