Hello Guest it is April 25, 2024, 06:52:46 PM

Author Topic: m03, m05 and G04 dwell commands  (Read 329 times)

0 Members and 1 Guest are viewing this topic.

m03, m05 and G04 dwell commands
« on: August 01, 2023, 04:44:00 PM »
So, I have an m6 tool change macro that I am trying to fix before something terrible happens.

My m6 macro checks to see if the spindle is running and if it is, it commands an m5 and then a g04 to dwell for 10 seconds. The problem is it doesn't dwell after the m5. The m5 must have a pause built-in because it doesn't move right away to perform a tool change but the spindle never fully stops before heading to do a tool change. It will usually just stops before entering the forks.

Another confusing thing is when I test it in MDI, it seems to work. When performing a tool change in a g-code program, it doesn't. Am I crazy?

What is the built-in pause for accel and decel in M03 and m05?

Code: [Select]
function m6()

 local inst=mc.mcGetInstance()
 mc.mcCntlSetLastError(inst, 'Tool change in progress')
 
 
 ------ Get and compare next and current tools ------
 local SelectedTool = mc.mcToolGetSelected(inst)
 local CurrentTool = mc.mcToolGetCurrent(inst)
 if (SelectedTool == CurrentTool) then
 mc.mcCntlSetLastError(inst, "Next tool = Current tool")
 mc.mcCntlGcodeExecute(inst, "G43 H"..CurrentTool) --Turn on tool length offset for current tool
 do return end
end
mc.mcCntlSetLastError(inst, 'Break 0')

 ------ Define slide distance and direction ------
 ------ Only 1 of these should have a non-zero value -----
 ------ Changing from a positive to negative slide value, or vice-versa, will change the direction that the tool slides into the tool fork -----
 local XSlide = 0.00
 local YSlide = 2.00
 local dwell = 10.00
 
------- Declare Position Variables ------
 local XPos = 0
 local YPos = 0
 local ZPos = 0
 
 ------ For spindles that have a partial tool push out, define the amount of movement here -----
 local ZBump = 0.100
 
 ------ Define the OUTPUT# for the drawbar signal -----
 local DrawBarOut = mc.OSIG_OUTPUT6
 
  ------ Define the OUTPUT# for the dusthood signal -----
 local DustHood = mc.OSIG_OUTPUT7

 ------ Get current state ------
 local CurFeed = mc.mcCntlGetPoundVar(inst, 2134)
 local CurFeedMode = mc.mcCntlGetPoundVar(inst, 4001)
 local CurAbsMode = mc.mcCntlGetPoundVar(inst, 4003)
 
 ------ Raise Dust Hood if dust hood is down ------
 local dhsig = mc.mcSignalGetHandle(inst, DustHood)
 local dhstate = mc.mcSignalGetState(dhsig)
if (dhstate == 0) then
mc.mcSignalSetState(dhsig, 1)
end

 
 ------ Check to if spindle is on, if on, turn off spindle and wait for decel -------
if (mc.mcSpindleGetDirection(0) ~= 0) then
local GCode = ""
GCode = GCode .. "M5\n"
GCode = GCode .. string.format("G04 P%.4f\n", dwell)
mc.mcCntlGcodeExecuteWait(inst, GCode)
end
 
 
 ------ Move to current tool change position ------
 local tool = CurrentTool


--You will need to enter the tool position below. 
--Once you enter them, copy and paste the area defined
--below to the second set of positions further down in
--the script.  The two sets of tool positions must match
--exactly, or unexpected motion will occur!!!

-----------------Copy Start-------------------
----- Define Tool Postions ------
--Tool 1--
if tool == 1 then
XPos = 7.161
YPos = 49.144
ZPos = -6.283

--Tool 2--
elseif tool == 2 then
XPos = 11.447
YPos = 49.144
ZPos = -6.283

--Tool 3--
elseif tool == 3 then
XPos = 15.733
YPos = 49.144
ZPos = -6.283   

--Tool 4--
elseif tool == 4 then
XPos = 20.005
YPos = 49.144
ZPos = -6.283

--Tool 5--
elseif tool == 5 then
XPos = 24.322
YPos = 49.144
ZPos = -6.283 

--Tool 6--
elseif tool == 6 then
XPos = 28.593
YPos = 49.144
ZPos = -6.283   

--Tool 7--
elseif tool == 7 then
XPos = 32.859
YPos = 49.144
ZPos = -6.283   

--Tool 8--
elseif tool == 8 then
XPos = 37.126
YPos = 49.144
ZPos = -6.283

--Tool 9--
elseif tool == 9 then
XPos = 41.432
YPos = 49.144
ZPos = -6.283 

--Tool 10--
elseif tool == 10 then
XPos = 45.755
YPos = 49.144
ZPos = -6.283

---------------Copy Stop------------------

else
wx.wxMessageBox("Invalid tool #. Cancelling tool change!\nYour requested tool will now be set as the current tool in the spindle.\n****Ensure that the tool station that you have selected is empty!****")
mc.mcToolSetCurrent(inst, SelectedTool)
mc.mcCntlGcodeExecute(inst, "G43 H"..SelectedTool) --Turn on tool length offset for selected tool
 ------ Lower DustHood if originally was down at start of m6------
 if (dhstate == 0) then
mc.mcSignalSetState(dhsig, 0)
else
mc.mcSignalSetState(dhsig, 1)
end
do return end

