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 function m6() mc.mcCntlSetLastError(inst, "Tool Change Started") ------ Get and compare next and current tools to see if we need to change tools------ local SelectedTool = mc.mcToolGetSelected(inst) local CurrentTool = mc.mcToolGetCurrent(inst) --SelectedTool = 7 --CurrentTool = 5 mc.mcCntlSetLastError(inst, string.format("Tool change - From: %.0f To %.0f", CurrentTool, SelectedTool)) if (SelectedTool == CurrentTool) then mc.mcCntlSetLastError(inst, "Next tool = Current tool") do return end end ------ Stop The spindle ------ mc.mcCntlGcodeExecuteWait(inst, "M5\n") --Debug --local SpindleAtZero = mc.mcSignalGetHandle(inst, mc.ISIG_INPUT20) --local ret2 = mc.mcSignalGetState(SpindleAtZero) --local ret = mc.mcSignalWait(inst, mc.ISIG_INPUT20, mc.WAIT_MODE_HIGH, 0) --local ret3 = mc.mcSignalGetState(SpindleAtZero) --mc.mcCntlSetLastError(inst, string.format("%.0f, %.0f, %.0f", ret2, ret, ret3)) --mc.mcCntlGcodeExecuteWait(inst, "M5\n") ------ 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: 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: Tool number out of range!") do return end end ------ Move to current tool change position ------ local GCode = "" GCode = GCode .. "G00 G90 G53 Z0\n" GCode = GCode .. string.format("G00 G90 G53 X%.4f Y%.4f\n", XPos1, YPos1) mc.mcCntlGcodeExecuteWait(inst, GCode) ------ Make sure the spindle is stopped ------ local ret = mc.mcSignalWait(inst, mc.ISIG_INPUT20, mc.WAIT_MODE_HIGH, 0) --mc.mcCntlGcodeExecuteWait(inst, "G04 P5000\n") ------ Move to tool fork height ------ GCode = "" GCode = GCode .. string.format("G00 G90 G53 Z%.4f\n", ZPos1) mc.mcCntlGcodeExecuteWait(inst, GCode) ------ 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) mc.mcCntlGcodeExecuteWait(inst, GCode) ------ Get the new tool ------ GetTool(SelectedTool) ------ Set Tool Depth ------ SetToolDepth() ------ Move Z to home position ------ mc.mcCntlGcodeExecuteWait(inst, "G00 G90 G53 Z0\n") ------ 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 function ReleaseTool(CurrentTool) -- 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" mc.mcCntlGcodeExecuteWait(inst, GCode) else ------ Lower to drop off position ------ mc.mcCntlGcodeExecuteWait(inst, "G01 G91 X2 F100\n") end ----- Open Drawbar ------ local DrawbarOPEN = mc.mcSignalGetHandle(inst, mc.OSIG_OUTPUT1) mc.mcSignalSetState(DrawbarOPEN, 1) ------ Move to Clear position turn off drawbar------ mc.mcCntlGcodeExecuteWait(inst, "G00 G91 Z.75\n") mc.mcSignalSetState(DrawbarOPEN, 0) ------ Close Drawbar ------ local DrawbarCLOSE = mc.mcSignalGetHandle(inst, mc.OSIG_OUTPUT2) mc.mcSignalSetState(DrawbarCLOSE, 1) mc.mcCntlGcodeExecuteWait(inst, "G04 P200\n") mc.mcSignalSetState(DrawbarCLOSE, 0) ------ Move to clear tool position ------ mc.mcCntlGcodeExecuteWait(inst, "G00 G90 G53 Z-3.0") end function GetTool(SelectedTool) ------ 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" mc.mcCntlGcodeExecuteWait(inst, GCode) end mc.mcCntlGcodeExecuteWait(inst, "G00 G91 Z-1.75\n") ------ 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------ mc.mcCntlGcodeExecuteWait(inst, "G00 G91 Z-1\n") mc.mcSignalSetState(BlowOff, 0) local DrawbarOPEN = mc.mcSignalGetHandle(inst, mc.OSIG_OUTPUT1) mc.mcSignalSetState(DrawbarOPEN, 1) ------ Move to pickup position position ------ mc.mcCntlGcodeExecuteWait(inst, "G00 G91 Z-.5\n") ------ Turn off open drawbar ------ mc.mcSignalSetState(DrawbarOPEN, 0) ------ Close Drawbar ------ local DrawbarCLOSE = mc.mcSignalGetHandle(inst, mc.OSIG_OUTPUT2) mc.mcSignalSetState(DrawbarCLOSE, 1) mc.mcCntlGcodeExecuteWait(inst, "G00 G91 Z.2\n") ------ 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 mc.mcCntlGcodeExecuteWait(inst, GCode) end function SetToolDepth() ------ Auto Depth Setting ------ mc.mcCntlGcodeExecuteWait(inst, "G00 G53 X35.93 Y2.75\n") -- go to point over depth setter mc.mcCntlSetLastError(inst, 'Setting Tool Height') mc.mcCntlGcodeExecuteWait(inst, "G91 G31 Z-4 F50") --probe the new tool mc.mcCntlGcodeExecuteWait(inst, "G91 G0 Z+0.1") --Retract from the probe mc.mcCntlGcodeExecuteWait(inst, "G91 G31 Z-0.15 F5") --Probes at slow speed local measure_1 = mc.mcAxisGetPos(inst,2) --Saves probed Z position mc.mcCntlGcodeExecuteWait(inst, "G91 G0 Z+0.1") --Retracts from the probe mc.mcCntlGcodeExecuteWait(inst, "G91 G31 Z-0.15 F5") --Probes at slow speed 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 mc.mcCntlGcodeExecuteWait(inst, GCode) end if(mc.mcInEditor() == 1) then -- If you are in the mcLua editor, this will cause your script to execute if you Run or Debug -- Make sure your Capitalization and numbers match that of the function you are calling m6() end