Hello Guest it is August 19, 2019, 08:38:04 AM

Author Topic: Lua script problems  (Read 73 times)

0 Members and 1 Guest are viewing this topic.

Lua script problems
« on: August 16, 2019, 05:20:41 AM »
I am using a wirless transmitter to turn on and off the dust exreaction system I need it to turn on the output 50 for about half a second then turn it off  then the same again to stop the dust extraction so I need a pulse on output50
I use a delay function calles wait in MC6 tool changer and though I could call that from here but no if I include the wait function in this code I get the same error even if I call it some thing different it still has the same error
This is the error

C:\Mach4Hobby\ZeroBraneStudio\bin\lua53.exe: C:\Users\Raymondo\AppData\Local\Temp\leA8EB.mcs:14: attempt to call a nil value (global 'wait')
stack traceback:
   C:\Users\Raymondo\AppData\Local\Temp\leA8EB.mcs:14: in main chunk

Here is the script

-- Dust Collector button script
-- M110/M111 Output 50 signal 1100
inst = mc.mcGetInstance()

local hSig, rc = mc.mcSignalGetHandle(inst, mc.OSIG_OUTPUT50)
 
    mc.mcSignalSetState(hSig, 1)

 wait(1)
     mc.mcSignalSetState(hSig, 0)

And I added this  with or without it it is the same but this in in MC6 and works fine in MC6

function wait(seconds)
  local start = os.time()
  repeat until os.time() > start + seconds
end

 
any idea I just need about .3 to .5 seconds delay between turning  on output50 and turning it off
regards Ray
Re: Lua script problems
« Reply #1 on: August 16, 2019, 11:56:26 AM »
in the new version you cant use same function name in 2 M function
or use public main or change the name in each M function

Offline smurph

*
  • *
  •  1,117 1,117
  • "That there... that's an RV."
    • View Profile
Re: Lua script problems
« Reply #2 on: August 16, 2019, 12:48:18 PM »
wait(1) is the main problem.  At least that is what the error log said.  In order to use it, it must be defined ABOVE the code from which you want to call it.  It looks like you were nesting that wait function inside another function?  Possibly the m110() function? 

This should work:
Code: [Select]
-- Dust Collector button script
-- M110/M111 Output 50 signal 1100

function wait(seconds)
  local start = os.time()
  repeat until os.time() > start + seconds
end

function m110()
    inst = mc.mcGetInstance()
    local hSig, rc = mc.mcSignalGetHandle(inst, mc.OSIG_OUTPUT50)
 
    mc.mcSignalSetState(hSig, 1)
   
    wait(1)
    mc.mcSignalSetState(hSig, 0)
end --end function m110

Also, you may run into a even more hard to deal with problem if several macro scripts define a wait() function.  Only the LAST loaded wait() function will be used!  Now which one will it be?  The answer is "You don't know!"

The best way to use common functions that are available to ALL macro scripts is by using modules. 

But you don't even need to define the wait() function or use modules for this.  Fortunately, we have LUA with wxWidgets bindings and you can use its' wx.wxMilliSleep() function.  If you want to wait 1 second, then do wx.wxMilliSleep(1000).  Half a second is 500, etc...

Steve
Re: Lua script problems
« Reply #3 on: August 16, 2019, 11:40:35 PM »
Thank you
the wx.wxMilliSleep() command did ever thing I wanted and I now have it running perfect again
I was using the wait function and it was nested inside another function M6.mcs  but I will alter that get rid of the wait function and use wx.wxMilliSleep() instead
Thanks again for the help you guys are the legends
Regards Ray