Hello Guest it is November 13, 2019, 03:19:49 AM

Author Topic: Automatic tool change script/Macro?  (Read 304 times)

0 Members and 1 Guest are viewing this topic.

Offline sx3

*
  •  40 40
    • View Profile
Automatic tool change script/Macro?
« on: January 01, 2019, 06:39:12 AM »
Hi guys,

We are running a 1986 Dynamyte 4400 Mill with Mach 4 and a ESS.
The machine is eqipped with a 9 tool turret holder which is rotated by a STEP motor, the turret have a homing sensor.
The turret folds in and out to the spindle with a standard motor with limit switches on each end.
The turret have a button on it, to make it rotate manually.
On the machine there are buttons for clamping and unclamping the tool

Anyone with similar setup want to share their code? We need a good starting point and at the moment we are quite lost.

Thanks!
Re: Automatic tool change script/Macro?
« Reply #1 on: January 01, 2019, 11:45:24 AM »
Hello,
In the Mach4 Folder on your computer there is a folder called "LuaExamples".  In this folder there is another folder called "Toolchanger"  Check those codes out and see if they can give you a good starting place.  Once you look at those and get a baseline, this forum can be very helpful, a lot of great people on here.
Chad Byrd

Offline sx3

*
  •  40 40
    • View Profile
Re: Automatic tool change script/Macro?
« Reply #2 on: January 01, 2019, 12:07:19 PM »
Thanks @Cbyrdtopper, I checked the folder and find a couple of wxamples, although I can't find anything toolchange related.
If you have it, could you post it here perhaps?
Re: Automatic tool change script/Macro?
« Reply #3 on: January 01, 2019, 01:20:27 PM »
I don't know where you found wx examples.  These are all .mcs files.
But, here is the code out of one of the examples.

function m6()

--Non-Random automatic tool changer
--This file assumes that you have G54 X0 Y0 Z0 to be setup as the Y-offset distance from tools and the correct height for picking
--up/dropping a tool as well as the correct X location for entering the tool changer.


----------Variables----------
local inst = mc.mcGetInstance() --Get the instance of Mach4

local hReg = 0

local ToolInColletSig = mc.ISIG_INPUT0 --These outputs/inputs will be configured by the user depending what outputs/inputs they have setup in Mach config.
local MotorSig = mc.OSIG_OUTPUT0
local CloseColletSig = mc.OSIG_OUTPUT1

