Hello Guest it is March 28, 2024, 02:07:40 PM

Author Topic: call macro from another macro  (Read 6316 times)

0 Members and 1 Guest are viewing this topic.

call macro from another macro
« on: September 27, 2010, 12:14:49 PM »
Hi,

I'm guessing this is possible; can I call a macro from inside another macro, maybe via some conditional statement or other? Something along the lines of "If A=0 then M06"?

Thanks, Richard B

Offline Hood

*
  •  25,835 25,835
  • Carnoustie, Scotland
    • View Profile
Re: call macro from another macro
« Reply #1 on: September 27, 2010, 12:17:23 PM »
Dont think you can but will be interested if you can :) Ray or Dave I am sure will know so hopefully they will chime in.
Hood
Re: call macro from another macro
« Reply #2 on: September 27, 2010, 01:19:24 PM »
Then we await their presence with great anticipation! Or something...

Rich B

Offline Hood

*
  •  25,835 25,835
  • Carnoustie, Scotland
    • View Profile
Re: call macro from another macro
« Reply #3 on: September 27, 2010, 02:56:46 PM »
Ha ha yep something like that.
Hood
Re: call macro from another macro
« Reply #4 on: September 28, 2010, 09:02:49 AM »
You can call macros from with in other macros:  Even in different files.

Your statement would need a code "M06" however.

This is how you can pass parameters to different files.  NOTE:  the macro in different file will run at the same time, Mach3 just spawns a new thread (at least this is true from my testing)

    Passing Parameters 101
        FILE 1
            Code "M1101 P99 Q23 R5001"
       
        FILE 2
            parameter1 = Param1()
            parameter2 = Param2()
            parameter3 = Param3()
   
            Message "M1101: " & parameter1 & " " & parameter2 & " " & parameter3


Thanks,
JH
Re: call macro from another macro
« Reply #5 on: September 28, 2010, 09:13:01 AM »
Hi JHC,

Thanks! Good to know it can be done.

Having a bit of trouble following your code. It's for passing a parameter from 1 macro to another?
Is that the same as calling a macro form within another macro?

Would you mind maybe posting an example llne of code which would call a macro?
Not sure where MO6 fits here, sorry for the ignorance!

Rich B
Re: call macro from another macro
« Reply #6 on: September 28, 2010, 09:54:58 AM »
You can call one macro from another, but there are a number of pitfalls.  In particular, you cannot assume the first macro will resume execution when the second one completes - it won't.  The second macro is spawned in a new thread, so the two end up running asynchronously, and concurrently.  This can be circumvented by use of a semaphore that causes the first macro to wait:

M1000.m1s:

SetVar(1000, 0)  ' Clear sempahore
... Do something here
Semaphore = GetVar(1000)   ' Set semaphore
Semaphore = Semaphore + 1
SetVar (1000, Semaphore)
Code "M1001"   ' Call M1001 macro
Semaphore = GetVar(1000)   ' Wait for M1001 macro to complete
While Semaphore > 0
    Semaphore = GetVar(1000)
Wend
... Do something here

M1001.m1s:

... Do something here
Semaphore = GetVar(1000)   ' Reset semaphore
Semaphore = Semaphore - 1
SetVar (1000, Semaphore)
Return

Using this technique, you can nest macros safely to pretty much any depth.

Regards,
Ray L.
Regards,
Ray L.
Re: call macro from another macro
« Reply #7 on: September 28, 2010, 12:30:45 PM »
Thanks Ray,

That's excellent in depth stuff, just what I was looking for.

Would it make a difference if a macro made a call to a macropump, that just happened to be idle at that particular point in the execution of a gcode file? Macropumps apparently get called automatically every 0.2 sec, but there may be a situation where the macropump's code would be useful in certain situations between those times. An alternative might be to add the macropump's code as a subroutine within the macro itself, I guess. 

Incidentally, I also posted on the same day on the subject of DTHC, asking if anyone could direct me to some example code, but it seems it's not generated much interest. Would you take a look and tell me if you think my post is unclear, too hard to answer, or too easy (ie I should be able to find it myself?) or am I just being impatient?! I'll happily rephrase it if it doesn't make sense, or wait, or whatever.

Anyway, thanks for the advice, it's good to know there are people out there who know Mach well enough to answer questions like that

Rich B
Re: call macro from another macro
« Reply #8 on: September 28, 2010, 02:04:53 PM »
Hi,
I figured I'd throw in an extra 2 cents worth....

Yes M codes can call other M codes - many people find that the resulting actions are not quite what they assumed. This is because there are some synchronization considerations you have to worry about (as Ray pointed out).

Also realize that since Mcodes are just files that contain mach scripts, the script language tools that you have available vary  depending on the version of Mach that you are running.

With later versions of mach, the synchronization considerations can be easier - as there are more tools to help the script writer.
In particular, the 3.43.x development mach versions have programming tools which are not avail in the 3.42.40 lock down release.

To learn about these you go get the current mach programmers ref manual
http://machsupport.com/docs/Mach3_V3.x_Macro_Prog_Ref.pdf

Some key things to read about are:
3.43.x supports named scripts that live on disk. Read about #expand to see where mach looks for named scripts.
For me, this effectively let me eliminate all Mcode to Mcode calling. I only use Mcodes as a link from Gcode to get a script invoked.

Some things to read about for more info:

RunScript()   - this has the nice property that it does not return until the called script is finished.

StartperiodicScript() and StopPeriodicScript()  - these calls allow you to start and stop periodic scripts, and allow you to set the run period - this can be much more flexible than the older MacroPump which has only a fixed period.

Dave

Author of the MachStdMill Extensions for Mach3
www.CalypsoVentures.com