From ea374dd3698ac7d2383f447295d80aafb72dd099 Mon Sep 17 00:00:00 2001 From: Dobrica Pavlinusic Date: Thu, 23 Nov 2017 12:36:35 +0100 Subject: [PATCH] added https://playground.arduino.cc/Main/RunningAverage --- wlan_si/RunningAverage.cpp | 85 ++++++++++++++++++++++++++++++++++++++ wlan_si/RunningAverage.h | 48 +++++++++++++++++++++ wlan_si/wlan_si.ino | 29 ++++--------- 3 files changed, 141 insertions(+), 21 deletions(-) create mode 100644 wlan_si/RunningAverage.cpp create mode 100644 wlan_si/RunningAverage.h diff --git a/wlan_si/RunningAverage.cpp b/wlan_si/RunningAverage.cpp new file mode 100644 index 0000000..f72d90a --- /dev/null +++ b/wlan_si/RunningAverage.cpp @@ -0,0 +1,85 @@ +// +// FILE: RunningAverage.cpp +// AUTHOR: Rob Tillaart +// VERSION: 0.2.04 +// PURPOSE: RunningAverage library for Arduino +// +// The library stores the last N individual values in a circular buffer, +// to calculate the running average. +// +// HISTORY: +// 0.1.00 - 2011-01-30 initial version +// 0.1.01 - 2011-02-28 fixed missing destructor in .h +// 0.2.00 - 2012-??-?? Yuval Naveh added trimValue (found on web) +// http://stromputer.googlecode.com/svn-history/r74/trunk/Arduino/Libraries/RunningAverage/RunningAverage.cpp +// 0.2.01 - 2012-11-21 refactored +// 0.2.02 - 2012-12-30 refactored trimValue -> fillValue +// 0.2.03 - 2013-11-31 getElement +// 0.2.04 - 2014-07-03 added memory protection +// +// Released to the public domain +// + +#include "RunningAverage.h" +#include + +RunningAverage::RunningAverage(int n) +{ + _size = n; + _ar = (float*) malloc(_size * sizeof(float)); + if (_ar == NULL) _size = 0; + clear(); +} + +RunningAverage::~RunningAverage() +{ + if (_ar != NULL) free(_ar); +} + +// resets all counters +void RunningAverage::clear() +{ + _cnt = 0; + _idx = 0; + _sum = 0.0; + for (int i = 0; i< _size; i++) _ar[i] = 0.0; // needed to keep addValue simple +} + +// adds a new value to the data-set +void RunningAverage::addValue(float f) +{ + if (_ar == NULL) return; + _sum -= _ar[_idx]; + _ar[_idx] = f; + _sum += _ar[_idx]; + _idx++; + if (_idx == _size) _idx = 0; // faster than % + if (_cnt < _size) _cnt++; +} + +// returns the average of the data-set added sofar +float RunningAverage::getAverage() +{ + if (_cnt == 0) return NAN; + return _sum / _cnt; +} + +// returns the value of an element if exist, 0 otherwise +float RunningAverage::getElement(uint8_t idx) +{ + if (idx >=_cnt ) return NAN; + return _ar[idx]; +} + +// 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); + } +} +// END OF FILE diff --git a/wlan_si/RunningAverage.h b/wlan_si/RunningAverage.h new file mode 100644 index 0000000..7a400a4 --- /dev/null +++ b/wlan_si/RunningAverage.h @@ -0,0 +1,48 @@ +#ifndef RunningAverage_h +#define RunningAverage_h +// +// FILE: RunningAverage.h +// AUTHOR: Rob dot Tillaart at gmail dot com +// PURPOSE: RunningAverage library for Arduino +// URL: http://arduino.cc/playground/Main/RunningAverage +// HISTORY: See RunningAverage.cpp +// +// Released to the public domain +// + +// backwards compatibility +// clr() clear() +// add(x) addValue(x) +// avg() getAverage() + +#define RUNNINGAVERAGE_LIB_VERSION "0.2.04" + +#include "Arduino.h" + +class RunningAverage +{ +public: + RunningAverage(void); + RunningAverage(int); + ~RunningAverage(); + + void clear(); + void addValue(float); + void fillValue(float, int); + + float getAverage(); + + float getElement(uint8_t idx); + uint8_t getSize() { return _size; } + uint8_t getCount() { return _cnt; } + +protected: + uint8_t _size; + uint8_t _cnt; + uint8_t _idx; + float _sum; + float * _ar; +}; + +#endif +// END OF FILE diff --git a/wlan_si/wlan_si.ino b/wlan_si/wlan_si.ino index 2c12d93..ffb9f4e 100644 --- a/wlan_si/wlan_si.ino +++ b/wlan_si/wlan_si.ino @@ -9,23 +9,10 @@ int pir = 6; // PIR on d6 // hardware based on https://dev.wlan-si.net/wiki/Telemetry/sensgw // original software https://github.com/SloMusti/sensgw -float runningAverage(float M) { - #define LM_SIZE 100 - static float LM[LM_SIZE]; // LastMeasurements - static byte index = 0; - static float sum = 0; - static byte count = 0; - - // keep sum updated to improve speed. - sum -= LM[index]; - LM[index] = M; - sum += LM[index]; - index++; - index = index % LM_SIZE; - if (count < LM_SIZE) count++; - - return sum / count; -} +#include "RunningAverage.h" + +RunningAverage pressureKPA_avg(100); +RunningAverage temperatureC_avg(100); void setup(void) { @@ -48,10 +35,12 @@ void loop(void) float pressureKPA = 0, temperatureC = 0; mpl115a2.getPT(&pressureKPA,&temperatureC); + pressureKPA_avg.addValue(pressureKPA); + temperatureC_avg.addValue(temperatureC); if ( count++ % 10 == 0 ) { - Serial.print("Pressure="); Serial.print(runningAverage(pressureKPA), 4); Serial.print(" kPa "); - Serial.print("Temp="); Serial.print(temperatureC, 1); Serial.print(" C"); + Serial.print("Pressure="); Serial.print(pressureKPA_avg.getAverage(), 4); Serial.print(" kPa "); + Serial.print("Temp="); Serial.print(temperatureC_avg.getAverage(), 2); Serial.print(" C"); Serial.print(" PIR="); Serial.print( digitalRead(pir) ); @@ -59,8 +48,6 @@ void loop(void) Serial.println(); - } else { - runningAverage(pressureKPA); } digitalWrite(led, LOW); -- 2.20.1