Hello Guest it is October 05, 2022, 05:20:44 PM

Author Topic: M6 Tool change for the rest of us.  (Read 1803 times)

0 Members and 1 Guest are viewing this topic.

Offline AXYZ_WI

*
  •  4 4
  • TLAR
    • View Profile
M6 Tool change for the rest of us.
« on: March 29, 2020, 05:21:34 PM »
Here's my Mach4 tool change. I took many concepts from others on this forum so thank you. It has user parameters that let you tell it if you use a tool setter permanently located or a movable touch plate.  You can also tell it it you control the spindle or if Mach4 does with M3/M5 commands.

Here's a video of it in action and a quick explanation of the code.

https://youtu.be/kVqPkZG9ha4

Quote
-----------------------------------------------TLAR SMART TOOL CHANGE - Rev.1 03/2020------------------------------------------------------------
---------------------------https://www.youtube.com/channel/UCVMC1IaXdFonuWAGrmyUNmg?view_as=subscriber-------------------------------------------
------------------This tool change script assumes your spindle uses a collet and collet nut to retain the tool.----------------------------------
-------------------------------------------------------------------------------------------------------------------------------------------------
--------------------------------Before continuing, insure your input signals and mapping are correct---------------------------------------------
------Manual tool change uses a G31 command, AutoTool change with the tool setter uses a G31.1 command. The difference is the imput signal!!-----
function M6()
   local inst = mc.mcGetInstance();
   -------------------------------------------User inputs--------------------------------------------------------------------------------------
   local ManualChange = false             --True for manual change false for auto change You ****YOU MUST HAVE RELIABLE HOMING X Y and Z FOR AUTO TOOL CHANGE!!****
   local SpindleControl = true           -- Can Mach4 controll your spindle? True for M3/M5commands, false for manual spindle control.
   --------------------------------Auto Tool Change specific----------------------------------------------------------------------------------
   local xprobe = 93.6                    --Machine Coordinate of the tool setter on X axis
   local yprobe = 2.375               --Machine Coordinate of the tool setter on Y axis
   local XToolchangePosition = 93.6332    --Machine Coordinate of where you want to change your tool on X axis
   local YToolchangePosition = 5          --Machine Coordinate of where you want to change your tool on Y axis
   ------------------------------------Tool information----------------------------------------------------------------------------------------
   local DefaultToolLength = 2            -- Must be at least as long or longer then your longest tool This is what's used if your tool table is not filled in.
   local ColletAtProbeZCoordinate = -6.07 -- This is the distance from home Z must travel for the collet without a tool to hit the tool setter
   local ExtraProbeDistance = .75         -- This is your margin of error the smaller this number the larger the pucker factor.
   local ProbePrepSpeed = 20              -- This is how fast Z travels before it slows down to probe. The larger this number, the larger the pucker factor.
   local ProbeOperationDistance = -2.25   -- This is how far Z will move while looking for a probe hit.
   local ProbeSpeed = 5                   -- This is how fast Z will probe at. Keep this number small for acuracy.
   local SpindleDwell = 10000             -- this is the amount of time to wait in microseconds for the spindle to stop or start spinning. Example is 10 seconds
