Merge pull request #151 from dalathegreat/feature/LEAF-charger

Feature: LEAF charger + Cell monitoring
This commit is contained in:
Daniel Öster 2024-02-03 12:57:32 +02:00 committed by GitHub
commit e93e1289fb
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
16 changed files with 516 additions and 38 deletions

View file

@ -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 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_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 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; bool LFP_Chemistry = false;
// Common charger parameters // Common charger parameters
@ -414,8 +415,12 @@ void receive_can() { // This section checks if we have a complete CAN message i
#ifdef SMA_CAN #ifdef SMA_CAN
receive_can_sma(rx_frame); receive_can_sma(rx_frame);
#endif #endif
// Charger
#ifdef CHEVYVOLT_CHARGER #ifdef CHEVYVOLT_CHARGER
receive_can_chevyvolt_charger(rx_frame); receive_can_chevyvolt_charger(rx_frame);
#endif
#ifdef NISSANLEAF_CHARGER
receive_can_nissanleaf_charger(rx_frame);
#endif #endif
} else { } else {
//printf("New extended frame"); //printf("New extended frame");
@ -478,6 +483,9 @@ void send_can() {
#ifdef CHEVYVOLT_CHARGER #ifdef CHEVYVOLT_CHARGER
send_can_chevyvolt_charger(); send_can_chevyvolt_charger();
#endif #endif
#ifdef NISSANLEAF_CHARGER
send_can_nissanleaf_charger();
#endif
} }
#ifdef DUAL_CAN #ifdef DUAL_CAN

View file

@ -15,7 +15,7 @@ volatile uint16_t MAXCHARGEAMP =
volatile uint16_t MAXDISCHARGEAMP = volatile uint16_t MAXDISCHARGEAMP =
300; //30.0A , BYD CAN specific setting, Max discharge speed in Amp (Some inverters needs to be artificially limited) 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_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_MAX_HV = 420; // Max permissible output (VDC) of charger
volatile float CHARGER_MIN_HV = 200; // Min permissible output (VDC) of charger volatile float CHARGER_MIN_HV = 200; // Min permissible output (VDC) of charger

View file

@ -41,6 +41,7 @@
/* Select charger used (Optional) */ /* 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 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 */ /* Battery limits: These are set in the USER_SETTINGS.cpp file, or later on via the Webserver */
extern volatile uint16_t BATTERY_WH_MAX; extern volatile uint16_t BATTERY_WH_MAX;
@ -50,7 +51,7 @@ extern volatile uint16_t MAXCHARGEAMP;
extern volatile uint16_t MAXDISCHARGEAMP; extern volatile uint16_t MAXDISCHARGEAMP;
extern volatile uint8_t AccessPointEnabled; 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_VDC;
extern volatile float charger_setpoint_HV_IDC; extern volatile float charger_setpoint_HV_IDC;
extern volatile float charger_setpoint_HV_IDC_END; extern volatile float charger_setpoint_HV_IDC_END;

View file

@ -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 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 bms_status = ACTIVE; //Startout in active mode
/*Extra safety functions below*/ /*Extra safety functions below*/
@ -856,7 +861,10 @@ void send_can_leaf_battery() {
break; 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) 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 mprun10r = (mprun10r + 1) % 20; // 0x1F2 patter repeats after 20 messages. 0-1..19-0

View file

@ -26,6 +26,7 @@ extern uint16_t temperature_max; //C+1, Goes thru convert2unsignedint16 funct
extern uint16_t cell_max_voltage; //mV, 0-4350 extern uint16_t cell_max_voltage; //mV, 0-4350
extern uint16_t cell_min_voltage; //mV, 0-4350 extern uint16_t cell_min_voltage; //mV, 0-4350
extern uint8_t LEDcolor; //Enum, 0-10 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 extern bool batteryAllowsContactorClosing; //Bool, 1=true, 0=false
void update_values_leaf_battery(); void update_values_leaf_battery();

View file

@ -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) 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; 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: case 0x2d2:
//Min / max limits //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 min_voltage = ((rx_frame.data.u8[1] << 8) | rx_frame.data.u8[0]) * 0.01 * 2; //Example 24148mv * 0.01 = 241.48 V

View file

