Hello Guest it is November 21, 2019, 05:12:59 PM

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

0 Members and 1 Guest are viewing this topic.

Re: Lua script detect when the cycle is running
« Reply #20 on: February 16, 2018, 03:17:04 PM »
Hi,
that can be done.

Note that the one thing which determines if your M6 has been stopped or the machine disabled is the state of the control. If the m6 is progressing normally
the control state is busy and you wish to leave it alone. If however it has been stopped then the control state would be Idle and you wish to RETURN.

How about this:

if (mc.mcCntlGetState(inst)==mc.MC_STATE_IDLE) then return end

So if the control is in Idle state, and we know it shouldn't be if every thing is normal, then cause the macro to return immediately.

Craig
My wife left with my best friend...
     and I miss him!
Re: Lua script detect when the cycle is running
« Reply #21 on: February 18, 2018, 12:59:37 PM »
Thank you for your time. Here's what I ended up doing:

A function to ask if the machine hasn't been disabled or stopped, used each time there is a signal change or any other physical or Mach-related change, except some specific cases, more on them later.

Code: [Select]
function toolchange_machine_enabled()
    local inst = mc.mcGetInstance()
    local mcState = mc.mcCntlGetState(inst)
    if mcState==mc.MC_STATE_IDLE then
        return false
    else
        return true
    end
end

An example of its usage:

Code: [Select]
function toolchange_boringhead_motorOFF()
    if toolchange_machine_enabled()==true then
        local inst = mc.mcGetInstance()
        mc.mcSignalSetState(mc.mcSignalGetHandle(inst, mc.OSIG_OUTPUT4), false)
    end
end

The reason I didn't return out of the script if this function returned false, is that G-code states (like whether G90 or G91 is currently active) get recorded at the beginning of my M6 script, and have to be returned to afterwards. The script is complex enough that is actually has its own G-code safe start line similair to one in a G-code program. This is also the specific case during which I don't check if the machine hasn't been stopped - I need the machine returned to the previous state regardless of what happens in the meantime.

For safety regarding mc.mcCntlGcodeExecuteWait(), I made this function that I replaced every direct call of mc.mcCntlGcodeExecuteWait() with:

Code: [Select]
function toolchange_callGcode(gcode)
    local inst = mc.mcGetInstance()
    local rc = mc.mcCntlGcodeExecuteWait(inst, gcode)
    if rc ~=0 then
        mc.mcCntlEnable(inst, false)
        mc.mcCntlSetLastError(inst, "Something went wrong with executing G-code from within M6. The machine was disabled for safety. Error state: "..rc)
    end
end

Knowing the rest of my code can handle the machine stopping, I stop the machine directly from this function, which also saves me from having to track another error flag.


I also found out that if you call a custom M-script from within another custom M-script, Mach freezes, not taking any G-code anymore, yet not stating that there was an error, jogging also retains its function. Consider I was using Mach4 version 4.2.0.2872, and newer versions may not have this issue.