Rolling Average

Discuss issues and ideas you have to configuring displays with PowerVision
ECS-SA
Posts: 48
Joined: Thu Feb 17, 2011 7:19 am

Rolling Average

Post by ECS-SA » Fri Mar 17, 2017 10:18 am

Hi guys,

please can you let me know if someone has done this before and also if possible can you add your example.

what i would like to do is take 1 incoming variable and then i would like to get the average of it say in 2sec then i want to use the average value.

i have looked all over and all i could find is how to do this with C but not sure how to implement this on powervision.

thank you
Marcell
cconner_aie
Posts: 93
Joined: Thu Jun 11, 2015 10:12 am

Re: Rolling Average

Post by cconner_aie » Fri Mar 17, 2017 3:00 pm

I haven't tested this, but in theory this would work using scripts:
Image
GetAverage_Timer is a recurring timer at 50ms (smallest increment available)

"New Script" code:

Code: Select all

int count = 0;
int total = 0;

void $GetAverage$ () 
{
	int input;
    
	// read the input variable
	smRead(VariableIDs.Input, input);
	
	// increment total and count
	total += input;
	count++;
		
	// GetAverage ran every 50ms, so when count get to 40 - 2 seconds has elapsed.
	if (count >= 40) {
	
		smWrite(VariableIDs.Average, total / count);
		
		total = 0;
		count = 0;
	}
}
Coleby Conner
Controls Engineer, Anderson Industrial Engines
normanbutchgrant
Posts: 25
Joined: Fri Oct 28, 2016 9:07 am

Re: Rolling Average

Post by normanbutchgrant » Mon Mar 20, 2017 6:19 am

thank you for the code.

we did try it

however, your code produces a average once every 2 seconds and not a rolling average

the "arduino' code that does work is the based on the following example and politely, can this be translated please?



The code below sequentially stores 10 readings from your analog sensor into an arrays, one by one. With each new value, the sum of all the numbers is generated and divided, producing an average value which then be used to smooth outlying data. Because this averaging takes place each time a new value is added to the array (rather then waiting for 10 new values, for instance) there is no lag time in calculating this running average.

Altering the size of the array used, by changing numReadings to a larger value will smooth the data collected even further.

/*

Smoothing

Reads repeatedly from an analog input, calculating a running average
and printing it to the computer. Keeps ten readings in an array and
continually averages them.

The circuit:
* Analog sensor (potentiometer will do) attached to analog input 0

Created 22 April 2007
By David A. Mellis <dam@mellis.org>
modified 9 Apr 2012
by Tom Igoe
http://www.arduino.cc/en/Tutorial/Smoothing

This example code is in the public domain.


*/


// Define the number of samples to keep track of. The higher the number,
// the more the readings will be smoothed, but the slower the output will
// respond to the input. Using a constant rather than a normal variable lets
// use this value to determine the size of the readings array.
const int numReadings = 10;

int readings[numReadings]; // the readings from the analog input
int readIndex = 0; // the index of the current reading
int total = 0; // the running total
int average = 0; // the average

int inputPin = A0;

void setup() {
// initialize serial communication with computer:
Serial.begin(9600);
// initialize all the readings to 0:
for (int thisReading = 0; thisReading < numReadings; thisReading++) {
readings[thisReading] = 0;
}
}

void loop() {
// subtract the last reading:
total = total - readings[readIndex];
// read from the sensor:
readings[readIndex] = analogRead(inputPin);
// add the reading to the total:
total = total + readings[readIndex];
// advance to the next position in the array:
readIndex = readIndex + 1;

// if we're at the end of the array...
if (readIndex >= numReadings) {
// ...wrap around to the beginning:
readIndex = 0;
}

// calculate the average:
average = total / numReadings;
// send it to the computer as ASCII digits
Serial.println(average);
delay(1); // delay in between reads for stability
}




thank you
normanbutchgrant
Posts: 25
Joined: Fri Oct 28, 2016 9:07 am

Re: Rolling Average

Post by normanbutchgrant » Mon Mar 20, 2017 11:24 am

here is a script translated from above that can be used that worked, using my (very basic) knowledge of angelscript

int NumReadingsD = 5;
int [] readings D = { 0,0,0,0,0};
int readIndexD = 0;
Int totalD = 0;
int averageD =0;

void $deman Smoothing Event$ ()
{
//subtract the last reading
totalD = totatlD - readingsD[readIndexD];
//read the value
smRead(VariableIDs.predemand, readingsD{readIndexD]);
// add the reading to the total
totalD = totalD + readingsD[readindexD];
//advance to the next position in the array
readIndexD = readIndexD +1;

// if at end of the array........
if (readIndexD >= numReadingsD) {
//..... wrap to the beginning
readIndex = 0;

}

// calculate the average

averageD = totalD / numreadingsD;

//send to computer

smWrite(VariableIDs.Demand,averageD);
}