From 9f79d84c2f91d931a6ebee6abcb0cbd8b50a1c95 Mon Sep 17 00:00:00 2001 From: Daniel Date: Mon, 31 Jul 2023 00:45:35 +0300 Subject: [PATCH] Add skeleton for KiaHyundai 64kWh --- Software/BATTERIES.h | 4 + Software/KIA-HYUNDAI-64-BATTERY.cpp | 135 ++++++++++++++++++++++++++++ Software/KIA-HYUNDAI-64-BATTERY.h | 41 +++++++++ Software/Software.ino | 13 +++ 4 files changed, 193 insertions(+) create mode 100644 Software/KIA-HYUNDAI-64-BATTERY.cpp create mode 100644 Software/KIA-HYUNDAI-64-BATTERY.h diff --git a/Software/BATTERIES.h b/Software/BATTERIES.h index 96e9b111..c26b3562 100644 --- a/Software/BATTERIES.h +++ b/Software/BATTERIES.h @@ -17,6 +17,10 @@ #include "IMIEV-CZERO-ION-BATTERY.h" //See this file for more triplet battery settings #endif +#ifdef KIA_HYUNDAI_64_BATTERY + #include "KIA-HYUNDAI-64-BATTERY.h" //See this file for more 64kWh battery settings +#endif + #ifdef CHADEMO #include "CHADEMO-BATTERY.h" //See this file for more Chademo settings #endif diff --git a/Software/KIA-HYUNDAI-64-BATTERY.cpp b/Software/KIA-HYUNDAI-64-BATTERY.cpp new file mode 100644 index 00000000..b82bde78 --- /dev/null +++ b/Software/KIA-HYUNDAI-64-BATTERY.cpp @@ -0,0 +1,135 @@ +#include "KIA-HYUNDAI-64-BATTERY.h" +#include "ESP32CAN.h" +#include "CAN_config.h" + +/* Do not change code below unless you are sure what you are doing */ +static unsigned long previousMillis10 = 0; // will store last time a 10ms CAN Message was send +static unsigned long previousMillis100 = 0; // will store last time a 100ms CAN Message was send +static const int interval10 = 10; // interval (ms) at which send CAN Messages +static const int interval100 = 100; // interval (ms) at which send CAN Messages +static uint8_t CANstillAlive = 12; //counter for checking if CAN is still alive + +#define LB_MAX_SOC 1000 //BMS never goes over this value. We use this info to rescale SOC% sent to Inverter +#define LB_MIN_SOC 0 //BMS never goes below this value. We use this info to rescale SOC% sent to Inverter + +static int SOC_1 = 0; +static int SOC_2 = 0; +static int SOC_3 = 0; + +void update_values_kiaHyundai_64_battery() +{ //This function maps all the values fetched via CAN to the correct parameters used for modbus + bms_status = ACTIVE; //Startout in active mode + + SOC; + + battery_voltage; + + battery_current; + + capacity_Wh = BATTERY_WH_MAX; + + remaining_capacity_Wh; + + max_target_discharge_power; + + max_target_charge_power; + + stat_batt_power; + + temperature_min; + + temperature_max; + + /* Check if the BMS is still sending CAN messages. If we go 60s without messages we raise an error*/ + if(!CANstillAlive) + { + bms_status = FAULT; + Serial.println("No CAN communication detected for 60s. Shutting down battery control."); + } + else + { + CANstillAlive--; + } + + if(printValues) + { //values heading towards the inverter + Serial.print("SOC% candidate 1: "); + Serial.println(SOC_1); + Serial.print("SOC% candidate 2: "); + Serial.println(SOC_2); + Serial.print("SOC% candidate 3: "); + Serial.println(SOC_3); + } +} + +void receive_can_kiaHyundai_64_battery(CAN_frame_t rx_frame) +{ + CANstillAlive = 12; + switch (rx_frame.MsgID) + { + case 0x3F6: + break; + case 0x491: + break; + case 0x493: + break; + case 0x497: + break; + case 0x498: + break; + case 0x4DD: + break; + case 0x4DE: + break; + case 0x4E2: + break; + case 0x542: + SOC_1 = rx_frame.data.u8[0]; + break; + case 0x594: + SOC_2 = rx_frame.data.u8[5]; + break; + case 0x595: + break; + case 0x596: + break; + case 0x597: + break; + case 0x598: + SOC_3 = (rx_frame.data.u8[4] * 256.0 + rx_frame.data.u8[5]); + break; + case 0x599: + break; + case 0x59C: + break; + case 0x59E: + break; + case 0x5A3: + break; + case 0x5D5: + break; + case 0x5D6: + break; + case 0x5D7: + break; + case 0x5D8: + break; + default: + break; + } +} +void send_can_kiaHyundai_64_battery() +{ + unsigned long currentMillis = millis(); + // Send 100ms CAN Message + if (currentMillis - previousMillis100 >= interval100) + { + previousMillis100 = currentMillis; + + } + //Send 10ms message + if (currentMillis - previousMillis10 >= interval10) + { + previousMillis10 = currentMillis; + } +} diff --git a/Software/KIA-HYUNDAI-64-BATTERY.h b/Software/KIA-HYUNDAI-64-BATTERY.h new file mode 100644 index 00000000..6679a3da --- /dev/null +++ b/Software/KIA-HYUNDAI-64-BATTERY.h @@ -0,0 +1,41 @@ +#ifndef KIA_HYUNDAI_64_BATTERY_H +#define KIA_HYUNDAI_64_BATTERY_H +#include +#include "ESP32CAN.h" + +#define BATTERY_WH_MAX 60000 //Battery size in Wh (Maximum value Fronius accepts is 60000 [60kWh] you can use larger batteries but do set value over 60000 +#define ABSOLUTE_MAX_VOLTAGE 4040 // 404.4V,if battery voltage goes over this, charging is not possible (goes into forced discharge) +#define ABSOLUTE_MIN_VOLTAGE 3100 // 310.0V if battery voltage goes under this, discharging further is disabled +#define MAXPERCENTAGE 800 //80.0% , Max percentage the battery will charge to (App will show 100% once this value is reached) +#define MINPERCENTAGE 200 //20.0% , Min percentage the battery will discharge to (App will show 0% once this value is reached) +static byte printValues = 1; //Should debug values be printed to serial output? + +// These parameters need to be mapped for the Gen24 +extern uint16_t SOC; +extern uint16_t StateOfHealth; +extern uint16_t battery_voltage; +extern uint16_t battery_current; +extern uint16_t capacity_Wh; +extern uint16_t remaining_capacity_Wh; +extern uint16_t max_target_discharge_power; +extern uint16_t max_target_charge_power; +extern uint16_t bms_status; +extern uint16_t bms_char_dis_status; +extern uint16_t stat_batt_power; +extern uint16_t temperature_min; +extern uint16_t temperature_max; +extern uint16_t CANerror; +extern uint8_t batteryAllowsContactorClosing; +// Definitions for BMS status +#define STANDBY 0 +#define INACTIVE 1 +#define DARKSTART 2 +#define ACTIVE 3 +#define FAULT 4 +#define UPDATING 5 + +void update_values_kiaHyundai_64_battery(); +void receive_can_kiaHyundai_64_battery(CAN_frame_t rx_frame); +void send_can_kiaHyundai_64_battery(); + +#endif \ No newline at end of file diff --git a/Software/Software.ino b/Software/Software.ino index e4e4193d..d218783d 100644 --- a/Software/Software.ino +++ b/Software/Software.ino @@ -3,6 +3,7 @@ //#define TESLA_MODEL_3_BATTERY // See TESLA-MODEL-3-BATTERY.h for more Tesla battery settings //#define RENAULT_ZOE_BATTERY // See RENAULT-ZOE-BATTERY.h for more Zoe battery settings //#define IMIEV_ION_CZERO_BATTERY // See IMIEV-CZERO-ION-BATTERY.h for more triplet battery settings +//#define KIA_HYUNDAI_64_BATTERY // See KIA-HYUNDAI-64-BATTERY.h for more battery settings //#define CHADEMO // See CHADEMO.h for more Chademo related settings /* Select inverter communication protocol. See Wiki for which to use with your inverter: https://github.com/dalathegreat/BYD-Battery-Emulator-For-Gen24/wiki */ @@ -173,6 +174,9 @@ void setup() #ifdef IMIEV_ION_CZERO_BATTERY Serial.println("Mitsubishi i-MiEV / Citroen C-Zero / Peugeot Ion battery selected"); #endif + #ifdef KIA_HYUNDAI_64_BATTERY + Serial.println("Kia Niro / Hyundai Kona 64kWh battery selected"); + #endif } // perform main program functions @@ -215,6 +219,9 @@ void handle_can() #ifdef IMIEV_ION_CZERO_BATTERY receive_can_imiev_battery(rx_frame); #endif + #ifdef KIA_HYUNDAI_64_BATTERY + receive_can_kiaHyundai_64_battery(rx_frame); + #endif #ifdef CAN_BYD receive_can_byd(rx_frame); #endif @@ -255,6 +262,9 @@ void handle_can() #ifdef IMIEV_ION_CZERO_BATTERY send_can_imiev_battery(); #endif + #ifdef KIA_HYUNDAI_64_BATTERY + send_can_kiaHyundai_64_battery(); + #endif #ifdef CHADEMO send_can_chademo_battery(); #endif @@ -274,6 +284,9 @@ void handle_inverter() #ifdef IMIEV_ION_CZERO_BATTERY update_values_imiev_battery(); //Map the values to the correct registers #endif + #ifdef KIA_HYUNDAI_64_BATTERY + update_values_kiaHyundai_64_battery(); //Map the values to the correct registers + #endif #ifdef SOLAX_CAN update_values_can_solax(); #endif