This is the code I use and it works fine.
This is in my Screen Load Script.
There is a lot of extra stuff in here but go to the section on ATC.
function Surface()
----remove max depth cut
local MaxDepthRegH = mc.mcRegGetHandle(inst, 'iRegs0/MaxDepth')
mc.mcRegSetValue(MaxDepthRegH, -22.0)
mc.mcCntlSetParameter(1801, -22.0)
--set extra cut depth to 0 and turn off leds
local Extrareg = mc.mcRegGetHandle(inst, 'iRegs0/ExtraDepth')
mc.mcRegSetValue(Extrareg, 0.000)
mc.mcCntlSetPoundVar(inst, mc.SV_HEAD_SHIFT_Z, 0.000)
scr.SetProperty('led010', 'Value', '0')
scr.SetProperty('led030', 'Value', '0')
scr.SetProperty('led050', 'Value', '0')
--set surface register to 0
hreg = mc.mcRegGetHandle(inst, 'iRegs0/SurfSet')
mc.mcRegSetValue(hreg, 0)
--turn surface set led off
scr.SetProperty('ledSurfaceSet', 'Value', '0')
--get sm on or off register
local SMReg = mc.mcRegGetHandle(inst, 'iRegs0/SMOnOff')
local SMRegVal = mc.mcRegGetValue(SMReg)
--set to project coordinates
mc.mcCntlGcodeExecuteWait(inst, "g54")
--check if using tang knife
local TKOnOff = mc.mcCntlGetParameter(502)
if (TKOnOff == 1) then
SMRegVal = mc.mcRegSetValue(SMReg, 0)
--scr.SetProperty('ledSMOnOff', 'Value', '0')
end
--check for ATC
local ATCReg = mc.mcRegGetHandle(inst, 'iRegs0/ATCOnOff')
local ATCRegVal = mc.mcRegGetValue(ATCReg)
--if ATC turned off
if (ATCRegVal == 0) then
mc.mcCntlGcodeExecuteWait(inst, "G49")
mc.mcToolSetCurrent(inst, 0)
--If SM turned OFF
if (SMRegVal == 0) then
--set to 0 and lift 0.5
mc.mcAxisSetPos(inst, 2, 0.0)
mc.mcCntlGcodeExecuteWait(inst, "g0 z0.5")
SMHit = 1
end
--If SM turned ON
if (SMRegVal == 1) then
--set jog speed to 100%
scr.SetProperty('sliJogRate', 'Value', tostring(100))
--wx.wxMessageBox("SM Used")
mc.mcCntlGcodeExecuteWait(inst, "g31 z-8.0 f20.0")
--check to see if sm hit
SMHit = 0
SMHit, rc = mc.mcCntlProbeGetStrikeStatus(inst)
if (SMHit == 1) then
mc.mcAxisSetPos(inst, 2, 1.5)
--wx.wxMilliSleep(100)
mc.mcCntlGcodeExecuteWait(inst, "g0 z2.0")
else
wx.wxMessageBox("DID NOT HIT SURFACE MOUSE")
wx.wxMessageBox("MUST CLICK RESET BEFORE NEXT ATTEMPT")
end
end
end
--if ATC turned on
if (ATCRegVal == 1) then
rc = wx.wxMessageBox("Have any of the tools in the ATC been changed?", "ATC Tools Changed?",2)
if (rc == 2) then
local CurrentTool = mc.mcToolGetCurrent(inst)
mc.mcCntlGcodeExecuteWait(inst, "G49")
hregMHmd = mc.mcRegGetHandle(inst, 'iRegs0/MachHmd')
valMHmd = mc.mcRegGetValue(hregMHmd)
if (valMHmd == 1) then
--If SM turned OFF
if (SMRegVal == 0) then
local ZCurPos = mc.mcAxisGetPos(inst, 2)
mc.mcToolSetData(inst, mc.MTOOL_MILL_HEIGHT, CurrentTool, ZCurPos)
local ZCurClear = (ZCurPos + 0.5)
mc.mcCntlGcodeExecuteWait(inst, "g0 Z" .. ZCurClear)
mc.mcCntlGcodeExecuteWait(inst, "G43")
SMHit = 1
end
--If SM turned ON
if (SMRegVal == 1) then
--set jog speed to 100%
scr.SetProperty('sliJogRate', 'Value', tostring(100))
--wx.wxMessageBox("SM Used")
mc.mcCntlGcodeExecuteWait(inst, "g31 z-8.0 f20.0")
--check to see if sm hit
SMHit = 0
SMHit, rc = mc.mcCntlProbeGetStrikeStatus(inst)
if (SMHit == 1) then
--mc.mcAxisSetPos(inst, 2, 1.5)
--wx.wxMilliSleep(100)
--mc.mcCntlGcodeExecuteWait(inst, "g0 z2.0")
local ZCurPos = mc.mcAxisGetPos(inst, 2)
local ZActualPos = (ZCurPos - 1.5)
mc.mcToolSetData(inst, mc.MTOOL_MILL_HEIGHT, CurrentTool, ZActualPos)
local ZCurClear = (ZCurPos + 0.5)
mc.mcCntlGcodeExecuteWait(inst, "g0 Z" .. ZCurClear)
mc.mcCntlGcodeExecuteWait(inst, "G43")
else
wx.wxMessageBox("DID NOT HIT SURFACE MOUSE")
wx.wxMessageBox("MUST CLICK RESET BEFORE NEXT ATTEMPT")
end
end
else
wx.wxMessageBox("Must set Machine Home.")
end
elseif (rc ==

then
local CurrentTool = mc.mcToolGetCurrent(inst)
mc.mcCntlGcodeExecuteWait(inst, "G43")
SMHit = 1
end
end
--set Surface register to 1 and LED On
if (SMHit == 1) then
hreg = mc.mcRegGetHandle(inst, 'iRegs0/SurfSet')
mc.mcRegSetValue(hreg, 1)
--turn surface set led on
scr.SetProperty('ledSurfaceSet', 'Value', '1')
end
end