@ -28,6 +28,7 @@ extern uint16_t temperature_min; //C+1, Goes thru convert2unsignedint16 funct
extern uint16_t temperature_max; //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_max_voltage; //mV, 0-4350
extern uint16_t cell_min_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 uint8_t LEDcolor; //Enum, 0-10
extern bool batteryAllowsContactorClosing; //Bool, 1=true, 0=false extern bool batteryAllowsContactorClosing; //Bool, 1=true, 0=false
extern bool inverterAllowsContactorClosing; //Bool, 1=true, 0=false extern bool inverterAllowsContactorClosing; //Bool, 1=true, 0=false

View file

@ -25,7 +25,7 @@ void update_values_test_battery() { /* This function puts fake values onto the p
StateOfHealth = 9900; // 99.00% 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 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 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 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 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*/ /*Finally print out values to serial if configured to do so*/
#ifdef DEBUG_VIA_USB #ifdef DEBUG_VIA_USB
Serial.println("FAKE Values going to inverter"); Serial.println("FAKE Values going to inverter");

View file

@ -25,6 +25,7 @@ extern uint16_t temperature_min; //C+1, Goes thru convert2unsignedint16 funct
extern uint16_t temperature_max; //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_max_voltage; //mV, 0-4350
extern uint16_t cell_min_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 batteryAllowsContactorClosing; //Bool, 1=true, 0=false
extern bool inverterAllowsContactorClosing; //Bool, 1=true, 0=false extern bool inverterAllowsContactorClosing; //Bool, 1=true, 0=false
extern uint8_t LEDcolor; //Enum, 0-10 extern uint8_t LEDcolor; //Enum, 0-10

View file

@ -2,7 +2,11 @@
#define CHARGERS_H #define CHARGERS_H
#ifdef CHEVYVOLT_CHARGER #ifdef CHEVYVOLT_CHARGER
#include "chevyvolt.h" #include "CHEVY-VOLT-CHARGER.h"
#endif
#ifdef NISSANLEAF_CHARGER
#include "NISSAN-LEAF-CHARGER.h"
#endif #endif
#endif #endif

View file

@ -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/CAN_config.h"
#include "../lib/miwagner-ESP32-Arduino-CAN/ESP32CAN.h" #include "../lib/miwagner-ESP32-Arduino-CAN/ESP32CAN.h"

View file

@ -1,5 +1,5 @@
#ifndef CHEVYVOLT_H #ifndef CHEVYVOLT_CHARGER_H
#define CHEVYVOLT_H #define CHEVYVOLT_CHARGER_H
#include <Arduino.h> #include <Arduino.h>
#include "../../USER_SETTINGS.h" #include "../../USER_SETTINGS.h"
#include "../lib/miwagner-ESP32-Arduino-CAN/ESP32CAN.h" #include "../lib/miwagner-ESP32-Arduino-CAN/ESP32CAN.h"

View file

@ -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<uint8_t>(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
}
}

View file

@ -0,0 +1,12 @@
#ifndef NISSANLEAF_CHARGER_H
#define NISSANLEAF_CHARGER_H
#include <Arduino.h>
#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

View file

