Skip to content

Commit

Permalink
Merge pull request #91 from dalathegreat/improvement/defines-voltage
Browse files Browse the repository at this point in the history
Rewrite max/min voltage to save memory
  • Loading branch information
dalathegreat authored Nov 11, 2023
2 parents d493483 + 8c88352 commit 2295d88
Show file tree
Hide file tree
Showing 14 changed files with 49 additions and 61 deletions.
16 changes: 3 additions & 13 deletions Software/Software.ino
Original file line number Diff line number Diff line change
Expand Up @@ -53,19 +53,9 @@ ModbusServerRTU MBserver(Serial2, 2000);
#define UPDATING 5
//Common inverter parameters
uint16_t capacity_Wh_startup = BATTERY_WH_MAX;
uint16_t max_power = 40960; //41kW
const uint16_t max_voltage = ABSOLUTE_MAX_VOLTAGE; //if higher charging is not possible (goes into forced discharge)
const uint16_t min_voltage = ABSOLUTE_MIN_VOLTAGE; //if lower Gen24 disables battery
uint16_t min_volt_byd_can = min_voltage;
uint16_t max_volt_byd_can = max_voltage;
uint16_t min_volt_solax_can = min_voltage;
uint16_t max_volt_solax_can = max_voltage;
uint16_t min_volt_pylon_can = min_voltage;
uint16_t max_volt_pylon_can = max_voltage;
uint16_t min_volt_modbus_byd = min_voltage;
uint16_t max_volt_modbus_byd = max_voltage;
uint16_t min_volt_sma_can = min_voltage;
uint16_t max_volt_sma_can = max_voltage;
uint16_t max_power = 40960; //41kW
uint16_t max_voltage = ABSOLUTE_MAX_VOLTAGE; //if higher charging is not possible (goes into forced discharge)
uint16_t min_voltage = ABSOLUTE_MIN_VOLTAGE; //if lower Gen24 disables battery
uint16_t battery_voltage = 3700;
uint16_t battery_current = 0;
uint16_t SOC = 5000; //SOC 0-100.00% //Updates later on from CAN
Expand Down
14 changes: 7 additions & 7 deletions Software/src/inverter/BYD-CAN.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -110,16 +110,16 @@ static long inverter_timestamp = 0;

