Hello Guest it is April 19, 2024, 02:03:13 AM

Author Topic: Tool Length Offset Macro Program  (Read 5134 times)

0 Members and 1 Guest are viewing this topic.

Tool Length Offset Macro Program
« on: May 03, 2021, 09:32:04 AM »
I have seen A LOT of people asking about a tool length offset macro.
I made this Macro Program (G Code) for my tool length offsets. 

This is not a Macro in the sense of Mach4 Macros.  This is a Macro B Program.  It is G Code.

Put the 9001 and 9002 programs in the Subroutines folder in the Mach4 Directory.
Put two buttons on the screen to probe the tool length offset and to probe a rotating tool.  One or both, you do you.
Put the attached scripts into the appropriate button script.
This will probe the current tool that is active in Mach4. 

This Macro Program will set the tool offsets at a Machine Position.  So homing the Z Axis is a must for this to work well for you.
This will set every tool to the same position (your tool setter).  So you can (and need to) set your Z Work Offset with any tool that has been touched off.
You MUST have your Tool Height Offset active when you set your Z Work Offset. 
You MUST have your Tool Height Offset active when you set your Z Work Offset.
You MUST have your Tool Height Offset active when you set your Z Work Offset.

The rotating tool probe will use the diameter value in the tool table.  So be sure to put something in there if you intend to use that one. 
It will offset half of the diameter and rotate CCW and probe slow.  This is for tools tool large for the tool setter (exp:  Face Mills)

You will see variables inside the code that you will need to change for yourself.
Tool Setter X and Y positions. 
Feedrate for fast probe and slow probe.
Lift Height after fast probe.
ETC.....
Just be sure to adjust to your own needs. 

I also use G31 instead of G28 so I be sure you have something 0.000 in #5183 (I think it's #5183) so it will go to Z Home; or change it to G28, or take it out and make sure you put a safe Z in there first.

USE AT YOUR OWN RISK. 
I am using Mach4 Hobby so I'm not able to do conditions in the Macro Program for safety checks. 
Check the macro manually first.  Get it set up, hit the probe button and press the tool setter manually and be sure everything works.
As always when using CNC Machines, be safe and diligent.   
Chad Byrd

Offline Bill_O

*
  •  563 563
    • View Profile
Re: Tool Length Offset Macro Program
« Reply #1 on: May 03, 2021, 10:25:59 AM »
This is the code I use and it works fine.
This is in my Screen Load Script.
There is a lot of extra stuff in here but go to the section on ATC.

