Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
PLC timer
How do I make a timer in a script PLC?
open plc 1
L1 = sys.time + 0.01;
while(L1 > sys.time) {};
p2 = p2 + 1;
Another way to do this same thing is to use the new ability of subroutines and program calling from a PLC program. In this case you can have the timer function as follows:

open subprog timer (time)
local EndTime;
local count;

EndTime = Sys.Time + time;
do count++; while (EndTime > Sys.Time);

Then you can use this from a PLC program as a one-line function call as follows:

open plc 1
callsub timer (3); // make a 3 second delay
p2 = p2 + 1;
Attached is a file of timer functions that you can include in a project to give your code timer functionality.

.zip   func_timer (Size: 581 bytes / Downloads: 74)
brad Wrote:Attached is a file of timer functions that you can include in a project to give your code timer functionality.

How or where do I include this file in the project?
They go under the Pmac Script Language\Libraries directory.

I tried to write a simple timer in C, and I used the system time that you were using in your example above: "Sys.Time"

Of course in C I wrote it like this: "pshm->time" Since according to my training notes the Sys is implicit when accessing it through the pshm pointer.

Anyway, I received an error telling me that the structure SHM has no member named "Time". Can you advise me on how I should have done this?
There are some threads that show an older way to do this ( But now you can use the C API function GetCPUClock() which returns the clock in micro seconds. This is described in the C function help from the IDE Help Contents area.
Great, I'll try that out tomorrow.

Is there an explanation for why "pshm->Time" doesn't work? Is only part of the Sys. structure available in C or something? If so, is this described somewhere?
One can also use Sys.ServoCount or Sys.PhaseCount if it is desirable to time applications in units of servo cycles or phase cycles, respectively.
The explanation is that some structures actually call functions to nicely report data in the script language. Sys.Time is one of these. You can look into RtGpShm.h but the easiest way to see this is to use the IDE help.
What if instead of counting down, I want to count up? I have an application in a Geobrick where I need to time how long a process takes and then write that time to a P-variable so I can display it on an HMI. I think I can mock something up by writing Sys.time (or sys.runtime) to one variable at the start of the process, a different variable at the end, and writing the difference between the two to my P-variable, but that will only get me accurate to within a second.
First, am I thinking correctly that this will work?
Second, is there another, better way that will get me accuracy to the tenth or hundredth of a second?
Integrate this!
The sys.time structure element is floating point giving time resolution to microseconds. Just take the difference between your two events:

. . .
P2=sys.time - P1

Note that you cannot write to sys.time.

Forum Jump:

Users browsing this thread: 1 Guest(s)