Energy calculation for PWM CC fixed
- Correct negative sign for incoming solar current - Current multiplied with PWM duty cycle to get average current - Min/max calculation moved to log.cpp
This commit is contained in:
+9
-59
@@ -1,5 +1,5 @@
|
||||
/* LibreSolar MPPT charge controller firmware
|
||||
* Copyright (c) 2016-2018 Martin Jäger (www.libre.solar)
|
||||
/* LibreSolar charge controller firmware
|
||||
* Copyright (c) 2016-2019 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.
|
||||
@@ -95,15 +95,17 @@ void update_measurements(dcdc_t *dcdc, battery_state_t *bat, load_output_t *load
|
||||
// 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 =
|
||||
(float)(((adc_filtered[ADC_POS_V_BAT] >> (4 + ADC_FILTER_CONST)) * vcc) / 4096) *
|
||||
ADC_GAIN_V_BAT / 1000.0;
|
||||
|
||||
load->voltage = ls->voltage;
|
||||
load_bus->voltage = ls->voltage;
|
||||
|
||||
hs->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);
|
||||
#endif
|
||||
@@ -113,14 +115,13 @@ void update_measurements(dcdc_t *dcdc, battery_state_t *bat, load_output_t *load
|
||||
ADC_GAIN_I_LOAD / 1000.0 + load_current_offset;
|
||||
|
||||
load_bus->current = load->current;
|
||||
load_bus->voltage = ls->voltage;
|
||||
|
||||
/// \todo Multiply current with PWM duty cycle for PWM charger to get avg current.
|
||||
#ifdef CHARGER_TYPE_PWM
|
||||
hs->current =
|
||||
// current multiplied with PWM duty cycle for PWM charger to get avg current for correct power calculation
|
||||
hs->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->current;
|
||||
ADC_GAIN_I_SOLAR / 1000.0 + solar_current_offset);
|
||||
ls->current = -hs->current - load->current;
|
||||
#else // MPPT
|
||||
dcdc->ls_current =
|
||||
(float)(((adc_filtered[ADC_POS_I_DCDC] >> (4 + ADC_FILTER_CONST)) * vcc) / 4096) *
|
||||
@@ -129,7 +130,6 @@ void update_measurements(dcdc_t *dcdc, battery_state_t *bat, load_output_t *load
|
||||
hs->current = -dcdc->ls_current * ls->voltage / hs->voltage;
|
||||
#endif
|
||||
|
||||
|
||||
/** \todo Improved (faster) temperature calculation:
|
||||
https://www.embeddedrelated.com/showarticle/91.php
|
||||
*/
|
||||
@@ -160,56 +160,6 @@ void update_measurements(dcdc_t *dcdc, battery_state_t *bat, load_output_t *load
|
||||
uint16_t adcval = (adc_filtered[ADC_POS_TEMP_MCU] >> (4 + ADC_FILTER_CONST)) * vcc / VREFINT_VALUE;
|
||||
mcu_temp = (TSENSE_CAL2_VALUE - TSENSE_CAL1_VALUE) / (TSENSE_CAL2 - TSENSE_CAL1) * (adcval - TSENSE_CAL1) + TSENSE_CAL1_VALUE;
|
||||
//printf("TS_CAL1:%d TS_CAL2:%d ADC:%d, temp_int:%f\n", TS_CAL1, TS_CAL2, adcval, meas->temp_int);
|
||||
|
||||
if (ls->voltage > log_data.battery_voltage_max) {
|
||||
log_data.battery_voltage_max = ls->voltage;
|
||||
}
|
||||
|
||||
if (hs->voltage > log_data.solar_voltage_max) {
|
||||
log_data.solar_voltage_max = hs->voltage;
|
||||
}
|
||||
|
||||
if (solar_current_offset < 0.1) { // already calibrated
|
||||
if (ls->current > log_data.dcdc_current_max) {
|
||||
log_data.dcdc_current_max = ls->current;
|
||||
}
|
||||
|
||||
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) {
|
||||
log_data.mosfet_temp_max = dcdc->temp_mosfets;
|
||||
}
|
||||
|
||||
if (bat->temperature > log_data.bat_temp_max) {
|
||||
log_data.bat_temp_max = bat->temperature;
|
||||
}
|
||||
|
||||
if (mcu_temp > log_data.int_temp_max) {
|
||||
log_data.int_temp_max = mcu_temp;
|
||||
}
|
||||
}
|
||||
|
||||
void detect_battery_temperature(battery_state_t *bat, float bat_temp)
|
||||
|
||||
+1
-1
@@ -158,7 +158,7 @@ bool battery_conf_check(battery_conf_t *bat_conf)
|
||||
*/
|
||||
|
||||
return
|
||||
(bat_conf->voltage_load_reconnect > (bat_conf->voltage_load_disconnect + 0.6) &&
|
||||
(bat_conf->voltage_load_reconnect > (bat_conf->voltage_load_disconnect + 0.4) &&
|
||||
bat_conf->voltage_recharge < (bat_conf->voltage_topping - 0.4) &&
|
||||
bat_conf->voltage_recharge > (bat_conf->voltage_load_disconnect + 1) &&
|
||||
bat_conf->voltage_load_disconnect > (bat_conf->voltage_absolute_min + 0.4) &&
|
||||
|
||||
+55
-2
@@ -24,9 +24,11 @@
|
||||
#include <math.h> // for fabs function
|
||||
#include <stdio.h>
|
||||
|
||||
extern float mcu_temp;
|
||||
|
||||
//----------------------------------------------------------------------------
|
||||
// must be called exactly once per second, otherwise energy calculation gets wrong
|
||||
void log_update_energy(log_data_t *log_data, dc_bus_t *bat, dc_bus_t *solar, dc_bus_t *load)
|
||||
void log_update_energy(log_data_t *log_data, dc_bus_t *solar, dc_bus_t *bat, dc_bus_t *load)
|
||||
{
|
||||
// static variables so that it is not reset for each function call
|
||||
static int seconds_zero_solar = 0;
|
||||
@@ -62,5 +64,56 @@ void log_update_energy(log_data_t *log_data, dc_bus_t *bat, dc_bus_t *solar, dc_
|
||||
log_data->bat_chg_total_Wh = bat_chg_total_Wh_prev + (bat->chg_energy_Wh > 0 ? bat->chg_energy_Wh : 0);
|
||||
log_data->bat_dis_total_Wh = bat_dis_total_Wh_prev + (bat->dis_energy_Wh > 0 ? bat->dis_energy_Wh : 0);
|
||||
log_data->solar_in_total_Wh = solar_in_total_Wh_prev + (solar->dis_energy_Wh > 0 ? solar->dis_energy_Wh : 0);
|
||||
log_data->load_out_total_Wh = load_out_total_Wh_prev + (load->chg_energy_Wh > 0 ? load->dis_energy_Wh : 0);
|
||||
log_data->load_out_total_Wh = load_out_total_Wh_prev + (load->chg_energy_Wh > 0 ? load->chg_energy_Wh : 0);
|
||||
}
|
||||
|
||||
void log_update_min_max_values(log_data_t *log_data, dcdc_t *dcdc, battery_state_t *bat, load_output_t *load, dc_bus_t *solar_bus, dc_bus_t *bat_bus, dc_bus_t *load_bus)
|
||||
{
|
||||
if (bat_bus->voltage > log_data->battery_voltage_max) {
|
||||
log_data->battery_voltage_max = bat_bus->voltage;
|
||||
}
|
||||
|
||||
if (solar_bus->voltage > log_data->solar_voltage_max) {
|
||||
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 (load->current > log_data->load_current_max) {
|
||||
log_data->load_current_max = load->current;
|
||||
}
|
||||
|
||||
if (solar_bus->current < 0) {
|
||||
uint16_t solar_power = -solar_bus->voltage * solar_bus->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 = load->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) {
|
||||
log_data->mosfet_temp_max = dcdc->temp_mosfets;
|
||||
}
|
||||
|
||||
if (bat->temperature > log_data->bat_temp_max) {
|
||||
log_data->bat_temp_max = bat->temperature;
|
||||
}
|
||||
|
||||
if (mcu_temp > log_data->int_temp_max) {
|
||||
log_data->int_temp_max = mcu_temp;
|
||||
}
|
||||
}
|
||||
@@ -24,6 +24,8 @@
|
||||
*/
|
||||
|
||||
#include "dc_bus.h"
|
||||
#include "load.h"
|
||||
#include "dcdc.h"
|
||||
#include <stdbool.h>
|
||||
#include <stdint.h>
|
||||
|
||||
@@ -61,7 +63,12 @@ typedef struct {
|
||||
uint32_t error_flags; ///< Instantaneous errors
|
||||
} log_data_t;
|
||||
|
||||
/** Updates the total energy counters for solar, battery and load bus
|
||||
*/
|
||||
void log_update_energy(log_data_t *log_data, dc_bus_t *solar, dc_bus_t *bat, dc_bus_t *load);
|
||||
|
||||
void log_update_energy(log_data_t *log_data, dc_bus_t *bat, dc_bus_t *solar, dc_bus_t *load);
|
||||
/** Updates the logged min/max values for voltages, power, temperatures etc.
|
||||
*/
|
||||
void log_update_min_max_values(log_data_t *log_data, dcdc_t *dcdc, battery_state_t *bat, load_output_t *load, dc_bus_t *bat_bus, dc_bus_t *solar, dc_bus_t *load_bus);
|
||||
|
||||
#endif /* LOG_H */
|
||||
|
||||
+9
-3
@@ -1,5 +1,5 @@
|
||||
/* LibreSolar MPPT charge controller firmware
|
||||
* Copyright (c) 2016-2018 Martin Jäger (www.libre.solar)
|
||||
/* LibreSolar charge controller firmware
|
||||
* Copyright (c) 2016-2019 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.
|
||||
@@ -16,6 +16,11 @@
|
||||
|
||||
#ifndef UNIT_TEST
|
||||
|
||||
/** @file
|
||||
*
|
||||
* @brief Entry point of charge controller firmware
|
||||
*/
|
||||
|
||||
#include "mbed.h"
|
||||
|
||||
#include "thingset.h" // handles access to internal data via communication interfaces
|
||||
@@ -86,7 +91,8 @@ void system_control()
|
||||
dc_bus_energy_balance(&hs_bus);
|
||||
dc_bus_energy_balance(&ls_bus);
|
||||
dc_bus_energy_balance(&load_bus);
|
||||
log_update_energy(&log_data, &ls_bus, &hs_bus, &load_bus);
|
||||
log_update_energy(&log_data, &hs_bus, &ls_bus, &load_bus);
|
||||
log_update_min_max_values(&log_data, &dcdc, &bat_state, &load, &hs_bus, &ls_bus, &load_bus);
|
||||
battery_update_soc(&bat_conf, &bat_state, bat_bus->voltage, bat_bus->current);
|
||||
}
|
||||
counter++;
|
||||
|
||||
+5
-9
@@ -135,25 +135,21 @@ void pwm_switch_init(pwm_switch_t *pwm_switch)
|
||||
|
||||
void pwm_switch_control(pwm_switch_t *pwm_switch, dc_bus_t *solar_port, dc_bus_t *bat_port)
|
||||
{
|
||||
// for testing
|
||||
//solar_port->voltage_input_start = 14.0;
|
||||
//solar_port->voltage_input_stop = 13.0;
|
||||
if (_enabled) {
|
||||
if (bat_port->chg_allowed == false || solar_port->dis_allowed == false
|
||||
|| (solar_port->voltage < solar_port->dis_voltage_stop && bat_port->current < 0.1)
|
||||
|| (solar_port->voltage < solar_port->dis_voltage_stop && solar_port->current > 0)
|
||||
|| pwm_switch->enabled == false)
|
||||
{
|
||||
pwm_switch_stop();
|
||||
pwm_switch->off_timestamp = time(NULL);
|
||||
printf("PWM charger stop.\n");
|
||||
}
|
||||
else if (bat_port->voltage > (bat_port->chg_voltage_target - bat_port->chg_droop_res * bat_port->current) // output voltage above target
|
||||
|| bat_port->current > bat_port->chg_current_max // output current limit exceeded
|
||||
//|| (solar_port->voltage < (solar_port->voltage_input_start - solar_port->droop_resistance * solar_port->current) && bat_port->current > 0.1) // input voltage below limit
|
||||
|| solar_port->current < solar_port->dis_current_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
|
||||
|| solar_port->current < solar_port->dis_current_max) // input current (negative signs) above limit
|
||||
{
|
||||
pwm_switch_duty_cycle_step(-1);
|
||||
if (pwm_switch_get_duty_cycle() > 0.9)
|
||||
pwm_switch_duty_cycle_step(-1);
|
||||
}
|
||||
else {
|
||||
pwm_switch_duty_cycle_step(1);
|
||||
|
||||
+12
-4
@@ -25,6 +25,7 @@
|
||||
|
||||
#include "half_bridge.h"
|
||||
#include "dcdc.h"
|
||||
#include "pwm_switch.h"
|
||||
#include "dc_bus.h"
|
||||
#include "battery.h"
|
||||
#include "load.h"
|
||||
@@ -97,8 +98,8 @@ void uext_process_1s()
|
||||
oled.drawRect(66, 4, 2, 5, 1); // bar 5
|
||||
|
||||
// solar panel data
|
||||
if (half_bridge_enabled()) {
|
||||
tmp = -hs_bus.voltage * hs_bus.current;
|
||||
if (half_bridge_enabled() || pwm_switch_enabled()) {
|
||||
tmp = - hs_bus.voltage * hs_bus.current;
|
||||
oled.setTextCursor(0, 18);
|
||||
oled.printf("%4.0fW", (abs(tmp) < 1) ? 0 : tmp); // remove negative zeros
|
||||
}
|
||||
@@ -106,10 +107,13 @@ void uext_process_1s()
|
||||
oled.setTextCursor(8, 18);
|
||||
oled.printf("n/a");
|
||||
}
|
||||
//if (solar_port->voltage > bat_port->voltage) {
|
||||
#ifndef CHARGER_TYPE_PWM
|
||||
if (hs_bus.voltage > ls_bus.voltage)
|
||||
#endif
|
||||
{
|
||||
oled.setTextCursor(0, 26);
|
||||
oled.printf("%4.1fV", hs_bus.voltage);
|
||||
//}
|
||||
}
|
||||
|
||||
// battery data
|
||||
tmp = ls_bus.voltage * ls_bus.current;
|
||||
@@ -130,7 +134,11 @@ void uext_process_1s()
|
||||
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);
|
||||
#ifdef CHARGER_TYPE_PWM
|
||||
oled.printf("T %.0fC PWM %.0f%% SOC %d%%", dcdc.temp_mosfets, pwm_switch_get_duty_cycle() * 100.0, bat_state.soc);
|
||||
#else
|
||||
oled.printf("T %.0fC PWM %.0f%% SOC %d%%", dcdc.temp_mosfets, half_bridge_get_duty_cycle() * 100.0, bat_state.soc);
|
||||
#endif
|
||||
|
||||
oled.display();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user