Machsupport Forum

Mach Discussion => Mach4 General Discussion => Topic started by: Bill_O on March 04, 2019, 04:57:44 PM

Title: macro problem
Post by: Bill_O on March 04, 2019, 04:57:44 PM
I made a macro and saved it in the profile macros folder.
For some reason it is not working.
I also made a button that runs that m code.
It is not working either.
I actually think the problem is the macro because it will not run if i type m22 in the mdi either.
both are attached
Title: Re: macro problem
Post by: jbuehn on March 04, 2019, 07:34:28 PM
you need:

local inst = mc.mcGetInstance()

because you're passing that variable into mc.mcSignalGetHandle
Title: Re: macro problem
Post by: Bill_O on March 05, 2019, 01:43:13 PM
jbuehn,

thanks

bill
Title: Re: macro problem
Post by: Bill_O on March 05, 2019, 04:03:51 PM
when i try putting local inst = mc.mcGetInstance() the editor changes it to mc.mcCntlGetIntstanceHandle()
Title: Re: macro problem
Post by: jbuehn on March 05, 2019, 09:28:36 PM
when you start typing the mc.mc part you should get a drop down list that has mc.mcGetInstance() in it.

I'm guessing you had "Cntl" in there which is why it was selecting mc.mcCntGetInstanceHandle() and not just mc.mcGetInstance()
Title: Re: macro problem
Post by: Bill_O on March 06, 2019, 07:52:59 AM
jbuehn,
i did not put the cntl in it but i did find out how to make it not do the one i do not want.
if i type "mc.mcGetInstance" it only shows me the one with cntl and handle
if i type "mc.mcgetinstance" it brings up the one i want.
strange
bill
Title: Re: macro problem
Post by: Bill_O on March 06, 2019, 09:50:30 AM
still not functioning either when i push the button or type m22 in the mdi
i have attached them again.
once again i know i am missing something simple
i saved the m22 in the profiles macro folder as m22.mcs
do i need to do anything else?
Title: Re: macro problem
Post by: Cbyrdtopper on March 06, 2019, 11:39:36 AM
Running the m22 macro, I am able to debug it and run it on MDI.
I'm not sure what issue you are having with the Macro, it runs fine for me.

The button press however, instead of using GCodeExecuteWait, use MdiExecute
Also, for best practice, keep the "m" lower case, you had it upper case. 
--New button script.
local inst = mc.mcGetInstance()
local N2Ac = "m22"
--mc.mcCntlGcodeExecuteWait(inst, N2Ac)
mc.mcCntlMdiExecute(inst, N2Ac)
Title: Re: macro problem
Post by: joeaverage on March 06, 2019, 11:57:53 AM
Hi,
you are confusing a macro and a button script. Try writing them a two distinctly different pieces of code.

Next thing is don't call it m22(). All your macros should be named m100() or higher, avoiding m162 and m163
as they are also used by Mach for Laser Operations.


Try saving this as your macro, note that I renamed it m220():
Code: [Select]
function m220()
local inst=mc.mcGetInstance()
local sigHandle=mc.mcSignalGetHandle(inst,mc.OSIG_OUTPUT2)
mc.mcSignalSetState(sigHandle,1)
end
if (mc.mcInEditor()==1) then
m220()
end

For the button script use this:
Code: [Select]
local inst=mc.mcGetInstance()
local sigHandle=mc.mcSignalGetHandle(inst,mc.OSIG_OUTPUT2)
mc.mcSignalSetState(sigHandle,1)

You could as Chas has suggested call the macro with the mcCntlMDIExecute() API. When you press the button
the GUI chunk is running. Its actionis to execeute a macro which is in a different chunk, ie Mach has to switch from
one chunk to another. It can and does this all the time, but in this case is unnecessary, just repeat the functional code
in the button script and it will be compiled and contained within the GUI chunk and have no need of a context switch
to execute.
Title: Re: macro problem
Post by: Cbyrdtopper on March 06, 2019, 12:18:33 PM
Craig, I agree with you, I would much prefer having the logic in the button script as well.  May be a little redundant on the work, but it makes it bulletproof.
Title: Re: macro problem
Post by: Bill_O on March 06, 2019, 12:25:37 PM
Craig,

I will fix the M22 and make it M220.
I thought I looked at which Mcodes could be scripted and that was I thought in that group.
I was just using the button script to call M22.
Because you told me in the other thread on here that the code is sometimes different I will stop trying to use that for testing and just type in the MDI to test the M220 macro.

Thanks again,

Bill
Title: Re: macro problem
Post by: joeaverage on March 06, 2019, 12:42:19 PM
Hi,
you need to get into the habit of calling macros by their lowercase names, m220() not M220().
It will throw up errors from time to time which can be very hard to understand.

In Mach3 all M codes above M100 were considered user codes whereas any below that number were
reserved for Mach. I suspect m22() would work (in Mach4) but my preference is name it in such a manner that
there is no ambiguity.

Quote
Because you told me in the other thread on here that the code is sometimes different I will stop trying to use that for testing and just type in the MDI to test the M220 macro.

It not so much that the code is different but rather it is in two different locations. As Mach runs it is going from the GUI
chunk to the GCode interpreter chunk and back again all the time. What I have suggested is that if you want some particular
machine behavior that is the same in both chunks then code it in two places. With such simple code this is easy. If
the code were complex it would be best placed in a module. The module could then be accessed by either the GUI OR
the GCode interpreter but the code would not have to be repeated in both chunks.

Craig