'-------------------
'Josh Haywood I'll Do It Myself XYZ Probe Macro
'To probe Z-, X+, Y+
'https://www.youtube.com/illdoitmyself
'Note: This script is designed for metric units e.g. mm
'-------------------

'Allocate memory for variables
Dim Xrepos, Yrepos, Zprobeheight As Double, PlateHeight As Double, XPlateWidth As Double, XPosCurrent As Double, ProbeDiameter As Double, XZeroOffset As Double
Dim FeedCurrent, ZPosCurrent As Double, ZeroOffset As Double, YPlateWidth As Double, YPosCurrent As Double, YZeroOffset As Double

'Modify these for your measurements
Zprobeheight = 0 'Height to probe X and Y Indicated after Z is Zeroed
Xrepos = 30 'Distance to move X- before moving down for probe
Yrepos = 30 'Distance to move Y- before moving down for probe

'Variables to pull from DRO's
PlateHeight = getoemdro(1151) ' Get Plate Height (Darkscreen PlateHeight)
YPlateWidth = getoemdro(1152) 'Accurately measuerd plate wall width on Y Axis
XPlateWidth = getoemdro(1153) 'Accurately measuerd plate wall width on X Axis
ProbeDiameter = GetOemDro(1000) 'Get Probe diameter
FeedCurrent = GetOemDRO(818) 'Get the current settings

If GetOemLed (825) <> 0 Then 'Check to see if the probe is already grounded or faulty
   Code "(Probe plate is grounded, check connection and try again)"
Else


  ' ###### Zero DRO's ######
  Code "G92 X0 Y0 Z0"
  
  ' ###### Probe Z- ######
  ZPosCurrent = GetDro(2) 'Get current position
  Code "F100" 'slow feed rate to 100 mm/min

  Code "G31 Z" & (ZPosCurrent - 25) 'probe 25mm down
  While GetOemLed (825) = 0
    Sleep(1)
  Wend

  Sleep(250) 'Wait for DRO to update

  Call SetDro (2, PlateHeight) 'Set the Dro
  
  Sleep(250) 'Wait for DRO to update
  
  Code "G0 Z" & (GetDro(2) + Abs(10)) & " F400" 'Move off the plate 10mm
  While IsMoving()
  Wend

  Code "(Z Axis Zeroed for PlateHeight:" & PlateHeight & ")"

  '###### X Reposition ######
  Code "G0 X" & (GetDro(0) - Abs(Xrepos)) & " F400" 'Move left by Xrepos distance
  Code "G0 Z" & Zprobeheight & " F400"'Move Down to Zprobeheight
  While IsMoving()
  Wend

  '###### Probe X+ ######
  XPosCurrent = GetDro(0) 'Get current position
  Code "F100" 'slow feed rate to 100 mm/min

  Code "G31 X" & (XPosCurrent + 25) 'probe 25mm to the right
  While GetOemLed (825) = 0
    Sleep(1)
  Wend

  Sleep(250) 'Wait for DRO to update

  XZeroOffset = ((-1 * XPlateWidth) - (ProbeDiameter / 2)) 'Calculate X Zero
  Call SetDro (0, XZeroOffset) 'Set the Dro

  Sleep(250) 'Wait for DRO to update

  Code "G0 X" & (GetDro(0) - Abs(10)) & " F400" 'Move off the plate 10mm
  While IsMoving()
  Wend

  Code "(X Axis Zeroed for Platewall:" & XPlateWidth & " Tool Diameter:" & ProbeDiameter & ")"

  '###### Y Reposition ######
  Code "G0 Z" & (PlateHeight + 10) & " F400" 'Move Plateheight +10
  Code "G0 X10 Y" & (GetDro(1) - Abs(Yrepos)) & " F400" 'Move to Yrepos distance at X +10
  Code "G0 Z" & Zprobeheight & " F400" 'Move Down to Zprobeheight
  While IsMoving()
  Wend
  Sleep(250) 'Wait for DRO to update

  '###### Probe Y+ ######
  YPosCurrent = GetDro(1) 'Get current position
  Code "F100" 'slow feed rate to 100 mm/min

  Code "G31 Y" & (YPosCurrent + 25) 'probe 25mm to the right
  While GetOemLed (825) = 0
    Sleep(1)
  Wend

  Sleep(250) 'Wait for DRO to update

  YZeroOffset = ((-1 * YPlateWidth) - (ProbeDiameter / 2)) 'Calculate Y Zero
  Call SetDro (1, YZeroOffset) 'Set the Dro

  Sleep(250) 'Wait for DRO to update

  Code "G0 Y" & (GetDro(1) - Abs(10)) & " F400" 'Move off the plate 10mm
  While IsMoving()
  Wend

  Code "(Y Axis Zeroed for Platewall:" & YPlateWidth & " Tool Diameter:" & ProbeDiameter & ")"

  Code "G0 Z" & (PlateHeight + 10) & " F400" 'Move Plateheight +10
  Code "G0 X0 Y0 F400" 'Move to X0 Y0 to show result
  Code "F" & FeedCurrent 'Restore Feedrate
End If
  
 