function Surface()
   ----remove max depth cut
   local MaxDepthRegH = mc.mcRegGetHandle(inst, 'iRegs0/MaxDepth')
   mc.mcRegSetValue(MaxDepthRegH, -22.0)
   mc.mcCntlSetParameter(1801, -22.0)
   --set extra cut depth to 0 and turn off leds
   local Extrareg = mc.mcRegGetHandle(inst, 'iRegs0/ExtraDepth')
   mc.mcRegSetValue(Extrareg, 0.000)
   mc.mcCntlSetPoundVar(inst, mc.SV_HEAD_SHIFT_Z, 0.000)
   scr.SetProperty('led010', 'Value', '0')
   scr.SetProperty('led030', 'Value', '0')
   scr.SetProperty('led050', 'Value', '0')
   --set surface register to 0
   hreg = mc.mcRegGetHandle(inst, 'iRegs0/SurfSet')
   mc.mcRegSetValue(hreg, 0)
   --turn surface set led off
   scr.SetProperty('ledSurfaceSet', 'Value', '0')
   --get sm on or off register
   local SMReg = mc.mcRegGetHandle(inst, 'iRegs0/SMOnOff')
   local SMRegVal = mc.mcRegGetValue(SMReg)
   --set to project coordinates
   mc.mcCntlGcodeExecuteWait(inst, "g54")

   --check if using tang knife
   local TKOnOff = mc.mcCntlGetParameter(502)
   if (TKOnOff == 1) then
      SMRegVal = mc.mcRegSetValue(SMReg, 0)
      --scr.SetProperty('ledSMOnOff', 'Value', '0')
   end

      --check for ATC
      local ATCReg = mc.mcRegGetHandle(inst, 'iRegs0/ATCOnOff')
      local ATCRegVal = mc.mcRegGetValue(ATCReg)
      --if ATC turned off
      if (ATCRegVal == 0) then
         mc.mcCntlGcodeExecuteWait(inst, "G49")
         mc.mcToolSetCurrent(inst, 0)
         --If SM turned OFF
         if (SMRegVal == 0) then
            --set to 0 and lift 0.5
            mc.mcAxisSetPos(inst, 2, 0.0)
            mc.mcCntlGcodeExecuteWait(inst, "g0 z0.5")
            SMHit = 1
         end
         --If SM turned ON
         if (SMRegVal == 1) then
            --set jog speed to 100%
            scr.SetProperty('sliJogRate', 'Value', tostring(100))
            --wx.wxMessageBox("SM Used")
            mc.mcCntlGcodeExecuteWait(inst, "g31 z-8.0 f20.0")
            --check to see if sm hit
            SMHit = 0
            SMHit, rc = mc.mcCntlProbeGetStrikeStatus(inst)
            if (SMHit == 1) then
               mc.mcAxisSetPos(inst, 2, 1.5)
               --wx.wxMilliSleep(100)
               mc.mcCntlGcodeExecuteWait(inst, "g0 z2.0")
            else
               wx.wxMessageBox("DID NOT HIT SURFACE MOUSE")
               wx.wxMessageBox("MUST CLICK RESET BEFORE NEXT ATTEMPT")
            end
         end
      end   
      --if ATC turned on
      if (ATCRegVal == 1) then
         rc = wx.wxMessageBox("Have any of the tools in the ATC been changed?", "ATC Tools Changed?",2)
         if (rc == 2) then
            local CurrentTool = mc.mcToolGetCurrent(inst)
            mc.mcCntlGcodeExecuteWait(inst, "G49")
         
            hregMHmd = mc.mcRegGetHandle(inst, 'iRegs0/MachHmd')
            valMHmd = mc.mcRegGetValue(hregMHmd)
            if (valMHmd == 1) then
               --If SM turned OFF
               if (SMRegVal == 0) then
                  local ZCurPos = mc.mcAxisGetPos(inst, 2)
                  mc.mcToolSetData(inst, mc.MTOOL_MILL_HEIGHT, CurrentTool, ZCurPos)
                  local ZCurClear = (ZCurPos + 0.5)
                  mc.mcCntlGcodeExecuteWait(inst, "g0 Z" .. ZCurClear)
                  mc.mcCntlGcodeExecuteWait(inst, "G43")
                  SMHit = 1
               end
               --If SM turned ON
               if (SMRegVal == 1) then
                  --set jog speed to 100%
                  scr.SetProperty('sliJogRate', 'Value', tostring(100))
                  --wx.wxMessageBox("SM Used")
                  mc.mcCntlGcodeExecuteWait(inst, "g31 z-8.0 f20.0")
                  --check to see if sm hit
                  SMHit = 0
                  SMHit, rc = mc.mcCntlProbeGetStrikeStatus(inst)
                  if (SMHit == 1) then
                     --mc.mcAxisSetPos(inst, 2, 1.5)
                     --wx.wxMilliSleep(100)
                     --mc.mcCntlGcodeExecuteWait(inst, "g0 z2.0")
                     local ZCurPos = mc.mcAxisGetPos(inst, 2)
                     local ZActualPos = (ZCurPos - 1.5)
                     mc.mcToolSetData(inst, mc.MTOOL_MILL_HEIGHT, CurrentTool, ZActualPos)
                     local ZCurClear = (ZCurPos + 0.5)
                     mc.mcCntlGcodeExecuteWait(inst, "g0 Z" .. ZCurClear)
                     mc.mcCntlGcodeExecuteWait(inst, "G43")
                  else
                     wx.wxMessageBox("DID NOT HIT SURFACE MOUSE")
                     wx.wxMessageBox("MUST CLICK RESET BEFORE NEXT ATTEMPT")
                  end
               end
            else
               wx.wxMessageBox("Must set Machine Home.")
            end
         elseif (rc == 8) then
            local CurrentTool = mc.mcToolGetCurrent(inst)
            mc.mcCntlGcodeExecuteWait(inst, "G43")
            SMHit = 1
         end
      end      
      --set Surface register to 1 and LED On
      if (SMHit == 1) then
         hreg = mc.mcRegGetHandle(inst, 'iRegs0/SurfSet')
         mc.mcRegSetValue(hreg, 1)
         --turn surface set led on
         scr.SetProperty('ledSurfaceSet', 'Value', '1')
      end
