Code:
--Macro to automatically zero the Z-axis using a contact plate of known thickness positioned
--on top of the X-Y plane at the Z zero position. Cutting tool acts as a probe.
--Last updated: 3/5/17
--Author: Andy Wander. Adapted from the Mach3 macro by Christopher Esser (Smokingman). Based on the work done by Greolt
function m231()
-- this section sets user variables
local ProbeDist = -1 --Set the maximum distance in inches to move the Z-axis before probe touches contact plate(must be negative).
local ProbeFeed = 2 --Set the plunge speed for the probe in IPM
local DelayTime = 1 --Set the delay time in seconds from probe verify to start of Z-motion
local Retract = .1 --Set distance in inches to retract after probe
local platethickness = 0.0625 --plate thickness in inches
local VerifyTimeout = 10 --Set timeout to wait for probe verify in seconds
-- new variables added 3/5/17
local Zreturn = 1 --set to 1 to have Z axis return to previous height after routine is run; set to 0 to have Z axis remain at Retract distance above probe point
local Prompt = 1 --set to 1 to have the routine prompt you with a messagebox that requires clicking OK to continue befor probe starts; set to 0 to automatically start probe after touchplate verification
local notify = wx.wxSound('C:\\Mach4Hobby\\Sounds\\Ding.wav',0) --sound file to use for notifications-YOU MUST COPY copy the desired sound file to mach4hobby\sounds directory
local alert = wx.wxSound('C:\\Mach4Hobby\\Sounds\\Chord.wav',0) --sound file to use for alerts-YOU MUST COPY copy the desired sound file to mach4hobby\sounds directory
local abort = wx.wxSound('C:\\Mach4Hobby\\Sounds\\Ringout.wav',0) --sound file to use for Abort-YOU MUST COPY copy the desired sound file to mach4hobby\sounds directory
local success = wx.wxSound('C:\\Mach4Hobby\\Sounds\\Windows Print complete.wav',0) --sound file to use for Success-YOU MUST COPY copy the desired sound file to mach4hobby\sounds directory
-- this section sets Mach4 variables
local inst = mc.mcGetInstance()
local hProbe = mc.mcSignalGetHandle(inst, mc.ISIG_PROBE) --get handle of Input Signal for Probe activated
local hSpindle = mc.mcSignalGetHandle(inst, mc.OSIG_SPINDLEON) --get handle of Output Signal for Spindle ON
local cancel = 0 --set cancel signal to 0
local verifyTimer = VerifyTimeout*1000 --set timeout in milliseconds
-- this section records machine state variables
local zstart = mc.mcAxisGetMachinePos(inst, 2) --save current Machine Z coordinate
local curunits = mc.mcCntlGetPoundVar(inst, 4006) --save current units, feedrate, feedmode, abs/inc mode
local curfeed = mc.mcCntlGetPoundVar(inst, 2134)
local curfeedmode = mc.mcCntlGetPoundVar(inst, 4001)
local curabsmode = mc.mcCntlGetPoundVar(inst, 4003)
-- this section looks for a grounded touch plate
if ( mc.mcSignalGetState(hProbe) == 1 ) then --Check to see if the probe is already grounded or faulty
wx.wxMessageBox("Ground fault in probe detected. Fix probe and try again.") --tell user that probe is grounded, and exit function
local error = "Ground Fault in Probe detected! "
cancel = 1 --set cancel signal to 1
else --if (answer == 2 or others)
cancel = 0 --set cancel signal to 0
end
-- this section to prompt user if Spindle is on
if ( cancel == 0 ) then --if a previous operation has not cancelled the routine
if ( mc.mcSignalGetState(hSpindle) == 1 ) then--Check to see if the spindle is running
notify:Play()--play a warning sound
local answer = wx.wxMessageBox("It appears the spindle is running! Select 'Yes' to continue anyway or 'No' to exit.","" ,wx.wxYES +wx.wxNO)
if (answer == 8) then --if user cancels
error = "Spindle is ON! "
cancel = 1 --set cancel signal to 1
else --if (answer == 2 or others)
cancel = 0 --set cancel signal to 0
end
end
end
-- this section to prompt user to test touch plate
if ( cancel == 0 ) then --if a previous operation has not cancelled the routine
notify:Play()--play a warning sound
mc.mcCntlSetLastError(inst, "Touch probe to tool within 10s to verify connection") --prompt for touchplate test
while(verifyTimer >= 1) do
wx.wxMilliSleep(100) --wait for 100 mS
if ( mc.mcSignalGetState(hProbe) == 0 ) then --look for probe signal
verifyTimer = (verifyTimer - 100) --decrement timer
cancel = 1 --set cancel signal to 1
error = "Probe verify timed out. Fix Probe and try again. "
else
verifyTimer = 0 --zero timer
cancel = 0 --set cancel signal to 0
end
end
end
-- this section probes, sets Z coordinate, and restores machine to initial conditions
if ( cancel == 0 ) then --if previous a operation has not cancelled the operation
mc.mcCntlGcodeExecuteWait(inst, "G20")--Set Units to inches
if Prompt == 1 then --if the variable is set to 1
wx.wxMessageBox("Please place the touchplate in position, and click OK to continue.") --prompt for touchplate placement
notify:Play() --play a notification sound
mc.mcCntlGcodeExecuteWait(inst, "G04 P"..(1000*DelayTime)) --wait for the Delay Time before probing
elseif Prompt == 0 then --if prompt variable is 0
alert:Play() --play an alert sound
mc.mcCntlGcodeExecuteWait(inst, "G04 P"..(2000*DelayTime)) --wait for twice the Delay Time before probing
end
mc.mcCntlSetLastError(inst, "Probing for tool Z length!" )
local probestartZ = mc.mcAxisGetMachinePos(inst, 2) --save Machine Z coordinate before probing
mc.mcCntlGcodeExecuteWait(inst, "G20 G91 G31 z"..ProbeDist.." F"..ProbeFeed)--Probe for current tool length/position
local probeendZ = mc.mcAxisGetMachinePos(inst, 2) --save Machine Z coordinate after probing
local probeendZactual = mc.mcCntlGetPoundVar(inst, 5073) --get Z Machine coordinate of Actual Probe Position
if ( ProbeDist < (probeendZactual - probestartZ)) then --if probe moved less than max probe distance
local correction = (probeendZactual-probeendZ) --calculate overshoot/correction)
mc.mcAxisSetPos(inst, 2, (platethickness-correction))--set Z coord to thickness of plate - correction distance
mc.mcCntlGcodeExecuteWait(inst, "G20 G91 G0 Z"..Retract)--Move the Z axis off the touchplate by Retract Distance
success:Play() --play the Success sound
mc.mcCntlSetLastError(inst, "Probing Complete, Z Axis is Zeroed!")
else
mc.mcCntlSetLastError(inst, "Probe did not encounter touchplate. Auto Tool Zero Aborted!")
abort:Play() --play the Abort sound
end
mc.mcCntlGcodeExecuteWait(inst, "F"..curfeed) --set feedrate back to saved setting
mc.mcCntlGcodeExecuteWait(inst, "G"..curunits)--set units back to saved setting from beginning
if Zreturn == 1 then --if Variable is set to 1
mc.mcCntlGcodeExecuteWait(inst, "G90 G53 G0 Z"..zstart)--move Z axis back to saved position in Machine coords
end
mc.mcCntlGcodeExecuteWait(inst, "G"..curfeedmode) --set feed mode back to saved setting
mc.mcCntlGcodeExecuteWait(inst, "G"..curabsmode) --set abs mode back to saved setting
else --if "cancel" == 1 then cancel Probing operation!
abort:Play() --play the Abort sound
mc.mcCntlSetLastError(inst, error.."Auto Tool Zero Aborted!" )
end
end
if (mc.mcInEditor() == 1) then
m231()
end