Compare commits

..

3 Commits

Author SHA1 Message Date
83f2f0834d
Merge pull request #15 from ManuelW77/main
set to main state
2025-03-03 17:10:35 +01:00
6632aa8f95
Merge pull request #14 from janecker/scale-calibration-rework
Reworks the scale calibration handling
2025-03-03 17:08:06 +01:00
Jan Philipp Ecker
d434fde92e Reworks the scale calibration handling
Fixes some issues in the scale handling. Prevents a wdg reset after
after scale calibration. Also makes sure that after calibration all
tasks are started again that have been suspsended before.
2025-03-03 16:50:46 +01:00
3 changed files with 71 additions and 56 deletions

View File

@ -13,6 +13,9 @@
#include "esp_task_wdt.h" #include "esp_task_wdt.h"
#include "commonFS.h" #include "commonFS.h"
bool mainTaskWasPaused = 0;
uint8_t scaleTareCounter = 0;
// ##### SETUP ##### // ##### SETUP #####
void setup() { void setup() {
Serial.begin(115200); Serial.begin(115200);
@ -45,22 +48,7 @@ void setup() {
// NFC Reader // NFC Reader
startNfc(); startNfc();
uint8_t scaleCalibrated = start_scale(); start_scale();
if (scaleCalibrated == 3) {
oledShowMessage("Scale not calibrated!");
for (uint16_t i = 0; i < 50000; i++) {
yield();
vTaskDelay(pdMS_TO_TICKS(1));
esp_task_wdt_reset();
}
} else if (scaleCalibrated == 0) {
oledShowMessage("HX711 not found");
for (uint16_t i = 0; i < 50000; i++) {
yield();
vTaskDelay(pdMS_TO_TICKS(1));
esp_task_wdt_reset();
}
}
// WDT initialisieren mit 10 Sekunden Timeout // WDT initialisieren mit 10 Sekunden Timeout
bool panic = true; // Wenn true, löst ein WDT-Timeout einen System-Panik aus bool panic = true; // Wenn true, löst ein WDT-Timeout einen System-Panik aus
@ -68,9 +56,6 @@ void setup() {
// Aktuellen Task (loopTask) zum Watchdog hinzufügen // Aktuellen Task (loopTask) zum Watchdog hinzufügen
esp_task_wdt_add(NULL); esp_task_wdt_add(NULL);
// Optional: Andere Tasks zum Watchdog hinzufügen, falls nötig
// esp_task_wdt_add(task_handle);
} }
@ -147,10 +132,18 @@ void loop() {
} }
// Ausgabe der Waage auf Display // Ausgabe der Waage auf Display
if (pauseMainTask == 0 && weight != lastWeight && hasReadRfidTag == 0 && (!autoSendToBambu || autoSetToBambuSpoolId == 0)) if(pauseMainTask == 0)
{
if (mainTaskWasPaused || (weight != lastWeight && hasReadRfidTag == 0 && (!autoSendToBambu || autoSetToBambuSpoolId == 0)))
{ {
(weight < 2) ? ((weight < -2) ? oledShowMessage("!! -0") : oledShowWeight(0)) : oledShowWeight(weight); (weight < 2) ? ((weight < -2) ? oledShowMessage("!! -0") : oledShowWeight(0)) : oledShowWeight(weight);
} }
mainTaskWasPaused = false;
}
else
{
mainTaskWasPaused = true;
}
// Wenn Timer abgelaufen und nicht gerade ein RFID-Tag geschrieben wird // Wenn Timer abgelaufen und nicht gerade ein RFID-Tag geschrieben wird
@ -161,11 +154,20 @@ void loop() {
// Prüfen ob die Waage korrekt genullt ist // Prüfen ob die Waage korrekt genullt ist
if ((weight > 0 && weight < 5) || weight < 0) if ((weight > 0 && weight < 5) || weight < 0)
{ {
scale_tare_counter++; if(scaleTareCounter < 5)
{
scaleTareCounter++;
} }
else else
{ {
scale_tare_counter = 0; scaleTareRequest = true;
scaleTareCounter = 0;
}
}
else
{
scaleTareCounter = 0;
} }
// Prüfen ob das Gewicht gleich bleibt und dann senden // Prüfen ob das Gewicht gleich bleibt und dann senden
@ -210,6 +212,5 @@ void loop() {
} }
} }
yield();
esp_task_wdt_reset(); esp_task_wdt_reset();
} }

