diff --git a/src/charger.cpp b/src/charger.cpp index c37dbde..5adc9b1 100644 --- a/src/charger.cpp +++ b/src/charger.cpp @@ -28,36 +28,6 @@ void _enter_state(battery_state_t* bat_state, int next_state) bat_state->chg_state = next_state; } -/***************************************************************************** - * Charger state machine - - ## Idle - Initial state of the charge controller. If the solar voltage is high enough - and the battery is not full, charging in CC mode is started. - - ## CC / bulk charging - The battery is charged with maximum possible current (MPPT algorithm is - active) until the CV voltage limit is reached. - - ## CV / absorption charging - Lead-acid batteries are charged for some time using a slightly higher charge - voltage. After a current cutoff limit or a time limit is reached, the charger - goes into trickle or equalization mode for lead-acid batteries or back into - Standby for Li-ion batteries. - - ## Trickle charging - This mode is kept forever for a lead-acid battery and keeps the battery at - full state of charge. If too much power is drawn from the battery, the - charger switches back into CC / bulk charging mode. - - ## Equalization charging - This mode is only used for lead-acid batteries after several deep-discharge - cycles or a very long period of time with no equalization. Voltage is - increased to 15V or above, so care must be taken for the other system - components attached to the battery. (currently, no equalization charging is - enabled in the software) - - */ void charger_state_machine(power_port_t *port, battery_conf_t *bat_conf, battery_state_t *bat_state, float voltage, float current) { //printf("time_state_change = %d, time = %d, v_bat = %f, i_bat = %f\n", bat_state->time_state_changed, time(NULL), voltage, current); @@ -94,23 +64,23 @@ void charger_state_machine(power_port_t *port, battery_conf_t *bat_conf, battery port->current_output_max = bat_conf->charge_current_max; port->output_allowed = true; bat_state->full = false; - _enter_state(bat_state, CHG_STATE_CC); + _enter_state(bat_state, CHG_STATE_BULK); } break; } - case CHG_STATE_CC: { + case CHG_STATE_BULK: { if (voltage > port->voltage_output_target) { port->voltage_output_target = bat_conf->voltage_max; - _enter_state(bat_state, CHG_STATE_CV); + _enter_state(bat_state, CHG_STATE_TOPPING); } break; } - case CHG_STATE_CV: { + case CHG_STATE_TOPPING: { if (voltage >= port->voltage_output_target) { bat_state->time_voltage_limit_reached = time(NULL); } - // cut-off limit reached because battery full (i.e. CV mode still + // 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 ((current < bat_conf->current_cutoff_CV && (time(NULL) - bat_state->time_voltage_limit_reached) < 2) || (time(NULL) - bat_state->time_state_changed) > bat_conf->time_limit_CV) @@ -147,7 +117,7 @@ void charger_state_machine(power_port_t *port, battery_conf_t *bat_conf, battery port->current_output_max = bat_conf->charge_current_max; port->voltage_output_target = bat_conf->voltage_max; bat_state->full = false; - _enter_state(bat_state, CHG_STATE_CC); + _enter_state(bat_state, CHG_STATE_BULK); } // assumption: trickle does not harm the battery --> never go back to idle // (for Li-ion battery: disable trickle!) diff --git a/src/charger.h b/src/charger.h index bdc274b..44d5c78 100644 --- a/src/charger.h +++ b/src/charger.h @@ -32,13 +32,13 @@ ## Idle Initial state of the charge controller. If the solar voltage is high enough - and the battery is not full, charging in CC mode is started. + and the battery is not full, bulk charging mode is started. - ## CC / bulk charging + ## Bulk / CC / MPPT charging The battery is charged with maximum possible current (MPPT algorithm is active) until the CV voltage limit is reached. - ## CV / absorption charging + ## Topping / CV / absorption charging Lead-acid batteries are charged for some time using a slightly higher charge voltage. After a current cutoff limit or a time limit is reached, the charger goes into trickle or equalization mode for lead-acid batteries or back into @@ -56,11 +56,15 @@ components attached to the battery. (currently, no equalization charging is enabled in the software) + Further information: + https://en.wikipedia.org/wiki/IUoU_battery_charging + https://batteryuniversity.com/learn/article/charging_the_lead_acid_battery + */ enum charger_state { CHG_STATE_IDLE, - CHG_STATE_CC, - CHG_STATE_CV, + CHG_STATE_BULK, + CHG_STATE_TOPPING, CHG_STATE_TRICKLE, CHG_STATE_EQUALIZATION }; diff --git a/src/data_objects.cpp b/src/data_objects.cpp index e431935..bde794e 100644 --- a/src/data_objects.cpp +++ b/src/data_objects.cpp @@ -124,6 +124,7 @@ const data_object_t data_objects[] = { {0x77, TS_CAT_OUTPUT, TS_ACCESS_READ, TS_T_FLOAT32, 1, (void*) &(dcdc.temp_mosfets), "FETs_degC"}, #endif {0x78, TS_CAT_OUTPUT, TS_ACCESS_READ, TS_T_UINT16, 0, (void*) &(bat_state.chg_state), "ChgState"}, + {0x79, TS_CAT_OUTPUT, TS_ACCESS_READ, TS_T_UINT16, 0, (void*) &(dcdc.state), "DCDCState"}, // others {0x90, TS_CAT_OUTPUT, TS_ACCESS_READ, TS_T_FLOAT32, 0, (void*) &(latitude), "Latitude"}, @@ -138,7 +139,7 @@ const data_object_t data_objects[] = { {0x0B, TS_CAT_REC, TS_ACCESS_READ, TS_T_UINT32, 0, (void*) &(bat_state.dis_total_Wh), "BatDisTotal_Wh"}, {0x0C, TS_CAT_REC, TS_ACCESS_READ, TS_T_UINT16, 0, (void*) &(bat_state.num_full_charges), "FullChgCount"}, {0x0D, TS_CAT_REC, TS_ACCESS_READ, TS_T_UINT16, 0, (void*) &(bat_state.num_deep_discharges), "DeepDisCount"}, - {0x0E, TS_CAT_REC, TS_ACCESS_READ, TS_T_UINT16, 0, (void*) &(bat_state.usable_capacity), "BatUsable_Ah"}, // usable battery capacity + {0x0E, TS_CAT_REC, TS_ACCESS_READ, TS_T_FLOAT32, 0, (void*) &(bat_state.usable_capacity), "BatUsable_Ah"}, // usable battery capacity {0x0F, TS_CAT_REC, TS_ACCESS_READ, TS_T_UINT16, 2, (void*) &(log_data.solar_power_max_day), "SolarMaxDay_W"}, {0x10, TS_CAT_REC, TS_ACCESS_READ, TS_T_UINT16, 2, (void*) &(log_data.load_power_max_day), "LoadMaxDay_W"}, @@ -180,14 +181,15 @@ const data_object_t data_objects[] = { // stores object-ids of values to be published via Serial const uint16_t pub_serial[] = { 0x01, // internal time stamp - 0x70, 0x71, 0x72, 0x73, 0x74, // V, I, T - 0x04, 0x76, // LoadState, ChgState - 0xA0, 0xA1, 0xA2, 0xA3, // daily energy throughput + 0x70, 0x71, 0x72, 0x73, // voltage + current + 0x74, 0x76, 0x77, // temperatures + 0x04, 0x78, 0x79, // LoadState, ChgState, DCDCState + 0xA0, 0xA1, 0xA2, 0xA3, // daily energy throughput 0x06, 0xA4 // SOC, coulomb counter }; const ts_pub_channel_t pub_channels[] = { - { "Serial", pub_serial, sizeof(pub_serial)/sizeof(uint16_t) }, + { "Serial_1s", pub_serial, sizeof(pub_serial)/sizeof(uint16_t) }, }; // TODO: find better solution than manual configuration of channel number diff --git a/src/dcdc.cpp b/src/dcdc.cpp index b7a7356..c05d672 100644 --- a/src/dcdc.cpp +++ b/src/dcdc.cpp @@ -27,6 +27,7 @@ void dcdc_init(dcdc_t *dcdc) { dcdc->mode = DCDC_MODE_INIT; dcdc->enabled = true; + dcdc->state = DCDC_STATE_OFF; dcdc->ls_current_max = DCDC_CURRENT_MAX; dcdc->ls_current_min = 0.05; // A if lower, charger is switched off dcdc->hs_voltage_max = HIGH_SIDE_VOLTAGE_MAX; // V @@ -53,24 +54,31 @@ int _dcdc_output_control(dcdc_t *dcdc, power_port_t *out, power_port_t *in) { return 0; } - else if (out->voltage > (out->voltage_output_target - out->droop_resistance * out->current) // output voltage above target - || out->current > out->current_output_max // output current limit exceeded - || (in->voltage < (in->voltage_input_start - in->droop_resistance * in->current) && out->current > 0.1) // input voltage below limit - || in->current < in->current_input_max // input current (negative signs) above limit - || fabs(dcdc->ls_current) > dcdc->ls_current_max // current above hardware maximum - || dcdc->temp_mosfets > 80) // temperature limits exceeded + else if (out->voltage > (out->voltage_output_target - out->droop_resistance * out->current) // output voltage above target + || (in->voltage < (in->voltage_input_start - in->droop_resistance * in->current) && out->current > 0.1)) // input voltage below limit { - //printf(" dec\n"); + dcdc->state = DCDC_STATE_CV; + return -1; // decrease output power + } + else if (out->current > out->current_output_max // output current limit exceeded + || in->current < in->current_input_max) // input current (negative signs) limit exceeded + { + dcdc->state = DCDC_STATE_CC; + return -1; // decrease output power + } + else if (fabs(dcdc->ls_current) > dcdc->ls_current_max // current above hardware maximum + || dcdc->temp_mosfets > 80) // temperature limits exceeded + { + dcdc->state = DCDC_STATE_DERATING; return -1; // decrease output power } else if (out->current < 0.1 && out->voltage < out->voltage_input_start) // no load condition (e.g. start-up of nanogrid) --> raise voltage { - //printf(" inc\n"); return 1; // increase output power } else { - //printf(" MPPT\n"); // start MPPT + dcdc->state = DCDC_STATE_MPPT; if (dcdc->power > dcdc_power_new) { pwm_delta = -pwm_delta; } @@ -107,16 +115,19 @@ void dcdc_control(dcdc_t *dcdc, power_port_t *hs, power_port_t *ls) if (step == 0) { half_bridge_stop(); + dcdc->state = DCDC_STATE_OFF; dcdc->off_timestamp = time(NULL); printf("DC/DC stop.\n"); } else if (ls->voltage > dcdc->ls_voltage_max || hs->voltage > dcdc->hs_voltage_max) { half_bridge_stop(); + dcdc->state = DCDC_STATE_OFF; dcdc->off_timestamp = time(NULL); printf("DC/DC emergency stop (voltage limits exceeded).\n"); } else if (dcdc->enabled == false) { half_bridge_stop(); + dcdc->state = DCDC_STATE_OFF; printf("DC/DC stop (disabled).\n"); } } diff --git a/src/dcdc.h b/src/dcdc.h index 65b5883..af26a17 100644 --- a/src/dcdc.h +++ b/src/dcdc.h @@ -30,7 +30,7 @@ * * Defines which type of device is connected to the high side and low side ports */ -enum dcdc_control_mode +enum dcdc_operation_mode { MODE_MPPT_BUCK, ///< solar panel at high side port, battery / load at low side port (typical MPPT) MODE_MPPT_BOOST, ///< battery at high side port, solar panel at low side (e.g. e-bike charging) @@ -40,14 +40,28 @@ enum dcdc_control_mode ///< nano grid voltage. }; +/** DC/DC control state + * + * Allows to determine the current control state (off, CC, CV and MPPT) + */ +enum dcdc_control_state +{ + DCDC_STATE_OFF, ///< DC/DC switched off (low input power available or actively disabled) + DCDC_STATE_MPPT, ///< Maximum Power Point Tracking + DCDC_STATE_CC, ///< Constant-Current control + DCDC_STATE_CV, ///< Constant-Voltage control + DCDC_STATE_DERATING ///< Hardware-limits (current or temperature) reached +}; + /** DC/DC type * * Contains all data belonging to the DC/DC sub-component of the PCB, incl. * actual measurements and calibration parameters. */ typedef struct { - dcdc_control_mode mode; ///< DC/DC mode (buck, boost or nanogrid) + dcdc_operation_mode 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 float ls_current; ///< Low-side (inductor) current