Machsupport Forum

Mach Discussion => Mach4 General Discussion => Topic started by: FlyingForts on November 22, 2022, 12:13:53 PM

Title: M6 Tool Change Code Question
Post by: FlyingForts on November 22, 2022, 12:13:53 PM
Hello all!
I built a CNC router for my shop class about 14 years ago. The windows XP computer we were using to run Mach3 died a couple of weeks ago. I can't find another computer with windows xp and a parallel port so I'm trying to move over and use Mach4.  I can't seem to get the M6 function to work like it did in Mach 3. I've been searching but I can't seem to find the answer to my question. Any help you can provide would be much appreciated by me and my students who are waiting to use the machine!

What I would like the M6 command to do is as follows:
1. Stop the spindle
2. Allow us to manually jog the machine to a tool change location.
3. Run the probing script to reset Z zero when I press a button on the screen. ( My script does not allow me to press the touch button on the screen)
4. Press cycle start
5. Move the machine to a safe z location
6. Start the spindle
6. Move the machine back the the starting x,y,z location
7. Resume the program

Code: [Select]
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 SpindleDwell = 20000
local xstart = mc.mcAxisGetPos (inst, 0)
local ystart = mc.mcAxisGetPos (inst, 1)

if selectedTool == currentTool then
mc.mcCntlSetLastError(inst, "Current tool == Selected tool so there is nothing to do")
else
--Remove this line if you would not like the Z axis to move

mc.mcCntlGcodeExecute(inst, "G90 G0 Z0.0");--Move the Z axis all the way up
mc.mcCntlGcodeExecute(inst, "M5 G4 P"..SpindleDwell) --Turn off spindle
mc.mcCntlSetLastError(inst, "Change to tool " .. tostring(selectedTool) .. " and press start to continue") --Message at beginning of tool change
mc.mcCntlToolChangeManual(inst, true) --This will pause the tool change here and wait for a press of cycle start to continue
--What code is needed to allow me to jog the machine and use the tool touch plate function?
mc.mcCntlSetLastError(inst, "Current tool == " .. tostring(selectedTool) .. "   Previous Tool == " .. tostring(currentTool)) --Message that shows after Cycle Start
mc.mcToolSetCurrent(inst, selectedTool)
mc.mcCntlGcodeExecuteWait(inst,"G90 G0 X"..xstart.."Y"..ystart)--Move to start location
mc.mcCntlGcodeExecute(inst, "M3 G4 P"..SpindleDwell)--Turn spindle on and wait for dwell
mc.mcCntlGcodeExecuteWait(inst, "G90 G0 Z0")--Move to Z0

end
end

if (mc.mcInEditor() == 1) then
m6()
end
Title: Re: M6 Tool Change Code Question
Post by: JohnHaine on November 22, 2022, 02:34:15 PM
You can use a Win10 computer running Mach3 with a motion controller quite happily.  I use a uc100 usb controller with a Dell mini pc and the move was painless and all my settings and macros worked.  So you could carry on using Mach3 on a new PC with the macros you are used to.
Title: Re: M6 Tool Change Code Question
Post by: FlyingForts on November 22, 2022, 03:09:57 PM
You can use a Win10 computer running Mach3 with a motion controller quite happily.  I use a uc100 usb controller with a Dell mini pc and the move was painless and all my settings and macros worked.  So you could carry on using Mach3 on a new PC with the macros you are used to.

Thanks. I purchased a PMDX-411 for mach 4. I don't think that will work with mach 3 but I will give it a shot.
Title: Re: M6 Tool Change Code Question
Post by: joeaverage on November 22, 2022, 03:14:52 PM
Hi,
you can do as you want. There is a thread that covers some of the important points:

https://www.machsupport.com/forum/index.php?topic=45873.0 (https://www.machsupport.com/forum/index.php?topic=45873.0)

You need to be able to pause the m6 macro so that the trajectory planner is released so that you can manually jog the machine.

That is what the API is for:

LUA Syntax:
rc = mc.mcFileHoldAquire(
      number mInst,
      string reason,
      number JogAxisBits)

Description:
Used to hold G code processing.


Craig
Title: Re: M6 Tool Change Code Question
Post by: FlyingForts on November 22, 2022, 04:29:13 PM
Hi,
you can do as you want. There is a thread that covers some of the important points:

