Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
PLC timer
#1
How do I make a timer in a script PLC?
Reply
#2
open plc 1
L1 = sys.time + 0.01;
while(L1 > sys.time) {};
p2 = p2 + 1;
close
Reply
#3
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);
close

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;
close
Reply
#4
Attached is a file of timer functions that you can include in a project to give your code timer functionality.


.zip   func_timer 9_9_10.zip (Size: 581 bytes / Downloads: 74)
Reply
#5
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?
Reply
#6
They go under the Pmac Script Language\Libraries directory.
Reply
#7
Brad,

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?
Reply
#8
There are some threads that show an older way to do this (http://forums.deltatau.com/showthread.ph...ght=fclock). 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.
Reply
#9
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?
Reply
#10
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.
Reply
#11
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.
Reply
#12
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!
Reply
#13
The sys.time structure element is floating point giving time resolution to microseconds. Just take the difference between your two events:

P1=sys.time
. . .
P2=sys.time - P1

Note that you cannot write to sys.time.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)