Skip to content

Commit

Permalink
Added config parameter for auto tare tolerance
Browse files Browse the repository at this point in the history
  • Loading branch information
orlopau committed Dec 14, 2024
1 parent 6d99e5e commit 29cbeec
Show file tree
Hide file tree
Showing 10 changed files with 48 additions and 23 deletions.
32 changes: 18 additions & 14 deletions code/src/auto_tare.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
}

Expand All @@ -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;
}
}
Expand Down
2 changes: 1 addition & 1 deletion code/src/auto_tare.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ class AutoTare {
bool shouldTare();
void update(float rawWeight);
std::vector<float> weights;
private:
float tolerance;
float maxStdDev;
private:
RingBuffer<float> buffer;
bool isTare = false;

Expand Down
1 change: 1 addition & 0 deletions code/src/data/localization.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
3 changes: 3 additions & 0 deletions code/src/embedded/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,9 @@ void setup()
Updater::update_firmware();
}

/// MODE MANAGER ////
modeManager.begin();

ESP_LOGI(TAG, "Setup finished!");
}

Expand Down
2 changes: 2 additions & 0 deletions code/src/mode_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ ModeManager::ModeManager(Mode *modes[], const int modeCount)
{
}

void ModeManager::begin() { modes[currentMode]->enter(); }

void ModeManager::update()
{
if (inModeChange)
Expand Down
1 change: 1 addition & 0 deletions code/src/mode_manager.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ class ModeManager
ModeManager(Mode *modes[], const int modeCount);
~ModeManager(){};
void update();
void begin();

private:
const int modeCount;
Expand Down
17 changes: 15 additions & 2 deletions code/src/modes/mode_scale.cpp
Original file line number Diff line number Diff line change
@@ -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()
{
Expand Down Expand Up @@ -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()
Expand Down
2 changes: 1 addition & 1 deletion code/src/modes/mode_scale.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
};
7 changes: 4 additions & 3 deletions code/src/settings.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -33,8 +34,8 @@ namespace Settings
FloatSetting setting = static_cast<FloatSetting>(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);
}
Expand Down
4 changes: 2 additions & 2 deletions code/test/native/test_auto_tare/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down

0 comments on commit 29cbeec

Please sign in to comment.