Hello Guest it is March 28, 2024, 04:50:22 AM

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

0 Members and 1 Guest are viewing this topic.

Lua script detect when the cycle is running
« on: February 12, 2018, 01:27:44 PM »
I have an M6 lua script set up to automatically do everything required to switch tools on a 2 spindle (main spindle + drill spindle) machine, and it has got to the point of being rather complex and therefore hard to debug and even harder to keep reliable.

There's one thing that 's been coming up recently, which is that, if the machine is disabled or the G-code cycle stopped during M6, the script just keeps on being executed, save the G-code calls from within the lua script through mc.mcCntlGcodeExecuteWait(). This results in the machine's movement stopping as expected, but then signals being set as if it wasn't disabled or stopped at all possibly causing tools to fall out of the spindle among others. I do understand that this is a requirement for some scripts, such as ones meant to act to turn off spindles after a disable command, but for M6 this is not the case.

I have somewhat managed to fix it by calling a function to check if the machine is enabled each time a signal change occurs in the code, so it only proceeds if the function returns true. The function is as follows:

function toolchange_machine_enabled()
    local inst = mc.mcGetInstance()
    local hsig=mc.mcSignalGetHandle(inst, mc.OSIG_MACHINE_ENABLED)
    local val=mc.mcSignalGetState(hsig)
    if val==1 then
        return true
    else
        return false
    end
end


