Browse Source

Dynamically allocate memory for MedianFilter

fastled
Xose Pérez 7 years ago
parent
commit
f8fc2723da
3 changed files with 29 additions and 33 deletions
  1. +0
    -7
      code/espurna/config/general.h
  2. +25
    -22
      code/espurna/power.h
  3. +4
    -4
      code/espurna/power.ino

+ 0
- 7
code/espurna/config/general.h View File

@ -556,7 +556,6 @@ PROGMEM const char* const custom_reset_string[] = {
#define POWER_VOLTAGE 230 // Default voltage #define POWER_VOLTAGE 230 // Default voltage
#define POWER_READ_INTERVAL 6000 // Default reading interval (6 seconds) #define POWER_READ_INTERVAL 6000 // Default reading interval (6 seconds)
#define POWER_REPORT_INTERVAL 60000 // Default report interval (1 minute) #define POWER_REPORT_INTERVAL 60000 // Default report interval (1 minute)
#define POWER_REPORT_BUFFER 12 // Default buffer size ()
#define POWER_CURRENT_DECIMALS 2 // Decimals for current values #define POWER_CURRENT_DECIMALS 2 // Decimals for current values
#define POWER_VOLTAGE_DECIMALS 0 // Decimals for voltage values #define POWER_VOLTAGE_DECIMALS 0 // Decimals for voltage values
#define POWER_POWER_DECIMALS 0 // Decimals for power values #define POWER_POWER_DECIMALS 0 // Decimals for power values
@ -596,9 +595,6 @@ PROGMEM const char* const custom_reset_string[] = {
#if POWER_PROVIDER == POWER_PROVIDER_V9261F #if POWER_PROVIDER == POWER_PROVIDER_V9261F
#undef POWER_REPORT_BUFFER
#define POWER_REPORT_BUFFER 60 // Override median buffer size
#ifndef V9261F_PIN #ifndef V9261F_PIN
#define V9261F_PIN 2 // TX pin from the V9261F #define V9261F_PIN 2 // TX pin from the V9261F
#endif #endif
@ -619,9 +615,6 @@ PROGMEM const char* const custom_reset_string[] = {
#if POWER_PROVIDER == POWER_PROVIDER_ECH1560 #if POWER_PROVIDER == POWER_PROVIDER_ECH1560
#undef POWER_REPORT_BUFFER
#define POWER_REPORT_BUFFER 60 // Override median buffer size
#ifndef ECH1560_CLK_PIN #ifndef ECH1560_CLK_PIN
#define ECH1560_CLK_PIN 4 // Default CLK pin #define ECH1560_CLK_PIN 4 // Default CLK pin
#endif #endif


+ 25
- 22
code/espurna/power.h View File

@ -8,36 +8,41 @@ class MedianFilter {
public: public:
MedianFilter(unsigned char size) {
_size = size;
_data = new double[_size+1];
reset();
MedianFilter() {
_data = new std::vector<double>();
}
~MedianFilter() {
if (_data) delete _data;
} }
virtual void reset() { virtual void reset() {
if (_pointer > 1) _data[0] = _data[_pointer-1];
_pointer = 1;
for (unsigned char i=1; i<=_size; i++) _data[i] = 0;
double last = _data->empty() ? 0 : _data->back();
_data->clear();
add(last);
} }
virtual void add(double value) { virtual void add(double value) {
if (_pointer <= _size) {
_data[_pointer] = value;
++_pointer;
}
_data->push_back(value);
} }
virtual double average(bool do_reset = false) {
virtual double median(bool do_reset = false) {
double sum = 0; double sum = 0;
if (_pointer > 2) {
if (_data->size() == 1) {
sum = _data->back();
} else if (_data->size() == 2) {
sum = _data->front();
} else if (_data->size() > 2) {
for (unsigned char i = 1; i<_pointer-1; i++) {
for (unsigned char i = 1; i <= _data->size() - 2; i++) {
double previous = _data[i-1];
double current = _data[i];
double next = _data[i+1];
double previous = _data->at(i-1);
double current = _data->at(i);
double next = _data->at(i+1);
if (previous > current) std::swap(previous, current); if (previous > current) std::swap(previous, current);
if (current > next) std::swap(current, next); if (current > next) std::swap(current, next);
@ -47,7 +52,7 @@ class MedianFilter {
} }
sum /= (_pointer - 2);
sum /= (_data->size() - 2);
} }
@ -58,13 +63,11 @@ class MedianFilter {
} }
virtual unsigned char count() { virtual unsigned char count() {
return _pointer - 1;
return _data->size();
} }
private: private:
double *_data;
unsigned char _size = 0;
unsigned char _pointer = 0;
std::vector<double> *_data;
}; };

+ 4
- 4
code/espurna/power.ino View File

@ -24,15 +24,15 @@ double _power_current = 0;
double _power_voltage = 0; double _power_voltage = 0;
double _power_apparent = 0; double _power_apparent = 0;
double _power_energy = 0; double _power_energy = 0;
MedianFilter _filter_current = MedianFilter(POWER_REPORT_BUFFER);
MedianFilter _filter_current = MedianFilter();
#if POWER_HAS_ACTIVE #if POWER_HAS_ACTIVE
double _power_active = 0; double _power_active = 0;
double _power_reactive = 0; double _power_reactive = 0;
double _power_factor = 0; double _power_factor = 0;
MedianFilter _filter_voltage = MedianFilter(POWER_REPORT_BUFFER);
MedianFilter _filter_active = MedianFilter(POWER_REPORT_BUFFER);
MedianFilter _filter_apparent = MedianFilter(POWER_REPORT_BUFFER);
MedianFilter _filter_voltage = MedianFilter();
MedianFilter _filter_active = MedianFilter();
MedianFilter _filter_apparent = MedianFilter();
#endif #endif
#if POWER_HAS_ENERGY #if POWER_HAS_ENERGY


Loading…
Cancel
Save