Machsupport Forum

Mach Discussion => Mach4 General Discussion => Topic started by: jelle_bos on August 20, 2019, 10:27:23 AM

Title: Pause Lua script mach4
Post by: jelle_bos 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
Title: Re: Pause Lua script mach4
Post by: Bill_O on August 20, 2019, 05:57:03 PM
        wx.wxSleep(seconds)
   wx.wxMilliSleep(milliseconds)
Example:
   wx.wxSleep(1)
   wx.wxMilliSleep(1000)
Title: Re: Pause Lua script mach4
Post by: jelle_bos 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?
Title: Re: Pause Lua script mach4
Post by: Bill_O on August 21, 2019, 07:39:49 AM
Yes it does stop it all.
What exactly are you trying to do?
Title: Re: Pause Lua script mach4
Post by: jelle_bos 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.
Title: Re: Pause Lua script mach4
Post by: Chaoticone 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.
Title: Re: Pause Lua script mach4
Post by: jelle_bos 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.
Title: Re: Pause Lua script mach4
Post by: KatzYaakov 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
Title: Re: Pause Lua script mach4
Post by: Chaoticone 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.
Title: Re: Pause Lua script mach4
Post by: jelle_bos 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
Title: Re: Pause Lua script mach4
Post by: Chaoticone on August 21, 2019, 04:29:46 PM
OK, good.

Yup, running dwell in Gcode is as good a way as any. I would just use a single line or edit Katz function to take a time parameter if it was something i might use in different places.

The code below should also check the type of dwell and convert to a number or error out if it can't be converted.

Code: [Select]
function wait(dwell)
local inst = mc.mcGetInstance()
local defaultDwell = 250
if (dwell == nil) or (dwell < defaultDwell) then --No dwell passed or a shorter dwell than the motion device can process will cause errors
dwell = defaultDwell
end
local GCode = string.format("G4 p" .. tostring(dwell))
rc = mc.mcCntlGcodeExecuteWait (inst, GCode) ---wait for time of dwell
--Do something here if rc ~= 0 because you have an error
end

--Then use it like this

wait(100) --will wait for 250 milliseconds == .25 seconds because it is less than default defined in function

wait() --will wait for 250 milliseconds == .25 seconds because dwell is not defined so function will define as default

wait(500) --will wait for 500 milliseconds  == .5 seconds

Don't use it like this though..........

Code: [Select]
wait("10 seconds") --will cause problems if the dwell type check I mentioned above isn't added
Title: Re: Pause Lua script mach4
Post by: Eveider on November 03, 2022, 09:30:08 AM
why doesn't wx.wxMilliSleep() work in signal script
Title: Re: Pause Lua script mach4
Post by: mediumSasuage on November 01, 2023, 07:12:51 AM
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

Hi Chaoticone,
Do you agree that this is a valid solution or is it still dangerous for the reasons mentioned above?
Many thanks, Dan