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.