Go Down

Topic: RunningAverage Class on playground (updated) (Read 6 times) previous topic - next topic

robtillaart

Jan 30, 2011, 07:01 pm Last Edit: Dec 30, 2012, 12:02 pm by robtillaart Reason: 1
A question in this forum from Liudr - http://arduino.cc/forum/index.php/topic,50383.0.html - triggered me to implement a small runningAverage class. This can be found on - http://arduino.cc/playground/Main/RunningAverage .  A sample sketch shows how one can create running average on 2 different levels, - last minute, last hour - when one has a sample per second. In the sketch the sensor is simulated by the random function and samples are made far faster than one per second but in sample code this is allowed I guess :)

Please post comments and improvements in this thread,

thanks,
Rob

Code: [Select]
//
//    FILE: runningAverageHour.pde
//  AUTHOR: Rob Tillaart
//    DATE: 2012-12-30
//
// PUPROSE: show working of runningAverage per hour
//          in 2 steps - last minute + last hour
//          3 or more steps also possible
//

#include "RunningAverage.h"

RunningAverage raMinute(60);
RunningAverage raHour(60);

int samples = 0;

void setup(void)
{
  Serial.begin(115200);
  Serial.println("Demo RunningAverage lib - average per minute & hour");
  Serial.print("Version: ");
  Serial.println(RUNNINGAVERAGE_LIB_VERSION);
  raHour.clear();
  raMinute.clear();
}

void loop(void)
{
  long rn = random(0, 100);
  raMinute.addValue(rn);
  samples++;
 
  if (samples % 60 == 0) raHour.addValue(raMinute.getAverage());
 
  Serial.print("  raMinute: ");
  Serial.print(raMinute.getAverage(), 4);
  Serial.print("  raHour: ");
  Serial.println(raHour.getAverage(), 4);
}


updated example to reflect 0.2.02 version
Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

liudr

Thanks a lot Rob. The sample code here is very nice. I love the two level average, nice for OOP. I guess I can set the sample amount to 60 or other quantity? I'll look into it later today!

Graynomad

Thanks for that, it'll save me writing something for an upcoming project. I'll play with it when the time comes.

_____
Rob
Rob Gray aka the GRAYnomad www.robgray.com

Rocketboy


Rob, Just tried to get your example to go but comes up with a compile error.

\libraries\RunningAverage\RunningAverage.cpp:26: error: definition of implicitly-declared 'RunningAverage::~RunningAverage()'

Any help appreciated .

Thanks Rock.

robtillaart

#4
Feb 28, 2011, 08:16 am Last Edit: Feb 28, 2011, 08:45 am by robtillaart Reason: 1
Which version of Arduino are you using?
- short term patch is to comment the ~RunningAverage() from .cpp and .h but that would create a memory leak. However as long as the object is in scope and there is no need to free the internal array that should not give a problem.
I'll have to dive in this one

------
update:

The .h file is missing a prototype of the destructor. Add    ~RunningAverage();   in the public section just under the constructor. That should fix it.
Playground article is updated.

Thanks for finding this one.
Rob
Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

Go Up