Machsupport Forum

Mach Discussion => Mach4 General Discussion => Topic started by: secretspy711 on December 28, 2018, 10:17:33 PM

Title: M6 tool change, how to update Z axis with new offset
Post by: secretspy711 on December 28, 2018, 10:17:33 PM
I am trying to get the following script to work, which is based on the one from here: https://www.youtube.com/watch?v=6-4JkDv9AgM (https://www.youtube.com/watch?v=6-4JkDv9AgM).
I am having some trouble figuring out how to get it to actually update the Z axis DRO using the difference between the current and selected tool length offsets.  Can someone please help?

Code: [Select]
local inst = mc.mcGetInstance()

----------------------------------------------------------------------------------
--change lines here to either auto rapid, or manually jog to a tool change position
----------------------------------------------------------------------------------
--Manual Lines. Uncomment line below to allow you to manually jog to a tool change position.
--local MyChoice = wx.wxMessageBox("Click OK, \nThen Jog to A Safe Tool Change Position,\nInsert New tool,\nThen Click Cycle Start.","Click OK to continue" , 16)
---------------------------------------------------------------------------------
--Auto Lines.  Uncomment both lines below (and comment out local MyChoice line above) to automatically move to tool change position.
--Edit to include the machine coordinate values of that tool change position.

--AUTO LINES
mc.mcCntlGcodeExecuteWait(inst, "G90 G53 G0 Z0")--Move Z all the way up.
mc.mcCntlGcodeExecuteWait(inst, "G90 G53 G0 X-16 Y-12")--Move to X, Y Coords for tool change position.
mc.mcCntlSetLastError(inst, 'Now in Tool Change Position. Hit Cycle Start!')
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
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)
local currenttool = mc.mcToolGetCurrent(inst)
local currenttoollen = mc.mcToolGetData(inst, mc.MTOOL_MILL_HEIGHT, currenttool)
   
if selectedtool == currenttool then
mc.mcCntlSetLastError(inst, "Current tool == Selected tool so there is nothing to do")
else
wx.wxMessageBox("1. Change to tool " .. tostring(selectedtool) .. "\n2. Then press Cycle Start to perform auto TLO" )
mc.mcCntlToolChangeManual(inst, true);
mc.mcCntlSetLastError(inst, "Current tool == " .. tostring(selectedtool) .. "   Previous Tool == " .. tostring(currenttool))
mc.mcToolSetCurrent(inst, selectedtool)

local MyChoice =
--wx.wxMessageBox("Click Ok to Begin Probing the New Tool","Click OK to continue" , 16)
mc.mcCntlSetLastError(inst, "Probing in Progress!")
mc.mcCntlGcodeExecuteWait(inst, " G90 G53 G0 X-.4 Y-.54")--go to TLO position
mc.mcCntlGcodeExecuteWait(inst, " G91 G1 Z-3 F30")--go down a little to save time.
mc.mcCntlGcodeExecuteWait(inst, " G91 G31 Z-2 F5")--probe the new tool fast
mc.mcCntlGcodeExecuteWait(inst, " G91 G1 Z.125 F30")--pull off the touch pad a little
mc.mcCntlGcodeExecuteWait(inst, " G91 G31 Z-.135 F2")--probe the new tool slow


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
mc.mcCntlGcodeExecuteWait(inst, "G00 G53 Z0 ")--Retract
 
local NewOffset = probedz
mc.mcToolSetData(inst, mc.MTOOL_MILL_HEIGHT, selectedtool, NewOffset)
mc.mcCntlSetLastError(inst, string.format("Auto tool setting complete, Offset = %.4f", NewOffset))
wx.wxMessageBox("Toolchange Complete.\nTLO Set")
    end

end

if (mc.mcInEditor() == 1) then
m6()
end
Title: Re: M6 tool change, how to update Z axis with new offset
Post by: Dusty91 on December 31, 2018, 08:17:23 PM
My script is a little different but may be suitable for your needs. This does not have the manual positioning and makes use of spring plunger type tool probe. If you have a rigid tool probe then you will need to reduce the fast probe speed to avoid tool damage due to overtravel. I've added some comments to the code to describe the various lines.
This method keeps all the values of the tool table at zero. This means you are less likely to have issues when you set Z0 on a new part and forget to zero the table before hand or forget to enable tool length compensation.

Code: [Select]
function m6()

    local inst = mc.mcGetInstance();
    local selectedtool = mc.mcToolGetSelected(inst) --Variable for selected tool #
    local currenttool = mc.mcToolGetCurrent(inst) --Variable for current tool #

    if selectedtool == currenttool then --Compare tool numbers to see if tool change is required
    return
    mc.mcCntlSetLastError(inst, "Tool Change Not Required")
