'Tool Zero Setting Macro

' Change the following three lines to suit your machine
PlateThickness =0.062	' Thickness of touch plate
RetractClearance = 0.500 '- PlateThickness ' Clearance above touch plate to retract to
ProbeFeed = 1		' Feedrate to use for probing
RetractHeight = 0       ' Make sure the var is 0
' Define some constants
ZDRO = 2		' Z Axis DRO
AbsoluteModeLED = 48	' Absolute Coordinate Mode LED
IncrementalModeLED = 49	' Incremental Coordinate Mode LED
FeedrateDRO = 818	' OEM code for feedrate DRO
ProbeLED = 825		' OEM code for probe input LED
ZTouchDRO = 2002	' OEM code for DRO that holds Z touch position
'Clear some Vars
SetVar(2002,0)          'clear the DRO that holds Z touch position
ZProbePos = 0		'clear the var that holds Z touch position

' Do nothing if probe is already grounded
If GetOemLed(ProbeLED) = 0 Then

   ' Wait a few seconds for user to get touchplate in place
   Code "G4 P01000"
   SetVar(ZTouchDRO,0)
   ' Save current feedrate
   CurrentFeed = GetOemDRO(FeedrateDRO)
   
   ' Save current coordinate mode
   AbsMode = GetOemLED(AbsoluteModeLED)

   ' Set absolute coordinate mode
   Code "G90"

   ' Zero Z DRO
   Call SetDro (ZDRO, 0.0000)

   ' Pause for DRO to update
   Code "G4 P01000"
'Code "F" & ProbeFeed
   ' Do the touch move
   Code "G31 Z -1.5 F" & ProbeFeed

   ' Wait for it to complete
   While IsMoving()
   Wend

   ' Delay a bit for ???
   Code "G4 P01000"

   ' Get the Z position where probe input triggered
   ZProbePos = GetVar(ZTouchDRO)
CODE "G4 P0100"
   ' Go back to touch position (G31 backs off after touch)
Code "G0 Z" & ZProbePos

   ' Wait for it to complete
   While IsMoving ()
   Wend
CODE "G4 P0100"
   ' Set Z DRO to touch plate thickness
   Call SetDro (ZDRO, PlateThickness)

   ' Pause for DRO to update
   Code "G4 P01000"

   ' Retract Z to SafeZ, if enabled, else to RetractClearance above plate
   'If(IsSafeZ() = 1) Then
  ' code "(HELP)"
  '    SafeZ = GetSafeZ
  '    If  SafeZ  > PlateThickness Then
   '      GotoSafeZ()
  '    End If
  ' Else
      RetractHeight =  RetractClearance  - PlateThickness 
      Code "G0 Z" & RetractHeight
   ' Wait for it to complete
   While IsMoving ()
   Wend
      Code "G4 P0100"
  ' End If
   
   ' Tell user we're done
   Code "(Z axis is now zeroed and at   )" &RetractHeight
   ' Reset feedrate to original value
   Code "F" & CurrentFeed
   
   ' Reset coordinate mode to original value
   If AbsMode = 0 Then
      Code "G91"
   End If
Else
   Code "(Z-Plate is grounded, check connection and try again)"
End If 

Exit Sub
%
                               
