Hello Guest it is December 10, 2019, 07:54:28 PM

Author Topic: MACH4 Mcode testing  (Read 9002 times)

0 Members and 1 Guest are viewing this topic.

Offline BR549

*
  •  6,916 6,916
    • View Profile
Re: MACH4 Mcode testing
« Reply #10 on: May 26, 2014, 05:08:55 PM »
Steve here is another example of a SIMPLE Mcode to do a simple tap operation. It will not run from the MDI or a Gcode program call.

It will TRY to cycle the first line of code as you can SEE the TOOL CHANGE LED blink but will run the Tap cycle IF you run it from the editor. BUT same as the other example ONLY the first time you run it. After that it is a nogo until you restart Mach4.

function m111()
    inst=mc.mcGetInstance()

    mc.mcCntlGcodeExecuteWait(inst, "T111 \n M6  ");

    mc.mcCntlGcodeExecuteWait(inst, "  M3 S500 M8 \n G4P3 \n G84 Z-0.5000 R.1 G98 F10 \n M5 M9");

end

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


(;-) TP
« Last Edit: May 26, 2014, 05:13:14 PM by BR549 »

Offline smurph

*
  • *
  •  1,155 1,155
  • "That there... that's an RV."
    • View Profile
Re: MACH4 Mcode testing
« Reply #11 on: May 26, 2014, 06:30:16 PM »
For your first example, I don't know what you were doing with that table.  I assume that you wanted to walk the digits of the #590 var.  This is more easily accomplished by converting the #var number to a string.  This is done by the "string.sub(SNv, 1, 6);".  Then just loop through each character.

Code: [Select]
function m100()
    local inst = mc.mcGetInstance();
    local SNv = mc.mcCntlGetPoundVar(inst , 590);
    local str = string.sub(SNv, 1, 6);
    local idx = 0;
    --wx.wxMessageBox(tostring(str));
    --local SN = {}
    local SN;
    for idx = 1, #str do
        SN = str:sub(idx, idx);
        if     SN == "1" then
                mc.mcCntlGcodeExecuteWait(inst, "G0 X1.0");
        elseif SN == "2" then
                mc.mcCntlGcodeExecuteWait(inst, "G0 X2.0");
        elseif SN == "3" then
                mc.mcCntlGcodeExecuteWait(inst, "G0 X3.0");
        elseif SN == "4" then
                mc.mcCntlGcodeExecuteWait(inst, "G0 X4.0");
        elseif SN == "5" then
                mc.mcCntlGcodeExecuteWait(inst, "G0 X5.0");
        elseif SN == "6" then
                mc.mcCntlGcodeExecuteWait(inst, "G0 X6.0");
        elseif SN == "7" then
                mc.mcCntlGcodeExecuteWait(inst, "G0 X7.0");
        elseif SN == "8" then
                mc.mcCntlGcodeExecuteWait(inst, "G0 X8.0");
        elseif SN == "9" then
                mc.mcCntlGcodeExecuteWait(inst, "G0 X9.0");
        elseif SN == "0" then
                mc.mcCntlGcodeExecuteWait(inst, "G0 X0.0");
        end
    end

    mc.mcCntlSetPoundVar(inst , 590, tonumber(str) + 1);
 
end

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

Offline smurph

*
  • *
  •  1,155 1,155
  • "That there... that's an RV."
    • View Profile
Re: MACH4 Mcode testing
« Reply #12 on: May 26, 2014, 07:18:26 PM »
A better example.  m100 will not execute at all from MDI or G code file.  Because user M codes start at 101.  It will run in the editor though.  So I changed it to M101 and it works fine.

Code: [Select]
function m101()
    local inst = mc.mcGetInstance();
    mc.mcCntlSetLastError(inst, "m101");
    local SNv = mc.mcCntlGetPoundVar(inst , 590);
    local str = string.sub(SNv, 1, 6);
    local idx = 0;
    mc.mcCntlSetLastError(inst, "#590 = " .. str);
    --local SN = {}
    local SN;
    for idx = 1, #str do
        SN = str:sub(idx, idx);
        if     SN == "1" then
                mc.mcCntlGcodeExecuteWait(inst, "G0 X1.0");
        elseif SN == "2" then
                mc.mcCntlGcodeExecuteWait(inst, "G0 X2.0");
        elseif SN == "3" then
                mc.mcCntlGcodeExecuteWait(inst, "G0 X3.0");
        elseif SN == "4" then
                mc.mcCntlGcodeExecuteWait(inst, "G0 X4.0");
        elseif SN == "5" then
                mc.mcCntlGcodeExecuteWait(inst, "G0 X5.0");
        elseif SN == "6" then
                mc.mcCntlGcodeExecuteWait(inst, "G0 X6.0");
        elseif SN == "7" then
                mc.mcCntlGcodeExecuteWait(inst, "G0 X7.0");
        elseif SN == "8" then
                mc.mcCntlGcodeExecuteWait(inst, "G0 X8.0");
        elseif SN == "9" then
                mc.mcCntlGcodeExecuteWait(inst, "G0 X9.0");
        elseif SN == "0" then
                mc.mcCntlGcodeExecuteWait(inst, "G0 X0.0");
        end
    end
    mc.mcCntlSetLastError(inst, "Setting #590 = " .. tostring(SNv + 1));
    mc.mcCntlSetPoundVar(inst , 590, SNv + 1);
