|
| 1 | +#include "ScaleStabilizer.h" |
| 2 | + |
| 3 | +/*! |
| 4 | + * @brief simple average calculation |
| 5 | + */ |
| 6 | +double ScaleStabilizer::getAvg() { |
| 7 | + double sum = 0; |
| 8 | + |
| 9 | + for (int i = 0; i < _windowSize; i++) { |
| 10 | + sum += _window[i]; |
| 11 | + } |
| 12 | + |
| 13 | + return sum / _windowSize; |
| 14 | +} |
| 15 | + |
| 16 | +/*! |
| 17 | + * @brief checks if all the elements in the moving _window are the same |
| 18 | + */ |
| 19 | +bool ScaleStabilizer::isBufferMonotone() { |
| 20 | + for (int i = 0; i < _windowSize; i++) { |
| 21 | + if (_window[i] != _window[0]) { |
| 22 | + break; |
| 23 | + } |
| 24 | + } |
| 25 | +} |
| 26 | + |
| 27 | +/*! |
| 28 | + * @brief init of the stabilizer |
| 29 | + */ |
| 30 | +void ScaleStabilizer::begin(int windowSize, double weightThreshold) { |
| 31 | + _windowSize = windowSize; |
| 32 | + _weightThreshold = weightThreshold; |
| 33 | + _window = new double[_windowSize]; |
| 34 | + _forceOverwrite = _windowSize; |
| 35 | + _currentReadingIdx = 0; |
| 36 | + _lastOutputValue = 0.0; |
| 37 | +} |
| 38 | + |
| 39 | +/*! |
| 40 | + * @brief add a new ADC reading to the moving _window by replacing the olest one |
| 41 | + * but if the moving _window is empty or needs to be overwritten, force overwrite is executed |
| 42 | + */ |
| 43 | +void ScaleStabilizer::add(double reading) { |
| 44 | + if (_forceOverwrite > 0 || isBufferMonotone()) { |
| 45 | + int oldestReadingIdx = (_currentReadingIdx + 1) % _windowSize; |
| 46 | + _window[oldestReadingIdx] = reading; |
| 47 | + _currentReadingIdx = oldestReadingIdx; |
| 48 | + _forceOverwrite--; |
| 49 | + return; |
| 50 | + } |
| 51 | + |
| 52 | + if (abs(reading - getAvg()) < _weightThreshold) { |
| 53 | + int oldestReadingIdx = (_currentReadingIdx + 1) % _windowSize; |
| 54 | + _window[oldestReadingIdx] = reading; |
| 55 | + _currentReadingIdx = oldestReadingIdx; |
| 56 | + return; |
| 57 | + } |
| 58 | + |
| 59 | + // reading index just before the last one |
| 60 | + int nextToLastReadingIdx = _currentReadingIdx - 1 < 0 ? _windowSize : (_currentReadingIdx - 1) % _windowSize; |
| 61 | + |
| 62 | + if (abs(_window[nextToLastReadingIdx] - getAvg()) > _weightThreshold) { |
| 63 | + // replace 75% of the data in the filter _window with fresh readings |
| 64 | + _forceOverwrite = _windowSize * 0.75; |
| 65 | + } |
| 66 | + |
| 67 | + // everything else is probably just noise |
| 68 | +} |
| 69 | + |
| 70 | +/*! |
| 71 | + * @brief returns the stabilized average reading from the moving _window |
| 72 | + */ |
| 73 | +double ScaleStabilizer::getStablilizedReading(double displayResolution, int decimalPlaces) { |
| 74 | + // find min and max values |
| 75 | + int minIdx = 0; |
| 76 | + int maxIdx = 0; |
| 77 | + for (int i = 0; i < _windowSize; i++) { |
| 78 | + if (_window[i] < minIdx) { |
| 79 | + minIdx = i; |
| 80 | + } |
| 81 | + if (_window[i] > maxIdx) { |
| 82 | + maxIdx = i; |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + // remove min and max from measurements |
| 87 | + double sum = 0; |
| 88 | + for (int i = 0; i < _windowSize; i++) { |
| 89 | + if (i == minIdx || i == maxIdx) { |
| 90 | + continue; |
| 91 | + } |
| 92 | + sum += _window[i]; |
| 93 | + } |
| 94 | + |
| 95 | + // calc cleaned average |
| 96 | + double cleanedAverage = sum / (_windowSize - 2); |
| 97 | + |
| 98 | + // remove negative numbers and set to 0 if close to 0 |
| 99 | + if (cleanedAverage < 0.5) { |
| 100 | + cleanedAverage = 0.0; |
| 101 | + } |
| 102 | + |
| 103 | + // round to given decimal places |
| 104 | + float scale = pow(10, decimalPlaces); |
| 105 | + cleanedAverage = round(cleanedAverage * scale) / scale; |
| 106 | + |
| 107 | + // perform the output flicker reduction |
| 108 | + if (_lastOutputValue == cleanedAverage) { |
| 109 | + return cleanedAverage; |
| 110 | + } |
| 111 | + |
| 112 | + if(abs(cleanedAverage - _lastOutputValue) < displayResolution) { |
| 113 | + return _lastOutputValue; // probably was just noise - return the old measurement |
| 114 | + } |
| 115 | + |
| 116 | + _lastOutputValue = cleanedAverage; |
| 117 | + return cleanedAverage; |
| 118 | +} |
0 commit comments