diff --git a/main/battery.h b/main/battery.h index 842d3fc..e277a1e 100644 --- a/main/battery.h +++ b/main/battery.h @@ -1,40 +1,31 @@ #pragma once +// 3rdparty lib includes +#include +#include + // local includes #include "globals.h" #include "cpputils.h" -#define BATTERY_CELLTYPE_22P 0 -#define BATTERY_CELLTYPE_HG2 1 -#define BATTERY_CELLTYPE_MH1 2 -#define BATTERY_CELLTYPE_VTC5 3 +#define BatteryCellTypeValues(x) \ + x(_22P) \ + x(HG2) \ + x(MH1) \ + x(VTC5) +DECLARE_TYPESAFE_ENUM(BatteryCellType, : uint8_t, BatteryCellTypeValues) -float battery_percentage = 42.0; +namespace { +float battery_percentage = 42.f; -std::string getBatteryPercentageString() { +std::string getBatteryPercentageString() +{ std::string output = fmt::format("Battery: {:.1f}%", battery_percentage); return output; } -std::string getBatteryCellTypeString() { - std::string output = ""; - switch (settings.battery.cellType) { - case BATTERY_CELLTYPE_22P: - output = "Cells: 22P"; - break; - case BATTERY_CELLTYPE_HG2: - output = "Cells: HG2"; - break; - case BATTERY_CELLTYPE_MH1: - output = "Cells: MH1"; - break; - case BATTERY_CELLTYPE_VTC5: - output = "Cells: VTC5"; - break; - default: - output = "Unkown cell type"; - break; - } - - return output; +std::string getBatteryCellTypeString() +{ + return fmt::format("Cells: {}", toString(BatteryCellType(settings.battery.cellType))); } +} // namespace diff --git a/main/ble_bobby.h b/main/ble_bobby.h index 218868d..4bcd3ad 100644 --- a/main/ble_bobby.h +++ b/main/ble_bobby.h @@ -81,11 +81,11 @@ void handleBle() { auto arr = doc.createNestedArray("v"); if (controllers.front.feedbackValid) - arr.add(fixBatVoltage(controllers.front.feedback.batVoltage)); + arr.add(controllers.front.getCalibratedVoltage()); else arr.add(nullptr); if (controllers.back.feedbackValid) - arr.add(fixBatVoltage(controllers.back.feedback.batVoltage)); + arr.add(controllers.back.getCalibratedVoltage()); else arr.add(nullptr); } diff --git a/main/can.h b/main/can.h index 6bfa824..8a6bc9b 100644 --- a/main/can.h +++ b/main/can.h @@ -22,11 +22,6 @@ #include CAN_PLUGIN #endif -namespace { - float fixFrontBatVoltage(int16_t value); - float fixBackBatVoltage(int16_t value); -} - namespace can { namespace { @@ -257,7 +252,6 @@ bool tryParseCanInput() front.lastCanFeedback = espchrono::millis_clock::now(); front.feedbackValid = true; - front.calibrated.batVoltage = fixFrontBatVoltage(front.feedback.batVoltage); return true; } else @@ -270,7 +264,6 @@ bool tryParseCanInput() { back.lastCanFeedback = espchrono::millis_clock::now(); back.feedbackValid = true; - back.calibrated.batVoltage = fixBackBatVoltage(back.feedback.batVoltage); return true; } else diff --git a/main/cloud.h b/main/cloud.h index 0a423da..eac0e09 100644 --- a/main/cloud.h +++ b/main/cloud.h @@ -109,7 +109,7 @@ void cloudCollect() } cloudBuffer += fmt::format(",[{:.02f},{:.02f}", - fixBatVoltage(controller.feedback.batVoltage), + controller.getCalibratedVoltage(), fixBoardTemp(controller.feedback.boardTemp)); constexpr const auto addMotor = [](const bobbycar::protocol::serial::MotorState &command, diff --git a/main/controller.h b/main/controller.h index 148d74f..dbd2c59 100644 --- a/main/controller.h +++ b/main/controller.h @@ -56,8 +56,26 @@ struct Controller { #ifdef FEATURE_SERIAL FeedbackParser parser{serial, feedbackValid, feedback}; #endif - struct Calibrated { - float batVoltage; - } calibrated; + + float getCalibratedVoltage() const + { + return 0.f; + } + +// float fixFrontBatVoltage(int16_t value) +// { +// float frontVoltage = value; +// if (settings.battery.applyCalibration) +// frontVoltage = ((frontVoltage - float(settings.battery.front30VoltCalibration)) * (20.f / (float(settings.battery.front50VoltCalibration) - float(settings.battery.front30VoltCalibration))) + 30.f); +// return frontVoltage; +// } + +// float fixBackBatVoltage(int16_t value) +// { +// float backVoltage = value; +// if (settings.battery.applyCalibration) +// backVoltage = ((backVoltage - float(settings.battery.back30VoltCalibration)) * (20.f / (float(settings.battery.back50VoltCalibration) - float(settings.battery.back30VoltCalibration))) + 30.f); +// return backVoltage; +// } }; } diff --git a/main/debugtexthelpers.h b/main/debugtexthelpers.h index b553089..bdaa1a2 100644 --- a/main/debugtexthelpers.h +++ b/main/debugtexthelpers.h @@ -48,7 +48,7 @@ public: using RightCommand = CommandTexts; //struct BatVoltageText : public virtual TextInterface { public: std::string text() const override { std::string line{"batVoltage: "}; if (controller::get().feedbackValid) line += std::to_string(controller::get().feedback.batVoltage); return line; } }; - struct BatVoltageFixedText : public virtual TextInterface { public: std::string text() const override { std::string line{"batVoltage: "}; if (controller::get().feedbackValid) line += fmt::format("{:.2f}V", fixBatVoltage(controller::get().feedback.batVoltage)); return line; } }; + struct BatVoltageFixedText : public virtual TextInterface { public: std::string text() const override { std::string line{"batVoltage: "}; if (controller::get().feedbackValid) line += fmt::format("{:.2f}V", controller::get().getCalibratedVoltage()); return line; } }; //struct BoardTempText : public virtual TextInterface { public: std::string text() const override { std::string line{"boardTemp: "}; if (controller::get().feedbackValid) line += std::to_string(controller::get().feedback.boardTemp); return line; } }; struct BoardTempFixedText : public virtual TextInterface { public: std::string text() const override { std::string line{"boardTemp: "}; if (controller::get().feedbackValid) line += fmt::format("{:.2f}C", fixBoardTemp(controller::get().feedback.boardTemp)); return line; } }; struct TimeoutCntSerialText : public virtual TextInterface { public: std::string text() const override { std::string line{"timeoutCntSerial: "}; if (controller::get().feedbackValid) line += std::to_string(controller::get().feedback.timeoutCntSerial); return line; } }; diff --git a/main/displays/calibratevoltagedisplay.h b/main/displays/calibratevoltagedisplay.h index 9c10ab1..4cb5e26 100644 --- a/main/displays/calibratevoltagedisplay.h +++ b/main/displays/calibratevoltagedisplay.h @@ -52,11 +52,11 @@ namespace { } }; - class BatteryVoltageCalibrationFront30VText : public virtual TextInterface { public: std::string text() const override { return fmt::format("30V Front: {:.2f}V", fixBatVoltage(settings.battery.front30VoltCalibration)); } }; - class BatteryVoltageCalibrationBack30VText : public virtual TextInterface { public: std::string text() const override { return fmt::format("30V Back: {:.2f}V", fixBatVoltage(settings.battery.back30VoltCalibration)); } }; - class BatteryVoltageCalibrationFront50VText : public virtual TextInterface { public: std::string text() const override { return fmt::format("50V Front: {:.2f}V", fixBatVoltage(settings.battery.front50VoltCalibration)); } }; - class BatteryVoltageCalibrationBack50VText : public virtual TextInterface { public: std::string text() const override { return fmt::format("50V Back: {:.2f}V", fixBatVoltage(settings.battery.back50VoltCalibration)); } }; - class BatteryVoltageCalibratedText : public virtual TextInterface { public: std::string text() const override { if (settings.battery.applyCalibration) return fmt::format("F{:.2f}V B{:.2f}", fixFrontBatVoltage(controllers.front.feedback.batVoltage), fixFrontBatVoltage(controllers.back.feedback.batVoltage)); else return "Not activated"; } }; + class BatteryVoltageCalibrationFront30VText : public virtual TextInterface { public: std::string text() const override { return fmt::format("30V Front: {}", settings.battery.front30VoltCalibration); } }; + class BatteryVoltageCalibrationBack30VText : public virtual TextInterface { public: std::string text() const override { return fmt::format("30V Back: {}", settings.battery.back30VoltCalibration); } }; + class BatteryVoltageCalibrationFront50VText : public virtual TextInterface { public: std::string text() const override { return fmt::format("50V Front: {}", settings.battery.front50VoltCalibration); } }; + class BatteryVoltageCalibrationBack50VText : public virtual TextInterface { public: std::string text() const override { return fmt::format("50V Back: {}", settings.battery.back50VoltCalibration); } }; + class BatteryVoltageCalibratedText : public virtual TextInterface { public: std::string text() const override { if (settings.battery.applyCalibration) return fmt::format("F{:.2f}V B{:.2f}", controllers.front.getCalibratedVoltage(), controllers.back.getCalibratedVoltage()); else return "Not activated"; } }; } namespace { diff --git a/main/displays/metersdisplay.h b/main/displays/metersdisplay.h index fa8875a..7443ffc 100644 --- a/main/displays/metersdisplay.h +++ b/main/displays/metersdisplay.h @@ -74,8 +74,8 @@ void MetersDisplay::redraw() tft.setTextFont(2); m_sumCurrentLabel.redraw(std::to_string(sumCurrent) + 'A'); - meters[0].redraw(fixBatVoltage(controllers.front.feedback.batVoltage), 35, 50); - meters[1].redraw(fixBatVoltage(controllers.back.feedback.batVoltage), 35, 50); + meters[0].redraw(controllers.front.getCalibratedVoltage(), 35, 50); + meters[1].redraw(controllers.back.getCalibratedVoltage(), 35, 50); meters[2].redraw(fixCurrent(controllers.front.feedback.left.dcLink), -10, 10); meters[3].redraw(fixCurrent(controllers.front.feedback.right.dcLink), -10, 10); meters[4].redraw(fixCurrent(controllers.back.feedback.left.dcLink), -10, 10); diff --git a/main/displays/statusdisplay.h b/main/displays/statusdisplay.h index fffc344..3202ebf 100644 --- a/main/displays/statusdisplay.h +++ b/main/displays/statusdisplay.h @@ -297,14 +297,7 @@ void StatusDisplay::BoardStatus::redraw(const Controller &controller) if (controller.feedbackValid) { - if (settings.battery.applyCalibration) - { - m_labelVoltage.redraw(fmt::format("{:.2f}V", controller.calibrated.batVoltage)); - } - else - { - m_labelVoltage.redraw(fmt::format("{:.2f}V", fixBatVoltage(controller.feedback.batVoltage))); - } + m_labelVoltage.redraw(fmt::format("{:.2f}V", controller.getCalibratedVoltage())); m_labelTemperature.redraw(fmt::format("{:.2f}C", fixBoardTemp(controller.feedback.boardTemp))); m_leftMotor.redraw(controller.feedback.left); m_rightMotor.redraw(controller.feedback.right); diff --git a/main/presets.h b/main/presets.h index 5320ad6..eea3b68 100644 --- a/main/presets.h +++ b/main/presets.h @@ -262,7 +262,7 @@ constexpr Settings::Battery defaultBattery { .back30VoltCalibration = 30, .front50VoltCalibration = 50, .back50VoltCalibration = 50, - .applyCalibration = false + .applyCalibration = true }; constexpr Settings defaultSettings { diff --git a/main/statistics.h b/main/statistics.h index 662dfed..6090e21 100644 --- a/main/statistics.h +++ b/main/statistics.h @@ -30,13 +30,13 @@ void pushStats() statistics::sumCurrent.push_back(sumCurrent); if (controllers.front.feedbackValid) { - statistics::frontVoltage.push_back(fixBatVoltage(controllers.front.feedback.batVoltage)); + statistics::frontVoltage.push_back(controllers.front.getCalibratedVoltage()); statistics::frontLeftCurrent.push_back(fixCurrent(controllers.front.feedback.left.dcLink)); statistics::frontRightCurrent.push_back(fixCurrent(controllers.front.feedback.right.dcLink)); } if (controllers.back.feedbackValid) { - statistics::backVoltage.push_back(fixBatVoltage(controllers.back.feedback.batVoltage)); + statistics::backVoltage.push_back(controllers.back.getCalibratedVoltage()); statistics::backLeftCurrent.push_back(fixCurrent(controllers.back.feedback.left.dcLink)); statistics::backRightCurrent.push_back(fixCurrent(controllers.back.feedback.right.dcLink)); } diff --git a/main/utils.h b/main/utils.h index c970cb8..d65f413 100644 --- a/main/utils.h +++ b/main/utils.h @@ -59,27 +59,6 @@ float fixCurrent(int16_t value) return -value/50.; } -float fixBatVoltage(int16_t value) -{ - return value/100.; -} - -float fixFrontBatVoltage(int16_t value) -{ - float frontVoltage = fixBatVoltage(value); - if (!settings.battery.applyCalibration) return frontVoltage; - frontVoltage = ((frontVoltage - fixBatVoltage(settings.battery.front30VoltCalibration)) * (20.f / (fixBatVoltage(settings.battery.front50VoltCalibration) - fixBatVoltage(settings.battery.front30VoltCalibration))) + 30.f); - return frontVoltage; -} - -float fixBackBatVoltage(int16_t value) -{ - float backVoltage = fixBatVoltage(value); - if (!settings.battery.applyCalibration) return backVoltage; - backVoltage = ((backVoltage - fixBatVoltage(settings.battery.back30VoltCalibration)) * (20.f / (fixBatVoltage(settings.battery.back50VoltCalibration) - fixBatVoltage(settings.battery.back30VoltCalibration))) + 30.f); - return backVoltage; -} - float fixBoardTemp(int16_t value) { return value/10.;