https://www.machsupport.com/forum/index.php?topic=45873.0 (https://www.machsupport.com/forum/index.php?topic=45873.0)

You need to be able to pause the m6 macro so that the trajectory planner is released so that you can manually jog the machine.

That is what the API is for:

LUA Syntax:
rc = mc.mcFileHoldAquire(
      number mInst,
      string reason,
      number JogAxisBits)

Description:
Used to hold G code processing.

Craig

Thank you for the help. If I pose the M6 macro how to I start it again after I have finished the touch off?
Title: Re: M6 Tool Change Code Question
Post by: joeaverage on November 22, 2022, 04:54:05 PM
Hi,
I've just recognised that I have directed you to the wrong thread. The thread I did direct you to is related and requires the same solution but this is better:

https://www.machsupport.com/forum/index.php?topic=46728.0 (https://www.machsupport.com/forum/index.php?topic=46728.0)

You will see also that my first post in that thread is if not wrong, certainly uninformed. I made the argument that to jog the machine would require you to terminate
the current program, not just pause it. It needs be terminated so that the trajectory planner can then attend the jog commands.

Fortunately Smurph chimed in with yet another one of his gems, namely the API I posted. The complementary API is:

LUA Syntax:
rc = mc.mcFileHoldRelease(
      number mInst)

Description:
Used to exit a file hold state.

But take heed of Smurphs warning you MUST do a preparatory move back to the machine position at the moment the m6 was called....or you will crash,
just like a poorly performed RunFromHere.

Craig

Title: Re: M6 Tool Change Code Question
Post by: FlyingForts on November 22, 2022, 06:53:38 PM
JoeAverage thank you for the help, I fear I'm in over my head on this one. I think I need to find someone who can code it for me. I'm on a tight deadline to get everything back up and running for my students.
Title: Re: M6 Tool Change Code Question
Post by: joeaverage on November 22, 2022, 07:51:03 PM
Hi,
I wonder if you are making this a little more complicated than it need be.

Is there a reason for instance that you want to manually jog to a tool change location?. Does that location change from one job to the next?
If it does not change location then just have the m6 macro drive to that location, then pause while the tool change is happening....no need to FileHold
or anything fancy, then <cycle start> now have the macro drive to the probing location, again assuming this location remains fixed from one job to another, and perform
a g31 to set the tool. If the tool length is widely variable you may want to be able to manually jog the tool tip close to the toolsetter, is use FileHold, buit otherwise just probe
the tool then drive back to the machine re-start position and carry on.

Its only the need to manually jog during your macro that adds the complexity. Do you strictly need to jog?

Craig
Title: Re: M6 Tool Change Code Question
Post by: FlyingForts on November 22, 2022, 08:06:29 PM
Hi,
I wonder if you are making this a little more complicated than it need be.

Is there a reason for instance that you want to manually jog to a tool change location?. Does that location change from one job to the next?
If it does not change location then just have the m6 macro drive to that location, then pause while the tool change is happening....no need to FileHold
or anything fancy, then <cycle start> now have the macro drive to the probing location, again assuming this location remains fixed from one job to another, and perform
a g31 to set the tool. If the tool length is widely variable you may want to be able to manually jog the tool tip close to the toolsetter, is use FileHold, buit otherwise just probe
the tool then drive back to the machine re-start position and carry on.

Its only the need to manually jog during your macro that adds the complexity. Do you strictly need to jog?

Craig

The tool change location does change from job to job (My machine does not have homing switches to maintain good machine coordinates).

What I have done in the past is jog the machine to a good location to change the bit. Then jog it to the material and press the button on the screen to touch off on the plate and reset z zero. Then when I click cycle start it turns on the spindle, moves to a safe z location, moves back to the last location in the g-code, and resumes the program.

Similar to what is shown in this avid CNC video:  https://youtu.be/XUXeJywH46I?t=268
Title: Re: M6 Tool Change Code Question
Post by: joeaverage on November 22, 2022, 08:17:46 PM
Hi,
without Homing switches you are sunk.

The best single addition I ever made to my mini-mill six years ago was to add good Home switches. I used to have battles such as you are facing and plenty of
other crashes.....until I fitted Home switches, thereafter three crashes in six years, and all of those were operator induced!

Craig
Title: Re: M6 Tool Change Code Question
Post by: FlyingForts on November 22, 2022, 08:36:42 PM
Oh no well that stinks, I was kind of hopping I could do it the same way mach 3 worked but I guess not.

