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---------- hregox = mc.mcRegGetHandle(0, "iRegs0/ofsetx") valox = mc.mcRegGetValueString(hregox) Xofset = tonumber(valox) hregoy = mc.mcRegGetHandle(0, "iRegs0/ofsety") valoy = mc.mcRegGetValueString(hregoy) Yofset = tonumber(valoy) local GCode ="" GCode = GCode ..string.format("G52 X%.4f Y%.4f \n",Xofset,Yofset) mc.mcCntlGcodeExecuteWait (0, GCode) local hReg = 0 local ToolInColletSig = mc.ISIG_INPUT4 --Toll lock in spindel local CurrentTool = mc.mcToolGetCurrent(0) mc.mcCntlSetLastError(0, string.format(CurrentTool)); local NextTool = mc.mcToolGetSelected(0) mc.mcCntlSetLastError(0,string.format( NextTool)); local YOffsetDistance = 14.25------------DEFINE Y OFFSET DISTANCE local numtools = 14 local ToolPosition = {} local Zout=-40 local Zin= -134.37 -- -143.73 local Yout= 1749 local Yin= 1857.15-- 1894.24 local speed=8000 local speedZ=1000 local NextXPosition=500 local NextXposD=500 local Angeloffset=-27.51 -- 9.16 ----------------------------- function LoadMagazine() local inst = mc.mcGetInstance() local CSVPath = wx.wxGetCwd() .. "\\Magazine.csv" ToolNum = 0; local ff = io.input(io.open(CSVPath,"r")) local line; local counter =0 for line in io.lines(CSVPath) do ToolPosition[counter]={} tkz = wx.wxStringTokenizer(line, ","); local token = tkz:GetNextToken(); ToolPosition[counter][0]=token token = tkz:GetNextToken(); ToolPosition[counter][1]=token token = tkz:GetNextToken(); ToolPosition[counter][2]=token token = tkz:GetNextToken(); ToolPosition[counter][3]=token token = tkz:GetNextToken(); ToolPosition[counter][4]=token token = tkz:GetNextToken(); ToolPosition[counter][5]=token token = tkz:GetNextToken(); ToolPosition[counter][6]=token token = tkz:GetNextToken(); ToolPosition[counter][7]=token token = tkz:GetNextToken(); ToolPosition[counter][8]=token counter=counter +1 end end function WriteToInverterBigSpindle() local path="C:\\AbsEncoder\\MachComand.txt" file = io.open(path, "w") file:write("2") file:flush (path) --Save written data file:close (path) --Close file local hreg = mc.mcRegGetHandle(0, "iRegs0/BigSpindle") mc.mcRegSetValueString(hreg, tostring("0")) os.execute([[C:\\AbsEncoder\\WpfApp4.application]]) for i=1,99999999 do local t=i*2 local It= t/2 end hreg = mc.mcRegGetHandle(0, "iRegs0/BigSpindle") valUpdate1 = mc.mcRegGetValueString(hreg) hreg = mc.mcRegGetHandle(0, "iRegs0/SmallSpindle") valUpdate2 = mc.mcRegGetValueString(hreg) if(valUpdate1=="1" and valUpdate2=="0") then local smallhead = mc.OSIG_OUTPUT2 local hsig = mc.mcSignalGetHandle(0, smallhead) mc.mcSignalSetState(hsig, 0) local bighead = mc.OSIG_OUTPUT3 local hsig = mc.mcSignalGetHandle(0, bighead) mc.mcSignalSetState(hsig, 1) return 1 else return 0 end end function writeToFileToolChange() local inst = mc.mcGetInstance() local path="C:\\T\\MachineStatus.txt" file = io.open(path, "w") file:write("ToolChange") file:flush (path) --Save written data file:close (path) --Close file end function FindNextX() local inst = mc.mcGetInstance() val, rc = mc.mcCntlGetGcodeLineNbr( inst) buff, rc = mc.mcCntlGetGcodeLine( inst, val+5) local tmp local xpos=string.find(buff,"X") local ypos=string.find(buff,"Y") if (xpos == nil )then mc.mcCntlSetLastError(inst,"go to defoult x position") else NextXPosition= string.sub(buff,xpos+1,ypos-1) NextXposD=tonumber(NextXPosition) mc.mcCntlSetLastError(inst, NextXPosition) local G54x = mc.mcCntlGetPoundVar(inst, 5221)--maybi need 5223 NextXposD=NextXposD+G54x NextXPosition =string.format(NextXposD) mc.mcCntlSetLastError(inst,NextXPosition) end end function ToolExisst(NextTool) for i=1,numtools do local t=ToolPosition[i][0] local It= tonumber(t) if It==NextTool then return i end end return -1 end function ServoHoodUp1() local GCode ="" GCode = GCode ..string.format("G53 G0 C-3\n") local rc= mc.mcCntlGcodeExecuteWait (0, GCode) ---go up out z if rc ~= mc.MERROR_NOERROR then return "Could not lift servo hood", 20 end return 0 end function unload(UnloadPosition) local ypos= ToolPosition[UnloadPosition][2]-- get y position local Zin=ToolPosition[UnloadPosition][3]-- get y position local Xin= ToolPosition[UnloadPosition][1] local Xout= ToolPosition[UnloadPosition][4] --angel=angel+Angeloffset local GCode ="" GCode = GCode ..string.format("G90 G53 G0 Z0 X%.4f Y%.4f\n" ,Xout,ypos) mc.mcCntlGcodeExecuteWait (0, GCode) ---go up z and y out GCode ="" GCode = GCode ..string.format("G53 G0 Z%.4f\n",Zin) mc.mcCntlGcodeExecuteWait (0, GCode) ---go to z in GCode ="" GCode = GCode ..string.format("G53 G1 x%.4f F%.4f\n",Xin,speed) ---------12_11_2020 mc.mcCntlGcodeExecuteWait (0, GCode) ---go inside the magazine ---------------------------------------------cheack if stop press hreg = mc.mcRegGetHandle(0, "iRegs0/break") val = mc.mcRegGetValueString(hreg) val = tonumber(val) if(val==1) then return 100 end ---------------------------------------------------------------- local Sopen = mc.OSIG_OUTPUT8 local hsig = mc.mcSignalGetHandle(0, Sopen) ------open spindel mc.mcSignalSetState(hsig, 1) --------------------------------------------------=--------- mc.mcCntlSetLastError(0, "wait spindle open"); local SensorOpen = mc.ISIG_INPUT2 local so = mc.mcSignalGetHandle(0, SensorOpen) ------cheack if spindle open local state = mc.mcSignalGetState(so) if(state==0)then local dhs= mc.mcSignalWait(0, mc.ISIG_INPUT2, 1, 10); hreg = mc.mcRegGetHandle(0, "iRegs0/break") val = mc.mcRegGetValueString(hreg) val = tonumber(val) if(val==1) then return 100 end if (dhs ~= 0) then--if not rise up in 10 second wx.wxMessageBox ("Collet not Open") ------------cheack if sensor open colet on mc.mcCntlSetLastError(0, "ColetNotOpen"); return 10 end end --------------------------------------------------------------------- local GCode ="" GCode = GCode ..string.format("G4 P400 \n") --delay GCode = GCode ..string.format("G53 G0 Z%.4f ",Zout) local rc= mc.mcCntlGcodeExecuteWait (0, GCode) ---go up out z if rc ~= mc.MERROR_NOERROR then return "Could not complete GCode motion", false end return 0 end ---------------------------------------------------------------------------------------------------------------------------------------------------- function load(LoadPosition) local zn2=Zin +30 local ypos= ToolPosition[LoadPosition][2]-- get y position local Zin=ToolPosition[LoadPosition][3]-- get y position local Xin= ToolPosition[LoadPosition][1] local Xout= ToolPosition[LoadPosition][4] local GCode ="" GCode = GCode ..string.format("G53 G0 Y%.4f\n",ypos) GCode = GCode ..string.format("G4 P400 \n") --delay GCode = GCode ..string.format("G53 G0 Z%.4f\n",zn2) GCode = GCode ..string.format("G53 G1 Z%.4f F%.4f\n",Zin,speedZ) ----------------------------in case stop been press hreg = mc.mcRegGetHandle(0, "iRegs0/break") val = mc.mcRegGetValueString(hreg) val = tonumber(val) if(val==1) then return 100 end --------------------------------------------------------- mc.mcCntlGcodeExecuteWait (0, GCode) local Sopen = mc.OSIG_OUTPUT8 local hsig = mc.mcSignalGetHandle(0, Sopen) ------close spindel mc.mcSignalSetState(hsig, 0) ----------------------------------------- mc.mcCntlSetLastError(0, "wait spindle close"); ---deley local GCode ="" GCode = GCode ..string.format("G4 P1000 \n") --delay mc.mcCntlGcodeExecuteWait (0, GCode) local SensorClose = mc.ISIG_INPUT5 local sc = mc.mcSignalGetHandle(0, SensorClose) ------cheack if spindle close local state = mc.mcSignalGetState(sc) ------------------------------------------------------------------------------------ if(state==0)then--in case spindle not close need wait 10 sec dhs= mc.mcSignalWait(0, mc.ISIG_INPUT5, 1, 10); local Sdhs; if (dhs ~= 0) then wx.wxMessageBox ("Tool not Lock") ------------cheack if sensor Tool Lock colet on local Sopen = mc.OSIG_OUTPUT8 local hsig = mc.mcSignalGetHandle(0, Sopen) ------open spindel mc.mcSignalSetState(hsig, 1) local GCode ="" GCode = GCode ..string.format("G4 P1000 \n") --delay GCode = GCode ..string.format("G53 G0 Z%.4f ",Zout) local rc= mc.mcCntlGcodeExecuteWait (0, GCode) ---go up out z return 5 end end mc.mcCntlSetLastError(0, " spindle close"); local GCode ="" GCode = GCode ..string.format("G53 G0 X%.4f ",Xout) mc.mcCntlGcodeExecuteWait (0, GCode) ---go Y out GCode ="" GCode = GCode ..string.format("G53 G0 Z0. ") mc.mcCntlGcodeExecuteWait (0, GCode) ---go Z out return 0 end ---------------------------------------------------------------------------------------------------------------------------------------------------- function openSpindle() hreg = mc.mcRegGetHandle(0, "iRegs0/break") val = mc.mcRegGetValueString(hreg) val = tonumber(val) if(val==1) then return 100 end ---------------------------------------------------------------- local Sopen = mc.OSIG_OUTPUT8 local hsig = mc.mcSignalGetHandle(0, Sopen) ------open spindel mc.mcSignalSetState(hsig, 1) --------------------------------------------------=--------- mc.mcCntlSetLastError(0, "wait spindle open"); local SensorOpen = mc.ISIG_INPUT2 local so = mc.mcSignalGetHandle(0, SensorOpen) ------cheack if spindle open local state = mc.mcSignalGetState(so) if(state==0)then local dhs= mc.mcSignalWait(0, mc.ISIG_INPUT2, 1, 10); hreg = mc.mcRegGetHandle(0, "iRegs0/break") val = mc.mcRegGetValueString(hreg) val = tonumber(val) if(val==1) then return 100 end if (dhs ~= 0) then--if not rise up in 10 second wx.wxMessageBox ("Collet not Open") ------------cheack if sensor open colet on mc.mcCntlSetLastError(0, "ColetNotOpen"); return 10 end end return 0 end function ToolChange(NextTool) --------------------------test the sensor if tool inside =0 if (CurrentTool==0)then local SensorClose = mc.ISIG_INPUT5 local sc = mc.mcSignalGetHandle(0, SensorClose) ------cheack if spindle close local state = mc.mcSignalGetState(sc) if (state==1)then rc = mc.mcCntlCycleStop(0)--stop all gcode because there no match betwen sensor and value return 30,false end end if (CurrentTool>0)then local SensorClose = mc.ISIG_INPUT5 local sc = mc.mcSignalGetHandle(0, SensorClose) ------cheack if spindle close local state = mc.mcSignalGetState(sc) if (state==0)then rc = mc.mcCntlCycleStop(0)--stop all gcode because there tool inside but sensor no signa return 40,false end end -- HoodUp() if NextTool == CurrentTool then return "No need chage Tool", true else local errorret= ServoHoodUp1() if(errorret>0) then return errorret,false end LoadMagazine() -- FindNextX() local LoadPosition = ToolExisst(NextTool) if LoadPosition < 0 then rc = mc.mcCntlCycleStop(0)--stop all gcode because there no such tool on magazine return "No Tool on Magazine1", false end local UnloadPosition= ToolExisst(CurrentTool) if UnloadPosition>0 then ------------------unload----------------------- local error=unload(UnloadPosition); if (error>0)then return error,false end ----------------------------------------------- ----------------load------------------------ local error1=load(LoadPosition); if (error1>0)then return error1,false end ------------------------------------------------- end if (CurrentTool==0) then---no tool on spindle ,only load tool local eror2= openSpindle() if (eror2>0)then return eror2,false end local GCode ="" GCode = GCode ..string.format("G90 G53 G0 Z0 X%.4f Y%.4f\n" ,NextXPosition,Yin) mc.mcCntlGcodeExecuteWait (0, GCode) ---go up z and y out ----------------load------------------------ mc.mcCntlSetLastError(0, "no tool in spindle ,only loading"); local error1=load(LoadPosition); if (error1>0)then return error1,false end ------------------------------------------------- end ServoHoodUp1() return "Tool change compleate", true end end -- local invOk= WriteToInverterBigSpindle() -- if(invOk==0) then -- rc = mc.mcCntlCycleStop(0) --mc.mcCntlSetLastError(0,"No com to inverter") -- end local a,b = ToolChange(NextTool) mc.mcCntlSetLastError(0,string.format(a)) mc.mcCntlSetLastError(0,"446") if(b == true) then mc.mcToolSetCurrent(0, NextTool) --writeToFileToolChange() mc.mcCntlSetLastError(0,tostring(b)) end -------------here check the errors and show the messege and stop the gcoode if(b==false) then rc = mc.mcCntlCycleStop(0) if(a==10)then mc.mcCntlSetLastError(0,"tool change not compleate cant open colet") end if(a==100)then mc.mcCntlSetLastError(0,"stop been press tool change not compleate") end if(a==5)then mc.mcCntlSetLastError(0,"tool cant clamp tool change not compleate") end if(a==30)then mc.mcCntlSetLastError(0,"tool num 0,but sensor show tool exisst") end if(a==40)then mc.mcCntlSetLastError(0,"tool num more then 0,but sensor show no tool exisst") end if(a==20)then mc.mcCntlSetLastError(0,"tool change not compleate servo hood not up") end end end