'Macro M6Start 12 Station ATC  for JEFF

Message" "                   ' Clear the Message bar

DoSpinStop()                 ' Make sure the spindle is OFF
Code"M09"                    ' Make sure the coolant is OFF

If  GetOEMLed(807) And  GetOemLED(808) And  GetOemLed(809) And  GetOemled(811) Then
MsgBox" 1 or more axis are  NOT REFERENCED cancel program and REF XYZB axis"
End
End If
 
Dim OldTool As Single
Dim NewTool As Single
Dim X As Double
Dim Y As Double
Dim Z As Double
Dim Frate As Double
Dim AtcRate As Double
Dim ToolUpPosition As Double
Dim ToolDownPosition As Double
'********************************
OldTool = GetCurrentTool()	 ' Get Tool# that is in the spindle
NewTool = GetSelectedTool()	 ' Get Tool# of the New Tool
Frate = GetDro( 18)
AtcrRate = 100
ToolUpPosition = GetUserDRO(2000)
ToolDownPosition = GetUserDRO(2001)

If NewTool = OldTool Then        ' Check the tool Number for same tool#
Message" Same Tool,  NO ACTION"
End
End If

If  NewTool > 12 Or NewTool < 1 Then   ' Verify Tool # is between 1-12
MsgBox" Tool # not a valid Number 1-12 ONLY, ENDING Program RUN "
DoButton(3)
End
End If



x = GetUserDRO( 1200 )            ' Tool change location
y = GetUserDRO( 1201 )
z = GetUserDRO( 1202 )


'******************************

Code"G00"
Code "G53Z" & z
Code "G53X" & x & "Y" & y         ' Move to machine corrd position of tool change
While IsMoving()
Wend
Sleep(2000)


Call GotoToolDownPosition()	  ' Moves Z Axis Down to -2.5 to drop off old tool 


ActivateSignal(OutPut4)           ' Activate air cylinder for atc slide extend
Cnt1 = 0
While Not GetOEMLED(821)          ' Wait until end of travel signal is seen or timeout
   If Cnt1 > 1000 Then
   MsgBox(" Atc EXTEND timeout ")
   DoButton(3)   'Cancel Program
   End      ' Cancel Macro
   End If
Sleep(100)
Cnt1 = (Cnt1 +1)
Wend


ActivateSignal(OutPut5)           ' Activate PDB air cylinder to Release Tool
Cnt2 = 0
While Not GetOEMLED(823)          ' Wait until end of travel signal is seen or timeout
   If Cnt2 > 1000 Then
   MsgBox(" Atc EXTEND  timeout ")
   DoButton(3)    'Cancel Program
   End      ' Cancel Macro
   End If
Sleep(100)
Cnt2 = (Cnt2 +1)
Wend


Call GotoToolUpPosition()	  ' Moves Z Axis UP to -.6 to let carousel spin


ActivateSignal(OUTPUT7)		  ' Turn on Air Blast to clean tool
Sleep(2000)			  ' Two second wait
DeactivateSignal(OUTPUT7)	  ' Turning Air Blast OFF


SetDro(18, AtcRate)		  ' 18 is the feed rate DRO
Call MovePos(NewTool)		  ' The ATC moves to the new tool
While IsMoving()
Wend


Call GotoToolDownPosition()	  ' Moves Z Axis Down to -2.5 to pickup tool


DeactivateSignal(OutPut5)         ' PDB Grips new tool
Cnt3 =0
While Not  GetOEMLED(824)    	  ' wait until end of travel signal is seen
If Cnt3 > 1000 Then
   MsgBox(" Atc PDB Grip timeout ")
   DoButton(3)                    ' Cancel Program
   End                            ' Cancel Macro
   End If
Sleep(100)
Cnt3 = (Cnt3 +1)
Wend



DeactivateSignal(OutPut4)         ' Deactivate air cylinder for ATC slide Retract
Cnt4 = 0 
While Not GetOEMLED(822)          ' Wait until return to position signal is seen
If Cnt4 > 1000 Then
   MsgBox(" Atc RETRACT timeout ")
   DoButton(3)     ' Cancel Program
   End      ' Cancel Macro
   End If
Sleep(100)
Cnt4 = (Cnt4 +1)
Wend


SetOemDro(824,NewTool)              	    ' Update tool # dro
SetDro(18,Frate)             		    ' Set Feedrate back to programed rate   
Message" Tool Change Complete"


Sub GotoToolUpPosition
   MsgBox("Bring Z Up to TooUp Position " & ToolUpPosition)
   Code "G00 G53 Z" & ToolUpPosition
   While IsMoving()
     Sleep(10)
   Wend
End Sub


Sub GotoToolDownPosition
   MsgBox("Bring Z Down to ToolDown Position " & ToolDownPosition)
   Code "G00 G53 Z" & ToolDownPosition
   While IsMoving()
     Sleep(10)
   Wend
End Sub
  

Sub MovePos(ByVal ToolNumber As Integer)    ' This is the SUB routine for rotation

Select Case ToolNumber      		    ' Positions to each tool in B-axis
      Case  = 1
        Code "G00 G53 B0 "
      Case  = 2
        Code "G00 G53 B30 "
      Case  = 3
        Code "G00 G53 B60 "
      Case  = 4
        Code "G00 G53 B90 "
      Case  = 5
        Code "G00 G53 B120 "
      Case  = 6
        Code "G00 G53 B150 "
      Case  = 7
        Code "G00 G53 B180 "
      Case  = 8
        Code "G00 G53 B210 "
      Case  = 9
        Code "G00 G53 B240 "
      Case  = 10
        Code "G00 G53 B270 "
      Case  = 11
        Code "G00 G53 B300 "
      Case  = 12
        Code "G00 G53 B330 "   
End Select

End Sub     
