Ok, major changes below, my learned friend and mentor has enlightened me to a simple way by just comparing old and new tools, seems to make more sense now and needs no DRO's adding etc.
I have also made the last 3mm of move at F100 rather than G0 all the way so it gives the best chance of hitting the spot.
Sub Main()
If GetSelectedTool() = GetCurrentTool() Then Exit Sub ‘***Do nothing if current tool is called again
If GetOEMLED(1866) Then Exit Sub ***Ignore M6 calls LED
‘***Get the current G90/G91 state
CurrentAbsInc = GetOemLED(48)
‘***Get Axis Scale factors in use
XScale = GetOEMDRO(59)
YScale = GetOEMDRO(60)
ZScale = GetOEMDRO(61)
CScale = GetOEMDRO(64)
‘***Set All Axis Scales to 1
Call SetOEMDRO(59,1)
Call SetOEMDRO(60,1)
Call SetOEMDRO(61,1)
Call SetOEMDRO(64,1)
Sleep(250)
‘***Get old tool length
OldToolLength = roun(ToolLengthOffset)
‘***Set the requested tool to be the current tool
SetCurrentTool(GetSelectedTool())
‘***Get new tool length
NewToolLength = roun(ToolLengthOffset)
‘***Switch to absolute distance mode
Code "G90"
‘***Move Z axis to machine zero - fully retracted for tool change
Code "G53 G0 Z0”
While IsMoving()
Sleep(100)
Wend
'========================================================================================
' Start of Knee Positioning Code
'========================================================================================
‘Get the knee position in machine coordinates
CurrentKneePosition = roun(GetOEMDRO(88))
‘***Get the respective backlash clearance allowance for downwards knee moves
If GetOEMLED(801) Then ‘***On = English Measure INCH
ClearAllow = 0.125
Else ‘***Off = Metric Measure MM
ClearAllow = 3.0
End If
‘***Calculate the knee machine move value based on tool length difference
If OldToolLength > NewToolLength then ‘***We need to RAISE the knee
Message “Knee Will Lift ” & OldToolLength - NewToolLength ‘***Use this line for debugging
Code “G53 G0 C” & (CurrentKneePosition - (OldToolLength - NewToolLength)) + ClearAllow
While IsMoving ()
Sleep(100)
Wend
Code “G53 G1 C” & (CurrentKneePosition - (OldToolLength - NewToolLength)) & “ F100”
While IsMoving ()
Sleep(100)
Wend
End If
‘***If the knee is currently higher than it needs to be, we first
‘***move it down, to ensure the final move is always UP. This ensures
‘***backlash is taken out and provides more consistent positioning.
If OldToolLength < NewToolLength then ‘***We need to LOWER the knee
Message “Knee Will Lower ” & NewToolLength - OldToolLength ‘***Use this line for debugging
Code “G53 G0 C” & ((CurrentKneePosition + (NewToolLength - OldToolLength)) + ClearAllow
While IsMoving ()
Sleep(100)
Wend
Code “G53 G1 C” & (CurrentKneePosition + (NewToolLength - OldToolLength)) & “ F100”
While IsMoving ()
Sleep(100)
Wend
End If
If OldToolLength = NewToolLength then
Message “Knee Will Not Move” ‘***Use this line for debugging
End If
'========================================================================================
' End of Knee Positioning Code
'========================================================================================
‘***If G91 was in effect before then return to it
If CurrentAbsInc = 0 Then
Code "G91"
End If
‘***Put previous Axis Scale factors back
Call SetOEMDRO(59,XScale)
Call SetOEMDRO(60,YScale)
Call SetOEMDRO(61,ZScale)
Call SetOEMDRO(64,CScale)
Sleep(250)
End Sub