From da6b23d78ea842ed7dab40358c4ef8e471639359 Mon Sep 17 00:00:00 2001 From: Hector van der Aa Date: Mon, 30 Mar 2026 16:29:16 +0200 Subject: [PATCH] Engine temp warning messages and bug fixes --- src/base/task.h | 2 + src/data/general_store.cpp | 4 +- src/main.cpp | 6 +++ src/modules/battery/battery.cpp | 34 +++++++-------- src/modules/cmd/cmd.cpp | 2 +- src/modules/lcd/lcd.cpp | 47 ++++++++++++++++++-- src/modules/lcd/lcd.h | 4 ++ src/modules/thermocouple/thermocouple.cpp | 52 ++++++++++++++++++++--- src/modules/thermocouple/thermocouple.h | 4 ++ 9 files changed, 126 insertions(+), 29 deletions(-) diff --git a/src/base/task.h b/src/base/task.h index 217af0f..4b430d9 100644 --- a/src/base/task.h +++ b/src/base/task.h @@ -31,6 +31,8 @@ enum Type : uint8_t { DisplayMsgTrackDetectOk, DisplayMsgConfigNoTracks, DisplayMsgBatteryLow, + DisplayMsgEngineTempLow, + DisplayMsgEngineTempHigh, ConfigTrackDetect, ConfigWriteTempTrack, ConfigTrackDelete, diff --git a/src/data/general_store.cpp b/src/data/general_store.cpp index 05dca47..75bd20c 100644 --- a/src/data/general_store.cpp +++ b/src/data/general_store.cpp @@ -15,9 +15,9 @@ } void tengGlobalRead(double& out) { - out = vbat_global; + out = teng_global; } void tengGlobalWrite(const double& in) { - vbat_global = in; + teng_global = in; } diff --git a/src/main.cpp b/src/main.cpp index 3783f2f..d7db0d1 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -14,6 +14,7 @@ #include "base/module_base.h" #include "base/router.h" #include "modules/battery/battery.h" +#include "modules/thermocouple/thermocouple.h" SystemLogger *logger = new SystemLogger(&Serial); @@ -23,6 +24,8 @@ Gps *gps_module = new Gps(&Serial2, logger); Config *system_config = new Config(logger); Cmd *command_handler = new Cmd(&Serial, logger); Battery *battery_module = new Battery(logger); +Thermocouple *thermocouple_module = new Thermocouple(logger); + void setup() { @@ -33,6 +36,7 @@ void setup() { module_registry[module::Config] = system_config; module_registry[module::Cmd] = command_handler; module_registry[module::Battery] = battery_module; + module_registry[module::Thermocouple] = thermocouple_module; display->init(); display->printMessage("Starting Initialization"); @@ -62,6 +66,7 @@ void setup() { display->printMessage("Sensors Init..."); battery_module->init(); + thermocouple_module->init(); delay(1000); display->printMessage("Sensors Init Complete"); delay(1000); @@ -74,4 +79,5 @@ void loop() { command_handler->parseTask(); system_config->loop(); battery_module->loop(); + thermocouple_module->loop(); } diff --git a/src/modules/battery/battery.cpp b/src/modules/battery/battery.cpp index 325a4f8..23ec473 100644 --- a/src/modules/battery/battery.cpp +++ b/src/modules/battery/battery.cpp @@ -44,10 +44,11 @@ int Battery::loop(unsigned long timeout_ms) { vbatGlobalWrite(vbat_); if (vbat_ < low_threshold_) { - if (warning_sent_at_ == 0 || millis() > warning_sent_at_ + warning_timeout_) { - router::send(module::Lcd, task::DisplayMsgBatteryLow, 2000); - warning_sent_at_ = millis(); - } + if (warning_sent_at_ == 0 || + millis() > warning_sent_at_ + warning_timeout_) { + router::send(module::Lcd, task::DisplayMsgBatteryLow, 2000); + warning_sent_at_ = millis(); + } } last_read_at_ = millis(); } @@ -66,19 +67,18 @@ int Battery::loop(unsigned long timeout_ms) { break; } } else if (active_task.target_ == module::All) { - switch (active_task.type_) - { - case task::AllConfigUpdated: { - VehicleConfig config; - configGlobalRead(config); - calibration_ = config.vbat_calibration_; - low_threshold_ = config.vbat_low_; - break; - } - - default: - break; - } + switch (active_task.type_) { + case task::AllConfigUpdated: { + VehicleConfig config; + configGlobalRead(config); + calibration_ = config.vbat_calibration_; + low_threshold_ = config.vbat_low_; + break; + } + + default: + break; + } } } return 0; diff --git a/src/modules/cmd/cmd.cpp b/src/modules/cmd/cmd.cpp index f1764c5..bd06f84 100644 --- a/src/modules/cmd/cmd.cpp +++ b/src/modules/cmd/cmd.cpp @@ -483,7 +483,7 @@ int Cmd::handleThermoSetHigh(unsigned short argc, char* argv[]) { logger_->info("Setting high level for TENG"); } #endif - router::send(module::Config, task::ConfigTengSetLow, task_data); + router::send(module::Config, task::ConfigTengSetHigh, task_data); return 0; } diff --git a/src/modules/lcd/lcd.cpp b/src/modules/lcd/lcd.cpp index 826fa3c..2c7f29d 100644 --- a/src/modules/lcd/lcd.cpp +++ b/src/modules/lcd/lcd.cpp @@ -62,6 +62,8 @@ bool Lcd::isMessageTask(task::Type type) { case task::DisplayMsgTrackDetectOk: case task::DisplayMsgConfigNoTracks: case task::DisplayMsgBatteryLow: + case task::DisplayMsgEngineTempLow: + case task::DisplayMsgEngineTempHigh: return true; default: @@ -151,6 +153,8 @@ int Lcd::renderDriverPrimary() { double vbat; vbatGlobalRead(vbat); + double teng; + tengGlobalRead(teng); display_->setCursor(0,0); this->print("GPS:"); @@ -165,8 +169,12 @@ int Lcd::renderDriverPrimary() { this->print(gps_data.speed_.value_); display_->setCursor(0,3); - this->print("VBAT:"); + this->print("Vbat:"); this->print(vbat); + + display_->setCursor(10,3); + this->print("Teng:"); + this->print(teng); return 0; } @@ -206,13 +214,31 @@ int Lcd::renderMsgConfigNoTracks() { int Lcd::renderMsgBatteryLow() { this->clear(); - display_->setCursor(2, 1); - this->print("BATTERY WARNING"); + display_->setCursor(6, 1); + this->print("WARNING"); display_->setCursor(6, 2); this->print("VBAT LOW"); return 0; } +int Lcd::renderMsgEngineTempLow() { + this->clear(); + display_->setCursor(6, 1); + this->print("WARNING"); + display_->setCursor(2, 2); + this->print("ENGINE TEMP LOW"); + return 0; +} + +int Lcd::renderMsgEngineTempHigh() { + this->clear(); + display_->setCursor(6, 1); + this->print("WARNING"); + display_->setCursor(2, 2); + this->print("ENGINE TEMP HIGH"); + return 0; +} + int Lcd::push(const Task &task) { return queue_.push(task); } @@ -413,6 +439,13 @@ int Lcd::loop(unsigned long timeout_ms) { case task::DisplayMsgBatteryLow: activateMessage(screen::MsgBatteryLow, next_task.data_); break; + + case task::DisplayMsgEngineTempLow: + activateMessage(screen::MsgEngineTempLow, next_task.data_); + break; + + case task::DisplayMsgEngineTempHigh: + activateMessage(screen::MsgEngineTempLow, next_task.data_); default: break; @@ -467,6 +500,14 @@ int Lcd::loop(unsigned long timeout_ms) { case screen::MsgBatteryLow: this->renderMsgBatteryLow(); break; + + case screen::MsgEngineTempLow: + this->renderMsgEngineTempLow(); + break; + + case screen::MsgEngineTempHigh: + this->renderMsgEngineTempHigh(); + break; default: break; diff --git a/src/modules/lcd/lcd.h b/src/modules/lcd/lcd.h index 3bb565c..596af3b 100644 --- a/src/modules/lcd/lcd.h +++ b/src/modules/lcd/lcd.h @@ -26,6 +26,8 @@ enum LcdScreen : uint8_t { MsgTrackDetectOk, MsgConfigNoTracks, MsgBatteryLow, + MsgEngineTempLow, + MsgEngineTempHigh, }; } // namespace screen @@ -68,6 +70,8 @@ private: int renderMsgTrackDetectOk(); int renderMsgConfigNoTracks(); int renderMsgBatteryLow(); + int renderMsgEngineTempLow(); + int renderMsgEngineTempHigh(); public: int push(const Task &task) override; diff --git a/src/modules/thermocouple/thermocouple.cpp b/src/modules/thermocouple/thermocouple.cpp index 6563e20..e4afdbc 100644 --- a/src/modules/thermocouple/thermocouple.cpp +++ b/src/modules/thermocouple/thermocouple.cpp @@ -3,6 +3,7 @@ // SPDX-License-Identifier: GPL-3.0-or-later #include "thermocouple.h" +#include "base/router.h" #include "data/general_store.h" int Thermocouple::push(const Task &task) { return queue_.push(task); } @@ -14,17 +15,56 @@ Thermocouple::Thermocouple(SystemLogger *logger) : logger_(logger) {} Thermocouple::~Thermocouple() {} int Thermocouple::init() { - thermocouple_ = new MAX6675(THERMO_SCK, THERMO_CS, THERMO_SO); - return 0; + thermocouple_ = new MAX6675(THERMO_SCK, THERMO_CS, THERMO_SO); + VehicleConfig config; + configGlobalRead(config); + low_ = config.teng_low_; + high_ = config.teng_high_; + return 0; } int Thermocouple::loop(unsigned long timeout_ms) { - (void)timeout_ms; + (void)timeout_ms; - if (millis() > last_read_at_ + update_interval_) { + if (millis() > last_read_at_ + update_interval_) { temperature_ = thermocouple_->readCelsius(); tengGlobalWrite(temperature_); last_read_at_ = millis(); - } - return 0; + if (temperature_ > high_) { + if (warning_sent_at_ == 0 || + millis() > warning_sent_at_ + warning_timeout_) { + router::send(module::Lcd, task::DisplayMsgEngineTempHigh, 2000); + warning_sent_at_ = millis(); + } + } else if (temperature_ < low_) { + if (warning_sent_at_ == 0 || + millis() > warning_sent_at_ + warning_timeout_) { + router::send(module::Lcd, task::DisplayMsgEngineTempLow, 2000); + warning_sent_at_ = millis(); + } + } + + Task active_task; + int res = queue_.pop(active_task); + if (res == 0) { + if (active_task.target_ == module::Thermocouple) { + + } else if (active_task.target_ == module::All) { + + switch (active_task.type_) { + case task::AllConfigUpdated: { + VehicleConfig config; + configGlobalRead(config); + low_ = config.teng_low_; + high_ = config.teng_high_; + break; + } + + default: + break; + } + }; + } + return 0; + } } diff --git a/src/modules/thermocouple/thermocouple.h b/src/modules/thermocouple/thermocouple.h index 0908304..226e07a 100644 --- a/src/modules/thermocouple/thermocouple.h +++ b/src/modules/thermocouple/thermocouple.h @@ -18,8 +18,12 @@ private: RingBuffer queue_; MAX6675 *thermocouple_; double temperature_; + double low_; + double high_; unsigned long update_interval_ = 1000; unsigned long last_read_at_ = 0; + unsigned long warning_sent_at_ = 0; + unsigned long warning_timeout_ = 10000; public: int push(const Task &task) override;