Built-in function with global timer?
Hi,
OSCAT library has an extensive use of TIME() function in all filters and regulators, but that function is not available in PLCnext Engineer as far as I can tell.
This function is supposed to return a milisecond counter from an internal PLC timer which runs from boot until reboot or overflow.
I have tried different workarounds, but it is not desirable to use the function blocks available in PLCnextBase as they need to be in variable memory instead of using a function accessing a global internal timer which has the same value everywhere in the plc.
Other, even less desirable workarounds consists of creating a dedicated POU in a dedicated task to create this timer, but again I cannot seem to access global variable inside functions. Also having functions depending on another program and task to run correctly is poor programming practice.
So, any suggestions ?
Comments
It seems like you could just create a TON_LTIME that turns on when you are running. Feed it a Preset (Preset_LT) with a huge INIT VALUE (e.g., T#999999999s). Then you can store the elapsed time in another LTIME variable (e.g., Elapsed_LT).
You can SPLIT_LTIME into the components.
I did not show it but then you can recombine the components into number of milliseconds with some math; I anticipate you will need to use LINTs for your data types because your numbers will get very big. Here is a general description of how to break down the larger blocks of time into msec.
temp_hours = Elapsed_Days * 24
total_ hours = temp_hours + Elapsed_Hours
temp_minutes = total_hours * 60
total_minutes = temp_minutes + Elapsed_Minutes
temp_seconds = total_minutes * 60
total_seconds = temp_seconds + Elapsed_Seconds
temp_msec = total_seconds * 1000
total_msec = temp_msec + Elapsed_Milliseconds
Just my first thoughts...
SFW
Hi Steve and thanks for your reply.
Creating a TON-timer would require it to be instantiated in a function block and into memory, and I would really like to have a function.
I find it hard to fathom that there isn't any internal timer running in the PLC which is accessible by a TIME function. In some cases it would also work to get the real time clock and use the ms part, but even that is only accessible by means of a function block for some reason...
Hi fluxmodel,
I suppose you could try to write a function in C# or C++ that has access to the RTC variable.
But that's not my field of expertise so maybe @Martin PLCnext Team or @Eduard PLCnext Team can say if that's even possible.
Cheers
DivisionByZero
Yes, it should be possible for a Function or Function Block, written in C#, to use the
DateTime.Now
function to return the system time (see the eCLR help system for information on that function).I believe that the IEC 61131 definition of a Function is that it "always returns the same output value for the same inputs", so if the current time is always returned then that would not satisfy this definition of a Function, which might be part of the reason why the PLCnextBase library uses a Function Block instead.
Hi guys and thanks for your involvement.
I re-wrote the OSCAT-function using a function returning the PLC task interval time instead. After looking into the code it seems as this TIM() function was used to calculate just that, so I am avoiding the entire problem.
Cheers