local CurrentTool = mc.mcToolGetCurrent(inst)
local NextTool = mc.mcToolGetSelected(inst)
local YOffsetDistance = 14.25------------DEFINE Y OFFSET DISTANCE
local numtools = 4
local ToolPositionIO = {}
-----------------------------
function ToolChange(NextTool)
   --------------------------INPUT CHECKING-----------------------------
   if (NextTool > numtools) then
      return "That tool does not exist", false
   end
   
   hReg = 0 --reset hReg to 0 each time before reading it so you know if you get an error.
   hReg = mc.mcIoGetHandle(inst, ToolInColletSig)
   if hReg == 0 then
      return "Could not locate tool in collet signal!", false
   end
   
   hReg = 0
   hReg = mc.mcIoGetHandle(inst, MotorSig)
   if hReg == 0 then
      return "Could not locate motor signal!", false
   end
   
   for i = 1, numtools+1 do
      ToolPositionIO = 0
      ToolPositionIO = mc.mcIoGetHandle(inst, "Position"..tostring(i))
      if ToolPositionIO == 0 then
         return "Could not locate tool position signal!", false
        end
   end
   
   hReg = mc.mcIoGetHandle(inst, CloseColletSig)
   if hReg == 0 then
      return "Could not locate collet close signal!", false
   end
   ----------------------END INPUT CHECKING-----------------------------
   
   local rc = mc.mcIoGetState(ChuckhReg)
   if rc == 1 then --If rc equals 1, then there is a tool in the chuck
      --Put the tool away.
      rc = mcCntlGCodeExecuteWait(inst, "G54 X0 Y0 Z0") --Move to the offset zone.
      if rc ~= mc.MERROR_NOERROR then return "Could not complete GCode motion", false end
      --We are now at the offset line, and need to move the correct Y Distance so we can drop off the tool.
      mcCntlGCodeExecuteWait(inst, "G91 Y"..YOffsetDistance)
      if rc ~= mc.MERROR_NOERROR then return "Could not complete GCode motion", false end
      
      hReg = 0
      hReg = mc.mcIoGetHandle(inst, CloseColletSig)
      if hReg == 0 then
         return "Could not locate close collet signal!", false
      end
      mc.mcIoSetState(hReg, 0) --Release the tool
      mcCntlGCodeExecuteWait(inst, "G54 X0 Y0 Z0")--Move back to the offset line
      if rc ~= mc.MERROR_NOERROR then return "Could not complete GCode motion", false end
   else
      mcCntlGCodeExecuteWait(inst, "G54 X0 Y0 Z0")--Move to the offset line
      if rc ~= mc.MERROR_NOERROR then return "Could not complete GCode motion", false end
   end
   
   --At this point, we are at the offset line, and there is no tool in the chuck.   
   hReg = 0
   hReg = mc.mcIoGetHandle(inst, MotorSig)
   if hReg == 0 then
      return "Could not locate motor signal!", false
   end
   mc.mcIoSetState(hReg, 1) --Turn on the motor until we are at the desired tool change position.
   
   local loop = true
   while(loop == true) do
      local a,b = mcIoGetState(ToolPositionIO[NextTool])
      if(b == true) then
         if(a == true) then
            loop = false   
            hReg = 0
            hReg = mc.mcIoGetHandle(inst, MotorSig)
            if hReg == 0 then
               return "Could not locate motor signal!", false
            end
            mc.mcIoSetState(hReg, 0)--the tool changer is now rotated to the correct location.
                                  --so we turn off the motor.
         else
            wx.wxMilliSleep(100) --Sleep for 100 milliseconds, and then recheck input.
         end
      else
         hReg = 0
         hReg = mc.mcIoGetHandle(inst, MotorSig)
         if hReg == 0 then
            return "Could not locate motor signal!", false
         end
         mc.mcIoSetState(hReg, 0)
         return "Unable to get state of motor IO", false
      end
   end
   
   mcCntlGCodeExecuteWait(inst, "G91 Y"..YOffsetDistance) --Move into the tool changer
   if rc ~= mc.MERROR_NOERROR then return "Could not complete GCode motion", false end
   
   hReg = 0
   hReg = mc.mcIoGetHandle(inst, CloseColletSig)
   if hReg == 0 then
      return "Could not locate collet close signal!", false
   end
   mc.mcIoSetState(hReg, 1) --grab the tool.
   
   mcCntlGCodeExecuteWait(inst, "G54 X0 Y0 Z0")--Move back to the offset line
   if rc ~= mc.MERROR_NOERROR then return "Could not complete GCode motion", false end
   return "Tool change complete!", true
end

local a, b = ToolChange(NextTool)
mc.mcCntlSetLastError(tostring(a))--Prints status of tool change.
if(b == true) then
   mc.mcToolSetCurrent(inst, NextTool)
end

end
if (mc.mcInEditor() == 1) then
   dofile ("load_modules.mcs")
    m6()
end
Chad Byrd

Offline sx3

*
  •  40 40
    • View Profile
Re: Automatic tool change script/Macro?
« Reply #4 on: January 05, 2019, 05:51:29 AM »
Thanks for the example, can't seem to find it in my library.

What does this code check? If the spindle is off?
Code: [Select]
local MotorSig = mc.OSIG_OUTPUT0
 
   hReg = 0
   hReg = mc.mcIoGetHandle(inst, MotorSig)
   if hReg == 0 then
      return "Could not locate motor signal!", false
   end

In our machine, the spindle must stop in an exact postion before changing tool, I guess this could be correct?

Code: [Select]
-- Theese are all the INPUTS needed for the tool change
local SpindleInPosition = mc.ISIG_INPUT0 --When the spindle is in position for tool change, this input will be activate

   
--Here we check that spindle have stopped in correct postion for tool change
hReg = 0
hReg = mc.mcIoGetHandle(inst, SpindleInPosition)
if hReg == 0 then
return "Could not locate spindle in position signal", false
end