A simple (?) timer issue in ST

Decamber

Member
Join Date
Sep 2008
Location
Middle
Posts
14
Hello;

I'm programming a Wago 750-842 and am stuck on something that should be quite simple.

I want to save in an array ten values from an AI over a period of two seconds. Then I'll calculate the avarage and save in another variable. This procedure will repeat endlessly.

I'm trying to use TP set to 0.2 seconds in a while-loop, but the programming software (WAGO-IO-PRO 32; same thing as CoDeSys) warns about infinite loop.

Do you have any solution? (Preferably in ST).

Thanks!
Peter
 
I've managed to get one step further... this is a code sample:
Code:
toninst:= TON;


toninst(IN:=1, PT:=T#2s);

CASE TIME_TO_INT(toninst.ET) OF
	0..50:
		vikt_array[1]:=akt_vikt;
	450..550:
		vikt_array[2]:=akt_vikt;
	950..1050:
		vikt_array[3]:=akt_vikt;
	1450..1550:
		vikt_array[4]:=akt_vikt;
	1950..2000:
		vikt_array[5]:=akt_vikt;
END_CASE;

And it works! However... When I stop the PLC and start it again, it keeps the values recorded last and won't start the timer again. I've tried resetting the timer with toninst(IN:=0); but then it won't start the timer at all.

Any hints?
 
The way to do a timer that repeats itself endlessly would be like the following(Codesys example):

(in the declaration section)
Timer1 : TON;

(in the code section)
Timer1(IN:=(NOT Timer1.Q), PT:=T#2s);


By putting a 1 or 0 in the IN parameter the timer is forced on or off.

If you did a Timer1(IN:=1, PT:=T#2s) and then a Timer1(IN:=0, PT:=T#2s) later, I believe the PLC acts as if it only processes the IN:=0 line.
 
Here's what I would do.

As exabmorgan showed, you can make a self resetting timer by triggering it with the NOT done marker from that same timer.

Then you only copy the value when the timer is done. Because of the way the timer works, this is only true for 1 scan so you're not repeatadly copying the value over and over again. If you want 10 values over 2 seconds, then use a 200ms preset.

Use an index variable to keep track of where you are in the array of the last 10 variables. When it gets too big, reset it back to 1 and start all over again.

While you're copying the variable to the array, update the sum of all values used in creating the average and since your array values only change while the the timer .Q bit is high, that's the only time you need to calculate the average.

This sample assumes you only need an integer average and uses DINT variables for the Sum to avoid overflow if the values are large. It uses DINT for the average so you don't need a conversion when you calculate the average. If you want a REAL value then you'll need a conversion when you calculate the average.


PROGRAM PLC_PRG
VAR
TONinst: TON; (* Timer *)
i: INT := 1; (* Integer Index Variable *)
vikt_array: ARRAY [1..10] OF INT; (* Array of values *)
akt_vikt: INT; (* Value to track over time *)
vikt_sum: DINT; (* Running sum of all values *)
vikt_avg: DINT; (* Average of 10 values *)
END_VAR


(* Call the timer. It will reset itself continuously *)
TONinst(IN:= NOT(Toninst.Q), PT:=t#200ms );

(* When the timer times out, copy the value to the array *)
IF TONinst.Q THEN
vikt_sum := vikt_sum - vikt_array + akt_vikt;
(* Copy the Value to the Array *)
vikt_array := akt_vikt;
(* Incerment the Index Variable *)
i := i + 1;
(* Check if i is in range, reset if too big *)
IF i > 10 THEN
i := 1;
END_IF;

vikt_avg := vikt_sum / 10;

END_IF;


 
exabmorgan, ndzied1,


Thanks for the replies. I tried your code, ndzied1, and it worked eminently. I few minor modifications and it suits my needs. Perfect.
 

Similar Topics

Hi all, I have a simple question that I have overcomplicated and gotten stuck on. I have a variable, we can call it "light" that I need to stay...
Replies
4
Views
310
I want to replace a Grasslin timer with something that Communicates via EtherNet/IP™ I keep getting schedule changes that force me to go out and...
Replies
2
Views
1,598
Hi guys, i've a simple action to do with a air punch. When power on the machine with a toggle (always true), timing should be on adjustable about...
Replies
5
Views
2,060
So i am a EE and mostly work in protections. I recently got thrust into more of a controls roll and need to start learning some logic. What...
Replies
18
Views
6,331
Hi all. Stumbled upon this site during a few PLC beginner searches and it looks like a great community. I have a simple beginner question about a...
Replies
3
Views
2,847
Back
Top Bottom