(It's intentionally non-compact and readable.)
I'd like to hear from you suggestions to make it better so that it would return true only if mc.mcCntlGcodeExecuteWait() would execute. OR, if there's another way of solving the problem that is more correct, I'd be happy to hear on those.
Re: Lua script detect when the cycle is running
« Reply #1 on: February 13, 2018, 01:54:02 AM »
Hi,
I don't know if this would help:
Quote
LUA Syntax:
cycle, rc = mc.mcCntlIsInCycle(
      number mInst)

Also you may have to check the return code of GcodeExexcuteWait(). It will only execute if the control state is idle. There are occasions when it does not
execute even when the control is idle. Under those circumstances the return code is -18 or MERROR_NOT_NOW.

Just as a matter of interest do you check the control state before making the GCodeExecuteWait() call? I found if I didn't and the control is not idle then I would crash
Mach altogether!

Craig
'I enjoy sex at 73.....I live at 71 so its not too far to walk.'
Re: Lua script detect when the cycle is running
« Reply #2 on: February 13, 2018, 01:56:02 PM »
I don't check anything before calling mc.mcCntlGcodeExecuteWait(), Mach seems to be taking care of stopping any actions specified by it whenever required. I call my function before changing signals, as in outputs. However, now I'm thinking that maybe I should, if an error like you described could pop up.

Your example, while it seems to work for G-code files, doesn't work if ran under MDI (returns 1 if G-code file, 0 if MDI or machine disabled or stopped).

Here's what I used to test it - an M-code. I tested it by stopping or disabling the machine during the G4 time delay and comparing the results to occasions on which I let it complete.

function m400()
    local inst = mc.mcGetInstance()
    print("starting")
    mc.mcCntlGcodeExecuteWait(inst,"G4 P3")
    cycle, rc = mc.mcCntlIsInCycle(inst)
    print("cycle: "..cycle.." rc: "..rc)
    if (cycle == 1) then
        print("Cycle running")
    else
        print("Cycle not running")
    end
end
Re: Lua script detect when the cycle is running
« Reply #3 on: February 14, 2018, 01:06:06 AM »
Hi,
I think there is a difference between InCycle and MC_STATE_IDLE.

For any code  to produce motion requires the use of the motion planner. If for instance you want to run a Gcode file the sequence of events would be:
Is there a process or code currently using the Planner?
----If there is terminate the process/code
----If not the control will be idle and if idle the planner is available for use
Submit the Gcode job to the Planner

The code could be a Gcode file or an MDI command or a GcodeExecuteWait() API call or a screen script. In any case the control must be idle before a new stream
of motion instructions can be run.


When a Gcode file OR MDI command is run there must be a CycleStart command and thereafter the CntlIsInCycle() would return true. If a later CycleStop is executed
the CntlIsInCycle() would return false. What happens however if a FeedHold is encountered, is it still InCycle? Also what happen if it yields as part of a coroutine pair?
It is not clear to me that InCycle is the same as MC_STATE_IDLE

As I posted earlier I recently discovered that if I executed a GcodeExecuteWait() statement without first ensuring the control state is Idle then I could crash Mach bigtime.
As a consequence I put this line of code in:

if (mc.mcCntlGetState(inst) ~=0)then mc.mcCntlCycleStop(inst)end

Note that control state idle is numerically equivalent to 0. This statement checks that the control is idle and if it isn't issues a CycleStop to make it so.

The next issue I encountered is that even though the control is idle sometimes the GcodeExecuteWait() will not execute and will return MERROR_NOT_NOW.
If you were relying on the statement to make some move but for whatever reason did not move you could have a crash if you made some other move in expectation
that the previous move had been made. Thus I had this test:

local rc=mc.mcGcodeExecuteWait(inst,'g0......')
if rc==-18 then wx.wxMessageBox('GcodeExecuteWait() failed to complete') end

In fact my code became a little more sophisticated than that, if my GcodeExecuteWait() did not complete I would issue a MachDisable MachEnable pair and try again
for up to five times before the wxMessageBox error warning. This made my code pretty robust.

Craig
'I enjoy sex at 73.....I live at 71 so its not too far to walk.'
Re: Lua script detect when the cycle is running
« Reply #4 on: February 14, 2018, 07:07:35 PM »
So I decided to download mach4 to my travel computer and do some testing before I get home.  Here's what I did and what I found.  I added the following 2 lines to the standard M6.mcs file after the IF THEN ELSE END. I then moved the O9010 file from the subroutine sample directory into the subroutine directory and added an incremental move in it.
 
Code: [Select]
mc.mcCntlSetLastError(inst, "INSIDE MACRO M6")
mc.mcCntlGcodeExecuteWait(inst,"M98 P9010")
 

I created a simple file to run.

Code: [Select]
G90
M6
M30

So mach called the macro and the error printed.  It then DID make the incremental motion but it didn't seem to like it really waited like the command asks for .  I then reverse the process and called a macro from a subroutine and it seemed to work the same way.  I'll have to test this in more detail when I get home.
Re: Lua script detect when the cycle is running
« Reply #5 on: February 14, 2018, 10:18:25 PM »
Hi,
I was reading about subroutines and macros last night in Peter Smids book which is pretty much the standard
for both Fanuc and Mach4.

He doesn't call them subroutines or macros but calls all of them subprograms. Your bit of testing suggests they can be
used interchangeably as well.

Craig
'I enjoy sex at 73.....I live at 71 so its not too far to walk.'
Re: Lua script detect when the cycle is running
« Reply #6 on: February 14, 2018, 10:48:38 PM »
$h!t, I replied in the wrong post. Sorry.
Re: Lua script detect when the cycle is running
« Reply #7 on: February 15, 2018, 12:24:09 PM »
I think we might be getting a bit overcomplicated here. What I need would just be something to check so as to not execute a piece of code, if a Gcode or an MDI program isn't running. In other words, if the G-code or MDI program is stopped during executing an M-script, the script could know and halt itself. Checking mcCntlIsInCycle() works for G-code programs, but I couldn't find anything that would work for MDI.

The piece of code isn't related to the motion planner, rather changing the state of signals.

However, I'm really thankful that you mentioned GcodeExecuteWait() could return an error, which I had overlooked since it had never happened to me.
Re: Lua script detect when the cycle is running
« Reply #8 on: February 15, 2018, 12:56:48 PM »
Hi,

Quote
The piece of code isn't related to the motion planner, rather changing the state of signals.

No that's correct but if you check that the control state is Idle then that will determine that no code is running, Gcode or MDI or screenscript.

Quote
LUA Syntax:
mcState, rc = mc.mcCntlGetState(
      MINSTANCE mInst)

local mcState,rc=mc.mcGetState(inst)
if (mcState==mc.MC_STATE_IDLE) then
--
-- execute your code as the control is idle and ready for use
--
else
--
-- the control is not idle and not available to be tasked
-- if required you can terminate the current task
--
end

Craig

'I enjoy sex at 73.....I live at 71 so its not too far to walk.'
Re: Lua script detect when the cycle is running
« Reply #9 on: February 15, 2018, 02:07:07 PM »
Thanks, it seems to be suitable. Just to clear it up, for me it would be the opposite:

local mcState,rc=mc.mcCntlGetState(inst)
if (mcState==mc.MC_STATE_IDLE) then
    --
    -- Don't execute code, as the program was terminated (either the machine was disabled or the stop button was pressed) and the M-script should not proceed.
    --
else
    --
    -- The program is still running, and therefore it is safe to proceed execution
    --
    --
end


Now this might not be totally safe, as there may be other states under which code should not execute, yet which will take the 'else' route in this example. Is there a list of states and their descriptions? I couldn't find one in the docs or by using Google.