@ -65,6 +65,11 @@ void init_webserver() {
server.on("/settings", HTTP_GET, server.on("/settings", HTTP_GET,
[](AsyncWebServerRequest* request) { request->send_P(200, "text/html", index_html, settings_processor); }); [](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 // Route for editing Wh
server.on("/updateBatterySize", HTTP_GET, [](AsyncWebServerRequest* request) { server.on("/updateBatterySize", HTTP_GET, [](AsyncWebServerRequest* request) {
if (request->hasParam("value")) { if (request->hasParam("value")) {
@ -125,7 +130,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 // Route for editing ChargerTargetV
server.on("/updateChargeSetpointV", HTTP_GET, [](AsyncWebServerRequest* request) { server.on("/updateChargeSetpointV", HTTP_GET, [](AsyncWebServerRequest* request) {
if (!request->hasParam("value")) { if (!request->hasParam("value")) {
@ -414,10 +435,13 @@ String processor(const String& var) {
#endif #endif
content += "</h4>"; content += "</h4>";
#ifdef CHEVYVOLT_CHARGER #if defined CHEVYVOLT_CHARGER || defined NISSANLEAF_CHARGER
content += "<h4 style='color: white;'>Charger protocol: "; content += "<h4 style='color: white;'>Charger protocol: ";
#ifdef CHEVYVOLT_CHARGER #ifdef CHEVYVOLT_CHARGER
content += "Chevy Volt Gen1 Charger"; content += "Chevy Volt Gen1 Charger";
#endif
#ifdef NISSANLEAF_CHARGER
content += "Nissan LEAF 2013-2024 PDM charger";
#endif #endif
content += "</h4>"; content += "</h4>";
#endif #endif
@ -515,7 +539,13 @@ String processor(const String& var) {
content += "<span style='color: red;'>&#10005;</span></h4>"; content += "<span style='color: red;'>&#10005;</span></h4>";
} }
#ifdef CHEVYVOLT_CHARGER // Close the block
content += "</div>";
#if defined CHEVYVOLT_CHARGER || defined NISSANLEAF_CHARGER
// Start a new block with orange background color
content += "<div style='background-color: #FF6E00; padding: 10px; margin-bottom: 10px;border-radius: 50px'>";
content += "<h4>Charger HV Enabled: "; content += "<h4>Charger HV Enabled: ";
if (charger_HV_enabled) { if (charger_HV_enabled) {
content += "<span>&#10003;</span>"; content += "<span>&#10003;</span>";
@ -531,6 +561,7 @@ String processor(const String& var) {
content += "<span style='color: red;'>&#10005;</span>"; content += "<span style='color: red;'>&#10005;</span>";
} }
content += "</h4>"; content += "</h4>";
#ifdef CHEVYVOLT_CHARGER
float chgPwrDC = static_cast<float>(charger_stat_HVcur * charger_stat_HVvol); float chgPwrDC = static_cast<float>(charger_stat_HVcur * charger_stat_HVvol);
float chgPwrAC = static_cast<float>(charger_stat_ACcur * charger_stat_ACvol); float chgPwrAC = static_cast<float>(charger_stat_ACcur * charger_stat_ACvol);
float chgEff = chgPwrDC / chgPwrAC * 100; float chgEff = chgPwrDC / chgPwrAC * 100;
@ -543,24 +574,40 @@ String processor(const String& var) {
content += formatPowerValue("Charger Output Power", chgPwrDC, "", 1); content += formatPowerValue("Charger Output Power", chgPwrDC, "", 1);
content += "<h4 style='color: white;'>Charger Efficiency: " + String(chgEff) + "%</h4>"; content += "<h4 style='color: white;'>Charger Efficiency: " + String(chgEff) + "%</h4>";
content += "<h4 style='color: white;'>Charger HVDC Output V: " + String(HVvol, 2) + "</h4>"; content += "<h4 style='color: white;'>Charger HVDC Output V: " + String(HVvol, 2) + " V</h4>";
content += "<h4 style='color: white;'>Charger HVDC Output I: " + String(HVcur, 2) + "</h4>"; content += "<h4 style='color: white;'>Charger HVDC Output I: " + String(HVcur, 2) + " A</h4>";
content += "<h4 style='color: white;'>Charger LVDC Output I: " + String(LVcur, 2) + "</h4>"; content += "<h4 style='color: white;'>Charger LVDC Output I: " + String(LVcur, 2) + "</h4>";
content += "<h4 style='color: white;'>Charger LVDC Output V: " + String(LVvol, 2) + "</h4>"; content += "<h4 style='color: white;'>Charger LVDC Output V: " + String(LVvol, 2) + "</h4>";
content += "<h4 style='color: white;'>Charger AC Input V: " + String(ACvol, 2) + " VAC</h4>"; content += "<h4 style='color: white;'>Charger AC Input V: " + String(ACvol, 2) + " VAC</h4>";
content += "<h4 style='color: white;'>Charger AC Input I: " + String(ACvol, 2) + "VAC</h4>"; content += "<h4 style='color: white;'>Charger AC Input I: " + String(ACcur, 2) + " A</h4>";
#endif #endif
#ifdef NISSANLEAF_CHARGER
float chgPwrDC = static_cast<float>(charger_stat_HVcur * 100);
charger_stat_HVcur = chgPwrDC / (battery_voltage / 10); // P/U=I
charger_stat_HVvol = static_cast<float>(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 += "<h4 style='color: white;'>Charger HVDC Output V: " + String(HVvol, 2) + " V</h4>";
content += "<h4 style='color: white;'>Charger HVDC Output I: " + String(HVcur, 2) + " A</h4>";
content += "<h4 style='color: white;'>Charger AC Input V: " + String(ACvol, 2) + " VAC</h4>";
#endif
// Close the block // Close the block
content += "</div>"; content += "</div>";
#endif
content += "<button onclick='goToUpdatePage()'>Perform OTA update</button>"; content += "<button onclick='goToUpdatePage()'>Perform OTA update</button>";
content += " "; content += " ";
content += "<button onclick='goToSettingsPage()'>Change Battery Settings</button>"; content += "<button onclick='goToSettingsPage()'>Change Settings</button>";
content += " ";
content += "<button onclick='goToCellmonitorPage()'>Cellmonitor</button>";
content += " "; content += " ";
content += "<button onclick='promptToReboot()'>Reboot Emulator</button>"; content += "<button onclick='promptToReboot()'>Reboot Emulator</button>";
content += "<script>"; content += "<script>";
content += "function goToUpdatePage() { window.location.href = '/update'; }"; content += "function goToUpdatePage() { window.location.href = '/update'; }";
content += "function goToCellmonitorPage() { window.location.href = '/cellmonitor'; }";
content += "function goToSettingsPage() { window.location.href = '/settings'; }"; content += "function goToSettingsPage() { window.location.href = '/settings'; }";
content += content +=
"function promptToReboot() { if (window.confirm('Are you sure you want to reboot the emulator? NOTE: If " "function promptToReboot() { if (window.confirm('Are you sure you want to reboot the emulator? NOTE: If "
@ -605,7 +652,25 @@ String settings_processor(const String& var) {
" A </span> <button onclick='editMaxChargeA()'>Edit</button></h4>"; " A </span> <button onclick='editMaxChargeA()'>Edit</button></h4>";
content += "<h4 style='color: white;'>Max discharge speed: " + String(MAXDISCHARGEAMP / 10.0, 1) + content += "<h4 style='color: white;'>Max discharge speed: " + String(MAXDISCHARGEAMP / 10.0, 1) +
" A </span> <button onclick='editMaxDischargeA()'>Edit</button></h4>"; " A </span> <button onclick='editMaxDischargeA()'>Edit</button></h4>";
#ifdef CHEVYVOLT_CHARGER // Close the block
content += "</div>";
#ifdef TEST_FAKE_BATTERY
// Start a new block with blue background color
content += "<div style='background-color: #2E37AD; padding: 10px; margin-bottom: 10px;border-radius: 50px'>";
float voltageFloat = static_cast<float>(battery_voltage) / 10.0; // Convert to float and divide by 10
content += "<h4 style='color: white;'>Fake battery voltage: " + String(voltageFloat, 1) +
" V </span> <button onclick='editFakeBatteryVoltage()'>Edit</button></h4>";
// Close the block
content += "</div>";
#endif
#if defined CHEVYVOLT_CHARGER || defined NISSANLEAF_CHARGER
// Start a new block with orange background color
content += "<div style='background-color: #FF6E00; padding: 10px; margin-bottom: 10px;border-radius: 50px'>";
content += "<h4 style='color: white;'>Charger HVDC Enabled: "; content += "<h4 style='color: white;'>Charger HVDC Enabled: ";
if (charger_HV_enabled) { if (charger_HV_enabled) {
content += "<span>&#10003;</span>"; content += "<span>&#10003;</span>";
@ -626,6 +691,9 @@ String settings_processor(const String& var) {
" V </span> <button onclick='editChargerSetpointVDC()'>Edit</button></h4>"; " V </span> <button onclick='editChargerSetpointVDC()'>Edit</button></h4>";
content += "<h4 style='color: white;'>Charger Current Setpoint: " + String(charger_setpoint_HV_IDC, 1) + content += "<h4 style='color: white;'>Charger Current Setpoint: " + String(charger_setpoint_HV_IDC, 1) +
" A </span> <button onclick='editChargerSetpointIDC()'>Edit</button></h4>"; " A </span> <button onclick='editChargerSetpointIDC()'>Edit</button></h4>";
// Close the block
content += "</div>";
#endif #endif
content += "<script>"; content += "<script>";
@ -698,7 +766,22 @@ String settings_processor(const String& var) {
content += "}"; content += "}";
content += "}"; content += "}";
#ifdef CHEVYVOLT_CHARGER #ifdef TEST_FAKE_BATTERY
content += "function editFakeBatteryVoltage() {";
content += " var value = prompt('Enter new fake battery voltage');";
content += "if (value !== null) {";
content += " if (value >= 0 && value <= 5000) {";
content += " var xhr = new XMLHttpRequest();";
content += " xhr.open('GET', '/updateFakeBatteryVoltage?value=' + value, true);";
content += " xhr.send();";
content += " } else {";
content += " alert('Invalid value. Please enter a value between 0 and 1000');";
content += " }";
content += "}";
content += "}";
#endif
#if defined CHEVYVOLT_CHARGER || defined NISSANLEAF_CHARGER
content += "function editChargerHVDCEnabled() {"; content += "function editChargerHVDCEnabled() {";
content += " var value = prompt('Enable or disable HV DC output. Enter 1 for enabled, 0 for disabled');"; content += " var value = prompt('Enable or disable HV DC output. Enter 1 for enabled, 0 for disabled');";
content += " if (value !== null) {"; content += " if (value !== null) {";
@ -774,6 +857,57 @@ String settings_processor(const String& var) {
#endif #endif
content += "</script>"; content += "</script>";
content += "<button onclick='goToMainPage()'>Back to main page</button>";
content += "<script>";
content += "function goToMainPage() { window.location.href = '/'; }";
content += "</script>";
return content;
}
return String();
}
String cellmonitor_processor(const String& var) {
if (var == "PLACEHOLDER") {
String content = "";
// Page format
content += "<style>";
content += "body { background-color: black; color: white; }";
content += ".container { display: flex; flex-wrap: wrap; justify-content: space-around; }";
content += ".cell { width: 48%; margin: 1%; padding: 10px; border: 1px solid white; text-align: center; }";
content += ".low-voltage { color: red; }"; // Style for low voltage text
content += ".voltage-values { margin-bottom: 10px; }"; // Style for voltage values section
content += "</style>";
// Start a new block with a specific background color
content += "<div style='background-color: #303E47; padding: 10px; margin-bottom: 10px; border-radius: 50px'>";
// Display max, min, and deviation voltage values
content += "<div class='voltage-values'>";
content += "Max Voltage: " + String(cell_max_voltage) + " mV<br>";
content += "Min Voltage: " + String(cell_min_voltage) + " mV<br>";
int deviation = cell_max_voltage - cell_min_voltage;
content += "Voltage Deviation: " + String(deviation) + " mV";
content += "</div>";
// Visualize the populated cells in forward order using flexbox with conditional text color
content += "<div class='container'>";
for (int i = 0; i < 120; ++i) {
// Skip empty values
if (cellvoltages[i] == 0) {
continue;
}
String cellContent = "Cell " + String(i + 1) + "<br>" + String(cellvoltages[i]) + " mV";
// Check if the cell voltage is below 3000, apply red color
if (cellvoltages[i] < 3000) {
cellContent = "<span class='low-voltage'>" + cellContent + "</span>";
}
content += "<div class='cell'>" + cellContent + "</div>";
}
content += "</div>";
// Close the block // Close the block
content += "</div>"; content += "</div>";

View file

@ -25,6 +25,7 @@ extern uint16_t temperature_min; //C+1, Goes thru convert2unsignedint16 funct
extern uint16_t temperature_max; //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_max_voltage; //mV, 0-4350
extern uint16_t cell_min_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 uint8_t LEDcolor; //Enum, 0-10
extern bool batteryAllowsContactorClosing; //Bool, 1=true, 0=false extern bool batteryAllowsContactorClosing; //Bool, 1=true, 0=false
extern bool inverterAllowsContactorClosing; //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_LVcur;
extern float charger_stat_LVvol; extern float charger_stat_LVvol;
//LEAF charger
extern uint16_t OBC_Charge_Power;
/** /**
* @brief Initialization function for the webserver. * @brief Initialization function for the webserver.
* *
@ -107,6 +111,15 @@ String processor(const String& var);
*/ */
String settings_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 * @brief Executes on OTA start
* *