
o<toolchange> sub

; M19 R0  ;align the spindle

; only unload the tool if there is a tool in the spindle
; This assumes that the carousel is already aligned correctly. 
; It is important to unload the tool before shutting down the machine. 

O100 IF [#<tool_in_spindle> GT 0]
    G53 G01 F300 X740 Y10 Z 3.5 ; Spindel hoch
	M66 P0 L3 Q5 ; wait for up = true   #Endschalter oben 
    O103 if [#5399 LT 0]
        (abort, failed to 1)
        M0
    O103 endif

	G53 G01 F300 Z-50; move down
	
    M65 P1 ; links aus
    M64 P0 ; rechts an
    M66 P1 L3 Q5 ; wait for right = true #Endschalter rechts
    O105 if [#5399 LT 0]
        (abort, failed to 2)
        M0
    O105 endif

    M64 P4 ; release tool
    M66 P5 L3 Q5 ; wait for tool-released = true #Sp untere Endschalter
    O106 if [#5399 LT 0]
        (abort, failed to 3)
        M0
    O106 endif

	G53 G01 F300 Z3.5; move up
    
    M65 P4 ; clamp tool
    M66 P4 L3 Q5 ; wait for tool-clamp = true #Sp obere Endschalter
    O114 if [#5399 LT 0]
        (abort, failed to 12)
        M0
    O114 endif

	M65 P0 ; rechts aus
    M64 P1 ; links an
    M66 P0 L3 Q5 ; wait for left = true #Endschalter links
    O112 if [#5399 LT 0]
        (abort, failed to 7)
        M0
    O112 endif 
    


O100 ENDIF

    G53 G01 F300 X740 Y10 Z 3.5

O200 IF [#<selected_tool> GT 0]

    
    M68 E0 Q#<selected_pocket> ;set the carousel to move to the right pocket

    M64 P5 ; start carousel RE
    M64 P6 ; TMCC
    G4 P1
    M66 P7 L3 Q100 ; wait for carousel finished
    O107 if [#5399 LT 0]
        (abort, failed to 6)
        M0
    O107 endif
    
    M65 P6; stop carousel
    M65 P5; RE
   
    M65 P1 ; links aus
    M64 P0 ; rechts an
    M66 P1 L3 Q5 ; wait for right = true #Endschalter rechts
    O112 if [#5399 LT 0]
        (abort, failed to 7)
        M0
    O112 endif

    M64 P4 ; released tool
    M66 P5 L3 Q5 ; wait for tool-clamp = true #Sp untere Endschalter
    O113 if [#5399 LT 0]
        (abort, failed to 8) 
    O113 endif
	 
	G53 G01 F300 Z-50; move down

    
    M65 P4 ; clamp tool
    M66 P4 L3 Q5 ; wait for tool-clamp = true #Sp obere Endschalter
    O114 if [#5399 LT 0]
        (abort, failed to 12)
        M0
    O114 endif
    
    M65 P0 ; rechts aus
    M64 P1 ; links an
    M66 P0 L3 Q5 ; wait for left = true #Endschalter links
    O112 if [#5399 LT 0]
        (abort, failed to 7)
        M0
    O112 endif 
    
O200 ENDIF 


O300 IF [#<tool_in_spindle> EQ 0]
    G53 G01 F300 X740 Y10 Z 3.5 ; Spindel hoch
	M66 P6 L3 Q5 ; wait for up = true   #Endschalter oben 
    O103 if [#5399 LT 0]
        (abort, failed to 1)
        M0
    O103 endif

	M68 E0 Q#<selected_pocket> ;set the carousel to move to the right pocket

    M64 P5 ; start carousel
    M64 P6 ; start carousel
    G4 P1
    M66 P7 L3 Q100 ; wait for carousel finished
    O107 if [#5399 LT 0]
        (abort, failed to 6)
        M0
    O107 endif
    M65 P6; stop carousel TMCC
	M65 P5; stop carousel RE
	
	M65 P1 ; links aus
    M64 P0 ; rechts an
    M66 P1 L3 Q5 ; wait for right = true #Endschalter rechts
    O105 if [#5399 LT 0]
        (abort, failed to 2)
        M0
    O105 endif

    M64 P4 ; release tool
    M66 P5 L3 Q5 ; wait for tool-released = true #Sp untere Endschalter
    O106 if [#5399 LT 0]
        (abort, failed to 3)
        M0
    O106 endif

	G53 G01 F300 Z-50; move down
    
    M65 P4 ; clamp tool
    M66 P4 L3 Q5 ; wait for tool-clamp = true #Sp obere Endschalter
    O114 if [#5399 LT 0]
        (abort, failed to 12)
        M0
    O114 endif

	M65 P0 ; rechts aus
    M64 P1 ; links an
    M66 P0 L3 Q5 ; wait for left = true #Endschalter links
    O112 if [#5399 LT 0]
        (abort, failed to 7)
        M0
    O112 endif 
    


O300 ENDIF




G43

o<toolchange> endsub [1]


M2
