11#include " ScaleStabilizer.h"
22
3+ /* !
4+ * @brief Construct a new Scale Stabilizer:: S Cale Stabilizer object
5+ */
6+ ScaleStabilizer::SCaleStabilizer ()
7+ {
8+ }
9+
10+ ScaleStabilizer::~ScaleStabilizer ()
11+ {
12+ delete _window;
13+ }
14+
315/* !
416 * @brief simple average calculation
517 */
6- double ScaleStabilizer::getAvg () {
18+ double ScaleStabilizer::getAvg ()
19+ {
720 double sum = 0 ;
8-
9- for (int i = 0 ; i < _windowSize; i++) {
21+
22+ for (int i = 0 ; i < _windowSize; i++)
23+ {
1024 sum += _window[i];
1125 }
1226
@@ -16,9 +30,12 @@ double ScaleStabilizer::getAvg() {
1630/* !
1731 * @brief checks if all the elements in the moving _window are the same
1832 */
19- bool ScaleStabilizer::isBufferMonotone () {
20- for (int i = 0 ; i < _windowSize; i++) {
21- if (_window[i] != _window[0 ]) {
33+ bool ScaleStabilizer::isBufferMonotone ()
34+ {
35+ for (int i = 0 ; i < _windowSize; i++)
36+ {
37+ if (_window[i] != _window[0 ])
38+ {
2239 break ;
2340 }
2441 }
@@ -27,7 +44,8 @@ bool ScaleStabilizer::isBufferMonotone() {
2744/* !
2845 * @brief init of the stabilizer
2946 */
30- void ScaleStabilizer::begin (int windowSize, double weightThreshold) {
47+ void ScaleStabilizer::begin (int windowSize, double weightThreshold)
48+ {
3149 _windowSize = windowSize;
3250 _weightThreshold = weightThreshold;
3351 _window = new double [_windowSize];
@@ -40,26 +58,30 @@ void ScaleStabilizer::begin(int windowSize, double weightThreshold) {
4058 * @brief add a new ADC reading to the moving _window by replacing the olest one
4159 * but if the moving _window is empty or needs to be overwritten, force overwrite is executed
4260 */
43- void ScaleStabilizer::add (double reading) {
44- if (_forceOverwrite > 0 || isBufferMonotone ()) {
61+ void ScaleStabilizer::add (double reading)
62+ {
63+ if (_forceOverwrite > 0 || isBufferMonotone ())
64+ {
4565 int oldestReadingIdx = (_currentReadingIdx + 1 ) % _windowSize;
4666 _window[oldestReadingIdx] = reading;
4767 _currentReadingIdx = oldestReadingIdx;
4868 _forceOverwrite--;
4969 return ;
5070 }
5171
52- if (abs (reading - getAvg ()) < _weightThreshold) {
72+ if (abs (reading - getAvg ()) < _weightThreshold)
73+ {
5374 int oldestReadingIdx = (_currentReadingIdx + 1 ) % _windowSize;
5475 _window[oldestReadingIdx] = reading;
5576 _currentReadingIdx = oldestReadingIdx;
5677 return ;
5778 }
5879
59- // reading index just before the last one
60- int nextToLastReadingIdx = _currentReadingIdx - 1 < 0 ? _windowSize : (_currentReadingIdx - 1 ) % _windowSize;
80+ // reading index just before the last one
81+ int nextToLastReadingIdx = _currentReadingIdx - 1 < 0 ? _windowSize : (_currentReadingIdx - 1 ) % _windowSize;
6182
62- if (abs (_window[nextToLastReadingIdx] - getAvg ()) > _weightThreshold) {
83+ if (abs (_window[nextToLastReadingIdx] - getAvg ()) > _weightThreshold)
84+ {
6385 // replace 75% of the data in the filter _window with fresh readings
6486 _forceOverwrite = _windowSize * 0.75 ;
6587 }
@@ -70,23 +92,29 @@ void ScaleStabilizer::add(double reading) {
7092/* !
7193 * @brief returns the stabilized average reading from the moving _window
7294 */
73- double ScaleStabilizer::getStablilizedReading (double displayResolution, int decimalPlaces) {
95+ double ScaleStabilizer::getStablilizedReading (double displayResolution, int decimalPlaces)
96+ {
7497 // find min and max values
7598 int minIdx = 0 ;
7699 int maxIdx = 0 ;
77- for (int i = 0 ; i < _windowSize; i++) {
78- if (_window[i] < minIdx) {
100+ for (int i = 0 ; i < _windowSize; i++)
101+ {
102+ if (_window[i] < minIdx)
103+ {
79104 minIdx = i;
80105 }
81- if (_window[i] > maxIdx) {
106+ if (_window[i] > maxIdx)
107+ {
82108 maxIdx = i;
83109 }
84110 }
85111
86112 // remove min and max from measurements
87113 double sum = 0 ;
88- for (int i = 0 ; i < _windowSize; i++) {
89- if (i == minIdx || i == maxIdx) {
114+ for (int i = 0 ; i < _windowSize; i++)
115+ {
116+ if (i == minIdx || i == maxIdx)
117+ {
90118 continue ;
91119 }
92120 sum += _window[i];
@@ -96,7 +124,8 @@ double ScaleStabilizer::getStablilizedReading(double displayResolution, int deci
96124 double cleanedAverage = sum / (_windowSize - 2 );
97125
98126 // remove negative numbers and set to 0 if close to 0
99- if (cleanedAverage < 0.5 ) {
127+ if (cleanedAverage < 0.5 )
128+ {
100129 cleanedAverage = 0.0 ;
101130 }
102131
@@ -105,11 +134,13 @@ double ScaleStabilizer::getStablilizedReading(double displayResolution, int deci
105134 cleanedAverage = round (cleanedAverage * scale) / scale;
106135
107136 // perform the output flicker reduction
108- if (_lastOutputValue == cleanedAverage) {
137+ if (_lastOutputValue == cleanedAverage)
138+ {
109139 return cleanedAverage;
110140 }
111-
112- if (abs (cleanedAverage - _lastOutputValue) < displayResolution) {
141+
142+ if (abs (cleanedAverage - _lastOutputValue) < displayResolution)
143+ {
113144 return _lastOutputValue; // probably was just noise - return the old measurement
114145 }
115146
0 commit comments