Machsupport Forum

Mach Discussion => Mach4 General Discussion => Topic started by: Bx3mE on December 16, 2016, 10:43:13 AM

Title: Can i edit or add to a Predefined Macro?
Post by: Bx3mE on December 16, 2016, 10:43:13 AM
I want to ADD stuff to the M30 Macro. I want it to be working as it works by default but add some code to finish up a GCode run.

All my Cam software end programs with move Tool to Z clearance plane, turn spindle off and (M30) stop execution and set next line marker to the first line.
After this i want to do some more stuff like turn on the coffe mashine to make a nice cup of coffe so i can admire my work in peace and also send an email to my phone telling me coffe is ready in 3 minutes.... :D

How and or where do i do this?
Title: Re: Can i edit or add to a Predefined Macro?
Post by: DazTheGas on December 16, 2016, 11:09:31 AM
Prob best to create another custom m code and then edit your post processor to use that instead of m30

Well thats what I did

DazTheGas
Title: Re: Can i edit or add to a Predefined Macro?
Post by: Bx3mE on December 16, 2016, 11:54:26 AM
One way i thought of was to Create a signal script:

If cycle_start_has_been_detected and signal "sig_program_end" is detected then cycle_start_has_been_detected = 0 AND EXEC(Custom M Code)


Would that work or would it create problems?
Title: Re: Can i edit or add to a Predefined Macro?
Post by: smurph on January 03, 2017, 11:50:16 PM
You CAN create a M30 macro.  If you do, it will REPLACE the stock M30 functions.  So when you write it, you will need to put any of the original functionality of the stock M30 into the custom M30 macro. 

The stock actions for M30 in mill are:

1. Stop the spindle.
2. Turn off cutter comp.
3. Turn off mist.
4. Turn off flood.
5. End processing (Cycle Stop).
6. Rewind.

Note: #5 and #6 are preformed even if a custom M30 macro exists.  The entire custom M30 macro will be executed and then #5, followed by #6, will be executed. 

Code: [Select]
function m30()
local inst = mc.mcGetInstance()
mc.mcCntlSetLastError(inst, "This is my m30 macro")
end

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

If you used that file, when G code calls M30, it will simply print they message, end processing, rewind, and do NOTHING else.

So, to recap: 
1.  If the m30.mcs macro exists in the macros directory, it will be used in lieu of the stock M30 actions.
2.  If a custom M30 macro is used, it must include all of the functionality you wish as none of the stock M30 actions will be performed otherwise except for Cycle Stop and Rewind.

Steve
Title: Re: Can i edit or add to a Predefined Macro?
Post by: Bx3mE on January 04, 2017, 08:59:32 AM
Thank You So it is as easy as writing a M6 Macro :)
Title: Re: Can i edit or add to a Predefined Macro?
Post by: Chaoticone on January 04, 2017, 09:20:28 AM
Thank You So it is as easy as writing a M6 Macro :)


Yup, maybe even easier.  :)

Remember, the macro file name and functions inside the macro should all use a lower case m.......... m30 not M30
Title: Re: Can i edit or add to a Predefined Macro?
Post by: joeaverage on January 26, 2017, 04:01:32 PM
Hi,
I need to customise m03,m04 and m05.

I have written the macros and they run ok in the debugger. If I call them from the MDI line the Mach4 internal functions run and
ignore my customised macros.

Code: [Select]
function m03();
local inst=mc.mcGetInstance();
mc.mcCntlSetLastError(inst,"My m03");
local hcont=mc.mcRegGetHandle(inst,"modbus0/spinCntrl");
mc.mcSpindleSetDirection(inst,1);
mc.mcRegSetValue(hcont,10);
end
if (mc.mcInEditor() == 1) then
    m03()
end

As you can see I'm trying to write to a Modbus register in addition to the normal function. I set the 'Last Error' message just to indicate
whether my script runs or not.

Craig
Title: Re: Can i edit or add to a Predefined Macro?
Post by: Chaoticone on January 26, 2017, 05:05:28 PM
Have you tried naming it m3?

Got a m3 or m03.mcc or .backup thats needs to be deleted?
Title: Re: Can i edit or add to a Predefined Macro?
Post by: joeaverage on January 26, 2017, 05:50:16 PM
Hi,
thanks, naming m3 worked.

I'm guessing that I'm now calling a distinct macro (m3) whereas the internal function is m03. This is a workaround rather
than a solution, or rather an understanding of whats going on. I'll take whatever I can get!

Thanks again,
Craig
Title: Re: Can i edit or add to a Predefined Macro?
Post by: smurph on January 26, 2017, 06:18:38 PM
The macros (and functions inside them) should be lower case and leading zeros dropped.  m03, M03, m3, or M3 is accepted by the interpreter, but it gets low cased and leading zeros dropped shortly there after.  Windows is not case sensitive but LUA is.  Everything ought to be case sensitive.  Everything on the planet, except Windows, IS!!!! 

If you write a custom m3 macro, it replaces the stock actions of m3.  So you must provide all functionality you desire in the custom m3 macro.  See the doc on mc.mcSpindleSetDirectio().

Steve
Title: Re: Can i edit or add to a Predefined Macro?
Post by: joeaverage on January 26, 2017, 06:23:06 PM
Hi smurph,
thanks for the explanation. What would happen if a cam program called m03.Does the leading zero get stripped in that
instance as well?

Craig
Title: Re: Can i edit or add to a Predefined Macro?
Post by: joeaverage on January 26, 2017, 06:45:05 PM
Hi,
just read thru (again) the mcSpindleSetDirection() doc.

I tried using the ENUM mc.MC_SPINDLDE_FWD and that works fine. I take it that using the ENUM is considered
better programming practice?

I notice also that the call has a return code.

I have coded:
local rc=mc.mcSpindleSetDirection(inst,mc.MC_SPINDLE_FWD)

I am not testing rc and so I made it local that it may be garbarge collected soonest.

I have at other times coded:
mc.mcSpindleSetDirection(inst,mc.MC_SPINDLE_FWD)

It works fine too but I'm unsure what happens to the return code, does it get ignored and therefore not
require garbage collection? Is there a preferred method for dealing with unspecified return values?

Craig
Title: Re: Can i edit or add to a Predefined Macro?
Post by: smurph on January 26, 2017, 07:40:30 PM
The G code interpreter will take M03, m03, M3, or m3 and turn them all into m3.  So when looking for a macro, it will use m3.  Much simpler than looking for all 4 variations.

Using the constants is a very good idea.  Because if we change the value of the constant, it will not require you to change your code.  :)

The return code is simply discarded and garbage collected if it is not used.  However, I strongly suggest checking for error codes!  Not only is it a good programming practice, it could have bearing on whether or not you loose a finger or not!  Yikes!  In truth, it is not necessary all of the time.  But if you get into the habit, it is going to pay dividends in the future for sure.

All of the API functions have their possible return codes documented and I'd say that the majority are correct.  In the event that we messed up, I prefer to test for inverse success.  e.g.

if (mc.mcSpindleSetDirection(inst, mc.MC_SPINDLE_FWD) ~= mc.MERROR_NOERROR) then
   --handle any failure!
end

Steve
Title: Re: Can i edit or add to a Predefined Macro?
Post by: joeaverage on January 26, 2017, 08:01:14 PM
Hi Steve,
thanks for the explanation, something I seem to say to you a lot!

Craig