I tried putting them on at one point but electrical issues popped up. I have never had the the time to go back and take everything back apart and work on it. We couldn't afford to purchase one at my school so I built one years ago. It has been a huge hit and runs all of the time. I wish I would have been smart back in the day and grabbed two old windows xp machines to keep one as a backup. 

Thank you for all the help I truly appreciate it! I've been trying to get it to work for a few weeks now and finally gave up and jumped on here hopping someone could help me.
Title: Re: M6 Tool Change Code Question
Post by: joeaverage on November 22, 2022, 08:48:33 PM
Hi,
don't give up, you can still do what you want but because you cannot home regularly then you'll have to program your way around it.
Thats exactly what mach3 did under the hood, and you can replicate it if you wish.

The alternative is to fit Home switches......$50 and your done.

Craig.
Title: Re: M6 Tool Change Code Question
Post by: FlyingForts on November 22, 2022, 09:15:21 PM
Programing around my limitations would be great but I'm a novice with programming in Lua so I don't think I can do that.

As far as the machine goes I want to work on it over the summer when I have time for it to be apart. It has been running great for many years but it is time for some upgrades. I must say the students love it. When they write and run their first program it is a magical experience for most of them.  You can see their eyes light up as they watch it in pure amazement. If it's running any anyone comes in they can't help but stop and watch.
Title: Re: M6 Tool Change Code Question
Post by: smurph on November 30, 2022, 02:06:18 AM
Read the comments in the code below.  Mabey they will help you.  But as Craig said, home switches will help even more!  :) 

Code: [Select]
function m6()

local inst = mc.mcGetInstance('m6 macro script')
local selectedTool = mc.mcToolGetSelected(inst)
selectedTool = math.tointeger(selectedTool)
local currentTool = mc.mcToolGetCurrent(inst)
currentTool = math.tointeger(currentTool)
local SpindleDwell = 20000
local xstart = mc.mcAxisGetPos (inst, 0)
local ystart = mc.mcAxisGetPos (inst, 1)

if selectedTool == currentTool then
mc.mcCntlSetLastError(inst, "Current tool == Selected tool so there is nothing to do")
else
--Remove this line if you would not like the Z axis to move

mc.mcCntlGcodeExecuteWait(inst, "G90 G0 Z0.0");--Move the Z axis all the way up  -- Added the Wait variant of the API call.
mc.mcCntlGcodeExecuteWait(inst, "M5 G4 P"..SpindleDwell) --Turn off spindle          -- Added the Wait variant of the API call.
mc.mcCntlSetLastError(inst, "Change to tool " .. tostring(selectedTool) .. " and press start to continue") --Message at beginning of tool change
mc.mcCntlToolChangeManual(inst, true) --This will pause the tool change here and wait for a press of cycle start to continue 

-- mc.mcCntlToolChangeManual() uses mcFileHoldAquire() behind the scenes.  You should be able to jog you machine anywhere you want now.

-- The antidote is Cycle Start at this point.  So you could run any script from a button you choose.  This script may contain something like:

-- mc.mcCntlGcodeExecuteWait() with G31 to probe.
-- mc.mcCntlGetPoundVar() to read the probed positon G code #vars
-- mc.mcAxisSetPos() on the Z axis to set the current offset based on your tool probe. 
-- once the script is complete, press cycle start and the M6 script will continue from the next line of code

mc.mcCntlSetLastError(inst, "Current tool == " .. tostring(selectedTool) .. "   Previous Tool == " .. tostring(currentTool)) --Message that shows after Cycle Start
mc.mcToolSetCurrent(inst, selectedTool)
mc.mcCntlGcodeExecuteWait(inst, "G90 G0 Z0") -- Move Z to a safe location.  Change the position acordingly.
mc.mcCntlGcodeExecuteWait(inst,"G90 G0 X"..xstart.."Y"..ystart)  -- Move to start location.
mc.mcCntlGcodeExecuteWait(inst, "M3 G4 P"..SpindleDwell)  -- Turn spindle on and wait for dwell (You should really setup the spindle ramp times instead of using a dwell here.)
end
end

if (mc.mcInEditor() == 1) then
m6()
end

Steve
Title: Re: M6 Tool Change Code Question
Post by: FlyingForts on December 03, 2022, 07:07:24 PM
Thank you, I will give it a shot on Monday.