void update_values_can_byd() { //This function maps all the values fetched from battery CAN to the correct CAN messages
//Calculate values
charge_current = ((max_target_charge_power * 10) /
max_volt_byd_can); //Charge power in W , max volt in V+1decimal (P=UI, solve for I)
charge_current =
((max_target_charge_power * 10) / max_voltage); //Charge power in W , max volt in V+1decimal (P=UI, solve for I)
//The above calculation results in (30 000*10)/3700=81A
charge_current = (charge_current * 10); //Value needs a decimal before getting sent to inverter (81.0A)
if (charge_current > MAXCHARGEAMP) {
charge_current = MAXCHARGEAMP; //Cap the value to the max allowed Amp. Some inverters cannot handle large values.
}

discharge_current = ((max_target_discharge_power * 10) /
max_volt_byd_can); //Charge power in W , max volt in V+1decimal (P=UI, solve for I)
max_voltage); //Charge power in W , max volt in V+1decimal (P=UI, solve for I)
//The above calculation results in (30 000*10)/3700=81A
discharge_current = (discharge_current * 10); //Value needs a decimal before getting sent to inverter (81.0A)
if (discharge_current > MAXDISCHARGEAMP) {
Expand All @@ -131,11 +131,11 @@ void update_values_can_byd() { //This function maps all the values fetched from

//Map values to CAN messages
//Maxvoltage (eg 400.0V = 4000 , 16bits long)
BYD_110.data.u8[0] = (max_volt_byd_can >> 8);
BYD_110.data.u8[1] = (max_volt_byd_can & 0x00FF);
BYD_110.data.u8[0] = (max_voltage >> 8);
BYD_110.data.u8[1] = (max_voltage & 0x00FF);
//Minvoltage (eg 300.0V = 3000 , 16bits long)
BYD_110.data.u8[2] = (min_volt_byd_can >> 8);
BYD_110.data.u8[3] = (min_volt_byd_can & 0x00FF);
BYD_110.data.u8[2] = (min_voltage >> 8);
BYD_110.data.u8[3] = (min_voltage & 0x00FF);
//Maximum discharge power allowed (Unit: A+1)
BYD_110.data.u8[4] = (discharge_current >> 8);
BYD_110.data.u8[5] = (discharge_current & 0x00FF);
Expand Down
4 changes: 2 additions & 2 deletions Software/src/inverter/BYD-CAN.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ extern uint16_t stat_batt_power;
extern uint16_t temperature_min;
extern uint16_t temperature_max;
extern uint16_t CANerror;
extern uint16_t min_volt_byd_can;
extern uint16_t max_volt_byd_can;
extern uint16_t min_voltage;
extern uint16_t max_voltage;
// Definitions for BMS status
#define STANDBY 0
#define INACTIVE 1
Expand Down
4 changes: 2 additions & 2 deletions Software/src/inverter/BYD-MODBUS.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,9 @@ void handle_update_data_modbusp201_byd() {
system_data[4] =
(max_power); // Id.: p205 Value.: 14500 Scaled value.: 30,42kW Comment.: Max Charge/Discharge Power=10.24kW (lowest value of 204 and 205 will be enforced by Gen24)
system_data[5] =
(max_volt_modbus_byd); // Id.: p206 Value.: 3667 Scaled value.: 362,7VDC Comment.: Max Voltage, if higher charging is not possible (goes into forced discharge)
(max_voltage); // Id.: p206 Value.: 3667 Scaled value.: 362,7VDC Comment.: Max Voltage, if higher charging is not possible (goes into forced discharge)
system_data[6] =
(min_volt_modbus_byd); // Id.: p207 Value.: 2776 Scaled value.: 277,6VDC Comment.: Min Voltage, if lower Gen24 disables battery
(min_voltage); // Id.: p207 Value.: 2776 Scaled value.: 277,6VDC Comment.: Min Voltage, if lower Gen24 disables battery
system_data[7] =
53248; // Id.: p208 Value.: 53248 Scaled value.: 53248 Comment.: Always 53248 for this BYD, Peak Charge power?
system_data[8] = 10; // Id.: p209 Value.: 10 Scaled value.: 10 Comment.: Always 10
Expand Down
5 changes: 2 additions & 3 deletions Software/src/inverter/BYD-MODBUS.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,10 @@ 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 capacity_Wh_startup;
extern uint16_t max_power;
extern uint16_t max_volt_modbus_byd;
extern uint16_t min_volt_modbus_byd;
extern uint16_t max_voltage;
extern uint16_t min_voltage;

void handle_static_data_modbus_byd();
void handle_update_data_modbusp201_byd();
Expand Down
5 changes: 2 additions & 3 deletions Software/src/inverter/LUNA2000-MODBUS.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,10 @@ 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 capacity_Wh_startup;
extern uint16_t max_power;
extern uint16_t max_volt_modbus_byd;
extern uint16_t min_volt_modbus_byd;
extern uint16_t max_voltage;
extern uint16_t min_voltage;

void update_modbus_registers_luna2000();
void handle_update_data_modbus32051();
Expand Down
16 changes: 8 additions & 8 deletions Software/src/inverter/PYLON-CAN.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -200,16 +200,16 @@ void update_values_can_pylon() { //This function maps all the values fetched fr
PYLON_4211.data.u8[7] = (StateOfHealth * 0.01);

//Minvoltage (eg 300.0V = 3000 , 16bits long) Charge Cutoff Voltage
PYLON_4220.data.u8[0] = (min_volt_pylon_can >> 8);
PYLON_4220.data.u8[1] = (min_volt_pylon_can & 0x00FF);
PYLON_4221.data.u8[0] = (min_volt_pylon_can >> 8);
PYLON_4221.data.u8[1] = (min_volt_pylon_can & 0x00FF);
PYLON_4220.data.u8[0] = (min_voltage >> 8);
PYLON_4220.data.u8[1] = (min_voltage & 0x00FF);
PYLON_4221.data.u8[0] = (min_voltage >> 8);
PYLON_4221.data.u8[1] = (min_voltage & 0x00FF);

//Maxvoltage (eg 400.0V = 4000 , 16bits long) Discharge Cutoff Voltage
PYLON_4220.data.u8[2] = (max_volt_pylon_can >> 8);
PYLON_4220.data.u8[3] = (max_volt_pylon_can & 0x00FF);
PYLON_4221.data.u8[2] = (max_volt_pylon_can >> 8);
PYLON_4221.data.u8[3] = (max_volt_pylon_can & 0x00FF);
PYLON_4220.data.u8[2] = (max_voltage >> 8);
PYLON_4220.data.u8[3] = (max_voltage & 0x00FF);
PYLON_4221.data.u8[2] = (max_voltage >> 8);
PYLON_4221.data.u8[3] = (max_voltage & 0x00FF);

//In case we run into any errors/faults, we can set charge / discharge forbidden
if (bms_status == FAULT) {
Expand Down
4 changes: 2 additions & 2 deletions Software/src/inverter/PYLON-CAN.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ extern uint16_t stat_batt_power;
extern uint16_t temperature_min;
extern uint16_t temperature_max;
extern uint16_t CANerror;
extern uint16_t min_volt_pylon_can;
extern uint16_t max_volt_pylon_can;
extern uint16_t min_voltage;
extern uint16_t max_voltage;
// Definitions for BMS status
#define STANDBY 0
#define INACTIVE 1
Expand Down
14 changes: 7 additions & 7 deletions Software/src/inverter/SMA-CAN.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -125,13 +125,13 @@ static int ampere_hours_remaining = 0;

void update_values_can_sma() { //This function maps all the values fetched from battery CAN to the correct CAN messages
//Calculate values
charge_current = ((max_target_charge_power * 10) /
max_volt_sma_can); //Charge power in W , max volt in V+1decimal (P=UI, solve for I)
charge_current =
((max_target_charge_power * 10) / max_voltage); //Charge power in W , max volt in V+1decimal (P=UI, solve for I)
//The above calculation results in (30 000*10)/3700=81A
charge_current = (charge_current * 10); //Value needs a decimal before getting sent to inverter (81.0A)

discharge_current = ((max_target_discharge_power * 10) /
max_volt_sma_can); //Charge power in W , max volt in V+1decimal (P=UI, solve for I)
max_voltage); //Charge power in W , max volt in V+1decimal (P=UI, solve for I)
//The above calculation results in (30 000*10)/3700=81A
discharge_current = (discharge_current * 10); //Value needs a decimal before getting sent to inverter (81.0A)

Expand All @@ -142,11 +142,11 @@ void update_values_can_sma() { //This function maps all the values fetched from

//Map values to CAN messages
//Maxvoltage (eg 400.0V = 4000 , 16bits long)
SMA_358.data.u8[0] = (max_volt_sma_can >> 8);
SMA_358.data.u8[1] = (max_volt_sma_can & 0x00FF);
SMA_358.data.u8[0] = (max_voltage >> 8);
SMA_358.data.u8[1] = (max_voltage & 0x00FF);
//Minvoltage (eg 300.0V = 3000 , 16bits long)
SMA_358.data.u8[2] = (min_volt_sma_can >> 8); //Minvoltage behaves strange on SMA, cuts out at 56% of the set value?
SMA_358.data.u8[3] = (min_volt_sma_can & 0x00FF);
SMA_358.data.u8[2] = (min_voltage >> 8); //Minvoltage behaves strange on SMA, cuts out at 56% of the set value?
SMA_358.data.u8[3] = (min_voltage & 0x00FF);
//Discharge limited current, 500 = 50A, (0.1, A)
SMA_358.data.u8[4] = (discharge_current >> 8);
SMA_358.data.u8[5] = (discharge_current & 0x00FF);
Expand Down
4 changes: 2 additions & 2 deletions Software/src/inverter/SMA-CAN.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ 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 cell_max_voltage; //mV, 0-4350
extern uint16_t cell_min_voltage; //mV, 0-4350
extern uint16_t min_volt_sma_can;
extern uint16_t max_volt_sma_can;
extern uint16_t min_voltage;
extern uint16_t max_voltage;
extern uint8_t LEDcolor; //Enum, 0-2
// Definitions for BMS status
#define STANDBY 0
Expand Down
8 changes: 4 additions & 4 deletions Software/src/inverter/SOFAR-CAN.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -281,15 +281,15 @@ CAN_frame_t SOFAR_7C0 = {.FIR = {.B =
void update_values_can_sofar() { //This function maps all the values fetched from battery CAN to the correct CAN messages

//Maxvoltage (eg 400.0V = 4000 , 16bits long) Charge Cutoff Voltage
SOFAR_351.data.u8[0] = (max_volt_sofar_can >> 8);
SOFAR_351.data.u8[1] = (max_volt_sofar_can & 0x00FF);
SOFAR_351.data.u8[0] = (max_voltage >> 8);
SOFAR_351.data.u8[1] = (max_voltage & 0x00FF);
//SOFAR_351.data.u8[2] = DC charge current limitation (Default 25.0A)
//SOFAR_351.data.u8[3] = DC charge current limitation
//SOFAR_351.data.u8[4] = DC discharge current limitation (Default 25.0A)
//SOFAR_351.data.u8[5] = DC discharge current limitation
//Minvoltage (eg 300.0V = 3000 , 16bits long) Discharge Cutoff Voltage
SOFAR_351.data.u8[6] = (min_volt_sofar_can >> 8);
SOFAR_351.data.u8[7] = (min_volt_sofar_can & 0x00FF);
SOFAR_351.data.u8[6] = (min_voltage >> 8);
SOFAR_351.data.u8[7] = (min_voltage & 0x00FF);

//SOC
SOFAR_355.data.u8[0] = (SOC / 100);
Expand Down
4 changes: 2 additions & 2 deletions Software/src/inverter/SOFAR-CAN.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ extern uint16_t cell_max_voltage; //mV, 0-4350
extern uint16_t cell_min_voltage; //mV, 0-4350
extern uint8_t batteryAllowsContactorClosing; //Bool, 1=true, 0=false
extern uint8_t LEDcolor; //Enum, 0-2
extern uint16_t min_volt_sofar_can;
extern uint16_t max_volt_sofar_can;
extern uint16_t min_voltage;
extern uint16_t max_voltage;
// Definitions for BMS status
#define STANDBY 0
#define INACTIVE 1
Expand Down
8 changes: 4 additions & 4 deletions Software/src/inverter/SOLAX-CAN.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -150,10 +150,10 @@ void update_values_can_solax() { //This function maps all the values fetched fr

//Put the values into the CAN messages
//BMS_Limits
SOLAX_1872.data.u8[0] = (uint8_t)max_volt_solax_can; //Todo, scaling OK?
SOLAX_1872.data.u8[1] = (max_volt_solax_can >> 8);
SOLAX_1872.data.u8[2] = (uint8_t)min_volt_solax_can; //Todo, scaling OK?
SOLAX_1872.data.u8[3] = (min_volt_solax_can >> 8);
SOLAX_1872.data.u8[0] = (uint8_t)max_voltage; //Todo, scaling OK?
SOLAX_1872.data.u8[1] = (max_voltage >> 8);
SOLAX_1872.data.u8[2] = (uint8_t)min_voltage; //Todo, scaling OK?
SOLAX_1872.data.u8[3] = (min_voltage >> 8);
SOLAX_1872.data.u8[4] = (uint8_t)(max_charge_rate_amp * 10); //Todo, scaling OK?
SOLAX_1872.data.u8[5] = ((max_charge_rate_amp * 10) >> 8);
SOLAX_1872.data.u8[6] = (uint8_t)(max_discharge_rate_amp * 10); //Todo, scaling OK?
Expand Down
4 changes: 2 additions & 2 deletions Software/src/inverter/SOLAX-CAN.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ extern uint16_t stat_batt_power;
extern uint16_t temperature_min;
extern uint16_t temperature_max;
extern uint16_t CANerror;
extern uint16_t min_volt_solax_can;
extern uint16_t max_volt_solax_can;
extern uint16_t min_voltage;
extern uint16_t max_voltage;
extern uint16_t cell_max_voltage;
extern uint16_t cell_min_voltage;
extern uint8_t inverterAllowsContactorClosing;
Expand Down

0 comments on commit 2295d88

Please sign in to comment.