Skip to content

Commit

Permalink
Merge pull request #188 from dalathegreat/improvement/battery-cleanup
Browse files Browse the repository at this point in the history
Battery interface streamlining
  • Loading branch information
Cabooman authored Feb 16, 2024
2 parents 9b3aaa5 + e49f4d9 commit 4bcd7c9
Show file tree
Hide file tree
Showing 23 changed files with 154 additions and 198 deletions.
126 changes: 5 additions & 121 deletions Software/Software.ino
Original file line number Diff line number Diff line change
Expand Up @@ -360,40 +360,11 @@ void inform_user_on_inverter() {

void inform_user_on_battery() {
// Inform user what battery is used
#ifdef BMW_I3_BATTERY
Serial.println("BMW i3 battery selected");
#endif
#ifdef CHADEMO_BATTERY
Serial.println("Chademo battery selected");
#endif
#ifdef IMIEV_CZERO_ION_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
#ifdef NISSAN_LEAF_BATTERY
Serial.println("Nissan LEAF battery selected");
#endif
#ifdef RENAULT_KANGOO_BATTERY
Serial.println("Renault Kangoo battery selected");
#endif
#ifdef SANTA_FE_PHEV_BATTERY
Serial.println("Hyundai Santa Fe PHEV battery selected");
#endif
#ifdef RENAULT_ZOE_BATTERY
Serial.println("Renault Zoe battery selected");
#endif
#ifdef TESLA_MODEL_3_BATTERY
Serial.println("Tesla Model 3 battery selected");
#endif
#ifdef TEST_FAKE_BATTERY
Serial.println("Test mode with fake battery selected");
#endif
announce_battery();
#ifdef SERIAL_LINK_RECEIVER
Serial.println("SERIAL_DATA_LINK_RECEIVER selected");
#endif
#if !defined(ABSOLUTE_MAX_VOLTAGE)
#ifndef BATTERY_SELECTED
#error No battery selected! Choose one from the USER_SETTINGS.h file
#endif
}
Expand All @@ -406,36 +377,7 @@ void receive_can() { // This section checks if we have a complete CAN message i
if (rx_frame.FIR.B.FF == CAN_frame_std) {
//printf("New standard frame");
// Battery
#ifdef BMW_I3_BATTERY
receive_can_i3_battery(rx_frame);
#endif
#ifdef CHADEMO_BATTERY
receive_can_chademo_battery(rx_frame);
#endif
#ifdef IMIEV_CZERO_ION_BATTERY
receive_can_imiev_battery(rx_frame);
#endif
#ifdef KIA_HYUNDAI_64_BATTERY
receive_can_kiaHyundai_64_battery(rx_frame);
#endif
#ifdef NISSAN_LEAF_BATTERY
receive_can_leaf_battery(rx_frame);
#endif
#ifdef RENAULT_KANGOO_BATTERY
receive_can_kangoo_battery(rx_frame);
#endif
#ifdef SANTA_FE_PHEV_BATTERY
receive_can_santafe_phev_battery(rx_frame);
#endif
#ifdef RENAULT_ZOE_BATTERY
receive_can_zoe_battery(rx_frame);
#endif
#ifdef TESLA_MODEL_3_BATTERY
receive_can_tesla_model_3_battery(rx_frame);
#endif
#ifdef TEST_FAKE_BATTERY
receive_can_test_battery(rx_frame);
#endif
receive_can_battery(rx_frame);
// Inverter
#ifdef BYD_CAN
receive_can_byd(rx_frame);
Expand Down Expand Up @@ -484,36 +426,7 @@ void send_can() {
send_can_sofar();
#endif
// Battery
#ifdef BMW_I3_BATTERY
send_can_i3_battery();
#endif
#ifdef CHADEMO_BATTERY
send_can_chademo_battery();
#endif
#ifdef IMIEV_CZERO_ION_BATTERY
send_can_imiev_battery();
#endif
#ifdef KIA_HYUNDAI_64_BATTERY
send_can_kiaHyundai_64_battery();
#endif
#ifdef NISSAN_LEAF_BATTERY
send_can_leaf_battery();
#endif
#ifdef RENAULT_KANGOO_BATTERY
send_can_kangoo_battery();
#endif
#ifdef SANTA_FE_PHEV_BATTERY
send_can_santafe_phev_battery();
#endif
#ifdef RENAULT_ZOE_BATTERY
send_can_zoe_battery();
#endif
#ifdef TESLA_MODEL_3_BATTERY
send_can_tesla_model_3_battery();
#endif
#ifdef TEST_FAKE_BATTERY
send_can_test_battery();
#endif
send_can_battery();
#ifdef CHEVYVOLT_CHARGER
send_can_chevyvolt_charger();
#endif
Expand Down Expand Up @@ -693,36 +606,7 @@ void handle_contactors() {

void update_values() {
// Battery
#ifdef BMW_I3_BATTERY
update_values_i3_battery(); // Map the values to the correct registers
#endif
#ifdef CHADEMO_BATTERY
update_values_chademo_battery(); // Map the values to the correct registers
#endif
#ifdef IMIEV_CZERO_ION_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 NISSAN_LEAF_BATTERY
update_values_leaf_battery(); // Map the values to the correct registers
#endif
#ifdef RENAULT_KANGOO_BATTERY
update_values_kangoo_battery(); // Map the values to the correct registers
#endif
#ifdef SANTA_FE_PHEV_BATTERY
update_values_santafe_phev_battery(); // Map the values to the correct registers
#endif
#ifdef RENAULT_ZOE_BATTERY
update_values_zoe_battery(); // Map the values to the correct registers
#endif
#ifdef TESLA_MODEL_3_BATTERY
update_values_tesla_model_3_battery(); // Map the values to the correct registers
#endif
#ifdef TEST_FAKE_BATTERY
update_values_test_battery(); // Map the fake values to the correct registers
#endif
update_values_battery(); // Map the fake values to the correct registers
// Inverter
#ifdef BYD_CAN
update_values_can_byd();
Expand Down
2 changes: 1 addition & 1 deletion Software/USER_SETTINGS.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@

/* Select inverter communication protocol. See Wiki for which to use with your inverter: https://github.com/dalathegreat/BYD-Battery-Emulator-For-Gen24/wiki */
//#define BYD_CAN //Enable this line to emulate a "BYD Battery-Box Premium HVS" over CAN Bus
//#define BYD_MODBUS //Enable this line to emulate a "BYD 11kWh HVM battery" over Modbus RTU
//#define BYD_MODBUS //Enable this line to emulate a "BYD 11kWh HVM battery" over Modbus RTU
//#define LUNA2000_MODBUS //Enable this line to emulate a "Luna2000 battery" over Modbus RTU
//#define PYLON_CAN //Enable this line to emulate a "Pylontech battery" over CAN bus
//#define SMA_CAN //Enable this line to emulate a "BYD Battery-Box H 8.9kWh, 7 mod" over CAN bus
Expand Down
7 changes: 7 additions & 0 deletions Software/src/battery/BATTERIES.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,11 @@
#include "SERIAL-LINK-RECEIVER-FROM-BATTERY.h" //See this file for more Serial-battery settings
#endif

#ifndef SERIAL_LINK_RECEIVER // The serial thing does its thing
void update_values_battery();
void receive_can_battery(CAN_frame_t rx_frame);
void send_can_battery();
void announce_battery(void);
#endif

#endif
18 changes: 13 additions & 5 deletions Software/src/battery/BMW-I3-BATTERY.cpp
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
#include "BMW-I3-BATTERY.h"
#include "BATTERIES.h"
#ifdef BMW_I3_BATTERY
#include "../devboard/utils/events.h"
#include "../lib/miwagner-ESP32-Arduino-CAN/CAN_config.h"
#include "../lib/miwagner-ESP32-Arduino-CAN/ESP32CAN.h"
#include "BMW-I3-BATTERY.h"

//TODO: before using
// Map the final values in update_values_i3_battery, set some to static values if not available (current, discharge max , charge max)
// Map the final values in update_values_battery, set some to static values if not available (current, discharge max , charge max)
// Check if I3 battery stays alive with only 10B and 512. If not, add 12F. If that doesn't help, add more from CAN log (ask Dala)

/* Do not change code below unless you are sure what you are doing */
Expand Down Expand Up @@ -54,7 +56,7 @@ static uint16_t Battery_Status = 0;
static uint16_t DC_link = 0;
static int16_t Battery_Power = 0;

void update_values_i3_battery() { //This function maps all the values fetched via CAN to the correct parameters used for modbus
void update_values_battery() { //This function maps all the values fetched via CAN to the correct parameters used for modbus
//Calculate the SOC% value to send to inverter
Calculated_SOC = (Display_SOC * 10); //Increase decimal amount
Calculated_SOC =
Expand Down Expand Up @@ -122,7 +124,7 @@ void update_values_i3_battery() { //This function maps all the values fetched v
#endif
}

void receive_can_i3_battery(CAN_frame_t rx_frame) {
void receive_can_battery(CAN_frame_t rx_frame) {
CANstillAlive = 12;
switch (rx_frame.MsgID) {
case 0x431: //Battery capacity [200ms]
Expand Down Expand Up @@ -168,7 +170,7 @@ void receive_can_i3_battery(CAN_frame_t rx_frame) {
break;
}
}
void send_can_i3_battery() {
void send_can_battery() {
unsigned long currentMillis = millis();
// Send 600ms CAN Message
if (currentMillis - previousMillis600 >= interval600) {
Expand All @@ -190,3 +192,9 @@ void send_can_i3_battery() {
ESP32Can.CANWriteFrame(&BMW_10B);
}
}

void announce_battery(void) {
Serial.println("BMW i3 battery selected");
}

#endif
6 changes: 2 additions & 4 deletions Software/src/battery/BMW-I3-BATTERY.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
#include "../devboard/config.h" // Needed for all defines
#include "../lib/miwagner-ESP32-Arduino-CAN/ESP32CAN.h"

#define BATTERY_SELECTED

#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
Expand All @@ -26,8 +28,4 @@ extern uint16_t CANerror;
extern bool batteryAllowsContactorClosing; //Bool, 1=true, 0=false
extern bool inverterAllowsContactorClosing; //Bool, 1=true, 0=false

void update_values_i3_battery();
void receive_can_i3_battery(CAN_frame_t rx_frame);
void send_can_i3_battery();

#endif
16 changes: 12 additions & 4 deletions Software/src/battery/CHADEMO-BATTERY.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
#include "CHADEMO-BATTERY.h"
#include "BATTERIES.h"
#ifdef CHADEMO_BATTERY
#include "../devboard/utils/events.h"
#include "../lib/miwagner-ESP32-Arduino-CAN/CAN_config.h"
#include "../lib/miwagner-ESP32-Arduino-CAN/ESP32CAN.h"
#include "CHADEMO-BATTERY.h"

/* Do not change code below unless you are sure what you are doing */
static unsigned long previousMillis100 = 0; // will store last time a 100ms CAN Message was send
Expand Down Expand Up @@ -89,7 +91,7 @@ uint8_t DynamicControlStatus = 0;
uint8_t HighCurrentControlStatus = 0;
uint8_t HighVoltageControlStatus = 0;

void update_values_chademo_battery() { //This function maps all the values fetched via CAN to the correct parameters used for the inverter
void update_values_battery() { //This function maps all the values fetched via CAN to the correct parameters used for the inverter

SOC = ChargingRate;

Expand Down Expand Up @@ -146,7 +148,7 @@ void update_values_chademo_battery() { //This function maps all the values fetc
#endif
}

void receive_can_chademo_battery(CAN_frame_t rx_frame) {
void receive_can_battery(CAN_frame_t rx_frame) {
CANstillAlive == 12; //We are getting CAN messages from the vehicle, inform the watchdog

switch (rx_frame.MsgID) {
Expand Down Expand Up @@ -202,7 +204,7 @@ void receive_can_chademo_battery(CAN_frame_t rx_frame) {
break;
}
}
void send_can_chademo_battery() {
void send_can_battery() {
unsigned long currentMillis = millis();
// Send 100ms CAN Message
if (currentMillis - previousMillis100 >= interval100) {
Expand All @@ -218,3 +220,9 @@ void send_can_chademo_battery() {
}
}
}

void announce_battery(void) {
Serial.println("Chademo battery selected");
}

#endif
6 changes: 2 additions & 4 deletions Software/src/battery/CHADEMO-BATTERY.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
#include "../devboard/config.h" // Needed for all defines
#include "../lib/miwagner-ESP32-Arduino-CAN/ESP32CAN.h"

#define BATTERY_SELECTED

#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
Expand All @@ -27,8 +29,4 @@ extern uint16_t CANerror;
extern bool batteryAllowsContactorClosing; //Bool, 1=true, 0=false
extern bool inverterAllowsContactorClosing; //Bool, 1=true, 0=false

void update_values_chademo_battery();
void receive_can_chademo_battery(CAN_frame_t rx_frame);
void send_can_chademo_battery();

#endif
16 changes: 12 additions & 4 deletions Software/src/battery/IMIEV-CZERO-ION-BATTERY.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
#include "IMIEV-CZERO-ION-BATTERY.h"
#include "BATTERIES.h"
#ifdef IMIEV_CZERO_ION_BATTERY
#include "../devboard/utils/events.h"
#include "../lib/miwagner-ESP32-Arduino-CAN/CAN_config.h"
#include "../lib/miwagner-ESP32-Arduino-CAN/ESP32CAN.h"
#include "IMIEV-CZERO-ION-BATTERY.h"

//Code still work in progress, TODO:
//Figure out if CAN messages need to be sent to keep the system happy?
Expand Down Expand Up @@ -40,7 +42,7 @@ static double min_volt_cel = 3.70;
static double max_temp_cel = 20.00;
static double min_temp_cel = 19.00;

void update_values_imiev_battery() { //This function maps all the values fetched via CAN to the correct parameters used for modbus
void update_values_battery() { //This function maps all the values fetched via CAN to the correct parameters used for modbus
SOC = (uint16_t)(BMU_SOC * 100); //increase BMU_SOC range from 0-100 -> 100.00

battery_voltage = (uint16_t)(BMU_PackVoltage * 10); // Multiply by 10 and cast to uint16_t
Expand Down Expand Up @@ -161,7 +163,7 @@ void update_values_imiev_battery() { //This function maps all the values fetche
#endif
}

void receive_can_imiev_battery(CAN_frame_t rx_frame) {
void receive_can_battery(CAN_frame_t rx_frame) {
CANstillAlive =
12; //TODO: move this inside a known message ID to prevent CAN inverter from keeping battery alive detection going
switch (rx_frame.MsgID) {
Expand Down Expand Up @@ -217,10 +219,16 @@ void receive_can_imiev_battery(CAN_frame_t rx_frame) {
}
}

void send_can_imiev_battery() {
void send_can_battery() {
unsigned long currentMillis = millis();
// Send 100ms CAN Message
if (currentMillis - previousMillis100 >= interval100) {
previousMillis100 = currentMillis;
}
}

void announce_battery(void) {
Serial.println("Mitsubishi i-MiEV / Citroen C-Zero / Peugeot Ion battery selected");
}

#endif
6 changes: 2 additions & 4 deletions Software/src/battery/IMIEV-CZERO-ION-BATTERY.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
#include "../devboard/config.h" // Needed for all defines
#include "../lib/miwagner-ESP32-Arduino-CAN/ESP32CAN.h"

#define BATTERY_SELECTED

#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
Expand All @@ -28,8 +30,4 @@ extern uint16_t cell_min_voltage;
extern bool batteryAllowsContactorClosing; //Bool, 1=true, 0=false
extern bool inverterAllowsContactorClosing; //Bool, 1=true, 0=false

void update_values_imiev_battery();
void receive_can_imiev_battery(CAN_frame_t rx_frame);
void send_can_imiev_battery();

#endif
Loading

0 comments on commit 4bcd7c9

Please sign in to comment.