Code:
'Richards Toolchanger Macro Rev 1.5
'C Home offset in Homing is 1.0860
'Mach General Config - Tool Selections Persistent must be checked so tools are remembered.
'User DRO's must be setup in order to work
'ATC has 10 Slots, any Tool # from the tool table can be assigned to any slot
'If the New Tool is not assigned to a Slot then a manual change will occur this way
'Old Tool Has a Slot Assigned and NewTool has a slot assigned
' The Total Automatic ToolChange
'Old Tool is assign but New tool is not assigned
' Then Old Tool is put away, Head is moved up Tray is moved out of way, and you manually insert tool
'Old Tool is NOT assigned but New Tool is assigned
' Then Toolchange position, Msg to remove old tool, then gets new tool from tray
'Neither tool assigned
' then Tool Change Position, a message stating to remove and install a new tool
'Rev 4 - Added Timeout to Slide instead of Dwell
'Rev 5 - Added Options for Debug Message, Return to Previous Position after change, Move XY to CHange Position
'Going to convert to degrees, then use an Offset from Home to Position 1
'Note Z Values are Negative since Z Zeroes at Home at Top
'Note - All Values are Machine Coordinates
'ToolDown = -2.393 'Height for Spindle to sit down on Tool
'*************************************************************
' Declarations
'*************************************************************
'Constants
Const Debug = 0 'If On = 1 then write messages to Mach Error Log with small delay
Const RetToPrevPos = 0 'Return to Previous position after change
Const MoveXYToChangePos = 0 'Move X and Y to Change Position before CHange
'Safety Options
Const VerifySlide = 1 'Verifies Slide Limit Switches 0 for Testing 1 for running on machine
Const VerifyOffsets = 1 'Allows you to touch tool down and zero after toolchange if no length offset in table
'Z Down Move
Const Fast = 0
Const Slow = 1
'For Tool Sequences
Const Manual = 0
Const AutoMatic = 1
Const NoTool = 2
Const MaxTools = 10
Const TrayStart = 2010
'Last Error Constants - Took out for now
Const Okay = 0
Const ExtendFailed = 1
Const RetractFailed = 2
Const NotHomed = 3
Const NoOffset = 4
Const SameTool = 5
'OutPuts and Inputs Assignments
'PDB Output20
'Slide OutPut2
'Extend Input2
'Retract Input3
'Variables
Dim OldXPos As Double
Dim OldYPos As Double
Dim OldTool As Integer
Dim NewTool As Integer
Dim OrigFeedRate As Double
Dim ToolChangeX As Double
Dim ToolChangeY As Double
Dim ToolChangeZ As Double
Dim ToolUp As Double
Dim ToolDown As Double
Dim ToolDownFeed As Double
Dim SlideTimeOut As Double
Dim PDBDwell As Double
Dim SlotArray (1 To MaxTools) As Integer
Dim OldSlot As Integer
Dim NewSlot As Integer
Dim I As Integer 'General Purpose Work Var counter....
Dim Ret As Integer 'Function Result Var
Dim NewOffset As Double
Dim LastOp As Integer 'Holds Last Tool Change Operation Sequence
'*************************************************************
' Main Sub
'*************************************************************
Sub Main()
'Define variables
Call ClearUserLEDs()
OldTool = GetCurrentTool()
NewTool = GetSelectedTool()
'Make sure machine is referenced
If (GetOemLed(807) Or GetOemLed(808) Or GetOemLed(809)) Or GetOemLed(812) Then
MyMsg("Please Home Machine before Tool Change")
DoButton(3) 'Cycle Stop
Exit Sub
End If
MyMsg("Tool Change Initiated Old Tool #" & OldTool &" New Tool #" & NewTool)
'If tools are the same then exit
If NewTool = OldTool Then
MyMsg("New Tool and Old Tool are Same - No Action Requested")
Exit Sub
End If
Message("********** Tool Change Start ********************")
'Finds out what tools are in what slots
Call LoadTools()
'Get Original Settings and put back when done
OrigFeedRate = GetOEMDRO(818)
ToolChangeX = GetOemDRO(1200)
ToolChangeY = GetOemDRO(1201)
ToolChangeZ = GetOemDRO(1202)
'Get ATC Control DROs
ToolUp = GetUserDRO(2000)
ToolDown = GetUserDRO(2001)
ToolDownFeed = GetUserDRO(2002)
SlideTimeout = GetUserDRO(2003)
PDBDwell = GetUserDRO(2004)
OldSlot = GetUserDRO(2009) 'This holds the position tool changer is in
'Get axis positions so we have them if we want to put stuff back
OldXPos = GetToolChangeStart( 0 )
OldYPos = GetToolChangeStart( 1 )
Call MoveToChangePosition()
'*************************************************************
' Handle Old Tool
'*************************************************************
If OldTool > 0 Then 'No sense in putting old tool back if it's zero
If ExistInRack(OldTool) Then 'Is the old tool in the tool rack
SetUserLED(2002,1)
Call RotateToSlot(OldTool) 'Line up Slot for OldTool
SetUserLED(2001,1)
Call GoToToolDownPosition(FAST) 'Bring Z down to unload tool
SetUserLED(2003,1)
If ExtendSlide()= 0 Then 'Extend Slide to Get Old Tool
Call AllDone() 'Slide Failure
Exit Sub
End If
SetUserLED(2004,1)
Call PDBUnClamp() 'UnClamp From Old Tool
SetUserLED(2004,1)
Call GoToToolUpPosition() 'Get the Head Up so Old tool can be removed
LastOp = Automatic
Else
'Tool Doesn't Exist in Rack so It will be Manually Removed
MsgBox("Please Remove Tool # " & OldTool & " from Spindle and then Press Ok")
MyMsg("Old Tool # " & OldTool & " was manually removed")
LastOp = Manual
End If
Else
MyMsg("No Old Tool to Return - Old Tool # " & OldTool)
LastOp = NoTool
End If
'*************************************************************
' Handle New Tool
'*************************************************************
If NewTool > 0 Then 'If Newtool = 0 then Skip getting a new tool
If ExistInRack(NewTool) Then
MyMsg("Rotate ATC to New Tool #" & NewTool)
SetUserLED(2006,1)
Call RotateToSlot(NewTool)
While IsMoving()
Sleep(10)
Wend
If LastOp <> Automatic Then
'Head is in the Up Position, No Tool in the Spindle, Slide Not Extended, PDW not Released
'Slot is in the proper Position
SetUserLED(2005,1)
Call GoToToolUpPosition() 'Make sure Head is High out of way (****Should Already Be There!!!!)
SetUserLED(2003,1)
If ExtendSlide()= 0 Then 'Extend Slide to put new tool under spindle
Call AllDone() 'Slide Failure
Exit Sub
End If
SetUserLED(2004,1)
Call PDBUnClamp() 'Open Collet for New Tool
End If
'This has to be done for All Modes
SetUserLED(2007,1)
Call GoToToolDownPosition(SLOW) 'Let Head Slowly come down on to tool
SetUserLED(2008,1)
Call PDBClamp() 'Clamp the tool
SetUserLED(2009,1)
If RetractSlide()=0 Then 'Retract the Slide
Call AllDone()
Exit Sub
End If
SetUserLED(2005,1)
Call GoToToolUpPosition() 'Get Head Up
Else
'New Tool isnt in Rack so Install it Manually
'Slide Extended, at Old Slot, Head up, Collet Open
If LastOp = Automatic Then
SetUserLED(2008,1)
Call PDBClamp() 'Close Collet so it so it can manually be opened
SetUserLED(2009,1)
If RetractSlide()=0 Then 'Get Slide out of Way of Manual Tool Insert
Call AllDone()
Exit Sub
End If
End If
SetUserLED(2005,1)
Call GoToToolUpPosition() 'Have Spindle up so there is plenty of room (****Should Already Be There!!!!)
MsgBox("Please Insert Tool # " & NewTool & " into Spindle and then Press Ok")
MyMsg("New Tool # " & NewTool & " was manually Inserted")
End If
Else
MyMsg("No New Tool - Leaving Spindle Empty")
If LastOp = Automatic Then
SetUserLED(2008,1)
Call PDBClamp() 'Close Collet so it so it can manually be opened
SetUserLED(2009,1)
If RetractSlide()=0 Then 'Get Slide out of Way of Manual Tool Insert
Call AllDone()
Exit Sub
End If
End If
SetUserLED(2005,1)
Call GoToToolUpPosition() 'Have Spindle up so there is plenty of room (****Should Already Be There!!!!)
End If
'*************************************************************
' Wrap Things up
'*************************************************************
SetCurrentTool(NewTool)
Call AllDone()
'Check Offsets
If VerifyOffsets = 1 then
If NewTool <> 0 Then
NewOffset = GetToolParam(NewTool,2)
If NewOffset <= 0 Then
DoButton(3) 'Cycle Stop
MsgBox("The Tool Selected does not have Length Offsets, Please touch off your tool and zero the axis then Cycle Start")
End If
End If
End If
While IsMoving()
Sleep(10)
Wend
Call ClearUserLEDs()
Message("********** Tool Change End ********************")
MyMsg("")
End Sub
'*************************************************************
' Sub Routines
'*************************************************************
'Puts everything back where it was
Sub AllDone
Call GoToToolUpPosition()
Code "G90" 'Make sure we in Absolute Mode
If MoveXYToChangePos = 1 AND RetToPrevPos = 1 then
MyMsg("Putting X and Y back where they were")
Code "G00 X" & OldXPos & " Y" & OldYPos 'Move back to we were before tool change
While IsMoving()
Sleep(10)
Wend
End If
SetOEMDro(818,OrigFeedRate) 'Set original feedrate back
End Sub
'Rotates Tray to the Slot that the requested tool is in
Sub RotateToSlot(ByVal ToolNum As Integer)
Dim ToolSlot as Integer
ToolSlot = GetSlotByToolNum(ToolNum)
MyMsg("Rotate ATC to Slot #" & ToolSlot & " For Tool #" & ToolNum)
Call MovePos(ToolSlot)
While IsMoving()
Sleep(10)
Wend
SetUserDRO(2009,ToolSlot)
While IsMoving()
Sleep(10)
Wend
End Sub
'I left this in so I can fine tune positions if needed
'Can be simplified into a single line
Sub MovePos(ByVal ToolNumber As Integer)
Select Case ToolNumber 'Positions to each tool in C-axis
Case Is = 1
Code "G00 G53 C1.0"
Case Is = 2
Code "G00 G53 C2.0"
Case Is = 3
Code "G00 G53 C3.0"
Case Is = 4
Code "G00 G53 C4.0"
Case Is = 5
Code "G00 G53 C5.0"
Case Is = 6
Code "G00 G53 C6.0"
Case Is = 7
Code "G00 G53 C7.0"
Case Is = 8
Code "G00 G53 C8.0"
Case Is = 9
Code "G00 G53 C9.0"
Case Is = 10
Code "G00 G53 C10.0"
End Select
End Sub
'This Sub Loads the Tool Tray with what Tool Number is in What slot
Sub LoadTools
For I = 1 To MaxTools
SlotArray(I) = GetUserDRO(TrayStart +(I-1))
Next I
End Sub
'This Function keeps what tool# is in what slot#
'When A request for a tool# is made, call this function to get the slot.
Function GetSlotByToolNum (ByVal ToolNumber As Integer) As Integer
For I = 1 To MaxTools
If SlotArray(I) = ToolNumber Then
GetSlotByToolNum = I
Exit Sub
End If
Next I
GetSlotByToolNum = 0 'If we made it this far the tool wasn't found
End Function
'This Sub Checks a Slot to make sure it's not Empty - Not used
Function IsSlotEmpty (ByVal SlotNumber As Integer) As Boolean
If SlotArray(SlotNumber) >= 1 then
IsSlotEmpty = 0
Else
IsSlotEmpty = 1
End If
End Function
'Checks if a Tool# is in our rack
Function ExistInRack (ByVal ToolNumber As Integer) As Boolean
If ToolNumber <> 0 Then
For I = 1 To MaxTools
If SlotArray(I) = ToolNumber Then
ExistInRack = 1
Exit Sub
End If
Next I
End If
ExistInRack = 0 'If we made it this far the tool wasn't found
End Function
Function ExtendSlide As Boolean
MyMsg("Extending ATC Slide ")
ActivateSignal(Output2) 'Extend Slide ATC to Tool
If VerifySlide = 1 Then
i = 0
While Not IsActive(Input2)
Sleep 100
i = i + 1
If i > (SlideTimeOut*10) Then
MyMsg( "ERROR - Extend Slide Timeout Error")
Message( "ERROR - Extend Slide Timeout Error")
ExtendSlide = 0
Sleep 100
End 'Stop Everything
End If
Wend
MyMsg( "Slide In Extended Position")
ExtendSlide = 1
Else
'ByPass Verify
ExtendSlide = 1
End If
End Function
Function RetractSlide As Boolean
MyMsg("Retracting ATC Slide ")
DeActivateSignal(Output2) 'Retract Slide ATC to Tool
If VerifySlide = 1 Then
i = 0
While Not IsActive(Input3)
Sleep 100
i = i + 1
If i > (SlideTimeOut*10) Then
MyMsg("ERROR - Retract Slide Timeout Error")
Message("ERROR - Retract Slide Timeout Error")
RetractSlide = 0
Sleep 100
End
Else
MyMsg("Slide In Extended Position")
RetractSlide = 1
End If
Wend
Else
'ByPass Verify
RetractSlide = 1
End If
End Function
Sub PDBUnClamp
MyMsg("PDB UnClamp")
ActivateSignal(Output20) 'Send signal to release PDB.
Code "G4 P" & PDBDwell 'Wait for PDB
While IsMoving()
Sleep(10)
Wend
End Sub
Sub PDBClamp
MyMsg("PDB Clamp")
DeActivateSignal(Output20) 'Send signal to grip PDB.
Code "G4 P" & PDBDwell 'Wait for PDB
While IsMoving()
Sleep(10)
Wend
End Sub
Sub MoveToChangePosition
MyMsg( "Moving Z to Tool Change Position First")
Code "G00 G53 Z" &ToolChangeZ
While IsMoving()
Sleep(10)
Wend
If MoveXYToChangePos = 1 then
MyMsg( "Moving X & Y to Tool Change Position")
Code "G00 G53 X" &ToolChangeX &" Y" &ToolChangeY
While IsMoving()
Sleep(10)
Wend
SetUserLED(2000,1)
End If
End Sub
Sub GoToToolUpPosition
MyMsg("Bring Z Up to Toolup Position " & ToolUp)
Code "G00 G53 Z" & ToolUp 'The Z-axis up
While IsMoving()
Sleep(10)
Wend
End Sub
Sub GoToToolDownPosition (ByVal Speed As Integer)
If Speed = Slow Then
MyMsg("Bring Z down to ToolDown " & ToolDown & " at Feed " &ToolDownFeed)
Code "G01 G53 F" & ToolDownFeed & " Z" & ToolDown 'Z-axis moves down to where it touches tool
Else
MyMsg ("Bring Z down to ToolDown " & ToolDown)
Code "G0 G53 Z" & ToolDown 'Z-axis moves down to where it touches tool
End If
While IsMoving()
Sleep(10)
Wend
End Sub
Sub MyMsg(StrName as String)
SetUserLabel(1,StrName)
If Debug then
Message(StrName)
Code "G4 P0.1"
While IsMoving()
Sleep(10)
Wend
End If
End Sub
Sub ClearUserLEDs
SetUserLED(2000,0)
SetUserLED(2001,0)
SetUserLED(2002,0)
SetUserLED(2003,0)
SetUserLED(2004,0)
SetUserLED(2005,0)
SetUserLED(2006,0)
SetUserLED(2007,0)
SetUserLED(2008,0)
SetUserLED(2009,0)
End Sub