end


 local GCode = ""
 GCode = GCode .. "G00 G90 G53 Z0.0\n"
 GCode = GCode .. string.format("G00 G90 G53 X%.4f Y%.4f\n", (XPos-XSlide), (YPos-YSlide))
 GCode = GCode .. string.format("G00 G90 G53 Z%.4f\n", ZPos)
 GCode = GCode .. string.format("G00 G90 G53 X%.4f Y%.4f\n", XPos, YPos)
 mc.mcCntlGcodeExecuteWait(inst, GCode)
 
 ------ Open drawbar ------

 local hsig = mc.mcSignalGetHandle(inst, DrawBarOut)
 mc.mcSignalSetState(hsig, 1)
 
 
 ------ Raise spindle, after releasing tool ------
 GCode = ""
 GCode = GCode .. string.format("G01 G90 G53 Z-3.00 F50.0\n")
 mc.mcCntlGcodeExecuteWait(inst, GCode)
 
 ------ Move to next tool change position ------
 tool = SelectedTool


-----------------Copy Start-------------------
----- Define Tool Postions ------
--Tool 1--
if tool == 1 then
XPos = 7.161
YPos = 49.144
ZPos = -6.283

--Tool 2--
elseif tool == 2 then
XPos = 11.447
YPos = 49.144
ZPos = -6.283

--Tool 3--
elseif tool == 3 then
XPos = 15.733
YPos = 49.144
ZPos = -6.283 

--Tool 4--
elseif tool == 4 then
XPos = 20.005
YPos = 49.144
ZPos = -6.283

--Tool 5--
elseif tool == 5 then
XPos = 24.322
YPos = 49.144
ZPos = -6.283   

--Tool 6--
elseif tool == 6 then
XPos = 28.593
YPos = 49.144
ZPos = -6.283   

--Tool 7--
elseif tool == 7 then
XPos = 32.859
YPos = 49.144
ZPos = -6.283

--Tool 8--
elseif tool == 8 then
XPos = 37.126
YPos = 49.144
ZPos = -6.283

--Tool 9--
elseif tool == 9 then
XPos = 41.432
YPos = 49.144
ZPos = -6.283 

--Tool 10--
elseif tool == 10 then
XPos = 45.755
YPos = 49.144
ZPos = -6.283

---------------Copy Stop------------------


else
wx.wxMessageBox("Invalid tool #.  Retrieving previous tool!")
SelectedTool = CurrentTool
mc.mcCntlGcodeExecute(inst, "G43 H"..CurrentTool) -- Turn on Tool Length Offset to CurrentTool
mc.mcSignalSetState(dhsig, 0) -- Lower DustHood

end

 GCode = ""
 GCode = GCode .. string.format("G00 G90 G53 X%.4f Y%.4f\n", XPos, YPos)
 GCode = GCode .. string.format("G00 G90 G53 Z%.4f\n", ZPos + ZBump)
 mc.mcCntlGcodeExecuteWait(inst, GCode)
 
 ------ Clamp drawbar ------
 mc.mcSignalSetState(hsig, 0)
 
 GCode = ""
 GCode = GCode .. string.format("G01 G90 G53 Z%.4f F50.0\n", ZPos)
 GCode = GCode .. string.format("G00 G90 G53 X%.4f Y%.4f\n", (XPos-XSlide), (YPos-YSlide))
 mc.mcCntlGcodeExecuteWait(inst, GCode)


 ------ Move Z to home position ------
 GCode = ""
 mc.mcCntlGcodeExecuteWait(inst, "G00 G90 G53 Z0.0\n")
 mc.mcCntlGcodeExecute(inst, "G43 H"..SelectedTool) -- Turn on Tool Length Offset to selected tool

 ------ Reset state ------
 mc.mcCntlSetPoundVar(inst, 2134, CurFeed)
 mc.mcCntlSetPoundVar(inst, 4001, CurFeedMode)
 mc.mcCntlSetPoundVar(inst, 4003, CurAbsMode)

 ------ Set new tool ------
 mc.mcToolSetCurrent(inst, SelectedTool)
 mc.mcCntlSetLastError(inst, string.format("Tool change - Tool: %.0f", SelectedTool))
 
 ------ Lower DustHood if originally was down at start of m6------
 if (dhstate == 0) then
mc.mcSignalSetState(dhsig, 0)
else
mc.mcSignalSetState(dhsig, 1)
end

end

Kenny
Re: m03, m05 and G04 dwell commands
« Reply #1 on: August 02, 2023, 10:32:00 AM »
first thing I would try is to check the value in 'GCode' to make sure you have a valid string to execute.
GCode = GCode .. string.format("G04 P%.4f\n", dwell)
Re: m03, m05 and G04 dwell commands
« Reply #2 on: August 02, 2023, 10:41:09 AM »
How do I check the value in GCode string? I looked over the syntax and everything seems to be correct.
Re: m03, m05 and G04 dwell commands
« Reply #3 on: August 02, 2023, 10:49:06 AM »
mc.mcCntlSetLastError(inst, GCode )
Re: m03, m05 and G04 dwell commands
« Reply #4 on: August 02, 2023, 11:36:58 AM »
Thanks for showing me how to see the value of GCode.

Works as it should in MDI but not while running a GCode program.

I figured out the issue. The post-processor is including an M5(as it should) to stop the spindle so when the M6 tool change is run, the "if" command to check if the spindle is on, is no longer valid so it doesn't pause to allow the spindle to stop!

Code: [Select]
------ Check to if spindle is on, if on, turn off spindle and wait for decel -------
if (mc.mcSpindleGetDirection(0) ~= 0) then
local GCode = ""
GCode = GCode .. "M5\n"
GCode = GCode .. string.format("G04 P%.4f\n", dwell)
mc.mcCntlGcodeExecuteWait(inst, GCode)
end
 

After testing,