diff --git a/src/adc_dma.cpp b/src/adc_dma.cpp index c4206b1..492bc3d 100644 --- a/src/adc_dma.cpp +++ b/src/adc_dma.cpp @@ -83,8 +83,6 @@ void calibrate_current_sensors() //---------------------------------------------------------------------------- void update_measurements() { - //int v_temp, rts; - // reference voltage of 2.5 V at PIN_V_REF //int vcc = 2500 * 4096 / (adc_filtered[ADC_POS_V_REF] >> (4 + ADC_FILTER_CONST)); @@ -96,17 +94,15 @@ void update_measurements() //int vcc = 3300; // calculate LS voltage first, as it is needed for HS voltage calculation for PWM charge controller - lv_terminal.voltage = + lv_bus.voltage = (float)(((adc_filtered[ADC_POS_V_BAT] >> (4 + ADC_FILTER_CONST)) * vcc) / 4096) * ADC_GAIN_V_BAT / 1000.0; - lv_bus_int.voltage = lv_terminal.voltage; - load_terminal.voltage = lv_terminal.voltage; - hv_terminal.voltage = + hv_bus.voltage = (float)(((adc_filtered[ADC_POS_V_SOLAR] >> (4 + ADC_FILTER_CONST)) * vcc) / 4096) * ADC_GAIN_V_SOLAR / 1000.0; #ifdef ADC_OFFSET_V_SOLAR - hv_terminal.voltage = lv_terminal.voltage + -(vcc * ADC_OFFSET_V_SOLAR / 1000.0 + hv_terminal.voltage); + hv_bus.voltage = hv_bus.voltage + -(vcc * ADC_OFFSET_V_SOLAR / 1000.0 + hv_bus.voltage); #endif load_terminal.current = @@ -120,18 +116,20 @@ void update_measurements() ADC_GAIN_I_SOLAR / 1000.0 + solar_current_offset); lv_terminal.current = -hv_terminal.current - load_terminal.current; #else // MPPT - lv_bus_int.current = + dcdc_port_lv.current = (float)(((adc_filtered[ADC_POS_I_DCDC] >> (4 + ADC_FILTER_CONST)) * vcc) / 4096) * ADC_GAIN_I_DCDC / 1000.0 + solar_current_offset; - lv_terminal.current = lv_bus_int.current - load_terminal.current; - hv_terminal.current = -lv_bus_int.current * lv_bus_int.voltage / hv_terminal.voltage; + lv_terminal.current = dcdc_port_lv.current - load_terminal.current; + hv_terminal.current = -dcdc_port_lv.current * lv_bus.voltage / hv_bus.voltage; + dcdc_port_hv.current = hv_terminal.current; #endif // power calculations - hv_terminal.power = hv_terminal.voltage * hv_terminal.current; - lv_bus_int.power = lv_bus_int.voltage * lv_bus_int.current; - lv_terminal.power = lv_terminal.voltage * lv_terminal.current; - load_terminal.power = load_terminal.voltage * load_terminal.current; + dcdc_port_hv.power = hv_bus.voltage * dcdc_port_hv.current; + hv_terminal.power = hv_bus.voltage * hv_terminal.current; + dcdc_port_lv.power = lv_bus.voltage * dcdc_port_lv.current; + lv_terminal.power = lv_bus.voltage * lv_terminal.current; + load_terminal.power = lv_bus.voltage * load_terminal.current; /** \todo Improved (faster) temperature calculation: https://www.embeddedrelated.com/showarticle/91.php diff --git a/src/bat_charger.cpp b/src/bat_charger.cpp index 3895cae..e4e11dd 100644 --- a/src/bat_charger.cpp +++ b/src/bat_charger.cpp @@ -18,10 +18,6 @@ #include "config.h" #include "pcb.h" -#include "dc_bus.h" -#include "load.h" -#include "log.h" - #include // for fabs function #include #include @@ -219,20 +215,11 @@ void battery_conf_overwrite(BatConf *source, BatConf *destination, Charger *char // - restart DC/DC } - -void charger_init(Charger *charger) -{ - charger->num_batteries = 1; // initialize with only one battery in series - charger->soh = 100; // assume new battery - charger->bat_temperature = 25.0; - charger->first_full_charge_reached = false; -} - -void charger_detect_num_batteries(Charger *charger, BatConf *bat, DcBus *bus) +void Charger::detect_num_batteries(BatConf *bat) { if (bus->voltage > bat->voltage_absolute_min * 2 && bus->voltage < bat->voltage_absolute_max * 2) { - charger->num_batteries = 2; + num_batteries = 2; printf("Detected two batteries (total %.2f V max)\n", bat->topping_voltage * 2); } else { @@ -240,11 +227,11 @@ void charger_detect_num_batteries(Charger *charger, BatConf *bat, DcBus *bus) } } -void battery_update_soc(BatConf *bat_conf, Charger *charger, DcBus *bus) +void Charger::update_soc(BatConf *bat_conf) { static int soc_filtered = 0; // SOC / 100 for better filtering - if (fabs(bus->current) < 0.2) { + if (fabs(port->current) < 0.2) { int soc_new = (int)((bus->voltage - bat_conf->ocv_empty) / (bat_conf->ocv_full - bat_conf->ocv_empty) * 10000.0); @@ -263,162 +250,163 @@ void battery_update_soc(BatConf *bat_conf, Charger *charger, DcBus *bus) else if (soc_filtered < 0) { soc_filtered = 0; } - charger->soc = soc_filtered / 100; + soc = soc_filtered / 100; } - charger->discharged_Ah += -bus->current / 3600.0; // charged current is positive: change sign + discharged_Ah += -port->current / 3600.0; // charged current is positive: change sign } -static void _enter_state(Charger* charger, int next_state) +void Charger::enter_state(int next_state) { //printf("Enter State: %d\n", next_state); - charger->time_state_changed = time(NULL); - charger->state = next_state; + time_state_changed = time(NULL); + state = next_state; } -void battery_discharge_control(DcBus *bus, BatConf *bat_conf, Charger *charger) +void Charger::discharge_control(BatConf *bat_conf) { // load output state is defined by battery bus dis_allowed flag - if (bus->dis_current_limit < 0 // discharging allowed - && bus->voltage < charger->num_batteries * - (bat_conf->voltage_load_disconnect - bus->current * bus->dis_droop_res)) + if (port->dis_current_limit < 0 // discharging allowed + && bus->voltage < num_batteries * + (bat_conf->voltage_load_disconnect - port->current * port->dis_droop_res)) { // low state of charge - bus->dis_current_limit = 0; - charger->num_deep_discharges++; + port->dis_current_limit = 0; + num_deep_discharges++; - if (charger->usable_capacity < 0.1) { + if (usable_capacity < 0.1) { // reset to measured value if discharged the first time - if (charger->first_full_charge_reached) { - charger->usable_capacity = charger->discharged_Ah; + if (first_full_charge_reached) { + usable_capacity = discharged_Ah; } - } else { + } + else { // slowly adapt new measurements with low-pass filter - charger->usable_capacity = - 0.8 * charger->usable_capacity + - 0.2 * charger->discharged_Ah; + usable_capacity = + 0.8 * usable_capacity + + 0.2 * discharged_Ah; } // simple SOH estimation - charger->soh = charger->usable_capacity / bat_conf->nominal_capacity; + soh = usable_capacity / bat_conf->nominal_capacity; } - else if (bus->dis_current_limit < 0 - && (charger->bat_temperature > bat_conf->discharge_temp_max - || charger->bat_temperature < bat_conf->discharge_temp_min)) + else if (port->dis_current_limit < 0 + && (bat_temperature > bat_conf->discharge_temp_max + || bat_temperature < bat_conf->discharge_temp_min)) { // temperature limits exceeded - bus->dis_current_limit = 0; + port->dis_current_limit = 0; } - if (bus->voltage >= charger->num_batteries * - (bat_conf->voltage_load_reconnect - bus->current * bus->dis_droop_res) - && charger->bat_temperature < bat_conf->discharge_temp_max - 1 - && charger->bat_temperature > bat_conf->discharge_temp_min + 1) + if (bus->voltage >= num_batteries * + (bat_conf->voltage_load_reconnect - port->current * port->dis_droop_res) + && bat_temperature < bat_conf->discharge_temp_max - 1 + && bat_temperature > bat_conf->discharge_temp_min + 1) { // discharge current is stored as absolute value in bat_conf, but defined // as negative current for dc_bus - bus->dis_current_limit = -bat_conf->discharge_current_max; + port->dis_current_limit = -bat_conf->discharge_current_max; } } -void battery_charge_control(DcBus *bus, BatConf *bat_conf, Charger *charger) +void Charger::charge_control(BatConf *bat_conf) { // check battery temperature for charging direction - if (charger->bat_temperature > bat_conf->charge_temp_max - || charger->bat_temperature < bat_conf->charge_temp_min) + if (bat_temperature > bat_conf->charge_temp_max + || bat_temperature < bat_conf->charge_temp_min) { - bus->chg_current_limit = 0; - _enter_state(charger, CHG_STATE_IDLE); + port->chg_current_limit = 0; + enter_state(CHG_STATE_IDLE); } // state machine - switch (charger->state) { + switch (state) { case CHG_STATE_IDLE: { - if (bus->voltage < bat_conf->voltage_recharge * charger->num_batteries - && (time(NULL) - charger->time_state_changed) > bat_conf->time_limit_recharge - && charger->bat_temperature < bat_conf->charge_temp_max - 1 - && charger->bat_temperature > bat_conf->charge_temp_min + 1) + if (bus->voltage < bat_conf->voltage_recharge * num_batteries + && (time(NULL) - time_state_changed) > bat_conf->time_limit_recharge + && bat_temperature < bat_conf->charge_temp_max - 1 + && bat_temperature > bat_conf->charge_temp_min + 1) { - bus->chg_voltage_target = charger->num_batteries * (bat_conf->topping_voltage + - bat_conf->temperature_compensation * (charger->bat_temperature - 25)); - bus->chg_current_limit = bat_conf->charge_current_max; - charger->full = false; - _enter_state(charger, CHG_STATE_BULK); + bus->chg_voltage_target = num_batteries * (bat_conf->topping_voltage + + bat_conf->temperature_compensation * (bat_temperature - 25)); + port->chg_current_limit = bat_conf->charge_current_max; + full = false; + enter_state(CHG_STATE_BULK); } break; } case CHG_STATE_BULK: { // continuously adjust voltage setting for temperature compensation - bus->chg_voltage_target = charger->num_batteries * (bat_conf->topping_voltage + - bat_conf->temperature_compensation * (charger->bat_temperature - 25)); + bus->chg_voltage_target = num_batteries * (bat_conf->topping_voltage + + bat_conf->temperature_compensation * (bat_temperature - 25)); - if (bus->voltage > charger->num_batteries * - (bus->chg_voltage_target - bus->current * bus->chg_droop_res)) + if (bus->voltage > num_batteries * + (bus->chg_voltage_target - port->current * port->chg_droop_res)) { - _enter_state(charger, CHG_STATE_TOPPING); + enter_state(CHG_STATE_TOPPING); } break; } case CHG_STATE_TOPPING: { // continuously adjust voltage setting for temperature compensation - bus->chg_voltage_target = charger->num_batteries * (bat_conf->topping_voltage + - bat_conf->temperature_compensation * (charger->bat_temperature - 25)); + bus->chg_voltage_target = num_batteries * (bat_conf->topping_voltage + + bat_conf->temperature_compensation * (bat_temperature - 25)); - if (bus->voltage >= charger->num_batteries * - (bus->chg_voltage_target - bus->current * bus->chg_droop_res)) + if (bus->voltage >= num_batteries * + (bus->chg_voltage_target - port->current * port->chg_droop_res)) { - charger->time_voltage_limit_reached = time(NULL); + time_voltage_limit_reached = time(NULL); } // cut-off limit reached because battery full (i.e. CV limit still // reached by available solar power within last 2s) or CV period long enough? - if ((bus->current < bat_conf->topping_current_cutoff && - (time(NULL) - charger->time_voltage_limit_reached) < 2) - || (time(NULL) - charger->time_state_changed) > bat_conf->topping_duration) + if ((port->current < bat_conf->topping_current_cutoff && + (time(NULL) - time_voltage_limit_reached) < 2) + || (time(NULL) - time_state_changed) > bat_conf->topping_duration) { - charger->full = true; - charger->num_full_charges++; - charger->discharged_Ah = 0; // reset coulomb counter - charger->first_full_charge_reached = true; + full = true; + num_full_charges++; + discharged_Ah = 0; // reset coulomb counter + first_full_charge_reached = true; if (bat_conf->equalization_enabled && ( - (time(NULL) - charger->time_last_equalization) / (24*60*60) + (time(NULL) - time_last_equalization) / (24*60*60) >= bat_conf->equalization_trigger_days || - charger->num_deep_discharges - charger->deep_dis_last_equalization + num_deep_discharges - deep_dis_last_equalization >= bat_conf->equalization_trigger_deep_cycles)) { bus->chg_voltage_target = bat_conf->equalization_voltage; - bus->chg_current_limit = bat_conf->equalization_current_limit; - _enter_state(charger, CHG_STATE_EQUALIZATION); + port->chg_current_limit = bat_conf->equalization_current_limit; + enter_state(CHG_STATE_EQUALIZATION); } else if (bat_conf->trickle_enabled) { - bus->chg_voltage_target = charger->num_batteries * (bat_conf->trickle_voltage + - bat_conf->temperature_compensation * (charger->bat_temperature - 25)); - _enter_state(charger, CHG_STATE_TRICKLE); + bus->chg_voltage_target = num_batteries * (bat_conf->trickle_voltage + + bat_conf->temperature_compensation * (bat_temperature - 25)); + enter_state(CHG_STATE_TRICKLE); } else { - bus->chg_current_limit = 0; - _enter_state(charger, CHG_STATE_IDLE); + port->chg_current_limit = 0; + enter_state(CHG_STATE_IDLE); } } break; } case CHG_STATE_TRICKLE: { // continuously adjust voltage setting for temperature compensation - bus->chg_voltage_target = charger->num_batteries * (bat_conf->trickle_voltage + - bat_conf->temperature_compensation * (charger->bat_temperature - 25)); + bus->chg_voltage_target = num_batteries * (bat_conf->trickle_voltage + + bat_conf->temperature_compensation * (bat_temperature - 25)); - if (bus->voltage >= charger->num_batteries * - (bus->chg_voltage_target - bus->current * bus->chg_droop_res)) + if (bus->voltage >= num_batteries * + (bus->chg_voltage_target - port->current * port->chg_droop_res)) { - charger->time_voltage_limit_reached = time(NULL); + time_voltage_limit_reached = time(NULL); } - if (time(NULL) - charger->time_voltage_limit_reached > bat_conf->trickle_recharge_time) + if (time(NULL) - time_voltage_limit_reached > bat_conf->trickle_recharge_time) { - bus->chg_current_limit = bat_conf->charge_current_max; - charger->full = false; - _enter_state(charger, CHG_STATE_BULK); + port->chg_current_limit = bat_conf->charge_current_max; + full = false; + enter_state(CHG_STATE_BULK); } // assumption: trickle does not harm the battery --> never go back to idle // (for Li-ion battery: disable trickle!) @@ -426,26 +414,26 @@ void battery_charge_control(DcBus *bus, BatConf *bat_conf, Charger *charger) } case CHG_STATE_EQUALIZATION: { // continuously adjust voltage setting for temperature compensation - bus->chg_voltage_target = charger->num_batteries * (bat_conf->equalization_voltage + - bat_conf->temperature_compensation * (charger->bat_temperature - 25)); + bus->chg_voltage_target = num_batteries * (bat_conf->equalization_voltage + + bat_conf->temperature_compensation * (bat_temperature - 25)); // current or time limit for equalization reached - if (time(NULL) - charger->time_state_changed > bat_conf->equalization_duration) + if (time(NULL) - time_state_changed > bat_conf->equalization_duration) { // reset triggers - charger->time_last_equalization = time(NULL); - charger->deep_dis_last_equalization = charger->num_deep_discharges; + time_last_equalization = time(NULL); + deep_dis_last_equalization = num_deep_discharges; - charger->discharged_Ah = 0; // reset coulomb counter again + discharged_Ah = 0; // reset coulomb counter again if (bat_conf->trickle_enabled) { - bus->chg_voltage_target = charger->num_batteries * (bat_conf->trickle_voltage + - bat_conf->temperature_compensation * (charger->bat_temperature - 25)); - _enter_state(charger, CHG_STATE_TRICKLE); + bus->chg_voltage_target = num_batteries * (bat_conf->trickle_voltage + + bat_conf->temperature_compensation * (bat_temperature - 25)); + enter_state(CHG_STATE_TRICKLE); } else { - bus->chg_current_limit = 0; - _enter_state(charger, CHG_STATE_IDLE); + port->chg_current_limit = 0; + enter_state(CHG_STATE_IDLE); } } break; @@ -453,37 +441,32 @@ void battery_charge_control(DcBus *bus, BatConf *bat_conf, Charger *charger) } } -void battery_init_dc_bus(DcBus *bus, BatConf *bat, unsigned int num_batteries) +void battery_init_dc_bus(DcBus *bus, PowerPort *port, BatConf *bat, unsigned int num_batteries) { unsigned int n = (num_batteries == 2 ? 2 : 1); // only 1 or 2 allowed bus->dis_voltage_start = bat->voltage_load_reconnect * n; bus->dis_voltage_stop = bat->voltage_load_disconnect * n; - bus->dis_current_limit = -bat->discharge_current_max; + port->dis_current_limit = -bat->discharge_current_max; // negative sign for compensation of actual resistance - bus->dis_droop_res = -(bat->internal_resistance + -bat->wire_resistance) * n; + port->dis_droop_res = -(bat->internal_resistance + -bat->wire_resistance) * n; bus->chg_voltage_target = bat->topping_voltage * n; bus->chg_voltage_min = bat->voltage_absolute_min * n; - bus->chg_current_limit = bat->charge_current_max; + port->chg_current_limit = bat->charge_current_max; // negative sign for compensation of actual resistance - bus->chg_droop_res = -bat->wire_resistance * n; + port->chg_droop_res = -bat->wire_resistance * n; } -void charger_update_junction_bus(DcBus *junction, DcBus *bat, DcBus *load) +void update_dcdc_current_targets(PowerPort *dcdc_port, PowerPort *bat, PowerPort *load) { // relevant for DC/DC - junction->chg_current_limit = bat->chg_current_limit - bat->current + load->current; - junction->chg_voltage_target = bat->chg_voltage_target; - - // relevant for load output - junction->dis_voltage_start = bat->dis_voltage_start; - junction->dis_voltage_stop = bat->dis_voltage_stop; + dcdc_port->chg_current_limit = bat->chg_current_limit - bat->current + load->current; // this is a bit tricky... in theory, we could add the available DC/DC current to keep the // load switched on as long as solar power is available, even if the battery is empty. This // needs a fast point-of-load (PoL) control of the DC/DC, which is not possible (yet). - junction->dis_current_limit = bat->dis_current_limit - bat->current; + dcdc_port->dis_current_limit = bat->dis_current_limit - bat->current; } diff --git a/src/bat_charger.h b/src/bat_charger.h index 0fb6034..94bf9ce 100644 --- a/src/bat_charger.h +++ b/src/bat_charger.h @@ -26,7 +26,7 @@ #include #include -#include "dc_bus.h" +#include "power_port.h" /** Battery cell types */ @@ -232,14 +232,21 @@ typedef struct /** Charger configuration and battery state */ -typedef struct +class Charger { +public: + Charger(PowerPort *pwr_port): + port(pwr_port), bus(pwr_port->bus) {}; + + PowerPort *port; + DcBus *bus; + unsigned int state; ///< Current charger state (see enum ChargerState) - int num_batteries; ///< Used for automatic 12V/24V battery detection at + int num_batteries = 1; ///< Used for automatic 12V/24V battery detection at ///< start-up (can be 1 or 2 only) - float bat_temperature; ///< Battery temperature in °C from ext. temperature sensor + float bat_temperature = 25; ///< Battery temperature in °C from ext. temperature sensor ///< (if existing) bool ext_temp_sensor; ///< True if external temperature sensor was detected @@ -250,8 +257,8 @@ typedef struct uint16_t num_full_charges; ///< Number of full charge cycles uint16_t num_deep_discharges; ///< Number of deep-discharge cycles - uint16_t soc; ///< State of Charge (%) - uint16_t soh; ///< State of Health (%) + uint16_t soc = 100; ///< State of Charge (%) + uint16_t soh = 100; ///< State of Health (%) int time_state_changed; ///< Timestamp of last state change int time_voltage_limit_reached; ///< Last time the CV limit was reached @@ -260,9 +267,31 @@ typedef struct int deep_dis_last_equalization; ///< Deep discharge counter value after last equalization bool full; ///< Flag to indicate if battery was fully charged - bool first_full_charge_reached; ///< Set to true if battery was fully charged at least once + bool first_full_charge_reached = false; + //< Set to true if battery was fully charged at least once ///< (necessary for proper capacity estimation) -} Charger; + + /** Detect if two batteries are connected in series (12V/24V auto-detection) + */ + void detect_num_batteries(BatConf *bat); + + /** Discharging control update (for load output), should be called once per second + */ + void discharge_control(BatConf *bat_conf); + + /** Charger state machine update, should be called once per second + */ + void charge_control(BatConf *bat_conf); + + /** SOC estimation + * + * Must be called exactly once per second, otherwise SOC calculation gets wrong. + */ + void update_soc(BatConf *bat_conf); + +private: + void enter_state(int next_state); +}; /** Possible charger states @@ -333,36 +362,14 @@ bool battery_conf_check(BatConf *bat); */ void battery_conf_overwrite(BatConf *source, BatConf *destination, Charger *charger = NULL); -/** SOC estimation - * - * Must be called exactly once per second, otherwise SOC calculation gets wrong. - */ -void battery_update_soc(BatConf *bat_conf, Charger *charger, DcBus *bus); - /** Initialize dc bus for battery connection * * @param num_batteries definies the number of series connected batteries, e.g. 2 for 24V system */ -void battery_init_dc_bus(DcBus *bus, BatConf *bat, unsigned int num_batteries); - -/** Basic initialization of charger struct - */ -void charger_init(Charger *charger); - -/** Detect if two batteries are connected in series (12V/24V auto-detection) - */ -void charger_detect_num_batteries(Charger *charger, BatConf *bat, DcBus *bus); - -/** Discharging control update (for load output), should be called once per second - */ -void battery_discharge_control(DcBus *bus, BatConf *bat_conf, Charger *charger); - -/** Charger state machine update, should be called once per second - */ -void battery_charge_control(DcBus *bus, BatConf *bat_conf, Charger *charger); +void battery_init_dc_bus(DcBus *bus, PowerPort *port, BatConf *bat, unsigned int num_batteries); /** Calculates targets for junction of load and battery bus at the DC/DC output */ -void charger_update_junction_bus(DcBus *junction, DcBus *bat, DcBus *load); +void update_dcdc_current_targets(PowerPort *dcdc_port, PowerPort *bat, PowerPort *load); #endif /* BAT_CHARGER_H */ diff --git a/src/data_objects.cpp b/src/data_objects.cpp index 9ed38ef..276d66a 100644 --- a/src/data_objects.cpp +++ b/src/data_objects.cpp @@ -122,8 +122,8 @@ const data_object_t data_objects[] = { {0x06, TS_OUTPUT, TS_READ_ALL, TS_T_UINT16, 0, (void*) &(charger.soc), "SOC_%"}, // output will be uint8_t // battery related data objects - {0x70, TS_OUTPUT, TS_READ_ALL, TS_T_FLOAT32, 2, (void*) &(lv_terminal.voltage), "Bat_V"}, - {0x71, TS_OUTPUT, TS_READ_ALL, TS_T_FLOAT32, 2, (void*) &(hv_terminal.voltage), "Solar_V"}, + {0x70, TS_OUTPUT, TS_READ_ALL, TS_T_FLOAT32, 2, (void*) &(lv_bus.voltage), "Bat_V"}, + {0x71, TS_OUTPUT, TS_READ_ALL, TS_T_FLOAT32, 2, (void*) &(hv_bus.voltage), "Solar_V"}, {0x72, TS_OUTPUT, TS_READ_ALL, TS_T_FLOAT32, 2, (void*) &(lv_terminal.current), "Bat_A"}, {0x73, TS_OUTPUT, TS_READ_ALL, TS_T_FLOAT32, 2, (void*) &(load_terminal.current), "Load_A"}, {0x74, TS_OUTPUT, TS_READ_ALL, TS_T_FLOAT32, 1, (void*) &(charger.bat_temperature), "Bat_degC"}, @@ -135,7 +135,7 @@ const data_object_t data_objects[] = { {0x78, TS_OUTPUT, TS_READ_ALL, TS_T_UINT16, 0, (void*) &(charger.state), "ChgState"}, {0x79, TS_OUTPUT, TS_READ_ALL, TS_T_UINT16, 0, (void*) &(dcdc.state), "DCDCState"}, {0x7A, TS_OUTPUT, TS_READ_ALL, TS_T_FLOAT32, 2, (void*) &(hv_terminal.current), "Solar_A"}, - {0x7B, TS_OUTPUT, TS_READ_ALL, TS_T_FLOAT32, 2, (void*) &(lv_terminal.chg_voltage_target), "BatTarget_V"}, + {0x7B, TS_OUTPUT, TS_READ_ALL, TS_T_FLOAT32, 2, (void*) &(lv_bus.chg_voltage_target), "BatTarget_V"}, {0x7C, TS_OUTPUT, TS_READ_ALL, TS_T_FLOAT32, 2, (void*) &(lv_terminal.chg_current_limit), "BatTarget_A"}, {0x7D, TS_OUTPUT, TS_READ_ALL, TS_T_FLOAT32, 2, (void*) &(lv_terminal.power), "Bat_W"}, {0x7E, TS_OUTPUT, TS_READ_ALL, TS_T_FLOAT32, 2, (void*) &(hv_terminal.power), "Solar_W"}, diff --git a/src/dc_bus.h b/src/dc_bus.h deleted file mode 100644 index f1a76b7..0000000 --- a/src/dc_bus.h +++ /dev/null @@ -1,75 +0,0 @@ -/* LibreSolar charge controller firmware - * Copyright (c) 2016-2019 Martin Jäger (www.libre.solar) - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#ifndef DC_BUS_H -#define DC_BUS_H - -/** @file - * - * @brief Definition of power converter terminals (e.g. high voltage or low voltage side of a DC/DC) - */ - -#include - -/** DC bus type - * - * Saves current target settings of either high-side or low-side port of a - * DC/DC converter. In this way, e.g. a battery can be configured to be - * connected to high or low side of DC/DC converter w/o having to rewrite - * the control algorithm. - */ -typedef struct { - float voltage; ///< measured voltage - float current; ///< sum of currents (positive sign = increasing voltage/charge of the bus) - float power; ///< multiplication of power & voltage - - float chg_voltage_target; ///< target voltage if port is configured as output - float chg_droop_res; ///< v_target = v_out_max - r_droop_output * current - float chg_voltage_min; ///< minimum voltage to allow current output (necessary - ///< to prevent charging of deep-discharged Li-ion batteries) - float chg_current_limit; ///< current charging the bus, i.e. increasing its voltage - ///< (equals charging direction for battery terminal) - - float dis_voltage_start; ///< minimum voltage to allow current input (= discharging of batteries), - ///< starting point for discharging of batteries (load reconnect) - float dis_voltage_stop; ///< absolute minimum = load disconnect for batteries - float dis_droop_res; ///< v_stop = v_input_stop - r_droop_input * current - float dis_current_limit; ///< current which discharges the bus, i.e. decreases its - ///< voltage, value must be negative!!! - - float chg_energy_Wh; ///< cumulated energy in charge direction since last counter reset (Wh) - float dis_energy_Wh; ///< cumulated energy in discharge direction since last counter reset (Wh) - -} DcBus; - -/** Initialize dc bus for solar panel connection - * - * @param bus The DC bus struct to initialize - * @param max_abs_current Maximum input current allowed by PCB (as a positive value) - */ -void dc_bus_init_solar(DcBus *bus, float max_abs_current); - -/** Initialize dc bus for nanogrid connection - */ -void dc_bus_init_nanogrid(DcBus *bus); - -/** Energy balance calculation for dc bus - * - * Must be called exactly once per second, otherwise energy calculation gets wrong. - */ -void dc_bus_energy_balance(DcBus *bus); - -#endif /* DC_BUS_H */ diff --git a/src/dcdc.cpp b/src/dcdc.cpp index 41b6e5c..a86d9d1 100644 --- a/src/dcdc.cpp +++ b/src/dcdc.cpp @@ -18,8 +18,6 @@ #include "pcb.h" #include "log.h" -#ifndef CHARGER_TYPE_PWM - #include "half_bridge.h" #include // for time(NULL) function @@ -29,127 +27,133 @@ extern LogData log_data; -void dcdc_init(Dcdc *dcdc, DcBus *hv, DcBus *lv, DcdcOperationMode mode) +#ifdef CHARGER_TYPE_PWM + +Dcdc::Dcdc(PowerPort *hv_side, PowerPort *lv_side, DcdcOperationMode op_mode) {} + +#else + +Dcdc::Dcdc(PowerPort *hv_side, PowerPort *lv_side, DcdcOperationMode op_mode) { - dcdc->hv_bus = hv; - dcdc->lv_bus = lv; - dcdc->mode = mode; - dcdc->enabled = true; - dcdc->state = DCDC_STATE_OFF; - dcdc->ls_current_max = DCDC_CURRENT_MAX; - dcdc->hs_voltage_max = HIGH_SIDE_VOLTAGE_MAX; - dcdc->ls_voltage_max = LOW_SIDE_VOLTAGE_MAX; - dcdc->ls_voltage_min = 9.0; - dcdc->output_power_min = 1; // switch off iff power < 1 W - dcdc->restart_interval = 60; - dcdc->off_timestamp = -10000; // start immediately - dcdc->pwm_delta = 1; // start-condition of duty cycle pwr_inc_pwm_direction + hvs = hv_side; + lvs = lv_side; + mode = op_mode; + enabled = true; + state = DCDC_STATE_OFF; + ls_current_max = DCDC_CURRENT_MAX; + hs_voltage_max = HIGH_SIDE_VOLTAGE_MAX; + ls_voltage_max = LOW_SIDE_VOLTAGE_MAX; + ls_voltage_min = 9.0; + output_power_min = 1; // switch off iff power < 1 W + restart_interval = 60; + off_timestamp = -10000; // start immediately + pwm_delta = 1; // start-condition of duty cycle pwr_inc_pwm_direction } -/** Calculates the duty cycle change depending on operating mode and actual measurements - * - * @returns duty cycle step (-1 or 1) or 0 if DC/DC should be switched off - */ -int duty_cycle_delta(Dcdc *dcdc) +int Dcdc::duty_cycle_delta() { int pwr_inc_pwm_direction; // direction of PWM duty cycle change for increasing power - DcBus *in; - DcBus *out; + PowerPort *in; + PowerPort *out; - if (dcdc->mode == MODE_MPPT_BUCK || - (dcdc->mode == MODE_NANOGRID && dcdc->lv_bus->current > 0.1)) + if (mode == MODE_MPPT_BUCK || + (mode == MODE_NANOGRID && lvs->current > 0.1)) { // buck mode pwr_inc_pwm_direction = 1; - in = dcdc->hv_bus; - out = dcdc->lv_bus; + in = hvs; + out = lvs; } else { // boost mode pwr_inc_pwm_direction = -1; - in = dcdc->lv_bus; - out = dcdc->hv_bus; + in = lvs; + out = hvs; } - if (out->power >= dcdc->output_power_min) { - dcdc->power_good_timestamp = time(NULL); // reset the time + if (out->power >= output_power_min) { + power_good_timestamp = time(NULL); // reset the time } int pwr_inc_goal = 0; // stores if we want to increase (+1) or decrease (-1) power #if 0 - printf("P: %.2f, P_prev: %.2f, v_in: %.2f, v_out: %.2f, i_in: %.2f, i_out: %.2f, v_chg_target: %.2f, i_chg_max: %.2f, PWM: %.1f, chg_state: %d\n", - out->power, dcdc->power_prev, in->voltage, out->voltage, in->current, out->current, out->chg_voltage_target, - out->chg_current_limit, half_bridge_get_duty_cycle() * 100.0, dcdc->state); + printf("P: %.2f, P_prev: %.2f, v_in: %.2f, v_out: %.2f, i_in: %.2f, i_out: %.2f, " + "v_chg_target: %.2f, i_chg_max: %.2f, PWM: %.1f, chg_state: %d\n", + out->power, power_prev, in->bus->voltage, out->bus->voltage, in->current, out->current, + out->bus->chg_voltage_target, out->chg_current_limit, half_bridge_get_duty_cycle() * 100.0, + state); #endif - if (time(NULL) - dcdc->power_good_timestamp > 10 || // more than 10s low power + if (time(NULL) - power_good_timestamp > 10 || // more than 10s low power out->power < -1.0) // or already generating negative power { pwr_inc_goal = 0; // switch off } - else if (out->voltage > (out->chg_voltage_target - out->chg_droop_res * out->current) // output voltage above target - || (in->voltage < (in->dis_voltage_start - in->dis_droop_res * in->current) && out->current > 0.1)) // input voltage below limit + else if (out->bus->voltage > (out->bus->chg_voltage_target - out->chg_droop_res * out->current)) { - dcdc->state = DCDC_STATE_CV; + // output voltage target reached + state = DCDC_STATE_CV; pwr_inc_goal = -1; // decrease output power } - else if (out->current > out->chg_current_limit // output charge current limit exceeded - || in->current < in->dis_current_limit) // input current (negative signs) limit exceeded + else if (out->current > out->chg_current_limit) { - dcdc->state = DCDC_STATE_CC; + // output charge current limit reached + state = DCDC_STATE_CC; pwr_inc_goal = -1; // decrease output power } - else if (fabs(dcdc->lv_bus->current) > dcdc->ls_current_max // current above hardware maximum - || dcdc->temp_mosfets > 80) // temperature limits exceeded + else if (fabs(lvs->current) > ls_current_max // current above hardware maximum + || temp_mosfets > 80 // temperature limits exceeded + || (in->bus->voltage < (in->bus->dis_voltage_start - in->dis_droop_res * in->current) + && out->current > 0.1) // input voltage below limit + || in->current < in->dis_current_limit) // input current (negative signs) limit exceeded { - dcdc->state = DCDC_STATE_DERATING; + state = DCDC_STATE_DERATING; pwr_inc_goal = -1; // decrease output power } - else if (out->power < dcdc->output_power_min - && out->voltage < out->dis_voltage_start) // no load condition (e.g. start-up of nanogrid) - // --> raise voltage + else if (out->power < output_power_min && out->bus->voltage < out->bus->dis_voltage_start) { + // no load condition (e.g. start-up of nanogrid) --> raise voltage pwr_inc_goal = 1; // increase output power } else { // start MPPT - dcdc->state = DCDC_STATE_MPPT; - if (dcdc->power_prev > out->power) { - dcdc->pwm_delta = -dcdc->pwm_delta; + state = DCDC_STATE_MPPT; + if (power_prev > out->power) { + pwm_delta = -pwm_delta; } - pwr_inc_goal = dcdc->pwm_delta; + pwr_inc_goal = pwm_delta; } - // store the power BEFORE the executing the current change - dcdc->power_prev = out->power; + + power_prev = out->power; return pwr_inc_goal * pwr_inc_pwm_direction; } -int dcdc_check_start_conditions(Dcdc *dcdc) +int Dcdc::check_start_conditions() { - if (dcdc->enabled == false || - dcdc->hv_bus->voltage > dcdc->hs_voltage_max || // also critical for buck mode because of ringing - dcdc->lv_bus->voltage > dcdc->ls_voltage_max || - dcdc->lv_bus->voltage < dcdc->ls_voltage_min || - time(NULL) < (dcdc->off_timestamp + dcdc->restart_interval)) + if (enabled == false || + hvs->bus->voltage > hs_voltage_max || // also critical for buck mode because of ringing + lvs->bus->voltage > ls_voltage_max || + lvs->bus->voltage < ls_voltage_min || + time(NULL) < (off_timestamp + restart_interval)) { return 0; // no energy transfer allowed } - if (dcdc->lv_bus->chg_current_limit > 0 && - dcdc->lv_bus->voltage < dcdc->lv_bus->chg_voltage_target && - dcdc->lv_bus->voltage > dcdc->lv_bus->chg_voltage_min && - dcdc->hv_bus->dis_current_limit < 0 && - dcdc->hv_bus->voltage > dcdc->hv_bus->dis_voltage_start) + if (lvs->chg_current_limit > 0 && + lvs->bus->voltage < lvs->bus->chg_voltage_target && + lvs->bus->voltage > lvs->bus->chg_voltage_min && + hvs->dis_current_limit < 0 && + hvs->bus->voltage > hvs->bus->dis_voltage_start) { return 1; // start in buck mode allowed } - if (dcdc->hv_bus->chg_current_limit > 0 && - dcdc->hv_bus->voltage < dcdc->hv_bus->chg_voltage_target && - dcdc->hv_bus->voltage > dcdc->hv_bus->chg_voltage_min && - dcdc->lv_bus->dis_current_limit < 0 && - dcdc->lv_bus->voltage > dcdc->lv_bus->dis_voltage_start) + if (hvs->chg_current_limit > 0 && + hvs->bus->voltage < hvs->bus->chg_voltage_target && + hvs->bus->voltage > hvs->bus->chg_voltage_min && + lvs->dis_current_limit < 0 && + lvs->bus->voltage > lvs->bus->dis_voltage_start) { return -1; // start in boost mode allowed } @@ -157,33 +161,33 @@ int dcdc_check_start_conditions(Dcdc *dcdc) return 0; } -void dcdc_control(Dcdc *dcdc) +void Dcdc::control() { if (half_bridge_enabled()) { - int step = duty_cycle_delta(dcdc); + int step = duty_cycle_delta(); half_bridge_duty_cycle_step(step); if (step == 0) { half_bridge_stop(); - dcdc->state = DCDC_STATE_OFF; - dcdc->off_timestamp = time(NULL); + state = DCDC_STATE_OFF; + off_timestamp = time(NULL); printf("DC/DC stop (low power).\n"); } - else if (dcdc->lv_bus->voltage > dcdc->ls_voltage_max || dcdc->hv_bus->voltage > dcdc->hs_voltage_max) { + else if (lvs->bus->voltage > ls_voltage_max || hvs->bus->voltage > hs_voltage_max) { half_bridge_stop(); - dcdc->state = DCDC_STATE_OFF; - dcdc->off_timestamp = time(NULL); + state = DCDC_STATE_OFF; + off_timestamp = time(NULL); printf("DC/DC emergency stop (voltage limits exceeded).\n"); } - else if (dcdc->enabled == false) { + else if (enabled == false) { half_bridge_stop(); - dcdc->state = DCDC_STATE_OFF; + state = DCDC_STATE_OFF; printf("DC/DC stop (disabled).\n"); } } else { static int current_debounce_counter = 0; - if (dcdc->lv_bus->current > 0.5) { + if (lvs->current > 0.5) { // if there is current even though the DC/DC is switched off, the // high-side MOSFET must be broken --> set flag and let main() decide // what to do... (e.g. call dcdc_self_destruction) @@ -198,15 +202,20 @@ void dcdc_control(Dcdc *dcdc) } static int startup_delay_counter = 0; - static const int num_wait_calls = (CONTROL_FREQUENCY / 10 >= 1) ? (CONTROL_FREQUENCY / 10) : 1; // wait at least 100 ms for voltages to settle - int startup_mode = dcdc_check_start_conditions(dcdc); + // wait at least 100 ms for voltages to settle + static const int num_wait_calls = + (CONTROL_FREQUENCY / 10 >= 1) ? (CONTROL_FREQUENCY / 10) : 1; + + int startup_mode = check_start_conditions(); if (startup_mode == 1) { if (startup_delay_counter >= num_wait_calls) { - dcdc->power_good_timestamp = time(NULL); - // Don't start directly at Vmpp (approx. 0.8 * Voc) to prevent high inrush currents and stress on MOSFETs - half_bridge_start(dcdc->lv_bus->voltage / (dcdc->hv_bus->voltage - 1)); - printf("DC/DC buck mode start (HV: %.2fV, LV: %.2fV, PWM: %.1f).\n", dcdc->hv_bus->voltage, dcdc->lv_bus->voltage, half_bridge_get_duty_cycle() * 100); + power_good_timestamp = time(NULL); + // Don't start directly at Vmpp (approx. 0.8 * Voc) to prevent high inrush currents + // and stress on MOSFETs + half_bridge_start(lvs->bus->voltage / (hvs->bus->voltage - 1)); + printf("DC/DC buck mode start (HV: %.2fV, LV: %.2fV, PWM: %.1f).\n", + hvs->bus->voltage, lvs->bus->voltage, half_bridge_get_duty_cycle() * 100); } else { startup_delay_counter++; @@ -214,10 +223,12 @@ void dcdc_control(Dcdc *dcdc) } else if (startup_mode == -1) { if (startup_delay_counter >= num_wait_calls) { - dcdc->power_good_timestamp = time(NULL); - // will automatically start with max. duty (0.97) if connected to a nanogrid not yet started up (zero voltage) - half_bridge_start(dcdc->lv_bus->voltage / (dcdc->hv_bus->voltage + 1)); - printf("DC/DC boost mode start (HV: %.2fV, LV: %.2fV, PWM: %.1f).\n", dcdc->hv_bus->voltage, dcdc->lv_bus->voltage, half_bridge_get_duty_cycle() * 100); + power_good_timestamp = time(NULL); + // will automatically start with max. duty (0.97) if connected to a nanogrid not + // yet started up (zero voltage) + half_bridge_start(lvs->bus->voltage / (hvs->bus->voltage + 1)); + printf("DC/DC boost mode start (HV: %.2fV, LV: %.2fV, PWM: %.1f).\n", + hvs->bus->voltage, lvs->bus->voltage, half_bridge_get_duty_cycle() * 100); } else { startup_delay_counter++; @@ -229,7 +240,7 @@ void dcdc_control(Dcdc *dcdc) } } -void dcdc_test(Dcdc *dcdc) +void Dcdc::test() { if (half_bridge_enabled()) { if (half_bridge_get_duty_cycle() > 0.5) { @@ -238,14 +249,18 @@ void dcdc_test(Dcdc *dcdc) } else { static int startup_delay_counter = 0; - static const int num_wait_calls = (CONTROL_FREQUENCY / 10 >= 1) ? (CONTROL_FREQUENCY / 10) : 1; // wait at least 100 ms for voltages to settle - int startup_mode = dcdc_check_start_conditions(dcdc); + // wait at least 100 ms for voltages to settle + static const int num_wait_calls = (CONTROL_FREQUENCY / 10 >= 1) ? (CONTROL_FREQUENCY / 10) : 1; + + int startup_mode = check_start_conditions(); if (startup_mode == 1) { if (startup_delay_counter >= num_wait_calls) { - // Don't start directly at Vmpp (approx. 0.8 * Voc) to prevent high inrush currents and stress on MOSFETs - half_bridge_start(dcdc->lv_bus->voltage / (dcdc->hv_bus->voltage - 1)); - printf("DC/DC buck mode start (HV: %.2fV, LV: %.2fV, PWM: %.1f).\n", dcdc->hv_bus->voltage, dcdc->lv_bus->voltage, half_bridge_get_duty_cycle() * 100); + // Don't start directly at Vmpp (approx. 0.8 * Voc) to prevent high inrush currents + // and stress on MOSFETs + half_bridge_start(lvs->bus->voltage / (hvs->bus->voltage - 1)); + printf("DC/DC buck mode start (HV: %.2fV, LV: %.2fV, PWM: %.1f).\n", + hvs->bus->voltage, lvs->bus->voltage, half_bridge_get_duty_cycle() * 100); } else { startup_delay_counter++; @@ -253,9 +268,11 @@ void dcdc_test(Dcdc *dcdc) } else if (startup_mode == -1) { if (startup_delay_counter >= num_wait_calls) { - // will automatically start with max. duty (0.97) if connected to a nanogrid not yet started up (zero voltage) - half_bridge_start(dcdc->lv_bus->voltage / (dcdc->hv_bus->voltage + 1)); - printf("DC/DC boost mode start (HV: %.2fV, LV: %.2fV, PWM: %.1f).\n", dcdc->hv_bus->voltage, dcdc->lv_bus->voltage, half_bridge_get_duty_cycle() * 100); + // will automatically start with max. duty (0.97) if connected to a nanogrid not + // yet started up (zero voltage) + half_bridge_start(lvs->bus->voltage / (hvs->bus->voltage + 1)); + printf("DC/DC boost mode start (HV: %.2fV, LV: %.2fV, PWM: %.1f).\n", + hvs->bus->voltage, lvs->bus->voltage, half_bridge_get_duty_cycle() * 100); } else { startup_delay_counter++; @@ -267,7 +284,7 @@ void dcdc_test(Dcdc *dcdc) } } -void dcdc_self_destruction() +void Dcdc::self_destruction() { printf("Charge controller self-destruction called!\n"); //half_bridge_stop(); diff --git a/src/dcdc.h b/src/dcdc.h index a6f7e39..1d7ac62 100644 --- a/src/dcdc.h +++ b/src/dcdc.h @@ -25,7 +25,7 @@ #include #include -#include "dc_bus.h" +#include "power_port.h" /** DC/DC basic operation mode * @@ -54,19 +54,55 @@ enum DcdcControlState DCDC_STATE_DERATING ///< Hardware-limits (current or temperature) reached }; -/** DC/DC type +/** DC/DC class * * Contains all data belonging to the DC/DC sub-component of the PCB, incl. * actual measurements and calibration parameters. */ -typedef struct { +class Dcdc +{ +public: + /** Initialize DC/DC and DC/DC port structs + * + * See http://libre.solar/docs/dcdc_control for detailed information + * + * @param hv_side High voltage terminal (e.g. solar input for MPPT buck) + * @param lv_side Low voltage terminal (e.g. battery output for MPPT buck) + * @param mode Operation mode (buck, boost or nanogrid) + */ + Dcdc(PowerPort *hv_side, PowerPort* lv_side, DcdcOperationMode mode); + + /** Check for valid start conditions of the DC/DC converter + * + * @returns 1 for buck mode, -1 for boost mode and 0 for invalid conditions + */ + int check_start_conditions(); + + /** Main control function for the DC/DC converter + * + * If DC/DC is off, this function checks start conditions and starts conversion if possible. + */ + void control(); + + /** Test mode for DC/DC, ramping up to 50% duty cycle + */ + void test(); + + /** Prevent overcharging of battery in case of shorted HS MOSFET + * + * This function switches the LS MOSFET continuously on to blow the battery input fuse. The + * reason for self destruction should be logged and stored to EEPROM prior to calling this + * function, as the charge controller power supply will be cut after the fuse is destroyed. + */ + void self_destruction(); + DcdcOperationMode mode; ///< DC/DC mode (buck, boost or nanogrid) bool enabled; ///< Can be used to disable the DC/DC power stage uint16_t state; ///< Control state (off / MPPT / CC / CV) // actual measurements - DcBus *hv_bus; ///< Pointer to DC bus at high voltage side - DcBus *lv_bus; ///< Pointer to DC bus at low voltage (inductor) side + PowerPort *hvs; ///< Pointer to DC bus at high voltage side + PowerPort *lvs; ///< Pointer to DC bus at low voltage (inductor) side float temp_mosfets; ///< MOSFET temperature measurement (if existing) // current state @@ -85,47 +121,13 @@ typedef struct { // calibration parameters int restart_interval; ///< Restart interval (s): When should we retry to start charging ///< after low output power cut-off? -} Dcdc; - -/** Initialize DC/DC and DC/DC port structs - * - * See http://libre.solar/docs/dcdc_control for detailed information - * - * @param dcdc DC/DC object - * @param hv High voltage terminal (e.g. solar input for MPPT buck) - * @param lv Low voltage terminal (e.g. battery output for MPPT buck) - * @param mode Operation mode (buck, boost or nanogrid) - */ -void dcdc_init(Dcdc *dcdc, DcBus *hv, DcBus *lv, DcdcOperationMode mode); - -/** Check for valid start conditions of the DC/DC converter - * - * @param dcdc DC/DC object incl. measurement data - * @returns 1 for buck mode, -1 for boost mode and 0 for invalid conditions - */ -int dcdc_check_start_conditions(Dcdc *dcdc); - -/** Main control function for the DC/DC converter - * - * If DC/DC is off, this function checks start conditions and starts conversion if possible. - * - * @param dcdc DC/DC object incl. measurement data - */ -void dcdc_control(Dcdc *dcdc); - -/** Test mode for DC/DC, ramping up to 50% duty cycle - * - * @param dcdc DC/DC object incl. measurement data - */ -void dcdc_test(Dcdc *dcdc); - -/** Prevent overcharging of battery in case of shorted HS MOSFET - * - * This function switches the LS MOSFET continuously on to blow the battery input fuse. The reason for self destruction should - * be logged and stored to EEPROM prior to calling this function, as the charge controller power supply will be cut after the - * fuse is destroyed. - */ -void dcdc_self_destruction(); +private: + /** Calculates the duty cycle change depending on operating mode and actual measurements + * + * @returns duty cycle step (-1 or 1) or 0 if DC/DC should be switched off + */ + int duty_cycle_delta(); +}; #endif /* DCDC_H */ diff --git a/src/hardware.cpp b/src/hardware.cpp index 675a891..2fac443 100644 --- a/src/hardware.cpp +++ b/src/hardware.cpp @@ -25,6 +25,8 @@ #include "us_ticker_data.h" #include "leds.h" +extern LoadOutput load; + #if defined(STM32F0) void control_timer_start(int freq_Hz) // max. 10 kHz @@ -152,8 +154,8 @@ void init_watchdog(float timeout) { void mbed_die(void) { half_bridge_stop(); - load_switch_set(false); - load_usb_set(false); + load.switch_set(false); + load.usb_set(false); leds_init(false); diff --git a/src/load.cpp b/src/load.cpp index 4dae0c6..02887e3 100644 --- a/src/load.cpp +++ b/src/load.cpp @@ -24,6 +24,9 @@ volatile bool short_circuit = false; +extern LogData log_data; +extern float mcu_temp; + #ifndef UNIT_TEST #if defined(PIN_I_LOAD_COMP) && PIN_LOAD_DIS == PB_2 @@ -141,7 +144,7 @@ extern "C" void ADC1_COMP_IRQHandler(void) #endif // PIN_I_LOAD_COMP -void load_switch_set(bool enabled) +void LoadOutput::switch_set(bool enabled) { leds_set(LED_LOAD, enabled); @@ -163,7 +166,7 @@ void load_switch_set(bool enabled) #endif } -void load_usb_set(bool enabled) +void LoadOutput::usb_set(bool enabled) { #ifdef PIN_USB_PWR_EN DigitalOut usb_pwr_en(PIN_USB_PWR_EN); @@ -181,67 +184,67 @@ void load_usb_set(bool enabled) // dummy functions static void short_circuit_comp_init() {;} -void load_switch_set(bool enabled) {;} -void load_usb_set(bool enabled) {;} +void LoadOutput::switch_set(bool enabled) {;} +void LoadOutput::usb_set(bool enabled) {;} #endif -void load_init(LoadOutput *load, DcBus *bus_int, DcBus *terminal) +LoadOutput::LoadOutput(PowerPort *pwr_port) { - load->bus_int = bus_int; - load->terminal = terminal; - load->current_max = LOAD_CURRENT_MAX; - load->enabled_target = true; - load->usb_enabled_target = true; - load_switch_set(false); - load_usb_set(false); - load->switch_state = LOAD_STATE_DISABLED; - load->usb_state = LOAD_STATE_DISABLED; - load->junction_temperature = 25; + port = pwr_port; + bus = pwr_port->bus; + current_max = LOAD_CURRENT_MAX; + enabled_target = true; + usb_enabled_target = true; + switch_set(false); + usb_set(false); + switch_state = LOAD_STATE_DISABLED; + usb_state = LOAD_STATE_DISABLED; + junction_temperature = 25; // analog comparator to detect short circuits and trigger immediate load switch-off short_circuit_comp_init(); } -void usb_state_machine(LoadOutput *load) +void LoadOutput::usb_state_machine() { - switch (load->usb_state) { + switch (usb_state) { case LOAD_STATE_DISABLED: - if (load->usb_enabled_target == true) { - load_usb_set(true); - load->usb_state = LOAD_STATE_ON; + if (usb_enabled_target == true) { + usb_set(true); + usb_state = LOAD_STATE_ON; } break; case LOAD_STATE_ON: // currently still same cut-off SOC limit as the load - if (load->switch_state == LOAD_STATE_OFF_LOW_SOC) { - load_usb_set(false); - load->usb_state = LOAD_STATE_OFF_LOW_SOC; + if (switch_state == LOAD_STATE_OFF_LOW_SOC) { + usb_set(false); + usb_state = LOAD_STATE_OFF_LOW_SOC; } - else if (load->switch_state == LOAD_STATE_OFF_OVERCURRENT) { - load_usb_set(false); - load->usb_state = LOAD_STATE_OFF_OVERCURRENT; + else if (switch_state == LOAD_STATE_OFF_OVERCURRENT) { + usb_set(false); + usb_state = LOAD_STATE_OFF_OVERCURRENT; } - else if (load->usb_enabled_target == false) { - load_usb_set(false); - load->usb_state = LOAD_STATE_DISABLED; + else if (usb_enabled_target == false) { + usb_set(false); + usb_state = LOAD_STATE_DISABLED; } break; case LOAD_STATE_OFF_LOW_SOC: // currently still same cut-off SOC limit as the load - if (load->switch_state == LOAD_STATE_ON) { - if (load->usb_enabled_target == true) { - load_usb_set(true); - load->usb_state = LOAD_STATE_ON; + if (switch_state == LOAD_STATE_ON) { + if (usb_enabled_target == true) { + usb_set(true); + usb_state = LOAD_STATE_ON; } else { - load->usb_state = LOAD_STATE_DISABLED; + usb_state = LOAD_STATE_DISABLED; } } break; case LOAD_STATE_OFF_OVERCURRENT: - if (load->switch_state != LOAD_STATE_OFF_OVERCURRENT) { - load->usb_state = LOAD_STATE_DISABLED; + if (switch_state != LOAD_STATE_OFF_OVERCURRENT) { + usb_state = LOAD_STATE_DISABLED; } break; } @@ -249,101 +252,98 @@ void usb_state_machine(LoadOutput *load) static time_t lvd_timestamp = 0; // stores when the load was disconnected last time -void load_state_machine(LoadOutput *load) +void LoadOutput::state_machine() { - //printf("Load State: %d\n", load->switch_state); - switch (load->switch_state) { + //printf("Load State: %d\n", switch_state); + switch (switch_state) { case LOAD_STATE_DISABLED: - if (load->bus_int->dis_current_limit < 0 // discharging enabled - && load->enabled_target == true) + if (port->dis_current_limit < 0 // discharging enabled + && enabled_target == true) { - load_switch_set(true); - load->switch_state = LOAD_STATE_ON; + switch_set(true); + switch_state = LOAD_STATE_ON; } break; case LOAD_STATE_ON: - if (load->enabled_target == false) { - load_switch_set(false); - load->switch_state = LOAD_STATE_DISABLED; + if (enabled_target == false) { + switch_set(false); + switch_state = LOAD_STATE_DISABLED; } - else if (load->bus_int->dis_current_limit == 0) { // float == is allowed for 0.0 + else if (port->dis_current_limit == 0) { // float == is allowed for 0.0 lvd_timestamp = time(NULL); - load_switch_set(false); - load->switch_state = LOAD_STATE_OFF_LOW_SOC; + switch_set(false); + switch_state = LOAD_STATE_OFF_LOW_SOC; } break; case LOAD_STATE_OFF_LOW_SOC: // wait at least one hour - if (load->bus_int->dis_current_limit < 0 // discharging enabled again + if (port->dis_current_limit < 0 // discharging enabled again && time(NULL) - lvd_timestamp > 60*60) { - if (load->enabled_target == true) { - load_switch_set(true); - load->switch_state = LOAD_STATE_ON; + if (enabled_target == true) { + switch_set(true); + switch_state = LOAD_STATE_ON; } else { - load->switch_state = LOAD_STATE_DISABLED; + switch_state = LOAD_STATE_DISABLED; } } break; case LOAD_STATE_OFF_OVERCURRENT: // wait some time - if (time(NULL) > load->overcurrent_timestamp + LOAD_OVERCURRENT_RECOVERY_TIMEOUT) { - load->switch_state = LOAD_STATE_DISABLED; // switch to normal mode again + if (time(NULL) > overcurrent_timestamp + LOAD_OVERCURRENT_RECOVERY_TIMEOUT) { + switch_state = LOAD_STATE_DISABLED; // switch to normal mode again } break; case LOAD_STATE_OFF_OVERVOLTAGE: - if (load->bus_int->voltage < LOW_SIDE_VOLTAGE_MAX) { // TODO: add hysteresis? - load->switch_state = LOAD_STATE_DISABLED; // switch to normal mode again + if (bus->voltage < LOW_SIDE_VOLTAGE_MAX) { // TODO: add hysteresis? + switch_state = LOAD_STATE_DISABLED; // switch to normal mode again } break; case LOAD_STATE_OFF_SHORT_CIRCUIT: // stay here until the charge controller is reset or load is switched off remotely - if (load->enabled_target == false) { + if (enabled_target == false) { short_circuit = false; - load->switch_state = LOAD_STATE_DISABLED; // switch to normal mode again + switch_state = LOAD_STATE_DISABLED; // switch to normal mode again } break; } - usb_state_machine(load); + usb_state_machine(); } -extern LogData log_data; -extern float mcu_temp; - // this function is called more often than the state machine -void load_control(LoadOutput *load, float load_max_voltage) +void LoadOutput::control(float load_max_voltage) { if (short_circuit) { - load->switch_state = LOAD_STATE_OFF_SHORT_CIRCUIT; + switch_state = LOAD_STATE_OFF_SHORT_CIRCUIT; return; } // junction temperature calculation model for overcurrent detection - load->junction_temperature = load->junction_temperature + ( - mcu_temp - load->junction_temperature + - load->terminal->current * load->terminal->current / + junction_temperature = junction_temperature + ( + mcu_temp - junction_temperature + + port->current * port->current / (LOAD_CURRENT_MAX * LOAD_CURRENT_MAX) * (MOSFET_MAX_JUNCTION_TEMP - 25) ) / (MOSFET_THERMAL_TIME_CONSTANT * CONTROL_FREQUENCY); - if (load->junction_temperature > MOSFET_MAX_JUNCTION_TEMP) { - load_switch_set(false); - load_usb_set(false); - load->switch_state = LOAD_STATE_OFF_OVERCURRENT; - load->overcurrent_timestamp = time(NULL); + if (junction_temperature > MOSFET_MAX_JUNCTION_TEMP) { + switch_set(false); + usb_set(false); + switch_state = LOAD_STATE_OFF_OVERCURRENT; + overcurrent_timestamp = time(NULL); } static int debounce_counter = 0; - if (load->bus_int->voltage > load_max_voltage || - load->bus_int->voltage > LOW_SIDE_VOLTAGE_MAX) + if (bus->voltage > load_max_voltage || + bus->voltage > LOW_SIDE_VOLTAGE_MAX) { debounce_counter++; if (debounce_counter > CONTROL_FREQUENCY) { // waited 1s before setting the flag - load_switch_set(false); - load_usb_set(false); - load->switch_state = LOAD_STATE_OFF_OVERVOLTAGE; - load->overcurrent_timestamp = time(NULL); + switch_set(false); + usb_set(false); + switch_state = LOAD_STATE_OFF_OVERVOLTAGE; + overcurrent_timestamp = time(NULL); log_data.error_flags |= (1 << ERR_BAT_OVERVOLTAGE); } } diff --git a/src/load.h b/src/load.h index 387999a..441e588 100644 --- a/src/load.h +++ b/src/load.h @@ -26,7 +26,7 @@ #include #include -#include "dc_bus.h" +#include "power_port.h" /** Load/USB output states * @@ -45,13 +45,45 @@ enum LoadState { * * Stores status of load output incl. 5V USB output (if existing on PCB) */ -typedef struct { +class LoadOutput +{ +public: + /** Initialize LoadOutput struct and overcurrent / short circuit protection comparator (if existing) + * + * @param load Load output struct + * @param bus_int Internal DC bus the load switch is connected to (e.g. battery and DC/DC junction) + * @param terminal Load terminal DC bus (external interface) + */ + LoadOutput(PowerPort *pwr_port); + + /** Enable/disable load switch + */ + void switch_set(bool enabled); + + /** Enable/disable USB output + */ + void usb_set(bool enabled); + + /** State machine, called every second. + */ + void state_machine(); + + /** USB state machine, called every second. + */ + void usb_state_machine(); + + /** Main load control function, should be called by control timer + * + * Performs time-critical checks like overcurrent and overvoltage + */ + void control(float load_max_voltage); + uint16_t switch_state; ///< Current state of load output switch uint16_t usb_state; ///< Current state of USB output - DcBus *bus_int; ///< Pointer to internal DC bus where the load output + DcBus *bus; ///< Pointer to internal DC bus where the load output ///< current is coming from - DcBus *terminal; ///< Pointer to DC bus containting actual voltage and current + PowerPort *port; ///< Pointer to DC bus containting actual voltage and current ///< measurement of (external) load output terminal float current_max; ///< maximum allowed current @@ -63,34 +95,6 @@ typedef struct { bool enabled_target; ///< target setting defined via communication port (overruled if ///< battery is empty) bool usb_enabled_target; ///< same for USB output -} LoadOutput; - -/** Initialize LoadOutput struct and overcurrent / short circuit protection comparator (if existing) - * - * @param load Load output struct - * @param bus_int Internal DC bus the load switch is connected to (e.g. battery and DC/DC junction) - * @param terminal Load terminal DC bus (external interface) - */ -void load_init(LoadOutput *load, DcBus *bus_int, DcBus *terminal); - -/** Enable/disable load switch - */ -void load_switch_set(bool enabled); - -/** Enable/disable USB output - */ -void load_usb_set(bool enabled); - -/** State machine, called every second. - * - * @param load Load output struct - */ -void load_state_machine(LoadOutput *load); - -/** Main load control function, should be called by control timer - * - * Performs time-critical checks like overcurrent and overvoltage - */ -void load_control(LoadOutput *load, float load_max_voltage); +}; #endif /* LOAD_H */ diff --git a/src/log.cpp b/src/log.cpp index 3815ec6..25510fe 100644 --- a/src/log.cpp +++ b/src/log.cpp @@ -18,8 +18,7 @@ #include "config.h" #include "pcb.h" -#include "dc_bus.h" -#include "load.h" +#include "main.h" #include // for fabs function #include @@ -28,7 +27,7 @@ extern float mcu_temp; //---------------------------------------------------------------------------- // must be called exactly once per second, otherwise energy calculation gets wrong -void log_update_energy(LogData *log_data, DcBus *solar, DcBus *bat, DcBus *load) +void log_update_energy(LogData *log_data, PowerPort *solar, PowerPort *bat, PowerPort *load) { // static variables so that it is not reset for each function call static int seconds_zero_solar = 0; @@ -40,7 +39,7 @@ void log_update_energy(LogData *log_data, DcBus *solar, DcBus *bat, DcBus *load) static uint32_t bat_chg_total_Wh_prev = log_data->bat_chg_total_Wh; static uint32_t bat_dis_total_Wh_prev = log_data->bat_dis_total_Wh; - if (solar->voltage < bat->voltage) { + if (solar->bus->voltage < bat->bus->voltage) { seconds_zero_solar += 1; } else { @@ -61,39 +60,44 @@ void log_update_energy(LogData *log_data, DcBus *solar, DcBus *bat, DcBus *load) seconds_zero_solar = 0; } - log_data->bat_chg_total_Wh = bat_chg_total_Wh_prev + (bat->chg_energy_Wh > 0 ? bat->chg_energy_Wh : 0); - log_data->bat_dis_total_Wh = bat_dis_total_Wh_prev + (bat->dis_energy_Wh > 0 ? bat->dis_energy_Wh : 0); - log_data->solar_in_total_Wh = solar_in_total_Wh_prev + (solar->dis_energy_Wh > 0 ? solar->dis_energy_Wh : 0); - log_data->load_out_total_Wh = load_out_total_Wh_prev + (load->chg_energy_Wh > 0 ? load->chg_energy_Wh : 0); + log_data->bat_chg_total_Wh = bat_chg_total_Wh_prev + + (bat->chg_energy_Wh > 0 ? bat->chg_energy_Wh : 0); + log_data->bat_dis_total_Wh = bat_dis_total_Wh_prev + + (bat->dis_energy_Wh > 0 ? bat->dis_energy_Wh : 0); + log_data->solar_in_total_Wh = solar_in_total_Wh_prev + + (solar->dis_energy_Wh > 0 ? solar->dis_energy_Wh : 0); + log_data->load_out_total_Wh = load_out_total_Wh_prev + + (load->chg_energy_Wh > 0 ? load->chg_energy_Wh : 0); } -void log_update_min_max_values(LogData *log_data, Dcdc *dcdc, Charger *charger, LoadOutput *load, DcBus *solar_bus, DcBus *bat_bus, DcBus *load_bus) +void log_update_min_max_values(LogData *log_data, Dcdc *dcdc, Charger *charger, LoadOutput *load, + PowerPort *solar_port, PowerPort *bat_port, PowerPort *load_port) { - if (bat_bus->voltage > log_data->battery_voltage_max) { - log_data->battery_voltage_max = bat_bus->voltage; + if (bat_port->bus->voltage > log_data->battery_voltage_max) { + log_data->battery_voltage_max = bat_port->bus->voltage; } - if (solar_bus->voltage > log_data->solar_voltage_max) { - log_data->solar_voltage_max = solar_bus->voltage; + if (solar_port->bus->voltage > log_data->solar_voltage_max) { + log_data->solar_voltage_max = solar_port->bus->voltage; } - if (dcdc->lv_bus->current > log_data->dcdc_current_max) { - log_data->dcdc_current_max = dcdc->lv_bus->current; + if (dcdc->lvs->current > log_data->dcdc_current_max) { + log_data->dcdc_current_max = dcdc->lvs->current; } - if (load->terminal->current > log_data->load_current_max) { - log_data->load_current_max = load->terminal->current; + if (load->port->current > log_data->load_current_max) { + log_data->load_current_max = load->port->current; } - if (-solar_bus->power > log_data->solar_power_max_day) { - log_data->solar_power_max_day = -solar_bus->power; + if (-solar_port->power > log_data->solar_power_max_day) { + log_data->solar_power_max_day = -solar_port->power; if (log_data->solar_power_max_day > log_data->solar_power_max_total) { log_data->solar_power_max_total = log_data->solar_power_max_day; } } - if (load->terminal->power > log_data->load_power_max_day) { - log_data->load_power_max_day = load->terminal->power; + if (load->port->power > log_data->load_power_max_day) { + log_data->load_power_max_day = load->port->power; if (log_data->load_power_max_day > log_data->load_power_max_total) { log_data->load_power_max_total = log_data->load_power_max_day; } diff --git a/src/log.h b/src/log.h index 9d6141e..8898b65 100644 --- a/src/log.h +++ b/src/log.h @@ -24,7 +24,7 @@ */ #include "bat_charger.h" -#include "dc_bus.h" +#include "power_port.h" #include "load.h" #include "dcdc.h" #include @@ -66,10 +66,11 @@ typedef struct { /** Updates the total energy counters for solar, battery and load bus */ -void log_update_energy(LogData *log_data, DcBus *solar, DcBus *bat, DcBus *load); +void log_update_energy(LogData *log_data, PowerPort *solar, PowerPort *bat, PowerPort *load); /** Updates the logged min/max values for voltages, power, temperatures etc. */ -void log_update_min_max_values(LogData *log_data, Dcdc *dcdc, Charger *charger, LoadOutput *load, DcBus *solar_bus, DcBus *bat_bus, DcBus *load_bus); +void log_update_min_max_values(LogData *log_data, Dcdc *dcdc, Charger *charger, LoadOutput *load, + PowerPort *solar_bus, PowerPort *bat_bus, PowerPort *load_bus); #endif /* LOG_H */ diff --git a/src/main.cpp b/src/main.cpp index bf670f4..db521e2 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -44,19 +44,31 @@ Serial serial(PIN_SWD_TX, PIN_SWD_RX, "serial", 115200); -Dcdc dcdc = {}; -DcBus hv_terminal = {}; // high voltage terminal (solar for typical MPPT) -DcBus lv_bus_int = {}; // internal low voltage side of DC/DC converter -DcBus lv_terminal = {}; // low voltage terminal (battery for typical MPPT) -DcBus load_terminal = {}; // load terminal -DcBus *bat_terminal = NULL; // pointer to terminal where battery is connected -DcBus *solar_terminal = NULL; // pointer to above terminal where solar panel is connected +DcBus hv_bus; +PowerPort hv_terminal(&hv_bus); // high voltage terminal (solar for typical MPPT) +PowerPort dcdc_port_hv(&hv_bus); // internal high voltage side port of DC/DC converter + +DcBus lv_bus; +PowerPort dcdc_port_lv(&lv_bus); // internal low voltage side of DC/DC converter +PowerPort lv_terminal(&lv_bus); // low voltage terminal (battery for typical MPPT) +PowerPort load_terminal(&lv_bus); // load terminal (also connected to lv_bus) + +PowerPort *bat_terminal = NULL; +PowerPort *solar_terminal = NULL; +PowerPort *grid_terminal = NULL; + +Dcdc dcdc(&dcdc_port_hv, &dcdc_port_lv, DCDC_MODE_INIT); + PwmSwitch pwm_switch = {}; // only necessary for PWM charger + +Charger charger(&lv_terminal); +LoadOutput load(&load_terminal); + BatConf bat_conf; // actual (used) battery configuration BatConf bat_conf_user; // temporary storage where the user can write to -Charger charger; // charger state information -LoadOutput load; + LogData log_data; + extern ThingSet ts; // defined in data_objects.cpp time_t timestamp; // current unix timestamp (independent of time(NULL), as it is user-configurable) @@ -78,11 +90,11 @@ void system_control() #else // control PWM of the DC/DC according to hs and ls port settings // (this function includes MPPT algorithm) - dcdc_control(&dcdc); + dcdc.control(); leds_set_charging(half_bridge_enabled()); #endif - load_control(&load, bat_conf.voltage_absolute_max * charger.num_batteries); + load.control(bat_conf.voltage_absolute_max * charger.num_batteries); if (counter % CONTROL_FREQUENCY == 0) { // called once per second (this timer is much more accurate than time(NULL) based on LSI) @@ -90,12 +102,12 @@ void system_control() timestamp++; counter = 0; // energy + soc calculation must be called exactly once per second - dc_bus_energy_balance(&hv_terminal); - dc_bus_energy_balance(&lv_terminal); - dc_bus_energy_balance(&load_terminal); + hv_terminal.energy_balance(); + lv_terminal.energy_balance(); + load_terminal.energy_balance(); log_update_energy(&log_data, &hv_terminal, &lv_terminal, &load_terminal); log_update_min_max_values(&log_data, &dcdc, &charger, &load, &hv_terminal, &lv_terminal, &load_terminal); - battery_update_soc(&bat_conf, &charger, bat_terminal); + charger.update_soc(&bat_conf); } counter++; } @@ -108,14 +120,10 @@ int main() battery_conf_init(&bat_conf, BATTERY_TYPE, BATTERY_NUM_CELLS, BATTERY_CAPACITY); battery_conf_overwrite(&bat_conf, &bat_conf_user); // initialize conf_user with same values - charger_init(&charger); - - load_init(&load, &lv_bus_int, &load_terminal); #ifdef CHARGER_TYPE_PWM pwm_switch_init(&pwm_switch); #else // MPPT - dcdc_init(&dcdc, &hv_terminal, &lv_bus_int, DCDC_MODE_INIT); half_bridge_init(PWM_FREQUENCY, PWM_DEADTIME, 12 / dcdc.hs_voltage_max, 0.97); // lower duty limit might have to be adjusted dynamically depending on LS voltage #endif @@ -142,28 +150,29 @@ int main() #ifdef CHARGER_TYPE_PWM bat_terminal = &lv_terminal; solar_terminal = &hv_terminal; - dc_bus_init_solar(solar_terminal, PWM_CURRENT_MAX); + solar_terminal->init_solar(PWM_CURRENT_MAX); #else // Setup of DC/DC power stage switch(dcdc.mode) { case MODE_NANOGRID: bat_terminal = &lv_terminal; - dc_bus_init_nanogrid(&hv_terminal); + hv_terminal.init_nanogrid(); break; case MODE_MPPT_BUCK: // typical MPPT charge controller operation bat_terminal = &lv_terminal; solar_terminal = &hv_terminal; - dc_bus_init_solar(solar_terminal, DCDC_CURRENT_MAX); + solar_terminal->init_solar(DCDC_CURRENT_MAX); break; case MODE_MPPT_BOOST: // for charging of e-bike battery via solar panel bat_terminal = &hv_terminal; solar_terminal = &lv_terminal; - dc_bus_init_solar(solar_terminal, DCDC_CURRENT_MAX); + solar_terminal->init_solar(DCDC_CURRENT_MAX); break; } #endif - charger_detect_num_batteries(&charger, &bat_conf, bat_terminal); // check if we have 24V instead of 12V system - battery_init_dc_bus(bat_terminal, &bat_conf, charger.num_batteries); + + charger.detect_num_batteries(&bat_conf); // check if we have 24V instead of 12V system + battery_init_dc_bus(&lv_bus, bat_terminal, &bat_conf, charger.num_batteries); wait(2); // safety feature: be able to re-flash before starting control_timer_start(CONTROL_FREQUENCY); @@ -189,12 +198,12 @@ int main() //printf("Still alive... time: %d, mode: %d\n", (int)time(NULL), dcdc.mode); - battery_discharge_control(bat_terminal, &bat_conf, &charger); - battery_charge_control(bat_terminal, &bat_conf, &charger); + charger.discharge_control(&bat_conf); + charger.charge_control(&bat_conf); - charger_update_junction_bus(&lv_bus_int, bat_terminal, &load_terminal); + update_dcdc_current_targets(&dcdc_port_lv, bat_terminal, &load_terminal); - load_state_machine(&load); + load.state_machine(); eeprom_update(); diff --git a/src/main.h b/src/main.h index 29e77c4..ba73544 100644 --- a/src/main.h +++ b/src/main.h @@ -19,20 +19,30 @@ #include #include "bat_charger.h" -#include "dc_bus.h" +#include "power_port.h" #include "log.h" #include "pwm_switch.h" #include "half_bridge.h" #include "load.h" +extern DcBus hv_bus; +extern PowerPort dcdc_port_hv; +extern PowerPort hv_terminal; + +extern DcBus lv_bus; +extern PowerPort dcdc_port_lv; +extern PowerPort lv_terminal; +extern PowerPort load_terminal; + extern LogData log_data; extern Charger charger; extern BatConf bat_conf; extern BatConf bat_conf_user; + +#ifndef CHARGER_TYPE_PWM extern Dcdc dcdc; +#endif + extern LoadOutput load; -extern DcBus hv_terminal; -extern DcBus lv_bus_int; -extern DcBus lv_terminal; -extern DcBus load_terminal; + extern PwmSwitch pwm_switch; diff --git a/src/dc_bus.cpp b/src/power_port.cpp similarity index 63% rename from src/dc_bus.cpp rename to src/power_port.cpp index 9df61d5..f6f1858 100644 --- a/src/dc_bus.cpp +++ b/src/power_port.cpp @@ -14,39 +14,43 @@ * limitations under the License. */ -#include "dc_bus.h" +#include "power_port.h" -void dc_bus_init_solar(DcBus *bus, float max_abs_current) +void PowerPort::init_solar(float max_abs_current) { + dis_current_limit = -max_abs_current; + chg_current_limit = 0; // no current towards solar panel allowed + + // also initialize the connected bus bus->dis_voltage_start = 16.0; bus->dis_voltage_stop = 14.0; - bus->dis_current_limit = -max_abs_current; - - bus->chg_current_limit = 0; // no current towards solar panel allowed } -void dc_bus_init_nanogrid(DcBus *bus) +void PowerPort::init_nanogrid() { + dis_current_limit = -5.0; + dis_droop_res = 0.1; // 1 Ohm means 1V change of target voltage per amp + + chg_current_limit = 5.0; + chg_droop_res = 0.1; // 1 Ohm means 1V change of target voltage per amp + + // also initialize the connected bus bus->dis_voltage_start = 30.0; // starting buck mode above this point bus->dis_voltage_stop = 20.0; // stopping buck mode below this point - bus->dis_current_limit = -5.0; - bus->dis_droop_res = 0.1; // 1 Ohm means 1V change of target voltage per amp bus->chg_voltage_target = 28.0; // starting idle mode above this point - bus->chg_current_limit = 5.0; bus->chg_voltage_min = 10.0; - bus->chg_droop_res = 0.1; // 1 Ohm means 1V change of target voltage per amp } // must be called exactly once per second, otherwise energy calculation gets wrong -void dc_bus_energy_balance(DcBus *bus) +void PowerPort::energy_balance() { // remark: timespan = 1s, so no multiplication with time necessary for energy calculation - if (bus->current >= 0) { - bus->chg_energy_Wh += bus->voltage * bus->current / 3600.0; + if (current >= 0) { + chg_energy_Wh += bus->voltage * current / 3600.0; } else { - bus->dis_energy_Wh -= bus->voltage * bus->current / 3600.0; + dis_energy_Wh -= bus->voltage * current / 3600.0; } -} \ No newline at end of file +} diff --git a/src/power_port.h b/src/power_port.h new file mode 100644 index 0000000..ce89916 --- /dev/null +++ b/src/power_port.h @@ -0,0 +1,96 @@ +/* LibreSolar charge controller firmware + * Copyright (c) 2016-2019 Martin Jäger (www.libre.solar) + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef POWER_PORT_H +#define POWER_PORT_H + +/** @file + * + * @brief Definition of charge controller terminals and internal DC buses + */ + +#include + +/** DC bus type + * + * Stores the voltage measurement and voltage limits for a DC bus inside the charge controller PCB + * + * A DC bus is considered a connection of two or more electrical circuits in one point, such that + * they have the same electric potential. + */ +class DcBus +{ +public: + float voltage; ///< Measured bus voltage + + float chg_voltage_target; ///< Target voltage if bus is charged + float chg_voltage_min; ///< Minimum voltage to allow charge current (necessary + ///< to prevent charging of deep-discharged Li-ion batteries) + + float dis_voltage_start; ///< Minimum voltage to allow discharge current from the bus + ///< (equal to load reconnect voltage) + float dis_voltage_stop; ///< Absolute minimum = load disconnect for batteries +}; + + +/** Power Port type + * + * Stores the current and power measurements and limits for one connection to a DC bus. One power + * port is assigned to exactly ony DC bus, but one DC bus can have multiple ports. + */ +class PowerPort +{ +public: + PowerPort(DcBus *dc_bus): + bus(dc_bus) {}; + + DcBus *bus; ///< Pointer to the DC bus this port is connected to (also + ///< storing its voltage measurement) + + float current; ///< Measured current through this port (positive sign = + ///< increasing voltage/charge of the named device) + float power; ///< Product of current & voltage + + float chg_current_limit; ///< Maximum current charging the bus, i.e. increasing its + ///< voltage (charging direction for battery terminal) + float chg_droop_res; ///< control voltage = nominal voltage - droop_res * current + + float dis_current_limit; ///< Maximum current to discharge the bus, i.e. decrease its + ///< voltage, value must be negative!!! + float dis_droop_res; ///< control voltage = nominal voltage - droop_res * current + + float chg_energy_Wh; ///< cumulated energy in charge direction since last counter reset (Wh) + float dis_energy_Wh; ///< cumulated energy in discharge direction since last counter reset (Wh) + + /** Initialize power port for solar panel connection + * + * @param max_abs_current Maximum input current allowed by PCB (as a positive value) + */ + void init_solar(float max_abs_current); + + /** Initialize power port for nanogrid connection + */ + void init_nanogrid(); + + /** Energy balance calculation for power port + * + * Must be called exactly once per second, otherwise energy calculation gets wrong. + */ + void energy_balance(); +}; + + +#endif /* POWER_PORT_H */ diff --git a/src/pwm_switch.cpp b/src/pwm_switch.cpp index b9288e0..347db8a 100644 --- a/src/pwm_switch.cpp +++ b/src/pwm_switch.cpp @@ -127,20 +127,20 @@ void pwm_switch_init(PwmSwitch *pwm_switch) _enabled = false; // still disable actual switch } -void pwm_switch_control(PwmSwitch *pwm_switch, DcBus *solar_bus, DcBus *bat_bus) +void pwm_switch_control(PwmSwitch *pwm_switch, PowerPort *solar, PowerPort *bat) { if (_enabled) { - if (bat_bus->chg_current_limit == 0 || solar_bus->dis_current_limit == 0 - || solar_bus->current > 0 // discharging battery into solar panel --> stop + if (bat->chg_current_limit == 0 || solar->dis_current_limit == 0 + || solar->current > 0 // discharging battery into solar panel --> stop || pwm_switch->enabled == false) { pwm_switch_stop(); pwm_switch->off_timestamp = time(NULL); printf("PWM charger stop.\n"); } - else if (bat_bus->voltage > (bat_bus->chg_voltage_target - bat_bus->chg_droop_res * bat_bus->current) // output voltage above target - || bat_bus->current > bat_bus->chg_current_limit // output current limit exceeded - || solar_bus->current < solar_bus->dis_current_limit) // input current (negative signs) above limit + else if (bat->bus->voltage > (bat->bus->chg_voltage_target - bat->chg_droop_res * bat->current) // output voltage above target + || bat->current > bat->chg_current_limit // output current limit exceeded + || solar->current < solar->dis_current_limit) // input current (negative signs) above limit { // The gate driver switch-off time is quite high (fall time around 1ms), so very short // on or off periods (duty cycle close to 0 and 1) should be avoided @@ -169,11 +169,11 @@ void pwm_switch_control(PwmSwitch *pwm_switch, DcBus *solar_bus, DcBus *bat_bus) } } else { - if (bat_bus->chg_current_limit > 0 // charging allowed - && bat_bus->voltage < bat_bus->chg_voltage_target - && bat_bus->voltage > bat_bus->chg_voltage_min - && solar_bus->dis_current_limit < 0 // dischraging allowed - && solar_bus->voltage > bat_bus->voltage + pwm_switch->offset_voltage_start + if (bat->chg_current_limit > 0 // charging allowed + && bat->bus->voltage < bat->bus->chg_voltage_target + && bat->bus->voltage > bat->bus->chg_voltage_min + && solar->dis_current_limit < 0 // dischraging allowed + && solar->bus->voltage > bat->bus->voltage + pwm_switch->offset_voltage_start && time(NULL) > (pwm_switch->off_timestamp + pwm_switch->restart_interval) && pwm_switch->enabled == true) { diff --git a/src/pwm_switch.h b/src/pwm_switch.h index 95f26ec..d8f4d8a 100644 --- a/src/pwm_switch.h +++ b/src/pwm_switch.h @@ -23,7 +23,8 @@ */ #include -#include "dc_bus.h" + +#include "power_port.h" /** PWM charger type * @@ -46,7 +47,7 @@ void pwm_switch_init(PwmSwitch *pwm_switch); * @param solar_bus Solar connector DC bus struct * @param bat_bus Battery connector DC bus struct */ -void pwm_switch_control(PwmSwitch *pwm_switch, DcBus *solar_bus, DcBus *bat_bus); +void pwm_switch_control(PwmSwitch *pwm_switch, PowerPort *solar_bus, PowerPort *bat_bus); /** Read the on/off status of the PWM switch * diff --git a/src/uext/uext_oled.cpp b/src/uext/uext_oled.cpp index 4159e42..a6166cf 100644 --- a/src/uext/uext_oled.cpp +++ b/src/uext/uext_oled.cpp @@ -17,8 +17,8 @@ #ifndef UNIT_TEST #include "main.h" -extern DcBus *bat_terminal; -extern DcBus *solar_terminal; +extern PowerPort *bat_terminal; +extern PowerPort *solar_terminal; #ifdef OLED_ENABLED // otherwise don't compile code to reduce firmware size @@ -61,8 +61,6 @@ void uext_process_asap() {;} void uext_process_1s() { - float tmp; - oled.clearDisplay(); oled.drawBitmap(6, 0, bmp_pv_panel, 16, 16, 1); @@ -96,27 +94,26 @@ void uext_process_1s() #else if (half_bridge_enabled()) { #endif - tmp = -solar_terminal->voltage * solar_terminal->current; oled.setTextCursor(0, 18); - oled.printf("%4.0fW", (abs(tmp) < 1) ? 0 : tmp); // remove negative zeros + oled.printf("%4.0fW", (abs(solar_terminal->power) < 1) ? 0 : -solar_terminal->power); // remove negative zeros } else { oled.setTextCursor(8, 18); oled.printf("n/a"); } #ifndef CHARGER_TYPE_PWM - if (solar_terminal->voltage > bat_terminal->voltage) + if (solar_terminal->bus->voltage > bat_terminal->bus->voltage) #endif { oled.setTextCursor(0, 26); - oled.printf("%4.1fV", solar_terminal->voltage); + oled.printf("%4.1fV", solar_terminal->bus->voltage); } // battery data oled.setTextCursor(42, 18); oled.printf("%5.1fW", (abs(bat_terminal->power) < 0.1) ? 0 : bat_terminal->power); // remove negative zeros oled.setTextCursor(42, 26); - oled.printf("%5.1fV", bat_terminal->voltage); + oled.printf("%5.1fV", bat_terminal->bus->voltage); // load data oled.setTextCursor(90, 18); diff --git a/test/main.cpp b/test/main.cpp index 9aab325..6996d2b 100644 --- a/test/main.cpp +++ b/test/main.cpp @@ -17,19 +17,30 @@ #include "tests.h" -Dcdc dcdc = {}; -DcBus hv_terminal = {}; // high voltage terminal (solar for typical MPPT) -DcBus lv_bus_int = {}; // internal low voltage side of DC/DC converter -DcBus lv_terminal = {}; // low voltage terminal (battery for typical MPPT) -DcBus load_terminal = {}; // load terminal -DcBus *bat_terminal = NULL; // pointer to terminal where battery is connected -DcBus *solar_terminal = NULL; // pointer to above terminal where solar panel is connected +DcBus hv_bus; +PowerPort hv_terminal(&hv_bus); // high voltage terminal (solar for typical MPPT) +PowerPort dcdc_port_hv(&hv_bus); // internal high voltage side port of DC/DC converter + +DcBus lv_bus; +PowerPort dcdc_port_lv(&lv_bus); // internal low voltage side of DC/DC converter +PowerPort lv_terminal(&lv_bus); // low voltage terminal (battery for typical MPPT) +PowerPort load_terminal(&lv_bus); // load terminal (also connected to lv_bus) + +PowerPort *bat_terminal = NULL; // pointer to terminal where battery is connected +PowerPort *solar_terminal = NULL; // pointer to above terminal where solar panel is connected + +Dcdc dcdc(&dcdc_port_hv, &dcdc_port_lv, DCDC_MODE_INIT); + PwmSwitch pwm_switch = {}; // only necessary for PWM charger + +Charger charger(&lv_terminal); +LoadOutput load(&load_terminal); + BatConf bat_conf; // actual (used) battery configuration BatConf bat_conf_user; // temporary storage where the user can write to -Charger charger; // charger state information -LoadOutput load; + LogData log_data; + extern ThingSet ts; // defined in data_objects.cpp time_t timestamp; // current unix timestamp (independent of time(NULL), as it is user-configurable) @@ -38,8 +49,6 @@ int main() { bat_terminal = &lv_terminal; solar_terminal = &hv_terminal; - dcdc_init(&dcdc, &hv_terminal, &lv_bus_int, MODE_MPPT_BUCK); - load_init(&load, &lv_bus_int, &load_terminal); adc_tests(); bat_charger_tests(); diff --git a/test/tests_adc.cpp b/test/tests_adc.cpp index 3d054f5..0d7dc84 100644 --- a/test/tests_adc.cpp +++ b/test/tests_adc.cpp @@ -3,38 +3,34 @@ #include "adc_dma.h" #include "adc_dma_stub.h" -extern Dcdc dcdc; -extern DcBus hv_terminal; -extern DcBus lv_terminal; -extern DcBus lv_bus_int; -extern DcBus load_terminal; -extern Charger charger; -extern LoadOutput load; +#include "main.h" static adc_values_t adcval; void check_solar_terminal_readings() { - TEST_ASSERT_EQUAL_FLOAT(adcval.solar_voltage, round(hv_terminal.voltage * 10) / 10); - TEST_ASSERT_EQUAL_FLOAT(adcval.dcdc_current / adcval.solar_voltage * adcval.battery_voltage, -round(hv_terminal.current * 10) / 10); + TEST_ASSERT_EQUAL_FLOAT(adcval.solar_voltage, round(hv_terminal.bus->voltage * 10) / 10); + TEST_ASSERT_EQUAL_FLOAT(adcval.dcdc_current / adcval.solar_voltage * adcval.battery_voltage, + -round(hv_terminal.current * 10) / 10); } void check_bat_terminal_readings() { - TEST_ASSERT_EQUAL_FLOAT(adcval.battery_voltage, round(lv_terminal.voltage * 10) / 10); - TEST_ASSERT_EQUAL_FLOAT(adcval.dcdc_current - adcval.load_current, round(lv_terminal.current * 10) / 10); + TEST_ASSERT_EQUAL_FLOAT(adcval.battery_voltage, round(lv_terminal.bus->voltage * 10) / 10); + TEST_ASSERT_EQUAL_FLOAT(adcval.dcdc_current - adcval.load_current, + round(lv_terminal.current * 10) / 10); } void check_load_terminal_readings() { - TEST_ASSERT_EQUAL_FLOAT(adcval.battery_voltage, round(load_terminal.voltage * 10) / 10); + TEST_ASSERT_EQUAL_FLOAT(adcval.battery_voltage, round(load_terminal.bus->voltage * 10) / 10); TEST_ASSERT_EQUAL_FLOAT(adcval.load_current, round(load_terminal.current * 10) / 10); } void check_lv_bus_int_readings() { - TEST_ASSERT_EQUAL_FLOAT(adcval.battery_voltage, round(lv_bus_int.voltage * 10) / 10); - TEST_ASSERT_EQUAL_FLOAT(adcval.dcdc_current, round(lv_bus_int.current * 10) / 10); + TEST_ASSERT_EQUAL_FLOAT(adcval.battery_voltage, round(dcdc_port_lv.bus->voltage * 10) / 10); + TEST_ASSERT_EQUAL_FLOAT(adcval.dcdc_current, round(dcdc_port_lv.current * 10) / 10); } void check_temperature_readings() diff --git a/test/tests_bat_charger.cpp b/test/tests_bat_charger.cpp index 9a57620..4e7b321 100644 --- a/test/tests_bat_charger.cpp +++ b/test/tests_bat_charger.cpp @@ -1,32 +1,29 @@ #include "tests.h" -#include "bat_charger.h" -#include "dc_bus.h" - #include #include -extern BatConf bat_conf; -extern Charger charger; -extern DcBus *bat_terminal; +#include "main.h" + +extern PowerPort *bat_terminal; static void init_structs() { battery_conf_init(&bat_conf, BAT_TYPE_FLOODED, 6, 100); - charger_init(&charger); - battery_init_dc_bus(bat_terminal, &bat_conf, 1); + //charger_init(&charger); + battery_init_dc_bus(&lv_bus, bat_terminal, &bat_conf, 1); charger.state = CHG_STATE_IDLE; charger.bat_temperature = 25; - bat_terminal->voltage = 14.0; + bat_terminal->bus->voltage = 14.0; bat_terminal->current = 0; } void no_start_at_high_voltage() { init_structs(); - bat_terminal->voltage = bat_conf.voltage_recharge + 0.1; - battery_charge_control(bat_terminal, &bat_conf, &charger); + bat_terminal->bus->voltage = bat_conf.voltage_recharge + 0.1; + charger.charge_control(&bat_conf); TEST_ASSERT_EQUAL(CHG_STATE_IDLE, charger.state); } @@ -34,8 +31,8 @@ void no_start_after_short_rest() { init_structs(); charger.time_state_changed = time(NULL) - bat_conf.time_limit_recharge + 1; - bat_terminal->voltage = bat_conf.voltage_recharge - 0.1; - battery_charge_control(bat_terminal, &bat_conf, &charger); + bat_terminal->bus->voltage = bat_conf.voltage_recharge - 0.1; + charger.charge_control(&bat_conf); TEST_ASSERT_EQUAL(CHG_STATE_IDLE, charger.state); } @@ -43,12 +40,12 @@ void no_start_outside_temperature_limits() { init_structs(); charger.bat_temperature = bat_conf.charge_temp_max + 1; - bat_terminal->voltage = bat_conf.voltage_recharge - 0.1; - battery_charge_control(bat_terminal, &bat_conf, &charger); + bat_terminal->bus->voltage = bat_conf.voltage_recharge - 0.1; + charger.charge_control(&bat_conf); TEST_ASSERT_EQUAL(CHG_STATE_IDLE, charger.state); charger.bat_temperature = bat_conf.charge_temp_min - 1; - battery_charge_control(bat_terminal, &bat_conf, &charger); + charger.charge_control(&bat_conf); TEST_ASSERT_EQUAL(CHG_STATE_IDLE, charger.state); } @@ -56,8 +53,8 @@ void start_if_everything_just_fine() { init_structs(); charger.time_state_changed = time(NULL) - bat_conf.time_limit_recharge - 1; - bat_terminal->voltage = bat_conf.voltage_recharge - 0.1; - battery_charge_control(bat_terminal, &bat_conf, &charger); + bat_terminal->bus->voltage = bat_conf.voltage_recharge - 0.1; + charger.charge_control(&bat_conf); TEST_ASSERT_EQUAL(CHG_STATE_BULK, charger.state); } @@ -65,11 +62,11 @@ void enter_topping_at_voltage_setpoint() { init_structs(); charger.time_state_changed = time(NULL) - bat_conf.time_limit_recharge - 1; - bat_terminal->voltage = bat_conf.voltage_recharge - 0.1; - battery_charge_control(bat_terminal, &bat_conf, &charger); + bat_terminal->bus->voltage = bat_conf.voltage_recharge - 0.1; + charger.charge_control(&bat_conf); TEST_ASSERT_EQUAL(CHG_STATE_BULK, charger.state); - bat_terminal->voltage = bat_conf.topping_voltage + 0.1; - battery_charge_control(bat_terminal, &bat_conf, &charger); + bat_terminal->bus->voltage = bat_conf.topping_voltage + 0.1; + charger.charge_control(&bat_conf); TEST_ASSERT_EQUAL(CHG_STATE_TOPPING, charger.state); } @@ -78,14 +75,14 @@ void stop_topping_after_time_limit() enter_topping_at_voltage_setpoint(); charger.time_state_changed = time(NULL) - bat_conf.topping_duration + 1; - bat_terminal->voltage = bat_conf.topping_voltage + 0.1; + bat_terminal->bus->voltage = bat_conf.topping_voltage + 0.1; bat_terminal->current = bat_conf.topping_current_cutoff + 0.1; - battery_charge_control(bat_terminal, &bat_conf, &charger); + charger.charge_control(&bat_conf); TEST_ASSERT_EQUAL(CHG_STATE_TOPPING, charger.state); charger.time_state_changed = time(NULL) - bat_conf.topping_duration - 1; - battery_charge_control(bat_terminal, &bat_conf, &charger); + charger.charge_control(&bat_conf); TEST_ASSERT_EQUAL(CHG_STATE_TRICKLE, charger.state); } @@ -94,9 +91,9 @@ void stop_topping_at_cutoff_current() enter_topping_at_voltage_setpoint(); charger.time_state_changed = time(NULL) - 1; - bat_terminal->voltage = bat_conf.topping_voltage + 0.1; + bat_terminal->bus->voltage = bat_conf.topping_voltage + 0.1; bat_terminal->current = bat_conf.topping_current_cutoff - 0.1; - battery_charge_control(bat_terminal, &bat_conf, &charger); + charger.charge_control(&bat_conf); TEST_ASSERT_EQUAL(CHG_STATE_TRICKLE, charger.state); } @@ -106,9 +103,9 @@ void trickle_to_idle_for_li_ion() battery_conf_init(&bat_conf, BAT_TYPE_LFP, 4, 100); charger.time_state_changed = time(NULL) - 1; - bat_terminal->voltage = bat_conf.topping_voltage + 0.1; + bat_terminal->bus->voltage = bat_conf.topping_voltage + 0.1; bat_terminal->current = bat_conf.topping_current_cutoff - 0.1; - battery_charge_control(bat_terminal, &bat_conf, &charger); + charger.charge_control(&bat_conf); TEST_ASSERT_EQUAL(CHG_STATE_IDLE, charger.state); } @@ -118,14 +115,14 @@ void no_trickle_if_low_current_because_of_low_input() charger.time_state_changed = time(NULL) - 200; charger.time_voltage_limit_reached = time(NULL) - 3; - bat_terminal->voltage = bat_conf.topping_voltage - 0.1; + bat_terminal->bus->voltage = bat_conf.topping_voltage - 0.1; bat_terminal->current = bat_conf.topping_current_cutoff - 0.1; - battery_charge_control(bat_terminal, &bat_conf, &charger); + charger.charge_control(&bat_conf); TEST_ASSERT_EQUAL(CHG_STATE_TOPPING, charger.state); charger.time_voltage_limit_reached = time(NULL) - 1; - battery_charge_control(bat_terminal, &bat_conf, &charger); + charger.charge_control(&bat_conf); TEST_ASSERT_EQUAL(CHG_STATE_TRICKLE, charger.state); } @@ -141,9 +138,9 @@ void no_equalization_if_disabled() time(NULL) - bat_conf.equalization_trigger_days * 24*60*60; charger.time_state_changed = time(NULL) - 1; - bat_terminal->voltage = bat_conf.topping_voltage + 0.1; + bat_terminal->bus->voltage = bat_conf.topping_voltage + 0.1; bat_terminal->current = bat_conf.topping_current_cutoff - 0.1; - battery_charge_control(bat_terminal, &bat_conf, &charger); + charger.charge_control(&bat_conf); TEST_ASSERT_NOT_EQUAL(CHG_STATE_EQUALIZATION, charger.state); } @@ -159,9 +156,9 @@ void no_equalization_if_limits_not_reached() time(NULL) - (bat_conf.equalization_trigger_days - 1) * 24*60*60; charger.time_state_changed = time(NULL) - 1; - bat_terminal->voltage = bat_conf.topping_voltage + 0.1; + bat_terminal->bus->voltage = bat_conf.topping_voltage + 0.1; bat_terminal->current = bat_conf.topping_current_cutoff - 0.1; - battery_charge_control(bat_terminal, &bat_conf, &charger); + charger.charge_control(&bat_conf); TEST_ASSERT_NOT_EQUAL(CHG_STATE_EQUALIZATION, charger.state); } @@ -173,9 +170,9 @@ void trickle_to_equalization_if_enabled_and_time_limit_reached() time(NULL) - bat_conf.equalization_trigger_days * 24*60*60; charger.time_state_changed = time(NULL) - 1; - bat_terminal->voltage = bat_conf.topping_voltage + 0.1; + bat_terminal->bus->voltage = bat_conf.topping_voltage + 0.1; bat_terminal->current = bat_conf.topping_current_cutoff - 0.1; - battery_charge_control(bat_terminal, &bat_conf, &charger); + charger.charge_control(&bat_conf); TEST_ASSERT_EQUAL(CHG_STATE_EQUALIZATION, charger.state); } @@ -188,9 +185,9 @@ void trickle_to_equalization_if_enabled_and_deep_dis_limit_reached() charger.num_deep_discharges - bat_conf.equalization_trigger_deep_cycles; charger.time_state_changed = time(NULL) - 1; - bat_terminal->voltage = bat_conf.topping_voltage + 0.1; + bat_terminal->bus->voltage = bat_conf.topping_voltage + 0.1; bat_terminal->current = bat_conf.topping_current_cutoff - 0.1; - battery_charge_control(bat_terminal, &bat_conf, &charger); + charger.charge_control(&bat_conf); TEST_ASSERT_EQUAL(CHG_STATE_EQUALIZATION, charger.state); } @@ -200,13 +197,13 @@ void stop_equalization_after_time_limit() trickle_to_equalization_if_enabled_and_time_limit_reached(); charger.time_state_changed = time(NULL) - bat_conf.equalization_duration + 1; - bat_terminal->voltage = bat_conf.equalization_voltage + 0.1; + bat_terminal->bus->voltage = bat_conf.equalization_voltage + 0.1; - battery_charge_control(bat_terminal, &bat_conf, &charger); + charger.charge_control(&bat_conf); TEST_ASSERT_EQUAL(CHG_STATE_EQUALIZATION, charger.state); charger.time_state_changed = time(NULL) - bat_conf.topping_duration - 1; - battery_charge_control(bat_terminal, &bat_conf, &charger); + charger.charge_control(&bat_conf); TEST_ASSERT_EQUAL(CHG_STATE_TRICKLE, charger.state); } @@ -219,12 +216,12 @@ void stop_discharge_at_low_voltage() { init_structs(); - bat_terminal->voltage = 14; - battery_discharge_control(bat_terminal, &bat_conf, &charger); + bat_terminal->bus->voltage = 14; + charger.discharge_control(&bat_conf); TEST_ASSERT_LESS_THAN(0, bat_terminal->dis_current_limit); - bat_terminal->voltage = bat_conf.voltage_load_disconnect - 0.1; - battery_discharge_control(bat_terminal, &bat_conf, &charger); + bat_terminal->bus->voltage = bat_conf.voltage_load_disconnect - 0.1; + charger.discharge_control(&bat_conf); TEST_ASSERT_EQUAL(0, bat_terminal->dis_current_limit); } @@ -234,7 +231,7 @@ void stop_discharge_at_overtemp() TEST_ASSERT_LESS_THAN(0, bat_terminal->dis_current_limit); charger.bat_temperature = bat_conf.discharge_temp_max + 1; - battery_discharge_control(bat_terminal, &bat_conf, &charger); + charger.discharge_control(&bat_conf); TEST_ASSERT_EQUAL(0, bat_terminal->dis_current_limit); } @@ -244,7 +241,7 @@ void stop_discharge_at_undertemp() TEST_ASSERT_LESS_THAN(0, bat_terminal->dis_current_limit); charger.bat_temperature = bat_conf.discharge_temp_min - 1; - battery_discharge_control(bat_terminal, &bat_conf, &charger); + charger.discharge_control(&bat_conf); TEST_ASSERT_EQUAL(0, bat_terminal->dis_current_limit); } @@ -253,18 +250,18 @@ void restart_discharge_if_allowed() init_structs(); // stop because of undervoltage - bat_terminal->voltage = bat_conf.voltage_load_disconnect - 0.1; - battery_discharge_control(bat_terminal, &bat_conf, &charger); + bat_terminal->bus->voltage = bat_conf.voltage_load_disconnect - 0.1; + charger.discharge_control(&bat_conf); TEST_ASSERT_EQUAL(0, bat_terminal->dis_current_limit); // increase voltage slightly above DISconnect voltage - bat_terminal->voltage = bat_conf.voltage_load_disconnect + 0.1; - battery_discharge_control(bat_terminal, &bat_conf, &charger); + bat_terminal->bus->voltage = bat_conf.voltage_load_disconnect + 0.1; + charger.discharge_control(&bat_conf); TEST_ASSERT_EQUAL(0, bat_terminal->dis_current_limit); // increase voltage above REconnect voltage - bat_terminal->voltage = bat_conf.voltage_load_reconnect + 0.1; - battery_discharge_control(bat_terminal, &bat_conf, &charger); + bat_terminal->bus->voltage = bat_conf.voltage_load_reconnect + 0.1; + charger.discharge_control(&bat_conf); TEST_ASSERT_LESS_THAN(0, bat_terminal->dis_current_limit); } diff --git a/test/tests_dc_bus.cpp b/test/tests_dc_bus.cpp index 8211833..0d64c4a 100644 --- a/test/tests_dc_bus.cpp +++ b/test/tests_dc_bus.cpp @@ -3,29 +3,16 @@ #include "adc_dma_stub.h" #include "adc_dma.h" -#include "bat_charger.h" -#include "dc_bus.h" -#include "log.h" - #include #include #include -extern Dcdc dcdc; -extern DcBus hv_terminal; -extern DcBus lv_terminal; -extern DcBus lv_bus_int; -extern DcBus load_terminal; -extern Charger charger; -extern BatConf bat_conf; -extern LoadOutput load; -extern LogData log_data; +#include "main.h" static void init_structs() { battery_conf_init(&bat_conf, BAT_TYPE_FLOODED, 6, 100); - charger_init(&charger); - battery_init_dc_bus(&lv_terminal, &bat_conf, 1); + battery_init_dc_bus(&lv_bus, &lv_terminal, &bat_conf, 1); charger.state = CHG_STATE_IDLE; } @@ -61,9 +48,9 @@ void energy_calculation_init() update_measurements(); for (int i = 0; i < 60*60*sun_hours; i++) { - dc_bus_energy_balance(&hv_terminal); - dc_bus_energy_balance(&lv_terminal); - dc_bus_energy_balance(&load_terminal); + hv_terminal.energy_balance(); + lv_terminal.energy_balance(); + load_terminal.energy_balance(); } // disable DC/DC = solar charging @@ -72,9 +59,9 @@ void energy_calculation_init() update_measurements(); for (int i = 0; i < 60*60*night_hours; i++) { - dc_bus_energy_balance(&hv_terminal); - dc_bus_energy_balance(&lv_terminal); - dc_bus_energy_balance(&load_terminal); + hv_terminal.energy_balance(); + lv_terminal.energy_balance(); + load_terminal.energy_balance(); } } @@ -82,26 +69,26 @@ void charging_energy_calculation_valid() { energy_calculation_init(); // charging only during sun hours - TEST_ASSERT_EQUAL_FLOAT(round(sun_hours * lv_terminal.voltage * (dcdc_current_sun - load_current)), round(lv_terminal.chg_energy_Wh)); + TEST_ASSERT_EQUAL_FLOAT(round(sun_hours * lv_terminal.bus->voltage * (dcdc_current_sun - load_current)), round(lv_terminal.chg_energy_Wh)); } void discharging_energy_calculation_valid() { energy_calculation_init(); // discharging (sum of current) only during dis hours - TEST_ASSERT_EQUAL_FLOAT(round(night_hours * lv_terminal.voltage * adcval.load_current), round(lv_terminal.dis_energy_Wh)); + TEST_ASSERT_EQUAL_FLOAT(round(night_hours * lv_terminal.bus->voltage * adcval.load_current), round(lv_terminal.dis_energy_Wh)); } void solar_input_energy_calculation_valid() { energy_calculation_init(); - TEST_ASSERT_EQUAL_FLOAT(round(sun_hours * lv_terminal.voltage * dcdc_current_sun), round(hv_terminal.dis_energy_Wh)); + TEST_ASSERT_EQUAL_FLOAT(round(sun_hours * lv_terminal.bus->voltage * dcdc_current_sun), round(hv_terminal.dis_energy_Wh)); } void load_output_energy_calculation_valid() { energy_calculation_init(); - TEST_ASSERT_EQUAL_FLOAT(round((sun_hours + night_hours) * lv_terminal.voltage * adcval.load_current), round(load_terminal.chg_energy_Wh)); + TEST_ASSERT_EQUAL_FLOAT(round((sun_hours + night_hours) * lv_terminal.bus->voltage * adcval.load_current), round(load_terminal.chg_energy_Wh)); } void dc_bus_tests() diff --git a/test/tests_dcdc.cpp b/test/tests_dcdc.cpp index 0c87d02..ea1fe90 100644 --- a/test/tests_dcdc.cpp +++ b/test/tests_dcdc.cpp @@ -1,46 +1,41 @@ #include "tests.h" -#include "dcdc.h" -#include "dc_bus.h" -#include "log.h" #include "half_bridge.h" #include #include #include -extern Dcdc dcdc; -extern DcBus hv_terminal; -extern DcBus lv_bus_int; -extern BatConf bat_conf; +#include "main.h" static void init_structs_buck() { - dc_bus_init_solar(&hv_terminal, 10); - hv_terminal.voltage = 20; - hv_terminal.dis_voltage_start = 18; - hv_terminal.current = 0; + dcdc_port_hv.init_solar(10); + dcdc_port_hv.bus->voltage = 20; + dcdc_port_hv.bus->dis_voltage_start = 18; + dcdc_port_hv.current = 0; battery_conf_init(&bat_conf, BAT_TYPE_GEL, 6, 100); - battery_init_dc_bus(&lv_bus_int, &bat_conf, 1); - lv_bus_int.voltage = 14; - lv_bus_int.current = 0; + battery_init_dc_bus(&lv_bus, &dcdc_port_lv, &bat_conf, 1); + dcdc_port_lv.bus->voltage = 14; + dcdc_port_lv.current = 0; + dcdc.mode = MODE_MPPT_BUCK; dcdc.temp_mosfets = 25; dcdc.off_timestamp = 0; dcdc.power_prev = 0; dcdc.pwm_delta = 1; - dcdc_init(&dcdc, &hv_terminal, &lv_bus_int, MODE_MPPT_BUCK); + dcdc.enabled = true; } static void start_buck() { init_structs_buck(); half_bridge_init(70, 200, 12 / dcdc.hs_voltage_max, 0.97); - dcdc_control(&dcdc); - dcdc_control(&dcdc); - dcdc_control(&dcdc); // call multiple times because of startup delay + dcdc.control(); + dcdc.control(); + dcdc.control(); // call multiple times because of startup delay TEST_ASSERT_EQUAL(DCDC_STATE_MPPT, dcdc.state); } @@ -48,68 +43,69 @@ static void init_structs_boost() { half_bridge_stop(); - dc_bus_init_solar(&lv_bus_int, 10); - lv_bus_int.voltage = 20; - lv_bus_int.dis_voltage_start = 18; - lv_bus_int.current = 0; - lv_bus_int.power = 0; + dcdc_port_hv.init_solar(10); + dcdc_port_lv.bus->voltage = 20; + dcdc_port_lv.bus->dis_voltage_start = 18; + dcdc_port_lv.current = 0; + dcdc_port_lv.power = 0; battery_conf_init(&bat_conf, BAT_TYPE_NMC, 10, 9); - battery_init_dc_bus(&hv_terminal, &bat_conf, 1); - hv_terminal.voltage = 3.7 * 10; - hv_terminal.current = 0; - hv_terminal.power = 0; + battery_init_dc_bus(&hv_bus, &dcdc_port_hv, &bat_conf, 1); + dcdc_port_hv.bus->voltage = 3.7 * 10; + dcdc_port_hv.current = 0; + dcdc_port_hv.power = 0; + dcdc.mode = MODE_MPPT_BOOST; dcdc.temp_mosfets = 25; dcdc.off_timestamp = 0; dcdc.power_prev = 0; - dcdc.pwm_delta = -1; - dcdc_init(&dcdc, &hv_terminal, &lv_bus_int, MODE_MPPT_BOOST); + dcdc.pwm_delta = 1; + dcdc.enabled = true; } static void start_boost() { init_structs_boost(); half_bridge_init(70, 200, 12 / dcdc.hs_voltage_max, 0.97); - dcdc_control(&dcdc); - dcdc_control(&dcdc); - dcdc_control(&dcdc); // call multiple times because of startup delay + dcdc.control(); + dcdc.control(); + dcdc.control(); // call multiple times because of startup delay TEST_ASSERT_EQUAL(DCDC_STATE_MPPT, dcdc.state); } void start_valid_mppt_buck() { init_structs_buck(); - TEST_ASSERT_EQUAL(1, dcdc_check_start_conditions(&dcdc)); + TEST_ASSERT_EQUAL(1, dcdc.check_start_conditions()); } void start_valid_mppt_boost() { init_structs_boost(); - TEST_ASSERT_EQUAL(-1, dcdc_check_start_conditions(&dcdc)); + TEST_ASSERT_EQUAL(-1, dcdc.check_start_conditions()); } void no_start_before_restart_delay() { init_structs_buck(); dcdc.off_timestamp = time(NULL) - dcdc.restart_interval + 1; - TEST_ASSERT_EQUAL(0, dcdc_check_start_conditions(&dcdc)); + TEST_ASSERT_EQUAL(0, dcdc.check_start_conditions()); dcdc.off_timestamp = time(NULL) - dcdc.restart_interval; - TEST_ASSERT_EQUAL(1, dcdc_check_start_conditions(&dcdc)); + TEST_ASSERT_EQUAL(1, dcdc.check_start_conditions()); } void no_start_if_dcdc_disabled() { init_structs_buck(); dcdc.enabled = false; - TEST_ASSERT_EQUAL(0, dcdc_check_start_conditions(&dcdc)); + TEST_ASSERT_EQUAL(0, dcdc.check_start_conditions()); } void no_start_if_dcdc_lv_voltage_low() { init_structs_buck(); - lv_bus_int.voltage = dcdc.ls_voltage_min - 0.5; - TEST_ASSERT_EQUAL(0, dcdc_check_start_conditions(&dcdc)); + dcdc_port_lv.bus->voltage = dcdc.ls_voltage_min - 0.5; + TEST_ASSERT_EQUAL(0, dcdc.check_start_conditions()); } // buck @@ -117,36 +113,36 @@ void no_start_if_dcdc_lv_voltage_low() void no_buck_start_if_bat_voltage_low() { init_structs_buck(); - lv_bus_int.voltage = bat_conf.voltage_absolute_min - 0.1; - TEST_ASSERT_EQUAL(0, dcdc_check_start_conditions(&dcdc)); + dcdc_port_lv.bus->voltage = bat_conf.voltage_absolute_min - 0.1; + TEST_ASSERT_EQUAL(0, dcdc.check_start_conditions()); } void no_buck_start_if_bat_voltage_high() { init_structs_buck(); - lv_bus_int.voltage = bat_conf.topping_voltage + 0.1; - TEST_ASSERT_EQUAL(0, dcdc_check_start_conditions(&dcdc)); + dcdc_port_lv.bus->voltage = bat_conf.topping_voltage + 0.1; + TEST_ASSERT_EQUAL(0, dcdc.check_start_conditions()); } void no_buck_start_if_bat_chg_not_allowed() { init_structs_buck(); - lv_bus_int.chg_current_limit = 0; - TEST_ASSERT_EQUAL(0, dcdc_check_start_conditions(&dcdc)); + dcdc_port_lv.chg_current_limit = 0; + TEST_ASSERT_EQUAL(0, dcdc.check_start_conditions()); } void no_buck_start_if_solar_voltage_high() { init_structs_buck(); - hv_terminal.voltage = dcdc.hs_voltage_max + 1; - TEST_ASSERT_EQUAL(0, dcdc_check_start_conditions(&dcdc)); + dcdc_port_hv.bus->voltage = dcdc.hs_voltage_max + 1; + TEST_ASSERT_EQUAL(0, dcdc.check_start_conditions()); } void no_buck_start_if_solar_voltage_low() { init_structs_buck(); - hv_terminal.voltage = 17; - TEST_ASSERT_EQUAL(0, dcdc_check_start_conditions(&dcdc)); + dcdc_port_hv.bus->voltage = 17; + TEST_ASSERT_EQUAL(0, dcdc.check_start_conditions()); } // boost @@ -154,36 +150,36 @@ void no_buck_start_if_solar_voltage_low() void no_boost_start_if_bat_voltage_low() { init_structs_boost(); - hv_terminal.voltage = bat_conf.voltage_absolute_min - 0.1; - TEST_ASSERT_EQUAL(0, dcdc_check_start_conditions(&dcdc)); + dcdc_port_hv.bus->voltage = bat_conf.voltage_absolute_min - 0.1; + TEST_ASSERT_EQUAL(0, dcdc.check_start_conditions()); } void no_boost_start_if_bat_voltage_high() { init_structs_boost(); - hv_terminal.voltage = bat_conf.topping_voltage + 0.1; - TEST_ASSERT_EQUAL(0, dcdc_check_start_conditions(&dcdc)); + dcdc_port_hv.bus->voltage = bat_conf.topping_voltage + 0.1; + TEST_ASSERT_EQUAL(0, dcdc.check_start_conditions()); } void no_boost_start_if_bat_chg_not_allowed() { init_structs_boost(); - hv_terminal.chg_current_limit = 0; - TEST_ASSERT_EQUAL(0, dcdc_check_start_conditions(&dcdc)); + dcdc_port_hv.chg_current_limit = 0; + TEST_ASSERT_EQUAL(0, dcdc.check_start_conditions()); } void no_boost_start_if_solar_voltage_high() { init_structs_boost(); - lv_bus_int.voltage = dcdc.ls_voltage_max + 1; - TEST_ASSERT_EQUAL(0, dcdc_check_start_conditions(&dcdc)); + dcdc_port_lv.bus->voltage = dcdc.ls_voltage_max + 1; + TEST_ASSERT_EQUAL(0, dcdc.check_start_conditions()); } void no_boost_start_if_solar_voltage_low() { init_structs_boost(); - lv_bus_int.voltage = 17; - TEST_ASSERT_EQUAL(0, dcdc_check_start_conditions(&dcdc)); + dcdc_port_lv.bus->voltage = 17; + TEST_ASSERT_EQUAL(0, dcdc.check_start_conditions()); } // buck operation @@ -192,7 +188,7 @@ void buck_increasing_power() { start_buck(); float pwm_before = half_bridge_get_duty_cycle(); - dcdc_control(&dcdc); + dcdc.control(); float pwm_after = half_bridge_get_duty_cycle(); TEST_ASSERT(pwm_after > pwm_before); } @@ -201,8 +197,8 @@ void buck_derating_output_voltage_too_high() { start_buck(); float pwm_before = half_bridge_get_duty_cycle(); - lv_bus_int.voltage = lv_bus_int.chg_voltage_target + 0.1; - dcdc_control(&dcdc); + dcdc_port_lv.bus->voltage = dcdc_port_lv.bus->chg_voltage_target + 0.1; + dcdc.control(); float pwm_after = half_bridge_get_duty_cycle(); TEST_ASSERT(pwm_after < pwm_before); // less duty cycle = higher voltage TEST_ASSERT_EQUAL(DCDC_STATE_CV, dcdc.state); @@ -212,8 +208,8 @@ void buck_derating_output_current_too_high() { start_buck(); float pwm_before = half_bridge_get_duty_cycle(); - lv_bus_int.current = lv_bus_int.chg_current_limit + 0.1; - dcdc_control(&dcdc); + dcdc_port_lv.current = dcdc_port_lv.chg_current_limit + 0.1; + dcdc.control(); float pwm_after = half_bridge_get_duty_cycle(); TEST_ASSERT(pwm_after < pwm_before); TEST_ASSERT_EQUAL(DCDC_STATE_CC, dcdc.state); @@ -223,23 +219,23 @@ void buck_derating_input_voltage_too_low() { start_buck(); float pwm_before = half_bridge_get_duty_cycle(); - hv_terminal.voltage = hv_terminal.dis_voltage_stop - 0.1; - lv_bus_int.current = 0.2; - dcdc_control(&dcdc); + dcdc_port_hv.bus->voltage = dcdc_port_hv.bus->dis_voltage_stop - 0.1; + dcdc_port_lv.current = 0.2; + dcdc.control(); float pwm_after = half_bridge_get_duty_cycle(); TEST_ASSERT(pwm_after < pwm_before); - TEST_ASSERT_EQUAL(DCDC_STATE_CV, dcdc.state); + TEST_ASSERT_EQUAL(DCDC_STATE_DERATING, dcdc.state); } void buck_derating_input_current_too_high() { start_buck(); float pwm_before = half_bridge_get_duty_cycle(); - hv_terminal.current = hv_terminal.dis_current_limit - 0.1; - dcdc_control(&dcdc); + dcdc_port_hv.current = dcdc_port_hv.dis_current_limit - 0.1; + dcdc.control(); float pwm_after = half_bridge_get_duty_cycle(); TEST_ASSERT(pwm_after < pwm_before); - TEST_ASSERT_EQUAL(DCDC_STATE_CC, dcdc.state); + TEST_ASSERT_EQUAL(DCDC_STATE_DERATING, dcdc.state); } void buck_derating_temperature_limits_exceeded() @@ -247,7 +243,7 @@ void buck_derating_temperature_limits_exceeded() start_buck(); float pwm_before = half_bridge_get_duty_cycle(); dcdc.temp_mosfets = 81; - dcdc_control(&dcdc); + dcdc.control(); float pwm_after = half_bridge_get_duty_cycle(); TEST_ASSERT(pwm_after < pwm_before); } @@ -257,7 +253,7 @@ void buck_stop_input_power_too_low() start_buck(); float pwm_before = half_bridge_get_duty_cycle(); dcdc.power_good_timestamp = time(NULL) - 11; - dcdc_control(&dcdc); + dcdc.control(); TEST_ASSERT(half_bridge_enabled() == false); } @@ -265,8 +261,8 @@ void buck_stop_high_voltage_emergency() { start_buck(); float pwm_before = half_bridge_get_duty_cycle(); - lv_bus_int.voltage = dcdc.ls_voltage_max + 0.1; - dcdc_control(&dcdc); + dcdc_port_lv.bus->voltage = dcdc.ls_voltage_max + 0.1; + dcdc.control(); TEST_ASSERT(half_bridge_enabled() == false); } @@ -274,16 +270,16 @@ void buck_correct_mppt_operation() { start_buck(); - lv_bus_int.power = 5; - dcdc_control(&dcdc); + dcdc_port_lv.power = 5; + dcdc.control(); float pwm1 = half_bridge_get_duty_cycle(); - lv_bus_int.power = 7; - dcdc_control(&dcdc); + dcdc_port_lv.power = 7; + dcdc.control(); float pwm2 = half_bridge_get_duty_cycle(); TEST_ASSERT(pwm2 > pwm1); - lv_bus_int.power = 6; // decrease power to make the direction turn around - dcdc_control(&dcdc); + dcdc_port_lv.power = 6; // decrease power to make the direction turn around + dcdc.control(); float pwm3 = half_bridge_get_duty_cycle(); TEST_ASSERT(pwm3 < pwm2); } @@ -295,7 +291,7 @@ void boost_increasing_power() // make sure that without changing anything, dcdc_control goes in direction of increasing power start_boost(); float pwm_before = half_bridge_get_duty_cycle(); - dcdc_control(&dcdc); + dcdc.control(); float pwm_after = half_bridge_get_duty_cycle(); TEST_ASSERT(pwm_after < pwm_before); } @@ -304,8 +300,8 @@ void boost_derating_output_voltage_too_high() { start_boost(); float pwm_before = half_bridge_get_duty_cycle(); - hv_terminal.voltage = hv_terminal.chg_voltage_target + 0.5; - dcdc_control(&dcdc); + dcdc_port_hv.bus->voltage = dcdc_port_hv.bus->chg_voltage_target + 0.5; + dcdc.control(); float pwm_after = half_bridge_get_duty_cycle(); TEST_ASSERT(pwm_after > pwm_before); // higher duty cycle = less power } @@ -314,8 +310,8 @@ void boost_derating_output_current_too_high() { start_boost(); float pwm_before = half_bridge_get_duty_cycle(); - hv_terminal.current = hv_terminal.chg_current_limit + 0.1; - dcdc_control(&dcdc); + dcdc_port_hv.current = dcdc_port_hv.chg_current_limit + 0.1; + dcdc.control(); float pwm_after = half_bridge_get_duty_cycle(); TEST_ASSERT(pwm_after > pwm_before); } @@ -324,9 +320,9 @@ void boost_derating_input_voltage_too_low() { start_boost(); float pwm_before = half_bridge_get_duty_cycle(); - lv_bus_int.voltage = lv_bus_int.dis_voltage_stop - 0.1; - hv_terminal.current = 0.2; - dcdc_control(&dcdc); + dcdc_port_lv.bus->voltage = dcdc_port_lv.bus->dis_voltage_stop - 0.1; + dcdc_port_hv.current = 0.2; + dcdc.control(); float pwm_after = half_bridge_get_duty_cycle(); TEST_ASSERT(pwm_after > pwm_before); } @@ -335,8 +331,8 @@ void boost_derating_input_current_too_high() { start_boost(); float pwm_before = half_bridge_get_duty_cycle(); - lv_bus_int.current = lv_bus_int.dis_current_limit - 0.1; - dcdc_control(&dcdc); + dcdc_port_lv.current = dcdc_port_lv.dis_current_limit - 0.1; + dcdc.control(); float pwm_after = half_bridge_get_duty_cycle(); TEST_ASSERT(pwm_after > pwm_before); } @@ -346,7 +342,7 @@ void boost_derating_temperature_limits_exceeded() start_boost(); float pwm_before = half_bridge_get_duty_cycle(); dcdc.temp_mosfets = 81; - dcdc_control(&dcdc); + dcdc.control(); float pwm_after = half_bridge_get_duty_cycle(); TEST_ASSERT(pwm_after > pwm_before); } @@ -355,15 +351,15 @@ void boost_stop_input_power_too_low() { start_boost(); dcdc.power_good_timestamp = time(NULL) - 11; - dcdc_control(&dcdc); + dcdc.control(); TEST_ASSERT(half_bridge_enabled() == false); } void boost_stop_high_voltage_emergency() { start_boost(); - hv_terminal.voltage = dcdc.hs_voltage_max + 0.1; - dcdc_control(&dcdc); + dcdc_port_hv.bus->voltage = dcdc.hs_voltage_max + 0.1; + dcdc.control(); TEST_ASSERT(half_bridge_enabled() == false); } @@ -371,16 +367,16 @@ void boost_correct_mppt_operation() { start_boost(); - hv_terminal.power = 5; - dcdc_control(&dcdc); + dcdc_port_hv.power = 5; + dcdc.control(); float pwm1 = half_bridge_get_duty_cycle(); - hv_terminal.power = 7; - dcdc_control(&dcdc); + dcdc_port_hv.power = 7; + dcdc.control(); float pwm2 = half_bridge_get_duty_cycle(); TEST_ASSERT(pwm2 < pwm1); - hv_terminal.power = 6; // decrease power to make the direction turn around - dcdc_control(&dcdc); + dcdc_port_hv.power = 6; // decrease power to make the direction turn around + dcdc.control(); float pwm3 = half_bridge_get_duty_cycle(); TEST_ASSERT(pwm3 > pwm2); } diff --git a/test/tests_log.cpp b/test/tests_log.cpp index c70379e..a26cd0f 100644 --- a/test/tests_log.cpp +++ b/test/tests_log.cpp @@ -1,26 +1,18 @@ #include "tests.h" -#include "bat_charger.h" -#include "dc_bus.h" -#include "log.h" - #include #include #include -extern BatConf bat_conf; -extern Charger charger; -extern DcBus *solar_terminal; -extern DcBus *bat_terminal; -extern DcBus load_terminal; -extern LoadOutput load; -extern Dcdc dcdc; -extern LogData log_data; +#include "main.h" + +extern PowerPort *solar_terminal; +extern PowerPort *bat_terminal; void reset_counters_at_start_of_day() { - solar_terminal->voltage = bat_terminal->voltage - 1; + solar_terminal->bus->voltage = bat_terminal->bus->voltage - 1; log_data.day_counter = 0; @@ -48,7 +40,7 @@ void reset_counters_at_start_of_day() TEST_ASSERT_EQUAL(0, log_data.day_counter); // now solar power comes back - solar_terminal->voltage = bat_terminal->voltage + 1; + solar_terminal->bus->voltage = bat_terminal->bus->voltage + 1; log_update_energy(&log_data, solar_terminal, bat_terminal, &load_terminal); // day counter should be increased and daily energy counters reset @@ -61,28 +53,28 @@ void reset_counters_at_start_of_day() void log_new_solar_voltage_max() { - solar_terminal->voltage = 40; + solar_terminal->bus->voltage = 40; log_update_min_max_values(&log_data, &dcdc, &charger, &load, solar_terminal, bat_terminal, &load_terminal); TEST_ASSERT_EQUAL(40, log_data.solar_voltage_max); } void log_new_bat_voltage_max() { - bat_terminal->voltage = 31; + bat_terminal->bus->voltage = 31; log_update_min_max_values(&log_data, &dcdc, &charger, &load, solar_terminal, bat_terminal, &load_terminal); TEST_ASSERT_EQUAL(31, log_data.battery_voltage_max); } void log_new_dcdc_current_max() { - dcdc.lv_bus->current = 21; + dcdc.lvs->current = 21; log_update_min_max_values(&log_data, &dcdc, &charger, &load, solar_terminal, bat_terminal, &load_terminal); TEST_ASSERT_EQUAL(21, log_data.dcdc_current_max); } void log_new_load_current_max() { - load.terminal->current = 21; + load.port->current = 21; log_update_min_max_values(&log_data, &dcdc, &charger, &load, solar_terminal, bat_terminal, &load_terminal); TEST_ASSERT_EQUAL(21, log_data.load_current_max); } @@ -97,7 +89,7 @@ void log_solar_power_max() void log_load_power_max() { - load.terminal->power = 50; + load.port->power = 50; log_update_min_max_values(&log_data, &dcdc, &charger, &load, solar_terminal, bat_terminal, &load_terminal); TEST_ASSERT_EQUAL(50, log_data.load_power_max_day); TEST_ASSERT_EQUAL(50, log_data.load_power_max_total);