Skip to content

Commit

Permalink
EWA summarizer (in tests only).
Browse files Browse the repository at this point in the history
  • Loading branch information
dlyubimov committed Jul 1, 2017
1 parent c0a1696 commit 2a5cadd
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 5 deletions.
2 changes: 1 addition & 1 deletion Hydra_EVSE/Hydra_EVSE.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
#define SW_VERSION "2.4.1-dev"

// Define this for the basic unit tests run in a generica arduino uno board with a display shield.
//#define UNIT_TESTS
#define UNIT_TESTS

#define UINT_BITS (sizeof(unsigned int) << 3)
#define ULONG_BITS (sizeof(unsigned long) << 3)
Expand Down
2 changes: 1 addition & 1 deletion Hydra_EVSE/Hydra_EVSE.ino
Original file line number Diff line number Diff line change
Expand Up @@ -398,7 +398,7 @@ void displayStatus(unsigned int status)
display.print(carLetter); // 1
display.print(':'); // 2

logDebug(P("status: %x, CAR:%c, status bits: %x, status mask: %x."), status, carLetter, status & STATUS_MASK, STATUS_MASK);
logTrace(P("status: %x, CAR:%c, status bits: %x, status mask: %x."), status, carLetter, status & STATUS_MASK, STATUS_MASK);

switch ( status & STATUS_MASK) // 7
{
Expand Down
44 changes: 44 additions & 0 deletions Hydra_EVSE/onlineSum.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@

// Exponentially Weighted Average summarizer
// http://tdunning.blogspot.com/2011/03/exponentially-weighted-averaging-for.html
// http://weatheringthrutechdays.blogspot.com/2011/04/follow-up-for-mean-summarizer-post.html

// T is double or float
template <class T>
class EWASum {

T alpha;
T w, s, tn;

public:
// tHalfPeriod is amount of t after which the observation is exponentially weighed at 0.5 of the current observation.
EWASum(T tHalfPeriod) : alpha(-tHalfPeriod / log((T)0.5)) {
reset();
};

void reset() {
w = s = tn = 0;
}

void update(T x, T t);

// Evaluate EWA
T ewa();
};

template <class T>
void EWASum<T>::update(T x, T t) {
T pi = exp(tn - t) / alpha;
s += pi * s + x;
w += pi * w + 1;
if (t > tn) tn = t;
}

template <class T>
T EWASum<T>::ewa() {
return (w == 0) ? 0 : s / w;
}

// On arduino, double seems to be the same as float and take 4 bytes!
typedef EWASum<double> EWASumD;
typedef EWASum<float> EWASumF;
36 changes: 33 additions & 3 deletions Hydra_EVSE/units.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
///////////////////////////////////////////////////

#include "Hydra_EVSE.h"
#include "onlineSum.h"

// If unit tests are declared, this is called the last thing from setup().

Expand Down Expand Up @@ -59,7 +60,7 @@ static void showDS(char* desc, unsigned int status) {
display.clear();
display.print (desc);
displayStatus(status);
Delay(1000);
Delay(200);
}

void testDisplayStatus() {
Expand Down Expand Up @@ -95,8 +96,10 @@ void testDisplayStatus() {
if ( cars[0].carLetter() != 'A' || cars[1].carLetter() != 'B' )
logInfo(P("carLetter() UNIT FAIL"));

if ( cars[0].dispCol() != 0 || cars[1].dispCol() != 8 )
logInfo(P("dispCol() UNIT FAIL"));
if ( cars[0].dispCol() != 0 || cars[1].dispCol() != 8 )
logInfo(P("dispCol() UNIT FAIL"));

logInfo (P("displayStatus DONE."));
}


Expand All @@ -105,10 +108,37 @@ static void testMenuSetup() {
doMenu(true);
}

static void testEWASumSetup() {
// logInfo(P("sizeof(double)=%d"), sizeof(double));
// logInfo(P("sizeof(float)=%d"), sizeof(float));

EWASumD sum(100);

boolean fail = false;
sum.update(5, 1000);
if ( abs(sum.ewa() - 5.0) > 1e-10) fail = true;

sum.update(10, 1100);
if ( abs(sum.ewa() - 7.5) > 1e-10) fail = true;

// update-in-the-past test.
sum.reset();

sum.update(10, 1100);
if ( abs(sum.ewa() - 10) > 1e-10) fail = true;

sum.update(5, 1000);
if ( abs(sum.ewa() - 7.5) > 1e-10) fail = true;

if ( fail) logInfo(P("EWASum UNIT FAIL"));
else logInfo(P("EWASum DONE."));
}


int unitsSetup() {

testEepromSetup();
testEWASumSetup();
testDisplayStatus();
testMenuSetup();
return false;
Expand Down

0 comments on commit 2a5cadd

Please sign in to comment.