Hi,
I have gone through m6 and regrouped, I hope it might help you.
The main thing I have done is break m6 into several files, the m6() proper, releaseTool(), getTool() and setToolDepth().
Note that I have deliberately reduced the leading letter of each function to lowercase per smurph's recommendation. In truth
the interpreter will reduce subsequent characters to lowercase also but I have retained capitalization of some letters to maintain
readability.
The second thing I have done is on every occasion where a mcCntlGcodeExecuteWait() is called I have tested the return code to ensure
that the function completed. Should it not do so you get a wxMessageBox notification including a number that will help you identify
any particular call which failed and a second number corresponding to the MERROR.
It maybe that once you have worked out how to get all this code to work that you can either delete or comment out
all (24 or so) diagnostic statement groups.....but for the moment they will highlight any errors.
I have bundled the module load statement in m6.
Note also that ALL four files need to be saved in the Macros folder of your current profile.
Save as m6.mcs
function m6()
local ZGageBlockHeight = 3.3091
local inst = mc.mcGetInstance()
package.path = wx.wxGetCwd() .. "\\Profiles\\AXYZ\\Modules\\?.lua;"
if(package.loaded.ToolChangePositions == nil) then
tcp = require "ToolChangePositions"
end
local SelectedTool = mc.mcToolGetSelected(inst)
local CurrentTool = mc.mcToolGetCurrent(inst)
local rc=mc.mcCntlGcodeExecuteWait(inst, "m5\n")
if rc~=0 then
wx.wxMessageBox("mcCntlGcodeExecuteWait() did not progress 1 MERROR="..tostring(rc))
do return end
end
local CurFeed = mc.mcCntlGetPoundVar(inst, 2134)
local CurFeedMode = mc.mcCntlGetPoundVar(inst, 4001)
local CurAbsMode = mc.mcCntlGetPoundVar(inst, 4003)
local 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: Tool number out of range!")
do return end
end
local 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: Tool number out of range!")
do return end
end
local GCode = ""
GCode = GCode .. "G00 G90 G53 Z0\n"
GCode = GCode .. string.format("G00 G90 G53 X%.4f Y%.4f\n", XPos1, YPos1)
local rc=mc.mcCntlGcodeExecuteWait(inst, GCode)
if rc~=0 then
wx.wxMessageBox("mcCntlGcodeExecuteWait() did not progress 2 MERROR="..tostring(rc))
do return end
end
GCode = ""
GCode = GCode .. string.format("G00 G90 G53 Z%.4f\n", ZPos1)
local rc=mc.mcCntlGcodeExecuteWait(inst, GCode)
if rc~=0 then
wx.wxMessageBox("mcCntlGcodeExecuteWait() did not progress 3 MERROR="..tostring(rc))
do return end
end
------ Release the current tool ------
releaseTool(CurrentTool)
------ Move to next tool change position ------
GCode = ""
GCode = GCode .. string.format("G00 G90 G53 X%.4f Y%.4f\n", XPos2 + 2, YPos2)
local rc=mc.mcCntlGcodeExecuteWait(inst, GCode)
if rc~=0 then
wx.wxMessageBox("mcCntlGcodeExecuteWait() did not progress 4 MERROR="..tostring(rc))
do return end
end
------ Get the new tool ------
getTool(SelectedTool)
------ Set Tool Depth ------
setToolDepth()
------ Move Z to home position ------
local rc=mc.mcCntlGcodeExecuteWait(inst, "G00 G90 G53 Z0\n")
if rc~=0 then
wx.wxMessageBox("mcCntlGcodeExecuteWait() did not progress 5 MERROR="..tostring(rc))
do return end
end
------ Reset state ------
mc.mcCntlSetPoundVar(inst, 2134, CurFeed)
mc.mcCntlSetPoundVar(inst, 4001, CurFeedMode)
mc.mcCntlSetPoundVar(inst, 4003, CurAbsMode)
------ Set new tool ------
mc.mcToolSetCurrent(inst, SelectedTool)
CurrentTool = mc.mcToolGetCurrent(inst)
mc.mcCntlSetLastError(inst, string.format("Tool changed, now using tool: %.0f", CurrentTool))
end
if (mc.mcInEditor() == 1) then
m6()
end
Save as releaseTool.mcs
function releaseTool(CurrentTool)
local inst=mc.mcGetInstance()
-- Set special casses
if(CurrentTool == 7) then
------ Lower to drop off position ------
local GCode = ""
GCode = GCode .. "G00 G53 x36 Y2\n"
GCode = GCode .. "G00 G53 x39.6 Y2\n"
GCode = GCode .. "G00 G53 z-6.11\n"
GCode = GCode .. "G01 G91 Y-2 F100\n"
local rc=mc.mcCntlGcodeExecuteWait(inst, GCode)
if rc~=0 then
wx.wxMessageBox("mcCntlGcodeExecuteWait() did not progress 6 MERROR="..tostring(rc))
do return end
end
else
------ Lower to drop off position ------
local rc=mc.mcCntlGcodeExecuteWait(inst, "G01 G91 X2 F100\n")
if rc~=0 then
wx.wxMessageBox("mcCntlGcodeExecuteWait() did not progress 7 MERROR="..tostring(rc))
do return end
end
end
----- Open Drawbar ------
local DrawbarOPEN = mc.mcSignalGetHandle(inst, mc.OSIG_OUTPUT1)
mc.mcSignalSetState(DrawbarOPEN, 1)
------ Move to Clear position turn off drawbar------
local rc=mc.mcCntlGcodeExecuteWait(inst, "G00 G91 Z.75\n")
if rc~=0 then
wx.wxMessageBox("mcCntlGcodeExecuteWait() did not progress 8 MERROR="..tostring(rc))
do return end
end
mc.mcSignalSetState(DrawbarOPEN, 0)
------ Close Drawbar ------
local DrawbarCLOSE = mc.mcSignalGetHandle(inst, mc.OSIG_OUTPUT2)
mc.mcSignalSetState(DrawbarCLOSE, 1)
local rc=mc.mcCntlGcodeExecuteWait(inst, "G04 P200\n")
if rc~=0 then
wx.wxMessageBox("mcCntlGcodeExecuteWait() did not progress 9 MERROR="..tostring(rc))
do return end
end
mc.mcSignalSetState(DrawbarCLOSE, 0)
------ Move to clear tool position ------
local rc=mc.mcCntlGcodeExecuteWait(inst, "G00 G90 G53 Z-3.0")
if rc~=0 then
wx.wxMessageBox("mcCntlGcodeExecuteWait() did not progress 10 MERROR="..tostring(rc))
do return end
end
end
if (mc.mcInEditor()==1)then
releaseTool()
end
Save as getTool.mcs
function getTool(SelectedTool)
local inst=mc.mcGetInstance()
------ Lower to blow off start position ------
if(SelectedTool == 7) then -- Set special casses
------ Lower to drop off position ------
local GCode = ""
GCode = GCode .. "G00 G53 x39.6\n"
GCode = GCode .. "G00 G91 Y-2.75\n"
local rc=mc.mcCntlGcodeExecuteWait(inst, GCode)
if rc~=0 then
wx.wxMessageBox("mcCntlGcodeExecuteWait() did not progress 11 MERROR="..tostring(rc))
do return end
end
end
local rc=mc.mcCntlGcodeExecuteWait(inst, "G00 G91 Z-1.75\n")
if rc~=0 then
wx.wxMessageBox("mcCntlGcodeExecuteWait() did not progress 12 MERROR="..tostring(rc))
do return end
end
------ Turn on Blow off ------
local BlowOff = mc.mcSignalGetHandle(inst, mc.OSIG_OUTPUT0)
mc.mcSignalSetState(BlowOff, 1)
------ Lower to pick up start position shut off blow off, turn on drawbar------
local rc=mc.mcCntlGcodeExecuteWait(inst, "G00 G91 Z-1\n")
if rc~=0 then
wx.wxMessageBox("mcCntlGcodeExecuteWait() did not progress 13 MERROR="..tostring(rc))
do return end
end
mc.mcSignalSetState(BlowOff, 0)
local DrawbarOPEN = mc.mcSignalGetHandle(inst, mc.OSIG_OUTPUT1)
mc.mcSignalSetState(DrawbarOPEN, 1)
------ Move to pickup position position ------
local rc=mc.mcCntlGcodeExecuteWait(inst, "G00 G91 Z-.5\n")
if rc~=0 then
wx.wxMessageBox("mcCntlGcodeExecuteWait() did not progress 14 MERROR="..tostring(rc))
do return end
end
------ Turn off open drawbar ------
mc.mcSignalSetState(DrawbarOPEN, 0)
------ Close Drawbar ------
local DrawbarCLOSE = mc.mcSignalGetHandle(inst, mc.OSIG_OUTPUT2)
mc.mcSignalSetState(DrawbarCLOSE, 1)
local rc=mc.mcCntlGcodeExecuteWait(inst, "G00 G91 Z.2\n")
if rc~=0 then
wx.wxMessageBox("mcCntlGcodeExecuteWait() did not progress 15 MERROR="..tostring(rc))
do return end
end
------ Stop Closing drawbar ------
mc.mcSignalSetState(DrawbarCLOSE, 0)
------ Move to final tool change position ------
local GCode = ""
if(SelectedTool == 7) then
GCode = GCode .. "G01 G91 Y2 F100\n" -- Clear fork
GCode = GCode .. "G00 G90 G53 Z0\n" -- Go up
else
GCode = GCode .. "G01 G91 X-2 F100\n" -- clear fork
GCode = GCode .. "G00 G90 G53 Z0\n" -- Go up
GCode = GCode .. "G00 G53 X33 Y2.75\n" -- Go to clear position read to measure
end
local rc=mc.mcCntlGcodeExecuteWait(inst, GCode)
if rc~=0 then
wx.wxMessageBox("mcCntlGcodeExecuteWait() did not progress 16 MERROR="..tostring(rc))
do return end
end
end
if (mc.mcInEditor()==1)then
getTool()
end
Save as setToolDepth.mcs
function setToolDepth()
local inst=mc.mcGetInstance()
local ZGageBlockHeight = 3.3091
------ Auto Depth Setting ------
local rc=mc.mcCntlGcodeExecuteWait(inst, "G00 G53 X35.93 Y2.75\n") -- go to point over depth setter
if rc~=0 then
wx.wxMessageBox("mcCntlGcodeExecuteWait() did not progress 17 MERROR="..tostring(rc))
do return end
end
local rc=mc.mcCntlSetLastError(inst, 'Setting Tool Height')
if rc~=0 then
wx.wxMessageBox("mcCntlGcodeExecuteWait() did not progress 18 MERROR="..tostring(rc))
do return end
end
local rc=mc.mcCntlGcodeExecuteWait(inst, "G91 G31 Z-4 F50") --probe the new tool
if rc~=0 then
wx.wxMessageBox("mcCntlGcodeExecuteWait() did not progress 19 MERROR="..tostring(rc))
do return end
end
local rc=mc.mcCntlGcodeExecuteWait(inst, "G91 G0 Z+0.1") --Retract from the probe
if rc~=0 then
wx.wxMessageBox("mcCntlGcodeExecuteWait() did not progress 20 MERROR="..tostring(rc))
do return end
end
local rc=mc.mcCntlGcodeExecuteWait(inst, "G91 G31 Z-0.15 F5") --Probes at slow speed
if rc~=0 then
wx.wxMessageBox("mcCntlGcodeExecuteWait() did not progress 21 MERROR="..tostring(rc))
do return end
end
local measure_1 = mc.mcAxisGetPos(inst,2) --Saves probed Z position
local rc=mc.mcCntlGcodeExecuteWait(inst, "G91 G0 Z+0.1") --Retracts from the probe
if rc~=0 then
wx.wxMessageBox("mcCntlGcodeExecuteWait() did not progress 22 MERROR="..tostring(rc))
do return end
end
local rc=mc.mcCntlGcodeExecuteWait(inst, "G91 G31 Z-0.15 F5") --Probes at slow speed
if rc~=0 then
wx.wxMessageBox("mcCntlGcodeExecuteWait() did not progress 23 MERROR="..tostring(rc))
do return end
end
local measure_2 = mc.mcAxisGetPos(inst,2) --Saves probed Z position
local measured_average = (measure_1 + measure_2)/2 --Averages two probed values
local offSet = measure_2 - measured_average
local adjustedHeight = ZGageBlockHeight + offSet
mc.mcAxisSetPos(inst,2,adjustedHeight) --Sets current Z postion to gage block height with measured offset
GCode = ""
GCode = GCode .. "G00 G90 G53 Z0\n" --Retract
GCode = GCode .. "G00 G53 X33 Y2.75\n" -- Move to clear spot
local rc=mc.mcCntlGcodeExecuteWait(inst, GCode)
if rc~=0 then
wx.wxMessageBox("mcCntlGcodeExecuteWait() did not progress 24 MERROR="..tostring(rc))
do return end
end
end
if (mc.mcInEditor()==1)then
setToolDepth()
end
Scan the code and see what you think. Note that M1000() occurs nowhere. Should you want to call setToolDepth() from
from the main Gcode job, not m6, but the main program, THEN use m1000 and create a function m1000() in your Macros
folder that explicitly calls setToolDepth(). In this way setToolDepth() will exist as only one file but may be called by different
procedures.
Craig