function m6() --Wine rack tool change --This file assumes that you have G53 Y0 Z0 to be setup as the Y-offset distance from tools and the correct height for picking up/dropping a tool. -----------Tool 1------------------Tool 2----------Tool3-------------Tool 4------ --------------------------------------------------------------------------------- -------------------------------Y Offset------------------------------------------ -- -- -- -- -- -- -- Cutter Area -- -- -- -- -- --------------------------------------------------------------------------------- ----------Variables---------- local inst = mc.mcGetInstance() --Get the instance of Mach4 local ToolInColletSig = mc.ISIG_INPUT0 --These outputs/inputs will be configured by the user depending what outputs/inputs they have setup in Mach config. local CloseColletSig = mc.OSIG_OUTPUT1 local hReg = 0 local CurrentTool = mc.mcToolGetCurrent(inst) local NextTool = mc.mcToolGetSelected(inst) local YOffsetDistance = 10.5 --THE USER WOULD NEED TO DEFINE THIS TO FIT THEIR MACHINE local numtools = 4 ----------------------------- local ToolXPositions = {} ToolXPositions[1] = 4.5 --random numbers, you would configure them to be your own measurements/add to them if you have more tools. ToolXPositions[2] = 12 ToolXPositions[3] = 16 ToolXPositions[4] = 25 function ToolChange(NextTool) ----------------INPUT CHECKING----------------------------------- ---------Here we are checking to make sure that we can get all of the hReg's that we need, so that we don't start the code unless are of the outputs/inputs are configured.------ if (NextTool > numtools) then--If this is true, then the tool is out of range. return "That tool does not exist", false end hReg = 0 hReg = mc.mcIoGetHandle(inst, ToolInColletSig) if hReg == 0 then return "Could not locate collet signal!", false end hReg = 0 hReg = mc.mcIoGetHandle(inst, CloseColletSig) if hReg == 0 then return "Could not locate close collet signal!", false end ----------------END INPUT CHECKING----------------------------------- hReg = 0 hReg = mc.mcIoGetHandle(inst, ToolInColletSig) if hReg == 0 then return "Could not locate collet signal!", false end local rc = mc.mcIoGetState(hReg) if rc == 1 then --If rc equals 1, then there is a tool in the chuck --Put the tool away. mcCntlGCodeExecuteWait(inst, "G53 Y0 Z0 X"..ToolXPositions[CurrentTool]) -- ".." concatenates the xposition to the gcode string if rc ~= mc.MERROR_NOERROR then return "Could not complete GCode motion", false end --We are now at the offset line, and need to move the correct Y Distance so we can drop off the tool. mcCntlGCodeExecuteWait(inst, "G91 Y"..YOffsetDistance) if rc ~= mc.MERROR_NOERROR then return "Could not complete GCode motion", false end --We now release the tool. hReg = 0 hReg = mc.mcIoGetHandle(inst, CloseColletSig) if hReg == 0 then return "Could not locate close collet signal!", false end mc.mcIoSetState(hReg, 0) --Set the output low, so that the tool is released. mcCntlGCodeExecuteWait(inst, "G53 Y0 Z0")--Move back to the YOffset line if rc ~= mc.MERROR_NOERROR then return "Could not complete GCode motion", false end else mcCntlGCodeExecuteWait(inst, "G53 Y0 Z0")--Move to the YOffset line if rc ~= mc.MERROR_NOERROR then return "Could not complete GCode motion", false end end mcCntlGCodeExecuteWait(inst, "G53 Z0 X"..ToolXPositions[NextTool]) --Move to the XPosition of the tool you are wanting to load if rc ~= mc.MERROR_NOERROR then return "Could not complete GCode motion", false end mcCntlGCodeExecuteWait(inst, "G91 Y"..YOffsetDistance) --We are now hovering over the tool we want if rc ~= mc.MERROR_NOERROR then return "Could not complete GCode motion", false end hReg = 0 hReg = mc.mcIoGetHandle(inst, CloseColletSig) if hReg == 0 then return "Could not locate close collet signal!", false end mc.mcIoSetState(ChuckReleaseHandle, 0) --Grab the tool by closing the chuck mc.mcToolSetCurrent(inst, NextTool) --Update mach with the new tool. mcCntlGCodeExecuteWait(inst, "G53 Y0 Z0")--Move back to the YOffset line if rc ~= mc.MERROR_NOERROR then return "Could not complete GCode motion", false end return "Success", true end local a, b = ToolChange(NextTool) mc.mcCntlSetLastError(inst, tostring(a))--Prints the output of the tool change. if(b == true) then mc.mcToolSetCurrent(inst, NextTool) end end --This function is used to allow the debugging to be done in the mcLua editor if (mc.mcInEditor() == 1) then dofile ("load_modules.mcs") m6() end