diff --git a/code/src/auto_tare.cpp b/code/src/auto_tare.cpp index 83bd2df..9ace7a6 100644 --- a/code/src/auto_tare.cpp +++ b/code/src/auto_tare.cpp @@ -7,9 +7,10 @@ #define STABLE_WEIGHT_DIFF 2.0f -bool isCloseTo(float tolerance, float a, float b) { return abs(a - b) <= tolerance; } +bool isCloseTo(float toleranceAbs, float a, float b) { return abs(a - b) <= toleranceAbs; } -AutoTare::AutoTare(float tolerance, float maxStdDev, uint16_t bufferSize) : tolerance(tolerance), maxStdDev(maxStdDev), buffer(bufferSize) +AutoTare::AutoTare(float toleranceG, float maxStdDevG, uint16_t bufferSize) + : tolerance(toleranceG), maxStdDev(maxStdDevG), buffer(bufferSize) { } @@ -27,26 +28,29 @@ void AutoTare::update(float rawWeight) if (buffer.standardDeviationLast(buffer.size()) < maxStdDev) { float avgWeight = buffer.averageLast(buffer.size()); - float diff = avgWeight - lastStableWeight; - // only tare if the diff is positive - if (diff > 0) + // only use stable weight if there is some siginificant diff + if (!isCloseTo(STABLE_WEIGHT_DIFF, lastStableWeight, avgWeight)) { - // if the diff is similar to any saved weight, tare - for (float weight : weights) + float diff = avgWeight - lastStableWeight; + + // only tare if the diff is positive + if (diff > 0) { - if (isCloseTo(tolerance * weight, weight, diff)) + LOGI(TAG, "got new diff: %f\n", diff); + + // if the diff is similar to any saved weight, tare + for (float weight : weights) { - isTare = true; + if (isCloseTo(tolerance, weight, diff)) + { + isTare = true; + } } } - } - // only set new stable weight if there is some siginificant diff - if (!isCloseTo(STABLE_WEIGHT_DIFF, lastStableWeight, avgWeight)) - { // set the last stable one to this one - LOGI(TAG, "new stable weight: %f\n", avgWeight); + LOGI(TAG, "new last stable weight: %f\n", avgWeight); lastStableWeight = avgWeight; } } diff --git a/code/src/auto_tare.h b/code/src/auto_tare.h index 19b0100..c315ec7 100644 --- a/code/src/auto_tare.h +++ b/code/src/auto_tare.h @@ -15,9 +15,9 @@ class AutoTare { bool shouldTare(); void update(float rawWeight); std::vector weights; -private: float tolerance; float maxStdDev; +private: RingBuffer buffer; bool isTare = false; diff --git a/code/src/data/localization.h b/code/src/data/localization.h index ef208ad..f658172 100644 --- a/code/src/data/localization.h +++ b/code/src/data/localization.h @@ -125,5 +125,6 @@ #define SETTINGS_TARE_2 "Auto-Tare Weight 3" #define SETTINGS_TARE_3 "Auto-Tare Weight 4" #define SETTINGS_TARE_4 "Auto-Tare Weight 5" +#define SETTINGS_TARE_TOLERANCE "Auto-Tare Tolerance" ////////////////////////////////////////////// #endif \ No newline at end of file diff --git a/code/src/embedded/main.cpp b/code/src/embedded/main.cpp index 5f4254a..0383ccd 100644 --- a/code/src/embedded/main.cpp +++ b/code/src/embedded/main.cpp @@ -109,6 +109,9 @@ void setup() Updater::update_firmware(); } + /// MODE MANAGER //// + modeManager.begin(); + ESP_LOGI(TAG, "Setup finished!"); } diff --git a/code/src/mode_manager.cpp b/code/src/mode_manager.cpp index 9449c40..afc18fa 100644 --- a/code/src/mode_manager.cpp +++ b/code/src/mode_manager.cpp @@ -9,6 +9,8 @@ ModeManager::ModeManager(Mode *modes[], const int modeCount) { } +void ModeManager::begin() { modes[currentMode]->enter(); } + void ModeManager::update() { if (inModeChange) diff --git a/code/src/mode_manager.h b/code/src/mode_manager.h index 31c4004..0bc0d1c 100644 --- a/code/src/mode_manager.h +++ b/code/src/mode_manager.h @@ -9,6 +9,7 @@ class ModeManager ModeManager(Mode *modes[], const int modeCount); ~ModeManager(){}; void update(); + void begin(); private: const int modeCount; diff --git a/code/src/modes/mode_scale.cpp b/code/src/modes/mode_scale.cpp index 02d258e..62ad893 100644 --- a/code/src/modes/mode_scale.cpp +++ b/code/src/modes/mode_scale.cpp @@ -1,9 +1,10 @@ #include "modes/mode_scale.h" -#include "weight_sensor.h" #include "data/localization.h" -#include "interface.h" #include "display.h" +#include "interface.h" +#include "logger.h" #include "settings.h" +#include "weight_sensor.h" void ModeScale::update() { @@ -32,6 +33,18 @@ void ModeScale::update() void ModeScale::enter() { // set correct values for auto tare autoTare->weights = Settings::getAllAutoTares(); + for (auto weight : autoTare->weights) + { + LOGI("Scale", "auto tare weights: %f\n", weight); + } + + float tolerance = Settings::getFloat(Settings::AUTO_TARE_TOLERANCE); + if (!isnan(tolerance)) + { + autoTare->tolerance = tolerance; + } + + LOGI("Scale", "auto tare tolerance: %f\n", autoTare->tolerance); } bool ModeScale::canSwitchMode() diff --git a/code/src/modes/mode_scale.h b/code/src/modes/mode_scale.h index 56cc318..ee9bcf6 100644 --- a/code/src/modes/mode_scale.h +++ b/code/src/modes/mode_scale.h @@ -19,5 +19,5 @@ class ModeScale : public Mode private: WeightSensor &weightSensor; Stopwatch &stopwatch; - AutoTare *autoTare = new AutoTare(0.1, 1, 16); // 1g std deviation + AutoTare *autoTare = new AutoTare(2, 1, 16); // 1g std deviation, 2g tolerance by default }; \ No newline at end of file diff --git a/code/src/settings.h b/code/src/settings.h index db3b90e..446c7e2 100644 --- a/code/src/settings.h +++ b/code/src/settings.h @@ -13,11 +13,12 @@ namespace Settings AUTO_TARE_2, AUTO_TARE_3, AUTO_TARE_4, + AUTO_TARE_TOLERANCE, FLOAT_SETTING_NUM }; static const char *floatSettingNames[] = { - SETTINGS_TARE_0, SETTINGS_TARE_1, SETTINGS_TARE_2, SETTINGS_TARE_3, SETTINGS_TARE_4, + SETTINGS_TARE_0, SETTINGS_TARE_1, SETTINGS_TARE_2, SETTINGS_TARE_3, SETTINGS_TARE_4, SETTINGS_TARE_TOLERANCE }; float getFloat(FloatSetting s); @@ -33,8 +34,8 @@ namespace Settings FloatSetting setting = static_cast(i); float value = getFloat(setting); - // only use values that arent NaN - if (!std::isnan(value)) + // only use values that arent NaN and greater than 1 + if (!std::isnan(value) && value > 1) { autoTares.push_back(value); } diff --git a/code/test/native/test_auto_tare/main.cpp b/code/test/native/test_auto_tare/main.cpp index 3b83b25..4767f7b 100644 --- a/code/test/native/test_auto_tare/main.cpp +++ b/code/test/native/test_auto_tare/main.cpp @@ -6,8 +6,8 @@ AutoTare *autoTare; void setUp(void) { - // tolerance 0.1 and std dev 1g, 5 buffer size - autoTare = new AutoTare(0.1, 1, 5); + // tolerance 1 and std dev 1g, 5 buffer size + autoTare = new AutoTare(1, 1, 5); // add a weight of 36g autoTare->weights.push_back(36.0); }