--------------------------------------End of user input-----------------------------------------------------------------------------------------   
    local selectedtool = mc.mcToolGetSelected(inst)
    local currenttool = mc.mcToolGetCurrent(inst)
    local xstart = mc.mcAxisGetPos(inst,0)
    local ystart = mc.mcAxisGetPos(inst,1)
   
   if selectedtool == currenttool then
      mc.mcCntlSetLastError(inst, "ToolChange Activated But Not Required, That Looks About Right")
   return
   end
   mc.mcCntlSetLastError(inst, "TLAR Smart Tool Change Activated!")
   if SpindleControl == true then
      mc.mcCntlGcodeExecute(inst, "M5 G4 P"..SpindleDwell)
   else
      wx.wxMessageBox("Turn off your spindle\nClick ok to continue.")
   end
   
   if ManualChange == true then
      wx.wxMessageBox("Click OK, Then Jog to A Safe Tool Change Position\nJog Z axiz close to the touch plate,\nThen Click Cycle Start.","Click OK to continue")
      mc.mcCntlToolChangeManual(inst,true)
   else
      mc.mcCntlGcodeExecuteWait(inst,"G90 G53 G0 Z0.0 \n G90 G53 G0 Y"..yprobe.."\n G90 G53 G0 X"..xprobe)
   end
   
   RunProbe(currenttool, DefaultToolLength, ColletAtProbeZCoordinate, ExtraProbeDistance, ProbePrepSpeed, ProbeOperationDistance, ProbeSpeed, ManualChange)
    local toolz = mc.mcAxisGetPos(inst,2)
    mc.mcCntlGcodeExecuteWait(inst,"G90 G53 G0 Z0.0")
   
   if ManualChange == false then
      mc.mcCntlGcodeExecuteWait(inst, "G90 G53 G0 X"..XToolchangePosition.."Y"..YToolchangePosition)
   end
   
    local changetoo = mc.mcToolGetDesc(inst,selectedtool)
    wx.wxMessageBox("Please change too tool number "..selectedtool.."\n "..changetoo.." and press ok to continue")
   
   if ManualChange == true then
      wx.wxMessageBox("Click OK, Then jog Z axis close to the touch plate and press Cycle Start.\nClick OK to Continue ")
      mc.mcCntlToolChangeManual(inst,true)   
   else
      mc.mcCntlGcodeExecuteWait(inst,"G90 G53 G0 X"..xprobe.."Y"..yprobe)
   end
   
   RunProbe(selectedtool, DefaultToolLength, ColletAtProbeZCoordinate, ExtraProbeDistance, ProbePrepSpeed, ProbeOperationDistance, ProbeSpeed, ManualChange)
    mc.mcAxisSetPos(inst, 2 ,toolz)
    mc.mcCntlGcodeExecuteWait(inst,"G90 G53 G0 Z0.0")
   
   if ManualChange == true then
      wx.wxMessageBox("Remove touch plate and aligator clip from router\nGantry will move and motor will spin and continue GCode\nPress ok to Continue")
   end
   
    mc.mcToolSetCurrent(inst, selectedtool)
   
   if SpindleControl == true then
      mc.mcCntlGcodeExecute(inst, "M3 G4 P"..SpindleDwell)
   else
      wx.wxMessageBox("Turn on spindle\nClick ok to continue")
   end
   mc.mcCntlGcodeExecuteWait(inst,"G90 G0 X"..xstart.." Y"..ystart)
    mc.mcCntlSetLastError(inst, "TLAR Smart ToolChange Finished")

end

function RunProbe(tool, DefaultToolLength, ColletAtProbeZCoordinate, ExtraProbeDistance, ProbePrepSpeed, ProbeOperationDistance, ProbeSpeed, ManualChange)
    local inst = mc.mcGetInstance()
    local toollen = mc.mcToolGetData(inst, mc.MTOOL_MILL_HEIGHT, tool)
      if toollen == 0 then toollen = DefaultToolLength -- User Preference
      mc.mcCntlSetLastError(inst, "Changing to default tool length Fill in your tool table!")
      end
    local probestart = ColletAtProbeZCoordinate + ExtraProbeDistance + toollen
      if ManualChange == true then
         mc.mcCntlGcodeExecuteWait(inst,"G91 G31 Z"..ProbeOperationDistance.." F"..ProbeSpeed)
      else
         mc.mcCntlGcodeExecuteWait(inst,"G90 G53 G0 Z"..probestart.."F"..ProbePrepSpeed.."\n G91 G31.1 Z"..ProbeOperationDistance.." F"..ProbeSpeed)
      end
end
if (mc.mcInEditor() == 1)
then M6()
end
--Rev 1 Change log.
   --Added Spindle Control true/false
   --Added manualChange true/false
   --Added SpindleDwell parameter
