1
0
forked from me/IronOS

PID rework - use watts

This commit is contained in:
David P Hilton
2018-10-28 20:46:55 -06:00
parent febf55ad43
commit 76b460cd77
7 changed files with 188 additions and 132 deletions

View File

@@ -0,0 +1,41 @@
/*
* history.hpp
*
* Created on: 28 Oct, 2018
* Authors: Ben V. Brown, David Hilton
*/
#ifndef HISTORY_HPP_
#define HISTORY_HPP_
#include <stdint.h>
// max size = 127
template <class T=uint16_t, uint8_t SIZE=15>
struct history {
static const uint8_t size = SIZE;
T buf[size];
int32_t sum;
uint8_t loc;
void update(T const val) {
// step backwards so i+1 is the previous value.
loc = (size+loc-1) % size;
sum -= buf[loc];
sum += val;
buf[loc] = val;
}
T operator[] (uint8_t i) const {
// 0 = newest, size-1 = oldest.
i = (i+loc) % size;
return buf[i];
}
T average() const {
return sum / size;
}
};
#endif /* HISTORY_HPP_ */