Search code examples
luamach

Attempt to call global "Sicherheitskreis" (a nil value ) stack traceback


I'm trying to secure the doors of my CNC with switchs that send a Signal to my Laptop (I got Mach 4 on it). I created this Code that should make the Spindle stay still if the doors aren't locked, but I always get an error that says :

[string""]1576 attempt to call global 'SicherheitsKreis' (a Nil value) stack traceback:

I've tried to move the Code around and read topics on this, but Nothing works. Does someone have a solution ? Here's all my Code :

function SicherheitsKreis(Schliesserstate, Oeffnerstate)
   if (Schliesserstate ==0 and Oeffnerstate ==1 ) then 
     mc.mcSpindleSetDirection(inst,0)
   elseif (Schliesserstate == 1 and Oeffnerstate == 0 ) then 
     local sigh = mc.mcSignalGetHandle(inst, mc.OSIG_SPINDLEON);
     local sigState = mc.mcSignalGet State(sigh);
     if (sigState == 1) then 
       mc.mcSpindleSetDirection(inst,0)
     else 
       mc.mcSpindleSetDirection(inst,1);
     end
   else 
     mc.mcSpindleSetDirection(inst,0)
     end
end 

if (mc.mcInEditor() == 1) then
    SicherheitsKreis()
end

The Code that I'm using to call SicherheitsKreis is :

local inst = mc.mcGetInstance()
local hsigSchliesser = mc.mcSignalGetHandle(inst, mc.ISIG_INPUT8);
local hsigOeffner = mc.mcSignalGetHandle (inst, mc.ISIG_INPUT9);
local Schliesserstate = mc.mcSignalGetState(hsigSchliesser);
local Oeffnerstate = mc.mcSignalGetState(hsigSchliesser); 
 SicherheitsKreis(Schliesserstate, Oeffnerstate)

This Script is typed in Mach 4 and the function is saved as a m function (nach4 has free m function that the user can customize) in the Memory of Mach 4 (for my Computer it is m146)


Solution

  • Your code:

    function SicherheitsKreis(Schliesserstate, Oeffnerstate)
    ...
    end
    

    First possibility is that code is in some other function or other element, so it is not global. Second possibility is that you run SicherheitsKreis(Schliesserstate, Oeffnerstate) before loading this part of code. Third (very uncommon) is that you override it by SicherheitsKreis = nil or equivalent. There is no other possibilities.