Machsupport Forum

Mach Discussion => Mach4 General Discussion => Topic started by: donb9261 on March 18, 2015, 02:10:25 PM

Title: Timers - Am I crazy
Post by: donb9261 on March 18, 2015, 02:10:25 PM
I am somewhat confused as to how to implement a timer in Lua.

For my lube cycle (way lube pump) I need an on time and off time. Once the off time is exceeded turn on outputxx. When on time exceeded turn off outputxx. I tried the wxMilliSleep but it locks down the control during the timing and since this is a timing loop the control continuosly loops and is locked from any functionality.

As a test I tried this and it does what I said above: (Inserted in the PLC)

ActivateSignalTime = 5000

if (machEnabled == 1) then
    runLube()
end   

function runLube()
    local inst = mc.mcGetInstance()
    hsig1,rc = mc.mcSignalGetHandle(inst, mc.OSIG_OUTPUT1)
    mc.mcSignalSetState(hsig1, 1)
    wx.wxMessageBox("Output 1 On")
   wx.wxMilliSleep(ActivateSignalTime)
   mc.mcSignalSetState(hsig1, 0)
   wx.wxMessageBox("Output 1 Off")
end

Although crude it does as stated above. Any ideas Scott?
Title: Re: Timers - Am I crazy
Post by: poppabear on March 19, 2015, 06:52:25 AM
Yes, since your in a PLC loop, just use a counter that accumulates up to 5000 then when it does fire off your output, then reset your counter to 0.

Scott

Title: Re: Timers - Am I crazy
Post by: Tony Bullard on November 17, 2016, 04:59:53 PM
I am somewhat confused as to how to implement a timer in Lua.

For my lube cycle (way lube pump) I need an on time and off time. Once the off time is exceeded turn on outputxx. When on time exceeded turn off outputxx. I tried the wxMilliSleep but it locks down the control during the timing and since this is a timing loop the control continuosly loops and is locked from any functionality.

As a test I tried this and it does what I said above: (Inserted in the PLC)

ActivateSignalTime = 5000

if (machEnabled == 1) then
    runLube()
end   

function runLube()
    local inst = mc.mcGetInstance()
    hsig1,rc = mc.mcSignalGetHandle(inst, mc.OSIG_OUTPUT1)
    mc.mcSignalSetState(hsig1, 1)
    wx.wxMessageBox("Output 1 On")
   wx.wxMilliSleep(ActivateSignalTime)
   mc.mcSignalSetState(hsig1, 0)
   wx.wxMessageBox("Output 1 Off")
end

Although crude it does as stated above. Any ideas Scott?

How could I exit the loop if another signal became active during the sleep time? Can this be used in an M code. I'm trying to wait for a THC to fire. Thanks for any help.