diff --git a/lib/AnalogValue/AnalogValue.cpp b/lib/AnalogValue/AnalogValue.cpp index 573f655..60e1fe3 100644 --- a/lib/AnalogValue/AnalogValue.cpp +++ b/lib/AnalogValue/AnalogValue.cpp @@ -22,13 +22,14 @@ AnalogValue::AnalogValue(PinName pin, float multiplier, const int num_readings, _adc(pin), _num_readings(num_readings) { _multiplier = multiplier; + _offset = 0; update(); // initialize value without filter _filter_constant = filter_constant; } void AnalogValue::update() { - float tmp = adc_read_avg(_adc, _num_readings) * _vcc * _multiplier; + float tmp = adc_read_avg(_adc, _num_readings) * _vcc * _multiplier + _offset; _value = (1.0 - _filter_constant) * tmp + _filter_constant * _value; } @@ -51,3 +52,9 @@ void AnalogValue::update_reference_voltage(AnalogIn& adc_ref, float ref_voltage, { _vcc = ref_voltage / adc_read_avg(adc_ref, num_readings); } + +void AnalogValue::calibrate_offset(float expected_value) +{ + float tmp = adc_read_avg(_adc, _num_readings) * _vcc * _multiplier; + _offset = expected_value - tmp; +} diff --git a/lib/AnalogValue/AnalogValue.h b/lib/AnalogValue/AnalogValue.h index 4646372..293ff26 100644 --- a/lib/AnalogValue/AnalogValue.h +++ b/lib/AnalogValue/AnalogValue.h @@ -48,6 +48,12 @@ public: */ float read(); + /** Read the last updated (and filtered) ADC result (incl. multiplier) + * + * @param expected_value Value which should be read under current conditions (after application of multiplier) + */ + void calibrate_offset(float expected_value = 0.0); + /** An operator shorthand for read() * * The float() operator can be used as a shorthand for read() to simplify common code sequences @@ -66,7 +72,7 @@ public: } private: - + /** Read out ADC results and apply averaging and/or low pass filter * * @param input AnalogIn for the pin to be read @@ -85,4 +91,5 @@ private: const int _num_readings; float _filter_constant; + float _offset; }; diff --git a/src/config.h b/src/config.h index da99ad0..f0cbe98 100644 --- a/src/config.h +++ b/src/config.h @@ -22,7 +22,6 @@ #define MPPT_20A - // DC/DC converter settings const int _pwm_frequency = 70; // kHz @@ -45,7 +44,7 @@ ChargingProfile profile = { // State: CV/absorption 2.4, // cell_voltage_max; // max voltage per cell - 120, // time_limit_CV; // sec + 120*60, // time_limit_CV; // sec 2.0, // current_cutoff_CV; // A // State: float/trickle diff --git a/src/config_12a.h b/src/config_12a.h index e7d3b4b..7cd30b1 100644 --- a/src/config_12a.h +++ b/src/config_12a.h @@ -18,46 +18,55 @@ #define CONFIG_12A_H #include "mbed.h" +#include "ChargeController.h" #define MPPT_12A // DC/DC converter settings const int _pwm_frequency = 100; // kHz -const int charge_current_cutoff = 20; // mA --> if lower, charger is switched off -const int max_solar_voltage = 55000; // mV - -const int num_cells = 6; +const float dcdc_current_min = 0.1; // A --> if lower, charger is switched off +const float solar_voltage_max = 55.0; // V // State: Standby -const int solar_voltage_offset_start = 5000; // mV charging switched on if Vsolar > Vbat + offset -const int solar_voltage_offset_stop = 1000; // mV charging switched off if Vsolar < Vbat + offset -const int time_limit_recharge = 60; // sec -const int cell_voltage_recharge = 2300; // mV +const float solar_voltage_offset_start = 5.0; // V charging switched on if Vsolar > Vbat + offset +const float solar_voltage_offset_stop = 1.0; // V charging switched off if Vsolar < Vbat + offset -// State: CC/bulk -const int charge_current_max = 10000; // mA PCB maximum: 20 A +ChargingProfile profile = { + 6, // num_cells; -// State: CV/absorption -const int cell_voltage_max = 2400; // max voltage per cell -const int time_limit_CV = 120; // min -const int current_cutoff_CV = 300; //2000; // mA + // State: Standby + 60, // time_limit_recharge; // sec + 2.3, // cell_voltage_recharge; // V -// State: float/trickle -const bool trickle_enabled = true; -const int cell_voltage_trickle = 2300; // voltage for trickle charging of lead-acid batteries -const int time_trickle_recharge = 30; // min + // State: CC/bulk + 10.0, // charge_current_max; // A PCB maximum: 20 A -// State: equalization -const bool equalization_enabled = false; -const int cell_voltage_equalization = 2500; // mV -const int time_limit_equalization = 60; // min -const int current_limit_equalization = 1000; // mA -const int equalization_trigger_time = 8; // weeks -const int equalization_trigger_deep_cycles = 10; // times + // State: CV/absorption + 2.4, // cell_voltage_max; // max voltage per cell + 120*60, // time_limit_CV; // sec + 2.0, // current_cutoff_CV; // A + + // State: float/trickle + true, // trickle_enabled; + 2.3, // cell_voltage_trickle; // target voltage for trickle charging of lead-acid batteries + 30*60, // time_trickle_recharge; // sec + + // State: equalization + false, // equalization_enabled; + 2.5, // cell_voltage_equalization; // V + 60, // time_limit_equalization; // sec + 1.0, // current_limit_equalization; // A + 8, // equalization_trigger_time; // weeks + 10, // equalization_trigger_deep_cycles; // times + + 1.95, // cell_voltage_load_disconnect; + 2.2, // cell_voltage_load_reconnect; + + // TODO + 1.0 // temperature_compensation; +}; -const int cell_voltage_load_disconnect = 1950; -const int cell_voltage_load_reconnect = 2200; /* // typcial LiFePO4 battery diff --git a/src/main.cpp b/src/main.cpp index c924cac..e94581a 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -52,7 +52,9 @@ DigitalOut led_green(PIN_LED_GREEN); DigitalOut led_red(PIN_LED_RED); DigitalOut load_disable(PIN_LOAD_DIS); -//AnalogOut ref_i_dcdc(PIN_REF_I_DCDC); +#ifdef MPPT_20A +AnalogOut ref_i_dcdc(PIN_REF_I_DCDC); +#endif AnalogIn v_ref(PIN_V_REF); @@ -63,14 +65,18 @@ AnalogValue dcdc_current(PIN_I_DCDC, 1000 / 2 / (1500.0 / 22.0)); // op amp gain AnalogValue load_current(PIN_I_LOAD, 1000 / 2 / (1500.0 / 22.0)); AnalogValue thermistor_voltage(PIN_TEMP_INT, 1.0); #endif +#ifdef MPPT_12A +AnalogValue solar_voltage(PIN_V_SOLAR, 156.8 / 6.8); // solar voltage divider o-- 100k --o-- 5.6k --o +AnalogValue battery_voltage(PIN_V_BAT, 130 / 10); // battery voltage divider o-- 100k --o-- 10k --o +AnalogValue dcdc_current(PIN_I_DCDC, 1000 / 5 / 50); // op amp gain: 150/2.2 = 68.2, resistor: 2 mOhm +AnalogValue load_current(PIN_I_LOAD, 1000 / 5 / 50); +AnalogValue thermistor_voltage(PIN_TEMP_INT, 1.0); +#endif ChargeController charger(profile); float temperature; // °C -int dcdc_current_offset = 0; -int load_current_offset = 0; - // for daily energy counting int seconds_zero_solar = 0; int seconds_day = 0; @@ -88,7 +94,6 @@ void setup(); void update_measurements(); void update_screen(); void energy_counter(); -void load_management(); void log_data(); void rtc_write_backup_reg(uint32_t BackupRegister, uint32_t data); uint32_t rtc_read_backup_reg(uint32_t BackupRegister); @@ -105,7 +110,8 @@ int main() time_t last_second = time(NULL); tick1.attach(&update_measurements, 0.01); - tick2.attach(&update_mppt, 0.1); + tick2.attach(&update_mppt, 0.05); + //dcdc.start(0.5); // for testing only (don't run update_mppt!) //time_state_changed = time(NULL) - 60*60*24; // start immediately @@ -113,9 +119,21 @@ int main() // called once per second if (time(NULL) - last_second >= 1) { last_second = time(NULL); + + // updates charger state machine + charger.update(battery_voltage.read(), dcdc_current.read()); + + // load management + if (charger.discharging_enabled()) { + load_disable = 0; + } else { + load_disable = 1; + } + energy_counter(); update_screen(); log_data(); + fflush(stdout); } sleep(); // wake-up by ticker interrupts } @@ -126,7 +144,10 @@ void setup() { led_green = 1; load_disable = 1; - // ref_i_dcdc = 0; // 0 for buck, 1 for boost (TODO) + +#ifdef MPPT_20A + ref_i_dcdc = 0.1; // 0 for buck, 1 for boost (TODO) +#endif // for RTC backup register __PWR_CLK_ENABLE(); // normally in SystemClock_Config() @@ -134,20 +155,20 @@ void setup() set_time(rtc_read_backup_reg(0)); - input_Wh_day = (float)rtc_read_backup_reg(1) / 1000.0; // mWh - output_Wh_day = (float)rtc_read_backup_reg(2) / 1000.0; // mWh - input_Wh_total = (float)rtc_read_backup_reg(3); // Wh - output_Wh_total = (float)rtc_read_backup_reg(4); // Wh + input_Wh_day = rtc_read_backup_reg(1); + output_Wh_day = rtc_read_backup_reg(2); + input_Wh_total = rtc_read_backup_reg(3); + output_Wh_total = rtc_read_backup_reg(4); dcdc.deadtime_ns(300); dcdc.lock_settings(); dcdc.duty_cycle_limits((float) profile.cell_voltage_load_disconnect * profile.num_cells / solar_voltage_max, 0.97); + wait(0.2); update_measurements(); - // calibration of current sensors in no load condition - //load_current_offset = -load_current; - //dcdc_current_offset = -dcdc_current; + dcdc_current.calibrate_offset(0.0); + load_current.calibrate_offset(0.0); serial.baud(115200); serial.printf("\nSerial interface started...\n"); @@ -159,19 +180,11 @@ void setup() freopen("/serial", "w", stdout); // retarget stdout } -void load_management() -{ - if (charger.discharging_enabled()) { - load_disable = 0; - } else { - load_disable = 1; - } -} - void update_mppt() { - charger.update(battery_voltage.read(), dcdc_current.read()); - load_management(); + //serial.printf("B: %.2fV, %.2fA, S: %.2fV, Charger: %d, DCDC: %d, PWM: %.1f\n", + // battery_voltage.read(), dcdc_current.read(), solar_voltage.read(), + // charger.get_state(), dcdc.enabled(), dcdc.get_duty_cycle() * 100.0); float dcdc_power_new = battery_voltage * dcdc_current; @@ -187,6 +200,7 @@ void update_mppt() { dcdc.stop(); led_red = 0; + //serial.printf("DCDC stopped!\n"); } if (battery_voltage > charger.read_target_voltage()) { @@ -199,13 +213,14 @@ void update_mppt() // (otherwise current could get negative, i.e. into solar panel) dcdc.stop(); led_red = 0; + //serial.printf("DCDC stopped!\n"); } } else if (dcdc_current > charger.read_target_current()) { // increase input voltage to decrease current dcdc.duty_cycle_step(-1); } - else { + else if (dcdc.enabled()) { //printf("MPPT\n"); // start MPPT if (dcdc_power > dcdc_power_new) { @@ -252,10 +267,10 @@ void energy_counter() // save values to RTC backup register rtc_write_backup_reg(0, time(NULL)); - rtc_write_backup_reg(1, (uint32_t)(input_Wh_day * 1000)); // mWh - rtc_write_backup_reg(2, (uint32_t)(output_Wh_day * 1000)); // mWh - rtc_write_backup_reg(3, (uint32_t)(input_Wh_total)); // kWh - rtc_write_backup_reg(4, (uint32_t)(output_Wh_total)); // kWh + rtc_write_backup_reg(1, input_Wh_day); + rtc_write_backup_reg(2, output_Wh_day); + rtc_write_backup_reg(3, input_Wh_total); + rtc_write_backup_reg(4, output_Wh_total); } void log_data()