diff --git a/src/adc_dma.cpp b/src/adc_dma.cpp index 6c2b88f..581c85a 100644 --- a/src/adc_dma.cpp +++ b/src/adc_dma.cpp @@ -27,6 +27,15 @@ #include "log.h" #include "pwm_switch.h" +extern Charger charger; +extern Dcdc dcdc; +extern LoadOutput load; +extern DcBus hv_terminal; +extern DcBus lv_bus_int; +extern DcBus lv_terminal; +extern DcBus load_terminal; + + // factory calibration values for internal voltage reference and temperature sensor // (see MCU datasheet, not RM) #if defined(STM32F0) @@ -84,7 +93,7 @@ void calibrate_current_sensors() } //---------------------------------------------------------------------------- -void update_measurements(Dcdc *dcdc, Charger *charger, DcBus *hs, DcBus *ls, DcBus *load_bus) +void update_measurements() { //int v_temp, rts; @@ -92,49 +101,49 @@ void update_measurements(Dcdc *dcdc, Charger *charger, DcBus *hs, DcBus *ls, DcB //int vcc = 2500 * 4096 / (adc_filtered[ADC_POS_V_REF] >> (4 + ADC_FILTER_CONST)); // internal STM reference voltage - int vcc = VREFINT_VALUE * VREFINT_CAL / + int vcc = VREFINT_VALUE * VREFINT_CAL / (adc_filtered[ADC_POS_VREF_MCU] >> (4 + ADC_FILTER_CONST)); // rely on LDO accuracy //int vcc = 3300; // calculate LS voltage first, as it is needed for HS voltage calculation for PWM charge controller - ls->voltage = + 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; - load_bus->voltage = ls->voltage; - dcdc->ls_voltage = ls->voltage; - - hs->voltage = + 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 - hs->voltage = ls->voltage + -(vcc * ADC_OFFSET_V_SOLAR / 1000.0 + hs->voltage); + hv_terminal.voltage = lv_terminal.voltage + -(vcc * ADC_OFFSET_V_SOLAR / 1000.0 + hv_terminal.voltage); #endif - load_bus->current = + 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 // current multiplied with PWM duty cycle for PWM charger to get avg current for correct power calculation - hs->current = - pwm_switch_get_duty_cycle() * ( + hv_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); - ls->current = -hs->current - load_bus->current; + lv_terminal.current = -hv_terminal.current - load_terminal.current; #else // MPPT - dcdc->ls_current = + lv_bus_int.current = (float)(((adc_filtered[ADC_POS_I_DCDC] >> (4 + ADC_FILTER_CONST)) * vcc) / 4096) * ADC_GAIN_I_DCDC / 1000.0 + solar_current_offset; - ls->current = dcdc->ls_current - load_bus->current; - hs->current = -dcdc->ls_current * ls->voltage / hs->voltage; + 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 // power calculations - hs->power = hs->voltage * hs->current; - ls->power = ls->voltage * ls->current; - load_bus->power = load_bus->voltage * load_bus->current; + hv_terminal.power = hv_terminal.voltage * hv_terminal.current; + lv_bus_int.power = lv_bus_int.voltage * lv_bus_int.current; + lv_terminal.power = lv_terminal.voltage * lv_terminal.current; + load_terminal.power = load_terminal.voltage * load_terminal.current; /** \todo Improved (faster) temperature calculation: https://www.embeddedrelated.com/showarticle/91.php @@ -155,13 +164,13 @@ void update_measurements(Dcdc *dcdc, Charger *charger, DcBus *hs, DcBus *ls, DcB bat_temp = 1.0/(1.0/(273.15+25) + 1.0/NTC_BETA_VALUE*log(rts/10000.0)) - 273.15; // °C #endif - detect_battery_temperature(charger, bat_temp); + detect_battery_temperature(&charger, bat_temp); #ifdef PIN_ADC_TEMP_FETS // MOSFET temperature calculation v_temp = ((adc_filtered[ADC_POS_TEMP_FETS] >> (4 + ADC_FILTER_CONST)) * vcc) / 4096; // voltage read by ADC (mV) rts = 10000 * v_temp / (vcc - v_temp); // resistance of NTC (Ohm) - dcdc->temp_mosfets = 1.0/(1.0/(273.15+25) + 1.0/NTC_BETA_VALUE*log(rts/10000.0)) - 273.15; // °C + dcdc.temp_mosfets = 1.0/(1.0/(273.15+25) + 1.0/NTC_BETA_VALUE*log(rts/10000.0)) - 273.15; // °C #endif // internal MCU temperature diff --git a/src/adc_dma.h b/src/adc_dma.h index fde23c9..0108253 100644 --- a/src/adc_dma.h +++ b/src/adc_dma.h @@ -40,7 +40,7 @@ void detect_battery_temperature(Charger *bat, float bat_temp); /** Updates structures with data read from ADC */ -void update_measurements(Dcdc *dcdc, Charger *charger, DcBus *hs, DcBus *ls, DcBus *load_bus); +void update_measurements(); /** Initializes registers and starts ADC timer */ diff --git a/src/data_objects.cpp b/src/data_objects.cpp index 47e52bf..db08114 100644 --- a/src/data_objects.cpp +++ b/src/data_objects.cpp @@ -37,9 +37,11 @@ extern BatConf bat_conf; extern BatConf bat_conf_user; extern Dcdc dcdc; extern LoadOutput load; -extern DcBus hv_bus; -extern DcBus lv_bus; -extern DcBus load_bus; +extern DcBus hv_terminal; +extern DcBus lv_terminal; +extern DcBus load_terminal; +//extern DcBus *bat_terminal; // TODO: pointers cannot be used here, as they are not +//extern DcBus *solar_terminal; // yet initialized... extern PwmSwitch pwm_switch; const char* const manufacturer = "Libre Solar"; @@ -131,10 +133,10 @@ const data_object_t data_objects[] = { {0x06, TS_OUTPUT, TS_READ_ALL, TS_T_UINT16, 0, (void*) &(charger.soc), "SOC_%"}, // output will be uint8_t // battery related data objects - {0x70, TS_OUTPUT, TS_READ_ALL, TS_T_FLOAT32, 2, (void*) &(lv_bus.voltage), "Bat_V"}, - {0x71, TS_OUTPUT, TS_READ_ALL, TS_T_FLOAT32, 2, (void*) &(hv_bus.voltage), "Solar_V"}, - {0x72, TS_OUTPUT, TS_READ_ALL, TS_T_FLOAT32, 2, (void*) &(lv_bus.current), "Bat_A"}, - {0x73, TS_OUTPUT, TS_READ_ALL, TS_T_FLOAT32, 2, (void*) &(load_bus.current), "Load_A"}, + {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"}, @@ -143,12 +145,12 @@ const data_object_t data_objects[] = { #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_bus.current), "Solar_A"}, - {0x7B, TS_OUTPUT, TS_READ_ALL, TS_T_FLOAT32, 2, (void*) &(lv_bus.chg_voltage_target), "BatTarget_V"}, - {0x7C, TS_OUTPUT, TS_READ_ALL, TS_T_FLOAT32, 2, (void*) &(lv_bus.chg_current_max), "BatTarget_A"}, - {0x7D, TS_OUTPUT, TS_READ_ALL, TS_T_FLOAT32, 2, (void*) &(lv_bus.power), "Bat_W"}, - {0x7E, TS_OUTPUT, TS_READ_ALL, TS_T_FLOAT32, 2, (void*) &(hv_bus.power), "Solar_W"}, - {0x7F, TS_OUTPUT, TS_READ_ALL, TS_T_FLOAT32, 2, (void*) &(load_bus.power), "Load_W"}, + {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_max), "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"}, // RECORDED DATA /////////////////////////////////////////////////////// // using IDs >= 0xA0 @@ -164,10 +166,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_bus.dis_energy_Wh), "SolarInDay_Wh"}, - {0xA1, TS_REC, TS_READ_ALL | TS_WRITE_MAKER, TS_T_FLOAT32, 2, (void*) &(load_bus.chg_energy_Wh), "LoadOutDay_Wh"}, - {0xA2, TS_REC, TS_READ_ALL | TS_WRITE_MAKER, TS_T_FLOAT32, 2, (void*) &(lv_bus.chg_energy_Wh), "BatChgDay_Wh"}, - {0xA3, TS_REC, TS_READ_ALL | TS_WRITE_MAKER, TS_T_FLOAT32, 2, (void*) &(lv_bus.dis_energy_Wh), "BatDisDay_Wh"}, + {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"}, {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"}, diff --git a/src/dcdc.cpp b/src/dcdc.cpp index 6151c69..c4477fa 100644 --- a/src/dcdc.cpp +++ b/src/dcdc.cpp @@ -30,8 +30,10 @@ extern LogData log_data; -void dcdc_init(Dcdc *dcdc) +void dcdc_init(Dcdc *dcdc, DcBus *hv, DcBus *lv) { + dcdc->hv_bus = hv; + dcdc->lv_bus = lv; dcdc->mode = DCDC_MODE_INIT; dcdc->enabled = true; dcdc->state = DCDC_STATE_OFF; @@ -53,7 +55,7 @@ void dcdc_init(Dcdc *dcdc) */ int _dcdc_output_control(Dcdc *dcdc, DcBus *out, DcBus *in, const int dcdc_dir) { - float dcdc_power_new = dcdc->ls_voltage * dcdc->ls_current * dcdc_dir; + float dcdc_power_new = dcdc->lv_bus->voltage * dcdc->lv_bus->current * dcdc_dir; static int pwm_delta = 1; static uint32_t low_dcdc_power_count = 0; @@ -97,7 +99,7 @@ int _dcdc_output_control(Dcdc *dcdc, DcBus *out, DcBus *in, const int dcdc_dir) dcdc->state = DCDC_STATE_CC; retval = -1; // decrease output power } - else if (fabs(dcdc->ls_current) > dcdc->ls_current_max // current above hardware maximum + else if (fabs(dcdc->lv_bus->current) > dcdc->ls_current_max // current above hardware maximum || dcdc->temp_mosfets > 80) // temperature limits exceeded { dcdc->state = DCDC_STATE_DERATING; @@ -120,31 +122,31 @@ int _dcdc_output_control(Dcdc *dcdc, DcBus *out, DcBus *in, const int dcdc_dir) return retval; } -int dcdc_check_start_conditions(Dcdc *dcdc, DcBus *hv, DcBus *lv) +int dcdc_check_start_conditions(Dcdc *dcdc) { if (dcdc->enabled == false || - hv->voltage > dcdc->hs_voltage_max || // also critical for buck mode because of ringing - lv->voltage > dcdc->ls_voltage_max || - lv->voltage < dcdc->ls_voltage_min || + 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)) { return 0; // no energy transfer allowed } - if (lv->chg_current_max > 0 && - lv->voltage < lv->chg_voltage_target && - lv->voltage > lv->chg_voltage_min && - hv->dis_current_max < 0 && - hv->voltage > hv->dis_voltage_start) + if (dcdc->lv_bus->chg_current_max > 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_max < 0 && + dcdc->hv_bus->voltage > dcdc->hv_bus->dis_voltage_start) { return 1; // start in buck mode allowed } - if (hv->chg_current_max > 0 && - hv->voltage < hv->chg_voltage_target && - hv->voltage > hv->chg_voltage_min && - lv->dis_current_max < 0 && - lv->voltage > lv->dis_voltage_start) + if (dcdc->hv_bus->chg_current_max > 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_max < 0 && + dcdc->lv_bus->voltage > dcdc->lv_bus->dis_voltage_start) { return -1; // start in boost mode allowed } @@ -152,23 +154,23 @@ int dcdc_check_start_conditions(Dcdc *dcdc, DcBus *hv, DcBus *lv) return 0; } -void dcdc_control(Dcdc *dcdc, DcBus *hv, DcBus *lv) +void dcdc_control(Dcdc *dcdc) { if (half_bridge_enabled()) { int step; - if (dcdc->mode == MODE_MPPT_BUCK || dcdc->ls_current > 0.1 ) { + if (dcdc->mode == MODE_MPPT_BUCK || dcdc->lv_bus->current > 0.1 ) { // always in buck mode or when boost or nano grid are receiving energy from high side enegry producer // ( grid [nanogrid] or battery [boost] ) // for low side energy consumers (battery and/or load). //printf("-"); - step = _dcdc_output_control(dcdc, lv, hv, 1); + step = _dcdc_output_control(dcdc, dcdc->lv_bus, dcdc->hv_bus, 1); half_bridge_duty_cycle_step(step); } else { //printf("+"); - step = _dcdc_output_control(dcdc, hv, lv, -1); + step = _dcdc_output_control(dcdc, dcdc->hv_bus, dcdc->lv_bus, -1); half_bridge_duty_cycle_step(-step); } @@ -178,7 +180,7 @@ void dcdc_control(Dcdc *dcdc, DcBus *hv, DcBus *lv) dcdc->off_timestamp = time(NULL); printf("DC/DC stop.\n"); } - else if (lv->voltage > dcdc->ls_voltage_max || hv->voltage > dcdc->hs_voltage_max) { + else if (dcdc->lv_bus->voltage > dcdc->ls_voltage_max || dcdc->hv_bus->voltage > dcdc->hs_voltage_max) { half_bridge_stop(); dcdc->state = DCDC_STATE_OFF; dcdc->off_timestamp = time(NULL); @@ -192,7 +194,7 @@ void dcdc_control(Dcdc *dcdc, DcBus *hv, DcBus *lv) } else { static int current_debounce_counter = 0; - if (dcdc->ls_current > 0.5) { + if (dcdc->lv_bus->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) @@ -209,12 +211,12 @@ void dcdc_control(Dcdc *dcdc, DcBus *hv, DcBus *lv) 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, hv, lv); + int startup_mode = dcdc_check_start_conditions(dcdc); 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(lv->voltage / (hv->voltage - 1)); - printf("DC/DC buck mode start (HV: %.2fV, LV: %.2fV, PWM: %.1f).\n", hv->voltage, lv->voltage, half_bridge_get_duty_cycle() * 100); + 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); } else { startup_delay_counter++; @@ -223,8 +225,8 @@ void dcdc_control(Dcdc *dcdc, DcBus *hv, DcBus *lv) 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(lv->voltage / (hv->voltage + 1)); - printf("DC/DC boost mode start (HV: %.2fV, LV: %.2fV, PWM: %.1f).\n", hv->voltage, lv->voltage, half_bridge_get_duty_cycle() * 100); + 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); } else { startup_delay_counter++; @@ -236,7 +238,7 @@ void dcdc_control(Dcdc *dcdc, DcBus *hv, DcBus *lv) } } -void dcdc_test(Dcdc *dcdc, DcBus *hv, DcBus *lv) +void dcdc_test(Dcdc *dcdc) { if (half_bridge_enabled()) { if (half_bridge_get_duty_cycle() > 0.5) { @@ -247,12 +249,12 @@ void dcdc_test(Dcdc *dcdc, DcBus *hv, DcBus *lv) 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, hv, lv); + int startup_mode = dcdc_check_start_conditions(dcdc); 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(lv->voltage / (hv->voltage - 1)); - printf("DC/DC buck mode start (HV: %.2fV, LV: %.2fV, PWM: %.1f).\n", hv->voltage, lv->voltage, half_bridge_get_duty_cycle() * 100); + 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); } else { startup_delay_counter++; @@ -261,8 +263,8 @@ void dcdc_test(Dcdc *dcdc, DcBus *hv, DcBus *lv) 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(lv->voltage / (hv->voltage + 1)); - printf("DC/DC boost mode start (HV: %.2fV, LV: %.2fV, PWM: %.1f).\n", hv->voltage, lv->voltage, half_bridge_get_duty_cycle() * 100); + 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); } else { startup_delay_counter++; diff --git a/src/dcdc.h b/src/dcdc.h index 438e3d6..9289970 100644 --- a/src/dcdc.h +++ b/src/dcdc.h @@ -64,8 +64,8 @@ typedef struct { uint16_t state; ///< Control state (off / MPPT / CC / CV) // actual measurements - float ls_current; ///< Low-side (inductor) current - float ls_voltage; ///< Low-side (inductor) voltage + DcBus *hv_bus; ///< Pointer to DC bus at high voltage side + DcBus *lv_bus; ///< Pointer to DC bus at low voltage (inductor) side float temp_mosfets; ///< MOSFET temperature measurement (if existing) // current state @@ -91,36 +91,32 @@ typedef struct { * * See http://libre.solar/docs/dcdc_control for detailed information * - * @param dcdc DC/DC type description + * @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) */ -void dcdc_init(Dcdc *dcdc); +void dcdc_init(Dcdc *dcdc, DcBus *hv, DcBus *lv); /** Check for valid start conditions of the DC/DC converter * - * @param dcdc DC/DC type description - * @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 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, DcBus *hv, DcBus *lv); +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 type description - * @param hv High voltage side terminal (e.g. solar input for typical MPPT charge controller application) - * @param lv Low voltage side terminal (e.g. battery output for typical MPPT charge controller application) + * @param dcdc DC/DC object incl. measurement data */ -void dcdc_control(Dcdc *dcdc, DcBus *hv, DcBus *lv); +void dcdc_control(Dcdc *dcdc); /** Test mode for DC/DC, ramping up to 50% duty cycle * - * @param dcdc DC/DC type description - * @param hv High voltage side terminal (e.g. solar input for typical MPPT charge controller application) - * @param lv Low voltage side terminal (e.g. battery output for typical MPPT charge controller application) + * @param dcdc DC/DC object incl. measurement data */ -void dcdc_test(Dcdc *dcdc, DcBus *hv, DcBus *lv); +void dcdc_test(Dcdc *dcdc); /** Prevent overcharging of battery in case of shorted HS MOSFET * diff --git a/src/log.cpp b/src/log.cpp index 58f3d09..635ae3b 100644 --- a/src/log.cpp +++ b/src/log.cpp @@ -77,8 +77,8 @@ void log_update_min_max_values(LogData *log_data, Dcdc *dcdc, Charger *charger, log_data->solar_voltage_max = solar_bus->voltage; } - if (dcdc->ls_current > log_data->dcdc_current_max) { - log_data->dcdc_current_max = dcdc->ls_current; + if (dcdc->lv_bus->current > log_data->dcdc_current_max) { + log_data->dcdc_current_max = dcdc->lv_bus->current; } if (load->bus->current > log_data->load_current_max) { diff --git a/src/main.cpp b/src/main.cpp index f8e909b..08f4619 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -45,17 +45,19 @@ Serial serial(PIN_SWD_TX, PIN_SWD_RX, "serial", 115200); Dcdc dcdc = {}; -DcBus hv_bus = {}; // high voltage side (solar for typical MPPT) -DcBus lv_bus = {}; // low voltage side (battery for typical MPPT) -DcBus *bat_bus = NULL; -DcBus load_bus = {}; // load terminal -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; // battery state information +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 +extern ThingSet ts; // defined in data_objects.cpp time_t timestamp; // current unix timestamp (independent of time(NULL), as it is user-configurable) @@ -68,15 +70,15 @@ void system_control() static int counter = 0; // convert ADC readings to meaningful measurement values - update_measurements(&dcdc, &charger, &hv_bus, &lv_bus, &load_bus); + update_measurements(); #ifdef CHARGER_TYPE_PWM - pwm_switch_control(&pwm_switch, &hv_bus, bat_bus); + 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, &hv_bus, &lv_bus); + dcdc_control(&dcdc); leds_set_charging(half_bridge_enabled()); #endif @@ -88,12 +90,12 @@ void system_control() timestamp++; counter = 0; // energy + soc calculation must be called exactly once per second - dc_bus_energy_balance(&hv_bus); - dc_bus_energy_balance(&lv_bus); - dc_bus_energy_balance(&load_bus); - log_update_energy(&log_data, &hv_bus, &lv_bus, &load_bus); - log_update_min_max_values(&log_data, &dcdc, &charger, &load, &hv_bus, &lv_bus, &load_bus); - battery_update_soc(&bat_conf, &charger, bat_bus); + 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++; } @@ -108,12 +110,12 @@ int main() battery_conf_overwrite(&bat_conf, &bat_conf_user); // initialize conf_user with same values charger_init(&charger); - load_init(&load, &load_bus); + load_init(&load, &load_terminal); #ifdef CHARGER_TYPE_PWM pwm_switch_init(&pwm_switch); #else // MPPT - dcdc_init(&dcdc); + dcdc_init(&dcdc, &hv_terminal, &lv_bus_int); 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 @@ -128,45 +130,48 @@ int main() dma_setup(); adc_timer_start(1000); // 1 kHz wait(0.5); // wait for ADC to collect some measurement values - update_measurements(&dcdc, &charger, &hv_bus, &lv_bus, &load_bus); + update_measurements(); calibrate_current_sensors(); // Communication interfaces ts_devices.enable(); - + uext_init(); init_watchdog(10); // 10s should be enough for communication ports #ifdef CHARGER_TYPE_PWM - dc_bus_init_solar(&hv_bus, PWM_CURRENT_MAX); - bat_bus = &lv_bus; + 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: - dc_bus_init_nanogrid(&hv_bus); - bat_bus = &lv_bus; + bat_terminal = &lv_terminal; + dc_bus_init_nanogrid(&hv_terminal); break; case MODE_MPPT_BUCK: // typical MPPT charge controller operation - dc_bus_init_solar(&hv_bus, DCDC_CURRENT_MAX); - bat_bus = &lv_bus; + 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 - dc_bus_init_solar(&lv_bus, DCDC_CURRENT_MAX); - bat_bus = &hv_bus; + 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_bus); // check if we have 24V instead of 12V system - battery_init_dc_bus(bat_bus, &bat_conf, charger.num_batteries); + 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); wait(2); // safety feature: be able to re-flash before starting control_timer_start(CONTROL_FREQUENCY); wait(0.1); // necessary to prevent MCU from randomly getting stuck here if PV panel is connected before battery - sleep_manager_lock_deep_sleep(); // required to have sleep returning. - /* + sleep_manager_lock_deep_sleep(); // required to have sleep returning. + /* The mBed Serial class calls this internal during "attach", this is why it work with ThingSet Serial enabled even without this statement. Might be an issue of the particular STM32F072 mBed code or may affect also STM32L073 platforms on mBed @@ -184,9 +189,9 @@ int main() //printf("Still alive... time: %d, mode: %d\n", (int)time(NULL), dcdc.mode); - charger_state_machine(bat_bus, &bat_conf, &charger); + charger_state_machine(bat_terminal, &bat_conf, &charger); - load_state_machine(&load, lv_bus.dis_allowed); + load_state_machine(&load, lv_terminal.dis_allowed); eeprom_update(); diff --git a/src/uext/uext_oled.cpp b/src/uext/uext_oled.cpp index c112aff..ef0cb1e 100644 --- a/src/uext/uext_oled.cpp +++ b/src/uext/uext_oled.cpp @@ -33,9 +33,9 @@ extern LogData log_data; extern Dcdc dcdc; -extern DcBus hv_bus; -extern DcBus lv_bus; -extern DcBus load_bus; +extern DcBus *solar_terminal; +extern DcBus *bat_terminal; +extern DcBus load_terminal; extern Charger charger; extern LoadOutput load; @@ -110,7 +110,7 @@ void uext_process_1s() #else if (half_bridge_enabled()) { #endif - tmp = - hv_bus.voltage * hv_bus.current; + tmp = -solar_terminal->voltage * solar_terminal->current; oled.setTextCursor(0, 18); oled.printf("%4.0fW", (abs(tmp) < 1) ? 0 : tmp); // remove negative zeros } @@ -119,29 +119,27 @@ void uext_process_1s() oled.printf("n/a"); } #ifndef CHARGER_TYPE_PWM - if (hv_bus.voltage > lv_bus.voltage) + if (solar_terminal->voltage > bat_terminal->voltage) #endif { oled.setTextCursor(0, 26); - oled.printf("%4.1fV", hv_bus.voltage); + oled.printf("%4.1fV", solar_terminal->voltage); } // battery data - tmp = lv_bus.voltage * lv_bus.current; oled.setTextCursor(42, 18); - oled.printf("%5.1fW", (abs(tmp) < 0.1) ? 0 : tmp); // 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", lv_bus.voltage); + oled.printf("%5.1fV", bat_terminal->voltage); // load data - tmp = lv_bus.voltage * load.bus->current; oled.setTextCursor(90, 18); - oled.printf("%5.1fW", (abs(tmp) < 0.1) ? 0 : tmp); // remove negative zeros + oled.printf("%5.1fW", (abs(load_terminal.power) < 0.1) ? 0 : load_terminal.power); // remove negative zeros oled.setTextCursor(90, 26); oled.printf("%5.1fA\n", (abs(load.bus->current) < 0.1) ? 0 : load.bus->current); oled.setTextCursor(0, 36); - oled.printf("Day +%5.0fWh -%5.0fWh", hv_bus.dis_energy_Wh, fabs(load_bus.chg_energy_Wh)); + oled.printf("Day +%5.0fWh -%5.0fWh", solar_terminal->dis_energy_Wh, fabs(load_terminal.chg_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 54681d7..ef02ca7 100644 --- a/test/main.cpp +++ b/test/main.cpp @@ -18,22 +18,27 @@ #include "tests.h" Dcdc dcdc = {}; -DcBus hv_bus = {}; // high voltage side (solar for typical MPPT) -DcBus lv_bus = {}; // low voltage side (battery for typical MPPT) -DcBus *bat_bus = NULL; -DcBus load_bus = {}; // load terminal -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; // battery state information +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 +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; + adc_tests(); bat_charger_tests(); dc_bus_tests(); diff --git a/test/tests_adc.cpp b/test/tests_adc.cpp index 9936b7e..3d054f5 100644 --- a/test/tests_adc.cpp +++ b/test/tests_adc.cpp @@ -4,30 +4,37 @@ #include "adc_dma_stub.h" extern Dcdc dcdc; -extern DcBus hv_bus; -extern DcBus lv_bus; -extern DcBus load_bus; +extern DcBus hv_terminal; +extern DcBus lv_terminal; +extern DcBus lv_bus_int; +extern DcBus load_terminal; extern Charger charger; extern LoadOutput load; static adc_values_t adcval; -void check_solar_bus_readings() +void check_solar_terminal_readings() { - TEST_ASSERT_EQUAL_FLOAT(adcval.solar_voltage, round(hv_bus.voltage * 10) / 10); - TEST_ASSERT_EQUAL_FLOAT(adcval.dcdc_current / adcval.solar_voltage * adcval.battery_voltage, -round(hv_bus.current * 10) / 10); + 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); } -void check_battery_bus_readings() +void check_bat_terminal_readings() { - TEST_ASSERT_EQUAL_FLOAT(adcval.battery_voltage, round(lv_bus.voltage * 10) / 10); - TEST_ASSERT_EQUAL_FLOAT(adcval.dcdc_current - adcval.load_current, round(lv_bus.current * 10) / 10); + 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); } -void check_load_bus_readings() +void check_load_terminal_readings() { - TEST_ASSERT_EQUAL_FLOAT(adcval.battery_voltage, round(load_bus.voltage * 10) / 10); - TEST_ASSERT_EQUAL_FLOAT(adcval.load_current, round(load_bus.current * 10) / 10); + TEST_ASSERT_EQUAL_FLOAT(adcval.battery_voltage, round(load_terminal.voltage * 10) / 10); + TEST_ASSERT_EQUAL_FLOAT(adcval.load_current, round(load_terminal.current * 10) / 10); +} + +void check_lv_bus_int_readings() +{ + TEST_ASSERT_EQUAL_FLOAT(adcval.battery_voltage, round(lv_bus_int.voltage * 10) / 10); + TEST_ASSERT_EQUAL_FLOAT(adcval.dcdc_current, round(lv_bus_int.current * 10) / 10); } void check_temperature_readings() @@ -52,13 +59,15 @@ void adc_tests() prepare_adc_readings(adcval); // call original update_measurements function - update_measurements(&dcdc, &charger, &hv_bus, &lv_bus, &load_bus); + update_measurements(); UNITY_BEGIN(); - RUN_TEST(check_solar_bus_readings); - RUN_TEST(check_battery_bus_readings); - RUN_TEST(check_load_bus_readings); + RUN_TEST(check_solar_terminal_readings); + RUN_TEST(check_bat_terminal_readings); + RUN_TEST(check_load_terminal_readings); + RUN_TEST(check_lv_bus_int_readings); + //RUN_TEST(check_temperature_readings); // TODO UNITY_END(); diff --git a/test/tests_bat_charger.cpp b/test/tests_bat_charger.cpp index 1e4aacf..f0914a6 100644 --- a/test/tests_bat_charger.cpp +++ b/test/tests_bat_charger.cpp @@ -9,21 +9,21 @@ extern BatConf bat_conf; extern Charger charger; -extern DcBus lv_bus; +extern DcBus *bat_terminal; static void init_structs() { battery_conf_init(&bat_conf, BAT_TYPE_FLOODED, 6, 100); charger_init(&charger); - battery_init_dc_bus(&lv_bus, &bat_conf, 1); + battery_init_dc_bus(bat_terminal, &bat_conf, 1); charger.state = CHG_STATE_IDLE; } void no_start_at_high_voltage() { init_structs(); - lv_bus.voltage = bat_conf.voltage_recharge + 0.1; - charger_state_machine(&lv_bus, &bat_conf, &charger); + bat_terminal->voltage = bat_conf.voltage_recharge + 0.1; + charger_state_machine(bat_terminal, &bat_conf, &charger); TEST_ASSERT_EQUAL(CHG_STATE_IDLE, charger.state); } @@ -31,8 +31,8 @@ void no_start_after_short_rest() { init_structs(); charger.time_state_changed = time(NULL) - bat_conf.time_limit_recharge + 1; - lv_bus.voltage = bat_conf.voltage_recharge - 0.1; - charger_state_machine(&lv_bus, &bat_conf, &charger); + bat_terminal->voltage = bat_conf.voltage_recharge - 0.1; + charger_state_machine(bat_terminal, &bat_conf, &charger); TEST_ASSERT_EQUAL(CHG_STATE_IDLE, charger.state); } @@ -40,12 +40,12 @@ void no_start_outside_temperature_limits() { init_structs(); charger.bat_temperature = bat_conf.charge_temp_max + 1; - lv_bus.voltage = bat_conf.voltage_recharge - 0.1; - charger_state_machine(&lv_bus, &bat_conf, &charger); + bat_terminal->voltage = bat_conf.voltage_recharge - 0.1; + charger_state_machine(bat_terminal, &bat_conf, &charger); TEST_ASSERT_EQUAL(CHG_STATE_IDLE, charger.state); charger.bat_temperature = bat_conf.charge_temp_min - 1; - charger_state_machine(&lv_bus, &bat_conf, &charger); + charger_state_machine(bat_terminal, &bat_conf, &charger); TEST_ASSERT_EQUAL(CHG_STATE_IDLE, charger.state); } @@ -53,8 +53,8 @@ void start_if_everything_just_fine() { init_structs(); charger.time_state_changed = time(NULL) - bat_conf.time_limit_recharge - 1; - lv_bus.voltage = bat_conf.voltage_recharge - 0.1; - charger_state_machine(&lv_bus, &bat_conf, &charger); + bat_terminal->voltage = bat_conf.voltage_recharge - 0.1; + charger_state_machine(bat_terminal, &bat_conf, &charger); TEST_ASSERT_EQUAL(CHG_STATE_BULK, charger.state); } @@ -62,11 +62,11 @@ void enter_topping_at_voltage_setpoint() { init_structs(); charger.time_state_changed = time(NULL) - bat_conf.time_limit_recharge - 1; - lv_bus.voltage = bat_conf.voltage_recharge - 0.1; - charger_state_machine(&lv_bus, &bat_conf, &charger); + bat_terminal->voltage = bat_conf.voltage_recharge - 0.1; + charger_state_machine(bat_terminal, &bat_conf, &charger); TEST_ASSERT_EQUAL(CHG_STATE_BULK, charger.state); - lv_bus.voltage = bat_conf.voltage_topping + 0.1; - charger_state_machine(&lv_bus, &bat_conf, &charger); + bat_terminal->voltage = bat_conf.voltage_topping + 0.1; + charger_state_machine(bat_terminal, &bat_conf, &charger); TEST_ASSERT_EQUAL(CHG_STATE_TOPPING, charger.state); } @@ -75,14 +75,14 @@ void stop_topping_after_time_limit() enter_topping_at_voltage_setpoint(); charger.time_state_changed = time(NULL) - bat_conf.time_limit_topping + 1; - lv_bus.voltage = bat_conf.voltage_topping + 0.1; - lv_bus.current = bat_conf.current_cutoff_topping + 0.1; + bat_terminal->voltage = bat_conf.voltage_topping + 0.1; + bat_terminal->current = bat_conf.current_cutoff_topping + 0.1; - charger_state_machine(&lv_bus, &bat_conf, &charger); + charger_state_machine(bat_terminal, &bat_conf, &charger); TEST_ASSERT_EQUAL(CHG_STATE_TOPPING, charger.state); charger.time_state_changed = time(NULL) - bat_conf.time_limit_topping - 1; - charger_state_machine(&lv_bus, &bat_conf, &charger); + charger_state_machine(bat_terminal, &bat_conf, &charger); TEST_ASSERT_EQUAL(CHG_STATE_TRICKLE, charger.state); } @@ -91,9 +91,9 @@ void stop_topping_at_cutoff_current() enter_topping_at_voltage_setpoint(); charger.time_state_changed = time(NULL) - 1; - lv_bus.voltage = bat_conf.voltage_topping + 0.1; - lv_bus.current = bat_conf.current_cutoff_topping - 0.1; - charger_state_machine(&lv_bus, &bat_conf, &charger); + bat_terminal->voltage = bat_conf.voltage_topping + 0.1; + bat_terminal->current = bat_conf.current_cutoff_topping - 0.1; + charger_state_machine(bat_terminal, &bat_conf, &charger); TEST_ASSERT_EQUAL(CHG_STATE_TRICKLE, charger.state); } @@ -103,9 +103,9 @@ void trickle_to_idle_for_li_ion() battery_conf_init(&bat_conf, BAT_TYPE_LFP, 4, 100); charger.time_state_changed = time(NULL) - 1; - lv_bus.voltage = bat_conf.voltage_topping + 0.1; - lv_bus.current = bat_conf.current_cutoff_topping - 0.1; - charger_state_machine(&lv_bus, &bat_conf, &charger); + bat_terminal->voltage = bat_conf.voltage_topping + 0.1; + bat_terminal->current = bat_conf.current_cutoff_topping - 0.1; + charger_state_machine(bat_terminal, &bat_conf, &charger); TEST_ASSERT_EQUAL(CHG_STATE_IDLE, charger.state); } @@ -115,9 +115,9 @@ void trickle_to_equalization_if_enabled() bat_conf.equalization_enabled = true; charger.time_state_changed = time(NULL) - 1; - lv_bus.voltage = bat_conf.voltage_topping + 0.1; - lv_bus.current = bat_conf.current_cutoff_topping - 0.1; - charger_state_machine(&lv_bus, &bat_conf, &charger); + bat_terminal->voltage = bat_conf.voltage_topping + 0.1; + bat_terminal->current = bat_conf.current_cutoff_topping - 0.1; + charger_state_machine(bat_terminal, &bat_conf, &charger); TEST_ASSERT_EQUAL(CHG_STATE_EQUALIZATION, charger.state); } @@ -127,14 +127,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; - lv_bus.voltage = bat_conf.voltage_topping - 0.1; - lv_bus.current = bat_conf.current_cutoff_topping - 0.1; + bat_terminal->voltage = bat_conf.voltage_topping - 0.1; + bat_terminal->current = bat_conf.current_cutoff_topping - 0.1; - charger_state_machine(&lv_bus, &bat_conf, &charger); + charger_state_machine(bat_terminal, &bat_conf, &charger); TEST_ASSERT_EQUAL(CHG_STATE_TOPPING, charger.state); charger.time_voltage_limit_reached = time(NULL) - 1; - charger_state_machine(&lv_bus, &bat_conf, &charger); + charger_state_machine(bat_terminal, &bat_conf, &charger); TEST_ASSERT_EQUAL(CHG_STATE_TRICKLE, charger.state); } diff --git a/test/tests_dc_bus.cpp b/test/tests_dc_bus.cpp index b46f1ec..8211833 100644 --- a/test/tests_dc_bus.cpp +++ b/test/tests_dc_bus.cpp @@ -12,10 +12,10 @@ #include extern Dcdc dcdc; -extern DcBus hv_bus; -extern DcBus lv_bus; -extern DcBus *bat_port; -extern DcBus load_bus; +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; @@ -25,7 +25,7 @@ static void init_structs() { battery_conf_init(&bat_conf, BAT_TYPE_FLOODED, 6, 100); charger_init(&charger); - battery_init_dc_bus(&lv_bus, &bat_conf, 1); + battery_init_dc_bus(&lv_terminal, &bat_conf, 1); charger.state = CHG_STATE_IDLE; } @@ -42,10 +42,10 @@ void energy_calculation_init() log_data.load_out_total_Wh = 0; log_data.bat_chg_total_Wh = 0; log_data.bat_dis_total_Wh = 0; - hv_bus.dis_energy_Wh = 0.0; - load_bus.chg_energy_Wh = 0.0; - lv_bus.chg_energy_Wh = 0.0; - lv_bus.dis_energy_Wh = 0.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; @@ -58,23 +58,23 @@ void energy_calculation_init() // insert values into ADC functions prepare_adc_readings(adcval); - update_measurements(&dcdc, &charger, &hv_bus, &lv_bus, &load_bus); + update_measurements(); for (int i = 0; i < 60*60*sun_hours; i++) { - dc_bus_energy_balance(&hv_bus); - dc_bus_energy_balance(&lv_bus); - dc_bus_energy_balance(&load_bus); + 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(&dcdc, &charger, &hv_bus, &lv_bus, &load_bus); + update_measurements(); for (int i = 0; i < 60*60*night_hours; i++) { - dc_bus_energy_balance(&hv_bus); - dc_bus_energy_balance(&lv_bus); - dc_bus_energy_balance(&load_bus); + dc_bus_energy_balance(&hv_terminal); + dc_bus_energy_balance(&lv_terminal); + dc_bus_energy_balance(&load_terminal); } } @@ -82,26 +82,26 @@ void charging_energy_calculation_valid() { energy_calculation_init(); // charging only during sun hours - TEST_ASSERT_EQUAL_FLOAT(round(sun_hours * lv_bus.voltage * (dcdc_current_sun - load_current)), round(lv_bus.chg_energy_Wh)); + 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_bus.voltage * adcval.load_current), round(lv_bus.dis_energy_Wh)); + 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_bus.voltage * dcdc_current_sun), round(hv_bus.dis_energy_Wh)); + 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_bus.voltage * adcval.load_current), round(load_bus.chg_energy_Wh)); + 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() diff --git a/test/tests_dcdc.cpp b/test/tests_dcdc.cpp index cd547de..99a55f0 100644 --- a/test/tests_dcdc.cpp +++ b/test/tests_dcdc.cpp @@ -10,69 +10,69 @@ #include extern Dcdc dcdc; -extern DcBus hv_bus; -extern DcBus lv_bus; +extern DcBus hv_terminal; +extern DcBus lv_bus_int; extern BatConf bat_conf; static void init_structs_buck() { - dc_bus_init_solar(&hv_bus, 10); - hv_bus.voltage = 20; - hv_bus.dis_voltage_start = 18; + dc_bus_init_solar(&hv_terminal, 10); + hv_terminal.voltage = 20; + hv_terminal.dis_voltage_start = 18; battery_conf_init(&bat_conf, BAT_TYPE_GEL, 6, 100); - battery_init_dc_bus(&lv_bus, &bat_conf, 1); - lv_bus.voltage = 14; + battery_init_dc_bus(&lv_bus_int, &bat_conf, 1); + lv_bus_int.voltage = 14; - dcdc_init(&dcdc); + dcdc_init(&dcdc, &hv_terminal, &lv_bus_int); } static void init_structs_boost() { - dc_bus_init_solar(&lv_bus, 10); - lv_bus.voltage = 20; - lv_bus.dis_voltage_start = 18; + dc_bus_init_solar(&lv_bus_int, 10); + lv_bus_int.voltage = 20; + lv_bus_int.dis_voltage_start = 18; battery_conf_init(&bat_conf, BAT_TYPE_NMC, 10, 9); - battery_init_dc_bus(&hv_bus, &bat_conf, 1); - hv_bus.voltage = 3.7 * 10; + battery_init_dc_bus(&hv_terminal, &bat_conf, 1); + hv_terminal.voltage = 3.7 * 10; - dcdc_init(&dcdc); + dcdc_init(&dcdc, &hv_terminal, &lv_bus_int); } void start_valid_mppt_buck() { init_structs_buck(); - TEST_ASSERT_EQUAL(1, dcdc_check_start_conditions(&dcdc, &hv_bus, &lv_bus)); + TEST_ASSERT_EQUAL(1, dcdc_check_start_conditions(&dcdc)); } void start_valid_mppt_boost() { init_structs_boost(); - TEST_ASSERT_EQUAL(-1, dcdc_check_start_conditions(&dcdc, &hv_bus, &lv_bus)); + TEST_ASSERT_EQUAL(-1, dcdc_check_start_conditions(&dcdc)); } 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, &hv_bus, &lv_bus)); + TEST_ASSERT_EQUAL(0, dcdc_check_start_conditions(&dcdc)); dcdc.off_timestamp = time(NULL) - dcdc.restart_interval; - TEST_ASSERT_EQUAL(1, dcdc_check_start_conditions(&dcdc, &hv_bus, &lv_bus)); + TEST_ASSERT_EQUAL(1, dcdc_check_start_conditions(&dcdc)); } void no_start_if_dcdc_disabled() { init_structs_buck(); dcdc.enabled = false; - TEST_ASSERT_EQUAL(0, dcdc_check_start_conditions(&dcdc, &hv_bus, &lv_bus)); + TEST_ASSERT_EQUAL(0, dcdc_check_start_conditions(&dcdc)); } void no_start_if_dcdc_lv_voltage_low() { init_structs_buck(); - lv_bus.voltage = dcdc.ls_voltage_min - 0.5; - TEST_ASSERT_EQUAL(0, dcdc_check_start_conditions(&dcdc, &hv_bus, &lv_bus)); + lv_bus_int.voltage = dcdc.ls_voltage_min - 0.5; + TEST_ASSERT_EQUAL(0, dcdc_check_start_conditions(&dcdc)); } // buck @@ -80,36 +80,36 @@ void no_start_if_dcdc_lv_voltage_low() void no_buck_start_if_bat_voltage_low() { init_structs_buck(); - lv_bus.voltage = bat_conf.voltage_absolute_min - 0.1; - TEST_ASSERT_EQUAL(0, dcdc_check_start_conditions(&dcdc, &hv_bus, &lv_bus)); + lv_bus_int.voltage = bat_conf.voltage_absolute_min - 0.1; + TEST_ASSERT_EQUAL(0, dcdc_check_start_conditions(&dcdc)); } void no_buck_start_if_bat_voltage_high() { init_structs_buck(); - lv_bus.voltage = bat_conf.voltage_topping + 0.1; - TEST_ASSERT_EQUAL(0, dcdc_check_start_conditions(&dcdc, &hv_bus, &lv_bus)); + lv_bus_int.voltage = bat_conf.voltage_topping + 0.1; + TEST_ASSERT_EQUAL(0, dcdc_check_start_conditions(&dcdc)); } void no_buck_start_if_bat_chg_not_allowed() { init_structs_buck(); - lv_bus.chg_current_max = 0; - TEST_ASSERT_EQUAL(0, dcdc_check_start_conditions(&dcdc, &hv_bus, &lv_bus)); + lv_bus_int.chg_current_max = 0; + TEST_ASSERT_EQUAL(0, dcdc_check_start_conditions(&dcdc)); } void no_buck_start_if_solar_voltage_high() { init_structs_buck(); - hv_bus.voltage = dcdc.hs_voltage_max + 1; - TEST_ASSERT_EQUAL(0, dcdc_check_start_conditions(&dcdc, &hv_bus, &lv_bus)); + hv_terminal.voltage = dcdc.hs_voltage_max + 1; + TEST_ASSERT_EQUAL(0, dcdc_check_start_conditions(&dcdc)); } void no_buck_start_if_solar_voltage_low() { init_structs_buck(); - hv_bus.voltage = 17; - TEST_ASSERT_EQUAL(0, dcdc_check_start_conditions(&dcdc, &hv_bus, &lv_bus)); + hv_terminal.voltage = 17; + TEST_ASSERT_EQUAL(0, dcdc_check_start_conditions(&dcdc)); } // boost @@ -117,36 +117,36 @@ void no_buck_start_if_solar_voltage_low() void no_boost_start_if_bat_voltage_low() { init_structs_boost(); - hv_bus.voltage = bat_conf.voltage_absolute_min - 0.1; - TEST_ASSERT_EQUAL(0, dcdc_check_start_conditions(&dcdc, &hv_bus, &lv_bus)); + hv_terminal.voltage = bat_conf.voltage_absolute_min - 0.1; + TEST_ASSERT_EQUAL(0, dcdc_check_start_conditions(&dcdc)); } void no_boost_start_if_bat_voltage_high() { init_structs_boost(); - hv_bus.voltage = bat_conf.voltage_topping + 0.1; - TEST_ASSERT_EQUAL(0, dcdc_check_start_conditions(&dcdc, &hv_bus, &lv_bus)); + hv_terminal.voltage = bat_conf.voltage_topping + 0.1; + TEST_ASSERT_EQUAL(0, dcdc_check_start_conditions(&dcdc)); } void no_boost_start_if_bat_chg_not_allowed() { init_structs_boost(); - hv_bus.chg_current_max = 0; - TEST_ASSERT_EQUAL(0, dcdc_check_start_conditions(&dcdc, &hv_bus, &lv_bus)); + hv_terminal.chg_current_max = 0; + TEST_ASSERT_EQUAL(0, dcdc_check_start_conditions(&dcdc)); } void no_boost_start_if_solar_voltage_high() { init_structs_boost(); - lv_bus.voltage = dcdc.ls_voltage_max + 1; - TEST_ASSERT_EQUAL(0, dcdc_check_start_conditions(&dcdc, &hv_bus, &lv_bus)); + lv_bus_int.voltage = dcdc.ls_voltage_max + 1; + TEST_ASSERT_EQUAL(0, dcdc_check_start_conditions(&dcdc)); } void no_boost_start_if_solar_voltage_low() { init_structs_boost(); - lv_bus.voltage = 17; - TEST_ASSERT_EQUAL(0, dcdc_check_start_conditions(&dcdc, &hv_bus, &lv_bus)); + lv_bus_int.voltage = 17; + TEST_ASSERT_EQUAL(0, dcdc_check_start_conditions(&dcdc)); } void dcdc_tests()