diff --git a/src/adc_dma.cpp b/src/adc_dma.cpp index c4206b1..354927c 100644 --- a/src/adc_dma.cpp +++ b/src/adc_dma.cpp @@ -72,9 +72,10 @@ void calibrate_current_sensors() { int vcc = VREFINT_VALUE * VREFINT_CAL / (adc_filtered[ADC_POS_VREF_MCU] >> (4 + ADC_FILTER_CONST)); -#ifdef CHARGER_TYPE_PWM +#if FEATURE_PWM_SWITCH solar_current_offset = -(float)(((adc_filtered[ADC_POS_I_SOLAR] >> (4 + ADC_FILTER_CONST)) * vcc) / 4096) * ADC_GAIN_I_SOLAR / 1000.0; -#else +#endif +#if FEATURE_DCDC_CONVERTER solar_current_offset = -(float)(((adc_filtered[ADC_POS_I_DCDC] >> (4 + ADC_FILTER_CONST)) * vcc) / 4096) * ADC_GAIN_I_DCDC / 1000.0; #endif load_current_offset = -(float)(((adc_filtered[ADC_POS_I_LOAD] >> (4 + ADC_FILTER_CONST)) * vcc) / 4096) * ADC_GAIN_I_LOAD / 1000.0; @@ -83,8 +84,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)); @@ -95,42 +94,49 @@ void update_measurements() // rely on LDO accuracy //int vcc = 3300; - // calculate LS voltage first, as it is needed for HS voltage calculation for PWM charge controller + // calculate lower voltage first, as it is needed for PWM terminal voltage calculation lv_terminal.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; +#if FEATURE_DCDC_CONVERTER hv_terminal.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); + dcdc_lv_port.voltage = lv_terminal.voltage; +#endif +#if FEATURE_PWM_SWITCH + pwm_terminal.voltage = lv_terminal.voltage - vcc * ADC_OFFSET_V_SOLAR / 1000.0 - + (float)(((adc_filtered[ADC_POS_V_SOLAR] >> (4 + ADC_FILTER_CONST)) * vcc) / 4096) * + ADC_GAIN_V_SOLAR / 1000.0; + pwm_port_int.voltage = lv_terminal.voltage; #endif load_terminal.current = (float)(((adc_filtered[ADC_POS_I_LOAD] >> (4 + ADC_FILTER_CONST)) * vcc) / 4096) * ADC_GAIN_I_LOAD / 1000.0 + load_current_offset; -#ifdef CHARGER_TYPE_PWM +#if FEATURE_PWM_SWITCH // current multiplied with PWM duty cycle for PWM charger to get avg current for correct power calculation - hv_terminal.current = - pwm_switch_get_duty_cycle() * ( + pwm_terminal.current = - pwm_switch.get_duty_cycle() * ( (float)(((adc_filtered[ADC_POS_I_SOLAR] >> (4 + ADC_FILTER_CONST)) * vcc) / 4096) * ADC_GAIN_I_SOLAR / 1000.0 + solar_current_offset); - lv_terminal.current = -hv_terminal.current - load_terminal.current; -#else // MPPT - lv_bus_int.current = + pwm_port_int.current = pwm_terminal.current; + lv_terminal.current = -pwm_port_int.current - load_terminal.current; + pwm_terminal.power = pwm_terminal.voltage * pwm_terminal.current; +#endif +#if FEATURE_DCDC_CONVERTER + dcdc_lv_port.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; -#endif + lv_terminal.current = dcdc_lv_port.current - load_terminal.current; + hv_terminal.current = -dcdc_lv_port.current * lv_terminal.voltage / hv_terminal.voltage; - // 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; + dcdc_lv_port.power = dcdc_lv_port.voltage * dcdc_lv_port.current; + hv_terminal.power = hv_terminal.voltage * hv_terminal.current; +#endif + lv_terminal.power = lv_terminal.voltage * lv_terminal.current; load_terminal.power = load_terminal.voltage * load_terminal.current; /** \todo Improved (faster) temperature calculation: diff --git a/src/bat_charger.cpp b/src/bat_charger.cpp index 3895cae..ebe59f7 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) +void Charger::detect_num_batteries(BatConf *bat) { - 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) -{ - if (bus->voltage > bat->voltage_absolute_min * 2 && - bus->voltage < bat->voltage_absolute_max * 2) { - charger->num_batteries = 2; + if (port->voltage > bat->voltage_absolute_min * 2 && + port->voltage < bat->voltage_absolute_max * 2) { + num_batteries = 2; printf("Detected two batteries (total %.2f V max)\n", bat->topping_voltage * 2); } else { @@ -240,12 +227,12 @@ 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) { - int soc_new = (int)((bus->voltage - bat_conf->ocv_empty) / + if (fabs(port->current) < 0.2) { + int soc_new = (int)((port->voltage - bat_conf->ocv_empty) / (bat_conf->ocv_full - bat_conf->ocv_empty) * 10000.0); if (soc_new > 500 && soc_filtered == 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->neg_current_limit < 0 // discharging allowed + && port->voltage < num_batteries * + (bat_conf->voltage_load_disconnect - port->current * port->neg_droop_res)) { // low state of charge - bus->dis_current_limit = 0; - charger->num_deep_discharges++; + port->neg_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->neg_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->neg_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 (port->voltage >= num_batteries * + (bat_conf->voltage_load_reconnect - port->current * port->neg_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->neg_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->pos_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 (port->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); + port->sink_voltage_max = num_batteries * (bat_conf->topping_voltage + + bat_conf->temperature_compensation * (bat_temperature - 25)); + port->pos_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)); + port->sink_voltage_max = 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 (port->voltage > num_batteries * + (port->sink_voltage_max - port->current * port->pos_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)); + port->sink_voltage_max = 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 (port->voltage >= num_batteries * + (port->sink_voltage_max - port->current * port->pos_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->sink_voltage_max = bat_conf->equalization_voltage; + port->pos_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); + port->sink_voltage_max = 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->pos_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)); + port->sink_voltage_max = 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 (port->voltage >= num_batteries * + (port->sink_voltage_max - port->current * port->pos_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->pos_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)); + port->sink_voltage_max = 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); + port->sink_voltage_max = 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->pos_current_limit = 0; + enter_state(CHG_STATE_IDLE); } } break; @@ -453,37 +441,21 @@ 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(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->src_voltage_start = bat->voltage_load_reconnect * n; + port->src_voltage_stop = bat->voltage_load_disconnect * n; + port->neg_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->neg_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->sink_voltage_max = bat->topping_voltage * n; + port->sink_voltage_min = bat->voltage_absolute_min * n; + port->pos_current_limit = bat->charge_current_max; // negative sign for compensation of actual resistance - bus->chg_droop_res = -bat->wire_resistance * n; -} - -void charger_update_junction_bus(DcBus *junction, DcBus *bat, DcBus *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; - - // 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; + port->pos_droop_res = -bat->wire_resistance * n; } diff --git a/src/bat_charger.h b/src/bat_charger.h index 0fb6034..b083e49 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,20 @@ typedef struct /** Charger configuration and battery state */ -typedef struct +class Charger { +public: + Charger(PowerPort *pwr_port): + port(pwr_port) {}; + + PowerPort *port; + 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 +256,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 +266,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 +361,10 @@ 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); - -/** 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 battery_init_dc_bus(PowerPort *port, BatConf *bat, unsigned int num_batteries); #endif /* BAT_CHARGER_H */ diff --git a/src/config.h_template b/src/config.h_template index fcb0ae7..393f96b 100644 --- a/src/config.h_template +++ b/src/config.h_template @@ -26,6 +26,9 @@ #define DCDC_MODE_INIT MODE_MPPT_BUCK //#define DCDC_MODE_INIT MODE_NANOGRID +#define SOLAR_TERMINAL hv_terminal +#define BATTERY_TERMINAL lv_terminal + // basic battery configuration #define BATTERY_TYPE BAT_TYPE_GEL ///< GEL most suitable for general batteries (see bat_charger.h for other types) #define BATTERY_NUM_CELLS 6 ///< For lead-acid batteries: 6 for 12V system, 12 for 24V system diff --git a/src/data_objects.cpp b/src/data_objects.cpp index 9ed38ef..8de771a 100644 --- a/src/data_objects.cpp +++ b/src/data_objects.cpp @@ -91,8 +91,8 @@ const data_object_t data_objects[] = { {0x5D, TS_CONF, TS_READ_ALL | TS_WRITE_ALL, TS_T_INT32, 2, (void*) &(bat_conf_user.equalization_trigger_deep_cycles), "EqDeepDisTrigger"}, // load settings - {0x40, TS_CONF, TS_READ_ALL | TS_WRITE_ALL, TS_T_BOOL, 0, (void*) &(load.enabled_target), "LoadEnDefault"}, - {0x41, TS_CONF, TS_READ_ALL | TS_WRITE_ALL, TS_T_BOOL, 0, (void*) &(load.usb_enabled_target), "UsbEnDefault"}, + {0x40, TS_CONF, TS_READ_ALL | TS_WRITE_ALL, TS_T_BOOL, 0, (void*) &(load.enable), "LoadEnDefault"}, + {0x41, TS_CONF, TS_READ_ALL | TS_WRITE_ALL, TS_T_BOOL, 0, (void*) &(load.usb_enable), "UsbEnDefault"}, {0x42, TS_CONF, TS_READ_ALL | TS_WRITE_ALL, TS_T_FLOAT32, 2, (void*) &(bat_conf_user.voltage_load_disconnect), "LoadDisconnect_V"}, {0x43, TS_CONF, TS_READ_ALL | TS_WRITE_ALL, TS_T_FLOAT32, 2, (void*) &(bat_conf_user.voltage_load_reconnect), "LoadReconnect_V"}, //{0x44, TS_CONF, TS_READ_ALL | TS_WRITE_ALL, TS_T_FLOAT32, 2, (void*) &(bat_conf_user.voltage_load_disconnect), "UsbDisconnect_V"}, @@ -105,11 +105,12 @@ const data_object_t data_objects[] = { // INPUT DATA ///////////////////////////////////////////////////////////// // using IDs >= 0x60 - {0x60, TS_INPUT, TS_READ_ALL | TS_WRITE_ALL, TS_T_BOOL, 0, (void*) &(load.enabled_target), "LoadEn"}, // change w/o store setting in NVM - {0x61, TS_INPUT, TS_READ_ALL | TS_WRITE_ALL, TS_T_BOOL, 0, (void*) &(load.usb_enabled_target), "UsbEn"}, -#ifdef CHARGER_TYPE_PWM + {0x60, TS_INPUT, TS_READ_ALL | TS_WRITE_ALL, TS_T_BOOL, 0, (void*) &(load.enable), "LoadEn"}, // change w/o store setting in NVM + {0x61, TS_INPUT, TS_READ_ALL | TS_WRITE_ALL, TS_T_BOOL, 0, (void*) &(load.usb_enable), "UsbEn"}, +#if FEATURE_PWM_SWITCH {0x62, TS_INPUT, TS_READ_ALL | TS_WRITE_ALL, TS_T_BOOL, 0, (void*) &(pwm_switch.enabled), "PwmEn"}, -#else +#endif +#if FEATURE_DCDC_CONVERTER {0x62, TS_INPUT, TS_READ_ALL | TS_WRITE_ALL, TS_T_BOOL, 0, (void*) &(dcdc.enabled), "DcdcEn"}, #endif @@ -117,29 +118,31 @@ const data_object_t data_objects[] = { // using IDs >= 0x70 except for high priority data objects // high priority data objects (low IDs) - {0x04, TS_OUTPUT, TS_READ_ALL, TS_T_UINT16, 0, (void*) &(load.switch_state), "LoadState"}, - {0x05, TS_OUTPUT, TS_READ_ALL, TS_T_UINT16, 0, (void*) &(load.usb_state), "UsbState"}, - {0x06, TS_OUTPUT, TS_READ_ALL, TS_T_UINT16, 0, (void*) &(charger.soc), "SOC_%"}, // output will be uint8_t + {0x04, TS_OUTPUT, TS_READ_ALL, TS_T_UINT16, 0, (void*) &(load.switch_state), "LoadState"}, + {0x05, TS_OUTPUT, TS_READ_ALL, TS_T_UINT16, 0, (void*) &(load.usb_state), "UsbState"}, + {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"}, - {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"}, - {0x75, TS_OUTPUT, TS_READ_ALL, TS_T_BOOL, 1, (void*) &(charger.ext_temp_sensor), "BatTempExt"}, - {0x76, TS_OUTPUT, TS_READ_ALL, TS_T_FLOAT32, 1, (void*) &(mcu_temp), "MCU_degC"}, -#ifdef PIN_ADC_TEMP_FETS - {0x77, TS_OUTPUT, TS_READ_ALL, TS_T_FLOAT32, 1, (void*) &(dcdc.temp_mosfets), "Mosfet_degC"}, + {0x70, TS_OUTPUT, TS_READ_ALL, TS_T_FLOAT32, 2, (void*) &(bat_terminal.voltage), "Bat_V"}, + {0x71, TS_OUTPUT, TS_READ_ALL, TS_T_FLOAT32, 2, (void*) &(solar_terminal.voltage), "Solar_V"}, + {0x72, TS_OUTPUT, TS_READ_ALL, TS_T_FLOAT32, 2, (void*) &(bat_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"}, + {0x75, TS_OUTPUT, TS_READ_ALL, TS_T_BOOL, 1, (void*) &(charger.ext_temp_sensor), "BatTempExt"}, + {0x76, TS_OUTPUT, TS_READ_ALL, TS_T_FLOAT32, 1, (void*) &(mcu_temp), "MCU_degC"}, +#if defined(PIN_ADC_TEMP_FETS) && FEATURE_DCDC_CONVERTER + {0x77, TS_OUTPUT, TS_READ_ALL, TS_T_FLOAT32, 1, (void*) &(dcdc.temp_mosfets), "Mosfet_degC"}, #endif - {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"}, - {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"}, - {0x7F, TS_OUTPUT, TS_READ_ALL, TS_T_FLOAT32, 2, (void*) &(load_terminal.power), "Load_W"}, + {0x78, TS_OUTPUT, TS_READ_ALL, TS_T_UINT16, 0, (void*) &(charger.state), "ChgState"}, +#if FEATURE_DCDC_CONVERTER + {0x79, TS_OUTPUT, TS_READ_ALL, TS_T_UINT16, 0, (void*) &(dcdc.state), "DCDCState"}, +#endif + {0x7A, TS_OUTPUT, TS_READ_ALL, TS_T_FLOAT32, 2, (void*) &(solar_terminal.current), "Solar_A"}, + {0x7B, TS_OUTPUT, TS_READ_ALL, TS_T_FLOAT32, 2, (void*) &(bat_terminal.sink_voltage_max), "BatTarget_V"}, + {0x7C, TS_OUTPUT, TS_READ_ALL, TS_T_FLOAT32, 2, (void*) &(bat_terminal.pos_current_limit), "BatTarget_A"}, + {0x7D, TS_OUTPUT, TS_READ_ALL, TS_T_FLOAT32, 2, (void*) &(bat_terminal.power), "Bat_W"}, + {0x7E, TS_OUTPUT, TS_READ_ALL, TS_T_FLOAT32, 2, (void*) &(solar_terminal.power), "Solar_W"}, + {0x7F, TS_OUTPUT, TS_READ_ALL, TS_T_FLOAT32, 2, (void*) &(load_terminal.power), "Load_W"}, // RECORDED DATA /////////////////////////////////////////////////////// // using IDs >= 0xA0 @@ -155,10 +158,10 @@ const data_object_t data_objects[] = { {0x10, TS_REC, TS_READ_ALL | TS_WRITE_MAKER, TS_T_UINT16, 2, (void*) &(log_data.load_power_max_day), "LoadMaxDay_W"}, // accumulated data - {0xA0, TS_REC, TS_READ_ALL | TS_WRITE_MAKER, TS_T_FLOAT32, 2, (void*) &(hv_terminal.dis_energy_Wh), "SolarInDay_Wh"}, - {0xA1, TS_REC, TS_READ_ALL | TS_WRITE_MAKER, TS_T_FLOAT32, 2, (void*) &(load_terminal.chg_energy_Wh), "LoadOutDay_Wh"}, - {0xA2, TS_REC, TS_READ_ALL | TS_WRITE_MAKER, TS_T_FLOAT32, 2, (void*) &(lv_terminal.chg_energy_Wh), "BatChgDay_Wh"}, - {0xA3, TS_REC, TS_READ_ALL | TS_WRITE_MAKER, TS_T_FLOAT32, 2, (void*) &(lv_terminal.dis_energy_Wh), "BatDisDay_Wh"}, + {0xA0, TS_REC, TS_READ_ALL | TS_WRITE_MAKER, TS_T_FLOAT32, 2, (void*) &(solar_terminal.neg_energy_Wh), "SolarInDay_Wh"}, + {0xA1, TS_REC, TS_READ_ALL | TS_WRITE_MAKER, TS_T_FLOAT32, 2, (void*) &(load_terminal.pos_energy_Wh), "LoadOutDay_Wh"}, + {0xA2, TS_REC, TS_READ_ALL | TS_WRITE_MAKER, TS_T_FLOAT32, 2, (void*) &(bat_terminal.pos_energy_Wh), "BatChgDay_Wh"}, + {0xA3, TS_REC, TS_READ_ALL | TS_WRITE_MAKER, TS_T_FLOAT32, 2, (void*) &(bat_terminal.neg_energy_Wh), "BatDisDay_Wh"}, {0xA4, TS_REC, TS_READ_ALL | TS_WRITE_MAKER, TS_T_FLOAT32, 0, (void*) &(charger.discharged_Ah), "Dis_Ah"}, // coulomb counter {0xA5, TS_REC, TS_READ_ALL | TS_WRITE_MAKER, TS_T_UINT16, 0, (void*) &(charger.soh), "SOH_%"}, // output will be uint8_t {0xA6, TS_REC, TS_READ_ALL | TS_WRITE_MAKER, TS_T_INT32, 0, (void*) &(log_data.day_counter), "DayCount"}, @@ -178,11 +181,13 @@ const data_object_t data_objects[] = { // CALIBRATION DATA /////////////////////////////////////////////////////// // using IDs >= 0xD0 +#if FEATURE_DCDC_CONVERTER {0xD0, TS_CAL, TS_READ_ALL | TS_WRITE_MAKER, TS_T_FLOAT32, 1, (void*) &(dcdc.output_power_min), "DcdcMin_W"}, {0xD1, TS_CAL, TS_READ_ALL | TS_WRITE_MAKER, TS_T_FLOAT32, 1, (void*) &(dcdc.hs_voltage_max), "SolarAbsMax_V"}, {0xD2, TS_CAL, TS_READ_ALL | TS_WRITE_MAKER, TS_T_INT32, 0, (void*) &(dcdc.restart_interval), "DcdcRestart_s"}, //{0xD3, TS_CAL, TS_READ_ALL | TS_WRITE_MAKER, TS_T_FLOAT32, 1, (void*) &(dcdc.offset_voltage_start), "SolarOffsetStart_V"}, //{0xD4, TS_CAL, TS_READ_ALL | TS_WRITE_MAKER, TS_T_FLOAT32, 1, (void*) &(dcdc.offset_voltage_stop), "SolarOffsetStop_V"} +#endif // FUNCTION CALLS (EXEC) ////////////////////////////////////////////////// #ifndef UNIT_TEST diff --git a/src/dc_bus.cpp b/src/dc_bus.cpp deleted file mode 100644 index 9df61d5..0000000 --- a/src/dc_bus.cpp +++ /dev/null @@ -1,52 +0,0 @@ -/* LibreSolar MPPT charge controller firmware - * Copyright (c) 2016-2018 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. - */ - -#include "dc_bus.h" - -void dc_bus_init_solar(DcBus *bus, float max_abs_current) -{ - 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) -{ - 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) -{ - // 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; - } - else { - bus->dis_energy_Wh -= bus->voltage * bus->current / 3600.0; - } - -} \ No newline at end of file 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..b300d80 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,136 @@ extern LogData log_data; -void dcdc_init(Dcdc *dcdc, DcBus *hv, DcBus *lv, DcdcOperationMode mode) +#if FEATURE_DCDC_CONVERTER == 0 + +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 + + // lower duty limit might have to be adjusted dynamically depending on LS voltage + half_bridge_init(PWM_FREQUENCY, PWM_DEADTIME, 12 / hs_voltage_max, 0.97); } -/** 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->voltage, out->voltage, in->current, out->current, + out->sink_voltage_max, out->pos_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->voltage > (out->sink_voltage_max - out->pos_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->pos_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->voltage < (in->src_voltage_start - in->neg_droop_res * in->current) + && out->current > 0.1) // input voltage below limit + || in->current < in->neg_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->voltage < out->src_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->voltage > hs_voltage_max || // also critical for buck mode because of ringing + lvs->voltage > ls_voltage_max || + lvs->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->pos_current_limit > 0 && + lvs->voltage < lvs->sink_voltage_max && + lvs->voltage > lvs->sink_voltage_min && + hvs->neg_current_limit < 0 && + hvs->voltage > hvs->src_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->pos_current_limit > 0 && + hvs->voltage < hvs->sink_voltage_max && + hvs->voltage > hvs->sink_voltage_min && + lvs->neg_current_limit < 0 && + lvs->voltage > lvs->src_voltage_start) { return -1; // start in boost mode allowed } @@ -157,33 +164,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->voltage > ls_voltage_max || hvs->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 +205,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->voltage / (hvs->voltage - 1)); + printf("DC/DC buck mode start (HV: %.2fV, LV: %.2fV, PWM: %.1f).\n", + hvs->voltage, lvs->voltage, half_bridge_get_duty_cycle() * 100); } else { startup_delay_counter++; @@ -214,10 +226,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->voltage / (hvs->voltage + 1)); + printf("DC/DC boost mode start (HV: %.2fV, LV: %.2fV, PWM: %.1f).\n", + hvs->voltage, lvs->voltage, half_bridge_get_duty_cycle() * 100); } else { startup_delay_counter++; @@ -229,7 +243,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 +252,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->voltage / (hvs->voltage - 1)); + printf("DC/DC buck mode start (HV: %.2fV, LV: %.2fV, PWM: %.1f).\n", + hvs->voltage, lvs->voltage, half_bridge_get_duty_cycle() * 100); } else { startup_delay_counter++; @@ -253,9 +271,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->voltage / (hvs->voltage + 1)); + printf("DC/DC boost mode start (HV: %.2fV, LV: %.2fV, PWM: %.1f).\n", + hvs->voltage, lvs->voltage, half_bridge_get_duty_cycle() * 100); } else { startup_delay_counter++; @@ -267,7 +287,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..1fff592 100644 --- a/src/load.cpp +++ b/src/load.cpp @@ -20,10 +20,12 @@ #include "hardware.h" #include "leds.h" #include "log.h" -#include 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 +143,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 +165,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,169 +183,163 @@ 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; + current_max = LOAD_CURRENT_MAX; + enable = true; + usb_enable = 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_enable == 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_enable == 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_enable == 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; } } -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) - { - load_switch_set(true); - load->switch_state = LOAD_STATE_ON; + if (enable == true && port->pos_current_limit > 0) { + 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 (enable == 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->pos_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->pos_current_limit > 0 && time(NULL) - lvd_timestamp > 60*60) { - if (load->enabled_target == true) { - load_switch_set(true); - load->switch_state = LOAD_STATE_ON; + if (enable == 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 (port->voltage < port->sink_voltage_max && + port->voltage < LOW_SIDE_VOLTAGE_MAX) + { + 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 (enable == 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() { 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 (port->voltage > port->sink_voltage_max || + port->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..cacd2aa 100644 --- a/src/load.h +++ b/src/load.h @@ -25,8 +25,9 @@ #include #include +#include -#include "dc_bus.h" +#include "power_port.h" /** Load/USB output states * @@ -45,52 +46,55 @@ 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(); + 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 - ///< 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 - int overcurrent_timestamp; ///< time at which an overcurrent event occured + time_t overcurrent_timestamp; ///< time at which an overcurrent event occured + time_t lvd_timestamp; ///< stores when the load was disconnected last time float junction_temperature; ///< calculated using thermal model based on current and ambient ///< temperature measurement (unit: °C) - bool enabled_target; ///< target setting defined via communication port (overruled if + bool enable; ///< 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); + bool usb_enable; ///< same for USB output +}; #endif /* LOAD_H */ diff --git a/src/log.cpp b/src/log.cpp index 3815ec6..da93cd2 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) { // 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_terminal.voltage < bat_terminal.voltage) { seconds_zero_solar += 1; } else { @@ -53,58 +52,64 @@ void log_update_energy(LogData *log_data, DcBus *solar, DcBus *bat, DcBus *load) load_out_total_Wh_prev = log_data->load_out_total_Wh; bat_chg_total_Wh_prev = log_data->bat_chg_total_Wh; bat_dis_total_Wh_prev = log_data->bat_dis_total_Wh; - solar->dis_energy_Wh = 0.0; - load->chg_energy_Wh = 0.0; - bat->chg_energy_Wh = 0.0; - bat->dis_energy_Wh = 0.0; + solar_terminal.neg_energy_Wh = 0.0; + load_terminal.pos_energy_Wh = 0.0; + bat_terminal.pos_energy_Wh = 0.0; + bat_terminal.neg_energy_Wh = 0.0; } 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_terminal.pos_energy_Wh > 0 ? bat_terminal.pos_energy_Wh : 0); + log_data->bat_dis_total_Wh = bat_dis_total_Wh_prev + + (bat_terminal.neg_energy_Wh > 0 ? bat_terminal.neg_energy_Wh : 0); + log_data->solar_in_total_Wh = solar_in_total_Wh_prev + + (solar_terminal.neg_energy_Wh > 0 ? solar_terminal.neg_energy_Wh : 0); + log_data->load_out_total_Wh = load_out_total_Wh_prev + + (load_terminal.pos_energy_Wh > 0 ? load_terminal.pos_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) { - if (bat_bus->voltage > log_data->battery_voltage_max) { - log_data->battery_voltage_max = bat_bus->voltage; + if (bat_terminal.voltage > log_data->battery_voltage_max) { + log_data->battery_voltage_max = bat_terminal.voltage; } - if (solar_bus->voltage > log_data->solar_voltage_max) { - log_data->solar_voltage_max = solar_bus->voltage; + if (solar_terminal.voltage > log_data->solar_voltage_max) { + log_data->solar_voltage_max = solar_terminal.voltage; } - if (dcdc->lv_bus->current > log_data->dcdc_current_max) { - log_data->dcdc_current_max = dcdc->lv_bus->current; +#if FEATURE_DCDC_CONVERTER + 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 (dcdc.temp_mosfets > log_data->mosfet_temp_max) { + log_data->mosfet_temp_max = dcdc.temp_mosfets; + } +#endif + + if (load_terminal.current > log_data->load_current_max) { + log_data->load_current_max = load_terminal.current; } - if (-solar_bus->power > log_data->solar_power_max_day) { - log_data->solar_power_max_day = -solar_bus->power; + if (-solar_terminal.power > log_data->solar_power_max_day) { + log_data->solar_power_max_day = -solar_terminal.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_terminal.power > log_data->load_power_max_day) { + log_data->load_power_max_day = load_terminal.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; } } - if (dcdc->temp_mosfets > log_data->mosfet_temp_max) { - log_data->mosfet_temp_max = dcdc->temp_mosfets; - } - - if (charger->bat_temperature > log_data->bat_temp_max) { - log_data->bat_temp_max = charger->bat_temperature; + if (charger.bat_temperature > log_data->bat_temp_max) { + log_data->bat_temp_max = charger.bat_temperature; } if (mcu_temp > log_data->int_temp_max) { diff --git a/src/log.h b/src/log.h index 9d6141e..e547ad4 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,10 @@ 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); /** 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); #endif /* LOG_H */ diff --git a/src/main.cpp b/src/main.cpp index bf670f4..6e10738 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -42,63 +42,45 @@ #include "thingset_serial.h" // UART or USB serial communication #include "thingset_can.h" // CAN bus communication -Serial serial(PIN_SWD_TX, PIN_SWD_RX, "serial", 115200); +PowerPort lv_terminal; // low voltage terminal (battery for typical MPPT) + +#if FEATURE_DCDC_CONVERTER +PowerPort hv_terminal; // high voltage terminal (solar for typical MPPT) +PowerPort dcdc_lv_port; // internal low voltage side of DC/DC converter +Dcdc dcdc(&hv_terminal, &dcdc_lv_port, DCDC_MODE_INIT); +#endif + +#if FEATURE_PWM_SWITCH +PowerPort pwm_terminal; // external terminal of PWM switch port (normally solar) +PowerPort pwm_port_int; // internal side of PWM switch +PwmSwitch pwm_switch(&pwm_terminal, &pwm_port_int); +PowerPort &solar_terminal = pwm_terminal; +#else +PowerPort &solar_terminal = SOLAR_TERMINAL; // defined in config.h +#endif + +#if FEATURE_LOAD_OUTPUT +PowerPort load_terminal; // load terminal (also connected to lv_bus) +LoadOutput load(&load_terminal); +#endif + +PowerPort &bat_terminal = BATTERY_TERMINAL; // defined in config.h +#ifdef GRID_TERMINAL +PowerPort &grid_terminal = GRID_TERMINAL; +#endif + +Charger charger(&bat_terminal); -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 -PwmSwitch pwm_switch = {}; // only necessary for PWM charger 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) -/** High priority function for DC/DC control and safety functions - * - * Called by control timer with 10 Hz frequency (see hardware.cpp). - */ -void system_control() -{ - static int counter = 0; - - // convert ADC readings to meaningful measurement values - update_measurements(); - -#ifdef CHARGER_TYPE_PWM - pwm_switch_control(&pwm_switch, &hv_terminal, bat_terminal); - leds_set_charging(pwm_switch_enabled()); -#else - // control PWM of the DC/DC according to hs and ls port settings - // (this function includes MPPT algorithm) - dcdc_control(&dcdc); - leds_set_charging(half_bridge_enabled()); -#endif - - load_control(&load, 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) - // see also here: https://github.com/ARMmbed/mbed-os/issues/9065 - 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); - 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); - } - counter++; -} +Serial serial(PIN_SWD_TX, PIN_SWD_RX, "serial", 115200); /** Main function including initialization and continuous loop */ @@ -108,16 +90,6 @@ 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 // Configuration from EEPROM data_objects_read_eeprom(); @@ -139,31 +111,15 @@ int main() uext_init(); init_watchdog(10); // 10s should be enough for communication ports -#ifdef CHARGER_TYPE_PWM - bat_terminal = &lv_terminal; - solar_terminal = &hv_terminal; - dc_bus_init_solar(solar_terminal, 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); - 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); - 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); - 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); + solar_terminal.init_solar(); + + #ifdef GRID_TERMINAL + grid_terminal.init_nanogrid(); + #endif + + charger.detect_num_batteries(&bat_conf); // check if we have 24V instead of 12V system + battery_init_dc_bus(&bat_terminal, &bat_conf, charger.num_batteries); + load_terminal.init_load(bat_conf.voltage_absolute_max * charger.num_batteries); wait(2); // safety feature: be able to re-flash before starting control_timer_start(CONTROL_FREQUENCY); @@ -189,12 +145,17 @@ 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); + #if FEATURE_DCDC_CONVERTER + bat_terminal.pass_voltage_targets(&dcdc_lv_port); + #endif + #if FEATURE_PWM_SWITCH + bat_terminal.pass_voltage_targets(&pwm_port_int); + #endif - load_state_machine(&load); + load.state_machine(); eeprom_update(); @@ -211,4 +172,45 @@ int main() } } +/** High priority function for DC/DC / PWM control and safety functions + * + * Called by control timer with 10 Hz frequency (see hardware.cpp). + */ +void system_control() +{ + static int counter = 0; + + // convert ADC readings to meaningful measurement values + update_measurements(); + + #if FEATURE_PWM_SWITCH + ports_update_current_limits(&pwm_port_int, &bat_terminal, &load_terminal); + pwm_switch.control(); + leds_set_charging(pwm_switch.active()); + #endif + + #if FEATURE_DCDC_CONVERTER + ports_update_current_limits(&dcdc_lv_port, &bat_terminal, &load_terminal); + dcdc.control(); // control of DC/DC including MPPT algorithm + leds_set_charging(half_bridge_enabled()); + #endif + + load.control(); + + if (counter % CONTROL_FREQUENCY == 0) { + // called once per second (this timer is much more accurate than time(NULL) based on LSI) + // see also here: https://github.com/ARMmbed/mbed-os/issues/9065 + timestamp++; + counter = 0; + // energy + soc calculation must be called exactly once per second + solar_terminal.energy_balance(); + bat_terminal.energy_balance(); + load_terminal.energy_balance(); + log_update_energy(&log_data); + log_update_min_max_values(&log_data); + charger.update_soc(&bat_conf); + } + counter++; +} + #endif diff --git a/src/main.h b/src/main.h index 29e77c4..a7b8558 100644 --- a/src/main.h +++ b/src/main.h @@ -19,20 +19,34 @@ #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" +#include "pcb.h" + +extern PowerPort lv_terminal; +extern PowerPort load_terminal; + +#if FEATURE_DCDC_CONVERTER +extern PowerPort hv_terminal; +extern PowerPort dcdc_lv_port; +extern Dcdc dcdc; +#endif + +#if FEATURE_PWM_SWITCH +extern PowerPort pwm_terminal; +extern PowerPort pwm_port_int; +extern PwmSwitch pwm_switch; +#endif + +extern PowerPort &bat_terminal; +extern PowerPort &solar_terminal; extern LogData log_data; extern Charger charger; extern BatConf bat_conf; extern BatConf bat_conf_user; -extern Dcdc dcdc; + 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/pcbs/mppt_1210_hus_0v2.h b/src/pcbs/mppt_1210_hus_0v2.h index e8f8084..d067233 100644 --- a/src/pcbs/mppt_1210_hus_0v2.h +++ b/src/pcbs/mppt_1210_hus_0v2.h @@ -22,6 +22,11 @@ #include "mbed.h" +// specify features of charge controller +#define FEATURE_DCDC_CONVERTER 1 +#define FEATURE_PWM_SWITCH 0 +#define FEATURE_LOAD_OUTPUT 1 + // DC/DC converter settings #define PWM_FREQUENCY 50 // kHz 50 = better for cloud solar to increase efficiency #define PWM_DEADTIME 300 // ns diff --git a/src/pcbs/mppt_1210_hus_0v4.h b/src/pcbs/mppt_1210_hus_0v4.h index 073ed59..43c4cf4 100644 --- a/src/pcbs/mppt_1210_hus_0v4.h +++ b/src/pcbs/mppt_1210_hus_0v4.h @@ -22,6 +22,11 @@ #include "mbed.h" +// specify features of charge controller +#define FEATURE_DCDC_CONVERTER 1 +#define FEATURE_PWM_SWITCH 0 +#define FEATURE_LOAD_OUTPUT 1 + // DC/DC converter settings #define PWM_FREQUENCY 50 // kHz 50 = better for cloud solar to increase efficiency #define PWM_DEADTIME 230 // ns diff --git a/src/pcbs/mppt_1210_hus_0v7.h b/src/pcbs/mppt_1210_hus_0v7.h index 51bc433..d90f368 100644 --- a/src/pcbs/mppt_1210_hus_0v7.h +++ b/src/pcbs/mppt_1210_hus_0v7.h @@ -22,6 +22,11 @@ #include "mbed.h" +// specify features of charge controller +#define FEATURE_DCDC_CONVERTER 1 +#define FEATURE_PWM_SWITCH 0 +#define FEATURE_LOAD_OUTPUT 1 + // DC/DC converter settings #define PWM_FREQUENCY 50 // kHz 50 = better for cloud solar to increase efficiency #define PWM_DEADTIME 230 // ns diff --git a/src/pcbs/mppt_2420_lc_0v10.h b/src/pcbs/mppt_2420_lc_0v10.h index 5e7736e..d715c86 100644 --- a/src/pcbs/mppt_2420_lc_0v10.h +++ b/src/pcbs/mppt_2420_lc_0v10.h @@ -22,6 +22,11 @@ #include "mbed.h" +// specify features of charge controller +#define FEATURE_DCDC_CONVERTER 1 +#define FEATURE_PWM_SWITCH 0 +#define FEATURE_LOAD_OUTPUT 1 + // DC/DC converter settings #define PWM_FREQUENCY 70 // kHz 70 = good compromise between output ripple and efficiency #define PWM_DEADTIME 300 // ns diff --git a/src/pcbs/pcb_stub.h b/src/pcbs/pcb_stub.h index c80f972..cc45131 100644 --- a/src/pcbs/pcb_stub.h +++ b/src/pcbs/pcb_stub.h @@ -20,6 +20,13 @@ #define DEVICE_TYPE "PCB-STUB" #define HARDWARE_VERSION "v0.1" +// specify features of charge controller +#define FEATURE_DCDC_CONVERTER 1 +#define FEATURE_PWM_SWITCH 1 +#define FEATURE_LOAD_OUTPUT 1 + +#define PWM_FREQUENCY 50 // kHz +#define PWM_DEADTIME 230 // ns #define PWM_TIM 3 // use TIM3 timer #define DCDC_CURRENT_MAX 20 // PCB maximum DCDC output current @@ -80,6 +87,12 @@ static const pin_state_t led_pin_setup[NUM_LEDS][NUM_LED_PINS] = { #define ADC_GAIN_I_LOAD (1000 / 4 / 50) // amp gain: 50, resistor: 4 mOhm #define ADC_GAIN_I_DCDC (1000 / 4 / 50) +#define ADC_OFFSET_V_SOLAR (-120.0 / 8.2) // to be multiplied with VDDA to get absolute voltage offset + +// trick to pass unit tests for MPPT +#define ADC_POS_I_SOLAR ADC_POS_I_DCDC +#define ADC_GAIN_I_SOLAR ADC_GAIN_I_DCDC + // position in the array written by the DMA controller enum { ADC_POS_V_BAT, // ADC 0 (PA_0) @@ -92,5 +105,4 @@ enum { NUM_ADC_CH // trick to get the number of elements }; - #endif diff --git a/src/pcbs/pwm_2420_lus.h b/src/pcbs/pwm_2420_lus.h index 1a0ac65..1683735 100644 --- a/src/pcbs/pwm_2420_lus.h +++ b/src/pcbs/pwm_2420_lus.h @@ -27,7 +27,10 @@ #include "mbed.h" -#define CHARGER_TYPE_PWM 1 // PWM charge controller instead of MPPT +// specify features of charge controller +#define FEATURE_DCDC_CONVERTER 0 +#define FEATURE_PWM_SWITCH 1 +#define FEATURE_LOAD_OUTPUT 1 #define PWM_TIM 3 // use TIM3 timer #define PWM_FREQUENCY 25 // Hz diff --git a/src/power_port.cpp b/src/power_port.cpp new file mode 100644 index 0000000..b448c24 --- /dev/null +++ b/src/power_port.cpp @@ -0,0 +1,85 @@ +/* LibreSolar MPPT charge controller firmware + * Copyright (c) 2016-2018 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. + */ + +#include "power_port.h" + +void PowerPort::init_solar() +{ + neg_current_limit = -50; // derating based on max. DC/DC or PWM switch current only + pos_current_limit = 0; // no current towards solar panel allowed + + // also initialize the connected bus + src_voltage_start = 16.0; + src_voltage_stop = 14.0; +} + +void PowerPort::init_nanogrid() +{ + neg_current_limit = -5.0; + neg_droop_res = 0.1; // 1 Ohm means 1V change of target voltage per amp + + pos_current_limit = 5.0; + pos_droop_res = 0.1; // 1 Ohm means 1V change of target voltage per amp + + // also initialize the connected bus + src_voltage_start = 30.0; // starting buck mode above this point + src_voltage_stop = 20.0; // stopping buck mode below this point + + sink_voltage_max = 28.0; // starting idle mode above this point + sink_voltage_min = 10.0; +} + +void PowerPort::init_load(float max_load_voltage) +{ + sink_voltage_max = max_load_voltage; // above this voltage, load is switched off + sink_voltage_min = 0; + + // other settings are not relevant for load output +} + +// must be called exactly once per second, otherwise energy calculation gets wrong +void PowerPort::energy_balance() +{ + // remark: timespan = 1s, so no multiplication with time necessary for energy calculation + if (current >= 0) { + pos_energy_Wh += voltage * current / 3600.0; + } + else { + neg_energy_Wh -= voltage * current / 3600.0; + } +} + +void PowerPort::pass_voltage_targets(PowerPort *port) +{ + // droop resistance added only to limits where current actually flows + port->sink_voltage_min = sink_voltage_min; + port->sink_voltage_max = sink_voltage_max + pos_droop_res * current; + port->src_voltage_start = src_voltage_start + pos_droop_res * current; + port->src_voltage_stop = src_voltage_stop + pos_droop_res * current; +} + +void ports_update_current_limits(PowerPort *p_dcdc, PowerPort *p_bat, PowerPort *p_load) +{ + p_dcdc->pos_current_limit = p_bat->pos_current_limit - p_bat->current + p_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). + p_dcdc->neg_current_limit = p_bat->neg_current_limit - p_bat->current; + + // this statement enables or disables the load output + p_load->pos_current_limit = -p_bat->neg_current_limit; +} diff --git a/src/power_port.h b/src/power_port.h new file mode 100644 index 0000000..1072257 --- /dev/null +++ b/src/power_port.h @@ -0,0 +1,116 @@ +/* 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 + + +// ----------------- +// | | >> positive current/power direction +// | o---->---- +// | | + | +// | considered | | | external device: battery / solar panel / load / DC grid +// | system or | | | +// | sub-circuit | | | (the port should be named after the external device) +// | | - | +// | o--------- +// | | +// ----------------- + +/** Power Port class + * + * Stores actual measurements and voltage / current limits for external terminals or internal ports + * + * The signs follow the passive sign convention. Current or power from the considered system or + * circuit towards an external device connected to the port has a positive sign. For all terminals, + * the entire charge controller is considered as the system boundary and acts as a source or a sink. + * For internal sub-circuits, e.g. the DC/DC converter circuit defines the sub-system boundaries. + * + * Examples: + * - Charging a connected battery has a positive sign, as current flows from the charge controller + * into the battery, i.e. the battery acts as a sink. + * - Power from a solar panel (power source) has a negative sign, as the charge controller acts as + * the sink and power flows from the external device into the charge controller. + * - A DC/DC converter in buck mode results in a positive current flow at the low voltage side and + * a negative current at the high voltage side. The system boundary is the DC/DC sub-circuit, + * which sources current from the high side port and sinks it through the low side port. + */ +class PowerPort +{ +public: + float voltage; ///< Measured port voltage + + float sink_voltage_max; ///< Maximum voltage if connected device acts as a sink + ///< (equivalent to battery charging target voltage) + float sink_voltage_min; ///< Minimum voltage to allow positive current (necessary + ///< to prevent charging of deep-discharged Li-ion batteries) + + float src_voltage_start; ///< Minimum voltage to allow source current from the device + ///< (equal to load reconnect voltage) + float src_voltage_stop; ///< Absolute minimum = load disconnect for batteries + + float current; ///< Measured current through this port (positive sign = + ///< sinking current into the named external device) + float power; ///< Product of current & voltage + + float pos_current_limit; ///< Maximum positive current (valid values >= 0.0) + float pos_droop_res; ///< control voltage = nominal voltage - droop_res * current + + float neg_current_limit; ///< Maximum negative current (valid values <= 0.0) + float neg_droop_res; ///< control voltage = nominal voltage - droop_res * current + + float pos_energy_Wh; ///< Cumulated sunk energy since last counter reset (Wh) + float neg_energy_Wh; ///< Cumulated sourced energy 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(); + + /** Initialize power port for nanogrid connection + */ + void init_nanogrid(); + + /** Initialize power port for load output connection + */ + void init_load(float max_load_voltage); + + /** Energy balance calculation for power port + * + * Must be called exactly once per second, otherwise energy calculation gets wrong. + */ + void energy_balance(); + + /** Passes own voltage target settings to another port + * + * @param port Port where the voltage settings will be adjusted + */ + void pass_voltage_targets(PowerPort *port); +}; + +/** Sets current limits of DC/DC and load according to battery status + */ +void ports_update_current_limits(PowerPort *p_dcdc, PowerPort *p_bat, PowerPort *p_load); + +#endif /* POWER_PORT_H */ diff --git a/src/pwm_switch.cpp b/src/pwm_switch.cpp index b9288e0..8611e87 100644 --- a/src/pwm_switch.cpp +++ b/src/pwm_switch.cpp @@ -14,22 +14,21 @@ * limitations under the License. */ -#ifndef UNIT_TEST - #include "pwm_switch.h" #include "config.h" #include "pcb.h" +#include #include // for time(NULL) function -// timer used for PWM generation has to be globally selected for the used PCB -#if (PWM_TIM == 3) && defined(CHARGER_TYPE_PWM) +#if FEATURE_PWM_SWITCH static int _pwm_resolution; +static bool _pwm_active; -static bool _enabled; +#ifndef UNIT_TEST -void pwm_switch_init_registers(int freq_Hz) +void pwm_signal_init_registers(int freq_Hz) { // Enable peripheral clock of GPIOB RCC->IOPENR |= RCC_IOPENR_IOPBEN; @@ -70,31 +69,26 @@ void pwm_switch_init_registers(int freq_Hz) TIM3->ARR = _pwm_resolution - 1; } -void pwm_switch_set_duty_cycle(float duty) +void pwm_signal_set_duty_cycle(float duty) { TIM3->CCR4 = _pwm_resolution * duty; } -void pwm_switch_duty_cycle_step(int delta) +void pwm_signal_duty_cycle_step(int delta) { if ((int)TIM3->CCR4 + delta <= _pwm_resolution && (int)TIM3->CCR4 + delta >= 0) { TIM3->CCR4 += delta; } } -bool pwm_switch_enabled() -{ - return _enabled; -} - -float pwm_switch_get_duty_cycle() +float pwm_signal_get_duty_cycle() { return (float)(TIM3->CCR4) / _pwm_resolution; } -void pwm_switch_start(float pwm_duty) +void pwm_signal_start(float pwm_duty) { - pwm_switch_set_duty_cycle(pwm_duty); + pwm_signal_set_duty_cycle(pwm_duty); // Capture/Compare Enable Register // CCxE = 1: Enable the output on OCx @@ -103,86 +97,109 @@ void pwm_switch_start(float pwm_duty) // CCxNP = 0: Active high polarity on OC1N (default) TIM3->CCER |= TIM_CCER_CC4E; - _enabled = true; + _pwm_active = true; } -void pwm_switch_stop() +void pwm_signal_stop() { TIM3->CCER &= ~(TIM_CCER_CC4E); TIM3->CCR4 = 0; - _enabled = false; + _pwm_active = false; } -void pwm_switch_init(PwmSwitch *pwm_switch) +#else + +// dummy functions for unit tests +float pwm_signal_get_duty_cycle() { return 0; } +void pwm_signal_set_duty_cycle(float duty) {;} +void pwm_signal_duty_cycle_step(int delta) {;} +void pwm_signal_init_registers(int freq_Hz) {;} +void pwm_signal_start(float pwm_duty) {;} +void pwm_signal_stop() {;} + +#endif + +bool PwmSwitch::active() { - pwm_switch->off_timestamp = -10000; // start immediately + return _pwm_active; +} + +PwmSwitch::PwmSwitch(PowerPort *pwm_terminal, PowerPort *pwm_port_int) +{ + terminal = pwm_terminal; + port_int = pwm_port_int; + + off_timestamp = -10000; // start immediately // calibration parameters - pwm_switch->offset_voltage_start = 2.0; // V charging switched on if Vsolar > Vbat + offset - pwm_switch->restart_interval = 60; // s When should we retry to start charging after low solar power cut-off? + offset_voltage_start = 2.0; // V charging switched on if Vsolar > Vbat + offset + restart_interval = 60; // s When should we retry to start charging after low solar power cut-off? - pwm_switch_init_registers(PWM_FREQUENCY); + pwm_signal_init_registers(PWM_FREQUENCY); - pwm_switch->enabled = true; // enable charging in general - _enabled = false; // still disable actual switch + enabled = true; // enable charging in general + _pwm_active = false; // still disable actual switch } -void pwm_switch_control(PwmSwitch *pwm_switch, DcBus *solar_bus, DcBus *bat_bus) +void PwmSwitch::control() { - 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 - || pwm_switch->enabled == false) + if (_pwm_active) { + if (port_int->pos_current_limit == 0 || terminal->neg_current_limit == 0 + || terminal->current > 0 // discharging battery into solar panel --> stop + || enabled == false) { - pwm_switch_stop(); - pwm_switch->off_timestamp = time(NULL); + pwm_signal_stop(); + 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 (port_int->voltage > (port_int->sink_voltage_max - port_int->pos_droop_res * port_int->current) // output voltage above target + || port_int->current > port_int->pos_current_limit // output current limit exceeded + || terminal->current < terminal->neg_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 - if (pwm_switch_get_duty_cycle() > 0.95) { + if (pwm_signal_get_duty_cycle() > 0.95) { // prevent very short off periods - pwm_switch_set_duty_cycle(0.95); + pwm_signal_set_duty_cycle(0.95); } - else if (pwm_switch_get_duty_cycle() < 0.05) { + else if (pwm_signal_get_duty_cycle() < 0.05) { // prevent very short on periods and switch completely off instead - pwm_switch_stop(); - pwm_switch->off_timestamp = time(NULL); + pwm_signal_stop(); + off_timestamp = time(NULL); printf("PWM charger stop, no further derating possible.\n"); } else { - pwm_switch_duty_cycle_step(-1); + pwm_signal_duty_cycle_step(-1); } } else { - if (pwm_switch_get_duty_cycle() > 0.95) { + if (pwm_signal_get_duty_cycle() > 0.95) { // prevent very short off periods and switch completely on instead - pwm_switch_set_duty_cycle(1); + pwm_signal_set_duty_cycle(1); } else { - pwm_switch_duty_cycle_step(1); + pwm_signal_duty_cycle_step(1); } } } 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 - && time(NULL) > (pwm_switch->off_timestamp + pwm_switch->restart_interval) - && pwm_switch->enabled == true) + if (port_int->pos_current_limit > 0 // charging allowed + && port_int->voltage < port_int->sink_voltage_max + && port_int->voltage > port_int->sink_voltage_min + && terminal->neg_current_limit < 0 // discharging allowed + && terminal->voltage > port_int->voltage + offset_voltage_start + && time(NULL) > (off_timestamp + restart_interval) + && enabled == true) { - pwm_switch_start(1); + pwm_signal_start(1); printf("PWM charger start.\n"); } } } -#endif /* CHARGER_TYPE_PWM */ +float PwmSwitch::get_duty_cycle() +{ + return pwm_signal_get_duty_cycle(); +} -#endif /* UNIT_TEST */ +#endif /* FEATURE_PWM_SWITCH */ diff --git a/src/pwm_switch.h b/src/pwm_switch.h index 95f26ec..082c654 100644 --- a/src/pwm_switch.h +++ b/src/pwm_switch.h @@ -23,41 +23,43 @@ */ #include -#include "dc_bus.h" + +#include "power_port.h" /** PWM charger type * * Contains all data belonging to the PWM switching sub-component. */ -typedef struct { +class PwmSwitch +{ +public: + /** Initialization of PWM switch struct + */ + PwmSwitch(PowerPort *pwm_terminal, PowerPort *pwm_port_int); + + /** Main control function for the PWM switching algorithm + */ + void control(); + + /** Read the on/off status of the PWM switch + * + * @returns true if on + */ + bool active(); + + /** Read the currently set duty cycle + * + * @returns Duty cycle between 0.0 and 1.0 + */ + float get_duty_cycle(); + + PowerPort *terminal; ///< Pointer to external power port (terminal) + PowerPort *port_int; ///< Pointer to internal power port (junction with load and battery) + bool enabled; ///< Can be used to disable the PWM power stage float offset_voltage_start; ///< Offset voltage of solar panel vs. battery to start charging (V) int restart_interval; ///< Interval to wait before retrying charging after low solar power cut-off (s) int off_timestamp; ///< Time when charger was switched off last time -} PwmSwitch; - -/** Initialization of PWM switch struct - */ -void pwm_switch_init(PwmSwitch *pwm_switch); - -/** Main control function for the PWM switching algorithm - * - * @param pwm_switch PWM switch struct - * @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); - -/** Read the on/off status of the PWM switch - * - * @returns true if enabled - */ -bool pwm_switch_enabled(); - -/** Read the currently set duty cycle - * - * @returns Duty cycle between 0.0 and 1.0 - */ -float pwm_switch_get_duty_cycle(); +}; #endif /* PWM_SWITCH_H */ diff --git a/src/uext/uext_oled.cpp b/src/uext/uext_oled.cpp index 4159e42..47d3f7e 100644 --- a/src/uext/uext_oled.cpp +++ b/src/uext/uext_oled.cpp @@ -17,8 +17,6 @@ #ifndef UNIT_TEST #include "main.h" -extern DcBus *bat_terminal; -extern DcBus *solar_terminal; #ifdef OLED_ENABLED // otherwise don't compile code to reduce firmware size @@ -61,8 +59,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 +92,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.voltage > bat_terminal.voltage) #endif { oled.setTextCursor(0, 26); - oled.printf("%4.1fV", solar_terminal->voltage); + oled.printf("%4.1fV", solar_terminal.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.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.voltage); // load data oled.setTextCursor(90, 18); @@ -125,7 +120,7 @@ void uext_process_1s() oled.printf("%5.1fA\n", (abs(load_terminal.current) < 0.1) ? 0 : load_terminal.current); oled.setTextCursor(0, 36); - oled.printf("Day +%5.0fWh -%5.0fWh", solar_terminal->dis_energy_Wh, fabs(load_terminal.chg_energy_Wh)); + oled.printf("Day +%5.0fWh -%5.0fWh", solar_terminal.neg_energy_Wh, fabs(load_terminal.pos_energy_Wh)); oled.printf("Tot +%4.1fkWh -%4.1fkWh", log_data.solar_in_total_Wh / 1000.0, fabs(log_data.load_out_total_Wh) / 1000.0); oled.setTextCursor(0, 56); diff --git a/test/main.cpp b/test/main.cpp index 9aab325..cf89747 100644 --- a/test/main.cpp +++ b/test/main.cpp @@ -17,33 +17,49 @@ #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 -PwmSwitch pwm_switch = {}; // only necessary for PWM charger +PowerPort lv_terminal; // low voltage terminal (battery for typical MPPT) + +#if FEATURE_DCDC_CONVERTER +PowerPort hv_terminal; // high voltage terminal (solar for typical MPPT) +PowerPort dcdc_lv_port; // internal low voltage side of DC/DC converter +Dcdc dcdc(&hv_terminal, &dcdc_lv_port, DCDC_MODE_INIT); +#endif + +#if FEATURE_PWM_SWITCH +PowerPort pwm_terminal; // external terminal of PWM switch port (normally solar) +PowerPort pwm_port_int; // internal side of PWM switch +PwmSwitch pwm_switch(&pwm_terminal, &pwm_port_int); +PowerPort &solar_terminal = pwm_terminal; +#else +PowerPort &solar_terminal = SOLAR_TERMINAL; // defined in config.h +#endif + +#if FEATURE_LOAD_OUTPUT +PowerPort load_terminal; // load terminal (also connected to lv_bus) +LoadOutput load(&load_terminal); +#endif + +PowerPort &bat_terminal = BATTERY_TERMINAL; // defined in config.h +#ifdef GRID_TERMINAL +PowerPort &grid_terminal = GRID_TERMINAL; +#endif + +Charger charger(&lv_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) 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(); - dc_bus_tests(); + power_port_tests(); half_brigde_tests(); dcdc_tests(); log_tests(); diff --git a/test/tests.h b/test/tests.h index a52af5a..b4d64b7 100644 --- a/test/tests.h +++ b/test/tests.h @@ -5,7 +5,7 @@ void bat_charger_tests(); void adc_tests(); -void dc_bus_tests(); +void power_port_tests(); void half_brigde_tests(); diff --git a/test/tests_adc.cpp b/test/tests_adc.cpp index 3d054f5..ea419ac 100644 --- a/test/tests_adc.cpp +++ b/test/tests_adc.cpp @@ -3,26 +3,22 @@ #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.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.dcdc_current - adcval.load_current, + round(lv_terminal.current * 10) / 10); } void check_load_terminal_readings() @@ -33,8 +29,8 @@ void check_load_terminal_readings() 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_lv_port.voltage * 10) / 10); + TEST_ASSERT_EQUAL_FLOAT(adcval.dcdc_current, round(dcdc_lv_port.current * 10) / 10); } void check_temperature_readings() diff --git a/test/tests_bat_charger.cpp b/test/tests_bat_charger.cpp index 9a57620..cc0b092 100644 --- a/test/tests_bat_charger.cpp +++ b/test/tests_bat_charger.cpp @@ -1,32 +1,27 @@ #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" 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(&bat_terminal, &bat_conf, 1); charger.state = CHG_STATE_IDLE; charger.bat_temperature = 25; - bat_terminal->voltage = 14.0; - bat_terminal->current = 0; + bat_terminal.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.voltage = bat_conf.voltage_recharge + 0.1; + charger.charge_control(&bat_conf); TEST_ASSERT_EQUAL(CHG_STATE_IDLE, charger.state); } @@ -34,8 +29,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.voltage = bat_conf.voltage_recharge - 0.1; + charger.charge_control(&bat_conf); TEST_ASSERT_EQUAL(CHG_STATE_IDLE, charger.state); } @@ -43,12 +38,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.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 +51,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.voltage = bat_conf.voltage_recharge - 0.1; + charger.charge_control(&bat_conf); TEST_ASSERT_EQUAL(CHG_STATE_BULK, charger.state); } @@ -65,11 +60,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.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.voltage = bat_conf.topping_voltage + 0.1; + charger.charge_control(&bat_conf); TEST_ASSERT_EQUAL(CHG_STATE_TOPPING, charger.state); } @@ -78,14 +73,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->current = bat_conf.topping_current_cutoff + 0.1; + bat_terminal.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 +89,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->current = bat_conf.topping_current_cutoff - 0.1; - battery_charge_control(bat_terminal, &bat_conf, &charger); + bat_terminal.voltage = bat_conf.topping_voltage + 0.1; + bat_terminal.current = bat_conf.topping_current_cutoff - 0.1; + charger.charge_control(&bat_conf); TEST_ASSERT_EQUAL(CHG_STATE_TRICKLE, charger.state); } @@ -106,9 +101,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->current = bat_conf.topping_current_cutoff - 0.1; - battery_charge_control(bat_terminal, &bat_conf, &charger); + bat_terminal.voltage = bat_conf.topping_voltage + 0.1; + bat_terminal.current = bat_conf.topping_current_cutoff - 0.1; + charger.charge_control(&bat_conf); TEST_ASSERT_EQUAL(CHG_STATE_IDLE, charger.state); } @@ -118,14 +113,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->current = bat_conf.topping_current_cutoff - 0.1; + bat_terminal.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 +136,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->current = bat_conf.topping_current_cutoff - 0.1; - battery_charge_control(bat_terminal, &bat_conf, &charger); + bat_terminal.voltage = bat_conf.topping_voltage + 0.1; + bat_terminal.current = bat_conf.topping_current_cutoff - 0.1; + charger.charge_control(&bat_conf); TEST_ASSERT_NOT_EQUAL(CHG_STATE_EQUALIZATION, charger.state); } @@ -159,9 +154,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->current = bat_conf.topping_current_cutoff - 0.1; - battery_charge_control(bat_terminal, &bat_conf, &charger); + bat_terminal.voltage = bat_conf.topping_voltage + 0.1; + bat_terminal.current = bat_conf.topping_current_cutoff - 0.1; + charger.charge_control(&bat_conf); TEST_ASSERT_NOT_EQUAL(CHG_STATE_EQUALIZATION, charger.state); } @@ -173,9 +168,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->current = bat_conf.topping_current_cutoff - 0.1; - battery_charge_control(bat_terminal, &bat_conf, &charger); + bat_terminal.voltage = bat_conf.topping_voltage + 0.1; + bat_terminal.current = bat_conf.topping_current_cutoff - 0.1; + charger.charge_control(&bat_conf); TEST_ASSERT_EQUAL(CHG_STATE_EQUALIZATION, charger.state); } @@ -188,9 +183,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->current = bat_conf.topping_current_cutoff - 0.1; - battery_charge_control(bat_terminal, &bat_conf, &charger); + bat_terminal.voltage = bat_conf.topping_voltage + 0.1; + bat_terminal.current = bat_conf.topping_current_cutoff - 0.1; + charger.charge_control(&bat_conf); TEST_ASSERT_EQUAL(CHG_STATE_EQUALIZATION, charger.state); } @@ -200,13 +195,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.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,33 +214,33 @@ void stop_discharge_at_low_voltage() { init_structs(); - bat_terminal->voltage = 14; - battery_discharge_control(bat_terminal, &bat_conf, &charger); - TEST_ASSERT_LESS_THAN(0, bat_terminal->dis_current_limit); + bat_terminal.voltage = 14; + charger.discharge_control(&bat_conf); + TEST_ASSERT_LESS_THAN(0, bat_terminal.neg_current_limit); - bat_terminal->voltage = bat_conf.voltage_load_disconnect - 0.1; - battery_discharge_control(bat_terminal, &bat_conf, &charger); - TEST_ASSERT_EQUAL(0, bat_terminal->dis_current_limit); + bat_terminal.voltage = bat_conf.voltage_load_disconnect - 0.1; + charger.discharge_control(&bat_conf); + TEST_ASSERT_EQUAL(0, bat_terminal.neg_current_limit); } void stop_discharge_at_overtemp() { init_structs(); - TEST_ASSERT_LESS_THAN(0, bat_terminal->dis_current_limit); + TEST_ASSERT_LESS_THAN(0, bat_terminal.neg_current_limit); charger.bat_temperature = bat_conf.discharge_temp_max + 1; - battery_discharge_control(bat_terminal, &bat_conf, &charger); - TEST_ASSERT_EQUAL(0, bat_terminal->dis_current_limit); + charger.discharge_control(&bat_conf); + TEST_ASSERT_EQUAL(0, bat_terminal.neg_current_limit); } void stop_discharge_at_undertemp() { init_structs(); - TEST_ASSERT_LESS_THAN(0, bat_terminal->dis_current_limit); + TEST_ASSERT_LESS_THAN(0, bat_terminal.neg_current_limit); charger.bat_temperature = bat_conf.discharge_temp_min - 1; - battery_discharge_control(bat_terminal, &bat_conf, &charger); - TEST_ASSERT_EQUAL(0, bat_terminal->dis_current_limit); + charger.discharge_control(&bat_conf); + TEST_ASSERT_EQUAL(0, bat_terminal.neg_current_limit); } void restart_discharge_if_allowed() @@ -253,19 +248,19 @@ 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); - TEST_ASSERT_EQUAL(0, bat_terminal->dis_current_limit); + bat_terminal.voltage = bat_conf.voltage_load_disconnect - 0.1; + charger.discharge_control(&bat_conf); + TEST_ASSERT_EQUAL(0, bat_terminal.neg_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); - TEST_ASSERT_EQUAL(0, bat_terminal->dis_current_limit); + bat_terminal.voltage = bat_conf.voltage_load_disconnect + 0.1; + charger.discharge_control(&bat_conf); + TEST_ASSERT_EQUAL(0, bat_terminal.neg_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); - TEST_ASSERT_LESS_THAN(0, bat_terminal->dis_current_limit); + bat_terminal.voltage = bat_conf.voltage_load_reconnect + 0.1; + charger.discharge_control(&bat_conf); + TEST_ASSERT_LESS_THAN(0, bat_terminal.neg_current_limit); } void battery_values_propagated_to_lv_bus_int() diff --git a/test/tests_dc_bus.cpp b/test/tests_dc_bus.cpp deleted file mode 100644 index 8211833..0000000 --- a/test/tests_dc_bus.cpp +++ /dev/null @@ -1,120 +0,0 @@ - -#include "tests.h" -#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; - -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); - charger.state = CHG_STATE_IDLE; -} - -static adc_values_t adcval; - -const float dcdc_current_sun = 3; -const float load_current = 1; -const int sun_hours = 1; -const int night_hours = 3; - -void energy_calculation_init() -{ - log_data.solar_in_total_Wh = 0; - log_data.load_out_total_Wh = 0; - log_data.bat_chg_total_Wh = 0; - log_data.bat_dis_total_Wh = 0; - hv_terminal.dis_energy_Wh = 0.0; - load_terminal.chg_energy_Wh = 0.0; - lv_terminal.chg_energy_Wh = 0.0; - lv_terminal.dis_energy_Wh = 0.0; - - // set desired measurement values - adcval.bat_temperature = 25; - adcval.battery_voltage = 12; - adcval.dcdc_current = dcdc_current_sun; - adcval.internal_temperature = 25; - adcval.load_current = load_current; - adcval.mcu_temperature = 25; - adcval.solar_voltage = 30; - - // insert values into ADC functions - prepare_adc_readings(adcval); - 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); - } - - // disable DC/DC = solar charging - adcval.dcdc_current = 0; - prepare_adc_readings(adcval); - 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); - } -} - -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)); -} - -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)); -} - -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)); -} - -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)); -} - -void dc_bus_tests() -{ - energy_calculation_init(); - - UNITY_BEGIN(); - - // energy calculation - RUN_TEST(charging_energy_calculation_valid); - RUN_TEST(discharging_energy_calculation_valid); - RUN_TEST(solar_input_energy_calculation_valid); - RUN_TEST(load_output_energy_calculation_valid); - - UNITY_END(); -} \ No newline at end of file diff --git a/test/tests_dcdc.cpp b/test/tests_dcdc.cpp index 0c87d02..38bf212 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.init_solar(); hv_terminal.voltage = 20; - hv_terminal.dis_voltage_start = 18; + hv_terminal.src_voltage_start = 18; hv_terminal.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(&dcdc_lv_port, &bat_conf, 1); + dcdc_lv_port.voltage = 14; + dcdc_lv_port.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,11 +43,11 @@ 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; + hv_terminal.init_solar(); + dcdc_lv_port.voltage = 20; + dcdc_lv_port.src_voltage_start = 18; + dcdc_lv_port.current = 0; + dcdc_lv_port.power = 0; battery_conf_init(&bat_conf, BAT_TYPE_NMC, 10, 9); battery_init_dc_bus(&hv_terminal, &bat_conf, 1); @@ -60,56 +55,57 @@ static void init_structs_boost() hv_terminal.current = 0; hv_terminal.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_lv_port.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_lv_port.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_lv_port.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_lv_port.pos_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)); + 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)); + TEST_ASSERT_EQUAL(0, dcdc.check_start_conditions()); } // boost @@ -155,35 +151,35 @@ 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)); + 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)); + 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)); + hv_terminal.pos_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_lv_port.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_lv_port.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_lv_port.voltage = dcdc_lv_port.sink_voltage_max + 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_lv_port.current = dcdc_lv_port.pos_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); + hv_terminal.voltage = hv_terminal.src_voltage_stop - 0.1; + dcdc_lv_port.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); + hv_terminal.current = hv_terminal.neg_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_lv_port.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_lv_port.power = 5; + dcdc.control(); float pwm1 = half_bridge_get_duty_cycle(); - lv_bus_int.power = 7; - dcdc_control(&dcdc); + dcdc_lv_port.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_lv_port.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); + hv_terminal.voltage = hv_terminal.sink_voltage_max + 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); + hv_terminal.current = hv_terminal.pos_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; + dcdc_lv_port.voltage = dcdc_lv_port.src_voltage_stop - 0.1; hv_terminal.current = 0.2; - dcdc_control(&dcdc); + 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_lv_port.current = dcdc_lv_port.neg_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,7 +351,7 @@ 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); } @@ -363,7 +359,7 @@ void boost_stop_high_voltage_emergency() { start_boost(); hv_terminal.voltage = dcdc.hs_voltage_max + 0.1; - dcdc_control(&dcdc); + dcdc.control(); TEST_ASSERT(half_bridge_enabled() == false); } @@ -372,15 +368,15 @@ void boost_correct_mppt_operation() start_boost(); hv_terminal.power = 5; - dcdc_control(&dcdc); + dcdc.control(); float pwm1 = half_bridge_get_duty_cycle(); hv_terminal.power = 7; - dcdc_control(&dcdc); + 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.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..a3ec438 100644 --- a/test/tests_log.cpp +++ b/test/tests_log.cpp @@ -1,37 +1,26 @@ #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" void reset_counters_at_start_of_day() { - solar_terminal->voltage = bat_terminal->voltage - 1; + solar_terminal.voltage = bat_terminal.voltage - 1; log_data.day_counter = 0; - solar_terminal->dis_energy_Wh = 10.0; - bat_terminal->dis_energy_Wh = 3.0; - bat_terminal->chg_energy_Wh = 4.0; - load_terminal.chg_energy_Wh = 9.0; + solar_terminal.neg_energy_Wh = 10.0; + bat_terminal.neg_energy_Wh = 3.0; + bat_terminal.pos_energy_Wh = 4.0; + load_terminal.pos_energy_Wh = 9.0; // 5 houurs without sun for (int i = 0; i <= 5 * 60 * 60; i++) { - log_update_energy(&log_data, solar_terminal, bat_terminal, &load_terminal); + log_update_energy(&log_data); } TEST_ASSERT_EQUAL(10, log_data.solar_in_total_Wh); @@ -39,66 +28,66 @@ void reset_counters_at_start_of_day() TEST_ASSERT_EQUAL(4, log_data.bat_chg_total_Wh); TEST_ASSERT_EQUAL(9, log_data.load_out_total_Wh); - TEST_ASSERT_EQUAL(10, solar_terminal->dis_energy_Wh); - TEST_ASSERT_EQUAL(3, bat_terminal->dis_energy_Wh); - TEST_ASSERT_EQUAL(4, bat_terminal->chg_energy_Wh); - TEST_ASSERT_EQUAL(9, load_terminal.chg_energy_Wh); + TEST_ASSERT_EQUAL(10, solar_terminal.neg_energy_Wh); + TEST_ASSERT_EQUAL(3, bat_terminal.neg_energy_Wh); + TEST_ASSERT_EQUAL(4, bat_terminal.pos_energy_Wh); + TEST_ASSERT_EQUAL(9, load_terminal.pos_energy_Wh); // solar didn't come back yet TEST_ASSERT_EQUAL(0, log_data.day_counter); // now solar power comes back - solar_terminal->voltage = bat_terminal->voltage + 1; - log_update_energy(&log_data, solar_terminal, bat_terminal, &load_terminal); + solar_terminal.voltage = bat_terminal.voltage + 1; + log_update_energy(&log_data); // day counter should be increased and daily energy counters reset TEST_ASSERT_EQUAL(1, log_data.day_counter); - TEST_ASSERT_EQUAL(0, solar_terminal->dis_energy_Wh); - TEST_ASSERT_EQUAL(0, bat_terminal->dis_energy_Wh); - TEST_ASSERT_EQUAL(0, bat_terminal->chg_energy_Wh); - TEST_ASSERT_EQUAL(0, load_terminal.chg_energy_Wh); + TEST_ASSERT_EQUAL(0, solar_terminal.neg_energy_Wh); + TEST_ASSERT_EQUAL(0, bat_terminal.neg_energy_Wh); + TEST_ASSERT_EQUAL(0, bat_terminal.pos_energy_Wh); + TEST_ASSERT_EQUAL(0, load_terminal.pos_energy_Wh); } void log_new_solar_voltage_max() { - solar_terminal->voltage = 40; - log_update_min_max_values(&log_data, &dcdc, &charger, &load, solar_terminal, bat_terminal, &load_terminal); + solar_terminal.voltage = 40; + log_update_min_max_values(&log_data); TEST_ASSERT_EQUAL(40, log_data.solar_voltage_max); } void log_new_bat_voltage_max() { - bat_terminal->voltage = 31; - log_update_min_max_values(&log_data, &dcdc, &charger, &load, solar_terminal, bat_terminal, &load_terminal); + bat_terminal.voltage = 31; + log_update_min_max_values(&log_data); TEST_ASSERT_EQUAL(31, log_data.battery_voltage_max); } void log_new_dcdc_current_max() { - dcdc.lv_bus->current = 21; - log_update_min_max_values(&log_data, &dcdc, &charger, &load, solar_terminal, bat_terminal, &load_terminal); + dcdc.lvs->current = 21; + log_update_min_max_values(&log_data); TEST_ASSERT_EQUAL(21, log_data.dcdc_current_max); } void log_new_load_current_max() { - load.terminal->current = 21; - log_update_min_max_values(&log_data, &dcdc, &charger, &load, solar_terminal, bat_terminal, &load_terminal); + load.port->current = 21; + log_update_min_max_values(&log_data); TEST_ASSERT_EQUAL(21, log_data.load_current_max); } void log_solar_power_max() { - solar_terminal->power = -50; - log_update_min_max_values(&log_data, &dcdc, &charger, &load, solar_terminal, bat_terminal, &load_terminal); + solar_terminal.power = -50; + log_update_min_max_values(&log_data); TEST_ASSERT_EQUAL(50, log_data.solar_power_max_day); TEST_ASSERT_EQUAL(50, log_data.solar_power_max_total); } void log_load_power_max() { - load.terminal->power = 50; - log_update_min_max_values(&log_data, &dcdc, &charger, &load, solar_terminal, bat_terminal, &load_terminal); + load.port->power = 50; + log_update_min_max_values(&log_data); TEST_ASSERT_EQUAL(50, log_data.load_power_max_day); TEST_ASSERT_EQUAL(50, log_data.load_power_max_total); } @@ -106,14 +95,14 @@ void log_load_power_max() void log_new_mosfet_temp_max() { dcdc.temp_mosfets = 80; - log_update_min_max_values(&log_data, &dcdc, &charger, &load, solar_terminal, bat_terminal, &load_terminal); + log_update_min_max_values(&log_data); TEST_ASSERT_EQUAL(80, log_data.mosfet_temp_max); } void log_new_bat_temp_max() { charger.bat_temperature = 45; - log_update_min_max_values(&log_data, &dcdc, &charger, &load, solar_terminal, bat_terminal, &load_terminal); + log_update_min_max_values(&log_data); TEST_ASSERT_EQUAL(45, log_data.bat_temp_max); } @@ -122,7 +111,7 @@ extern float mcu_temp; void log_new_int_temp_max() { mcu_temp = 22; - log_update_min_max_values(&log_data, &dcdc, &charger, &load, solar_terminal, bat_terminal, &load_terminal); + log_update_min_max_values(&log_data); TEST_ASSERT_EQUAL(22, log_data.int_temp_max); } diff --git a/test/tests_power_port.cpp b/test/tests_power_port.cpp new file mode 100644 index 0000000..9e55c01 --- /dev/null +++ b/test/tests_power_port.cpp @@ -0,0 +1,138 @@ + +#include "tests.h" +#include "adc_dma_stub.h" +#include "adc_dma.h" + +#include +#include +#include + +#include "main.h" + +static void init_structs() +{ + battery_conf_init(&bat_conf, BAT_TYPE_FLOODED, 6, 100); + battery_init_dc_bus(&bat_terminal, &bat_conf, 1); + charger.state = CHG_STATE_IDLE; +} + +static adc_values_t adcval; + +const float dcdc_current_sun = 3; +const float load_current = 1; +const int sun_hours = 1; +const int night_hours = 3; + +void energy_calculation_init() +{ + log_data.solar_in_total_Wh = 0; + log_data.load_out_total_Wh = 0; + log_data.bat_chg_total_Wh = 0; + log_data.bat_dis_total_Wh = 0; + hv_terminal.neg_energy_Wh = 0.0; + load_terminal.pos_energy_Wh = 0.0; + lv_terminal.pos_energy_Wh = 0.0; + lv_terminal.neg_energy_Wh = 0.0; + + // set desired measurement values + adcval.bat_temperature = 25; + adcval.battery_voltage = 12; + adcval.dcdc_current = dcdc_current_sun; + adcval.internal_temperature = 25; + adcval.load_current = load_current; + adcval.mcu_temperature = 25; + adcval.solar_voltage = 30; + + // insert values into ADC functions + prepare_adc_readings(adcval); + update_measurements(); + + for (int i = 0; i < 60*60*sun_hours; i++) { + hv_terminal.energy_balance(); + lv_terminal.energy_balance(); + load_terminal.energy_balance(); + } + + // disable DC/DC = solar charging + adcval.dcdc_current = 0; + prepare_adc_readings(adcval); + update_measurements(); + + for (int i = 0; i < 60*60*night_hours; i++) { + hv_terminal.energy_balance(); + lv_terminal.energy_balance(); + load_terminal.energy_balance(); + } +} + +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.pos_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.neg_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.neg_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.pos_energy_Wh)); +} + +void pass_voltage_targets_to_adjacent_bus() +{ + // without any droop + bat_terminal.current = 0; + bat_terminal.pass_voltage_targets(&dcdc_lv_port); + TEST_ASSERT_EQUAL(bat_terminal.sink_voltage_min, dcdc_lv_port.sink_voltage_min); + TEST_ASSERT_EQUAL(bat_terminal.sink_voltage_max, dcdc_lv_port.sink_voltage_max); + TEST_ASSERT_EQUAL(bat_terminal.src_voltage_start, dcdc_lv_port.src_voltage_start); + TEST_ASSERT_EQUAL(bat_terminal.src_voltage_stop, dcdc_lv_port.src_voltage_stop); + + // now with droop for positive current + bat_terminal.current = 11; + bat_terminal.pos_droop_res = 0.1; + bat_terminal.pass_voltage_targets(&dcdc_lv_port); + TEST_ASSERT_EQUAL(bat_terminal.sink_voltage_min, dcdc_lv_port.sink_voltage_min); + TEST_ASSERT_EQUAL(bat_terminal.sink_voltage_max + 11*0.1, dcdc_lv_port.sink_voltage_max); + TEST_ASSERT_EQUAL(bat_terminal.src_voltage_start + 11*0.1, dcdc_lv_port.src_voltage_start); + TEST_ASSERT_EQUAL(bat_terminal.src_voltage_stop + 11*0.1, dcdc_lv_port.src_voltage_stop); + + // now with droop for negative current + bat_terminal.current = -11; + bat_terminal.pos_droop_res = 0.1; + bat_terminal.pass_voltage_targets(&dcdc_lv_port); + TEST_ASSERT_EQUAL(bat_terminal.sink_voltage_min, dcdc_lv_port.sink_voltage_min); + TEST_ASSERT_EQUAL(bat_terminal.sink_voltage_max - 11*0.1, dcdc_lv_port.sink_voltage_max); + TEST_ASSERT_EQUAL(bat_terminal.src_voltage_start - 11*0.1, dcdc_lv_port.src_voltage_start); + TEST_ASSERT_EQUAL(bat_terminal.src_voltage_stop - 11*0.1, dcdc_lv_port.src_voltage_stop); +} + +void power_port_tests() +{ + energy_calculation_init(); + + UNITY_BEGIN(); + + // energy calculation + RUN_TEST(charging_energy_calculation_valid); + RUN_TEST(discharging_energy_calculation_valid); + RUN_TEST(solar_input_energy_calculation_valid); + RUN_TEST(load_output_energy_calculation_valid); + + RUN_TEST(pass_voltage_targets_to_adjacent_bus); + + UNITY_END(); +} \ No newline at end of file