Re: M6 Tool change for the rest of us.
« Reply #1 on: April 21, 2020, 01:59:26 PM »
When I put this code in my m6.mcs and type m6 in MDI, half of the screen buttons gray momentarily, and then nothing happens.  I assume there is a Lua error.

The default m6.mcs script will run in MDI.

Why does Mach4 not tell us what the error is?

Does anybody know a good fix?

This code will run in script editor when I click the double arrows.

Offline AXYZ_WI

*
  •  4 4
  • TLAR
    • View Profile
Re: M6 Tool change for the rest of us.
« Reply #2 on: April 21, 2020, 11:11:13 PM »
Are you telling it what tool you want? M6 T3 for example.
Re: M6 Tool change for the rest of us.
« Reply #3 on: April 29, 2020, 12:49:20 PM »
Yes.  Thanks for all your good work.

I'm using Mach4 on Win10, and the whole thing is remarkably buggie.  I reinstalled the controller board and Mach4, and that seemed to help a little.

I simplified your code to the extent possible and I stripped the code down to the bare minimum that I needed for my tool changes on a homemade mill, and then a lot of trial and error to get it to run.  Here's the code I ended up with:

function m6()

   local inst = mc.mcGetInstance()
   
   local selectedTool = mc.mcToolGetSelected(inst)
   selectedTool = math.tointeger(selectedTool)
   local currentTool = mc.mcToolGetCurrent(inst)
   currentTool = math.tointeger(currentTool)
   
   local ProbeOperationDistance = -15      -- This is how far Z will move while looking for a probe hit.
   local ProbeSpeed = 15                   -- This is how fast Z will probe at. Keep this number small for acuracy.
       
   
  if selectedTool == currentTool then
      mc.mcCntlSetLastError(inst, "Current tool == Selected tool so there is nothing to do")
  else
                
      mc.mcCntlSetLastError(inst, "Tool Change")
      mc.mcCntlGcodeExecuteWait(inst,"G90 G53 G0 Z0.0 X0.0 Y0.0")
      
      mc.mcCntlSetLastError(inst, "Position the tool less than 1/2 inch over the touch plate and click Start")
      mc.mcCntlToolChangeManual(inst,true)
      
      mc.mcCntlGcodeExecuteWait(inst,"G91 G31 Z"..ProbeOperationDistance.." F"..ProbeSpeed)

      local toolz = mc.mcAxisGetPos(inst, 2)
      mc.mcCntlGcodeExecuteWait(inst,"G90 G53 G0 Z0.0")
   
      mc.mcCntlSetLastError(inst, "Change the tool, reposition it over touch plate and click Start")
      mc.mcCntlToolChangeManual(inst,true)   
      
      mc.mcCntlGcodeExecuteWait(inst,"G91 G31 Z"..ProbeOperationDistance.." F"..ProbeSpeed)
      
      mc.mcAxisSetPos(inst, 2, toolz)
      mc.mcCntlGcodeExecuteWait(inst,"G90 G53 G0 Z0.0")
             
      mc.mcCntlSetLastError(inst, "Turn the spindle on, click cycle start, and we are cutting.")
      mc.mcCntlToolChangeManual(inst,true)   
                     
  end

end

if (mc.mcInEditor() == 1) then
   m6()
end
Re: M6 Tool change for the rest of us.
« Reply #4 on: October 07, 2020, 09:45:59 PM »
Is there a way to update this for those of us that dot have touch off plates ad do manual tool changes? Im still old school using the paper trick.
Re: M6 Tool change for the rest of us.
« Reply #5 on: February 15, 2021, 02:47:27 PM »
When I put this code in my m6.mcs and type m6 in MDI, half of the screen buttons gray momentarily, and then nothing happens.  I assume there is a Lua error.


Why does Mach4 not tell us what the error is?

Does anybody know a good fix?

This code will run in script editor when I click the double arrows.

The 2 occurances of M6 need to be changed to m6.  Mach4 strips m codes to lowercase and then looks for scrips and functions using the same case.