function m6() --Non-Random automatic tool changer --This file assumes that you have G54 X0 Y0 Z0 to be setup as the Y-offset distance from tools and the correct height for picking --up/dropping a tool as well as the correct X location for entering the tool changer. ----------Variables---------- local inst = mc.mcGetInstance() --Get the instance of Mach4 local hReg = 0 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 MotorSig = mc.OSIG_OUTPUT0 local CloseColletSig = mc.OSIG_OUTPUT1 local CurrentTool = mc.mcToolGetCurrent(inst) local NextTool = mc.mcToolGetSelected(inst) local YOffsetDistance = 14.25------------DEFINE Y OFFSET DISTANCE local numtools = 4 local ToolPositionIO = {} ----------------------------- function ToolChange(NextTool) --------------------------INPUT CHECKING----------------------------- if (NextTool > numtools) then return "That tool does not exist", false end hReg = 0 --reset hReg to 0 each time before reading it so you know if you get an error. hReg = mc.mcIoGetHandle(inst, ToolInColletSig) if hReg == 0 then return "Could not locate tool in collet signal!", false end hReg = 0 hReg = mc.mcIoGetHandle(inst, MotorSig) if hReg == 0 then return "Could not locate motor signal!", false end for i = 1, numtools+1 do ToolPositionIO[i] = 0 ToolPositionIO[i] = mc.mcIoGetHandle(inst, "Position"..tostring(i)) if ToolPositionIO[i] == 0 then return "Could not locate tool position signal!", false end end hReg = mc.mcIoGetHandle(inst, CloseColletSig) if hReg == 0 then return "Could not locate collet close signal!", false end ----------------------END INPUT CHECKING----------------------------- local rc = mc.mcIoGetState(ChuckhReg) if rc == 1 then --If rc equals 1, then there is a tool in the chuck --Put the tool away. rc = mcCntlGCodeExecuteWait(inst, "G54 X0 Y0 Z0") --Move to the offset zone. 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 hReg = 0 hReg = mc.mcIoGetHandle(inst, CloseColletSig) if hReg == 0 then return "Could not locate close collet signal!", false end mc.mcIoSetState(hReg, 0) --Release the tool mcCntlGCodeExecuteWait(inst, "G54 X0 Y0 Z0")--Move back to the offset line if rc ~= mc.MERROR_NOERROR then return "Could not complete GCode motion", false end else mcCntlGCodeExecuteWait(inst, "G54 X0 Y0 Z0")--Move to the offset line if rc ~= mc.MERROR_NOERROR then return "Could not complete GCode motion", false end end --At this point, we are at the offset line, and there is no tool in the chuck. hReg = 0 hReg = mc.mcIoGetHandle(inst, MotorSig) if hReg == 0 then return "Could not locate motor signal!", false end mc.mcIoSetState(hReg, 1) --Turn on the motor until we are at the desired tool change position. local loop = true while(loop == true) do local a,b = mcIoGetState(ToolPositionIO[NextTool]) if(b == true) then if(a == true) then loop = false hReg = 0 hReg = mc.mcIoGetHandle(inst, MotorSig) if hReg == 0 then return "Could not locate motor signal!", false end mc.mcIoSetState(hReg, 0)--the tool changer is now rotated to the correct location. --so we turn off the motor. else wx.wxMilliSleep(100) --Sleep for 100 milliseconds, and then recheck input. end else hReg = 0 hReg = mc.mcIoGetHandle(inst, MotorSig) if hReg == 0 then return "Could not locate motor signal!", false end mc.mcIoSetState(hReg, 0) return "Unable to get state of motor IO", false end end mcCntlGCodeExecuteWait(inst, "G91 Y"..YOffsetDistance) --Move into the tool changer 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 collet close signal!", false end mc.mcIoSetState(hReg, 1) --grab the tool. mcCntlGCodeExecuteWait(inst, "G54 X0 Y0 Z0")--Move back to the offset line if rc ~= mc.MERROR_NOERROR then return "Could not complete GCode motion", false end return "Tool change complete!", true end local a, b = ToolChange(NextTool) mc.mcCntlSetLastError(tostring(a))--Prints status of tool change. if(b == true) then mc.mcToolSetCurrent(inst, NextTool) end end if (mc.mcInEditor() == 1) then dofile ("load_modules.mcs") m6() end