S7 Averaging FB

consys

Member
Join Date
Jul 2006
Location
melbourne
Posts
208
I have been "playing" with S7 and have written a small FB that will average a REAL value over a number of samples. The code is attached. My questions is.. Whay do I have to load the data into the DB using L DBD [#dPointer]? I have a STAT declared which is a REAL array of 60 elements - "rSample[1..60] Of Real" but I cannot get the code to accept this, instead I have had to OPN the DB at the start of FB and use L DBD [#dPointer]. It's no big deal to me either way as the code appears to work this is more about learning ..so any criticism will be appreciated

Thanks
 
Here's one implementation. You can use address registers to index through the array and use the pointer operator (P#) to point to the address of a variable so that when the variable moves in memory, your program will automatically reference the correct locations.

Code:
FUNCTION_BLOCK FB 1
TITLE =
VERSION : 0.1

VAR_INPUT
  iLength : INT ; 
  rValue : REAL ; 
END_VAR
VAR_OUTPUT
  rAverage : REAL ; 
END_VAR
VAR
  rSamples : ARRAY  [1 .. 50 ] OF REAL ; 
  iSampleCount : INT ; 
END_VAR
VAR_TEMP
  iLoopCount : INT ; 
  rAvg : REAL ; 
  dwPtr : DWORD ; 
END_VAR
BEGIN
NETWORK
TITLE =
	  TAR2  ; 
	  LAR1  P##rSamples; 
	  +AR1  ; 
	  TAR1  #dwPtr; 
NETWORK
TITLE =
	  L	 #iSampleCount; 
	  SLD   5; 
	  +AR1  ; 
	  L	 #rValue; 
	  T	 DID [AR1,P#0.0]; 
	  L	 #iSampleCount; 
	  +	 1; 
	  T	 #iSampleCount; 
	  L	 #iLength; 
	  <I	; 
	  JC	Exit; 
NETWORK
TITLE =
	  L	 0.000000e+000; 
	  T	 #rAvg; 
	  L	 0; 
	  T	 #iSampleCount; 
	  LAR1  #dwPtr; 
	  L	 #iLength; 
avgL: T	 #iLoopCount; 
	  L	 DID [AR1,P#0.0]; 
	  L	 #rAvg; 
	  +R	; 
	  T	 #rAvg; 
	  +AR1  P#4.0; 
	  L	 #iLoopCount; 
	  LOOP  avgL; 
	  L	 #rAvg; 
	  L	 #iLength; 
	  ITD   ; 
	  DTR   ; 
	  /R	; 
	  T	 #rAverage; 
NETWORK
TITLE =
Exit: SET   ; 
	  SAVE  ; 
END_FUNCTION_BLOCK
 
Looking at original post you seem to be using average to filter inputs.
I use the following code in a FB that provides a very simple and efficient filter.
2 Inputs - Input Value and Percentage of New.
3 Static - Percent of Old, New Contribution and Old Contribution.
1 Output - Output (Filtered) Value.
At the call you simply adjust the Percentage of New to increase or decrease the severity of filtering.

//Output = ((Input * %New) + (Stored Value * 100.0 - %New))/100.0
L 1.000000e+002
L #Percent_Of_New
-R
T #Percent_Of_Old // = 100 - %New
L #Input_FB
L #Percent_Of_New
*R
T #New_Contribution // = Input * %New
L #Stored_Value
L #Percent_Of_Old
*R
T #Old_Contribution // = Stored * %Old
L #New_Contribution
+R
L 1.000000e+002
/R
T #Output_FB // = ( New + Old )/100
T #Stored_Value // Update Stored Value
 
Thanks L D[AR2, P#0.0]

John_Gaunt are you sure you can't see me from your desk ??
That is exactly what I am doing, I need to monitor two variables and if one of them starts to climb when the other is falling I need to trigger an alarm, so I thought I would use the average to filter out some of the noise.
 
consys-

If this really is an input filtering application John Gaunt's solution is a very good one. It is an implementation of a first order digital IIR lowpass filter. They work quite well for general purpose filtering. However, this filter won't produce an average. It produdces an exponential response. Again, it is good for general filtering but it isn't an average.

Keith
 
Thank you all for your responses,
John G & Pandiani I will try your code and will let you know as I do have a separate need for filtering some analogs.
Now for a bit of an explanation..as I stated earlier I need to watch two variables and set an Alarm ON if they start to diverge. As most analog values tend to have some noise I cannot simply read both values on a scan and then next scan trigger an alarm if one has climbed and the other has dropped. I was originally intending to use the average as a form of filter but I am now thinking that I might be better off still using the average but NOT as a filter, simply to look at the average over a period of time and then create some code that basically says "the average has climbed by more than X% over the last time period" . In this particlur case I am not so interested in a noise free signal as I am in a signal that is dropping/climbing over time.
 
Last edited:

Similar Topics

Hi, I have a bit of PLC experience in Siemens and have unfortunately been blessed with working on Automation Studio and B&R which has been a big...
Replies
5
Views
1,364
The PLC code calculates oil flow rate and outputs it on D420. It is represented on the HMI as a vertical bar. Problem is that the operator says...
Replies
33
Views
4,927
Hello, I am working with a laser encoder that is providing the speed of a fiber strand to our AB 1756-L61 PLC. In order to calculate the amount...
Replies
3
Views
3,146
Hi ;Configure A1s64AD card in slot 3 is as attached. This is averaging count. my signals are coming in at channel 3 and 4. There are spiking so i...
Replies
1
Views
1,632
Hi, folks. Thanks for taking the time to read this. I need to average 5 numbers, they being an accumulated value (mSec) from a timer. My director...
Replies
19
Views
5,644
Back
Top Bottom