4
« on: July 18, 2018, 06:16:25 PM »
I am setting up a machine with a linear ATC. The script is based on the example in the Lua scripting guide in the documentation folder of Mach4. The script is working somehow, but there is an issue if you hit the Stop button during a tool change as this causes Mach4 to believe the tool change is finished even though it might haven't done anything yet.
Is there a way to make sure the macro is terminated if the Stop button is hit while the tool change is ongoing?
The only way I could think of solving this is to compare the requested tool change position with the actual machine position before and after the tool is but back or released - and if it doesn't reach that position I could return from the script. But if there is an easier way to see if the Stop button was hit that would be better.
package.path = wx.wxGetCwd() .. "\\Profiles\\Mach4Mill\\Modules\\?.lua;"
if(package.loaded.ToolChangePositions == nil) then
tcp = require "ToolChangePositions"
end
function m6()
local inst = mc.mcGetInstance()
local SelectedTool = mc.mcToolGetSelected(inst)
local CurrentTool = mc.mcToolGetCurrent(inst)
-- local xstart = mc.mcAxisGetMachinePos(inst,0)
-- local ystart = mc.mcAxisGetMachinePos(inst,1)
local en_axis = 0
repeat
local Is_Enabled = mc.mcAxisIsEnabled(inst, en_axis)
if Is_Enabled == 1 then
local is_Homed = mc.mcAxisIsHomed(inst, en_axis)
if is_Homed ~= 1 then
mc.mcCntlEnable(inst, 0)
wx.wxMessageBox("RESET: Machine was not referenced")
return
end
end
en_axis = en_axis + 1
until( en_axis == 6 )
if SelectedTool == CurrentTool then
mc.mcCntlSetLastError(inst, "No need for tool change!")
do return end
end
------ Get current state ------
local CurFeed = mc.mcCntlGetPoundVar(inst, 2134)
local CurFeedMode = mc.mcCntlGetPoundVar(inst, 4001)
local CurAbsMode = mc.mcCntlGetPoundVar(inst, 4003)
------ Get position data for current tool ------
ToolData = tcp.GetToolData(CurrentTool)
if (ToolData ~= nil) then
Num1 = ToolData.Tool_Number
XPos1 = ToolData.X_Position
YPos1 = ToolData.Y_Position
ZPos1 = ToolData.Z_Position
else
mc.mcCntlEStop(inst)
mc.mcCntlSetLastError(inst, "ERROR: No place for this tool. Tool number out of range!")
do return end
end
------ Get position data for next tool ------
ToolData = tcp.GetToolData(SelectedTool)
if (ToolData ~= nil) then
Num2 = ToolData.Tool_Number
XPos2 = ToolData.X_Position
YPos2 = ToolData.Y_Position
ZPos2 = ToolData.Z_Position
else
mc.mcCntlEStop(inst)
mc.mcCntlSetLastError(inst, "ERROR: Requested tool number out of range!")
do return end
end
------ Move to current tool change position ------
local GCode = ""
GCode = GCode .. "G00 G90 G53 Z0.0\n"
GCode = GCode .. string.format("G00 G90 G53 X%.4f Y%.4f\n", XPos1, YPos1-100)
GCode = GCode .. string.format("G00 G90 G53 Z%.4f\n", ZPos1)
GCode = GCode .. string.format("G01 G90 G53 Y%.4f F100\n", YPos1)
mc.mcCntlGcodeExecuteWait(inst, GCode)
wx.wxMessageBox("Machine will now open drawbar")
------ Release drawbar ------
local DrawBarOut = mc.OSIG_OUTPUT10
local hsig = mc.mcSignalGetHandle(inst, DrawBarOut)
mc.mcSignalSetState(hsig, 1)
------ Move to current tool change position ------
local GCode = ""
GCode = GCode .. string.format("G00 G90 G53 Z%.4f\n", ZPos1+20)
GCode = GCode .. string.format("G00 G90 G53 X%.4f Y%.4f\n", XPos2, YPos2)
GCode = GCode .. string.format("G00 G90 G53 Z%.4f F100\n", ZPos2)
mc.mcCntlGcodeExecuteWait(inst, GCode)
------ Clamp drawbar ------
mc.mcSignalSetState(hsig, 0)
------ Retract from tool holder ------
local GCode = ""
GCode = GCode .. string.format("G00 G90 G53 X%.4f Y%.4f\n", XPos2, YPos2-100)
GCode = GCode .. "G00 G90 G53 Z0.0\n"
mc.mcCntlGcodeExecuteWait(inst, GCode)
------ Reset state ------
mc.mcCntlSetPoundVar(inst, 2134, CurFeed)
mc.mcCntlSetPoundVar(inst, 4001, CurFeedMode)
mc.mcCntlSetPoundVar(inst, 4003, CurAbsMode)
-- mc.mcCntlGcodeExecuteWait(inst, "G90 G53 G0 X"..xstart.." Y"..ystart) --back to initial position
mc.mcToolSetCurrent(inst, SelectedTool) -- change to new tool
mc.mcCntlSetPoundVar(inst, 2032, SelectedTool) -- set height offset number
mc.mcCntlGcodeExecuteWait(inst, "G43 H"..SelectedTool)
mc.mcCntlSetLastError(inst, string.format("Tool change finished - Tool: %.0f", SelectedTool))
end
if (mc.mcInEditor() == 1) then
m6()
end