Hello Guest it is April 23, 2024, 05:54:01 PM

Author Topic: Pause Lua script mach4  (Read 3761 times)

0 Members and 1 Guest are viewing this topic.

Pause Lua script mach4
« on: August 20, 2019, 10:27:23 AM »
Hello People,

I am trying to figure out how to make a script pause/ wait before it continues. I don't want to stop the whole script but only the function where the timer is in. So the other functions will continue.

Does someone know how to achieve this in Lua? I want it the program to wait before a piston moves.

Thanks in advance.
Kind regards,

Jelle

Offline Bill_O

*
  •  563 563
    • View Profile
Re: Pause Lua script mach4
« Reply #1 on: August 20, 2019, 05:57:03 PM »
        wx.wxSleep(seconds)
   wx.wxMilliSleep(milliseconds)
Example:
   wx.wxSleep(1)
   wx.wxMilliSleep(1000)
Re: Pause Lua script mach4
« Reply #2 on: August 21, 2019, 06:22:03 AM »
Thanks for the reply. But doesn't this stop the whole program/script . So that it does not receive or process other data that is put in?

Offline Bill_O

*
  •  563 563
    • View Profile
Re: Pause Lua script mach4
« Reply #3 on: August 21, 2019, 07:39:49 AM »
Yes it does stop it all.
What exactly are you trying to do?
Re: Pause Lua script mach4
« Reply #4 on: August 21, 2019, 11:27:54 AM »
I want a function inside a script to pause if a input is true before it goes further and sets an output true . This function is for the M6 auto toolchange. So it will work if the sleep only pauses the m6 macro and not other lua scripts like plc script etc. Because there is only one tool to change in every toolchange .

So does sleep only stop the whole m6 script or all the scripts inside mach4?

Thanks in advance.

Offline Chaoticone

*
  • *
  •  5,624 5,624
  • Precision Chaos
    • View Profile
Re: Pause Lua script mach4
« Reply #5 on: August 21, 2019, 01:06:11 PM »
If I have said it once I have said it a thousand times............. sleeps should be avoided. If you must use sleep anywhere you probably need to rethink your code/strategy. I'm not saying using a sleep is always wrong but I am saying it is the wrong answer 99% of the time.

Why not use mcSignalWait? Wait on that input to go false. Check your rc and do whatever you want after. Continue running, throw an error, etc.

Sleep will pause every script in the chunk it is called from. In macros, that is everything in the macros directory and any dependencies (required modules for instance). In a screen it is all the screen scripts and any dependencies required by any of it.
« Last Edit: August 21, 2019, 01:14:57 PM by Chaoticone »
;D If you could see the things I have in my head, you would be laughing too. ;D

My guard dog is not what you need to worry about!
Re: Pause Lua script mach4
« Reply #6 on: August 21, 2019, 01:31:10 PM »
Thanks for the reply. I was afraid it would do that just like delay in Arduino. But I don't think I can use the wait function, because there is a inductive sensor on the end of the pneumatic cylinder so if the piston would extend it would directly be false and the pneumatic holder in the spindle would loosen and the toolholder would fall before the pneumatic cylinder could hold it.

 Do you have an idea because I don't have a sensor on the pneumatic cylinder when it is extended. I wanted to use a timer before the holder in the spindle would loosen to prevent the tool from dropping.
Re: Pause Lua script mach4
« Reply #7 on: August 21, 2019, 03:26:59 PM »
i use this:
function waiit()
   local inst = mc.mcGetInstance()
   local GCode =""
            GCode = GCode ..string.format("G4 p500")
            mc.mcCntlGcodeExecuteWait (inst, GCode) ---wait 0.5 second
   
end

Offline Chaoticone

*
  • *
  •  5,624 5,624
  • Precision Chaos
    • View Profile
Re: Pause Lua script mach4
« Reply #8 on: August 21, 2019, 03:29:59 PM »
Sounds like you need a conformation input if you are wanting it to wait until a cylinder is in a certain position. Inputs are what makes machines smart. Are there other ways? Absolutely but none of them are the best way to do it. Using any time based event to wait for the success of a logic driven event is not a good idea. Switches, sensors and I/O are too cheap now. What happens if the air pressure is low and the event takes longer or never even happens? What if you get water in the air and that water ends up in the cylinder? Do yourself a favor and do it right. This is the exact reason mcSignalWait exist. It is the best of both. It waits for a logic change for the set amount of time. As soon as the logic changes it goes on (no wasted time). If the logic doesn't change in the specified amount of time you choose what to do next. This makes it efficient. It does not wait any longer than is necessary but (just like in your situation) it gives a time parameter for those instances where it doesn't do it as fast as optimal conditions would allow it. It also gives you options for when it doesn't do it at all. You have full control. Hard to beat that.
« Last Edit: August 21, 2019, 03:31:59 PM by Chaoticone »
;D If you could see the things I have in my head, you would be laughing too. ;D

My guard dog is not what you need to worry about!
Re: Pause Lua script mach4
« Reply #9 on: August 21, 2019, 03:46:25 PM »
i use this:
function waiit()
   local inst = mc.mcGetInstance()
   local GCode =""
            GCode = GCode ..string.format("G4 p500")
            mc.mcCntlGcodeExecuteWait (inst, GCode) ---wait 0.5 second
   
end
Thanks katzyaakov I will try this for now. 

Sounds like you need a conformation input if you are wanting it to wait until a cylinder is in a certain position. Inputs are what makes machines smart. Are there other ways? Absolutely but none of them are the best way to do it. Using any time based event to wait for the success of a logic driven event is not a good idea. Switches, sensors and I/O are too cheap now. What happens if the air pressure is low and the event takes longer or never even happens? What if you get water in the air and that water ends up in the cylinder? Do yourself a favor and do it right. This is the exact reason mcSignalWait exist. It is the best of both. It waits for a logic change for the set amount of time. As soon as the logic changes it goes on (no wasted time). If the logic doesn't change in the specified amount of time you choose what to do next. This makes it efficient. It does not wait any longer than is necessary but (just like in your situation) it gives a time parameter for those instances where it doesn't do it as fast as optimal conditions would allow it. It also gives you options for when it doesn't do it at all. You have full control. Hard to beat that.

First thank Chaoticone for showing me the signalwait function I didn't know this existed, it sounds very useful for future implementations and for sure I will use it.
And thanks for the reply you are right indeed. I rather do it with logic inputs than time based. But the machine is from 1993 and the reed sensors that can be mounted on the pneumatic cylinders are not available. But there are indeed different ways to mount a universal or newer model sensor and for sure I will figure something out.

But for testing while waiting for the sensors I will use katzyaakov's solution. 

Thanks again guys