Update master branch with new features from development branch (#20)
* Major changes in DC/DC control and board support + ThingSet library added * Small platformio.ini fixes - added libignore for mbed-USBDevice to all STM32F072 builds - add map file generation. Use text editor or AMAP (http://www.sikorskiy.net/prj/amap/) to view and learn where the precious flash and ram goes to. - fixed board define for LS 0.10 board * Travis fix and more - Now runs only a selected subset of all configurations (all STM32F072/L073) - Added a src_filter to the cloudsolar_03 since L073 does not have CAN. Maybe there is a better way to do this. Also added libignore for USB to this environment. - Added memory.map to .gitignore * STML0 support improved + small changes - STM32L0 now widely supported: PWM signal generation, ADC via DMA and display via I2C are working - Old files for USB support (only F0) included into repository, as files in original mbed repository don't compile. - CAN bus support only for F0 (added header defines to allow compilation) * Cell & Pack Config refactoring: First work on #13. Reorganized battery init a bit to get a better idea of how to split things. All known chemistries are now supported with safe values (please check the values). Added data for NMC (aka "normal" LiIo cells such as most 18650) A configuration now declares itself to be valid or not after battery_init. BAT_TYPE_NONE (formerly BAT_TYPE_DEFAULT, which equals 0) will not pass check and thus is an invalid configuration, as a result an unconfigured battery_t struct will not be considered valid by accident. We should not continue to start normal operation if a battery configuration is not valid. However, this is not yet implemented. Also slightly changed interface to battery_init, now we pass the number of cells. Moved the static pub_enabled into main.cpp to get ride of a ton of "unused static variable" warnings. * ThingSet moved to separate library * Removed serious bug in PWM signal generation resulting in PWM with 0% duty cycle * LED control via PWM for STM32L0 board added * Gets rid of USB deprecated warnings in application code This simply disables any warning regarding the use of deprecated internals in mbed for now. Since the "new" USBDevice code does not run on F0 or L0, we use old one which works but generates warnings a lot. If they ever remove the deprecated interface, we'll know sure enough since then code will no longer compile. * More battery init refactoring In addition to the mandatory arguments cell type, number in series and capacity, the code now accepts a partially configure battery_t structure (you provide various voltage levels and charge currents) for Lithium based cells, checks the configuration values for sanity and tries to adjust if outside valid operational limits. The defaults try to balance between charging stress and capacity loss. Charge is by default to about 90% maximum full charge, and discharge stops around 20%-30%. For lead acid cells all voltage levels and currents are set / calculated based on the mandatory parameters, no configuration of voltage levels possible. We may change that, but I don't have enough no knowledge about valid ranges etc. I added a high voltage lithium chemistry for cells which have typically 4.35 voltage limit (vs. 4.2 normally). * Added user configuration for battery and system operation mode Copy the most relevant template to config.cpp and then compile. config.cpp is intentionally not part of the git managed source code since it represents the user specific configuration part which varies across different firmware builds. * Turn off MBED Deprecated Warning for Old USB Stack attempt 2 This time cross platform compatible... And also fix the travis build issue * Boost mode added and first updates for timer-based DC/DC control - system_mode now distinguishes between MPPT_BOOST and MPPT_BUCK - new function system_control() which does all important and fast control stuff necessary for the DC/DC (will be included into interrupt in the future) - small correction in _dcdc_output_control() algorithm after test with e-bike battery solar charging * Naming of structs and enums adjusted * Documentation amended and separate docs folder created. * UNIT_TEST defines removed * Timer for DC/DC control implemented, ADC timer updated * Files restructured / renamed - config.h is now pcb.h and contains only hardware-specific settings - anything which communicates to the outside world (display, serial interface, etc) is now renamed to more generic "interface" instead of "output" and "communication". Use interface_<DEVICE>.cpp for implementations of specific interfaces. * Updates in user configuration and interfaces - Battery configuration can now be adjusted in battery_init_user in config.cpp - New function battery_update_settings implemented to check new battery configuration for plausibility (also needed for later user-configuration via serial interface) - Templates for GSM and LoRa interfaces created - Restructuring of main to allow different interface configurations * Travis CI adjusted to new config.h * Load output templates added * ThingSet library updated, interface processing updated, publication functions added * Load output implementation updated and moved to load.cpp/h * Energy calculation updated and eTotal changed to uint32_t * Update firmware.md * Issue monitor_speed typing mistake We found a typing issue: "monitor_speed" is mistyped as "monitor_port". * Major updates of recent months
This commit is contained in:
@@ -0,0 +1,386 @@
|
||||
/* 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.
|
||||
*/
|
||||
|
||||
#include "bat_charger.h"
|
||||
#include "config.h"
|
||||
#include "pcb.h"
|
||||
#include "half_bridge.h"
|
||||
#include "load.h"
|
||||
|
||||
#include <time.h> // for time(NULL) function
|
||||
#include <math.h> // for fabs function
|
||||
|
||||
|
||||
extern load_output_t load; // ToDo: Remove global definitions!
|
||||
extern dcdc_port_t ls_port;
|
||||
extern dcdc_port_t hs_port;
|
||||
extern log_data_t log_data;
|
||||
extern dcdc_t dcdc;
|
||||
|
||||
// private function
|
||||
void _enter_state(battery_t* bat, int next_state)
|
||||
{
|
||||
//printf("Enter State: %d\n", next_state);
|
||||
bat->time_state_changed = time(NULL);
|
||||
bat->state = next_state;
|
||||
}
|
||||
|
||||
// weak function to be overridden by user in config.cpp
|
||||
WEAK void battery_init_user(battery_t *bat, battery_user_settings_t *bat_user) {;}
|
||||
|
||||
void battery_init(battery_t *bat, battery_user_settings_t *bat_user)
|
||||
{
|
||||
// core battery pack specification (set in config.h)
|
||||
bat->type = BATTERY_TYPE;
|
||||
bat->num_cells = BATTERY_NUM_CELLS;
|
||||
bat->nominal_capacity = BATTERY_CAPACITY;
|
||||
bat->num_batteries = 1; // initialize with only one battery in series
|
||||
bat->soh = 100; // assume new battery
|
||||
|
||||
// common values for all batteries
|
||||
bat->charge_current_max = bat->nominal_capacity; // 1C should be safe for all batteries
|
||||
|
||||
bat->time_limit_recharge = 60; // sec
|
||||
bat->time_limit_CV = 120*60; // sec
|
||||
|
||||
switch (bat->type)
|
||||
{
|
||||
case BAT_TYPE_FLOODED:
|
||||
case BAT_TYPE_AGM:
|
||||
case BAT_TYPE_GEL:
|
||||
bat->cell_voltage_max = 2.4;
|
||||
bat->cell_voltage_recharge = 2.3;
|
||||
|
||||
bat->cell_voltage_load_disconnect = 1.95;
|
||||
bat->cell_voltage_load_reconnect = 2.1;
|
||||
bat->cell_voltage_absolute_min = 1.8;
|
||||
|
||||
bat->cell_ocv_full = 2.2;
|
||||
bat->cell_ocv_empty = 1.9;
|
||||
|
||||
// https://batteryuniversity.com/learn/article/charging_the_lead_acid_battery
|
||||
bat->current_cutoff_CV = bat->nominal_capacity * 0.04; // 3-5 % of C/1
|
||||
|
||||
bat->trickle_enabled = true;
|
||||
bat->time_trickle_recharge = 30*60;
|
||||
// http://batteryuniversity.com/learn/article/charging_the_lead_acid_battery
|
||||
bat->cell_voltage_trickle = (bat->type == BAT_TYPE_FLOODED) ? 2.25 : 2.3;
|
||||
|
||||
bat->equalization_enabled = false;
|
||||
bat->cell_voltage_equalization = 2.5;
|
||||
bat->time_limit_equalization = 60*60;
|
||||
bat->current_limit_equalization = (1.0 /7.0) * bat->nominal_capacity;
|
||||
bat->equalization_trigger_time = 8; // weeks
|
||||
bat->equalization_trigger_deep_cycles = 10; // times
|
||||
|
||||
// not yet implemented...
|
||||
bat->temperature_compensation = -0.003; // -3 mV/°C/cell
|
||||
break;
|
||||
|
||||
case BAT_TYPE_LFP:
|
||||
bat->cell_voltage_max = 3.55; // CV voltage
|
||||
bat->cell_voltage_recharge = 3.35;
|
||||
|
||||
bat->cell_voltage_load_disconnect = 3.0;
|
||||
bat->cell_voltage_load_reconnect = 3.15;
|
||||
bat->cell_voltage_absolute_min = 2.0;
|
||||
|
||||
bat->cell_ocv_full = 3.4; // will give really bad SOC calculation
|
||||
bat->cell_ocv_empty = 3.0; // because of flat OCV of LFP cells...
|
||||
|
||||
bat->current_cutoff_CV = bat->nominal_capacity / 10; // C/10 cut-off at end of CV phase by default
|
||||
|
||||
bat->trickle_enabled = false;
|
||||
bat->equalization_enabled = false;
|
||||
bat->temperature_compensation = 0.0;
|
||||
break;
|
||||
|
||||
case BAT_TYPE_NMC:
|
||||
case BAT_TYPE_NMC_HV:
|
||||
bat->cell_voltage_max = (bat->type == BAT_TYPE_NMC_HV) ? 4.35 : 4.20;
|
||||
bat->cell_voltage_recharge = 3.9;
|
||||
|
||||
bat->cell_voltage_load_disconnect = 3.3;
|
||||
bat->cell_voltage_load_reconnect = 3.6;
|
||||
bat->cell_voltage_absolute_min = 2.5;
|
||||
|
||||
bat->cell_ocv_full = 4.0;
|
||||
bat->cell_ocv_empty = 3.0;
|
||||
|
||||
bat->current_cutoff_CV = bat->nominal_capacity / 10; // C/10 cut-off at end of CV phase by default
|
||||
|
||||
bat->trickle_enabled = false;
|
||||
bat->equalization_enabled = false;
|
||||
bat->temperature_compensation = 0.0;
|
||||
break;
|
||||
|
||||
case BAT_TYPE_NONE:
|
||||
case BAT_TYPE_CUSTOM:
|
||||
break;
|
||||
}
|
||||
|
||||
// initialize bat_user
|
||||
bat_user->nominal_capacity = bat->nominal_capacity;
|
||||
bat_user->voltage_max = bat->num_cells * bat->cell_voltage_max;
|
||||
bat_user->voltage_recharge = bat->num_cells * bat->cell_voltage_recharge;
|
||||
bat_user->voltage_load_reconnect = bat->num_cells * bat->cell_voltage_load_reconnect;
|
||||
bat_user->voltage_load_disconnect = bat->num_cells * bat->cell_voltage_load_disconnect;
|
||||
bat_user->voltage_absolute_min = bat->num_cells * bat->cell_voltage_absolute_min;
|
||||
bat_user->charge_current_max = bat->charge_current_max;
|
||||
bat_user->current_cutoff_CV = bat->current_cutoff_CV;
|
||||
bat_user->time_limit_CV = bat->time_limit_CV;
|
||||
bat_user->trickle_enabled = bat->trickle_enabled;
|
||||
bat_user->time_trickle_recharge = bat->time_trickle_recharge;
|
||||
bat_user->temperature_compensation = bat->temperature_compensation;
|
||||
|
||||
// call user function (defined in config.cpp to overwrite above initializations)
|
||||
battery_init_user(bat, bat_user);
|
||||
}
|
||||
|
||||
// checks settings in bat_user for plausibility and writes them to bat if everything is fine
|
||||
bool battery_user_settings(battery_t *bat, battery_user_settings_t *bat_user)
|
||||
{
|
||||
// things to check:
|
||||
// - cell_voltage_XXX in the range of what the charger can handle?
|
||||
// - load_disconnect/reconnect hysteresis makes sense?
|
||||
// - cutoff current not extremely low/high
|
||||
// - capacity plausible
|
||||
|
||||
if (bat_user->voltage_max > LOW_SIDE_VOLTAGE_MAX &&
|
||||
bat_user->voltage_load_reconnect > (bat_user->voltage_load_disconnect + 0.8) &&
|
||||
bat_user->voltage_recharge < (bat_user->voltage_max - 0.6) &&
|
||||
bat_user->voltage_recharge > (bat_user->voltage_load_disconnect + 1) &&
|
||||
bat_user->voltage_load_disconnect > (bat_user->voltage_absolute_min + 0.5) &&
|
||||
bat_user->current_cutoff_CV < (bat_user->nominal_capacity / 9.0) && // C/10 or lower allowed
|
||||
bat_user->current_cutoff_CV > 0.01 &&
|
||||
(bat_user->trickle_enabled == false ||
|
||||
(bat_user->voltage_trickle < bat_user->voltage_max &&
|
||||
bat_user->voltage_trickle > bat_user->voltage_load_disconnect))
|
||||
) {
|
||||
|
||||
// TODO: stop DC/DC
|
||||
|
||||
bat->type = BAT_TYPE_CUSTOM;
|
||||
bat->num_cells = 1; // custom battery defines voltages on battery level
|
||||
bat->nominal_capacity = bat_user->nominal_capacity;
|
||||
bat->cell_voltage_max = bat_user->voltage_max;
|
||||
bat->cell_voltage_recharge = bat_user->voltage_recharge;
|
||||
bat->cell_voltage_load_reconnect = bat_user->voltage_load_reconnect;
|
||||
bat->cell_voltage_load_disconnect = bat_user->voltage_load_disconnect;
|
||||
bat->cell_voltage_absolute_min = bat_user->voltage_absolute_min;
|
||||
bat->charge_current_max = bat_user->charge_current_max;
|
||||
bat->current_cutoff_CV = bat_user->current_cutoff_CV;
|
||||
bat->time_limit_CV = bat_user->time_limit_CV;
|
||||
bat->trickle_enabled = bat_user->trickle_enabled;
|
||||
bat->time_trickle_recharge = bat_user->time_trickle_recharge;
|
||||
bat->temperature_compensation = bat_user->temperature_compensation;
|
||||
|
||||
// TODO:
|
||||
// - update also DC/DC etc. (now this function only works at system startup)
|
||||
// - restart DC/DC
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/*****************************************************************************
|
||||
* 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(dcdc_port_t *port, battery_t *bat, float voltage, float current)
|
||||
{
|
||||
//printf("time_state_change = %d, time = %d, v_bat = %f, i_bat = %f\n", bat->time_state_changed, time(NULL), voltage, current);
|
||||
|
||||
// Load management
|
||||
// battery port input state (i.e. battery discharging direction) defines load state
|
||||
if (port->input_allowed == true &&
|
||||
voltage < bat->cell_voltage_load_disconnect * bat->num_cells)
|
||||
{
|
||||
port->input_allowed = false;
|
||||
bat->num_deep_discharges++;
|
||||
|
||||
if (bat->useable_capacity < 0.1) {
|
||||
bat->useable_capacity = bat->discharged_Ah;
|
||||
} else {
|
||||
// slowly adapt new measurements with low-pass filter
|
||||
bat->useable_capacity = 0.8 * bat->useable_capacity + 0.2 * bat->discharged_Ah;
|
||||
}
|
||||
|
||||
// simple SOH estimation
|
||||
bat->soh = bat->useable_capacity / bat->nominal_capacity;
|
||||
}
|
||||
if (voltage >= bat->cell_voltage_load_reconnect * bat->num_cells) {
|
||||
port->input_allowed = true;
|
||||
}
|
||||
|
||||
// state machine
|
||||
switch (bat->state) {
|
||||
case CHG_STATE_IDLE: {
|
||||
if (voltage < bat->num_cells * bat->cell_voltage_recharge
|
||||
&& (time(NULL) - bat->time_state_changed) > bat->time_limit_recharge)
|
||||
{
|
||||
port->voltage_output_target = bat->num_cells * bat->cell_voltage_max;
|
||||
port->current_output_max = bat->charge_current_max;
|
||||
port->output_allowed = true;
|
||||
bat->full = false;
|
||||
_enter_state(bat, CHG_STATE_CC);
|
||||
}
|
||||
break;
|
||||
}
|
||||
case CHG_STATE_CC: {
|
||||
if (voltage > port->voltage_output_target) {
|
||||
port->voltage_output_target = bat->num_cells * bat->cell_voltage_max;
|
||||
_enter_state(bat, CHG_STATE_CV);
|
||||
}
|
||||
break;
|
||||
}
|
||||
case CHG_STATE_CV: {
|
||||
if (voltage >= port->voltage_output_target) {
|
||||
bat->time_voltage_limit_reached = time(NULL);
|
||||
}
|
||||
|
||||
// cut-off limit reached because battery full (i.e. CV mode still
|
||||
// reached by available solar power within last 2s) or CV period long enough?
|
||||
if ((current < bat->current_cutoff_CV && (time(NULL) - bat->time_voltage_limit_reached) < 2)
|
||||
|| (time(NULL) - bat->time_state_changed) > bat->time_limit_CV)
|
||||
{
|
||||
bat->full = true;
|
||||
bat->num_full_charges++;
|
||||
bat->discharged_Ah = 0; // reset coulomb counter
|
||||
|
||||
if (bat->equalization_enabled) {
|
||||
// TODO: additional conditions!
|
||||
port->voltage_output_target = bat->num_cells * bat->cell_voltage_equalization;
|
||||
port->current_output_max = bat->current_limit_equalization;
|
||||
_enter_state(bat, CHG_STATE_EQUALIZATION);
|
||||
}
|
||||
else if (bat->trickle_enabled) {
|
||||
port->voltage_output_target = bat->num_cells * bat->cell_voltage_trickle;
|
||||
_enter_state(bat, CHG_STATE_TRICKLE);
|
||||
}
|
||||
else {
|
||||
port->current_output_max = 0;
|
||||
port->output_allowed = false;
|
||||
_enter_state(bat, CHG_STATE_IDLE);
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
case CHG_STATE_TRICKLE: {
|
||||
if (voltage >= port->voltage_output_target) {
|
||||
bat->time_voltage_limit_reached = time(NULL);
|
||||
}
|
||||
|
||||
if (time(NULL) - bat->time_voltage_limit_reached > bat->time_trickle_recharge)
|
||||
{
|
||||
port->current_output_max = bat->charge_current_max;
|
||||
port->voltage_output_target = bat->num_cells * bat->cell_voltage_max;
|
||||
bat->full = false;
|
||||
_enter_state(bat, CHG_STATE_CC);
|
||||
}
|
||||
// assumtion: trickle does not harm the battery --> never go back to idle
|
||||
// (for Li-ion battery: disable trickle!)
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//----------------------------------------------------------------------------
|
||||
// must be called exactly once per second, otherwise energy calculation gets wrong
|
||||
void battery_update_energy(battery_t *bat, float voltage, float current)
|
||||
{
|
||||
// static variables so that the are not reset during each function call
|
||||
static int seconds_zero_solar = 0;
|
||||
static int soc_filtered = 0; // SOC / 100 for better filtering
|
||||
|
||||
// stores the input/output energy status of previous day and to add
|
||||
// xxx_Wh_day only once per day and increase accuracy
|
||||
static uint32_t input_Wh_total_prev = bat->input_Wh_total;
|
||||
static uint32_t output_Wh_total_prev = bat->output_Wh_total;
|
||||
|
||||
if (hs_port.voltage < ls_port.voltage) {
|
||||
seconds_zero_solar += 1;
|
||||
}
|
||||
else {
|
||||
// solar voltage > battery voltage after 5 hours of night time means sunrise in the morning
|
||||
// --> reset daily energy counters
|
||||
if (seconds_zero_solar > 60*60*5) {
|
||||
//printf("Night!\n");
|
||||
log_data.day_counter++;
|
||||
input_Wh_total_prev = bat->input_Wh_total;
|
||||
output_Wh_total_prev = bat->output_Wh_total;
|
||||
bat->input_Wh_day = 0.0;
|
||||
bat->output_Wh_day = 0.0;
|
||||
}
|
||||
seconds_zero_solar = 0;
|
||||
}
|
||||
|
||||
bat->input_Wh_day += voltage * current / 3600.0; // timespan = 1s, so no multiplication with time
|
||||
bat->output_Wh_day += load.current * ls_port.voltage / 3600.0;
|
||||
bat->input_Wh_total = input_Wh_total_prev + (bat->input_Wh_day > 0 ? bat->input_Wh_day : 0);
|
||||
bat->output_Wh_total = output_Wh_total_prev + (bat->output_Wh_day > 0 ? bat->output_Wh_day : 0);
|
||||
bat->discharged_Ah += (load.current - current) / 3600.0;
|
||||
|
||||
if (fabs(current) < 0.2) {
|
||||
int soc_new = (int)((voltage / bat->num_cells - bat->cell_ocv_empty) /
|
||||
(bat->cell_ocv_full - bat->cell_ocv_empty) * 10000.0);
|
||||
|
||||
if (soc_new > 10000) {
|
||||
soc_new = 10000;
|
||||
}
|
||||
else if (soc_new < 0) {
|
||||
soc_new = 0;
|
||||
}
|
||||
|
||||
if (soc_new > 500 && soc_filtered == 0) {
|
||||
// bypass filter during initialization
|
||||
soc_filtered = soc_new;
|
||||
}
|
||||
else {
|
||||
// filtering to adjust SOC very slowly
|
||||
soc_filtered += (soc_new - soc_filtered) / 100;
|
||||
}
|
||||
bat->soc = soc_filtered / 100;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user