'MACRO incremental probing +Y, stepover +X
'
'this macro runs probing code and checks for edge losing
'retracts probe after a hit by stepover distance

'DRO#s:
'Stepover:	1002	' probing resolution
'Probing FR: 	1003
'Retact FR: 	1004
'Probe Travel: 	1005	' distance to travel until trip must occur (G31-parameter)
'Xlength: 	1006	' when stepping over x-axis: maximum distance/ edge length
'Ylength: 	1007	' ditto for Y
'TestY1		1010	' for debugging, this should display the Ypos1 value: Y before G31
'TestY2		1011	' ditto: Y after G31
'TestOvertravel	1012	' calculeted cancellation-criteria

Option Explicit
Dim Stepover
Stepover = getoemdro(1002) ' e.g. 5mm

Dim N
N = 1 + Int(getoemdro(1007) / Stepover)

Dim Ypos1, Ypos2, ProbeTravel 
ProbeTravel = getoemdro(1005) * +1 ' here


Dim ProbFR, RetrFR
ProbFR = getoemdro(1003)
RetrFR = getoemdro(1004)


Dim RetrcDist1, RetrcDist2, OverTravel, codestr



RetrcDist1 = Stepover * -1 ' here
' normal retract

RetrcDist2 = ProbeTravel * -1
' lost edge retract

code "G71 G91"
Ypos1 = opendigfile()  ' may be this function could be called without  variable= ??

While N>0		' this is the main loop
  Ypos1= getdro(1)
  setOEMDRO(1010, Ypos1)
  ' remember starting position
  code "F"&ProbFR  
  code "G91 G31 Y"&ProbeTravel
  'do probing

  While IsMoving()
    Sleep 100
  Wend

  ' probe tripped or traveld complete distance

  Ypos2= getdro(1)
  setOEMDRO(1011, Ypos2)
  ' notice final position

  ' test for lost edge
 
  OverTravel=Abs(Ypos2-Ypos1)-Abs(ProbeTravel)+0.2 	' the 0.2 is a margin only
  setOEMDRO(1012,OverTravel )
  If Overtravel < 0  Then
  ' next sample
  Else
  ' lost edge
    code "G00 Y"&RetrcDist2	' linear retract
    N=0 				' terminate loop
  End If

  ' retract
    code "F" & RetrFR		' feedrate for retract
  
  ' extended retract ?
  If getoemled(1168) Then code "G01 Y" & RetrcDist1	' add some linear retract 
  
  ' normal circular retract
  codestr = "G03 X" &(+1*Stepover) & "Y"&RetrcDist1 & "I"&(+1*Stepover)
  ' here
  
  If N>1 Then 	
    code codestr
  Else
    If N>0 Then code "G00 Y"&RetrcDist1	' last retract only linear
  End If
  
  N = N-1
Wend

closedigfile()
code "G90"
code "M30"

End   
