Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: LEAF charger + Cell monitoring #151

Merged
merged 11 commits into from
Feb 3, 2024
8 changes: 8 additions & 0 deletions Software/Software.ino
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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");
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion Software/USER_SETTINGS.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion Software/USER_SETTINGS.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down
8 changes: 8 additions & 0 deletions Software/src/battery/NISSAN-LEAF-BATTERY.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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*/
Expand Down Expand Up @@ -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

Expand Down
11 changes: 6 additions & 5 deletions Software/src/battery/NISSAN-LEAF-BATTERY.h
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
16 changes: 16 additions & 0 deletions Software/src/battery/TESLA-MODEL-3-BATTERY.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
11 changes: 6 additions & 5 deletions Software/src/battery/TESLA-MODEL-3-BATTERY.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
10 changes: 7 additions & 3 deletions Software/src/battery/TEST-FAKE-BATTERY.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,17 @@ 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

capacity_Wh = 30000; // 30kWh

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

Expand All @@ -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");
Expand Down
9 changes: 5 additions & 4 deletions Software/src/battery/TEST-FAKE-BATTERY.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 5 additions & 1 deletion Software/src/charger/CHARGERS.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Original file line number Diff line number Diff line change
@@ -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"

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#ifndef CHEVYVOLT_H
#define CHEVYVOLT_H
#ifndef CHEVYVOLT_CHARGER_H
#define CHEVYVOLT_CHARGER_H
#include <Arduino.h>
#include "../../USER_SETTINGS.h"
#include "../lib/miwagner-ESP32-Arduino-CAN/ESP32CAN.h"
Expand Down
Loading
Loading