Hello Guest it is November 22, 2019, 10:37:37 AM

Author Topic: MACH4 Mcode testing  (Read 8984 times)

0 Members and 1 Guest are viewing this topic.

Offline BR549

*
  •  6,916 6,916
    • View Profile
MACH4 Mcode testing
« on: May 25, 2014, 10:51:32 PM »
Has anyone gotten a NEW Mcode to work properly. I was doing a simple test to see IF I could create a new Mcode.

I tried it this way, AND that errored out in the editor

-- Serial Number Engrave
function m100()
inst=mc.mcGetInstance();
mc.mcCntlSetLastError(inst, 'Serial Number Engrave')
mc.mcCntlGcodeExecute(inst, "G0X10 ");
mc.mcCntlGcodeExecute(inst, "G0X0 ");
end


SO I tried it this way. It will run from the EDITOR if you step though it but not from a M100 call in MACH4.  OR will not run with RUN in the editor.

-- Serial Number Engrave
inst=mc.mcGetInstance();
mc.mcCntlSetLastError(inst, 'Serial Number Engrave')
mc.mcCntlGcodeExecute(inst, "G0X10 ");
mc.mcCntlGcodeExecute(inst, "G0X0 ");


Can we create Mcodes for Mach4 ??

Does anyone have an example that works??

, (;-) TP

Offline smurph

*
  • *
  •  1,151 1,151
  • "That there... that's an RV."
    • View Profile
Re: MACH4 Mcode testing
« Reply #1 on: May 25, 2014, 11:53:49 PM »
Did you name the file m100.mcs?   Go look at m3.mcs.  It is NAMED m3.mcs, there is a function NAMED m3.mcs, and then there is a piece of code at the bottom of the script that tests to see if it is being run from the editor.  If it is running in the editor, if calls the m3() function inside that if block.

In a file called m100.mcs, put:
Code: [Select]
function m100()
    inst=mc.mcGetInstance();
    mc.mcCntlSetLastError(inst, 'Serial Number Engrave')
    mc.mcCntlGcodeExecute(inst, "G0X10 ");
    mc.mcCntlGcodeExecute(inst, "G0X0 ");
end

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

It is like that for a reason.  It has to be that way where the scripts run in compiled mode as part of a larger "chunk" in Mach 4 and it also enables you to run the "snippet" inside the editor to debug it.

Steve

Offline BR549

*
  •  6,916 6,916
    • View Profile
Re: MACH4 Mcode testing
« Reply #2 on: May 26, 2014, 02:09:07 AM »
You mean this ??    NONE of the Mcodes in the macro DIR  here have that piece of script, I just looked.

 if (mc.mcInEditor() == 1) then
    m100()


Offline BR549

*
  •  6,916 6,916
    • View Profile
Re: MACH4 Mcode testing
« Reply #3 on: May 26, 2014, 02:17:10 AM »
Steve it still does not work even with your code. It tries to run it BUT it only moves about .236" of the first line of Gcode then BLOWS right on past the REST of the Gcode lines and ENDS.

NOW ONLY IF you step though the code in the LUA editor will it complete the Motion. USE RUN and you get the same problem.

TP

Offline smurph

*
  • *
  •  1,151 1,151
  • "That there... that's an RV."
    • View Profile
Re: MACH4 Mcode testing
« Reply #4 on: May 26, 2014, 02:40:44 AM »
Try combining the two Gcode executes.  Like:

mc.mcCntlGcodeExecute(inst, "G0X10\nG0X0");

I wasn't looking too closely at your original script and I didn't catch that.  If you'll take a look at the m6 macro, you'll see where we did the same sort of thing that combines a lot of lines of G code into one call to mc.mcCntlGcodeExecute().

The technical reason is that each call to mc.mcCntlGcodeExecute() fires up it's own interpreter.  It is not like feeding a new line to the same interpreter.  It executes the code as a unit of work.  If you want the script to wait on the G code to complete before moving on, call mc.mcCntlGcodeExecuteWait() instead.

Steve

Offline BR549