View File

@ -14,7 +14,7 @@ TaskHandle_t ScaleTask;
int16_t weight = 0; int16_t weight = 0;
uint8_t weigthCouterToApi = 0; uint8_t weigthCouterToApi = 0;
uint8_t scale_tare_counter = 0; bool scaleTareRequest = false;
uint8_t pauseMainTask = 0; uint8_t pauseMainTask = 0;
uint8_t scaleCalibrated = 1; uint8_t scaleCalibrated = 1;
@ -34,14 +34,16 @@ void scale_loop(void * parameter) {
Serial.println("++++++++++++++++++++++++++++++"); Serial.println("++++++++++++++++++++++++++++++");
Serial.println("Scale Loop started"); Serial.println("Scale Loop started");
Serial.println("++++++++++++++++++++++++++++++"); Serial.println("++++++++++++++++++++++++++++++");
for(;;) { for(;;) {
if (scale.is_ready()) if (scale.is_ready())
{ {
// Waage nochmal Taren, wenn zu lange Abweichung // Waage nochmal Taren, wenn zu lange Abweichung
if (scale_tare_counter >= 5) if (scaleTareRequest == true)
{ {
Serial.println("Re-Tare scale");
scale.tare(); scale.tare();
scale_tare_counter = 0; scaleTareRequest = false;
} }
weight = round(scale.get_units()); weight = round(scale.get_units());
@ -51,13 +53,13 @@ void scale_loop(void * parameter) {
} }
} }
uint8_t start_scale() { void start_scale() {
Serial.println("Prüfe Calibration Value"); Serial.println("Prüfe Calibration Value");
long calibrationValue; float calibrationValue;
// NVS lesen // NVS lesen
preferences.begin(NVS_NAMESPACE, true); // true = readonly preferences.begin(NVS_NAMESPACE, true); // true = readonly
calibrationValue = preferences.getLong(NVS_KEY_CALIBRATION, defaultScaleCalibrationValue); calibrationValue = preferences.getFloat(NVS_KEY_CALIBRATION, defaultScaleCalibrationValue);
preferences.end(); preferences.end();
Serial.print("Read Scale Calibration Value "); Serial.print("Read Scale Calibration Value ");
@ -68,6 +70,13 @@ uint8_t start_scale() {
if (isnan(calibrationValue) || calibrationValue < 1) { if (isnan(calibrationValue) || calibrationValue < 1) {
calibrationValue = defaultScaleCalibrationValue; calibrationValue = defaultScaleCalibrationValue;
scaleCalibrated = 0; scaleCalibrated = 0;
oledShowMessage("Scale not calibrated!");
for (uint16_t i = 0; i < 50000; i++) {
yield();
vTaskDelay(pdMS_TO_TICKS(1));
esp_task_wdt_reset();
}
} }
oledShowMessage("Scale Tare Please remove all"); oledShowMessage("Scale Tare Please remove all");
@ -101,21 +110,21 @@ uint8_t start_scale() {
} else { } else {
Serial.println("ScaleLoop-Task erfolgreich erstellt"); Serial.println("ScaleLoop-Task erfolgreich erstellt");
} }
return (scaleCalibrated == 1) ? 1 : 3;
} }
uint8_t calibrate_scale() { uint8_t calibrate_scale() {
long newCalibrationValue; uint8_t returnState = 0;
float newCalibrationValue;
vTaskSuspend(RfidReaderTask);
vTaskSuspend(ScaleTask);
//vTaskSuspend(RfidReaderTask);
vTaskDelete(RfidReaderTask);
vTaskDelete(ScaleTask);
pauseBambuMqttTask = true; pauseBambuMqttTask = true;
pauseMainTask = 1; pauseMainTask = 1;
if (scale.wait_ready_timeout(1000)) if (scale.wait_ready_timeout(1000))
{ {
scale.set_scale(); scale.set_scale();
oledShowMessage("Step 1 empty Scale"); oledShowMessage("Step 1 empty Scale");
@ -137,7 +146,7 @@ uint8_t calibrate_scale() {
esp_task_wdt_reset(); esp_task_wdt_reset();
} }
long newCalibrationValue = scale.get_units(10); float newCalibrationValue = scale.get_units(10);
Serial.print("Result: "); Serial.print("Result: ");
Serial.println(newCalibrationValue); Serial.println(newCalibrationValue);
@ -150,35 +159,43 @@ uint8_t calibrate_scale() {
// Speichern mit NVS // Speichern mit NVS
preferences.begin(NVS_NAMESPACE, false); // false = readwrite preferences.begin(NVS_NAMESPACE, false); // false = readwrite
preferences.putLong(NVS_KEY_CALIBRATION, newCalibrationValue); preferences.putFloat(NVS_KEY_CALIBRATION, newCalibrationValue);
preferences.end(); preferences.end();
// Verifizieren // Verifizieren
preferences.begin(NVS_NAMESPACE, true); preferences.begin(NVS_NAMESPACE, true);
long verifyValue = preferences.getLong(NVS_KEY_CALIBRATION, 0); float verifyValue = preferences.getFloat(NVS_KEY_CALIBRATION, 0);
preferences.end(); preferences.end();
Serial.print("Verified stored value: "); Serial.print("Verified stored value: ");
Serial.println(verifyValue); Serial.println(verifyValue);
Serial.println("End calibration, revome weight"); Serial.println("End calibration, remove weight");
oledShowMessage("Remove weight"); oledShowMessage("Remove weight");
scale.set_scale(newCalibrationValue);
for (uint16_t i = 0; i < 2000; i++) { for (uint16_t i = 0; i < 2000; i++) {
yield(); yield();
vTaskDelay(pdMS_TO_TICKS(1)); vTaskDelay(pdMS_TO_TICKS(1));
esp_task_wdt_reset(); esp_task_wdt_reset();
} }
oledShowMessage("Calibration done"); oledShowMessage("Scale calibrated");
// For some reason it is not possible to re-tare the scale here, it will result in a wdt timeout. Instead let the scale loop do the taring
//scale.tare();
scaleTareRequest = true;
for (uint16_t i = 0; i < 2000; i++) { for (uint16_t i = 0; i < 2000; i++) {
yield(); yield();
vTaskDelay(pdMS_TO_TICKS(1)); vTaskDelay(pdMS_TO_TICKS(1));
esp_task_wdt_reset(); esp_task_wdt_reset();
} }
returnState = 1;
} }
else else
{ {
{ {
@ -191,7 +208,7 @@ uint8_t calibrate_scale() {
vTaskDelay(pdMS_TO_TICKS(1)); vTaskDelay(pdMS_TO_TICKS(1));
esp_task_wdt_reset(); esp_task_wdt_reset();
} }
return 0; returnState = 0;
} }
} }
} }
@ -206,16 +223,13 @@ uint8_t calibrate_scale() {
vTaskDelay(pdMS_TO_TICKS(1)); vTaskDelay(pdMS_TO_TICKS(1));
esp_task_wdt_reset(); esp_task_wdt_reset();
} }
return 0; returnState = 0;
} }
oledShowMessage("Scale Ready"); vTaskResume(RfidReaderTask);
vTaskResume(ScaleTask);
Serial.println("restart Scale Task");
start_scale();
pauseBambuMqttTask = false; pauseBambuMqttTask = false;
pauseMainTask = 0; pauseMainTask = 0;
return 1; return returnState;
} }

View File

@ -12,7 +12,7 @@ uint8_t tareScale();
extern HX711 scale; extern HX711 scale;
extern int16_t weight; extern int16_t weight;
extern uint8_t weigthCouterToApi; extern uint8_t weigthCouterToApi;
extern uint8_t scale_tare_counter; extern uint8_t scaleTareRequest;
extern uint8_t pauseMainTask; extern uint8_t pauseMainTask;
extern uint8_t scaleCalibrated; extern uint8_t scaleCalibrated;