From bb2ab2a9d5406a6ded093569877417b218fadf5b Mon Sep 17 00:00:00 2001 From: Martin Jaeger <17674105+martinjaeger@users.noreply.github.com> Date: Mon, 25 Mar 2019 17:50:34 +0100 Subject: [PATCH] Initialization of some rec data objects improved --- src/adc_dma.cpp | 36 +++++++++++---------- src/battery.cpp | 14 +++++++-- src/battery.h | 3 +- src/config.cpp_template | 70 ----------------------------------------- src/data_objects.cpp | 4 +-- src/eeprom.cpp | 3 +- 6 files changed, 37 insertions(+), 93 deletions(-) delete mode 100644 src/config.cpp_template diff --git a/src/adc_dma.cpp b/src/adc_dma.cpp index 0dd3703..930d124 100644 --- a/src/adc_dma.cpp +++ b/src/adc_dma.cpp @@ -142,6 +142,26 @@ void update_measurements(dcdc_t *dcdc, battery_state_t *bat, load_output_t *load if (load->current > log_data.load_current_max) { log_data.load_current_max = load->current; } + + if (ls->current > 0) { + uint16_t solar_power = ls->voltage * ls->current; + if (solar_power > log_data.solar_power_max_day) { + log_data.solar_power_max_day = solar_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->current > 0) { + uint16_t load_power = ls->voltage * load->current; + if (load_power > log_data.load_power_max_day) { + log_data.load_power_max_day = load_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) { @@ -151,22 +171,6 @@ void update_measurements(dcdc_t *dcdc, battery_state_t *bat, load_output_t *load //if (meas->temp_int > log_data.temp_int_max) { // log_data.temp_int_max = meas->temp_int; //} - - uint16_t solar_power = ls->voltage * ls->current; - if (solar_power > log_data.solar_power_max_day) { - log_data.solar_power_max_day = solar_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; - } - } - - uint16_t load_power = ls->voltage * load->current; - if (load_power > log_data.load_power_max_day) { - log_data.load_power_max_day = solar_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; - } - } } diff --git a/src/battery.cpp b/src/battery.cpp index 384bd4c..487e74a 100644 --- a/src/battery.cpp +++ b/src/battery.cpp @@ -150,12 +150,10 @@ bool battery_conf_check(battery_conf_t *bat_conf) bat_conf->voltage_trickle > bat_conf->voltage_load_disconnect)) ); } - -void battery_conf_overwrite(battery_conf_t *source, battery_conf_t *destination) +void battery_conf_overwrite(battery_conf_t *source, battery_conf_t *destination, battery_state_t *bat_state) { // TODO: stop DC/DC - destination->nominal_capacity = source->nominal_capacity; destination->voltage_max = source->voltage_max; destination->voltage_recharge = source->voltage_recharge; destination->voltage_load_reconnect = source->voltage_load_reconnect; @@ -169,6 +167,16 @@ void battery_conf_overwrite(battery_conf_t *source, battery_conf_t *destination) destination->time_trickle_recharge = source->time_trickle_recharge; destination->temperature_compensation = source->temperature_compensation; + // reset Ah counter and SOH if battery nominal capacity was changed + if (destination->nominal_capacity != source->nominal_capacity) { + destination->nominal_capacity = source->nominal_capacity; + if (bat_state != NULL) { + bat_state->discharged_Ah = 0; + bat_state->usable_capacity = 0; + bat_state->soh = 0; + } + } + // TODO: // - update also DC/DC etc. (now this function only works at system startup) // - restart DC/DC diff --git a/src/battery.h b/src/battery.h index f048826..db0905c 100644 --- a/src/battery.h +++ b/src/battery.h @@ -24,6 +24,7 @@ #include #include +#include /** Battery cell types */ @@ -222,7 +223,7 @@ bool battery_conf_check(battery_conf_t *bat); * Settings specified in bat_user will be copied to actual battery_t, * if suggested updates are valid (includes plausibility check!) */ -void battery_conf_overwrite(battery_conf_t *source, battery_conf_t *destination); +void battery_conf_overwrite(battery_conf_t *source, battery_conf_t *destination, battery_state_t *state = NULL); /** Basic initialization of battery state (e.g. SOC) */ diff --git a/src/config.cpp_template b/src/config.cpp_template deleted file mode 100644 index 51be8e1..0000000 --- a/src/config.cpp_template +++ /dev/null @@ -1,70 +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. - */ - -/* This file includes your personal settings, e.g. which type of interface - * at the UEXT port should be used. - */ - -#include "bat_charger.h" - -/* Battery User Settings - * - * Specify battery parameters to overwrite default settings here. - * - * IMPORTANT: All settings (e.g. voltages) have to be defined on battery level, - * not cell level! - */ -void battery_init_user(battery_t *bat, battery_user_settings_t *bat_user) -{ - // bat_user was initialized in battery_init function already (based on - // config.h settings) --> only change the settings you want to adjust - - // DO NOT CHANGE ANYTHING in battery_t *bat! - -/* - bat_user.capacity = ?; // Ah Cell capacity or sum of parallel cells capacity - // Capacity is currently mainly used to define current limits, but might - // become important for improved SOC calculation algorithms - - // State: Standby - bat_user.voltage_recharge = ?; // V below this voltage start charging again after battery has been fully charged - // Remark: setting the value too close to the max voltage will cause more - // charging stress on lithium based batteries - - bat_user.voltage_absolute_min = ?; // V below this voltage the battery is considered damaged - - // State: CC/bulk - bat_user.charge_current_max = ?; // A limits the current if PCB allows more than the battery - - // State: CV/absorption - bat_user.voltage_max = ?; // V charger target voltage per cell, switching from CC to CV at this voltage - bat_user.current_cutoff_CV = ?; // A constant voltage charging phase stopped if current is below this value - bat_user.time_limit_CV = ?; // sec after this time limit, CV charging is stopped independent of current - - // State: float/trickle - bat_user.trickle_enabled = ?; - bat_user.voltage_trickle = ?; // V charger target voltage for trickle charging of lead-acid batteries - bat_user.time_trickle_recharge = ?; // sec - - bat_user.voltage_load_disconnect = ?; // V when discharging, stop power to load if battery voltage drops below this value - bat_user.voltage_load_reconnect = ?; // V re-enable the load only after charged beyond this value - - bat_user.temperature_compensation = ?; // voltage compensation (suggested: -3 mV/°C/cell) -*/ - - // try to update settings (checks input values for plausibility) - battery_user_settings(bat, bat_user); -} diff --git a/src/data_objects.cpp b/src/data_objects.cpp index ae4b7a1..f43b93e 100644 --- a/src/data_objects.cpp +++ b/src/data_objects.cpp @@ -197,7 +197,7 @@ void data_objects_update_conf() bool changed = false; if (battery_conf_check(&bat_conf_user)) { printf("New config valid and activated.\n"); - battery_conf_overwrite(&bat_conf_user, &bat_conf); + battery_conf_overwrite(&bat_conf_user, &bat_conf, &bat_state); changed = true; } else { @@ -216,7 +216,7 @@ void data_objects_read_eeprom() { eeprom_restore_data(); if (battery_conf_check(&bat_conf_user)) { - battery_conf_overwrite(&bat_conf_user, &bat_conf); + battery_conf_overwrite(&bat_conf_user, &bat_conf, &bat_state); } else { battery_conf_overwrite(&bat_conf, &bat_conf_user); diff --git a/src/eeprom.cpp b/src/eeprom.cpp index 51a967d..5d5881f 100644 --- a/src/eeprom.cpp +++ b/src/eeprom.cpp @@ -6,7 +6,7 @@ // versioning of EEPROM layout (2 bytes) // change the version number each time the data object array below is changed! -#define EEPROM_VERSION 2 +#define EEPROM_VERSION 3 #define EEPROM_HEADER_SIZE 8 // bytes @@ -17,6 +17,7 @@ extern ThingSet ts; // stores object-ids of values to be stored in EEPROM const uint16_t eeprom_data_objects[] = { 0x01, // timestamp + 0x18, // DeviceID 0x08, 0x09, 0x0A, 0x0B, // input / output wh 0x0C, 0x0D, 0x0E, // num full charge / deep-discharge / usable Ah 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x3F, // battery settings