diff --git a/main/battery.h b/main/battery.h index e277a1e..aa2694c 100644 --- a/main/battery.h +++ b/main/battery.h @@ -16,11 +16,33 @@ DECLARE_TYPESAFE_ENUM(BatteryCellType, : uint8_t, BatteryCellTypeValues) namespace { -float battery_percentage = 42.f; + +float getBatteryPercentage(float batVoltage, BatteryCellType cellType) +{ + float percentage = 0; + switch (cellType) { + case BatteryCellType::_22P: + break; + case BatteryCellType::MH1: + break; + case BatteryCellType::HG2: + break; + case BatteryCellType::VTC5: + break; + } + return percentage; +} std::string getBatteryPercentageString() { - std::string output = fmt::format("Battery: {:.1f}%", battery_percentage); + float avgVoltage = 0; + for (auto &controller : controllers) + { + avgVoltage += controller.getCalibratedVoltage(settings.battery.applyCalibration); + } + avgVoltage = avgVoltage / controllers.size(); + + std::string output = fmt::format("Battery: {:.1f}%", getBatteryPercentage(avgVoltage, BatteryCellType(settings.battery.cellType))); return output; } diff --git a/main/ble_bobby.h b/main/ble_bobby.h index 4bcd3ad..e72c746 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(controllers.front.getCalibratedVoltage()); + arr.add(controllers.front.getCalibratedVoltage(settings.battery.applyCalibration)); else arr.add(nullptr); if (controllers.back.feedbackValid) - arr.add(controllers.back.getCalibratedVoltage()); + arr.add(controllers.back.getCalibratedVoltage(settings.battery.applyCalibration)); else arr.add(nullptr); } diff --git a/main/cloud.h b/main/cloud.h index eac0e09..a4ba268 100644 --- a/main/cloud.h +++ b/main/cloud.h @@ -109,7 +109,7 @@ void cloudCollect() } cloudBuffer += fmt::format(",[{:.02f},{:.02f}", - controller.getCalibratedVoltage(), + controller.getCalibratedVoltage(settings.battery.applyCalibration), 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 5c6979a..cc415b6 100644 --- a/main/controller.h +++ b/main/controller.h @@ -24,6 +24,7 @@ class HardwareSerial; #endif namespace { + struct Controller { Controller( #ifdef FEATURE_SERIAL @@ -60,11 +61,17 @@ struct Controller { FeedbackParser parser{serial, feedbackValid, feedback}; #endif - float getCalibratedVoltage() const + float getCalibratedVoltage(bool applyCalibration) const { float voltage = feedback.batVoltage; - //if (settings.battery.applyCalibration) + if (applyCalibration) + { voltage = ((voltage - float(voltageCalib30V)) * (20.f / (float(voltageCalib50V) - float(voltageCalib30V))) + 30.f); + } + else + { + voltage = voltage / 100.; + } return voltage; } }; diff --git a/main/debugtexthelpers.h b/main/debugtexthelpers.h index bdaa1a2..ecb135f 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", controller::get().getCalibratedVoltage()); 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(settings.battery.applyCalibration)); 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 5f2f795..67c9646 100644 --- a/main/displays/calibratevoltagedisplay.h +++ b/main/displays/calibratevoltagedisplay.h @@ -61,7 +61,7 @@ namespace { class BatteryVoltageCalibrationBack30VText : public virtual TextInterface { public: std::string text() const override { return fmt::format("30V Back: {}", convertToFloat(settings.battery.back30VoltCalibration)); } }; class BatteryVoltageCalibrationFront50VText : public virtual TextInterface { public: std::string text() const override { return fmt::format("50V Front: {}", convertToFloat(settings.battery.front50VoltCalibration)); } }; class BatteryVoltageCalibrationBack50VText : public virtual TextInterface { public: std::string text() const override { return fmt::format("50V Back: {}", convertToFloat(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"; } }; + 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(settings.battery.applyCalibration), controllers.back.getCalibratedVoltage(settings.battery.applyCalibration)); else return "Not activated"; } }; } namespace { diff --git a/main/displays/metersdisplay.h b/main/displays/metersdisplay.h index 7443ffc..92e621a 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(controllers.front.getCalibratedVoltage(), 35, 50); - meters[1].redraw(controllers.back.getCalibratedVoltage(), 35, 50); + meters[0].redraw(controllers.front.getCalibratedVoltage(settings.battery.applyCalibration), 35, 50); + meters[1].redraw(controllers.back.getCalibratedVoltage(settings.battery.applyCalibration), 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 3202ebf..533e5f0 100644 --- a/main/displays/statusdisplay.h +++ b/main/displays/statusdisplay.h @@ -97,8 +97,8 @@ private: Label m_batterypercent{0, 30}; - BoardStatus m_frontStatus{42}; - BoardStatus m_backStatus{142}; + BoardStatus m_frontStatus{45}; + BoardStatus m_backStatus{145}; Label m_labelWifiStatus{35, bottomLines[0]}; // 120, 15 Label m_labelLimit0{205, bottomLines[0]}; // 35, 15 @@ -297,7 +297,7 @@ void StatusDisplay::BoardStatus::redraw(const Controller &controller) if (controller.feedbackValid) { - m_labelVoltage.redraw(fmt::format("{:.2f}V", controller.getCalibratedVoltage())); + m_labelVoltage.redraw(fmt::format("{:.2f}V", controller.getCalibratedVoltage(settings.battery.applyCalibration))); 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/statistics.h b/main/statistics.h index 6090e21..d03d891 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(controllers.front.getCalibratedVoltage()); + statistics::frontVoltage.push_back(controllers.front.getCalibratedVoltage(settings.battery.applyCalibration)); 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(controllers.back.getCalibratedVoltage()); + statistics::backVoltage.push_back(controllers.back.getCalibratedVoltage(settings.battery.applyCalibration)); statistics::backLeftCurrent.push_back(fixCurrent(controllers.back.feedback.left.dcLink)); statistics::backRightCurrent.push_back(fixCurrent(controllers.back.feedback.right.dcLink)); }