Continous calculation with the last values from an AI

Decamber

Member
Join Date
Sep 2008
Location
Middle
Posts
14
Hello,

I have a WAGO 740-842 PLC that takes a 4-20 mA signal from some load cells and calculates the avarage value over some seconds, and finally write it to an AO.

Currently I have a timer in which the calculation takes place, and once every X seconds the avarage is written to the AO. See below for the code.

Now I'd like to change this. I want the avarage to be calculated continously based on the last N values (for example N=20). How do I accomplish this?

That is:
I want the measured instantaneous weight to be added to an array / FIFO / list ... and continously have the average be calculated, based on the last 20 values (for instance).



Code:
 skalfaktor:= WORD_TO_REAL(maxvikt)/32764;
 tidd:= WORD_TO_TIME(tid);	
 
 TONinst(IN:= NOT(TONinst.Q), PT:=tidd);
   
   
   IF TONinst.Q THEN
   
   	vikt_sum:= vikt_sum - vikt_array[i] + akt_vikt;
   	vikt_array[i]:=akt_vikt;	  (* akt_vikt is the value from the AI *)
   	i:= i + 1;
   
   	IF i > antal_varden THEN	(* antal_varden is the number of samples to be taken *)
   		i:= 1;
   		vikt_avg:= DINT_TO_REAL(vikt_sum / antal_varden);
   		IF skalfaktor*vikt_avg > 3  THEN
   			vikt_skalad:= REAL_TO_WORD(skalfaktor*vikt_avg);
   		ELSE
   			vikt_skalad:= 0;
   		END_IF
   		display:=vikt_skalad;
   	END_IF
 
Just take out the IF TONinst.Q THEN and its matching END_IF.


Now the calculations are happinging on each PLC scan, not just when the timer finishes....
 
every second make a loop to:
measure
take the reading and 19 times your display divide by 20 and you will have a smooth line.
make the 20 and 19 variable so you can change them.
no need for array and shift. only two realviables.
 

Similar Topics

I am working on a project that requires an additional remote rack (say about 150 i/o points) on a L73 redundancy system with about 900 i/o points...
Replies
0
Views
864
Hi to all, I have a power supply "gwinstek" PSP-2010, with rs-232, so I want to control it with S7-1200 "1212acdcrly with CM-1241 RS-232 module...
Replies
2
Views
1,438
I was going over the code for one of our process unit and discovered that our PID loops are in the continuous task. I know they are usually placed...
Replies
20
Views
5,931
Had an interesting phone call today with a fellow controls engineer who claimed that Continuous tasks are bad for the processor and cause it to...
Replies
15
Views
8,495
Guys, My Versa View 7000 is acting up again. It is continously restarting/rebooting during start of the OS.What is happening here?
Replies
2
Views
1,694
Back
Top Bottom