--[[ AUTO TOOL ZERO FUNCTION Author: GB Ward, Create! 2015 Rev: 0.1 Date: 4/25/15 Mach4 4.0.1, Build 2336 License: Public Domain References: Mach4 Core API Version 1.01 (C:\Mach4Hobby\Docs\Mach4CoreAPI.chm) Mach4 CNC Controller Lua Scripting Guide Newfangled Solutions 2014 http://www.machsupport.com/wp-content/uploads/2014/05/Mach4%20Scripting%20Manual.pdf "Motion plugin probing procedure." (C:\Mach4Hobby\Docs\probing.txt) Down and Dirty "mcLua scripting, quick ref guide" poppabear, Mach4 English Forums, under "Mach4 Toolbox" board. Pound Var List - poppabear, Mach4 Forums http://www.machsupport.com/forum/index.php?action=dlattach;topic=27396.0;attach=38035 McLua mc scripting reference - poppabear, Mach4 Forums http://www.machsupport.com/forum/index.php?action=dlattach;topic=27141.0;attach=37937 ]] function m10() local mInst = 0; --Sets current controller instance to 0, if running multiple controller instances, this will need to change local rc = 0; --Clear rc local hSig = 0; --Clear hSig local inst = mc.mcGetInstance(mInst); --Captures current instance of Mach 4 --SETUP OF VARIABLES local probe = mc.ISIG_PROBE; --Set input here, mc.; input needs to be mapped in Mach4 local strikePlateThickness = 0.060; --What is the thickness of the strikeplate, or how much do you want to offset 0? --Function to check to see if probe already grounded function checkProbe () hSig, rc = mc.mcSignalGetHandle(inst, probe); --Load hReg with probe state state = mc.mcSignalGetState(hSig); --Get Signal State if (state == 1) then --if hReg true ('if probe is activated then') return true else return false end end --checkProbe() --BEGIN --Get current Feed rate, G90/G91, G0/G1 states to return to later. --Mach4 stores these variables as 'Pound Variables' --See Mach4 Lua Scripting Manual Page 23 local CurrFeedRate = mc.mcCntlGetPoundVar(inst, 2134); --Gets current Feed Rate, #var 2134 local CurrFeedMode = mc.mcCntlGetPoundVar(inst, 4001); --Gets current G0/G1 state, #var 4001 local CurrPositionMode = mc.mcCntlGetPoundVar(inst, 4003); --Gets current G90/G91 state, #var 4003 local zProbeStrikePos = 0; --clear variable if checkProbe() then --Check probe status mc.mcCntlSetLastError(inst, "ERROR: Probe is already triggered. Cannot continue."); do return end --"do return end" stops program. Not sure how this works end --check if probe is grounded mc.mcAxisSetPos(inst, 2, 0); --"Zero Z" (0 is x, 1 y, and so on) mc.mcCntlGcodeExecuteWait(inst, 'G01 G90 G31 Z-4 F4'); --G31. Machine will stop on probe strike or go to Z-4, store in #var 5063 local zProbeStrikePos = mc.mcCntlGetPoundVar(inst, 5063); --Get Z strike position local zAxisCurrentPos = mc.mcAxisGetPos(inst, 2); --Get Current Z position, sometimes the probe will continue past strike point. local zAxisDifference = (zProbeStrikePos - ZAxisCurrentPos); --Gets overshoot, returns positive local zAxisNewOffset = strikePlateThickness - zAxisDifference; --Subracts overshoot from known thickness of strikeplate mc.mcAxisSetPos(inst, 2, zAxisNewOffset); --Sets DRO to current position --mc.mcAxisSetMachinePos(inst, 2, zAxisNewOffset); --Sets Machine to offset if anyone finds this necessary mc.mcCntlGcodeExecuteWait(inst, 'G00 G91 Z1'); --Rapid move to 1 inch above current pos local isProbeSafe = checkProbe(); --Checks to see if probe is safe if isProbeSafe == true then wx.wxMessageBox("Probe is still activated! Check to make sure probe is not damaged or still contacting strike plate."); else mc.mcCntlSetLastError(inst, "Z Axis now referenced."); end --if end --main if (mc.mcInEditor() == 1) then m10() end