Machsupport Forum

Mach Discussion => Mach4 General Discussion => Topic started by: Tony Bullard on April 30, 2019, 10:31:01 AM

Title: Mach4 Timer Script
Post by: Tony Bullard on April 30, 2019, 10:31:01 AM
I’d like to have a timer start and stop a lub pump after an hour of spindle on time. That would be an accumulated time of spindle on and off time. The scr.StartTimer(#,Time,oneshot) starts the event after the time has expired. The scr.StopTimer(#) seems to cancel the timer before the event. I’d like to start and then pause the timer and then restart. The scr.RestartTimer(#) doesn’t seem to have any effect on the timer. How Is the RestartTimer supposed to work? Thanks for any help.
 
Title: Re: Mach4 Timer Script
Post by: Chaoticone on April 30, 2019, 11:42:38 AM
I'm not sure about the RestartTimer but think I know how i would handle it.

In the PLC script keep up with spindle on time. If it's on increment a var. Once that var == Time wanted / scan time start a one shot timer for the amount of time I want the lube on, turn the lube output on and reset that var to 1. The timer would just turn the lube output off.
Title: Re: Mach4 Timer Script
Post by: Chaoticone on April 30, 2019, 12:50:43 PM
This might help if you go with my suggestion.

Code: [Select]
local scrName = scr.GetScreenName()
local inter = scr.GetProperty(scrName, "PLC Interval")
Title: Re: Mach4 Timer Script
Post by: Tony Bullard on May 01, 2019, 01:42:50 PM
Thank you. This looks promising. I've set up a register and functions to get and wright to them. My plc Interval is 50. I run this code in the PLC Script for a test. The functions are in the Screen Load Script.

local val = GetRegister("HurcoTimer")
val = val + 1
WrightRegister("HurcoTimer", val)

I am in demo mode. The PLC Interval seems to be much slower than 50. I get 1000 counts in 62 seconds repeatedly. Any suggestions? thanks.
Title: Re: Mach4 Timer Script
Post by: joeaverage on May 01, 2019, 02:43:09 PM
Hi,
I think the PLC script runs as an interrupt routine. The interrupt interval might be 50 ms but the PC looks to be
taking 12 ms or so to respond to the interrupt and reset the timer which results in a 62 ms delay between script runs.

Relying on the PLC script interval as an accurate timing mechanism is not realistic. In this case does it matter unduly
if the lube pump runs after 1hr spindle time or 1hr 5 min?

Craig
Title: Re: Mach4 Timer Script
Post by: Tony Bullard on May 01, 2019, 03:31:54 PM
Thank you Craig. No the timing is not critical and I can easily adjust the code to use it for the lub pump. As much as I'd like to do most everything in Mach4 I may just use an Arduino with its millis function for the timed events.
Title: Re: Mach4 Timer Script
Post by: Chaoticone on May 01, 2019, 04:51:47 PM
Yup, Craig is right. And I would not use the PLC loop count if accuracy is critical.

You could do some testing and adjust the script but things could change it (like adding more script to the PLC script itself). Would it ever change enough to matter in most cases? No but each line of code takes time to process.

Any of the following would yield more accurate results I think..... os.date, os.difftime, mc.mcCntlGetRunTime at the expense of added complexity.

All ways would have a rounding error.

Lots of ways to skin this cat. Knowing how good is good enough may be the determining factor.
Title: Re: Mach4 Timer Script
Post by: joeaverage on May 02, 2019, 08:04:28 AM
Hi,
I've been looking at the provisions of wxTimer. It would appear that could be used.

https://docs.wxwidgets.org/trunk/classwx_timer.html (https://docs.wxwidgets.org/trunk/classwx_timer.html)

Thus a Lua statement like:

MyLubeTimer:start(-1)   would cause the previously defined wxTimer called MyLubeTimer to restart from it
previous stop time. Thus the timer would accumulate. Your m3 and m4 macros would each contain such a timer
start statement and your m5 macro would contain a timer stop statement.

In your PLC script you would test the sate of the timer, once 1 hour had accumulated the lube pump would be run.
The timer could then be reset and it would start to accumulate again.

I have yet to complete my experimentations with wxTimer to confirm my idea. If it works as I imagine that would allow
you to track spindle on time to within seconds.

Quote
As much as I'd like to do most everything in Mach4 I may just use an Arduino with its millis function for the timed events.

This adds complexity.......but then so does the programming that Brett has hinted at or the wxTimer idea that I have
outlined. The question is 'does the required accuracy warrant the extra complexity', the operative word is 'required',
not 'wanted' or 'desired' or 'I really want to skite about this' or 'my mother told me I'd go blind doing this'
BUT 'required'.

Craig
Title: Re: Mach4 Timer Script
Post by: Tony Bullard on May 03, 2019, 07:32:13 AM
  MyLubeTimer:start(10000) starts the timer for 10seconds.

MyLubeTimer:Stop()  stops the timer.

MyLubeTimer:start(-1) restarts MyLubeTimer for 10 seconds. In other words the default valve.
 
I think I'll take another look at the TimerPanel.
Title: Re: Mach4 Timer Script
Post by: Tony Bullard on May 03, 2019, 01:12:40 PM
Is there a way to use the Time Function # variables in scripts?
From Mach4 Mill Gcode Programing.pdf
Title: Re: Mach4 Timer Script
Post by: Chaoticone on May 03, 2019, 06:48:52 PM
Yup

value, rc = mc.mcCntlGetPoundVar(
      number mInst,
      number param)
Title: Re: Mach4 Timer Script
Post by: Tony Bullard on May 04, 2019, 08:10:29 AM
Thank you for the method. They return null in a button script for me. Apparently the time variables are not implemented yet.
From poppabear.
Title: Re: Mach4 Timer Script
Post by: joeaverage on May 04, 2019, 03:35:21 PM
Hi Tony,
I think that list of poppabears is out of date.

May I suggest opening the register diagnostic and pinning it to the top.
Observe the range including the timer variables. They are populated and change as time passes. This is the latest build of
Mach4 but these variables have been enacted for several years at least.

Craig
Title: Re: Mach4 Timer Script
Post by: Tony Bullard on May 05, 2019, 08:28:38 AM

Hi Craig,

Thank you again. The Time # variables are there and are represented correctly. I must not have coded my test correctly. I will work on some timers and post my results hopefully this week.