*
  •  6,916 6,916
    • View Profile
Re: MACH4 Mcode testing
« Reply #5 on: May 26, 2014, 03:53:49 AM »
OK that worked for the simple stuff  one or two lines of Gcode. Have you ever tried running a Sub or Gcode Macro from the Mcode. such as

mc.mcCntlGcodeExecuteWait(inst, "M98 P9020 ");

or

mc.mcCntlGcodeExecuteWait(inst, "G65 P9020 A10 B1.125");

I have an sequential serial number engraving function that works well from a SUB or G65 macro and I am trying to get it to run as a Mcode using the G65 call for inside the Mcode.

TP

The cgode is over 200 lines and uses condition redirection a LOT.

Offline BR549

*
  •  6,916 6,916
    • View Profile
Re: MACH4 Mcode testing
« Reply #6 on: May 26, 2014, 10:45:09 AM »
Using

mc.mcCntlGcodeExecuteWait(inst, "G65 P9020 A10 B1.125");

Should help with large Mcodes with lots of Gcode lines IF it would run the G65 or M98 as a SUB.

IS there a limit to the nesting of a sub ?? Maybe that is the problem.

 I'll test some more,(;-) TP

Offline BR549

*
  •  6,916 6,916
    • View Profile
Re: MACH4 Mcode testing
« Reply #7 on: May 26, 2014, 12:04:19 PM »
Steve IF you get a chance could you test this script . HERE it will try to run the very first time your try it after staring Mach4. It stalls out in the Gcode somewhere randomly.

after the first run it will never run again untill you restart MAch4.

What happens IS on line 3 where it is to get the #var value it stalls for about 20 seconds and return the WRONG value(Looks like it is trying to find it but cannot) it returns "0" when it should return the #var value(it does it correctly the first time it runs)

I cannot tell if it is the PC ,Mach4 or me so I would appreciate a 2nd set of eyes on this one.

Thanks (;-) TP

Offline smurph

*
  • *
  •  1,151 1,151
  • "That there... that's an RV."
    • View Profile
Re: MACH4 Mcode testing
« Reply #8 on: May 26, 2014, 02:28:53 PM »
Did you forget to post the script?  Or are you talking about the one above?

Offline BR549

*
  •  6,916 6,916
    • View Profile
Re: MACH4 Mcode testing
« Reply #9 on: May 26, 2014, 02:56:51 PM »
OOPS, sorry here is the script I am trying to run it as a Mcode.

function m100()
    inst=mc.mcGetInstance()
local SNv = mc.mcCntlGetPoundVar(inst , 590)
local str = string.sub(SNv,1,6)
 wx.wxMessageBox(tostring(str))
local SN = {}
for i = 1, #str do
    SN = str:sub(i, i)
end


  for _,v in ipairs(SN) do
    if     v == "1" then
            mc.mcCntlGcodeExecuteWait(inst, "G0 X1.0");
    elseif v == "2" then
            mc.mcCntlGcodeExecuteWait(inst, "G0 X2.0");
    elseif v == "3" then
            mc.mcCntlGcodeExecuteWait(inst, "G0 X3.0");
    elseif v == "4" then
            mc.mcCntlGcodeExecuteWait(inst, "G0 X4.0");
    elseif v == "5" then
            mc.mcCntlGcodeExecuteWait(inst, "G0 X5.0");
    elseif v == "6" then
            mc.mcCntlGcodeExecuteWait(inst, "G0 X6.0");
    elseif v == "7" then
            mc.mcCntlGcodeExecute(inst, "G0 X7.0");
    elseif v == "8" then
            mc.mcCntlGcodeExecuteWait(inst, "G0 X8.0");
    elseif v == "9" then
            mc.mcCntlGcodeExecuteWait(inst, "G0 X9.0");
    elseif v == "0" then
            mc.mcCntlGcodeExecuteWait(inst, "G0 X0.0");

    end
  end


--SN = (SN +1)
mc.mcCntlSetPoundVar(inst , 590,(str +1))
 
end

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