end

Re: Tool Length Offset Macro Program
« Reply #2 on: May 03, 2021, 11:41:15 AM »
The reason I started this thread was because I kept seeing people complaining about the Tool Probing.
I've used this Macro Program for a few years now on several machines.  I haven't had any issues out of it and it works great every time.  I don't have to worry about Mach freezing or messing up and it is A LOT less code to write, mainly because 1) It's basically plug and play.  2) It's G Code.

I love Mach4 and the ability to change basically anything we want.  I make custom screens and macros all the time.  But for simplicity purposes for new users, learning LUA is a huge curve.  This is simple; make a button, throw in the script I have provided; open the G Code editor and change a few numbers and you're good to go.
Chad Byrd

Offline Bill_O

*
  •  563 563
    • View Profile
Re: Tool Length Offset Macro Program
« Reply #3 on: May 03, 2021, 02:24:19 PM »
Cbyrdtopper,

I know why you started it after seeing the several posts about tool probing.
I was basically just letting them know there are many ways to do what they wanted.
Yours is much, much easier to start with.
Yes Lua can be a bear for us non programmers but it is well worth the effort to get exactly what you want.
Re: Tool Length Offset Macro Program
« Reply #4 on: May 03, 2021, 02:28:08 PM »
Bill_O
Gotcha!  Yeah, a ton of ways to do this with Mach4. 
If you have the time to put into LUA, it can be very powerful that is for sure!!
Chad Byrd

Offline smurph

*
  • *
  •  1,546 1,546
  • "That there... that's an RV."
    • View Profile
Re: Tool Length Offset Macro Program
« Reply #5 on: May 05, 2021, 03:47:32 PM »
Chad,

Yeah, MACRO B is the way to go.  Thanks for sharing!

However, Hobby users don't have MACRO B, so they will have to use the API method like Bill presented. 

Steve
Re: Tool Length Offset Macro Program
« Reply #6 on: May 10, 2021, 03:26:46 PM »
Wow, you guys are blowing my head with the Lua.... i'm a mechanical guy lol! i have a lua Script, please forgive the amature question, but am i right or wrong in the following,

1. tool offsets off when probing fixed tool setter
2. Tool offsets on when zeroing surface of material

Thanks TIA
 
Re: Tool Length Offset Macro Program
« Reply #7 on: May 10, 2021, 03:30:30 PM »
The macro I put in the zipped folder is not LUA.  Using the program I put in the zipped folder, you’ll need to make sure the tool height offset is active when setting your z work offset. It doesn’t matter when you probe the tool. It only uses the machine coordinates to get the information it needs.
Chad Byrd
Re: Tool Length Offset Macro Program
« Reply #8 on: May 10, 2021, 03:35:37 PM »
The macro I put in the zipped folder is not LUA.  Using the program I put in the zipped folder, you’ll need to make sure the tool height offset is active when setting your z work offset. It doesn’t matter when you probe the tool. It only uses the machine coordinates to get the information it needs.
That's great. I used to be a cnc operator 20 years ago but I'm no coder!  I am used to cnc where you put the new cutter in, set the WPC and go..... I'm trying to set up my self build the same so have not been involved with developing lua script etc, though i have some success all i need is for mach to know the difference in tools and bring them to the right place!
Tim
Re: Tool Length Offset Macro Program
« Reply #9 on: May 10, 2021, 03:54:01 PM »
If you are using a tool setter probe, then this should get you closer to that. 
Once you probe your tools, then use any of the probed tools to set your Z Work Offset, make sure your tool height offset is active.
The "Offset" tab is a pretty awesome place for setting work offsets.
They put in a Gage Block Length and a Set Z Offset button that I use to set work offsets when I'm using Mach4.  I use a 2in tool setter to get my Z Work offset, so I would put 2.00 in the gage block DRO, make sure my Tool Height Offset was active (there is a toggle button in the top right of the Offset tab) and then press "Set Z".  So easy!
Chad Byrd