diff --git a/src/data_objects.cpp b/src/data_objects.cpp index 8cf1476..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,8 +105,8 @@ 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"}, + {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"}, #endif diff --git a/src/load.cpp b/src/load.cpp index 0cfdbb7..1fff592 100644 --- a/src/load.cpp +++ b/src/load.cpp @@ -20,7 +20,6 @@ #include "hardware.h" #include "leds.h" #include "log.h" -#include volatile bool short_circuit = false; @@ -193,8 +192,8 @@ LoadOutput::LoadOutput(PowerPort *pwr_port) { port = pwr_port; current_max = LOAD_CURRENT_MAX; - enabled_target = true; - usb_enabled_target = true; + enable = true; + usb_enable = true; switch_set(false); usb_set(false); switch_state = LOAD_STATE_DISABLED; @@ -209,7 +208,7 @@ void LoadOutput::usb_state_machine() { switch (usb_state) { case LOAD_STATE_DISABLED: - if (usb_enabled_target == true) { + if (usb_enable == true) { usb_set(true); usb_state = LOAD_STATE_ON; } @@ -224,7 +223,7 @@ void LoadOutput::usb_state_machine() usb_set(false); usb_state = LOAD_STATE_OFF_OVERCURRENT; } - else if (usb_enabled_target == false) { + else if (usb_enable == false) { usb_set(false); usb_state = LOAD_STATE_DISABLED; } @@ -232,7 +231,7 @@ void LoadOutput::usb_state_machine() case LOAD_STATE_OFF_LOW_SOC: // currently still same cut-off SOC limit as the load if (switch_state == LOAD_STATE_ON) { - if (usb_enabled_target == true) { + if (usb_enable == true) { usb_set(true); usb_state = LOAD_STATE_ON; } @@ -249,26 +248,22 @@ void LoadOutput::usb_state_machine() } } -static time_t lvd_timestamp = 0; // stores when the load was disconnected last time - void LoadOutput::state_machine() { //printf("Load State: %d\n", switch_state); switch (switch_state) { case LOAD_STATE_DISABLED: - if (port->neg_current_limit < 0 // discharging enabled - && enabled_target == true) - { + if (enable == true && port->pos_current_limit > 0) { switch_set(true); switch_state = LOAD_STATE_ON; } break; case LOAD_STATE_ON: - if (enabled_target == false) { + if (enable == false) { switch_set(false); switch_state = LOAD_STATE_DISABLED; } - else if (port->neg_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); switch_set(false); switch_state = LOAD_STATE_OFF_LOW_SOC; @@ -276,10 +271,10 @@ void LoadOutput::state_machine() break; case LOAD_STATE_OFF_LOW_SOC: // wait at least one hour - if (port->neg_current_limit < 0 // discharging enabled again + if (port->pos_current_limit > 0 && time(NULL) - lvd_timestamp > 60*60) { - if (enabled_target == true) { + if (enable == true) { switch_set(true); switch_state = LOAD_STATE_ON; } @@ -295,13 +290,15 @@ void LoadOutput::state_machine() } break; case LOAD_STATE_OFF_OVERVOLTAGE: - if (port->voltage < LOW_SIDE_VOLTAGE_MAX) { // TODO: add hysteresis? + 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 (enabled_target == false) { + if (enable == false) { short_circuit = false; switch_state = LOAD_STATE_DISABLED; // switch to normal mode again } @@ -312,7 +309,7 @@ void LoadOutput::state_machine() } // this function is called more often than the state machine -void LoadOutput::control(float load_max_voltage) +void LoadOutput::control() { if (short_circuit) { switch_state = LOAD_STATE_OFF_SHORT_CIRCUIT; @@ -334,7 +331,7 @@ void LoadOutput::control(float load_max_voltage) } static int debounce_counter = 0; - if (port->voltage > load_max_voltage || + if (port->voltage > port->sink_voltage_max || port->voltage > LOW_SIDE_VOLTAGE_MAX) { debounce_counter++; diff --git a/src/load.h b/src/load.h index f198784..cacd2aa 100644 --- a/src/load.h +++ b/src/load.h @@ -25,6 +25,7 @@ #include #include +#include #include "power_port.h" @@ -76,7 +77,7 @@ public: * * Performs time-critical checks like overcurrent and overvoltage */ - void control(float load_max_voltage); + void control(); uint16_t switch_state; ///< Current state of load output switch uint16_t usb_state; ///< Current state of USB output @@ -85,14 +86,15 @@ public: ///< 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 + bool usb_enable; ///< same for USB output }; #endif /* LOAD_H */ diff --git a/src/main.cpp b/src/main.cpp index 71a07a5..6e10738 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -119,6 +119,7 @@ int main() 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); @@ -194,7 +195,7 @@ void system_control() leds_set_charging(half_bridge_enabled()); #endif - load.control(bat_conf.voltage_absolute_max * charger.num_batteries); + load.control(); if (counter % CONTROL_FREQUENCY == 0) { // called once per second (this timer is much more accurate than time(NULL) based on LSI) diff --git a/src/power_port.cpp b/src/power_port.cpp index 6dc595f..b448c24 100644 --- a/src/power_port.cpp +++ b/src/power_port.cpp @@ -42,6 +42,14 @@ void PowerPort::init_nanogrid() 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() { @@ -71,4 +79,7 @@ void ports_update_current_limits(PowerPort *p_dcdc, PowerPort *p_bat, PowerPort // 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 index 0ead16e..1072257 100644 --- a/src/power_port.h +++ b/src/power_port.h @@ -92,6 +92,10 @@ public: */ 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.