Hello Guest it is July 12, 2020, 03:46:20 PM

Author Topic: Lua script detect when the cycle is running  (Read 3213 times)

0 Members and 1 Guest are viewing this topic.

Re: Lua script detect when the cycle is running
« Reply #10 on: February 15, 2018, 04:15:33 PM »
Hi,

http://www.machsupport.com/forum/index.php/topic,36548.0.html

No, what I am saying is that IF you try to execute code when the control is NOT idle then you'll
crash Mach.

What you are asking is: 'how did the control become idle...did the previous code execute and exit normally...
did it get aborted or did it fail to execute?'

I can see that you may not wish to execute your code if the previous code ended abonrmally, this test does'nt
help it that decision. It does tell if you 'could' execute your code.

Craig
My wife left with my best friend...
     and I miss him!
Re: Lua script detect when the cycle is running
« Reply #11 on: February 16, 2018, 09:50:46 AM »
I'm not talking about separate scripts, but rather a single M-script and code within it. There are 2 ways this can work out:

1. M6 gets called by either MDI or G-code and nothing interrupts it. The execution starts at the top of the script and ends at the bottom, with nothing left out in between.

2. M6 gets called by either MDI or G-code but the machine is stopped or disabled halfway through by the operator. The execution starts at the top, YET STILL ENDS AT THE BOTTOM, since Mach actually doesn't stop executing until the whole thing has been gone through. AND, G-code calls won't do anything, because they actually seem to check if the machine is enabled and not stopped. This means that some things will be left out, yet others (like signal changes) won't be. This is an obvious problem since no-one wants to have a script execute only partially with some things arbitrarily left out.

What I wanted to do was simply make everything else in my M6 script also check if the machine was enabled and not stopped, so after the latter, nothing inside M6 would actually be executed. I'm sorry if I wasn't clear on this. Your solution is still appliccable, though.

Offline DazTheGas

*
  •  752 752
  • DazTheGas
    • View Profile
Re: Lua script detect when the cycle is running
« Reply #12 on: February 16, 2018, 01:20:31 PM »
May I suggest that you actualy post your m6 code, its very hard to give advice with only partly knowing what you are doing, you never know there may be a completely different solution to what you need.

DazTheGas
Theres always time for a choccy biccy
Re: Lua script detect when the cycle is running
« Reply #13 on: February 16, 2018, 02:10:47 PM »
https://pastebin.com/QcUSfu7E

Here you go. This one still has a lot of print() uses instead of mc.mcCntlSetLastError(), shouldn't really matter though.
Re: Lua script detect when the cycle is running
« Reply #14 on: February 16, 2018, 02:12:17 PM »
Hi,
I'm with Daz and am unsure that I understand the problem you are describing.

Quote
but the machine is stopped or disabled halfway through by the operator. The execution starts at the top, YET STILL ENDS AT THE BOTTOM, since Mach actually doesn't stop executing until the whole thing has been gone through.

I'm not sure about this....surely the M6 starts at the top but the code stops at the point that you disable or stop it? The only way I can think that the code would continue
to execute but not actually physically move would be if the motors/servos were disabled while Mach remained enabled and therefore carried on executing?.

You may have noted a current and fairly long running thread where a guy wanted an 'Offline' button on his machine. The solution was to disable the physical motion of
the servos while allowing normal operation of Mach. When the machine is to be re-instated it is necessary to apply a machine coordinate correction so the machines
referencing remains consistant. Eventually I got the code to work pretty well, regrettably the OP is being rather obtuse and refuses to follow the steps required to get it
to work on his machine but it works fine here.

With the solution I proposed for his "Offline' button the servos stopped motion but if a signal changed state as a result of Mach's continued execution while the servos
are offline that could cause problems. Is this the concern you have?

Craig
My wife left with my best friend...
     and I miss him!
Re: Lua script detect when the cycle is running
« Reply #15 on: February 16, 2018, 02:20:01 PM »
It doesn't stop. Maybe I've made a rookie mistake somewhere along the line though, that causes it to not stop properly.

Nevertheless, I've tested it, and you can too. Try this M-script. if you press either disable or stop while G4 is delaying (any other G-codes that take up time also work), you still see the prints near the end.

function m401()
    local inst = mc.mcGetInstance()
    print("starting")
    mc.mcCntlGcodeExecuteWait(inst,"G4 P3")
    local mcState,rc=mc.mcCntlGetState(inst)
    if (mcState==mc.MC_STATE_IDLE) then
        print("IDLE")
    else
        print("ELSE")
    end
    print("end")    
end
Re: Lua script detect when the cycle is running
« Reply #16 on: February 16, 2018, 02:42:21 PM »
Hi,
I had to modify your script slightly to get it to run, my installation wont do anything useful with 'print()'
Also your  G4 P3 statement is interpreted by my machine as 'pause for 3 milliseconds', if you put a decimal point in the P parameter it is interpreted as seconds,
as you see I changed mine to P10.0.

Code: [Select]
function m401()
    local inst = mc.mcGetInstance()
    mc.mcCntlSetLastError(inst,"starting")
    mc.mcCntlGcodeExecuteWait(inst,"G4 P10.0")
    local mcState,rc=mc.mcCntlGetState(inst)
    if (mcState==mc.MC_STATE_IDLE) then
        mc.mcCntlSetLastError(inst,"IDLE")
    else
        mc.mcCntlSetLastError(inst,"ELSE")
    end
    mc.mcCntlSetLastError(inst,"end")   
end
if (mc.mcInEditor()==1) then
    m401()
end
My wife left with my best friend...
     and I miss him!
Re: Lua script detect when the cycle is running
« Reply #17 on: February 16, 2018, 02:54:38 PM »
Hi,
whoops posted before I'd actually finished. The code runs fine.
If I leave it to time out its delay it completes with the 'end' message and if I look at the error log it is preceeded with 'else' as it should be. Note that because
m401 is running the control can't be Idle, we happen to know that its busy running our script!
If however I either <Stop> or <Disable> m401 while the rather lengthy pause it running the script end and prints 'end' but doesn't print the 'else' which would
ordinarily preceed it. That is to say it skipped over the mc.mcCntlGetState() and the if-then-else-end code between the point at which I <stop> and the scripts
endpoint. Once the endpoint is reached m401 releases control and control reverts to the state which preceeded the m401 call namely Idle.

Craig
My wife left with my best friend...
     and I miss him!
Re: Lua script detect when the cycle is running
« Reply #18 on: February 16, 2018, 03:03:21 PM »
Hi,
I owe you an apology I misinterpreted what I saw.
When I <stop> the code while the delay is timing the error log shows that CycleStopped has been issued but then Lua is stuck in limbo and it carries on from
the point that it stopped, and you note that now because of the CycleStop the control state is IDLE, then it reaches the end.

Craig
My wife left with my best friend...
     and I miss him!
Re: Lua script detect when the cycle is running
« Reply #19 on: February 16, 2018, 03:04:26 PM »
Now, is it clear what the problem is? Prints happening like that are harmless. However, if you were to have a call to change a signal there instead, say for releasing a tool from the spindle, it becomes quite dangerous, doesn't it?
Checking if the machine hasn't been stopped each time I need a signal switched would solve the issue, and something to check for that is what I requested assistance with at the beginning.

FYI, I'm running Mach4 version 4.2.0.2872.