Loading...
Pages: [1]   Go Down
Author Topic: RunningAverage Class on playground (updated)  (Read 3179 times)
0 Members and 1 Guest are viewing this topic.
Netherlands
Offline Offline
Tesla Member
***
Karma: 90
Posts: 9407
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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 smiley

Please post comments and improvements in this thread,

thanks,
Rob

Code:
//
//    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
« Last Edit: December 30, 2012, 06:02:57 am by robtillaart » Logged

Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -

Central MN, USA
Offline Offline
Faraday Member
**
Karma: 35
Posts: 5935
Phi_prompt, phi_interfaces, phi-2 shields, phi-panels
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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!
Logged


nr Bundaberg, Australia
Offline Offline
Tesla Member
***
Karma: 71
Posts: 6830
Scattered showers my arse -- Noah, 2348BC.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

_____
Rob
Logged

Rob Gray aka the GRAYnomad http://www.robgray.com

0
Offline Offline
Newbie
*
Karma: 0
Posts: 5
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset


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

Netherlands
Offline Offline
Tesla Member
***
Karma: 90
Posts: 9407
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
« Last Edit: February 28, 2011, 02:45:08 am by robtillaart » Logged

Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -

0
Offline Offline
Newbie
*
Karma: 0
Posts: 5
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Rob, Thanks for that, the compiler gets past that, but now comes up with another problem,  error : request for member 'clr' in 'myRA', which is of non-class type 'RunningAverage ()()'.
This occurs also for member 'add' and 'avg' when I comment out the other problem lines.

I know zip about C++ so I'm at a loss .

Thanks Rock.
Logged

Netherlands
Offline Offline
Tesla Member
***
Karma: 90
Posts: 9407
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Please post your sketch so I can check this one too.
Logged

Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -

0
Offline Offline
Newbie
*
Karma: 0
Posts: 5
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Rob, I found the problem , I had not deleted the .o file and restarted the IDE after changing the .h file.

compiles OK now.

Thanks for your help.

Rock.
Logged

Netherlands
Offline Offline
Tesla Member
***
Karma: 90
Posts: 9407
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset


Found a newer versio of the runningAverage class by Yuval Naveh here

- http://stromputer.googlecode.com/svn-history/r74/trunk/Arduino/Libraries/RunningAverage/RunningAverage.cpp -

most important addition:
Code:
void RunningAverage::trimToValue(float value)
{
clr();
add(value);
}

TODO: update the playground
Logged

Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -

Netherlands
Offline Offline
Tesla Member
***
Karma: 90
Posts: 9407
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Finally found some time to update the runningAverage lib on the playground: - http://playground.arduino.cc//Main/RunningAverage -

changes:
1) There are some breaking changes in the method names to make them more descriptive;
// clr()  => clear()
// add(x) => addValue(x)
// avg() => getAverage()

2) new is the fillValue() function, based upon the trimValue() of Yuval Naveh  - see previous post
Code:
// fill the average with a value
// the param number determines how often value is added (weight)
// number should preferably be between 1 and size
void RunningAverage::fillValue(float value, int number)
{
clear();
for (int i = 0; i < number; i++)
{
addValue(value);
}
}
It fills the internal array with a number of identical values to get a starting value for average.
By adding more than one value, the initial average gets a certain weight.
This extends the original trimValue() so that's why I gave another name.

3) some small refactoring and added comments

as always comments, remarks and ideas are welcome
« Last Edit: December 30, 2012, 06:03:24 am by robtillaart » Logged

Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -

Pages: [1]   Go Up
Print
 
Jump to: