Hello Guest it is March 28, 2024, 05:59:24 PM

Author Topic: using loops inside macros  (Read 1017 times)

0 Members and 1 Guest are viewing this topic.

using loops inside macros
« on: February 10, 2020, 02:39:53 PM »
Good day people,

I'm doing some custom macros for my cnc machine, everything is working fine but I'm having a problem with "FOR" loops when hitting the "STOP" button.
The thing is I have an M73 file wich performs a pattern for punching, in sumary it looks something like this:

for Y=1,Y lines,1
    moves to the next Y coords.
        for X=1, X columns,1
           moves to the next X coords
           external function for punching
        end
end

the problem here is when I need to press the stop button for any reason, the movement stops but the machine continues punching until the end of FOR loops.
I tried using a BREAK statement when the signal Gcode_Running turns off but nothing happens.
Any idea of how to properly break the loops inside a macro?

Regards.
Re: using loops inside macros
« Reply #1 on: February 11, 2020, 11:34:38 PM »
The way I've done this in the past is create a gReg. Goto Configure -> Plugins -> RegFile and create register. Then in your stop button script set the register to a value. In any for loops in your MScript listen for changes in the gReg value and use it to completely exit the MScript. You can also set the gReg value in the sigLib, for example, to exit for loop if EStop is pressed.
Re: using loops inside macros
« Reply #2 on: February 12, 2020, 05:51:02 PM »
The way I've done this in the past is create a gReg.

Thanks for the reply, it "worked", but some times the controller lost connection when breaking the loops what could be anoying in large pieces, is there any way to implement coroutines inside macro files in order to "pause" the loops instead of breaking it and maybe I would be able to resume the loops instead of repeating them from the begining?

regards.

Offline Graham Waterworth

*
  • *
  •  2,668 2,668
  • Yorkshire Dales, England
    • View Profile
Re: using loops inside macros
« Reply #3 on: February 12, 2020, 06:35:43 PM »
Why not do the loops in the gcode using sub programs.

(Main)
G00 G90 X0 Y0
M98 P1 L10
M30

O1 (SUB 1 - Y MOVES)
M98 P2 L5
G00 G90 X0
G91 Y-25.
G90
M99

O2 (SUB 2 - X MOVES)
M5678 (CALL EXTERNAL DEVICE)
G91 X-20.
G90
M99

Without engineers the world stops
Re: using loops inside macros
« Reply #4 on: February 12, 2020, 06:57:17 PM »
I don't know how to use subprograms to replace the Lua macros I did to calculate the path for the machine and the control of the outputs.
Re: using loops inside macros
« Reply #5 on: February 12, 2020, 07:13:32 PM »
If you know you want to exit the Macro when you hit stop or estop then just 'return' out of the Macro instead of just breaking out of the for loop. The Macro is just a function and you can get out of the function with a return statement.

Sounds like you'll have to debug the exit condition if the return doesn't work. You can use global registers for debugging the Macro too. You should be using the return codes in the Mach API to handle errors as well.
« Last Edit: February 12, 2020, 07:19:39 PM by compewter_numerical »