This seems to be working with the on screen button "Auto Tool Zero" I made where there was a blank button in my screen set. It is calling out the function AutoToolZero() when you click it.
This code is in the Screen Load Script in the functions area section I made for my mill.
-----Auto Tool Zero button------
function AutoToolZero()
local posmode = mc.mcCntlGetPoundVar(inst, mc.SV_MOD_GROUP_3) --get the current mode so we can return to it when macro ends
--local selectedtool = mc.mcToolGetSelected(inst) --ERASE THIS LINE IF NOT NEEDED
local currenttool = mc.mcToolGetCurrent(inst)
--Get positions before moving to do tool change
local valX, rc = mc.mcAxisGetMachinePos(inst, mc.X_AXIS) --Get the position of the X axis in Machine Position
local valY, rc = mc.mcAxisGetMachinePos(inst, mc.Y_AXIS) --Get the position of the Y axis in Machine Position
local valZ, rc = mc.mcAxisGetMachinePos(inst, mc.Z_AXIS) --Get the position of the Z axis in Machine Position
rc = mc.mcCntlGcodeExecuteWait(inst, "M05") --Turns off the spindle (includes decel time)
rc = mc.mcCntlGcodeExecuteWait(inst, "G90 G53 G0 Z0.0000") --Move the Z to Home
rc = mc.mcCntlGcodeExecuteWait(inst, "G90 G53 G0 X19.5300 Y14.4500") --Move the the X, Y Coords to to the touch pad location.
rc = mc.mcCntlSetLastError(inst, 'Now in Tool Measuring Position')
local MyChoice = wx.wxMessageBox("Click Ok to Begin Probing the New Tool","Click OK to continue" , 16)
if (MyChoice == 16) then
rc = mc.mcCntlSetLastError(inst, 'Auto Tool Zero was cancelled.')
mc.mcCntlGcodeExecuteWait(inst, "G90 G53 G0 Z0.0000 F15.0") --Retract back to Z home
mc.mcCntlGcodeExecuteWait(inst, "G90 G53 G0 X" .. tonumber(valX) .. "Y" .. tonumber(valY)) --Move back to X & Y initial location
return
elseif (MyChoice == 4) then
mc.mcCntlSetLastError(inst, "Probing in Progress!")
mc.mcCntlGcodeExecuteWait(inst, "G90 G31.1 Z-5. F5.0") --probe the new tool
mc.mcCntlGcodeExecuteWait(inst, "G90 G53 G0 Z0.0000 F15.0") --Retract back to Z home
mc.mcCntlGcodeExecuteWait(inst, "G90 G53 G0 X" .. tonumber(valX) .. "Y" .. tonumber(valY)) --Move back to X & Y initial location
local probedz = mc.mcCntlGetPoundVar(inst, mc.SV_PROBE_POS_Z) -- Z Probe position in Machine coords
mc.mcCntlGcodeExecute(inst, string.format('G ' .. posmode))--return to pre macro mode G90, or G91
local NewOffset = probedz
mc.mcToolSetData(inst, mc.MTOOL_MILL_HEIGHT, currenttool, NewOffset)
mc.mcCntlSetLastError(inst, string.format("Auto tool setting complete, Offset = %.4f", NewOffset))
wx.wxMessageBox("Toolchange Complete.\nTLO Set")
end
end
Please feel free to critique.
I do think I want to add a data entry field on the screen to enter the height of the Tool Height Setter (Probe G31.1) and then utilize that in an equation to get proper offsets.....I am still trying to figure out how all this works with the 3D probe as tool 1....