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).
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