Guest User

M6 script v1

a guest
Apr 19th, 2021
479
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 29.40 KB | None | 0 0
  1. function m6()
  2.     local inst = mc.mcGetInstance()
  3.  
  4.     --Tool Change
  5.     mc.mcCntlSetLastError(inst, "M06")
  6.     --Create a global variable that will hold the semaphore that allows action (like releasing tools from the spindle)
  7.     toolchange_action_allowed_flag = true;
  8.     --this is changed to false whenever an error occurs and is checked before all actions
  9.  
  10.     --get newtool/oldtool numbers and return them as selectedtool for newtool and currenttool for oldtool
  11.     --if newtool is the same, end
  12.     --toolchange_checksensors_s1s4()
  13.    
  14.     if(toolchange_getspindlestatus()==2) then
  15.         mc.mcCntlSetLastError(inst, "Tool attached incorrectly, Tool change will not start")
  16.         toolchange_stop_safely()
  17.         do return end
  18.     end
  19.     if (toolchange_machine_homed()==false) then
  20.         --the machine has not been homed, which might result in a crash while trying to go to any of the tools' locations
  21.         mc.mcCntlSetLastError(inst, "Machine not homed, Tool change will not start")
  22.         toolchange_stop_safely()
  23.         do return end
  24.     end
  25.     if (toolchange_machine_coords_allowed()==false) then
  26.         --the machine is in an unallowed location (next to the tool rack). A crash is likely.
  27.         mc.mcCntlSetLastError(inst, "Y axis too close to the tool rack, Tool change will not start")
  28.         toolchange_stop_safely()
  29.         do return end
  30.     end
  31.     local selectedtool = mc.mcToolGetSelected(inst) --get the n. of the new tool
  32.     local currenttool = mc.mcToolGetCurrent(inst) --get the n. of the tool already selected
  33.  
  34.  
  35.     if (selectedtool == currenttool) then --if the to-be-selected tool is the same as the current one, no tool change is necessary
  36.         mc.mcCntlSetLastError(inst, "No tool change necessary")
  37.         do return end
  38.     end
  39.     if ((selectedtool>=1 and selectedtool<=21) or (selectedtool>=30 and selectedtool<=31))==false then
  40.         mc.mcCntlSetLastError(inst, "Selected tool number out of range. Allowed values 1-21, 31-32")
  41.         toolchange_stop_safely()
  42.         do return end
  43.     end
  44.     if ((currenttool>=1 and currenttool<=21) or (currenttool>=30 and currenttool<=31))==false then
  45.         mc.mcCntlSetLastError(inst, "Current tool number out of range. Allowed values 1-21")
  46.         toolchange_stop_safely()
  47.         do return end
  48.     end
  49.  
  50.     if (currenttool>9) then
  51.         if (toolchange_checksensors_s2()==0) then
  52.             --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.
  53.             mc.mcCntlSetLastError(inst, "Tool number incorrect. Remove the tool from the main spindle or correct the tool number")
  54.             do return end
  55.         end
  56.     end
  57.     --get the machine's state to return to it back later. Absolute vs Incremental, etc.
  58.  
  59.     --Var# is the PoundVar number for the register holding the value of the attribute.
  60.     --Var# Gcode Description
  61.     --4002 G17 set XY plane
  62.     --4003 G90 set absolute mode
  63.     --4005 G95 set feed as units per minute
  64.     --4006 G21 set units to mm
  65.     --!NO 4007 G40 cancel cutter compensation
  66.     --4015 G61 set to exact stop mode, as to avoid taking shortcuts around corners and move predictably
  67.     local CurFeed = mc.mcCntlGetPoundVar(inst, 2134)
  68.     local CurFeedMode = mc.mcCntlGetPoundVar(inst, 4001)
  69.     local CurPlaneMode = mc.mcCntlGetPoundVar(inst, 4002)
  70.     local CurAbsMode = mc.mcCntlGetPoundVar(inst, 4003)
  71.     local CurSpeedMode = mc.mcCntlGetPoundVar(inst, 4005)
  72.     local CurUnit = mc.mcCntlGetPoundVar(inst, 4006)
  73.     --local CurCutterCompMode = mc.mcCntlGetPoundVar(inst, 4007)
  74.     local CurMotionMode = mc.mcCntlGetPoundVar(inst, 4015)
  75.     --mc.mcCntlSetLastError(inst, "Feed: "..CurFeed.." G0/G01: "..CurFeedMode.." Plane: "..CurPlaneMode.." Abs: "..CurAbsMode.." Speed Mode: "..CurSpeedMode.." Units: "..CurUnit.." Motion mode: "..CurMotionMode)
  76.  
  77.     --initialise (Safe start G codes)
  78.     toolchange_callGcode("G17 G21 G61 G95")
  79.     --toolchange_callGcode("G17 G90 G95 G21 G40 G61")
  80.     --toolchange_action_allowed_flag = false;
  81.     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.
  82.     toolchange_spindlecover_up()
  83.     if (currenttool>8) then --The current tool is in the boring head
  84.         if (selectedtool>8) then --The selected tool is in the boring head
  85.             toolchange_boringhead_allin()
  86.             toolchange_gotosafe()
  87.             toolchange_boringhead_toolout(selectedtool)
  88.             toolchange_boringhead_down()
  89.             toolchange_setoffsets(selectedtool)
  90.         elseif (selectedtool<9) then --The to-be-selected tool is goes in the main spindle
  91.             toolchange_boringhead_up()
  92.             toolchange_boringhead_allin()
  93.             toolchange_boringhead_motorOFF()
  94.             toolchange_goto_take_tool(selectedtool)
  95.             toolchange_spindle_grabtool()
  96.             if(toolchange_getspindlestatus()==2) then --ERROR within the tool attachment system, STOP
  97.                 mc.mcCntlSetLastError(inst, "Tool attached incorrectly, remove it manually")
  98.                 mc.mcToolSetCurrent(inst, selectedtool)
  99.                 mc.mcCntlSetLastError(inst, string.format("New tool %.0f", selectedtool))
  100.                 do return end
  101.             end
  102.             toolchange_goto_slide_out_tool()
  103.         end
  104.     end
  105.     --The current tool is goes in the main spindle, put it back first
  106.     if(currenttool<9) then
  107.         --mc.mcCntlSetLastError(inst, "TC currentTool in spindle")
  108.         --just in case
  109.         toolchange_boringhead_up()
  110.         toolchange_boringhead_allin()
  111.  
  112.         toolchange_spindle_spindleOFF()
  113.         if(toolchange_getspindlestatus()==1) then
  114.             toolchange_goto_remove_tool(currenttool)
  115.             toolchange_spindle_releasetool()
  116.             if(toolchange_getspindlestatus()>0) then
  117.                 mc.mcCntlSetLastError(inst, "Tool didn't release correctly")
  118.                 do return end
  119.             end
  120.             toolchange_zup()
  121.             toolchange_callGcode("G4 P1")
  122.         end
  123.         if(toolchange_getspindlestatus()==2) then
  124.             mc.mcCntlSetLastError(inst, "Tool attached incorrectly, remove it manually")
  125.             mc.mcToolSetCurrent(inst, selectedtool)
  126.             mc.mcCntlSetLastError(inst, string.format("New tool %.0f", selectedtool))
  127.             do return end
  128.         end
  129.         if(selectedtool<9) then --The to-be-selected tool is goes in the main spindle
  130.             toolchange_goto_take_tool(selectedtool)
  131.             toolchange_spindle_grabtool()
  132.             if(toolchange_getspindlestatus()==2) then --ERROR within the tool attachment system, STOP
  133.                 mc.mcCntlSetLastError(inst, "Tool attached incorrectly, remove it manually")
  134.                 mc.mcToolSetCurrent(inst, selectedtool)
  135.                 mc.mcCntlSetLastError(inst, string.format("New tool %.0f", selectedtool))
  136.                 do return end
  137.             end
  138.             toolchange_goto_slide_out_tool()
  139.         elseif (selectedtool>8) then --The to-be-selected tool is in the boring head
  140.             toolchange_gotosafe()
  141.             --toolchange_boringhead_motorON()
  142.             toolchange_boringhead_toolout(selectedtool)
  143.             toolchange_boringhead_down()
  144.             toolchange_setoffsets(selectedtool)
  145.         end
  146.     end
  147.     --return the machine's state to the one it was in before, recorded at the start of this function
  148.  
  149.     --There is a bug that causes set #4xxx variables to be of size ten times smaller,
  150.     --i.e 17 becomes 1.7.
  151.     --In order to account for this, they have to be multiplied by 10
  152.     mc.mcCntlSetPoundVar(inst, 2134, CurFeed)
  153.     mc.mcCntlSetPoundVar(inst, 4001, CurFeedMode*10)
  154.     mc.mcCntlSetPoundVar(inst, 4002, CurPlaneMode*10)
  155.     mc.mcCntlSetPoundVar(inst, 4003, CurAbsMode*10)
  156.     mc.mcCntlSetPoundVar(inst, 4005, CurSpeedMode*10)
  157.     mc.mcCntlSetPoundVar(inst, 4006, CurUnit*10)
  158.     --mc.mcCntlSetPoundVar(inst, 4007, CurCutterCompMode)
  159.     mc.mcCntlSetPoundVar(inst, 4015, CurMotionMode*10)
  160.  
  161.     --set a new tool number, since the tool is now attached
  162.     if toolchange_is_action_allowed()==true then
  163.         toolchange_setoffsets(selectedtool)
  164.         mc.mcToolSetCurrent(inst, selectedtool)
  165.         mc.mcCntlSetLastError(inst, string.format("New tool %.0f", selectedtool))
  166.     end
  167. end
  168.  
  169. function toolchange_callGcode(gcode)
  170.     if toolchange_is_action_allowed()==true then
  171.         local inst = mc.mcGetInstance()
  172.         mc.mcCntlSetLastError(inst, gcode)
  173.         --wait for the machine to be
  174.         local rc = mc.mcCntlGcodeExecuteWait(inst, gcode)
  175.         if rc ~=0 then
  176.             toolchange_stop_safely()
  177.             mc.mcCntlSetLastError(inst, "Something went wrong with executing G-code from within M6. The machine was disable for safety. Error state: "..rc)
  178.         end
  179.     end
  180. end
  181.  
  182.  
  183.  
  184. function toolchange_boringhead_toolout(tool_n) --this function pushes a specific tool out from the boring head. Range tool n. 9-21 (total 13)
  185.     local inst = mc.mcGetInstance()
  186.     local hsig = nil
  187.     if toolchange_is_action_allowed()==true then
  188.         if (tool_n>=9 and tool_n<=21) then  --the tool number represents a single tool in the boring head
  189.             if tool_n==9 then
  190.                 hsig = mc.mcSignalGetHandle(inst, mc.OSIG_OUTPUT6)
  191.             elseif tool_n==10 then
  192.                 hsig = mc.mcSignalGetHandle(inst, mc.OSIG_OUTPUT5)
  193.             elseif tool_n==11 then
  194.                 hsig = mc.mcSignalGetHandle(inst, mc.OSIG_OUTPUT16)
  195.             elseif tool_n==12 then
  196.                 hsig = mc.mcSignalGetHandle(inst, mc.OSIG_OUTPUT15)
  197.             elseif tool_n==13 then
  198.                 hsig = mc.mcSignalGetHandle(inst, mc.OSIG_OUTPUT14)
  199.             elseif tool_n==14 then
  200.                 hsig = mc.mcSignalGetHandle(inst, mc.OSIG_OUTPUT13)
  201.             elseif tool_n==15 then
  202.                 hsig = mc.mcSignalGetHandle(inst, mc.OSIG_OUTPUT12)
  203.             elseif tool_n==16 then
  204.                 hsig = mc.mcSignalGetHandle(inst, mc.OSIG_OUTPUT11)
  205.             elseif tool_n==17 then
  206.                 hsig = mc.mcSignalGetHandle(inst, mc.OSIG_OUTPUT10)
  207.             elseif tool_n==18 then
  208.                 hsig = mc.mcSignalGetHandle(inst, mc.OSIG_OUTPUT9)
  209.             elseif tool_n==19 then
  210.                 hsig = mc.mcSignalGetHandle(inst, mc.OSIG_OUTPUT8)
  211.             elseif tool_n==20 then
  212.                 hsig = mc.mcSignalGetHandle(inst, mc.OSIG_OUTPUT7)
  213.             elseif tool_n==21 then
  214.                 hsig = mc.mcSignalGetHandle(inst, mc.OSIG_OUTPUT17)
  215.             end
  216.             mc.mcSignalSetState(hsig, true)
  217.         elseif (tool_n>=30 and tool_n<=31) then
  218.             if tool_n==30 then
  219.                 hsig = mc.mcSignalGetHandle(inst, mc.OSIG_OUTPUT16) --11
  220.                 mc.mcSignalSetState(hsig, true)
  221.                 hsig = mc.mcSignalGetHandle(inst, mc.OSIG_OUTPUT15) --12
  222.                 mc.mcSignalSetState(hsig, true)
  223.                 hsig = mc.mcSignalGetHandle(inst, mc.OSIG_OUTPUT14) --13
  224.                 mc.mcSignalSetState(hsig, true)
  225.                 hsig = mc.mcSignalGetHandle(inst, mc.OSIG_OUTPUT13) --14
  226.                 mc.mcSignalSetState(hsig, true)
  227.                 hsig = mc.mcSignalGetHandle(inst, mc.OSIG_OUTPUT12) --15
  228.                 mc.mcSignalSetState(hsig, true)
  229.             elseif tool_n==21 then
  230.                 hsig = mc.mcSignalGetHandle(inst, mc.OSIG_OUTPUT8) --19
  231.                 mc.mcSignalSetState(hsig, true)
  232.                 hsig = mc.mcSignalGetHandle(inst, mc.OSIG_OUTPUT9) --18
  233.                 mc.mcSignalSetState(hsig, true)
  234.                 hsig = mc.mcSignalGetHandle(inst, mc.OSIG_OUTPUT10) --17
  235.                 mc.mcSignalSetState(hsig, true)
  236.                 hsig = mc.mcSignalGetHandle(inst, mc.OSIG_OUTPUT11) --16
  237.                 mc.mcSignalSetState(hsig, true)
  238.                 hsig = mc.mcSignalGetHandle(inst, mc.OSIG_OUTPUT12) --15
  239.                 mc.mcSignalSetState(hsig, true)
  240.             end
  241.         end  
  242.     end
  243. end
  244. function toolchange_boringhead_toolin(tool_n)  --this function pulls a specific tool in to the boring head. Range tool n. 9-21 (total 13)
  245.     local inst = mc.mcGetInstance()
  246.     local hsig = nil
  247.     if toolchange_is_action_allowed()==true then
  248.         if tool_n==9 then
  249.             hsig = mc.mcSignalGetHandle(inst, mc.OSIG_OUTPUT6)
  250.         elseif tool_n==10 then
  251.             hsig = mc.mcSignalGetHandle(inst, mc.OSIG_OUTPUT5)
  252.         elseif tool_n==11 then
  253.             hsig = mc.mcSignalGetHandle(inst, mc.OSIG_OUTPUT16)
  254.         elseif tool_n==12 then
  255.             hsig = mc.mcSignalGetHandle(inst, mc.OSIG_OUTPUT15)
  256.         elseif tool_n==13 then
  257.             hsig = mc.mcSignalGetHandle(inst, mc.OSIG_OUTPUT14)
  258.         elseif tool_n==14 then
  259.             hsig = mc.mcSignalGetHandle(inst, mc.OSIG_OUTPUT13)
  260.         elseif tool_n==15 then
  261.             hsig = mc.mcSignalGetHandle(inst, mc.OSIG_OUTPUT12)
  262.         elseif tool_n==16 then
  263.             hsig = mc.mcSignalGetHandle(inst, mc.OSIG_OUTPUT11)
  264.         elseif tool_n==17 then
  265.             hsig = mc.mcSignalGetHandle(inst, mc.OSIG_OUTPUT10)
  266.         elseif tool_n==18 then
  267.             hsig = mc.mcSignalGetHandle(inst, mc.OSIG_OUTPUT9)
  268.         elseif tool_n==19 then
  269.             hsig = mc.mcSignalGetHandle(inst, mc.OSIG_OUTPUT8)
  270.         elseif tool_n==20 then
  271.             hsig = mc.mcSignalGetHandle(inst, mc.OSIG_OUTPUT7)
  272.         elseif tool_n==21 then
  273.             hsig = mc.mcSignalGetHandle(inst, mc.OSIG_OUTPUT17)
  274.         end
  275.         mc.mcSignalSetState(hsig, false)
  276.     end
  277. end
  278. function toolchange_boringhead_allin()  --this function pulls all the tools in to the boring head. Range total 13
  279.     local inst = mc.mcGetInstance()
  280.     if toolchange_is_action_allowed()==true then
  281.         mc.mcSignalSetState(mc.mcSignalGetHandle(inst, mc.OSIG_OUTPUT5), false)
  282.         mc.mcSignalSetState(mc.mcSignalGetHandle(inst, mc.OSIG_OUTPUT6), false)
  283.         mc.mcSignalSetState(mc.mcSignalGetHandle(inst, mc.OSIG_OUTPUT7), false)
  284.         mc.mcSignalSetState(mc.mcSignalGetHandle(inst, mc.OSIG_OUTPUT8), false)
  285.         mc.mcSignalSetState(mc.mcSignalGetHandle(inst, mc.OSIG_OUTPUT9), false)
  286.         mc.mcSignalSetState(mc.mcSignalGetHandle(inst, mc.OSIG_OUTPUT10), false)
  287.         mc.mcSignalSetState(mc.mcSignalGetHandle(inst, mc.OSIG_OUTPUT11), false)
  288.         mc.mcSignalSetState(mc.mcSignalGetHandle(inst, mc.OSIG_OUTPUT12), false)
  289.         mc.mcSignalSetState(mc.mcSignalGetHandle(inst, mc.OSIG_OUTPUT13), false)
  290.         mc.mcSignalSetState(mc.mcSignalGetHandle(inst, mc.OSIG_OUTPUT14), false)
  291.         mc.mcSignalSetState(mc.mcSignalGetHandle(inst, mc.OSIG_OUTPUT15), false)
  292.         mc.mcSignalSetState(mc.mcSignalGetHandle(inst, mc.OSIG_OUTPUT16), false)
  293.         mc.mcSignalSetState(mc.mcSignalGetHandle(inst, mc.OSIG_OUTPUT17), false)
  294.     end
  295. end
  296. 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.
  297.     local inst = mc.mcGetInstance()
  298.     if toolchange_is_action_allowed()==true then
  299.         mc.mcSignalSetState(mc.mcSignalGetHandle(inst, mc.OSIG_OUTPUT2), 0)
  300.         local counter=0
  301.         while (mc.mcSignalGetState(mc.mcSignalGetHandle(inst, mc.ISIG_INPUT2))==0) and (counter<20) do
  302.             counter=counter+1
  303.             toolchange_callGcode("G4 P0.1")
  304.         end
  305.         if (counter>=20) then
  306.             toolchange_stop_safely()
  307.             mc.mcCntlSetLastError(inst, "Unable to lift the boring head. Tool change will not start. Check air pressure.")
  308.         end
  309.     end
  310. end
  311. 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.
  312.     local inst = mc.mcGetInstance()
  313.     if toolchange_is_action_allowed()==true then
  314.         mc.mcSignalSetState(mc.mcSignalGetHandle(inst, mc.OSIG_OUTPUT3), false)
  315.         local counter=0
  316.         while (mc.mcSignalGetState(mc.mcSignalGetHandle(inst, mc.ISIG_INPUT0))== 1) and (counter<20) do
  317.             counter=counter+1
  318.             toolchange_callGcode("G4 P0.1")
  319.         end
  320.         if (counter>=20) then
  321.             toolchange_stop_safely()
  322.             mc.mcCntlSetLastError(inst, "Unable to lift the spindle cover. Tool change will not start. Check air pressure.")
  323.         end
  324.     end
  325. end
  326. 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.
  327.     local inst = mc.mcGetInstance()
  328.     if toolchange_is_action_allowed()==true then
  329.         mc.mcSignalSetState(mc.mcSignalGetHandle(inst, mc.OSIG_OUTPUT2), 1)
  330.         local counter=0
  331.         while (mc.mcSignalGetState(mc.mcSignalGetHandle(inst, mc.ISIG_INPUT1))==0) and (counter<20) do
  332.             counter=counter+1
  333.             toolchange_callGcode("G4 P0.1")
  334.         end
  335.         if (counter>=20) then
  336.             toolchange_stop_safely()
  337.             mc.mcCntlSetLastError(inst, "Unable to lower the boring head. Tool change will not start. Check air pressure.")
  338.         end
  339.     end
  340. end
  341. function toolchange_boringhead_motorON()
  342.     if toolchange_is_action_allowed()==true then
  343.         local inst = mc.mcGetInstance()
  344.         mc.mcSignalSetState(mc.mcSignalGetHandle(inst, mc.OSIG_OUTPUT4), true)
  345.     end
  346. end
  347. function toolchange_boringhead_motorOFF()
  348.     if toolchange_is_action_allowed()==true then
  349.         local inst = mc.mcGetInstance()
  350.         mc.mcSignalSetState(mc.mcSignalGetHandle(inst, mc.OSIG_OUTPUT4), false)
  351.     end
  352. end
  353. function toolchange_spindle_spindleOFF()
  354.     if toolchange_is_action_allowed()==true then
  355.         local inst = mc.mcGetInstance()
  356.         toolchange_callGcode("M5")
  357.     end
  358. end
  359. function toolchange_spindle_grabtool() --this function operates the main spindle's pneumatic tool attacher, grabbing a tool
  360.         if toolchange_checksensors_s2()==false then
  361.             --tool holder has not closed
  362.             toolchange_stop_safely()
  363.             mc.mcCntlSetLastError(inst, "Tool holder isn't open when it should be. Check air pressure.")
  364.         end
  365.         local inst = mc.mcGetInstance()
  366.         toolchange_callGcode("G04 P1")
  367.         if toolchange_is_action_allowed()==true then
  368.             mc.mcSignalSetState(mc.mcSignalGetHandle(inst, mc.OSIG_OUTPUT1), false)
  369.         end
  370.         toolchange_callGcode("G04 P1")
  371.         if toolchange_checksensors_s2()==true then
  372.             --tool holder has not closed
  373.             toolchange_stop_safely()
  374.             mc.mcCntlSetLastError(inst, "Tool holder didn't close in time. Check air pressure.")
  375.         end
  376.        
  377. end
  378. function toolchange_spindle_releasetool() --this function operates the main spindle's pneumatic tool attacher, releasing a tool
  379.         if toolchange_checksensors_s2()==true then
  380.             --tool holder has not closed
  381.             toolchange_stop_safely()
  382.             mc.mcCntlSetLastError(inst, "Tool holder is open when it should be. Check air pressure.")
  383.         end
  384.         local inst = mc.mcGetInstance()
  385.         toolchange_callGcode("G04 P1")
  386.         if toolchange_is_action_allowed()==true then
  387.             mc.mcSignalSetState(mc.mcSignalGetHandle(inst, mc.OSIG_OUTPUT1), true)
  388.         end
  389.         toolchange_callGcode("G04 P1")
  390.         if toolchange_checksensors_s2()==false then
  391.             --tool holder has not closed
  392.             toolchange_stop_safely()
  393.             mc.mcCntlSetLastError(inst, "Tool holder didn't open in time. Check air pressure.")
  394.         end
  395. end
  396. 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
  397.     if toolchange_is_action_allowed()==true then
  398.         --Get the Y axis coords
  399.         local inst = mc.mcGetInstance()
  400.         local loc_Y=0
  401.         loc_Y= mc.mcAxisGetMachinePos(inst, 1)
  402.         --Compare them to unallowed values
  403.         if (loc_Y>=-105) then
  404.             --Y in a dangerous area
  405.             mc.mcCntlSetLastError(inst, "Machine near the rack, going to a safe place to change the tool")
  406.             toolchange_callGcode("G90 G21\n G01 G53 Z0 F3000\n G01 G53 Y-105 F3000\n G01 G53 X200 F3000")
  407.         end
  408.     end
  409. end
  410. function toolchange_zup() --this function rises up the Z axis
  411.     if toolchange_is_action_allowed()==true then
  412.         local inst = mc.mcGetInstance()
  413.         toolchange_callGcode("G90 G21\n G01 G53 Z0 F3000")
  414.     end
  415. end
  416. 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)
  417.     if toolchange_is_action_allowed()==true then
  418.         --setup\n Z up\n go in front of the holder in XY\n Z down \n Y slide in
  419.         local inst = mc.mcGetInstance()
  420.         if (tool_n==8) then
  421.             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")
  422.         elseif (tool_n==7) then
  423.             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")
  424.         elseif (tool_n==6) then
  425.             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")
  426.         elseif (tool_n==5) then
  427.             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") --
  428.         elseif (tool_n==4) then
  429.             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") --
  430.         elseif (tool_n==3) then
  431.             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")
  432.         elseif (tool_n==2) then
  433.             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")
  434.         elseif (tool_n==1) then
  435.             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")
  436.         end
  437.     end
  438. end
  439. 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)
  440.     if toolchange_is_action_allowed()==true then
  441.         --setup\n Z up\n go on top of the holder XY\n Z down
  442.         local inst = mc.mcGetInstance()
  443.         if (tool_n==8) then
  444.             toolchange_callGcode("G90 G21\n G01 G53 Z0 F3000\n G01 G53 X326.25 Y29.99 F3000\n G01 G53 Z-85.24 F500")
  445.         elseif (tool_n==7) then
  446.             toolchange_callGcode("G90 G21\n G01 G53 Z0 F3000\n G01 G53 X446.17 Y30.89 F3000\n G01 G53 Z-83.90 F500")
  447.         elseif (tool_n==6) then
  448.             toolchange_callGcode("G90 G21\n G01 G53 Z0 F3000\n G01 G53 X564.820 Y31.374 F3000\n G01 G53 Z-84.097 F500")
  449.         elseif (tool_n==5) then
  450.             toolchange_callGcode("G90 G21\n G01 G53 Z0 F3000\n G01 G53 X685.033 Y30.204 F3000\n G01 G53 Z-82.701 F500") --
  451.         elseif (tool_n==4) then
  452.             toolchange_callGcode("G90 G21\n G01 G53 Z0 F3000\n G01 G53 X804.750 Y31.298 F3000\n G01 G53 Z-82.821 F500") --
  453.         elseif (tool_n==3) then
  454.             toolchange_callGcode("G90 G21\n G01 G53 Z0 F3000\n G01 G53 X924.999 Y31.175 F3000\n G01 G53 Z-82.963 F500")
  455.         elseif (tool_n==2) then
  456.             toolchange_callGcode("G90 G21\n G01 G53 Z0 F3000\n G01 G53 X1045.017 Y31.005 F3000\n G01 G53 Z-82.285 F500")
  457.         elseif (tool_n==1) then
  458.             toolchange_callGcode("G90 G21\n G01 G53 Z0 F3000\n G01 G53 X1164.406 Y30.837 F3000\n G01 G53 Z-82.423 F500")
  459.         end
  460.     end
  461. end
  462. 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.
  463.     if toolchange_is_action_allowed()==true then
  464.         --slide out Y\n Z up
  465.         local inst = mc.mcGetInstance()
  466.         toolchange_callGcode("G90 G21\n G01 G53 Y-54 F1000\n G01 G53 Z0 F3000")
  467.     end
  468. end
  469. 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)
  470.     local inst = mc.mcGetInstance()
  471.     if (tool_n<9) then
  472.         toolchange_callGcode("G90 G10 L20 P0 X0 Y0")
  473.     elseif (tool_n==21) then
  474.         toolchange_callGcode("G90 G10 L20 P0 X180.28 Y61.9")
  475.     elseif (tool_n==20) then
  476.         toolchange_callGcode("G90 G10 L20 P0 X267.28 Y61.9")
  477.     elseif (tool_n==19) then
  478.         toolchange_callGcode("G90 G10 L20 P0 X299.28 Y61.9")
  479.     elseif (tool_n==18) then
  480.         toolchange_callGcode("G90 G10 L20 P0 X331.28 Y61.9")
  481.     elseif (tool_n==17) then
  482.         toolchange_callGcode("G90 G10 L20 P0 X363.28 Y61.9")
  483.     elseif (tool_n==16) then
  484.         toolchange_callGcode("G90 G10 L20 P0 X395.28 Y61.9")
  485.     elseif (tool_n==15 or tool_n==30 or tool_n==31) then
  486.         toolchange_callGcode("G90 G10 L20 P0 X427.28 Y61.9")
  487.     elseif (tool_n==14) then
  488.         toolchange_callGcode("G90 G10 L20 P0 X427.28 Y93.7")
  489.     elseif (tool_n==13) then
  490.         toolchange_callGcode("G90 G10 L20 P0 X427.28 Y125.9")
  491.     elseif (tool_n==12) then
  492.         toolchange_callGcode("G90 G10 L20 P0 X427.28 Y157.9")
  493.     elseif (tool_n==11) then
  494.         toolchange_callGcode("G90 G10 L20 P0 X427.28 Y189.9")
  495.     elseif (tool_n==10) then
  496.         toolchange_callGcode("G90 G10 L20 P0 X427.28 Y221.9")
  497.     elseif (tool_n==9) then
  498.         toolchange_callGcode("G90 G10 L20 P0 X427.28 Y308.9")
  499.     end
  500. end
  501. function toolchange_checksensors_s1s4() --this function checks the main spindle's S1S4 sensor values and returns a state
  502.     local inst = mc.mcGetInstance()
  503.     local hsig=mc.mcSignalGetHandle(inst, mc.ISIG_INPUT5)
  504.     local val=mc.mcSignalGetState(hsig)
  505.         -- mc.mcCntlSetLastError(inst, "getStatusS1S4")
  506.         --mc.mcCntlSetLastError(inst, val)
  507.     --[[if (val==1) then
  508.         mc.mcCntlSetLastError(inst, "TOOL OK")
  509.     elseif (val==0) then
  510.         mc.mcCntlSetLastError(inst, "TOOL NOT OK")
  511.     else
  512.         mc.mcCntlSetLastError(inst, "ERROR")
  513.     end]]
  514.     return val
  515. end
  516. function toolchange_checksensors_s2() --this function checks the main spindle's S2 sensor values and returns a state
  517.     local inst = mc.mcGetInstance()
  518.     local hsig=mc.mcSignalGetHandle(inst, mc.ISIG_INPUT4)
  519.     local val=mc.mcSignalGetState(hsig)
  520.  
  521.     --0: tool holder blocked (maybe there's a tool attached to it)
  522.     --1: tool holder open (there's no tool attached to it)
  523.     return val
  524.  
  525.  
  526.     --return 1
  527.     --HACK TO MAKE IT POSSIBLE TO USE ATC WITHOUT AN ACTUAL MAIN SPINDLE ATTACHED
  528. end
  529. function toolchange_getspindlestatus() --this function compiles the main spindle's sensor values and returns a state of the tool.
  530.     local inst = mc.mcGetInstance()
  531.     if (toolchange_checksensors_s2()==1) then
  532.         return 0
  533.     elseif (toolchange_checksensors_s2()==0) then
  534.         local s1s4=toolchange_checksensors_s1s4()
  535.         if(s1s4==1) then
  536.             return 1 --Tool OK
  537.         else
  538.             return 2 --Tool NOT OK
  539.         end
  540.     end
  541. end
  542. function toolchange_machine_enabled()
  543.     --mc.mcCntlSetLastError(inst, "ifEnbld")
  544.     local inst = mc.mcGetInstance()
  545.     local mcState = mc.mcCntlGetState(inst)
  546.     if mcState==mc.MC_STATE_IDLE then
  547.         mc.mcCntlSetLastError(inst, "Machine stopped")
  548.         return false
  549.     else
  550.         return true
  551.     end
  552. end
  553. function toolchange_is_action_allowed()
  554.     local inst = mc.mcGetInstance()
  555.     if (toolchange_action_allowed_flag==true and toolchange_machine_enabled()==true) then
  556.         mc.mcCntlSetLastError(inst, "Action allowed")
  557.         return true
  558.     else
  559.         mc.mcCntlSetLastError(inst, "Action not allowed")
  560.         return false
  561.     end
  562. end
  563. function toolchange_stop_safely()
  564.     local inst = mc.mcGetInstance()
  565.     mc.mcCntlSetLastError(inst, "M6 has stopped to ensure safety. Check error messages for reasons.")
  566.     toolchange_action_allowed_flag = false
  567.     local rc
  568.     local error_during_safety_stop = false
  569.     rc = mc.mcCntlFeedHold(inst)
  570.     if rc ~=0 then
  571.         error_during_safety_stop=true
  572.     end
  573.     rc = mc.mcCntlCycleStop(inst)
  574.     if rc ~=0 then
  575.         error_during_safety_stop=true
  576.     end
  577.     rc = mc.mcCntlEnable(inst, false)
  578.     if rc ~=0 then
  579.         error_during_safety_stop=true
  580.     end
  581.     mc.mcCntlSetLastError(inst, "M6 has stopped to ensure safety. Check error messages for reasons.")
  582.     if error_during_safety_stop == true then
  583.         mc.mcCntlSetLastError(inst, "Additinally, the safety stop function encountered errors.")
  584.     end
  585. end
  586. function toolchange_machine_homed()
  587.     local inst = mc.mcGetInstance()
  588.     if (mc.mcAxisIsHomed(inst, 0)==1 and mc.mcAxisIsHomed(inst, 1)==1 and mc.mcAxisIsHomed(inst, 2)==1) then
  589.         return true
  590.     else
  591.         return false
  592.     end
  593. end
  594. 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
  595.     --Get the Y axis coords
  596.     local inst = mc.mcGetInstance()
  597.     local loc_Y=0
  598.     loc_Y= mc.mcAxisGetMachinePos(inst, 1)
  599.     --Compare them to unallowed values
  600.     if (loc_Y>=-50) then
  601.         --Y not allowed, and so return that the coords are not allowed (false)
  602.         return false
  603.     elseif (loc_Y<-50) then
  604.         --Y is allowed
  605.         return true
  606.     else
  607.         --error, (no further motion by caller allowed). It is up to the caller to stop, however.
  608.         return false
  609.     end
  610. end
  611.  
  612.  
  613. if (mc.mcInEditor() == 1) then --debugging
  614.  m6()
  615. end
Advertisement
Add Comment
Please, Sign In to add comment