Skip to content

Commit

Permalink
Add internal resistance to voltage table calculations
Browse files Browse the repository at this point in the history
  • Loading branch information
brtietz committed Apr 21, 2023
1 parent 9b08216 commit f7972b9
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 10 deletions.
19 changes: 10 additions & 9 deletions shared/lib_battery_voltage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ voltage_t *voltage_table_t::clone() {
return new voltage_table_t(*this);
}

double voltage_table_t::calculate_voltage(double DOD) {
double voltage_table_t::calculate_voltage(double DOD, double I) {
DOD = fmax(0., DOD);
DOD = fmin(DOD, 100.);

Expand All @@ -187,22 +187,22 @@ double voltage_table_t::calculate_voltage(double DOD) {
row++;
}

return fmax(slopes[row] * DOD + intercepts[row], 0);
return fmax(slopes[row] * DOD + intercepts[row], 0) - I * params->resistance;
}

void voltage_table_t::set_initial_SOC(double init_soc) {
state->cell_voltage = calculate_voltage(100. - init_soc);
state->cell_voltage = calculate_voltage(100. - init_soc, 0.0);
}

double voltage_table_t::calculate_voltage_for_current(double I, double q, double qmax, double) {
double DOD = (q - I * params->dt_hr) / qmax * 100.;
return calculate_voltage(DOD) * params->num_cells_series;
return calculate_voltage(DOD, I / params->num_strings) * params->num_cells_series;
}


void voltage_table_t::updateVoltage(double q, double qmax, double, const double, double) {
void voltage_table_t::updateVoltage(double q, double qmax, double I, const double, double) {
double DOD = 100. * (1 - q / qmax);
state->cell_voltage = calculate_voltage(DOD);
state->cell_voltage = calculate_voltage(DOD, I / params->num_strings);
}

// helper fx to calculate depth of discharge from current and max capacities
Expand All @@ -212,7 +212,7 @@ double voltage_table_t::calculate_max_charge_w(double q, double qmax, double, do
double current = (q - qmax) / params->dt_hr;
if (max_current)
*max_current = current;
return calculate_voltage(0.) * current * params->num_cells_series;
return calculate_voltage(0., current / params->num_strings) * current * params->num_cells_series;
}

double voltage_table_t::calculate_max_discharge_w(double q, double qmax, double, double *max_current) {
Expand All @@ -227,7 +227,7 @@ double voltage_table_t::calculate_max_discharge_w(double q, double qmax, double,
dod = fmin(100, dod);
dod = fmax(0, dod);
double current = qmax * ((1. - DOD0 / 100.) - (1. - dod / 100.)) / params->dt_hr;
double p = calculate_voltage(dod) * current;
double p = calculate_voltage(dod, current / params->num_strings) * current;
if (p > max_P) {
max_P = p;
max_I = current;
Expand Down Expand Up @@ -288,7 +288,8 @@ double voltage_table_t::calculate_current_for_target_w(double P_watts, double q,
auto DOD_upper = params->voltage_table[upper][0];
auto DOD_lower = params->voltage_table[lower][0];
if (DOD_new <= DOD_upper && DOD_new >= DOD_lower) {
double P = (q - (100. - DOD_new) * qmax/100) * (a * DOD_new + b);
current = (q - (100. - DOD_new) * qmax / 100);
double P = current * (a * DOD_new + b - current / params->num_strings * params->resistance);
if (std::abs(P) > std::abs(P_best)) {
P_best = P;
DOD_best = DOD_new;
Expand Down
2 changes: 1 addition & 1 deletion shared/lib_battery_voltage.h
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ class voltage_table_t : public voltage_t {
std::vector<double> slopes;
std::vector<double> intercepts;

double calculate_voltage(double DOD);
double calculate_voltage(double DOD, double I);

private:
void initialize();
Expand Down

0 comments on commit f7972b9

Please sign in to comment.