Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function m6()
- local inst = mc.mcGetInstance()
- --Tool Change
- mc.mcCntlSetLastError(inst, "M06")
- --Create a global variable that will hold the semaphore that allows action (like releasing tools from the spindle)
- toolchange_action_allowed_flag = true;
- --this is changed to false whenever an error occurs and is checked before all actions
- --get newtool/oldtool numbers and return them as selectedtool for newtool and currenttool for oldtool
- --if newtool is the same, end
- --toolchange_checksensors_s1s4()
- if(toolchange_getspindlestatus()==2) then
- mc.mcCntlSetLastError(inst, "Tool attached incorrectly, Tool change will not start")
- toolchange_stop_safely()
- do return end
- end
- if (toolchange_machine_homed()==false) then
- --the machine has not been homed, which might result in a crash while trying to go to any of the tools' locations
- mc.mcCntlSetLastError(inst, "Machine not homed, Tool change will not start")
- toolchange_stop_safely()
- do return end
- end
- if (toolchange_machine_coords_allowed()==false) then
- --the machine is in an unallowed location (next to the tool rack). A crash is likely.
- mc.mcCntlSetLastError(inst, "Y axis too close to the tool rack, Tool change will not start")
- toolchange_stop_safely()
- do return end
- end
- local selectedtool = mc.mcToolGetSelected(inst) --get the n. of the new tool
- local currenttool = mc.mcToolGetCurrent(inst) --get the n. of the tool already selected
- if (selectedtool == currenttool) then --if the to-be-selected tool is the same as the current one, no tool change is necessary
- mc.mcCntlSetLastError(inst, "No tool change necessary")
- do return end
- end
- if ((selectedtool>=1 and selectedtool<=21) or (selectedtool>=30 and selectedtool<=31))==false then
- mc.mcCntlSetLastError(inst, "Selected tool number out of range. Allowed values 1-21, 31-32")
- toolchange_stop_safely()
- do return end
- end
- if ((currenttool>=1 and currenttool<=21) or (currenttool>=30 and currenttool<=31))==false then
- mc.mcCntlSetLastError(inst, "Current tool number out of range. Allowed values 1-21")
- toolchange_stop_safely()
- do return end
- end
- if (currenttool>9) then
- if (toolchange_checksensors_s2()==0) then
- --there's a tool in the tool holder, even though there shouldn't be one there. The tool holder in a state where a tool would be there also counts as if there was a tool attached.
- mc.mcCntlSetLastError(inst, "Tool number incorrect. Remove the tool from the main spindle or correct the tool number")
- do return end
- end
- end
- --get the machine's state to return to it back later. Absolute vs Incremental, etc.
- --Var# is the PoundVar number for the register holding the value of the attribute.
- --Var# Gcode Description
- --4002 G17 set XY plane
- --4003 G90 set absolute mode
- --4005 G95 set feed as units per minute
- --4006 G21 set units to mm
- --!NO 4007 G40 cancel cutter compensation
- --4015 G61 set to exact stop mode, as to avoid taking shortcuts around corners and move predictably
- local CurFeed = mc.mcCntlGetPoundVar(inst, 2134)
- local CurFeedMode = mc.mcCntlGetPoundVar(inst, 4001)
- local CurPlaneMode = mc.mcCntlGetPoundVar(inst, 4002)
- local CurAbsMode = mc.mcCntlGetPoundVar(inst, 4003)
- local CurSpeedMode = mc.mcCntlGetPoundVar(inst, 4005)
- local CurUnit = mc.mcCntlGetPoundVar(inst, 4006)
- --local CurCutterCompMode = mc.mcCntlGetPoundVar(inst, 4007)
- local CurMotionMode = mc.mcCntlGetPoundVar(inst, 4015)
- --mc.mcCntlSetLastError(inst, "Feed: "..CurFeed.." G0/G01: "..CurFeedMode.." Plane: "..CurPlaneMode.." Abs: "..CurAbsMode.." Speed Mode: "..CurSpeedMode.." Units: "..CurUnit.." Motion mode: "..CurMotionMode)
- --initialise (Safe start G codes)
- toolchange_callGcode("G17 G21 G61 G95")
- --toolchange_callGcode("G17 G90 G95 G21 G40 G61")
- --toolchange_action_allowed_flag = false;
- toolchange_zup() --Rise the Z axis up in order to avoid crashes while moving around the table. This will not happen in the tool rack since the toolchange will not start there.
- toolchange_spindlecover_up()
- if (currenttool>8) then --The current tool is in the boring head
- if (selectedtool>8) then --The selected tool is in the boring head
- toolchange_boringhead_allin()
- toolchange_gotosafe()
- toolchange_boringhead_toolout(selectedtool)
- toolchange_boringhead_down()
- toolchange_setoffsets(selectedtool)
- elseif (selectedtool<9) then --The to-be-selected tool is goes in the main spindle
- toolchange_boringhead_up()
- toolchange_boringhead_allin()
- toolchange_boringhead_motorOFF()
- toolchange_goto_take_tool(selectedtool)
- toolchange_spindle_grabtool()
- if(toolchange_getspindlestatus()==2) then --ERROR within the tool attachment system, STOP
- mc.mcCntlSetLastError(inst, "Tool attached incorrectly, remove it manually")
- mc.mcToolSetCurrent(inst, selectedtool)
- mc.mcCntlSetLastError(inst, string.format("New tool %.0f", selectedtool))
- do return end
- end
- toolchange_goto_slide_out_tool()
- end
- end
- --The current tool is goes in the main spindle, put it back first
- if(currenttool<9) then
- --mc.mcCntlSetLastError(inst, "TC currentTool in spindle")
- --just in case
- toolchange_boringhead_up()
- toolchange_boringhead_allin()
- toolchange_spindle_spindleOFF()
- if(toolchange_getspindlestatus()==1) then
- toolchange_goto_remove_tool(currenttool)
- toolchange_spindle_releasetool()
- if(toolchange_getspindlestatus()>0) then
- mc.mcCntlSetLastError(inst, "Tool didn't release correctly")
- do return end
- end
- toolchange_zup()
- toolchange_callGcode("G4 P1")
- end
- if(toolchange_getspindlestatus()==2) then
- mc.mcCntlSetLastError(inst, "Tool attached incorrectly, remove it manually")
- mc.mcToolSetCurrent(inst, selectedtool)
- mc.mcCntlSetLastError(inst, string.format("New tool %.0f", selectedtool))
- do return end
- end
- if(selectedtool<9) then --The to-be-selected tool is goes in the main spindle
- toolchange_goto_take_tool(selectedtool)
- toolchange_spindle_grabtool()
- if(toolchange_getspindlestatus()==2) then --ERROR within the tool attachment system, STOP
- mc.mcCntlSetLastError(inst, "Tool attached incorrectly, remove it manually")
- mc.mcToolSetCurrent(inst, selectedtool)
- mc.mcCntlSetLastError(inst, string.format("New tool %.0f", selectedtool))
- do return end
- end
- toolchange_goto_slide_out_tool()
- elseif (selectedtool>8) then --The to-be-selected tool is in the boring head
- toolchange_gotosafe()
- --toolchange_boringhead_motorON()
- toolchange_boringhead_toolout(selectedtool)
- toolchange_boringhead_down()
- toolchange_setoffsets(selectedtool)
- end
- end
- --return the machine's state to the one it was in before, recorded at the start of this function
- --There is a bug that causes set #4xxx variables to be of size ten times smaller,
- --i.e 17 becomes 1.7.
- --In order to account for this, they have to be multiplied by 10
- mc.mcCntlSetPoundVar(inst, 2134, CurFeed)
- mc.mcCntlSetPoundVar(inst, 4001, CurFeedMode*10)
- mc.mcCntlSetPoundVar(inst, 4002, CurPlaneMode*10)
- mc.mcCntlSetPoundVar(inst, 4003, CurAbsMode*10)
- mc.mcCntlSetPoundVar(inst, 4005, CurSpeedMode*10)
- mc.mcCntlSetPoundVar(inst, 4006, CurUnit*10)
- --mc.mcCntlSetPoundVar(inst, 4007, CurCutterCompMode)
- mc.mcCntlSetPoundVar(inst, 4015, CurMotionMode*10)
- --set a new tool number, since the tool is now attached
- if toolchange_is_action_allowed()==true then
- toolchange_setoffsets(selectedtool)
- mc.mcToolSetCurrent(inst, selectedtool)
- mc.mcCntlSetLastError(inst, string.format("New tool %.0f", selectedtool))
- end
- end
- function toolchange_callGcode(gcode)
- if toolchange_is_action_allowed()==true then
- local inst = mc.mcGetInstance()
- mc.mcCntlSetLastError(inst, gcode)
- --wait for the machine to be
- local rc = mc.mcCntlGcodeExecuteWait(inst, gcode)
- if rc ~=0 then
- toolchange_stop_safely()
- mc.mcCntlSetLastError(inst, "Something went wrong with executing G-code from within M6. The machine was disable for safety. Error state: "..rc)
- end
- end
- end
- function toolchange_boringhead_toolout(tool_n) --this function pushes a specific tool out from the boring head. Range tool n. 9-21 (total 13)
- local inst = mc.mcGetInstance()
- local hsig = nil
- if toolchange_is_action_allowed()==true then
- if (tool_n>=9 and tool_n<=21) then --the tool number represents a single tool in the boring head
- if tool_n==9 then
- hsig = mc.mcSignalGetHandle(inst, mc.OSIG_OUTPUT6)
- elseif tool_n==10 then
- hsig = mc.mcSignalGetHandle(inst, mc.OSIG_OUTPUT5)
- elseif tool_n==11 then
- hsig = mc.mcSignalGetHandle(inst, mc.OSIG_OUTPUT16)
- elseif tool_n==12 then
- hsig = mc.mcSignalGetHandle(inst, mc.OSIG_OUTPUT15)
- elseif tool_n==13 then
- hsig = mc.mcSignalGetHandle(inst, mc.OSIG_OUTPUT14)
- elseif tool_n==14 then
- hsig = mc.mcSignalGetHandle(inst, mc.OSIG_OUTPUT13)
- elseif tool_n==15 then
- hsig = mc.mcSignalGetHandle(inst, mc.OSIG_OUTPUT12)
- elseif tool_n==16 then
- hsig = mc.mcSignalGetHandle(inst, mc.OSIG_OUTPUT11)
- elseif tool_n==17 then
- hsig = mc.mcSignalGetHandle(inst, mc.OSIG_OUTPUT10)
- elseif tool_n==18 then
- hsig = mc.mcSignalGetHandle(inst, mc.OSIG_OUTPUT9)
- elseif tool_n==19 then
- hsig = mc.mcSignalGetHandle(inst, mc.OSIG_OUTPUT8)
- elseif tool_n==20 then
- hsig = mc.mcSignalGetHandle(inst, mc.OSIG_OUTPUT7)
- elseif tool_n==21 then
- hsig = mc.mcSignalGetHandle(inst, mc.OSIG_OUTPUT17)
- end
- mc.mcSignalSetState(hsig, true)
- elseif (tool_n>=30 and tool_n<=31) then
- if tool_n==30 then
- hsig = mc.mcSignalGetHandle(inst, mc.OSIG_OUTPUT16) --11
- mc.mcSignalSetState(hsig, true)
- hsig = mc.mcSignalGetHandle(inst, mc.OSIG_OUTPUT15) --12
- mc.mcSignalSetState(hsig, true)
- hsig = mc.mcSignalGetHandle(inst, mc.OSIG_OUTPUT14) --13
- mc.mcSignalSetState(hsig, true)
- hsig = mc.mcSignalGetHandle(inst, mc.OSIG_OUTPUT13) --14
- mc.mcSignalSetState(hsig, true)
- hsig = mc.mcSignalGetHandle(inst, mc.OSIG_OUTPUT12) --15
- mc.mcSignalSetState(hsig, true)
- elseif tool_n==21 then
- hsig = mc.mcSignalGetHandle(inst, mc.OSIG_OUTPUT8) --19
- mc.mcSignalSetState(hsig, true)
- hsig = mc.mcSignalGetHandle(inst, mc.OSIG_OUTPUT9) --18
- mc.mcSignalSetState(hsig, true)
- hsig = mc.mcSignalGetHandle(inst, mc.OSIG_OUTPUT10) --17
- mc.mcSignalSetState(hsig, true)
- hsig = mc.mcSignalGetHandle(inst, mc.OSIG_OUTPUT11) --16
- mc.mcSignalSetState(hsig, true)
- hsig = mc.mcSignalGetHandle(inst, mc.OSIG_OUTPUT12) --15
- mc.mcSignalSetState(hsig, true)
- end
- end
- end
- end
- function toolchange_boringhead_toolin(tool_n) --this function pulls a specific tool in to the boring head. Range tool n. 9-21 (total 13)
- local inst = mc.mcGetInstance()
- local hsig = nil
- if toolchange_is_action_allowed()==true then
- if tool_n==9 then
- hsig = mc.mcSignalGetHandle(inst, mc.OSIG_OUTPUT6)
- elseif tool_n==10 then
- hsig = mc.mcSignalGetHandle(inst, mc.OSIG_OUTPUT5)
- elseif tool_n==11 then
- hsig = mc.mcSignalGetHandle(inst, mc.OSIG_OUTPUT16)
- elseif tool_n==12 then
- hsig = mc.mcSignalGetHandle(inst, mc.OSIG_OUTPUT15)
- elseif tool_n==13 then
- hsig = mc.mcSignalGetHandle(inst, mc.OSIG_OUTPUT14)
- elseif tool_n==14 then
- hsig = mc.mcSignalGetHandle(inst, mc.OSIG_OUTPUT13)
- elseif tool_n==15 then
- hsig = mc.mcSignalGetHandle(inst, mc.OSIG_OUTPUT12)
- elseif tool_n==16 then
- hsig = mc.mcSignalGetHandle(inst, mc.OSIG_OUTPUT11)
- elseif tool_n==17 then
- hsig = mc.mcSignalGetHandle(inst, mc.OSIG_OUTPUT10)
- elseif tool_n==18 then
- hsig = mc.mcSignalGetHandle(inst, mc.OSIG_OUTPUT9)
- elseif tool_n==19 then
- hsig = mc.mcSignalGetHandle(inst, mc.OSIG_OUTPUT8)
- elseif tool_n==20 then
- hsig = mc.mcSignalGetHandle(inst, mc.OSIG_OUTPUT7)
- elseif tool_n==21 then
- hsig = mc.mcSignalGetHandle(inst, mc.OSIG_OUTPUT17)
- end
- mc.mcSignalSetState(hsig, false)
- end
- end
- function toolchange_boringhead_allin() --this function pulls all the tools in to the boring head. Range total 13
- local inst = mc.mcGetInstance()
- if toolchange_is_action_allowed()==true then
- mc.mcSignalSetState(mc.mcSignalGetHandle(inst, mc.OSIG_OUTPUT5), false)
- mc.mcSignalSetState(mc.mcSignalGetHandle(inst, mc.OSIG_OUTPUT6), false)
- mc.mcSignalSetState(mc.mcSignalGetHandle(inst, mc.OSIG_OUTPUT7), false)
- mc.mcSignalSetState(mc.mcSignalGetHandle(inst, mc.OSIG_OUTPUT8), false)
- mc.mcSignalSetState(mc.mcSignalGetHandle(inst, mc.OSIG_OUTPUT9), false)
- mc.mcSignalSetState(mc.mcSignalGetHandle(inst, mc.OSIG_OUTPUT10), false)
- mc.mcSignalSetState(mc.mcSignalGetHandle(inst, mc.OSIG_OUTPUT11), false)
- mc.mcSignalSetState(mc.mcSignalGetHandle(inst, mc.OSIG_OUTPUT12), false)
- mc.mcSignalSetState(mc.mcSignalGetHandle(inst, mc.OSIG_OUTPUT13), false)
- mc.mcSignalSetState(mc.mcSignalGetHandle(inst, mc.OSIG_OUTPUT14), false)
- mc.mcSignalSetState(mc.mcSignalGetHandle(inst, mc.OSIG_OUTPUT15), false)
- mc.mcSignalSetState(mc.mcSignalGetHandle(inst, mc.OSIG_OUTPUT16), false)
- mc.mcSignalSetState(mc.mcSignalGetHandle(inst, mc.OSIG_OUTPUT17), false)
- end
- end
- function toolchange_boringhead_up() --this function operates the pneumatic cylinder, pulling the boring head complex up, checking (and waiting until) the success and DISABLING the machine if it fails.
- local inst = mc.mcGetInstance()
- if toolchange_is_action_allowed()==true then
- mc.mcSignalSetState(mc.mcSignalGetHandle(inst, mc.OSIG_OUTPUT2), 0)
- local counter=0
- while (mc.mcSignalGetState(mc.mcSignalGetHandle(inst, mc.ISIG_INPUT2))==0) and (counter<20) do
- counter=counter+1
- toolchange_callGcode("G4 P0.1")
- end
- if (counter>=20) then
- toolchange_stop_safely()
- mc.mcCntlSetLastError(inst, "Unable to lift the boring head. Tool change will not start. Check air pressure.")
- end
- end
- end
- function toolchange_spindlecover_up() --this function operates the pneumatic cylinder, pulling the spindle cover up, checking (and waiting until) the success and DISABLING the machine if it fails.
- local inst = mc.mcGetInstance()
- if toolchange_is_action_allowed()==true then
- mc.mcSignalSetState(mc.mcSignalGetHandle(inst, mc.OSIG_OUTPUT3), false)
- local counter=0
- while (mc.mcSignalGetState(mc.mcSignalGetHandle(inst, mc.ISIG_INPUT0))== 1) and (counter<20) do
- counter=counter+1
- toolchange_callGcode("G4 P0.1")
- end
- if (counter>=20) then
- toolchange_stop_safely()
- mc.mcCntlSetLastError(inst, "Unable to lift the spindle cover. Tool change will not start. Check air pressure.")
- end
- end
- end
- function toolchange_boringhead_down() --this function operates the pneumatic cylinder, pulling the boring head complex up, checking (and waiting until) the success and DISABLING the machine if it fails.
- local inst = mc.mcGetInstance()
- if toolchange_is_action_allowed()==true then
- mc.mcSignalSetState(mc.mcSignalGetHandle(inst, mc.OSIG_OUTPUT2), 1)
- local counter=0
- while (mc.mcSignalGetState(mc.mcSignalGetHandle(inst, mc.ISIG_INPUT1))==0) and (counter<20) do
- counter=counter+1
- toolchange_callGcode("G4 P0.1")
- end
- if (counter>=20) then
- toolchange_stop_safely()
- mc.mcCntlSetLastError(inst, "Unable to lower the boring head. Tool change will not start. Check air pressure.")
- end
- end
- end
- function toolchange_boringhead_motorON()
- if toolchange_is_action_allowed()==true then
- local inst = mc.mcGetInstance()
- mc.mcSignalSetState(mc.mcSignalGetHandle(inst, mc.OSIG_OUTPUT4), true)
- end
- end
- function toolchange_boringhead_motorOFF()
- if toolchange_is_action_allowed()==true then
- local inst = mc.mcGetInstance()
- mc.mcSignalSetState(mc.mcSignalGetHandle(inst, mc.OSIG_OUTPUT4), false)
- end
- end
- function toolchange_spindle_spindleOFF()
- if toolchange_is_action_allowed()==true then
- local inst = mc.mcGetInstance()
- toolchange_callGcode("M5")
- end
- end
- function toolchange_spindle_grabtool() --this function operates the main spindle's pneumatic tool attacher, grabbing a tool
- if toolchange_checksensors_s2()==false then
- --tool holder has not closed
- toolchange_stop_safely()
- mc.mcCntlSetLastError(inst, "Tool holder isn't open when it should be. Check air pressure.")
- end
- local inst = mc.mcGetInstance()
- toolchange_callGcode("G04 P1")
- if toolchange_is_action_allowed()==true then
- mc.mcSignalSetState(mc.mcSignalGetHandle(inst, mc.OSIG_OUTPUT1), false)
- end
- toolchange_callGcode("G04 P1")
- if toolchange_checksensors_s2()==true then
- --tool holder has not closed
- toolchange_stop_safely()
- mc.mcCntlSetLastError(inst, "Tool holder didn't close in time. Check air pressure.")
- end
- end
- function toolchange_spindle_releasetool() --this function operates the main spindle's pneumatic tool attacher, releasing a tool
- if toolchange_checksensors_s2()==true then
- --tool holder has not closed
- toolchange_stop_safely()
- mc.mcCntlSetLastError(inst, "Tool holder is open when it should be. Check air pressure.")
- end
- local inst = mc.mcGetInstance()
- toolchange_callGcode("G04 P1")
- if toolchange_is_action_allowed()==true then
- mc.mcSignalSetState(mc.mcSignalGetHandle(inst, mc.OSIG_OUTPUT1), true)
- end
- toolchange_callGcode("G04 P1")
- if toolchange_checksensors_s2()==false then
- --tool holder has not closed
- toolchange_stop_safely()
- mc.mcCntlSetLastError(inst, "Tool holder didn't open in time. Check air pressure.")
- end
- end
- function toolchange_gotosafe() --this function moves the X axis to the side of the machine, in order to avoid any crashes from pushing out a tool from the boring head
- if toolchange_is_action_allowed()==true then
- --Get the Y axis coords
- local inst = mc.mcGetInstance()
- local loc_Y=0
- loc_Y= mc.mcAxisGetMachinePos(inst, 1)
- --Compare them to unallowed values
- if (loc_Y>=-105) then
- --Y in a dangerous area
- mc.mcCntlSetLastError(inst, "Machine near the rack, going to a safe place to change the tool")
- toolchange_callGcode("G90 G21\n G01 G53 Z0 F3000\n G01 G53 Y-105 F3000\n G01 G53 X200 F3000")
- end
- end
- end
- function toolchange_zup() --this function rises up the Z axis
- if toolchange_is_action_allowed()==true then
- local inst = mc.mcGetInstance()
- toolchange_callGcode("G90 G21\n G01 G53 Z0 F3000")
- end
- end
- function toolchange_goto_remove_tool(tool_n) --this function moves to a specific tool's XY location in front of the rack, for it to be slid in, and then slides it in. Range tool n. 1-8 (total 8)
- if toolchange_is_action_allowed()==true then
- --setup\n Z up\n go in front of the holder in XY\n Z down \n Y slide in
- local inst = mc.mcGetInstance()
- if (tool_n==8) then
- toolchange_callGcode("G90 G21\n G01 G53 Z0 F3000\n G01 G53 X326.25 Y-54 F3000\n G01 G53 Z-85.24 F1000\n G01 F500 G53 Y29.99")
- elseif (tool_n==7) then
- toolchange_callGcode("G90 G21\n G01 G53 Z0 F3000\n G01 G53 X446.17 Y-54 F3000\n G01 G53 Z-83.90 F1000\n G01 F500 G53 Y30.89")
- elseif (tool_n==6) then
- toolchange_callGcode("G90 G21\n G01 G53 Z0 F3000\n G01 G53 X564.820 Y-54 F3000\n G01 G53 Z-84.097 F1000\n G01 F500 G53 Y31.374")
- elseif (tool_n==5) then
- toolchange_callGcode("G90 G21\n G01 G53 Z0 F3000\n G01 G53 X685.033 Y-54 F3000\n G01 G53 Z-82.701 F1000\n G01 F500 G53 Y30.204") --
- elseif (tool_n==4) then
- toolchange_callGcode("G90 G21\n G01 G53 Z0 F3000\n G01 G53 X804.750 Y-54 F3000\n G01 G53 Z-82.821 F1000\n G01 F500 G53 Y31.298") --
- elseif (tool_n==3) then
- toolchange_callGcode("G90 G21\n G01 G53 Z0 F3000\n G01 G53 X924.999 Y-54 F3000\n G01 G53 Z-82.963 F1000\n G01 F500 G53 Y31.175")
- elseif (tool_n==2) then
- toolchange_callGcode("G90 G21\n G01 G53 Z0 F3000\n G01 G53 X1045.017 Y-54 F3000\n G01 G53 Z-82.285 F1000\n G01 F500 G53 Y31.005")
- elseif (tool_n==1) then
- toolchange_callGcode("G90 G21\n G01 G53 Z0 F3000\n G01 G53 X1164.406 Y-54 F3000\n G01 G53 Z-82.423 F1000\n G01 F500 G53 Y30.837")
- end
- end
- end
- function toolchange_goto_take_tool(tool_n) --this function moves to a specific tool's XY location on top of the rack for it to be grabbed, and the moves exactly on the position where the tool can be properly attached. IT DOES NOT SLIDE IT OUT. Range tool n. 1-8 (total 8)
- if toolchange_is_action_allowed()==true then
- --setup\n Z up\n go on top of the holder XY\n Z down
- local inst = mc.mcGetInstance()
- if (tool_n==8) then
- toolchange_callGcode("G90 G21\n G01 G53 Z0 F3000\n G01 G53 X326.25 Y29.99 F3000\n G01 G53 Z-85.24 F500")
- elseif (tool_n==7) then
- toolchange_callGcode("G90 G21\n G01 G53 Z0 F3000\n G01 G53 X446.17 Y30.89 F3000\n G01 G53 Z-83.90 F500")
- elseif (tool_n==6) then
- toolchange_callGcode("G90 G21\n G01 G53 Z0 F3000\n G01 G53 X564.820 Y31.374 F3000\n G01 G53 Z-84.097 F500")
- elseif (tool_n==5) then
- toolchange_callGcode("G90 G21\n G01 G53 Z0 F3000\n G01 G53 X685.033 Y30.204 F3000\n G01 G53 Z-82.701 F500") --
- elseif (tool_n==4) then
- toolchange_callGcode("G90 G21\n G01 G53 Z0 F3000\n G01 G53 X804.750 Y31.298 F3000\n G01 G53 Z-82.821 F500") --
- elseif (tool_n==3) then
- toolchange_callGcode("G90 G21\n G01 G53 Z0 F3000\n G01 G53 X924.999 Y31.175 F3000\n G01 G53 Z-82.963 F500")
- elseif (tool_n==2) then
- toolchange_callGcode("G90 G21\n G01 G53 Z0 F3000\n G01 G53 X1045.017 Y31.005 F3000\n G01 G53 Z-82.285 F500")
- elseif (tool_n==1) then
- toolchange_callGcode("G90 G21\n G01 G53 Z0 F3000\n G01 G53 X1164.406 Y30.837 F3000\n G01 G53 Z-82.423 F500")
- end
- end
- end
- function toolchange_goto_slide_out_tool() --this function slides out a tool from the tool rack, only moving the Y axis. Then it rises the Z-axis up, only moving the Z axis.
- if toolchange_is_action_allowed()==true then
- --slide out Y\n Z up
- local inst = mc.mcGetInstance()
- toolchange_callGcode("G90 G21\n G01 G53 Y-54 F1000\n G01 G53 Z0 F3000")
- end
- end
- function toolchange_setoffsets(tool_n) --this function sets a specific tool's offset value on the boring head. Range tool n. 9-21 (total 13)
- local inst = mc.mcGetInstance()
- if (tool_n<9) then
- toolchange_callGcode("G90 G10 L20 P0 X0 Y0")
- elseif (tool_n==21) then
- toolchange_callGcode("G90 G10 L20 P0 X180.28 Y61.9")
- elseif (tool_n==20) then
- toolchange_callGcode("G90 G10 L20 P0 X267.28 Y61.9")
- elseif (tool_n==19) then
- toolchange_callGcode("G90 G10 L20 P0 X299.28 Y61.9")
- elseif (tool_n==18) then
- toolchange_callGcode("G90 G10 L20 P0 X331.28 Y61.9")
- elseif (tool_n==17) then
- toolchange_callGcode("G90 G10 L20 P0 X363.28 Y61.9")
- elseif (tool_n==16) then
- toolchange_callGcode("G90 G10 L20 P0 X395.28 Y61.9")
- elseif (tool_n==15 or tool_n==30 or tool_n==31) then
- toolchange_callGcode("G90 G10 L20 P0 X427.28 Y61.9")
- elseif (tool_n==14) then
- toolchange_callGcode("G90 G10 L20 P0 X427.28 Y93.7")
- elseif (tool_n==13) then
- toolchange_callGcode("G90 G10 L20 P0 X427.28 Y125.9")
- elseif (tool_n==12) then
- toolchange_callGcode("G90 G10 L20 P0 X427.28 Y157.9")
- elseif (tool_n==11) then
- toolchange_callGcode("G90 G10 L20 P0 X427.28 Y189.9")
- elseif (tool_n==10) then
- toolchange_callGcode("G90 G10 L20 P0 X427.28 Y221.9")
- elseif (tool_n==9) then
- toolchange_callGcode("G90 G10 L20 P0 X427.28 Y308.9")
- end
- end
- function toolchange_checksensors_s1s4() --this function checks the main spindle's S1S4 sensor values and returns a state
- local inst = mc.mcGetInstance()
- local hsig=mc.mcSignalGetHandle(inst, mc.ISIG_INPUT5)
- local val=mc.mcSignalGetState(hsig)
- -- mc.mcCntlSetLastError(inst, "getStatusS1S4")
- --mc.mcCntlSetLastError(inst, val)
- --[[if (val==1) then
- mc.mcCntlSetLastError(inst, "TOOL OK")
- elseif (val==0) then
- mc.mcCntlSetLastError(inst, "TOOL NOT OK")
- else
- mc.mcCntlSetLastError(inst, "ERROR")
- end]]
- return val
- end
- function toolchange_checksensors_s2() --this function checks the main spindle's S2 sensor values and returns a state
- local inst = mc.mcGetInstance()
- local hsig=mc.mcSignalGetHandle(inst, mc.ISIG_INPUT4)
- local val=mc.mcSignalGetState(hsig)
- --0: tool holder blocked (maybe there's a tool attached to it)
- --1: tool holder open (there's no tool attached to it)
- return val
- --return 1
- --HACK TO MAKE IT POSSIBLE TO USE ATC WITHOUT AN ACTUAL MAIN SPINDLE ATTACHED
- end
- function toolchange_getspindlestatus() --this function compiles the main spindle's sensor values and returns a state of the tool.
- local inst = mc.mcGetInstance()
- if (toolchange_checksensors_s2()==1) then
- return 0
- elseif (toolchange_checksensors_s2()==0) then
- local s1s4=toolchange_checksensors_s1s4()
- if(s1s4==1) then
- return 1 --Tool OK
- else
- return 2 --Tool NOT OK
- end
- end
- end
- function toolchange_machine_enabled()
- --mc.mcCntlSetLastError(inst, "ifEnbld")
- local inst = mc.mcGetInstance()
- local mcState = mc.mcCntlGetState(inst)
- if mcState==mc.MC_STATE_IDLE then
- mc.mcCntlSetLastError(inst, "Machine stopped")
- return false
- else
- return true
- end
- end
- function toolchange_is_action_allowed()
- local inst = mc.mcGetInstance()
- if (toolchange_action_allowed_flag==true and toolchange_machine_enabled()==true) then
- mc.mcCntlSetLastError(inst, "Action allowed")
- return true
- else
- mc.mcCntlSetLastError(inst, "Action not allowed")
- return false
- end
- end
- function toolchange_stop_safely()
- local inst = mc.mcGetInstance()
- mc.mcCntlSetLastError(inst, "M6 has stopped to ensure safety. Check error messages for reasons.")
- toolchange_action_allowed_flag = false
- local rc
- local error_during_safety_stop = false
- rc = mc.mcCntlFeedHold(inst)
- if rc ~=0 then
- error_during_safety_stop=true
- end
- rc = mc.mcCntlCycleStop(inst)
- if rc ~=0 then
- error_during_safety_stop=true
- end
- rc = mc.mcCntlEnable(inst, false)
- if rc ~=0 then
- error_during_safety_stop=true
- end
- mc.mcCntlSetLastError(inst, "M6 has stopped to ensure safety. Check error messages for reasons.")
- if error_during_safety_stop == true then
- mc.mcCntlSetLastError(inst, "Additinally, the safety stop function encountered errors.")
- end
- end
- function toolchange_machine_homed()
- local inst = mc.mcGetInstance()
- if (mc.mcAxisIsHomed(inst, 0)==1 and mc.mcAxisIsHomed(inst, 1)==1 and mc.mcAxisIsHomed(inst, 2)==1) then
- return true
- else
- return false
- end
- end
- function toolchange_machine_coords_allowed() --this function checks if the current Y axis location value is accepted, in order to avoid a Z rise in the tool's location holder, or any other crashes
- --Get the Y axis coords
- local inst = mc.mcGetInstance()
- local loc_Y=0
- loc_Y= mc.mcAxisGetMachinePos(inst, 1)
- --Compare them to unallowed values
- if (loc_Y>=-50) then
- --Y not allowed, and so return that the coords are not allowed (false)
- return false
- elseif (loc_Y<-50) then
- --Y is allowed
- return true
- else
- --error, (no further motion by caller allowed). It is up to the caller to stop, however.
- return false
- end
- end
- if (mc.mcInEditor() == 1) then --debugging
- m6()
- end
Advertisement
Add Comment
Please, Sign In to add comment