From 41a6678323a12c5a02eca314e3e631fb7846c508 Mon Sep 17 00:00:00 2001 From: Jaakko Haakana Date: Mon, 28 Apr 2025 20:39:44 +0300 Subject: [PATCH 1/2] Common base class for batteries and Zoe implementation --- Software/src/battery/BATTERIES.cpp | 30 +++++++++++++++++++ Software/src/battery/BATTERIES.h | 13 ++++++++ .../src/battery/RENAULT-ZOE-GEN1-BATTERY.cpp | 11 ++++--- .../src/battery/RENAULT-ZOE-GEN1-BATTERY.h | 13 ++++++-- Software/src/communication/can/obd.cpp | 2 ++ Software/src/devboard/webserver/webserver.cpp | 2 ++ 6 files changed, 65 insertions(+), 6 deletions(-) create mode 100644 Software/src/battery/BATTERIES.cpp diff --git a/Software/src/battery/BATTERIES.cpp b/Software/src/battery/BATTERIES.cpp new file mode 100644 index 00000000..850a1768 --- /dev/null +++ b/Software/src/battery/BATTERIES.cpp @@ -0,0 +1,30 @@ +#include "BATTERIES.h" +#include "../include.h" + +// These functions adapt the old C-style global functions battery-API to the +// object-oriented battery API. + +#ifdef OO_BATTERY_SELECTED + +static CanBattery* battery; + +void setup_battery() { + // Currently only one battery is implemented as a class. + // TODO: Extend based on build-time or run-time selected battery. + battery = new RenaultZoeGen1Battery(); + battery->setup(); +} + +void update_values_battery() { + battery->update_values(); +} + +void transmit_can_battery() { + battery->transmit_can(); +} + +void handle_incoming_can_frame_battery(CAN_frame rx_frame) { + battery->handle_incoming_can_frame(rx_frame); +} + +#endif diff --git a/Software/src/battery/BATTERIES.h b/Software/src/battery/BATTERIES.h index 1bf91d0f..5c92696d 100644 --- a/Software/src/battery/BATTERIES.h +++ b/Software/src/battery/BATTERIES.h @@ -2,6 +2,19 @@ #define BATTERIES_H #include "../../USER_SETTINGS.h" +#include "src/devboard/utils/types.h" + +// Abstract base class for next-generation battery implementations. +// Defines the interface to call battery specific functionality. +// No support for double battery yet. +class CanBattery { + public: + virtual void setup(void) = 0; + virtual void handle_incoming_can_frame(CAN_frame rx_frame) = 0; + virtual void update_values() = 0; + virtual void transmit_can() = 0; +}; + #ifdef BMW_SBOX #include "BMW-SBOX.h" void handle_incoming_can_frame_shunt(CAN_frame rx_frame); diff --git a/Software/src/battery/RENAULT-ZOE-GEN1-BATTERY.cpp b/Software/src/battery/RENAULT-ZOE-GEN1-BATTERY.cpp index 6d9e9b6f..5501782b 100644 --- a/Software/src/battery/RENAULT-ZOE-GEN1-BATTERY.cpp +++ b/Software/src/battery/RENAULT-ZOE-GEN1-BATTERY.cpp @@ -4,6 +4,8 @@ #include "../devboard/utils/events.h" #include "RENAULT-ZOE-GEN1-BATTERY.h" +void transmit_can_frame(CAN_frame* tx_frame, int interface); + /* Information in this file is based of the OVMS V3 vehicle_renaultzoe.cpp component https://github.com/openvehicles/Open-Vehicle-Monitoring-System-3/blob/master/vehicle/OVMS.V3/components/vehicle_renaultzoe/src/vehicle_renaultzoe.cpp The Zoe BMS apparently does not send total pack voltage, so we use the polled 96x cellvoltages summed up as total voltage @@ -75,7 +77,8 @@ static unsigned long previousMillis100 = 0; // will store last time a 100ms CAN static unsigned long previousMillis250 = 0; // will store last time a 250ms CAN Message was sent static uint8_t counter_423 = 0; -void update_values_battery() { //This function maps all the values fetched via CAN to the correct parameters used for modbus +void RenaultZoeGen1Battery:: + update_values() { //This function maps all the values fetched via CAN to the correct parameters used for modbus datalayer.battery.status.soh_pptt = (LB_SOH * 100); // Increase range from 99% -> 99.00% datalayer.battery.status.real_soc = SOC_polled; @@ -134,7 +137,7 @@ void update_values_battery() { //This function maps all the values fetched via datalayer.battery.status.voltage_dV = static_cast((calculated_total_pack_voltage_mV / 100)); // mV to dV } -void handle_incoming_can_frame_battery(CAN_frame rx_frame) { +void RenaultZoeGen1Battery::handle_incoming_can_frame(CAN_frame rx_frame) { switch (rx_frame.ID) { case 0x155: //10ms - Charging power, current and SOC datalayer.battery.status.CAN_battery_still_alive = CAN_STILL_ALIVE; @@ -490,7 +493,7 @@ void handle_incoming_can_frame_battery(CAN_frame rx_frame) { } } -void transmit_can_battery() { +void RenaultZoeGen1Battery::transmit_can() { unsigned long currentMillis = millis(); // Send 100ms CAN Message if (currentMillis - previousMillis100 >= INTERVAL_100_MS) { @@ -542,7 +545,7 @@ void transmit_can_battery() { } } -void setup_battery(void) { // Performs one time setup at startup +void RenaultZoeGen1Battery::setup(void) { // Performs one time setup at startup strncpy(datalayer.system.info.battery_protocol, "Renault Zoe Gen1 22/40kWh", 63); datalayer.system.info.battery_protocol[63] = '\0'; datalayer.system.status.battery_allows_contactor_closing = true; diff --git a/Software/src/battery/RENAULT-ZOE-GEN1-BATTERY.h b/Software/src/battery/RENAULT-ZOE-GEN1-BATTERY.h index 5b092b23..c5f6b48f 100644 --- a/Software/src/battery/RENAULT-ZOE-GEN1-BATTERY.h +++ b/Software/src/battery/RENAULT-ZOE-GEN1-BATTERY.h @@ -3,13 +3,22 @@ #include "../include.h" #define BATTERY_SELECTED + +// Indicates that the object-oriented battery interface is to be activated. +#define OO_BATTERY_SELECTED + #define MAX_PACK_VOLTAGE_DV 4200 //5000 = 500.0V #define MIN_PACK_VOLTAGE_DV 3000 #define MAX_CELL_DEVIATION_MV 150 #define MAX_CELL_VOLTAGE_MV 4250 //Battery is put into emergency stop if one cell goes over this value #define MIN_CELL_VOLTAGE_MV 2700 //Battery is put into emergency stop if one cell goes below this value -void setup_battery(void); -void transmit_can_frame(CAN_frame* tx_frame, int interface); +class RenaultZoeGen1Battery : public CanBattery { + public: + virtual void setup(void); + virtual void handle_incoming_can_frame(CAN_frame rx_frame); + virtual void update_values(); + virtual void transmit_can(); +}; #endif diff --git a/Software/src/communication/can/obd.cpp b/Software/src/communication/can/obd.cpp index 3a451e70..cfe73dd9 100644 --- a/Software/src/communication/can/obd.cpp +++ b/Software/src/communication/can/obd.cpp @@ -1,6 +1,8 @@ #include "obd.h" #include "comm_can.h" +void transmit_can_frame(CAN_frame* tx_frame, int interface); + void show_dtc(uint8_t byte0, uint8_t byte1); void show_dtc(uint8_t byte0, uint8_t byte1) { diff --git a/Software/src/devboard/webserver/webserver.cpp b/Software/src/devboard/webserver/webserver.cpp index c3422226..cdad4375 100644 --- a/Software/src/devboard/webserver/webserver.cpp +++ b/Software/src/devboard/webserver/webserver.cpp @@ -11,6 +11,8 @@ #include "../utils/timer.h" #include "esp_task_wdt.h" +void transmit_can_frame(CAN_frame* tx_frame, int interface); + // Create AsyncWebServer object on port 80 AsyncWebServer server(80); From da3a6b1c0490b03317b6a4c8a72a932763f5d18a Mon Sep 17 00:00:00 2001 From: Jaakko Haakana Date: Mon, 28 Apr 2025 21:47:44 +0300 Subject: [PATCH 2/2] Fix header file include order --- Software/src/battery/BATTERIES.cpp | 1 - 1 file changed, 1 deletion(-) diff --git a/Software/src/battery/BATTERIES.cpp b/Software/src/battery/BATTERIES.cpp index 850a1768..2d88dd6a 100644 --- a/Software/src/battery/BATTERIES.cpp +++ b/Software/src/battery/BATTERIES.cpp @@ -1,4 +1,3 @@ -#include "BATTERIES.h" #include "../include.h" // These functions adapt the old C-style global functions battery-API to the