  ' tool change for VMC100 with 10 tool carousel    ' -----------------------------------------  ' output1 = override Z limit  ' output2 = spindle air valve  ' input2 = toolchanger clamped  ' input3 = toolchanger some tool position  ' input4 = toolchanger tool 1 position  ' -----------------------------------------    ' Variables  ' -----------------------------------------  ' wanted tool from Mach3  tool = GetSelectedTool()  ' current tool (0 if not known)  CurrentTool = GetCurrentTool()  ' position for tool change (+90.0mm)  Zposition = 90  ' spindle gear turns for one tool position change (in spindle full rev)  ToolRevDistance = 2  ' current spindle A axis position (in spindle full rev)  ToolRevAngleNow = 0  ' calculated movement of carousel (in tool positions! NOT revs)  ToolRevRequired = 0  ' single verify z axis number  VerifyZaxis = 2  VerifyAaxis = 3  ' -----------------------------------------  ' Check proper tool number    If tool < 1 Or tool > 10 Then    Message "ATC ERROR! New tool number not within 1-10!"    End  End If    If CurrentTool < 0 Or tool > 10 Then    Message "ATC ERROR! Current tool number not within 0-10!"    End  End If      ' -----------------------------------------  ' Check that spindle is stopped    DoSpinStop()       ' -----------------------------------------  ' Tool changer must be properly clamped    If Not IsActive(INPUT2) Then     Message "ATC ERROR! Toolchanger not initially clamped!"     End  End If    ' -----------------------------------------  ' Exit if correct tool already selected    If tool = CurrentTool Then    Message "ATC: Tool " & tool & " is already current tool."    Exit Sub  End If      ' -----------------------------------------  ' Home Z and A so position is definitely correct    Message "ATC: Homing Z and A"  ' Home A and Z   RefZ = 4  RefA = 8  RefCombination(RefZ + RefA)  While IsMoving     Sleep(100)  Wend  ' Home A a second time as sometimes first one doesn't work!!  Message "ATC: Homing A again"  RefCombination(RefA)  While IsMoving     Sleep(100)  Wend  Sleep(100)    ' -----------------------------------------  ' Proper rotary alignment for carousel gear    Message "ATC: Setting A rotary alignment"  ToolRevAngleNow = -0.025  Code "G53 G01 A" & ToolRevAngleNow & " F100"  While IsMoving    Sleep(100)  Wend   Sleep(100)    ' -----------------------------------------  ' override Z limit and run carousel up    Message "ATC: Running to tool change position Z" & Zposition  ' override Z limit to run carousel up  ActivateSignal(OUTPUT1)  Sleep(100)  ' run Z to change position  Code "G53 G01 Z" & Zposition & " F300"  While IsMoving     Sleep(100)  Wend  Sleep(100)  ' -----------------------------------------  ' run carousel to tool 1 (if no known tool position at start)    If CurrentTool = 0 Then    Message "ATC: Running carousel to tool 1"      While Not IsActive(INPUT4)      ToolRevAngleNow = ToolRevAngleNow + ToolRevDistance      Code "G53 G01 A" & ToolRevAngleNow & " F100"      While IsMoving        Sleep(100)      Wend       Sleep(100)      If IsActive(INPUT3) Then    	message "ATC ERROR! Tool carousel not at any tool position!"    	SetCurrentTool(0)    	End      End If    Wend    CurrentTool = 1  End If    ' -----------------------------------------  ' run carousel to wanted tool (if not current tool)    If Not CurrentTool = tool Then    Message "ATC: Running carousel to tool " & tool    ' movement required (in tool places! NOT REV!)    ToolRevRequired = (CurrentTool - tool)     ' optimize movement    If ToolRevRequired > 5 Then      ToolRevRequired = ToolRevRequired - 10    End If    If ToolRevRequired < -5 Then      ToolRevRequired = ToolRevRequired + 10    End If    ' move to new tool     ToolRevAngleNow = ToolRevAngleNow + ToolRevRequired * ToolRevDistance    Code "G53 G01 A" & ToolRevAngleNow & " F100"    While IsMoving      Sleep(100)    Wend     Sleep(100)    If IsActive(INPUT3) Then      message "ATC ERROR! Tool carousel not at any tool position!"      SetCurrentTool(0)      End    End If    End If  ' -----------------------------------------  ' Air blast  Message "ATC: Cleaning spindle with air"  ActivateSignal(OUTPUT2)  Sleep(1000)  DeActivateSignal(OUTPUT2)    ' -----------------------------------------  ' Run Z back down and enable Z limit  Message "ATC: Running Z back down"  ' run Z back down  Code "G53 G01 Z0 F300"  While IsMoving    Sleep(100)  Wend   Sleep(100)  ' Reactivate Z limit   DeactivateSignal(OUTPUT1)  ' -----------------------------------------  ' Tool changer must be properly clamped  If Not IsActive(INPUT2) Then     Message "ATC ERROR! Toolchanger not clamped!"     SetCurrentTool(0)     End  End If      ' -----------------------------------------  ' tool is changed    Message "ATC: Tool " & tool & " changed successfully!"  SetCurrentTool( tool )  ' end               