else
    mc.mcCntlGcodeExecuteWait(inst, "G00")
    mc.mcCntlGcodeExecuteWait(inst, "G49") --Cancel tool length compensation if active
    mc.mcCntlGcodeExecuteWait(inst, "G91 G28 G0 Z0") --Raises Z-axis to home position
    mc.mcCntlGcodeExecuteWait(inst, "G90 G53 G0 X1.8810 Y25.2330")         --Goes to probe postion in machine coordinates
mc.mcCntlGcodeExecuteWait(inst, "G91 G31 Z-7.0 F20") --Probes current tool at fast speed
mc.mcCntlGcodeExecuteWait(inst, "G91 G0 Z+0.1") --Retract from the probe
mc.mcCntlGcodeExecuteWait(inst, "G91 G31 Z-0.15 F1") --Probes at slow speed
local measure_1 = mc.mcAxisGetPos(inst,2) --Saves probed Z position

mc.mcCntlGcodeExecuteWait(inst, "G91 G0 Z+0.1") --Retracts from the probe
mc.mcCntlGcodeExecuteWait(inst, "G91 G31 Z-0.15 F1") --Probes at slow speed
local measure_2 = mc.mcAxisGetPos(inst,2) --Saves probed Z position

local measured_average = (measure_1 + measure_2)/2 --Averages two probed values

mc.mcCntlGcodeExecuteWait(inst, "G00")
    mc.mcCntlGcodeExecuteWait(inst, "G91 G28 G0 Z0") --Raises Z-axis to home position
mc.mcCntlGcodeExecuteWait(inst, "G91 G28 G0 X0 Y0") --Send spindle to XY home position

wx.wxMessageBox("Change to Tool "..selectedtool.." and Press OK to Continue") --Prompt to change tool. Press OK only after tool has been exchanged.

mc.mcCntlGcodeExecuteWait(inst, "G00")
    mc.mcCntlGcodeExecuteWait(inst, "G49") --Cancel tool length compensation if active
    mc.mcCntlGcodeExecuteWait(inst, "G91 G28 G0 Z0") --Raises Z-axis to home position
    mc.mcCntlGcodeExecuteWait(inst, "G90 G53 G0 X1.8810 Y25.2330")         --Goes to probe postion in machine coordinates
mc.mcCntlGcodeExecuteWait(inst, "G91 G31 Z-7.0 F20") --Probes new tool at fast speed

mc.mcCntlGcodeExecuteWait(inst, "G91 G0 Z+0.1") --Retracts from the probe
mc.mcCntlGcodeExecuteWait(inst, "G91 G31 Z-0.15 F1") --Probes at slow speed
mc.mcAxisSetPos(inst,2,measured_average) --Sets current Z postion to saved position from previous tool

mc.mcCntlGcodeExecuteWait(inst, "G00")
mc.mcCntlGcodeExecuteWait(inst, "G91 G28 G0 Z0") --Raises Z-axis to home position
mc.mcToolSetCurrent(inst, selectedtool) --Update current tool DRO
mc.mcCntlGcodeExecuteWait(inst, "G90") --Switch back to absolute mode
mc.mcCntlSetLastError(inst, "Tool Change Complete")

--mc.mcCntlToolChangeManual(inst, true); --Uncomment to require Cycle Start press to resume G-code
    end
   
end

if (mc.mcInEditor() == 1) then
 m6()
end
Title: Re: M6 tool change, how to update Z axis with new offset
Post by: secretspy711 on January 01, 2019, 02:04:02 PM
Thank you, this is exactly what I needed.

Without tool holders, I think you are right to keep the tool table values at zero.  It doesn't really make any sense to populate them when they will be different every time.

The tool probe I made has some compliance, so probing fast isn't a problem, but the touch surface is bare aluminum which is too soft and probing too fast causes marks on it.  I either need to anodize that surface, or perhaps make one from steel.
Title: Re: M6 tool change, how to update Z axis with new offset
Post by: Dusty91 on January 01, 2019, 02:51:54 PM
Happy to help. I use one of these and have been pleased with the performance. Plus it has an overtravel switch that I programmed to the E-stop in case the probing portion doesn't register.
https://www.amazon.com/Automatic-Normally-Blowing-Setting-Presetter/dp/B01MYGDU3K/ref=sr_1_46?ie=UTF8&qid=1546371798&sr=8-46&keywords=cnc+tool+probe
Title: Re: M6 tool change, how to update Z axis with new offset
Post by: secretspy711 on January 01, 2019, 05:31:04 PM
I almost got that one but it's a little too tall for my application.