end

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

Offline BR549

*
  •  6,916 6,916
    • View Profile
Re: MACH4 Mcode testing
« Reply #13 on: May 26, 2014, 07:38:32 PM »
OK SO, M101 is the starting point of User Macros is there a upper limit of macros one can create?

The first example take the #var(6 digit serial Number) and WILL eventially engrave the full 6 digit serial number one digit at a time. After teh engraving the SN is incremented by 1 for teh next call.

The problem with it is it will ONLY run one time per session of MACH4 else it errors ou because it cannot find the #var value again it searches for something for about 20-30 seconds then ends with a 0 value for the #var then ends.

Offline BR549

*
  •  6,916 6,916
    • View Profile
Re: MACH4 Mcode testing
« Reply #14 on: May 26, 2014, 08:32:34 PM »
HIYA Steve I tried your version and it tried to run 1 time and got to the 4th digit then stopped, crashed ,after about 30 seconds it cleared BUT would not run again.

No need to go further with it

Thanks for the TIME, (;-) TP

Offline smurph

*
  • *
  •  1,155 1,155
  • "That there... that's an RV."
    • View Profile
Re: MACH4 Mcode testing
« Reply #15 on: May 27, 2014, 01:27:10 AM »
Hmm...  mine works every time.  I also have G code that uses Macro B that does a nice job of serial numbers.

Steve

Offline BR549

*
  •  6,916 6,916
    • View Profile
Re: MACH4 Mcode testing
« Reply #16 on: May 27, 2014, 12:49:21 PM »
YEP I have a SUB/Macro that does Sequential SN, Engrave, Etc.

Was just playing to see what  Mach4 could do as Mcodes.

 Mcodes are simple just type in the MCODE #(M101) . Alot of users don't quite get the Subs/G65macro deal YET. BUT hopefully they will soon.

I was HOPING that Mach4 could call a SUB/Macro from inside the Mcode . THAT would make it very simple to create Mcode functions based on Sub/Macros code.

In mach3(yes I know) I had Mcodes for every little special function. Take 5/16x24 tapping. I have one that loads the tap and does the cycle where ever you are. Yes I do a LOT of one off tapping functions. That just made it easy to do on the fly.

I tried it in MACH4 BUT it will run from the editor (RUN) just fine but has NEVER ran from MACH4 MDI or Gcode call

Just have to order another LARGE truck full of rocks.

(;-) TP

« Last Edit: May 27, 2014, 12:57:15 PM by BR549 »

Offline BR549

*
  •  6,916 6,916
    • View Profile
Re: MACH4 Mcode testing
« Reply #17 on: May 27, 2014, 05:57:34 PM »
Steve Just out of curiosity and maybe to save some time (;-). IS IT possible to call a SUB/Macro (M98 or G65) from inside a Mcode ???

(;-) TP

Offline BR549

*
  •  6,916 6,916
    • View Profile
Re: MACH4 Mcode testing
« Reply #18 on: May 27, 2014, 06:30:37 PM »
Ok simple step by step test, Here is the Mcode to run a SUB via M98..

The Mcode Runs AND the SUB(o5555) loads in Mach4  BUT it does not know to Cycle Start the sub to run. It just STOPS there and the Mcode ends (no error) IT does run teh last sequence of the Gocde line as it places the comment (StartMacro) in the comment line.   BUT it does not finish the next sequence in the Mcode.

Could the Mcode be fixed to RUN the SUB and then finish out the Mcode code?  If so "THAT" would be the CAT's MEOW

function m222()
    local inst = mc.mcGetInstance();
    mc.mcCntlSetLastError(inst, "m101");
  
    mc.mcCntlGcodeExecuteWait(inst, "M98 P5555 L1 \n (StartMacro) ");
      
    mc.mcCntlSetLastError(inst, "M222 Completed " );
    
end

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








« Last Edit: May 27, 2014, 06:46:39 PM by BR549 »

Offline smurph

*
  • *
  •  1,155 1,155
  • "That there... that's an RV."
    • View Profile
Re: MACH4 Mcode testing
« Reply #19 on: May 27, 2014, 06:59:11 PM »
I don't know Terry.  It was never a design goal to have M codes run subs.  But it is probably possible. 

The build that you have has an issue where IF the G code that is passed to mc.mcCntlExecuteGcode() or the Wait variant DOES NOT produce any movement DIRECTLY, it will basically just return.  For example, one of your previous M codes was trying to do "T111 M6" on one line.  Well...  that line produced no direct movement of any sort.  M6 could have possibly produced movement, but only as a result of another interpreter firing up.  Thus it would have been indirect.  In any case, the only line that was processed was that "T111 M6" line and the rest was ignored.  So the same thing is going to happen with "M98 P5555 L1". 

I'm working on making the mc.mcCntlExecuteGcode() calls work in a different manner where they are not waiting on movement, per say, but rather the execution of the code to be completed.  It seems all just a play on semantics, but there is a difference.  And with this, it just may be able to run subs or G65s.  i don't know yet though.

Steve