Advertisement
Guest User

M6 script

a guest
Feb 16th, 2018
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 24.08 KB | None | 0 0
  1. function m6()
  2.  
  3.     --Tool Change
  4.  
  5.  
  6.     --get newtool/oldtool numbers and return them as selectedtool for newtool and currenttool for oldtool
  7.     --if newtool is the same, end
  8.     --toolchange_checksensors_s1s4()
  9.     local inst = mc.mcGetInstance()
  10.     if(toolchange_getspindlestatus()==2) then
  11.         print("Tool attached incorrectly, Tool change will not start")
  12.         mc.mcCntlEnable(inst, false)
  13.         do return end
  14.     end
  15.     if (toolchange_machine_homed()==false) then
  16.         --the machine has not been homed, which might result in a crash while trying to go to any of the tools' locations
  17.         print("Machine not homed, Tool change will not start")
  18.         mc.mcCntlEnable(inst, false)
  19.         do return end
  20.     end
  21.     if (toolchange_machine_coords_allowed()==false) then
  22.         --the machine is in an unallowed location (next to the tool rack). A crash is likely.
  23.         print("Y axis too close to the tool rack, Tool change will not start")
  24.         mc.mcCntlEnable(inst, false)
  25.         do return end
  26.     end
  27.     local selectedtool = mc.mcToolGetSelected(inst) --get the n. of the new tool
  28.     local currenttool = mc.mcToolGetCurrent(inst) --get the n. of the tool already selected
  29.  
  30.  
  31.     if (selectedtool == currenttool) then --if the to-be-selected tool is the same as the current one, no tool change is necessary
  32.         print("No tool change necessary")
  33.         do return end
  34.     end
  35.     if (selectedtool<1) then
  36.         print("Selected tool number out of range. Allowed values 1-21")
  37.         mc.mcCntlEnable(inst, false)
  38.         do return end
  39.     elseif (selectedtool>21) then
  40.         print("Selected tool number out of range. Allowed values 1-21")
  41.         mc.mcCntlEnable(inst, false)
  42.         do return end
  43.     end
  44.     if (currenttool<1) then
  45.         print("Current tool number out of range. Allowed values 1-21")
  46.         mc.mcCntlEnable(inst, false)
  47.         do return end
  48.     elseif (currenttool21) then
  49.         print("Current tool number out of range. Allowed values 1-21")
  50.         mc.mcCntlEnable(inst, false)
  51.         do return end
  52.     end
  53.  
  54.     if (currenttool>9) then
  55.         if (toolchange_checksensors_s2()==0) then
  56.             --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.
  57.             print("Tool number incorrect. Remove the tool from the main spindle or correct the tool number")
  58.             do return end
  59.         end
  60.     end
  61.     --get the machine's state to return to it back later. Absolute vs Incremental, etc.
  62.  
  63.     --Var# is the PoundVar number for the register holding the value of the attribute.
  64.     --Var# Gcode Description
  65.     --4002 G17 set XY plane
  66.     --4003 G90 set absolute mode
  67.     --4005 G95 set feed as units per minute
  68.     --4006 G21 set units to mm
  69.     --4015 G61 set to exact stop mode, as to avoid taking shortcuts around corners and move predictably
  70.     local CurFeed = mc.mcCntlGetPoundVar(inst, 2134)
  71.     local CurFeedMode = mc.mcCntlGetPoundVar(inst, 4001)
  72.     local CurPlaneMode = mc.mcCntlGetPoundVar(inst, 4002)
  73.     local CurAbsMode = mc.mcCntlGetPoundVar(inst, 4003)
  74.     local CurSpeedMode = mc.mcCntlGetPoundVar(inst, 4005)
  75.     local CurUnit = mc.mcCntlGetPoundVar(inst, 4006)
  76.     local CurMotionMode = mc.mcCntlGetPoundVar(inst, 4015)
  77.  
  78.     --initialise (Safe start G codes)
  79.     mc.mcCntlGcodeExecuteWait(inst, "G21 G40 G61")
  80.     --mc.mcCntlGcodeExecuteWait(inst, "G17 G90 G95 G21 G40 G61")
  81.     --mc.mcCntlSetLastError(inst, "TC initialised")
  82.     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.
  83.     --toolchange_spindlecover_up()
  84.     if (currenttool>8) then --The current tool is in the boring head
  85.         if (selectedtool>8) then --The selected tool is in the boring head
  86.             toolchange_boringhead_allin()
  87.             toolchange_gotosafe()
  88.             toolchange_boringhead_toolout(selectedtool)
  89.             toolchange_boringhead_down()
  90.             toolchange_setoffsets(selectedtool)
  91.         elseif (selectedtool<9) then --The to-be-selected tool is goes in the main spindle
  92.             toolchange_boringhead_up()
  93.             toolchange_boringhead_allin()
  94.             toolchange_boringhead_motorOFF()
  95.             toolchange_goto_take_tool(selectedtool)
  96.             toolchange_spindle_grabtool()
  97.             if(toolchange_getspindlestatus()==2) then --ERROR within the tool attachment system, STOP
  98.                 mc.mcCntlSetLastError(inst, "Tool attached incorrectly, remove it manually")
  99.                 mc.mcToolSetCurrent(inst, selectedtool)
  100.                 mc.mcCntlSetLastError(inst, string.format("New tool %.0f", selectedtool))
  101.                 do return end
  102.             end
  103.             toolchange_goto_slide_out_tool()
  104.         end
  105.     end
  106.     --The current tool is goes in the main spindle, put it back first
  107.     if(currenttool<9) then
  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.         end
  122.         if(toolchange_getspindlestatus()==2) then
  123.             mc.mcCntlSetLastError(inst, "Tool attached incorrectly, remove it manually")
  124.             mc.mcToolSetCurrent(inst, selectedtool)
  125.             mc.mcCntlSetLastError(inst, string.format("New tool %.0f", selectedtool))
  126.             do return end
  127.         end
  128.         if(selectedtool<9) then --The to-be-selected tool is goes in the main spindle
  129.             toolchange_goto_take_tool(selectedtool)
  130.             toolchange_spindle_grabtool()
  131.             if(toolchange_getspindlestatus()==2) then --ERROR within the tool attachment system, STOP
  132.                 mc.mcCntlSetLastError(inst, "Tool attached incorrectly, remove it manually")
  133.                 mc.mcToolSetCurrent(inst, selectedtool)
  134.                 mc.mcCntlSetLastError(inst, string.format("New tool %.0f", selectedtool))
  135.                 do return end
  136.             end
  137.             toolchange_goto_slide_out_tool()
  138.         elseif (selectedtool>8) then --The to-be-selected tool is in the boring head
  139.             toolchange_gotosafe()
  140.             --toolchange_boringhead_motorON()
  141.             toolchange_boringhead_toolout(selectedtool)
  142.             toolchange_boringhead_down()
  143.             toolchange_setoffsets(selectedtool)
  144.         end
  145.     end
  146.     --return the machine's state to the one it was in before, recorded at the start of this function
  147.  
  148.     --There is a bug that causes set #4xxx variables to be of size ten times smaller,
  149.     --i.e 17 becomes 1.7.
  150.     --In order to account for this, they have to be multiplied by 10
  151.     mc.mcCntlSetPoundVar(inst, 2134, CurFeed)
  152.     mc.mcCntlSetPoundVar(inst, 4001, CurFeedMode*10)
  153.     mc.mcCntlSetPoundVar(inst, 4002, CurPlaneMode*10)
  154.     mc.mcCntlSetPoundVar(inst, 4003, CurAbsMode*10)
  155.     mc.mcCntlSetPoundVar(inst, 4005, CurSpeedMode*10)
  156.     mc.mcCntlSetPoundVar(inst, 4006, CurUnit*10)
  157.     mc.mcCntlSetPoundVar(inst, 4015, CurMotionMode*10)
  158.  
  159.     --set a new tool number, since the tool is now attached
  160.     if toolchange_machine_enabled()==true then
  161.         toolchange_setoffsets(selectedtool)
  162.         mc.mcToolSetCurrent(inst, selectedtool)
  163.         mc.mcCntlSetLastError(inst, string.format("New tool %.0f", selectedtool))
  164.     end
  165. end
  166.  
  167. function toolchange_boringhead_toolout(tool_n) --this function pushes a specific tool out from the boring head. Range tool n. 9-21 (total 13)
  168.     local inst = mc.mcGetInstance()
  169.     local hsig = nil
  170.     if toolchange_machine_enabled()==true then
  171.         if tool_n==9 then
  172.             hsig = mc.mcSignalGetHandle(inst, mc.OSIG_OUTPUT6)
  173.         elseif tool_n==10 then
  174.             hsig = mc.mcSignalGetHandle(inst, mc.OSIG_OUTPUT5)
  175.         elseif tool_n==11 then
  176.             hsig = mc.mcSignalGetHandle(inst, mc.OSIG_OUTPUT16)
  177.         elseif tool_n==12 then
  178.             hsig = mc.mcSignalGetHandle(inst, mc.OSIG_OUTPUT15)
  179.         elseif tool_n==13 then
  180.             hsig = mc.mcSignalGetHandle(inst, mc.OSIG_OUTPUT14)
  181.         elseif tool_n==14 then
  182.             hsig = mc.mcSignalGetHandle(inst, mc.OSIG_OUTPUT13)
  183.         elseif tool_n==15 then
  184.             hsig = mc.mcSignalGetHandle(inst, mc.OSIG_OUTPUT12)
  185.         elseif tool_n==16 then
  186.             hsig = mc.mcSignalGetHandle(inst, mc.OSIG_OUTPUT11)
  187.         elseif tool_n==17 then
  188.             hsig = mc.mcSignalGetHandle(inst, mc.OSIG_OUTPUT10)
  189.         elseif tool_n==18 then
  190.             hsig = mc.mcSignalGetHandle(inst, mc.OSIG_OUTPUT9)
  191.         elseif tool_n==19 then
  192.             hsig = mc.mcSignalGetHandle(inst, mc.OSIG_OUTPUT8)
  193.         elseif tool_n==20 then
  194.             hsig = mc.mcSignalGetHandle(inst, mc.OSIG_OUTPUT7)
  195.         elseif tool_n==21 then
  196.             hsig = mc.mcSignalGetHandle(inst, mc.OSIG_OUTPUT17)
  197.         end
  198.         mc.mcSignalSetState(hsig, true)
  199.     end
  200. end
  201. function toolchange_boringhead_toolin(tool_n)  --this function pulls a specific tool in to the boring head. Range tool n. 9-21 (total 13)
  202.     local inst = mc.mcGetInstance()
  203.     local hsig = nil
  204.     if toolchange_machine_enabled()==true then
  205.         if tool_n==9 then
  206.             hsig = mc.mcSignalGetHandle(inst, mc.OSIG_OUTPUT6)
  207.         elseif tool_n==10 then
  208.             hsig = mc.mcSignalGetHandle(inst, mc.OSIG_OUTPUT5)
  209.         elseif tool_n==11 then
  210.             hsig = mc.mcSignalGetHandle(inst, mc.OSIG_OUTPUT16)
  211.         elseif tool_n==12 then
  212.             hsig = mc.mcSignalGetHandle(inst, mc.OSIG_OUTPUT15)
  213.         elseif tool_n==13 then
  214.             hsig = mc.mcSignalGetHandle(inst, mc.OSIG_OUTPUT14)
  215.         elseif tool_n==14 then
  216.             hsig = mc.mcSignalGetHandle(inst, mc.OSIG_OUTPUT13)
  217.         elseif tool_n==15 then
  218.             hsig = mc.mcSignalGetHandle(inst, mc.OSIG_OUTPUT12)
  219.         elseif tool_n==16 then
  220.             hsig = mc.mcSignalGetHandle(inst, mc.OSIG_OUTPUT11)
  221.         elseif tool_n==17 then
  222.             hsig = mc.mcSignalGetHandle(inst, mc.OSIG_OUTPUT10)
  223.         elseif tool_n==18 then
  224.             hsig = mc.mcSignalGetHandle(inst, mc.OSIG_OUTPUT9)
  225.         elseif tool_n==19 then
  226.             hsig = mc.mcSignalGetHandle(inst, mc.OSIG_OUTPUT8)
  227.         elseif tool_n==20 then
  228.             hsig = mc.mcSignalGetHandle(inst, mc.OSIG_OUTPUT7)
  229.         elseif tool_n==21 then
  230.             hsig = mc.mcSignalGetHandle(inst, mc.OSIG_OUTPUT17)
  231.         end
  232.         mc.mcSignalSetState(hsig, false)
  233.     end
  234. end
  235. function toolchange_boringhead_allin()  --this function pulls all the tools in to the boring head. Range total 13
  236.     local inst = mc.mcGetInstance()
  237.     if toolchange_machine_enabled()==true then
  238.         mc.mcSignalSetState(mc.mcSignalGetHandle(inst, mc.OSIG_OUTPUT5), false)
  239.         mc.mcSignalSetState(mc.mcSignalGetHandle(inst, mc.OSIG_OUTPUT6), false)
  240.         mc.mcSignalSetState(mc.mcSignalGetHandle(inst, mc.OSIG_OUTPUT7), false)
  241.         mc.mcSignalSetState(mc.mcSignalGetHandle(inst, mc.OSIG_OUTPUT8), false)
  242.         mc.mcSignalSetState(mc.mcSignalGetHandle(inst, mc.OSIG_OUTPUT9), false)
  243.         mc.mcSignalSetState(mc.mcSignalGetHandle(inst, mc.OSIG_OUTPUT10), false)
  244.         mc.mcSignalSetState(mc.mcSignalGetHandle(inst, mc.OSIG_OUTPUT11), false)
  245.         mc.mcSignalSetState(mc.mcSignalGetHandle(inst, mc.OSIG_OUTPUT12), false)
  246.         mc.mcSignalSetState(mc.mcSignalGetHandle(inst, mc.OSIG_OUTPUT13), false)
  247.         mc.mcSignalSetState(mc.mcSignalGetHandle(inst, mc.OSIG_OUTPUT14), false)
  248.         mc.mcSignalSetState(mc.mcSignalGetHandle(inst, mc.OSIG_OUTPUT15), false)
  249.         mc.mcSignalSetState(mc.mcSignalGetHandle(inst, mc.OSIG_OUTPUT16), false)
  250.         mc.mcSignalSetState(mc.mcSignalGetHandle(inst, mc.OSIG_OUTPUT17), false)
  251.     end
  252. end
  253. 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.
  254.     local inst = mc.mcGetInstance()
  255.     if toolchange_machine_enabled()==true then
  256.         mc.mcSignalSetState(mc.mcSignalGetHandle(inst, mc.OSIG_OUTPUT2), 0)
  257.         counter=0
  258.         while (mc.mcSignalGetState(mc.mcSignalGetHandle(inst, mc.ISIG_INPUT2))==0) and (counter<20) do
  259.             counter=counter+1
  260.             mc.mcCntlGcodeExecuteWait(inst, "G4 P0.1")
  261.         end
  262.         if (counter>=20) then
  263.             mc.mcCntlEnable(inst, false)
  264.             mc.mcCntlSetLastError(inst, "Unable to lift the boring head. Tool change will not start. Check air pressure.")
  265.         end
  266.     end
  267. end
  268. 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.
  269.     local inst = mc.mcGetInstance()
  270.     if toolchange_machine_enabled()==true then
  271.         mc.mcSignalSetState(mc.mcSignalGetHandle(inst, mc.OSIG_OUTPUT3), 0)
  272.         counter=0
  273.         while (mc.mcSignalGetState(mc.mcSignalGetHandle(inst, mc.ISIG_INPUT0))== 1) and (counter<20) do
  274.             counter=counter+1
  275.             mc.mcCntlGcodeExecuteWait(inst, "G4 P0.1")
  276.         end
  277.         if (counter>=20) then
  278.             mc.mcCntlEnable(inst, false)
  279.             mc.mcCntlSetLastError(inst, "Unable to lift the spindle cover. Tool change will not start. Check air pressure.")
  280.         end
  281.     end
  282. end
  283. 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.
  284.     local inst = mc.mcGetInstance()
  285.     if toolchange_machine_enabled()==true then
  286.         mc.mcSignalSetState(mc.mcSignalGetHandle(inst, mc.OSIG_OUTPUT2), 1)
  287.         counter=0
  288.         while (mc.mcSignalGetState(mc.mcSignalGetHandle(inst, mc.ISIG_INPUT1))==0) and (counter<20) do
  289.             counter=counter+1
  290.             mc.mcCntlGcodeExecuteWait(inst, "G4 P0.1")
  291.         end
  292.         if (counter>=20) then
  293.             mc.mcCntlEnable(inst, false)
  294.             mc.mcCntlSetLastError(inst, "Unable to lower the boring head. Tool change will not start. Check air pressure.")
  295.         end
  296.     end
  297. end
  298. function toolchange_boringhead_motorON()
  299.     if toolchange_machine_enabled()==true then
  300.         local inst = mc.mcGetInstance()
  301.         mc.mcCntlGcodeExecuteWait(inst, "M103")
  302.     end
  303. end
  304. function toolchange_boringhead_motorOFF()
  305.     if toolchange_machine_enabled()==true then
  306.         local inst = mc.mcGetInstance()
  307.         mc.mcCntlGcodeExecuteWait(inst, "M105")
  308.     end
  309. end
  310. function toolchange_spindle_spindleOFF()
  311.     if toolchange_machine_enabled()==true then
  312.         local inst = mc.mcGetInstance()
  313.         mc.mcCntlGcodeExecuteWait(inst, "M5")
  314.     end
  315. end
  316. function toolchange_spindle_grabtool() --this function operates the main spindle's pneumatic tool attacher, grabbing a tool
  317.         toolchange_checksensors_s2()
  318.         local inst = mc.mcGetInstance()
  319.         mc.mcCntlGcodeExecuteWait(inst, "G04 P1")
  320.         if toolchange_machine_enabled()==true then
  321.             mc.mcSignalSetState(mc.mcSignalGetHandle(inst, mc.OSIG_OUTPUT1), false)
  322.         end
  323.         mc.mcCntlGcodeExecuteWait(inst, "G04 P1")
  324.         toolchange_checksensors_s2()
  325. end
  326. function toolchange_spindle_releasetool() --this function operates the main spindle's pneumatic tool attacher, releasing a tool
  327.         toolchange_checksensors_s2()
  328.         local inst = mc.mcGetInstance()
  329.         mc.mcCntlGcodeExecuteWait(inst, "G04 P1")
  330.         if toolchange_machine_enabled()==true then
  331.             mc.mcSignalSetState(mc.mcSignalGetHandle(inst, mc.OSIG_OUTPUT1), true)
  332.         end
  333.         mc.mcCntlGcodeExecuteWait(inst, "G04 P1")
  334.         toolchange_checksensors_s2()
  335. end
  336. 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
  337.     if toolchange_machine_enabled()==true then
  338.         local inst = mc.mcGetInstance()
  339.         mc.mcCntlGcodeExecuteWait(inst, "G90 G21\n G01 G53 Z0 F3000\n G01 G53 X200 F3000")
  340.     end
  341. end
  342. function toolchange_zup() --this function rises up the Z axis
  343.     if toolchange_machine_enabled()==true then
  344.         local inst = mc.mcGetInstance()
  345.         mc.mcCntlGcodeExecuteWait(inst, "G90 G21\n G01 G53 Z0 F3000")
  346.     end
  347. end
  348. 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)
  349.     if toolchange_machine_enabled()==true then
  350.         --setup\n Z up\n go in front of the holder in XY\n Z down \n Y slide in
  351.         local inst = mc.mcGetInstance()
  352.         if (tool_n==8) then
  353.             mc.mcCntlGcodeExecuteWait(inst, "G90 G21\n G01 G53 Z0 F3000\n G01 G53 X328 Y-54 F3000\n G01 G53 Z-84.9 F1000\n G01 F500 G53 Y29")
  354.         elseif (tool_n==7) then
  355.             mc.mcCntlGcodeExecuteWait(inst, "G90 G21\n G01 G53 Z0 F3000\n G01 G53 X448 Y-54 F3000\n G01 G53 Z-84.7 F1000\n G01 F500 G53 Y32")
  356.         elseif (tool_n==6) then
  357.             mc.mcCntlGcodeExecuteWait(inst, "G90 G21\n G01 G53 Z0 F3000\n G01 G53 X568 Y-54 F3000\n G01 G53 Z-84.7 F1000\n G01 F500 G53 Y32")
  358.         elseif (tool_n==5) then
  359.             mc.mcCntlGcodeExecuteWait(inst, "G90 G21\n G01 G53 Z0 F3000\n G01 G53 X688 Y-54 F3000\n G01 G53 Z-84.5 F1000\n G01 F500 G53 Y32")
  360.         elseif (tool_n==4) then
  361.             mc.mcCntlGcodeExecuteWait(inst, "G90 G21\n G01 G53 Z0 F3000\n G01 G53 X807.6 Y-54 F3000\n G01 G53 Z-83.7 F1000\n G01 F500 G53 Y32")
  362.         elseif (tool_n==3) then
  363.             mc.mcCntlGcodeExecuteWait(inst, "G90 G21\n G01 G53 Z0 F3000\n G01 G53 X929 Y-54 F3000\n G01 G53 Z-84 F1000\n G01 F500 G53 Y32")
  364.         elseif (tool_n==2) then
  365.             mc.mcCntlGcodeExecuteWait(inst, "G90 G21\n G01 G53 Z0 F3000\n G01 G53 X1048 Y-54 F3000\n G01 G53 Z-82.3 F1000\n G01 F500 G53 Y32")
  366.         elseif (tool_n==1) then
  367.             mc.mcCntlGcodeExecuteWait(inst, "G90 G21\n G01 G53 Z0 F3000\n G01 G53 X1168 Y-54 F3000\n G01 G53 Z-82.3 F1000\n G01 F500 G53 Y32")
  368.         end
  369.     end
  370. end
  371. 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)
  372.     if toolchange_machine_enabled()==true then
  373.         --setup\n Z up\n go on top of the holder XY\n Z down
  374.         local inst = mc.mcGetInstance()
  375.         if (tool_n==8) then
  376.             mc.mcCntlGcodeExecuteWait(inst, "G90 G21\n G01 G53 Z0 F3000\n G01 G53 X328 Y29 F3000\n G01 G53 Z-84.9 F500")
  377.         elseif (tool_n==7) then
  378.             mc.mcCntlGcodeExecuteWait(inst, "G90 G21\n G01 G53 Z0 F3000\n G01 G53 X448 Y32 F3000\n G01 G53 Z-84.7 F500")
  379.         elseif (tool_n==6) then
  380.             mc.mcCntlGcodeExecuteWait(inst, "G90 G21\n G01 G53 Z0 F3000\n G01 G53 X568 Y32 F3000\n G01 G53 Z-84.7 F500")
  381.         elseif (tool_n==5) then
  382.             mc.mcCntlGcodeExecuteWait(inst, "G90 G21\n G01 G53 Z0 F3000\n G01 G53 X688 Y32 F3000\n G01 G53 Z-84.5 F500")
  383.         elseif (tool_n==4) then
  384.             mc.mcCntlGcodeExecuteWait(inst, "G90 G21\n G01 G53 Z0 F3000\n G01 G53 X807.6 Y32 F3000\n G01 G53 Z-83.7 F500")
  385.         elseif (tool_n==3) then
  386.             mc.mcCntlGcodeExecuteWait(inst, "G90 G21\n G01 G53 Z0 F3000\n G01 G53 X929 Y32 F3000\n G01 G53 Z-84 F500")
  387.         elseif (tool_n==2) then
  388.             mc.mcCntlGcodeExecuteWait(inst, "G90 G21\n G01 G53 Z0 F3000\n G01 G53 X1048 Y32 F3000\n G01 G53 Z-82.3 F500")
  389.         elseif (tool_n==1) then
  390.             mc.mcCntlGcodeExecuteWait(inst, "G90 G21\n G01 G53 Z0 F3000\n G01 G53 X1168 Y32 F3000\n G01 G53 Z-82.3 F500")
  391.         end
  392.     end
  393. end
  394. 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.
  395.     if toolchange_machine_enabled()==true then
  396.         --slide out Y\n Z up
  397.         local inst = mc.mcGetInstance()
  398.         mc.mcCntlGcodeExecuteWait(inst, "G90 G21\n G01 G53 Y-54 F1000\n G01 G53 Z0 F3000")
  399.     end
  400. end
  401. 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)
  402.     local inst = mc.mcGetInstance()
  403.     if (tool_n<9) then
  404.         mc.mcCntlGcodeExecuteWait(inst, "G90 G10 L20 P0 X0 Y0")
  405.     elseif (tool_n==21) then
  406.         mc.mcCntlGcodeExecuteWait(inst, "G90 G10 L20 P0 X180.3 Y62.6")
  407.     elseif (tool_n==20) then
  408.         mc.mcCntlGcodeExecuteWait(inst, "G90 G10 L20 P0 X267.3 Y62.6")
  409.     elseif (tool_n==19) then
  410.         mc.mcCntlGcodeExecuteWait(inst, "G90 G10 L20 P0 X299.3 Y62.6")
  411.     elseif (tool_n==18) then
  412.         mc.mcCntlGcodeExecuteWait(inst, "G90 G10 L20 P0 X331.3 Y62.6")
  413.     elseif (tool_n==17) then
  414.         mc.mcCntlGcodeExecuteWait(inst, "G90 G10 L20 P0 X363.3 Y62.6")
  415.     elseif (tool_n==16) then
  416.         mc.mcCntlGcodeExecuteWait(inst, "G90 G10 L20 P0 X395.3 Y62.6")
  417.     elseif (tool_n==15) then
  418.         mc.mcCntlGcodeExecuteWait(inst, "G90 G10 L20 P0 X427.3 Y62.6")
  419.     elseif (tool_n==14) then
  420.         mc.mcCntlGcodeExecuteWait(inst, "G90 G10 L20 P0 X427.3 Y94.6")
  421.     elseif (tool_n==13) then
  422.         mc.mcCntlGcodeExecuteWait(inst, "G90 G10 L20 P0 X427.3 Y126.6")
  423.     elseif (tool_n==12) then
  424.         mc.mcCntlGcodeExecuteWait(inst, "G90 G10 L20 P0 X427.3 Y158.6")
  425.     elseif (tool_n==11) then
  426.         mc.mcCntlGcodeExecuteWait(inst, "G90 G10 L20 P0 X427.3 Y190.6")
  427.     elseif (tool_n==10) then
  428.         mc.mcCntlGcodeExecuteWait(inst, "G90 G10 L20 P0 X427.3 Y222.6")
  429.     elseif (tool_n==9) then
  430.         mc.mcCntlGcodeExecuteWait(inst, "G90 G10 L20 P0 X427.3 Y309.6")
  431.     end
  432. end
  433. function toolchange_checksensors_s1s4() --this function checks the main spindle's S1S4 sensor values and returns a state
  434.     local inst = mc.mcGetInstance()
  435.     local hsig=mc.mcSignalGetHandle(inst, mc.ISIG_INPUT5)
  436.     local val=mc.mcSignalGetState(hsig)
  437.     return val
  438. end
  439. function toolchange_checksensors_s2() --this function checks the main spindle's S2 sensor values and returns a state
  440.     local inst = mc.mcGetInstance()
  441.     local hsig=mc.mcSignalGetHandle(inst, mc.ISIG_INPUT4)
  442.     local val=mc.mcSignalGetState(hsig)
  443.     --0: tool holde blocked (maybe there's a tool attached to it)
  444.     --1: tool holde open (there's no tool attached to it)
  445.     return val
  446. end
  447. function toolchange_getspindlestatus() --this function compiles the main spindle's sensor values and returns a state of the tool.
  448.     local inst = mc.mcGetInstance()
  449.     if (toolchange_checksensors_s2()==1) then
  450.         return 0
  451.     elseif (toolchange_checksensors_s2()==0) then
  452.         local s1s4=toolchange_checksensors_s1s4()
  453.         if(s1s4==1) then
  454.             return 1 --Tool OK
  455.         else
  456.             return 2 --Tool NOT OK
  457.         end
  458.     end
  459. end
  460. function toolchange_machine_enabled()
  461.     local inst = mc.mcGetInstance()
  462.     local hsig=mc.mcSignalGetHandle(inst, mc.OSIG_MACHINE_ENABLED)
  463.     local isRunning=mc.mcCntlIsInCycle(inst)
  464.     local val=mc.mcSignalGetState(hsig)
  465.     if val==1 then
  466.         --if isRunning==true then
  467.             return true
  468.         --else
  469.         --    return false
  470.         --end
  471.     else
  472.         return false
  473.     end
  474. end
  475. function toolchange_machine_homed()
  476.     local inst = mc.mcGetInstance()
  477.     if (mc.mcAxisIsHomed(inst, 0)==1 and mc.mcAxisIsHomed(inst, 1)==1 and mc.mcAxisIsHomed(inst, 2)==1) then
  478.         return true
  479.     else
  480.         return false
  481.     end
  482. end
  483. 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
  484.     --Get the Y axis coords
  485.     local inst = mc.mcGetInstance()
  486.     local loc_Y=0
  487.     loc_Y= mc.mcAxisGetMachinePos(inst, 1)
  488.     --Compare them to unallowed values
  489.     if (loc_Y>-50) then
  490.         --Y not allowed, and so return that the coords are not allowed (false)
  491.         return false
  492.     elseif (loc_Y<-50) then
  493.         --Y is allowed
  494.         return true
  495.     else
  496.         --error, (no further motion by caller allowed). It is up to the caller to stop, however.
  497.         return false
  498.     end
  499. end
  500.  
  501.  
  502. if (mc.mcInEditor() == 1) then --debugging
  503.  m6()
  504. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement