diff --git a/Software/Software.ino b/Software/Software.ino index 63828a42..60fe6f64 100644 --- a/Software/Software.ino +++ b/Software/Software.ino @@ -70,6 +70,7 @@ uint8_t bms_status = ACTIVE; // ACTIVE - [0..5]<>[STANDBY,INACTIVE,DA uint16_t stat_batt_power = 0; // Power going in/out of battery uint16_t cell_max_voltage = 3700; // Stores the highest cell voltage value in the system uint16_t cell_min_voltage = 3700; // Stores the minimum cell voltage value in the system +uint16_t cellvoltages[120]; // Stores all cell voltages bool LFP_Chemistry = false; // Common charger parameters @@ -413,8 +414,12 @@ void receive_can() { // This section checks if we have a complete CAN message i #ifdef SMA_CAN receive_can_sma(rx_frame); #endif + // Charger #ifdef CHEVYVOLT_CHARGER receive_can_chevyvolt_charger(rx_frame); +#endif +#ifdef NISSANLEAF_CHARGER + receive_can_nissanleaf_charger(rx_frame); #endif } else { //printf("New extended frame"); @@ -477,6 +482,9 @@ void send_can() { #ifdef CHEVYVOLT_CHARGER send_can_chevyvolt_charger(); #endif +#ifdef NISSANLEAF_CHARGER + send_can_nissanleaf_charger(); +#endif } #ifdef DUAL_CAN diff --git a/Software/USER_SETTINGS.cpp b/Software/USER_SETTINGS.cpp index 5805b45e..2b513933 100644 --- a/Software/USER_SETTINGS.cpp +++ b/Software/USER_SETTINGS.cpp @@ -15,7 +15,7 @@ volatile uint16_t MAXCHARGEAMP = volatile uint16_t MAXDISCHARGEAMP = 300; //30.0A , BYD CAN specific setting, Max discharge speed in Amp (Some inverters needs to be artificially limited) -/* Charger settings */ +/* Charger settings (Optional, when generator charging) */ volatile float CHARGER_SET_HV = 384; // Reasonably appropriate 4.0v per cell charging of a 96s pack volatile float CHARGER_MAX_HV = 420; // Max permissible output (VDC) of charger volatile float CHARGER_MIN_HV = 200; // Min permissible output (VDC) of charger diff --git a/Software/USER_SETTINGS.h b/Software/USER_SETTINGS.h index 501ff711..506806f8 100644 --- a/Software/USER_SETTINGS.h +++ b/Software/USER_SETTINGS.h @@ -41,6 +41,7 @@ /* Select charger used (Optional) */ //#define CHEVYVOLT_CHARGER //Enable this line to control a Chevrolet Volt charger connected to battery - for example, when generator charging or using an inverter without a charging function. +//#define NISSANLEAF_CHARGER //Enable this line to control a Nissan LEAF PDM connected to battery - for example, when generator charging /* Battery limits: These are set in the USER_SETTINGS.cpp file, or later on via the Webserver */ extern volatile uint16_t BATTERY_WH_MAX; @@ -50,7 +51,7 @@ extern volatile uint16_t MAXCHARGEAMP; extern volatile uint16_t MAXDISCHARGEAMP; extern volatile uint8_t AccessPointEnabled; -/* Charger limits: Set in the USER_SETTINGS.cpp or later in the webserver */ +/* Charger limits (Optional): Set in the USER_SETTINGS.cpp or later in the webserver */ extern volatile float charger_setpoint_HV_VDC; extern volatile float charger_setpoint_HV_IDC; extern volatile float charger_setpoint_HV_IDC_END; diff --git a/Software/src/battery/NISSAN-LEAF-BATTERY.cpp b/Software/src/battery/NISSAN-LEAF-BATTERY.cpp index fa31c023..9d20e2b9 100644 --- a/Software/src/battery/NISSAN-LEAF-BATTERY.cpp +++ b/Software/src/battery/NISSAN-LEAF-BATTERY.cpp @@ -237,6 +237,11 @@ void update_values_leaf_battery() { /* This function maps all the values fetched max_target_charge_power = 0; //No need to charge further, set max power to 0 } + //Map all cell voltages to the global array + for (int i = 0; i < 96; ++i) { + cellvoltages[i] = cell_voltages[i]; + } + bms_status = ACTIVE; //Startout in active mode /*Extra safety functions below*/ @@ -856,7 +861,10 @@ void send_can_leaf_battery() { break; } +//Only send this message when NISSANLEAF_CHARGER is not defined (otherwise it will collide!) +#ifndef NISSANLEAF_CHARGER ESP32Can.CANWriteFrame(&LEAF_1F2); //Contains (CHG_STA_RQ == 1 == Normal Charge) +#endif mprun10r = (mprun10r + 1) % 20; // 0x1F2 patter repeats after 20 messages. 0-1..19-0 diff --git a/Software/src/battery/NISSAN-LEAF-BATTERY.h b/Software/src/battery/NISSAN-LEAF-BATTERY.h index 02fe2c86..046bd3ca 100644 --- a/Software/src/battery/NISSAN-LEAF-BATTERY.h +++ b/Software/src/battery/NISSAN-LEAF-BATTERY.h @@ -21,11 +21,12 @@ extern uint16_t max_target_charge_power; //W, 0-60000 extern uint8_t bms_status; //Enum, 0-5 extern uint8_t bms_char_dis_status; //Enum, 0-2 extern uint16_t stat_batt_power; //W, Goes thru convert2unsignedint16 function (5W = 5, -5W = 65530) -extern uint16_t temperature_min; //C+1, Goes thru convert2unsignedint16 function (15.0C = 150, -15.0C = 65385) -extern uint16_t temperature_max; //C+1, Goes thru convert2unsignedint16 function (15.0C = 150, -15.0C = 65385) -extern uint16_t cell_max_voltage; //mV, 0-4350 -extern uint16_t cell_min_voltage; //mV, 0-4350 -extern uint8_t LEDcolor; //Enum, 0-10 +extern uint16_t temperature_min; //C+1, Goes thru convert2unsignedint16 function (15.0C = 150, -15.0C = 65385) +extern uint16_t temperature_max; //C+1, Goes thru convert2unsignedint16 function (15.0C = 150, -15.0C = 65385) +extern uint16_t cell_max_voltage; //mV, 0-4350 +extern uint16_t cell_min_voltage; //mV, 0-4350 +extern uint8_t LEDcolor; //Enum, 0-10 +extern uint16_t cellvoltages[120]; //mV 0-4350 per cell extern bool batteryAllowsContactorClosing; //Bool, 1=true, 0=false void update_values_leaf_battery(); diff --git a/Software/src/battery/TESLA-MODEL-3-BATTERY.cpp b/Software/src/battery/TESLA-MODEL-3-BATTERY.cpp index 2e742863..aded7618 100644 --- a/Software/src/battery/TESLA-MODEL-3-BATTERY.cpp +++ b/Software/src/battery/TESLA-MODEL-3-BATTERY.cpp @@ -451,6 +451,22 @@ void receive_can_tesla_model_3_battery(CAN_frame_t rx_frame) { min_temp = (rx_frame.data.u8[3] * 5) - 400; //Multiply by 5 and remove offset to get C+1 (0x61*5=485-400=8.5*C) } break; + case 0x401: // Cell stats + mux = (rx_frame.data.u8[0]); + + static uint16_t volts; + + if (rx_frame.data.u8[1] == 0x2A) // status byte must be 0x2A to read cellvoltages + { + // Example, frame3=0x89,frame2=0x1D = 35101 / 10 = 3510mV + volts = ((rx_frame.data.u8[3] << 8) | rx_frame.data.u8[2]) / 10; + cellvoltages[mux * 3] = volts; + volts = ((rx_frame.data.u8[5] << 8) | rx_frame.data.u8[4]) / 10; + cellvoltages[1 + mux * 3] = volts; + volts = ((rx_frame.data.u8[7] << 8) | rx_frame.data.u8[6]) / 10; + cellvoltages[2 + mux * 3] = volts; + } + break; case 0x2d2: //Min / max limits min_voltage = ((rx_frame.data.u8[1] << 8) | rx_frame.data.u8[0]) * 0.01 * 2; //Example 24148mv * 0.01 = 241.48 V diff --git a/Software/src/battery/TESLA-MODEL-3-BATTERY.h b/Software/src/battery/TESLA-MODEL-3-BATTERY.h index e958efd5..4de5457e 100644 --- a/Software/src/battery/TESLA-MODEL-3-BATTERY.h +++ b/Software/src/battery/TESLA-MODEL-3-BATTERY.h @@ -24,11 +24,12 @@ extern uint16_t max_target_charge_power; //W, 0-60000 extern uint8_t bms_status; //Enum, 0-5 extern uint8_t bms_char_dis_status; //Enum, 0-2 extern uint16_t stat_batt_power; //W, Goes thru convert2unsignedint16 function (5W = 5, -5W = 65530) -extern uint16_t temperature_min; //C+1, Goes thru convert2unsignedint16 function (15.0C = 150, -15.0C = 65385) -extern uint16_t temperature_max; //C+1, Goes thru convert2unsignedint16 function (15.0C = 150, -15.0C = 65385) -extern uint16_t cell_max_voltage; //mV, 0-4350 -extern uint16_t cell_min_voltage; //mV, 0-4350 -extern uint8_t LEDcolor; //Enum, 0-10 +extern uint16_t temperature_min; //C+1, Goes thru convert2unsignedint16 function (15.0C = 150, -15.0C = 65385) +extern uint16_t temperature_max; //C+1, Goes thru convert2unsignedint16 function (15.0C = 150, -15.0C = 65385) +extern uint16_t cell_max_voltage; //mV, 0-4350 +extern uint16_t cell_min_voltage; //mV, 0-4350 +extern uint16_t cellvoltages[120]; //mV 0-4350 per cell +extern uint8_t LEDcolor; //Enum, 0-10 extern bool batteryAllowsContactorClosing; //Bool, 1=true, 0=false extern bool inverterAllowsContactorClosing; //Bool, 1=true, 0=false extern bool LFP_Chemistry; diff --git a/Software/src/battery/TEST-FAKE-BATTERY.cpp b/Software/src/battery/TEST-FAKE-BATTERY.cpp index d825bd34..8deac988 100644 --- a/Software/src/battery/TEST-FAKE-BATTERY.cpp +++ b/Software/src/battery/TEST-FAKE-BATTERY.cpp @@ -25,7 +25,7 @@ void update_values_test_battery() { /* This function puts fake values onto the p StateOfHealth = 9900; // 99.00% - battery_voltage = 3700; // 370.0V + //battery_voltage = 3700; // 370.0V , value set in startup in .ino file, editable via webUI battery_current = 0; // 0 A @@ -33,9 +33,9 @@ void update_values_test_battery() { /* This function puts fake values onto the p remaining_capacity_Wh = 15000; // 15kWh - cell_max_voltage = 3750; + cell_max_voltage = 3596; - cell_min_voltage = 3730; + cell_min_voltage = 3500; stat_batt_power = 0; // 0W @@ -47,6 +47,10 @@ void update_values_test_battery() { /* This function puts fake values onto the p max_target_charge_power = 5000; // 5kW + for (int i = 0; i < 97; ++i) { + cellvoltages[i] = 3500 + i; + } + /*Finally print out values to serial if configured to do so*/ #ifdef DEBUG_VIA_USB Serial.println("FAKE Values going to inverter"); diff --git a/Software/src/battery/TEST-FAKE-BATTERY.h b/Software/src/battery/TEST-FAKE-BATTERY.h index 5c4b54fc..22f0a9c2 100644 --- a/Software/src/battery/TEST-FAKE-BATTERY.h +++ b/Software/src/battery/TEST-FAKE-BATTERY.h @@ -21,10 +21,11 @@ extern uint16_t max_target_charge_power; //W, 0-60000 extern uint8_t bms_status; //Enum, 0-5 extern uint8_t bms_char_dis_status; //Enum, 0-2 extern uint16_t stat_batt_power; //W, Goes thru convert2unsignedint16 function (5W = 5, -5W = 65530) -extern uint16_t temperature_min; //C+1, Goes thru convert2unsignedint16 function (15.0C = 150, -15.0C = 65385) -extern uint16_t temperature_max; //C+1, Goes thru convert2unsignedint16 function (15.0C = 150, -15.0C = 65385) -extern uint16_t cell_max_voltage; //mV, 0-4350 -extern uint16_t cell_min_voltage; //mV, 0-4350 +extern uint16_t temperature_min; //C+1, Goes thru convert2unsignedint16 function (15.0C = 150, -15.0C = 65385) +extern uint16_t temperature_max; //C+1, Goes thru convert2unsignedint16 function (15.0C = 150, -15.0C = 65385) +extern uint16_t cell_max_voltage; //mV, 0-4350 +extern uint16_t cell_min_voltage; //mV, 0-4350 +extern uint16_t cellvoltages[120]; //mV 0-5000 per cell extern bool batteryAllowsContactorClosing; //Bool, 1=true, 0=false extern bool inverterAllowsContactorClosing; //Bool, 1=true, 0=false extern uint8_t LEDcolor; //Enum, 0-10 diff --git a/Software/src/charger/CHARGERS.h b/Software/src/charger/CHARGERS.h index 73bae3a1..2b5793f3 100644 --- a/Software/src/charger/CHARGERS.h +++ b/Software/src/charger/CHARGERS.h @@ -2,7 +2,11 @@ #define CHARGERS_H #ifdef CHEVYVOLT_CHARGER -#include "chevyvolt.h" +#include "CHEVY-VOLT-CHARGER.h" +#endif + +#ifdef NISSANLEAF_CHARGER +#include "NISSAN-LEAF-CHARGER.h" #endif #endif diff --git a/Software/src/charger/chevyvolt.cpp b/Software/src/charger/CHEVY-VOLT-CHARGER.cpp similarity index 99% rename from Software/src/charger/chevyvolt.cpp rename to Software/src/charger/CHEVY-VOLT-CHARGER.cpp index a44e59b2..817c606b 100644 --- a/Software/src/charger/chevyvolt.cpp +++ b/Software/src/charger/CHEVY-VOLT-CHARGER.cpp @@ -1,4 +1,4 @@ -#include "chevyvolt.h" +#include "CHEVY-VOLT-CHARGER.h" #include "../lib/miwagner-ESP32-Arduino-CAN/CAN_config.h" #include "../lib/miwagner-ESP32-Arduino-CAN/ESP32CAN.h" diff --git a/Software/src/charger/chevyvolt.h b/Software/src/charger/CHEVY-VOLT-CHARGER.h similarity index 90% rename from Software/src/charger/chevyvolt.h rename to Software/src/charger/CHEVY-VOLT-CHARGER.h index 522397e6..8f80797a 100644 --- a/Software/src/charger/chevyvolt.h +++ b/Software/src/charger/CHEVY-VOLT-CHARGER.h @@ -1,5 +1,5 @@ -#ifndef CHEVYVOLT_H -#define CHEVYVOLT_H +#ifndef CHEVYVOLT_CHARGER_H +#define CHEVYVOLT_CHARGER_H #include #include "../../USER_SETTINGS.h" #include "../lib/miwagner-ESP32-Arduino-CAN/ESP32CAN.h" diff --git a/Software/src/charger/NISSAN-LEAF-CHARGER.cpp b/Software/src/charger/NISSAN-LEAF-CHARGER.cpp new file mode 100644 index 00000000..50e7a263 --- /dev/null +++ b/Software/src/charger/NISSAN-LEAF-CHARGER.cpp @@ -0,0 +1,275 @@ +#include "NISSAN-LEAF-CHARGER.h" +#include "../lib/miwagner-ESP32-Arduino-CAN/CAN_config.h" +#include "../lib/miwagner-ESP32-Arduino-CAN/ESP32CAN.h" + +/* This implements Nissan LEAF PDM charger support. 2013-2024 Gen2/3 PDMs are supported + * + * This code is intended to facilitate standalone battery charging, + * for instance for troubleshooting purposes or emergency generator usage + * + * Credits go to: + * Damien Maguire (evmbw.org, https://github.com/damienmaguire/Stm32-vcu) + * + * The code functions a bit differently incase a Nissan LEAF battery is used. Almost + * all CAN messages are already sent from the battery in that case, so the charger + * implementation can focus on only controlling the charger, spoofing battery messages + * is not needed. + * + * Incase another battery type is used, the code ofcourse emulates a complete Nissan LEAF + * battery onto the CAN bus. +*/ + +/* CAN cycles and timers */ +static const uint8_t interval10ms = 10; +static const uint8_t interval100ms = 100; +static unsigned long previousMillis10ms = 0; +static unsigned long previousMillis100ms = 0; + +/* LEAF charger/battery parameters */ +enum OBC_MODES : uint8_t { + IDLE_OR_QC = 1, + FINISHED = 2, + CHARGING_OR_INTERRUPTED = 4, + IDLE1 = 8, + IDLE2 = 9, + PLUGGED_IN_WAITING_ON_TIMER +}; +enum OBC_VOLTAGES : uint8_t { NO_SIGNAL = 0, AC110 = 1, AC230 = 2, ABNORMAL_WAVE = 3 }; +static uint16_t OBC_Charge_Power = 0; // Actual charger output +static uint8_t mprun100 = 0; // Counter 0-3 +static uint8_t mprun10 = 0; // Counter 0-3 +static uint8_t OBC_Charge_Status = IDLE_OR_QC; +static uint8_t OBC_Status_AC_Voltage = 0; //1=110V, 2=230V +static uint8_t OBCpowerSetpoint = 0; +static uint8_t OBCpower = 0; +static bool PPStatus = false; +static bool OBCwakeup = false; + +/* Voltage and current settings. Validation performed to set ceiling of 3300w vol*cur */ +extern volatile float charger_setpoint_HV_VDC; +extern volatile float charger_setpoint_HV_IDC; +extern volatile float charger_setpoint_HV_IDC_END; +extern bool charger_HV_enabled; +extern bool charger_aux12V_enabled; + +extern float charger_stat_HVcur; +extern float charger_stat_HVvol; +extern float charger_stat_ACcur; +extern float charger_stat_ACvol; +extern float charger_stat_LVcur; +extern float charger_stat_LVvol; + +//Actual content messages +static CAN_frame_t LEAF_1DB = {.FIR = {.B = + { + .DLC = 8, + .FF = CAN_frame_std, + }}, + .MsgID = 0x1DB, + .data = {0x00, 0x00, 0x00, 0x00, 0x40, 0x00, 0x00, 0x00}}; +static CAN_frame_t LEAF_1DC = {.FIR = {.B = + { + .DLC = 8, + .FF = CAN_frame_std, + }}, + .MsgID = 0x1DC, + .data = {0x6E, 0x0A, 0x05, 0xD5, 0x00, 0x00, 0x00, 0x00}}; +static CAN_frame_t LEAF_1F2 = {.FIR = {.B = + { + .DLC = 8, + .FF = CAN_frame_std, + }}, + .MsgID = 0x1F2, + .data = {0x30, 0x00, 0x20, 0xAC, 0x00, 0x3C, 0x00, 0x8F}}; +static CAN_frame_t LEAF_50B = {.FIR = {.B = + { + .DLC = 7, + .FF = CAN_frame_std, + }}, + .MsgID = 0x50B, + .data = {0x00, 0x00, 0x06, 0xC0, 0x00, 0x00, 0x00}}; +static CAN_frame_t LEAF_55B = {.FIR = {.B = + { + .DLC = 8, + .FF = CAN_frame_std, + }}, + .MsgID = 0x55B, + .data = {0xA4, 0x40, 0xAA, 0x00, 0xDF, 0xC0, 0x10, 0x00}}; +static CAN_frame_t LEAF_5BC = {.FIR = {.B = + { + .DLC = 8, + .FF = CAN_frame_std, + }}, + .MsgID = 0x5BC, + .data = {0x3D, 0x80, 0xF0, 0x64, 0xB0, 0x01, 0x00, 0x32}}; + +static CAN_frame_t LEAF_59E = {.FIR = {.B = + { + .DLC = 8, + .FF = CAN_frame_std, + }}, + .MsgID = 0x59E, + .data = {0x00, 0x00, 0x0C, 0x76, 0x18, 0x00, 0x00, 0x00}}; + +static uint8_t crctable[256] = { + 0, 133, 143, 10, 155, 30, 20, 145, 179, 54, 60, 185, 40, 173, 167, 34, 227, 102, 108, 233, 120, 253, + 247, 114, 80, 213, 223, 90, 203, 78, 68, 193, 67, 198, 204, 73, 216, 93, 87, 210, 240, 117, 127, 250, + 107, 238, 228, 97, 160, 37, 47, 170, 59, 190, 180, 49, 19, 150, 156, 25, 136, 13, 7, 130, 134, 3, + 9, 140, 29, 152, 146, 23, 53, 176, 186, 63, 174, 43, 33, 164, 101, 224, 234, 111, 254, 123, 113, 244, + 214, 83, 89, 220, 77, 200, 194, 71, 197, 64, 74, 207, 94, 219, 209, 84, 118, 243, 249, 124, 237, 104, + 98, 231, 38, 163, 169, 44, 189, 56, 50, 183, 149, 16, 26, 159, 14, 139, 129, 4, 137, 12, 6, 131, + 18, 151, 157, 24, 58, 191, 181, 48, 161, 36, 46, 171, 106, 239, 229, 96, 241, 116, 126, 251, 217, 92, + 86, 211, 66, 199, 205, 72, 202, 79, 69, 192, 81, 212, 222, 91, 121, 252, 246, 115, 226, 103, 109, 232, + 41, 172, 166, 35, 178, 55, 61, 184, 154, 31, 21, 144, 1, 132, 142, 11, 15, 138, 128, 5, 148, 17, + 27, 158, 188, 57, 51, 182, 39, 162, 168, 45, 236, 105, 99, 230, 119, 242, 248, 125, 95, 218, 208, 85, + 196, 65, 75, 206, 76, 201, 195, 70, 215, 82, 88, 221, 255, 122, 112, 245, 100, 225, 235, 110, 175, 42, + 32, 165, 52, 177, 187, 62, 28, 153, 147, 22, 135, 2, 8, 141}; + +static uint8_t calculate_CRC_Nissan(CAN_frame_t* frame) { + uint8_t crc = 0; + for (uint8_t j = 0; j < 7; j++) { + crc = crctable[(crc ^ static_cast(frame->data.u8[j])) % 256]; + } + return crc; +} + +static uint8_t calculate_checksum_nibble(CAN_frame_t* frame) { + uint8_t sum = 0; + for (uint8_t i = 0; i < 7; i++) { + sum += frame->data.u8[i] >> 4; + sum += frame->data.u8[i] & 0xF; + } + sum = (sum + 2) & 0xF; + return sum; +} + +void receive_can_nissanleaf_charger(CAN_frame_t rx_frame) { + + switch (rx_frame.MsgID) { + case 0x679: // This message fires once when charging cable is plugged in + OBCwakeup = true; + charger_aux12V_enabled = true; //Not possible to turn off 12V charging + // Startout with default values, so that charging can begin right when user plugs in cable + charger_HV_enabled = true; + charger_setpoint_HV_IDC = 16; // Ampere + charger_setpoint_HV_VDC = 400; // Target voltage + break; + case 0x390: + OBC_Charge_Status = ((rx_frame.data.u8[5] & 0x7E) >> 1); + if (OBC_Charge_Status == PLUGGED_IN_WAITING_ON_TIMER || CHARGING_OR_INTERRUPTED) { + PPStatus = true; //plug inserted + } else { + PPStatus = false; //plug not inserted + } + OBC_Status_AC_Voltage = ((rx_frame.data.u8[3] & 0x18) >> 3); + if (OBC_Status_AC_Voltage == AC110) { + charger_stat_ACvol = 110; + } + if (OBC_Status_AC_Voltage == AC230) { + charger_stat_ACvol = 230; + } + if (OBC_Status_AC_Voltage == ABNORMAL_WAVE) { + charger_stat_ACvol = 1; + } + + OBC_Charge_Power = ((rx_frame.data.u8[0] & 0x01) << 8) | (rx_frame.data.u8[1]); + charger_stat_HVcur = OBC_Charge_Power; + break; + default: + break; + } +} + +void send_can_nissanleaf_charger() { + unsigned long currentMillis = millis(); + + /* Send keepalive with mode every 10ms */ + if (currentMillis - previousMillis10ms >= interval10ms) { + previousMillis10ms = currentMillis; + + mprun10++; + if (mprun10 >= 4) + mprun10 = 0; + +/* 1DB is the main control message. If LEAF battery is used, the battery controls almost everything */ +// Only send these messages if Nissan LEAF battery is not used +#ifndef NISSAN_LEAF_BATTERY + + // VCM message, containing info if battery should sleep or stay awake + ESP32Can.CANWriteFrame(&LEAF_50B); // HCM_WakeUpSleepCommand == 11b == WakeUp, and CANMASK = 1 + + LEAF_1DB.data.u8[7] = calculate_CRC_Nissan(&LEAF_1DB); + ESP32Can.CANWriteFrame(&LEAF_1DB); + + LEAF_1DC.data.u8[7] = calculate_CRC_Nissan(&LEAF_1DC); + ESP32Can.CANWriteFrame(&LEAF_1DC); +#endif + + OBCpowerSetpoint = ((charger_setpoint_HV_IDC * 4) + 0x64); + + // convert power setpoint to PDM format: + // 0xA0 = 15A (60x) + // 0x70 = 3 amps ish (12x) + // 0x6a = 1.4A (6x) + // 0x66 = 0.5A (2x) + // 0x65 = 0.3A (1x) + // 0x64 = no chg + // so 0x64=100. 0xA0=160. so 60 decimal steps. 1 step=100W??? + + // This line controls if power should flow or not + if (PPStatus && + charger_HV_enabled) { //Charging starts when cable plugged in and User has requested charging to start via WebUI + // clamp min and max values + if (OBCpowerSetpoint > 0xA0) { //15A TODO, raise once cofirmed how to map bits into frame0 and frame1 + OBCpowerSetpoint = 0xA0; + } else if (OBCpowerSetpoint <= 0x64) { + OBCpowerSetpoint = 0x64; // 100W? stuck at 100 in drive mode (no charging) + } + + // if actual battery_voltage is less than setpoint got to max power set from web ui + if (battery_voltage < (CHARGER_SET_HV * 10)) { //battery_voltage = V+1, 0-500.0 (0-5000) + OBCpower = OBCpowerSetpoint; + } + + // decrement charger power if volt setpoint is reached + if (battery_voltage >= (CHARGER_SET_HV * 10)) { + if (OBCpower > 0x64) { + OBCpower--; + } + } + } else { + // set power to 0 if charge control is set to off or not in charge mode + OBCpower = 0x64; + } + + LEAF_1F2.data.u8[1] = OBCpower; + LEAF_1F2.data.u8[6] = mprun10; + LEAF_1F2.data.u8[7] = calculate_checksum_nibble(&LEAF_1F2); + + ESP32Can.CANWriteFrame( + &LEAF_1F2); // Sending of 1F2 message is halted in LEAF-BATTERY function incase charger is used! + } + + /* Send messages every 100ms here */ + if (currentMillis - previousMillis100ms >= interval100ms) { + previousMillis100ms = currentMillis; + + mprun100++; + if (mprun100 > 3) { + mprun100 = 0; + } + +// Only send these messages if Nissan LEAF battery is not used +#ifndef NISSAN_LEAF_BATTERY + + LEAF_55B.data.u8[6] = ((0x1 << 4) | (mprun100)); + + LEAF_55B.data.u8[7] = calculate_CRC_Nissan(&LEAF_55B); + ESP32Can.CANWriteFrame(&LEAF_55B); + + ESP32Can.CANWriteFrame(&LEAF_59E); + + ESP32Can.CANWriteFrame(&LEAF_5BC); +#endif + } +} diff --git a/Software/src/charger/NISSAN-LEAF-CHARGER.h b/Software/src/charger/NISSAN-LEAF-CHARGER.h new file mode 100644 index 00000000..691a7535 --- /dev/null +++ b/Software/src/charger/NISSAN-LEAF-CHARGER.h @@ -0,0 +1,12 @@ +#ifndef NISSANLEAF_CHARGER_H +#define NISSANLEAF_CHARGER_H +#include +#include "../../USER_SETTINGS.h" +#include "../lib/miwagner-ESP32-Arduino-CAN/ESP32CAN.h" + +extern uint16_t battery_voltage; //V+1, 0-500.0 (0-5000) + +void send_can_nissanleaf_charger(); +void receive_can_nissanleaf_charger(CAN_frame_t rx_frame); + +#endif diff --git a/Software/src/devboard/webserver/webserver.cpp b/Software/src/devboard/webserver/webserver.cpp index 21fa1afc..5de2585a 100644 --- a/Software/src/devboard/webserver/webserver.cpp +++ b/Software/src/devboard/webserver/webserver.cpp @@ -63,6 +63,11 @@ void init_webserver() { server.on("/settings", HTTP_GET, [](AsyncWebServerRequest* request) { request->send_P(200, "text/html", index_html, settings_processor); }); + // Route for going to cellmonitor web page + server.on("/cellmonitor", HTTP_GET, [](AsyncWebServerRequest* request) { + request->send_P(200, "text/html", index_html, cellmonitor_processor); + }); + // Route for editing Wh server.on("/updateBatterySize", HTTP_GET, [](AsyncWebServerRequest* request) { if (request->hasParam("value")) { @@ -123,7 +128,23 @@ void init_webserver() { } }); -#ifdef CHEVYVOLT_CHARGER +#ifdef TEST_FAKE_BATTERY + // Route for editing FakeBatteryVoltage + server.on("/updateFakeBatteryVoltage", HTTP_GET, [](AsyncWebServerRequest* request) { + if (!request->hasParam("value")) { + request->send(400, "text/plain", "Bad Request"); + } + + String value = request->getParam("value")->value(); + float val = value.toFloat(); + + battery_voltage = val * 10; + + request->send(200, "text/plain", "Updated successfully"); + }); +#endif + +#if defined CHEVYVOLT_CHARGER || defined NISSANLEAF_CHARGER // Route for editing ChargerTargetV server.on("/updateChargeSetpointV", HTTP_GET, [](AsyncWebServerRequest* request) { if (!request->hasParam("value")) { @@ -386,10 +407,13 @@ String processor(const String& var) { #endif content += ""; -#ifdef CHEVYVOLT_CHARGER +#if defined CHEVYVOLT_CHARGER || defined NISSANLEAF_CHARGER content += "

Charger protocol: "; #ifdef CHEVYVOLT_CHARGER content += "Chevy Volt Gen1 Charger"; +#endif +#ifdef NISSANLEAF_CHARGER + content += "Nissan LEAF 2013-2024 PDM charger"; #endif content += "

"; #endif @@ -487,7 +511,13 @@ String processor(const String& var) { content += ""; } -#ifdef CHEVYVOLT_CHARGER + // Close the block + content += ""; + +#if defined CHEVYVOLT_CHARGER || defined NISSANLEAF_CHARGER + // Start a new block with orange background color + content += "
"; + content += "

Charger HV Enabled: "; if (charger_HV_enabled) { content += ""; @@ -503,6 +533,7 @@ String processor(const String& var) { content += ""; } content += "

"; +#ifdef CHEVYVOLT_CHARGER float chgPwrDC = static_cast(charger_stat_HVcur * charger_stat_HVvol); float chgPwrAC = static_cast(charger_stat_ACcur * charger_stat_ACvol); float chgEff = chgPwrDC / chgPwrAC * 100; @@ -515,24 +546,40 @@ String processor(const String& var) { content += formatPowerValue("Charger Output Power", chgPwrDC, "", 1); content += "

Charger Efficiency: " + String(chgEff) + "%

"; - content += "

Charger HVDC Output V: " + String(HVvol, 2) + "

"; - content += "

Charger HVDC Output I: " + String(HVcur, 2) + "

"; + content += "

Charger HVDC Output V: " + String(HVvol, 2) + " V

"; + content += "

Charger HVDC Output I: " + String(HVcur, 2) + " A

"; content += "

Charger LVDC Output I: " + String(LVcur, 2) + "

"; content += "

Charger LVDC Output V: " + String(LVvol, 2) + "

"; - content += "

Charger AC Input V: " + String(ACvol, 2) + "VAC

"; - content += "

Charger AC Input I: " + String(ACvol, 2) + "VAC

"; + content += "

Charger AC Input V: " + String(ACvol, 2) + " VAC

"; + content += "

Charger AC Input I: " + String(ACcur, 2) + " A

"; #endif +#ifdef NISSANLEAF_CHARGER + float chgPwrDC = static_cast(charger_stat_HVcur * 100); + charger_stat_HVcur = chgPwrDC / (battery_voltage / 10); // P/U=I + charger_stat_HVvol = static_cast(battery_voltage / 10); + float ACvol = charger_stat_ACvol; + float HVvol = charger_stat_HVvol; + float HVcur = charger_stat_HVcur; + content += formatPowerValue("Charger Output Power", chgPwrDC, "", 1); + content += "

Charger HVDC Output V: " + String(HVvol, 2) + " V

"; + content += "

Charger HVDC Output I: " + String(HVcur, 2) + " A

"; + content += "

Charger AC Input V: " + String(ACvol, 2) + " VAC

"; +#endif // Close the block content += "
"; +#endif content += ""; content += " "; - content += ""; + content += ""; + content += " "; + content += ""; content += " "; content += ""; content += ""; + content += ""; + content += ""; + return content; + } + return String(); +} + +String cellmonitor_processor(const String& var) { + if (var == "PLACEHOLDER") { + String content = ""; + // Page format + content += ""; + + // Start a new block with a specific background color + content += "
"; + + // Display max, min, and deviation voltage values + content += "
"; + content += "Max Voltage: " + String(cell_max_voltage) + " mV
"; + content += "Min Voltage: " + String(cell_min_voltage) + " mV
"; + int deviation = cell_max_voltage - cell_min_voltage; + content += "Voltage Deviation: " + String(deviation) + " mV"; + content += "
"; + + // Visualize the populated cells in forward order using flexbox with conditional text color + content += "
"; + for (int i = 0; i < 120; ++i) { + // Skip empty values + if (cellvoltages[i] == 0) { + continue; + } + + String cellContent = "Cell " + String(i + 1) + "
" + String(cellvoltages[i]) + " mV"; + + // Check if the cell voltage is below 3000, apply red color + if (cellvoltages[i] < 3000) { + cellContent = "" + cellContent + ""; + } + + content += "
" + cellContent + "
"; + } + content += "
"; + // Close the block content += "
"; diff --git a/Software/src/devboard/webserver/webserver.h b/Software/src/devboard/webserver/webserver.h index 1cfda30c..69e7324a 100644 --- a/Software/src/devboard/webserver/webserver.h +++ b/Software/src/devboard/webserver/webserver.h @@ -21,11 +21,12 @@ extern uint16_t max_target_charge_power; //W, 0-60000 extern uint8_t bms_status; //Enum, 0-5 extern uint8_t bms_char_dis_status; //Enum, 0-2 extern uint16_t stat_batt_power; //W, Goes thru convert2unsignedint16 function (5W = 5, -5W = 65530) -extern uint16_t temperature_min; //C+1, Goes thru convert2unsignedint16 function (15.0C = 150, -15.0C = 65385) -extern uint16_t temperature_max; //C+1, Goes thru convert2unsignedint16 function (15.0C = 150, -15.0C = 65385) -extern uint16_t cell_max_voltage; //mV, 0-4350 -extern uint16_t cell_min_voltage; //mV, 0-4350 -extern uint8_t LEDcolor; //Enum, 0-10 +extern uint16_t temperature_min; //C+1, Goes thru convert2unsignedint16 function (15.0C = 150, -15.0C = 65385) +extern uint16_t temperature_max; //C+1, Goes thru convert2unsignedint16 function (15.0C = 150, -15.0C = 65385) +extern uint16_t cell_max_voltage; //mV, 0-4350 +extern uint16_t cell_min_voltage; //mV, 0-4350 +extern uint16_t cellvoltages[120]; //mV 0-4350 per cell +extern uint8_t LEDcolor; //Enum, 0-10 extern bool batteryAllowsContactorClosing; //Bool, 1=true, 0=false extern bool inverterAllowsContactorClosing; //Bool, 1=true, 0=false @@ -43,6 +44,9 @@ extern float charger_stat_ACvol; extern float charger_stat_LVcur; extern float charger_stat_LVvol; +//LEAF charger +extern uint16_t OBC_Charge_Power; + /** * @brief Initialization function for the webserver. * @@ -98,6 +102,15 @@ String processor(const String& var); */ String settings_processor(const String& var); +/** + * @brief Replaces placeholder with content section in web page + * + * @param[in] var + * + * @return String + */ +String cellmonitor_processor(const String& var); + /** * @brief Executes on OTA start *