Calculating average value S7

Werner

Member
Join Date
Apr 2005
Location
IJsselstein
Posts
336
Is there a function available to calculate the avarage value of a list of integer values? The problem is that the sum of values is to large to be hold in an INT value. I try to avoid to much conversion from INT > DINT etc.
 
No but find following source code for Average and SD - I've included the UDT (I think that should work)

TYPE UDT 67
VERSION : 0.1

STRUCT
Average : REAL ;
StandardDev : REAL ;
NoOfSamples : INT ;
Total : REAL ;
SquareTotal : REAL ;
END_STRUCT ;
END_TYPE
FUNCTION "AVERAGE_SD" : VOID
TITLE =Average & Standard Deviation
VERSION : 0.1

VAR_INPUT
CountingEnable : BOOL ;
Clock : BOOL ;
Reset : BOOL ;
Value : REAL ;
END_VAR
VAR_IN_OUT
Device : UDT 67;
END_VAR
VAR_TEMP
TempR : REAL ;
TempDI : DINT ;
NoOfSamples : REAL ;
StandardDevDenominator : REAL ;
Square : REAL ;
END_VAR
BEGIN
NETWORK
TITLE =Reset
A #Reset;
= L 20.0;
A( ;
A( ;
A L 20.0;
JNB _007;
L 0;
T #Device.NoOfSamples;
SET ;
SAVE ;
CLR ;
_007: A BR;
) ;
JNB _00c;
L 0.000000e+000;
T #Device.Total;
SET ;
SAVE ;
CLR ;
_00c: A BR;
) ;
JNB _00d;
L 0.000000e+000;
T #Device.SquareTotal;
_00d: NOP 0;
A( ;
A L 20.0;
JNB _00e;
L 0.000000e+000;
T #Device.Average;
SET ;
SAVE ;
CLR ;
_00e: A BR;
) ;
JNB _00f;
L 0.000000e+000;
T #Device.StandardDev;
_00f: NOP 0;
NETWORK
TITLE =Number Of Samples Update & Conversion
A( ;
A( ;
A #Clock;
A #CountingEnable;
JNB _014;
L #Device.NoOfSamples;
L 1;
+I ;
T #Device.NoOfSamples;
AN OV;
SAVE ;
CLR ;
_014: A BR;
) ;
JNB _015;
L #Device.NoOfSamples;
ITD ;
T #TempDI;
SET ;
SAVE ;
CLR ;
_015: A BR;
) ;
JNB _017;
L #TempDI;
DTR ;
T #NoOfSamples;
_017: NOP 0;
NETWORK
TITLE =Total Update
A #Clock;
A #CountingEnable;
JNB _018;
L #Device.Total;
L #Value;
+R ;
T #Device.Total;
_018: NOP 0;
NETWORK
TITLE =Square Total Update
A( ;
A #Clock;
A #CountingEnable;
JNB _019;
L #Value;
SQR ;
T #Square;
AN OV;
SAVE ;
CLR ;
_019: A BR;
) ;
JNB _01d;
L #Device.SquareTotal;
L #Square;
+R ;
T #Device.SquareTotal;
_01d: NOP 0;
NETWORK
TITLE =Average Update
A #Clock;
A #CountingEnable;
JNB _01e;
L #Device.Total;
L #NoOfSamples;
/R ;
T #Device.Average;
_01e: NOP 0;
NETWORK
TITLE =Standard Deviation Update
// _ _________________________
// \ / 2 2
// \ / n*SUM(x ) - (SUM(x))
//St.Dev = \ /--------------------------
// \/ n*(n-1)
A #Clock;
A #CountingEnable;
= L 20.0;
A( ;
A L 20.0;
JNB _020;
L #NoOfSamples;
L 1.000000e+000;
-R ;
T #StandardDevDenominator;
AN OV;
SAVE ;
CLR ;
_020: A BR;
) ;
JNB _021;
L #StandardDevDenominator;
L #NoOfSamples;
*R ;
T #StandardDevDenominator;
_021: NOP 0;
A( ;
A( ;
A L 20.0;
JNB _02a;
L #Device.Total;
SQR ;
T #Square;
AN OV;
SAVE ;
CLR ;
_02a: A BR;
) ;
JNB _02b;
L #Device.SquareTotal;
L #NoOfSamples;
*R ;
T #TempR;
AN OV;
SAVE ;
CLR ;
_02b: A BR;
) ;
JNB _030;
L #TempR;
L #Square;
-R ;
T #TempR;
_030: NOP 0;
A L 20.0;
JNB _031;
L #TempR;
L #StandardDevDenominator;
/R ;
T #TempR;
_031: NOP 0;
A L 20.0;
JNB _032;
L #TempR;
SQRT ;
T #Device.StandardDev;
_032: NOP 0;
NETWORK
TITLE =ENO Setting
SET ;
SAVE ;
END_FUNCTION
 

Similar Topics

I need to keep a running pass/fail yield of the previous 5,000 parts produced. I have used this formula before to calculate average: AvgValue =...
Replies
6
Views
2,156
All - I am writing a control panel program in RSLogix500 f/u/w a MicroLogix 1100 PLC controller. I have 8 exhaust port temps stored in...
Replies
4
Views
14,757
Hi, Can someone help me please? I have two temperatures and I'd like to calculate the average temperature. Average temperature must be written...
Replies
2
Views
3,843
This application has a motor with encoder feedback that drives a linear actuator that moves in/out, and is at roughly 45 degs from horiz. As the...
Replies
19
Views
1,365
Does anyone know how to calculate the savings from now needing to run an air compressor all day? Basically I have a design that replaced 6 * 1"...
Replies
26
Views
4,829
Back
Top Bottom