DC bus type introduced and unit tests added
This commit is contained in:
@@ -1,6 +1,8 @@
|
||||
# Libre Solar Charge Controller Firmware
|
||||
|
||||
Software based on ARM mbed framework for the Libre Solar MPPT/PWM solar charge controllers
|
||||

|
||||
|
||||
Firmware based on ARM mbed framework for the Libre Solar MPPT/PWM solar charge controllers
|
||||
|
||||
## Supported devices
|
||||
|
||||
@@ -35,6 +37,12 @@ See the Libre Solar website for a detailed instruction how to [develop software]
|
||||
|
||||
The documentation auto-generated by Doxygen can be found [here](https://libre.solar/charge-controller-firmware/).
|
||||
|
||||
## Unit tests
|
||||
|
||||
In order to run the unit tests, you need a PlatformIO Plus account. Run tests with the following command:
|
||||
|
||||
platformio test -e unit_test_native
|
||||
|
||||
## Additional firmware documentation (docs folder)
|
||||
|
||||
- [MPPT charger firmware details](docs/firmware.md)
|
||||
|
||||
+29
-19
@@ -15,16 +15,15 @@
|
||||
*/
|
||||
|
||||
#ifndef UNIT_TEST
|
||||
|
||||
#include "mbed.h"
|
||||
#include "config.h"
|
||||
#endif
|
||||
|
||||
// don't use this file during processor-in-the-loop tests
|
||||
#ifndef PIL_TESTING
|
||||
#include "config.h"
|
||||
|
||||
#include "adc_dma.h"
|
||||
#include "pcb.h" // contains defines for pins
|
||||
#include <math.h> // log for thermistor calculation
|
||||
#include <stdio.h>
|
||||
#include "log.h"
|
||||
#include "pwm_switch.h"
|
||||
|
||||
@@ -43,6 +42,13 @@
|
||||
const uint16_t TSENSE_CAL2 = *((uint16_t *)0x1FF8007E);
|
||||
#define TSENSE_CAL1_VALUE 30.0 // temperature of first calibration point
|
||||
#define TSENSE_CAL2_VALUE 130.0 // temperature of second calibration point
|
||||
#elif defined(UNIT_TEST)
|
||||
#define VREFINT_CAL (4096 * 1.224 / 3.0) // VREFINT @3.0V/25°C
|
||||
#define VREFINT_VALUE 3000 // mV
|
||||
#define TSENSE_CAL1 (4096.0 * (670 - 161) / 3300) // datasheet: slope 1.61 mV/°C
|
||||
#define TSENSE_CAL2 (4096.0 * 670 / 3300) // datasheet: 670 mV
|
||||
#define TSENSE_CAL1_VALUE 30.0 // temperature of first calibration point
|
||||
#define TSENSE_CAL2_VALUE 130.0 // temperature of second calibration point
|
||||
#endif
|
||||
|
||||
#ifdef PIN_REF_I_DCDC
|
||||
@@ -53,7 +59,7 @@ AnalogOut ref_i_dcdc(PIN_REF_I_DCDC);
|
||||
DigitalInOut temp_pd(PIN_TEMP_INT_PD);
|
||||
#endif
|
||||
|
||||
float dcdc_current_offset;
|
||||
float solar_current_offset;
|
||||
float load_current_offset;
|
||||
|
||||
// for ADC and DMA
|
||||
@@ -61,20 +67,22 @@ volatile uint16_t adc_readings[NUM_ADC_CH] = {0};
|
||||
volatile uint32_t adc_filtered[NUM_ADC_CH] = {0};
|
||||
//volatile int num_adc_conversions;
|
||||
|
||||
#define ADC_FILTER_CONST 5 // filter multiplier = 1/(2^ADC_FILTER_CONST)
|
||||
|
||||
extern Serial serial;
|
||||
extern log_data_t log_data;
|
||||
extern float mcu_temp;
|
||||
|
||||
void calibrate_current_sensors(dcdc_t *dcdc, load_output_t *load)
|
||||
void calibrate_current_sensors()
|
||||
{
|
||||
dcdc_current_offset = -dcdc->ls_current;
|
||||
load_current_offset = -load->current;
|
||||
int vcc = VREFINT_VALUE * VREFINT_CAL / (adc_filtered[ADC_POS_VREF_MCU] >> (4 + ADC_FILTER_CONST));
|
||||
#ifdef CHARGER_TYPE_PWM
|
||||
solar_current_offset = -(float)(((adc_filtered[ADC_POS_I_SOLAR] >> (4 + ADC_FILTER_CONST)) * vcc) / 4096) * ADC_GAIN_I_SOLAR / 1000.0;
|
||||
#else
|
||||
solar_current_offset = -(float)(((adc_filtered[ADC_POS_I_DCDC] >> (4 + ADC_FILTER_CONST)) * vcc) / 4096) * ADC_GAIN_I_DCDC / 1000.0;
|
||||
#endif
|
||||
load_current_offset = -(float)(((adc_filtered[ADC_POS_I_LOAD] >> (4 + ADC_FILTER_CONST)) * vcc) / 4096) * ADC_GAIN_I_LOAD / 1000.0;
|
||||
}
|
||||
|
||||
//----------------------------------------------------------------------------
|
||||
void update_measurements(dcdc_t *dcdc, battery_state_t *bat, load_output_t *load, power_port_t *hs, power_port_t *ls)
|
||||
void update_measurements(dcdc_t *dcdc, battery_state_t *bat, load_output_t *load, dc_bus_t *hs, dc_bus_t *ls, dc_bus_t *load_bus)
|
||||
{
|
||||
//int v_temp, rts;
|
||||
|
||||
@@ -104,16 +112,19 @@ void update_measurements(dcdc_t *dcdc, battery_state_t *bat, load_output_t *load
|
||||
(float)(((adc_filtered[ADC_POS_I_LOAD] >> (4 + ADC_FILTER_CONST)) * vcc) / 4096) *
|
||||
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 =
|
||||
(float)(((adc_filtered[ADC_POS_I_SOLAR] >> (4 + ADC_FILTER_CONST)) * vcc) / 4096) *
|
||||
ADC_GAIN_I_SOLAR / 1000.0 + dcdc_current_offset;
|
||||
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) *
|
||||
ADC_GAIN_I_DCDC / 1000.0 + dcdc_current_offset;
|
||||
ADC_GAIN_I_DCDC / 1000.0 + solar_current_offset;
|
||||
ls->current = dcdc->ls_current - load->current;
|
||||
hs->current = -dcdc->ls_current * ls->voltage / hs->voltage;
|
||||
#endif
|
||||
@@ -158,7 +169,7 @@ void update_measurements(dcdc_t *dcdc, battery_state_t *bat, load_output_t *load
|
||||
log_data.solar_voltage_max = hs->voltage;
|
||||
}
|
||||
|
||||
if (dcdc_current_offset < 0.1) { // already calibrated
|
||||
if (solar_current_offset < 0.1) { // already calibrated
|
||||
if (ls->current > log_data.dcdc_current_max) {
|
||||
log_data.dcdc_current_max = ls->current;
|
||||
}
|
||||
@@ -255,6 +266,7 @@ void detect_battery_temperature(battery_state_t *bat, float bat_temp)
|
||||
#endif
|
||||
}
|
||||
|
||||
#ifndef UNIT_TEST
|
||||
|
||||
void dma_setup()
|
||||
{
|
||||
@@ -400,6 +412,8 @@ void adc_setup()
|
||||
ADC->CCR |= ADC_CCR_TSEN | ADC_CCR_VREFEN;
|
||||
}
|
||||
|
||||
#endif // UNIT_TEST
|
||||
|
||||
#if defined(STM32F0)
|
||||
|
||||
void adc_timer_start(int freq_Hz) // max. 10 kHz
|
||||
@@ -463,7 +477,3 @@ extern "C" void TIM6_IRQHandler(void)
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
#endif /* TESTING_PIL */
|
||||
|
||||
#endif /* UNIT_TEST */
|
||||
|
||||
+4
-2
@@ -26,11 +26,13 @@
|
||||
#include "load.h"
|
||||
#include "battery.h"
|
||||
|
||||
#define ADC_FILTER_CONST 5 // filter multiplier = 1/(2^ADC_FILTER_CONST)
|
||||
|
||||
/** Sets offset to actual measured value, i.e. sets zero current point.
|
||||
*
|
||||
* All input/output switches and consumers should be switched off before calling this function
|
||||
*/
|
||||
void calibrate_current_sensors(dcdc_t *dcdc, load_output_t *load);
|
||||
void calibrate_current_sensors();
|
||||
|
||||
/** Detects if external temperature sensor is attached, otherwise takes internal sensor
|
||||
*/
|
||||
@@ -38,7 +40,7 @@ void detect_battery_temperature(battery_state_t *bat, float bat_temp);
|
||||
|
||||
/** Updates structures with data read from ADC
|
||||
*/
|
||||
void update_measurements(dcdc_t *dcdc, battery_state_t *bat, load_output_t *load, power_port_t *hs, power_port_t *ls);
|
||||
void update_measurements(dcdc_t *dcdc, battery_state_t *bat, load_output_t *load, dc_bus_t *hs, dc_bus_t *ls, dc_bus_t *load_bus);
|
||||
|
||||
/** Initializes registers and starts ADC timer
|
||||
*/
|
||||
|
||||
@@ -1,130 +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.
|
||||
*/
|
||||
|
||||
#ifndef UNIT_TEST
|
||||
|
||||
#include "mbed.h"
|
||||
#include "config.h"
|
||||
|
||||
#ifdef PIL_TESTING // processor-in-the-loop test
|
||||
|
||||
#include "adc_dma.h"
|
||||
#include "pcb.h" // contains defines for pins
|
||||
#include <math.h> // log for thermistor calculation
|
||||
#include "log.h"
|
||||
#include "pwm_switch.h"
|
||||
|
||||
#include "pil_test.h"
|
||||
|
||||
extern Serial serial;
|
||||
extern log_data_t log_data;
|
||||
extern float mcu_temp;
|
||||
|
||||
extern pil_test_data_t sim_data;
|
||||
bool sim_data_initialized = false;
|
||||
|
||||
void _init_sim_data()
|
||||
{
|
||||
sim_data.solar_voltage = 12.0;
|
||||
sim_data.battery_voltage = 12.6;
|
||||
sim_data.dcdc_current = 0;
|
||||
sim_data.load_current = 0;
|
||||
sim_data.mcu_temperature = 25;
|
||||
sim_data.bat_temperature = 25;
|
||||
sim_data.internal_temperature = 25;
|
||||
sim_data_initialized = true;
|
||||
}
|
||||
|
||||
//----------------------------------------------------------------------------
|
||||
void update_measurements(dcdc_t *dcdc, battery_state_t *bat, load_output_t *load, power_port_t *hs, power_port_t *ls)
|
||||
{
|
||||
if (sim_data_initialized == false)
|
||||
_init_sim_data();
|
||||
|
||||
ls->voltage = sim_data.battery_voltage;
|
||||
load->voltage = ls->voltage;
|
||||
|
||||
hs->voltage = sim_data.solar_voltage;
|
||||
|
||||
load->current = sim_data.load_current;
|
||||
|
||||
dcdc->ls_current = sim_data.dcdc_current;
|
||||
ls->current = dcdc->ls_current - load->current;
|
||||
hs->current = -dcdc->ls_current * ls->voltage / hs->voltage;
|
||||
|
||||
bat->temperature = sim_data.bat_temperature;
|
||||
dcdc->temp_mosfets = sim_data.internal_temperature;
|
||||
mcu_temp = sim_data.mcu_temperature;
|
||||
|
||||
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 (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;
|
||||
}
|
||||
}
|
||||
|
||||
// dummy functions
|
||||
void calibrate_current_sensors(dcdc_t *dcdc, load_output_t *load) {;}
|
||||
void detect_battery_temperature(battery_state_t *bat, float bat_temp) {;}
|
||||
void dma_setup() {;}
|
||||
void adc_setup() {;}
|
||||
void adc_timer_start(int freq_Hz) {;}
|
||||
|
||||
#endif /* TESTING_PIL */
|
||||
|
||||
#endif /* UNIT_TEST */
|
||||
+8
-58
@@ -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.
|
||||
@@ -18,15 +18,15 @@
|
||||
#include "config.h"
|
||||
#include "pcb.h"
|
||||
|
||||
#include "power_port.h"
|
||||
#include "dc_bus.h"
|
||||
#include "load.h"
|
||||
#include "log.h"
|
||||
|
||||
#include <math.h> // for fabs function
|
||||
|
||||
// ToDo: Remove global definitions!
|
||||
extern power_port_t ls_port;
|
||||
extern power_port_t hs_port;
|
||||
extern dc_bus_t ls_bus;
|
||||
extern dc_bus_t hs_bus;
|
||||
extern load_output_t load;
|
||||
extern log_data_t log_data;
|
||||
|
||||
@@ -219,58 +219,6 @@ void battery_state_init(battery_state_t *bat_state)
|
||||
bat_state->temperature = 25.0;
|
||||
}
|
||||
|
||||
//----------------------------------------------------------------------------
|
||||
// must be called exactly once per second, otherwise energy calculation gets wrong
|
||||
void battery_update_energy(battery_state_t *bat, float bat_voltage, float bat_current, float dcdc_current, float load_current)
|
||||
{
|
||||
// static variables so that it is not reset for each function call
|
||||
static int seconds_zero_solar = 0;
|
||||
|
||||
// stores the input/output energy status of previous day and to add
|
||||
// xxx_day_Wh only once per day and increase accuracy
|
||||
static uint32_t solar_in_total_Wh_prev = log_data.solar_in_total_Wh;
|
||||
static uint32_t load_out_total_Wh_prev = log_data.load_out_total_Wh;
|
||||
static uint32_t bat_chg_total_Wh_prev = bat->chg_total_Wh;
|
||||
static uint32_t bat_dis_total_Wh_prev = bat->dis_total_Wh;
|
||||
|
||||
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++;
|
||||
solar_in_total_Wh_prev = log_data.solar_in_total_Wh;
|
||||
load_out_total_Wh_prev = log_data.load_out_total_Wh;
|
||||
bat_chg_total_Wh_prev = bat->chg_total_Wh;
|
||||
bat_dis_total_Wh_prev = bat->dis_total_Wh;
|
||||
log_data.solar_in_day_Wh = 0.0;
|
||||
log_data.load_out_day_Wh = 0.0;
|
||||
bat->chg_total_Wh = 0.0;
|
||||
bat->dis_total_Wh = 0.0;
|
||||
}
|
||||
seconds_zero_solar = 0;
|
||||
}
|
||||
|
||||
float bat_energy = bat_voltage * bat_current / 3600.0; // timespan = 1s, so no multiplication with time
|
||||
if (bat_energy > 0) {
|
||||
bat->chg_day_Wh += bat_energy;
|
||||
}
|
||||
else {
|
||||
bat->dis_day_Wh += -(bat_energy);
|
||||
}
|
||||
bat->chg_total_Wh = bat_chg_total_Wh_prev + (bat->chg_day_Wh > 0 ? bat->chg_day_Wh : 0);
|
||||
bat->dis_total_Wh = bat_dis_total_Wh_prev + (bat->dis_day_Wh > 0 ? bat->dis_day_Wh : 0);
|
||||
bat->discharged_Ah += (load_current - bat_current) / 3600.0;
|
||||
|
||||
log_data.solar_in_day_Wh += bat_voltage * dcdc_current / 3600.0; // timespan = 1s, so no multiplication with time
|
||||
log_data.load_out_day_Wh += load.current * ls_port.voltage / 3600.0;
|
||||
log_data.solar_in_total_Wh = solar_in_total_Wh_prev + (log_data.solar_in_day_Wh > 0 ? log_data.solar_in_day_Wh : 0);
|
||||
log_data.load_out_total_Wh = load_out_total_Wh_prev + (log_data.load_out_day_Wh > 0 ? log_data.load_out_day_Wh : 0);
|
||||
}
|
||||
|
||||
void battery_update_soc(battery_conf_t *bat_conf, battery_state_t *bat_state, float voltage, float current)
|
||||
{
|
||||
static int soc_filtered = 0; // SOC / 100 for better filtering
|
||||
@@ -296,4 +244,6 @@ void battery_update_soc(battery_conf_t *bat_conf, battery_state_t *bat_state, fl
|
||||
}
|
||||
bat_state->soc = soc_filtered / 100;
|
||||
}
|
||||
}
|
||||
|
||||
bat_state->discharged_Ah += current / 3600.0;
|
||||
}
|
||||
|
||||
@@ -229,11 +229,6 @@ typedef struct
|
||||
float temperature; ///< Battery temperature in °C from ext. temperature sensor (if existing)
|
||||
bool ext_temp_sensor; ///< True if external temperature sensor was detected
|
||||
|
||||
float chg_day_Wh; ///< Cumulated energy in charge direction of today (Wh)
|
||||
float dis_day_Wh; ///< Cumulated energy in discharge direction of today (Wh)
|
||||
uint32_t chg_total_Wh; ///< Cumulated total energy in charge direction (Wh)
|
||||
uint32_t dis_total_Wh; ///< Cumulated total energy in discharge direction (Wh)
|
||||
|
||||
float usable_capacity; ///< Estimated usable capacity (Ah) based on coulomb counting
|
||||
|
||||
float discharged_Ah; ///< Coulomb counter for SOH calculation
|
||||
@@ -276,17 +271,10 @@ void battery_conf_overwrite(battery_conf_t *source, battery_conf_t *destination,
|
||||
*/
|
||||
void battery_state_init(battery_state_t *bat_state);
|
||||
|
||||
/** Energy calculation for battery, solar port and load output
|
||||
*
|
||||
* Must be called exactly once per second, otherwise energy calculation gets wrong.
|
||||
*/
|
||||
void battery_update_energy(battery_state_t *bat, float bat_voltage, float bat_current, float dcdc_current, float load_current);
|
||||
|
||||
/** SOC estimation
|
||||
*
|
||||
* Must be called exactly once per second, otherwise SOC calculation gets wrong.
|
||||
*/
|
||||
void battery_update_soc(battery_conf_t *bat_conf, battery_state_t *bat_state, float voltage, float current);
|
||||
|
||||
|
||||
#endif /* BATTERY_H */
|
||||
|
||||
+24
-24
@@ -29,16 +29,16 @@ void _enter_state(battery_state_t* bat_state, int next_state)
|
||||
bat_state->chg_state = next_state;
|
||||
}
|
||||
|
||||
void charger_state_machine(power_port_t *port, battery_conf_t *bat_conf, battery_state_t *bat_state, float voltage, float current)
|
||||
void charger_state_machine(dc_bus_t *bus, 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);
|
||||
|
||||
// Load management
|
||||
// battery port input state (i.e. battery discharging direction) defines load state
|
||||
if (port->input_allowed == true
|
||||
&& voltage < bat_conf->voltage_load_disconnect - current * port->droop_res_input)
|
||||
if (bus->dis_allowed == true
|
||||
&& voltage < bat_conf->voltage_load_disconnect - current * bus->dis_droop_res)
|
||||
{
|
||||
port->input_allowed = false;
|
||||
bus->dis_allowed = false;
|
||||
bat_state->num_deep_discharges++;
|
||||
|
||||
if (bat_state->usable_capacity < 0.1) {
|
||||
@@ -51,25 +51,25 @@ void charger_state_machine(power_port_t *port, battery_conf_t *bat_conf, battery
|
||||
// simple SOH estimation
|
||||
bat_state->soh = bat_state->usable_capacity / bat_conf->nominal_capacity;
|
||||
}
|
||||
else if (port->input_allowed == true
|
||||
else if (bus->dis_allowed == true
|
||||
&& (bat_state->temperature > bat_conf->discharge_temp_max
|
||||
|| bat_state->temperature < bat_conf->discharge_temp_min))
|
||||
{
|
||||
port->input_allowed = false;
|
||||
bus->dis_allowed = false;
|
||||
}
|
||||
|
||||
if (voltage >= bat_conf->voltage_load_reconnect - current * port->droop_res_input
|
||||
if (voltage >= bat_conf->voltage_load_reconnect - current * bus->dis_droop_res
|
||||
&& bat_state->temperature < bat_conf->discharge_temp_max - 1
|
||||
&& bat_state->temperature > bat_conf->discharge_temp_min + 1)
|
||||
{
|
||||
port->input_allowed = true;
|
||||
bus->dis_allowed = true;
|
||||
}
|
||||
|
||||
// check battery temperature
|
||||
if (bat_state->temperature > bat_conf->charge_temp_max
|
||||
|| bat_state->temperature < bat_conf->charge_temp_min)
|
||||
{
|
||||
port->output_allowed = false;
|
||||
bus->chg_allowed = false;
|
||||
_enter_state(bat_state, CHG_STATE_IDLE);
|
||||
}
|
||||
|
||||
@@ -81,9 +81,9 @@ void charger_state_machine(power_port_t *port, battery_conf_t *bat_conf, battery
|
||||
&& bat_state->temperature < bat_conf->charge_temp_max - 1
|
||||
&& bat_state->temperature > bat_conf->charge_temp_min + 1)
|
||||
{
|
||||
port->voltage_output_target = bat_conf->voltage_topping + bat_conf->temperature_compensation * (bat_state->temperature - 25);
|
||||
port->current_output_max = bat_conf->charge_current_max;
|
||||
port->output_allowed = true;
|
||||
bus->chg_voltage_target = bat_conf->voltage_topping + bat_conf->temperature_compensation * (bat_state->temperature - 25);
|
||||
bus->chg_current_max = bat_conf->charge_current_max;
|
||||
bus->chg_allowed = true;
|
||||
bat_state->full = false;
|
||||
_enter_state(bat_state, CHG_STATE_BULK);
|
||||
}
|
||||
@@ -91,18 +91,18 @@ void charger_state_machine(power_port_t *port, battery_conf_t *bat_conf, battery
|
||||
}
|
||||
case CHG_STATE_BULK: {
|
||||
// continuously adjust voltage setting for temperature compensation
|
||||
port->voltage_output_target = bat_conf->voltage_topping + bat_conf->temperature_compensation * (bat_state->temperature - 25);
|
||||
bus->chg_voltage_target = bat_conf->voltage_topping + bat_conf->temperature_compensation * (bat_state->temperature - 25);
|
||||
|
||||
if (voltage > port->voltage_output_target - current * port->droop_res_output) {
|
||||
if (voltage > bus->chg_voltage_target - current * bus->chg_droop_res) {
|
||||
_enter_state(bat_state, CHG_STATE_TOPPING);
|
||||
}
|
||||
break;
|
||||
}
|
||||
case CHG_STATE_TOPPING: {
|
||||
// continuously adjust voltage setting for temperature compensation
|
||||
port->voltage_output_target = bat_conf->voltage_topping + bat_conf->temperature_compensation * (bat_state->temperature - 25);
|
||||
bus->chg_voltage_target = bat_conf->voltage_topping + bat_conf->temperature_compensation * (bat_state->temperature - 25);
|
||||
|
||||
if (voltage >= port->voltage_output_target - current * port->droop_res_output) {
|
||||
if (voltage >= bus->chg_voltage_target - current * bus->chg_droop_res) {
|
||||
bat_state->time_voltage_limit_reached = time(NULL);
|
||||
}
|
||||
|
||||
@@ -117,17 +117,17 @@ void charger_state_machine(power_port_t *port, battery_conf_t *bat_conf, battery
|
||||
|
||||
/*if (bat_conf->equalization_enabled) {
|
||||
// TODO: additional conditions!
|
||||
port->voltage_output_target = bat_conf->voltage_equalization;
|
||||
port->current_output_max = bat_conf->current_limit_equalization;
|
||||
bus->chg_voltage_target = bat_conf->voltage_equalization;
|
||||
bus->chg_current_max = bat_conf->current_limit_equalization;
|
||||
_enter_state(bat_state, CHG_STATE_EQUALIZATION);
|
||||
}
|
||||
else */if (bat_conf->trickle_enabled) {
|
||||
port->voltage_output_target = bat_conf->voltage_trickle + bat_conf->temperature_compensation * (bat_state->temperature - 25);
|
||||
bus->chg_voltage_target = bat_conf->voltage_trickle + bat_conf->temperature_compensation * (bat_state->temperature - 25);
|
||||
_enter_state(bat_state, CHG_STATE_TRICKLE);
|
||||
}
|
||||
else {
|
||||
port->current_output_max = 0;
|
||||
port->output_allowed = false;
|
||||
bus->chg_current_max = 0;
|
||||
bus->chg_allowed = false;
|
||||
_enter_state(bat_state, CHG_STATE_IDLE);
|
||||
}
|
||||
}
|
||||
@@ -135,15 +135,15 @@ void charger_state_machine(power_port_t *port, battery_conf_t *bat_conf, battery
|
||||
}
|
||||
case CHG_STATE_TRICKLE: {
|
||||
// continuously adjust voltage setting for temperature compensation
|
||||
port->voltage_output_target = bat_conf->voltage_trickle + bat_conf->temperature_compensation * (bat_state->temperature - 25);
|
||||
bus->chg_voltage_target = bat_conf->voltage_trickle + bat_conf->temperature_compensation * (bat_state->temperature - 25);
|
||||
|
||||
if (voltage >= port->voltage_output_target - current * port->droop_res_output) {
|
||||
if (voltage >= bus->chg_voltage_target - current * bus->chg_droop_res) {
|
||||
bat_state->time_voltage_limit_reached = time(NULL);
|
||||
}
|
||||
|
||||
if (time(NULL) - bat_state->time_voltage_limit_reached > bat_conf->time_trickle_recharge)
|
||||
{
|
||||
port->current_output_max = bat_conf->charge_current_max;
|
||||
bus->chg_current_max = bat_conf->charge_current_max;
|
||||
bat_state->full = false;
|
||||
_enter_state(bat_state, CHG_STATE_BULK);
|
||||
}
|
||||
|
||||
+2
-2
@@ -26,7 +26,7 @@
|
||||
#include <stdint.h>
|
||||
|
||||
#include "dcdc.h"
|
||||
#include "power_port.h"
|
||||
#include "dc_bus.h"
|
||||
|
||||
/** Possible charger states
|
||||
|
||||
@@ -71,6 +71,6 @@ enum charger_state {
|
||||
|
||||
/** Charger state machine update, should be called once per second
|
||||
*/
|
||||
void charger_state_machine(power_port_t *port, battery_conf_t *bat_conf, battery_state_t *bat_state, float voltage, float current);
|
||||
void charger_state_machine(dc_bus_t *port, battery_conf_t *bat_conf, battery_state_t *bat_state, float voltage, float current);
|
||||
|
||||
#endif /* CHARGER_H */
|
||||
|
||||
+19
-32
@@ -30,19 +30,15 @@
|
||||
#include "pwm_switch.h"
|
||||
#include <stdio.h>
|
||||
|
||||
#ifdef PIL_TESTING
|
||||
#include "pil_test.h"
|
||||
pil_test_data_t sim_data;
|
||||
#endif
|
||||
|
||||
extern log_data_t log_data;
|
||||
extern battery_state_t bat_state;
|
||||
extern battery_conf_t bat_conf;
|
||||
extern battery_conf_t bat_conf_user;
|
||||
extern dcdc_t dcdc;
|
||||
extern load_output_t load;
|
||||
extern power_port_t hs_port;
|
||||
extern power_port_t ls_port;
|
||||
extern dc_bus_t hs_bus;
|
||||
extern dc_bus_t ls_bus;
|
||||
extern dc_bus_t load_bus;
|
||||
extern pwm_switch_t pwm_switch;
|
||||
|
||||
const char* manufacturer = "Libre Solar";
|
||||
@@ -127,15 +123,6 @@ const data_object_t data_objects[] = {
|
||||
{0x62, TS_INPUT, TS_ACCESS_READ | TS_ACCESS_WRITE, TS_T_BOOL, 0, (void*) &(dcdc.enabled), "DCDCEn"},
|
||||
#endif
|
||||
|
||||
#ifdef PIL_TESTING // only used during processor-in-the-loop test
|
||||
{0x63, TS_INPUT, TS_ACCESS_READ | TS_ACCESS_WRITE, TS_T_FLOAT32, 2, (void*) &(sim_data.solar_voltage), "SimSolar_V"},
|
||||
{0x64, TS_INPUT, TS_ACCESS_READ | TS_ACCESS_WRITE, TS_T_FLOAT32, 2, (void*) &(sim_data.battery_voltage), "SimBat_V"},
|
||||
{0x65, TS_INPUT, TS_ACCESS_READ | TS_ACCESS_WRITE, TS_T_FLOAT32, 2, (void*) &(sim_data.dcdc_current), "SimDCDC_A"},
|
||||
{0x66, TS_INPUT, TS_ACCESS_READ | TS_ACCESS_WRITE, TS_T_FLOAT32, 2, (void*) &(sim_data.load_current), "SimLoad_A"},
|
||||
{0x67, TS_INPUT, TS_ACCESS_READ | TS_ACCESS_WRITE, TS_T_FLOAT32, 1, (void*) &(sim_data.bat_temperature), "SimBat_degC"},
|
||||
{0x68, TS_INPUT, TS_ACCESS_READ | TS_ACCESS_WRITE, TS_T_FLOAT32, 1, (void*) &(sim_data.mcu_temperature), "SimMCU_degC"},
|
||||
#endif
|
||||
|
||||
// OUTPUT DATA ////////////////////////////////////////////////////////////
|
||||
// using IDs >= 0x70 except for high priority data objects
|
||||
|
||||
@@ -145,9 +132,9 @@ const data_object_t data_objects[] = {
|
||||
{0x06, TS_OUTPUT, TS_ACCESS_READ, TS_T_UINT16, 0, (void*) &(bat_state.soc), "SOC_%"}, // output will be uint8_t
|
||||
|
||||
// battery related data objects
|
||||
{0x70, TS_OUTPUT, TS_ACCESS_READ, TS_T_FLOAT32, 2, (void*) &(ls_port.voltage), "Bat_V"},
|
||||
{0x71, TS_OUTPUT, TS_ACCESS_READ, TS_T_FLOAT32, 2, (void*) &(hs_port.voltage), "Solar_V"},
|
||||
{0x72, TS_OUTPUT, TS_ACCESS_READ, TS_T_FLOAT32, 2, (void*) &(ls_port.current), "Bat_A"},
|
||||
{0x70, TS_OUTPUT, TS_ACCESS_READ, TS_T_FLOAT32, 2, (void*) &(ls_bus.voltage), "Bat_V"},
|
||||
{0x71, TS_OUTPUT, TS_ACCESS_READ, TS_T_FLOAT32, 2, (void*) &(hs_bus.voltage), "Solar_V"},
|
||||
{0x72, TS_OUTPUT, TS_ACCESS_READ, TS_T_FLOAT32, 2, (void*) &(ls_bus.current), "Bat_A"},
|
||||
{0x73, TS_OUTPUT, TS_ACCESS_READ, TS_T_FLOAT32, 2, (void*) &(load.current), "Load_A"},
|
||||
{0x74, TS_OUTPUT, TS_ACCESS_READ, TS_T_FLOAT32, 1, (void*) &(bat_state.temperature), "Bat_degC"},
|
||||
{0x75, TS_OUTPUT, TS_ACCESS_READ, TS_T_BOOL, 1, (void*) &(bat_state.ext_temp_sensor), "BatTempExt"},
|
||||
@@ -157,9 +144,9 @@ const data_object_t data_objects[] = {
|
||||
#endif
|
||||
{0x78, TS_OUTPUT, TS_ACCESS_READ, TS_T_UINT16, 0, (void*) &(bat_state.chg_state), "ChgState"},
|
||||
{0x79, TS_OUTPUT, TS_ACCESS_READ, TS_T_UINT16, 0, (void*) &(dcdc.state), "DCDCState"},
|
||||
{0x7A, TS_OUTPUT, TS_ACCESS_READ, TS_T_FLOAT32, 2, (void*) &(hs_port.current), "Solar_A"},
|
||||
{0x7B, TS_OUTPUT, TS_ACCESS_READ, TS_T_FLOAT32, 2, (void*) &(ls_port.voltage_output_target), "BatTarget_V"},
|
||||
{0x7C, TS_OUTPUT, TS_ACCESS_READ, TS_T_FLOAT32, 2, (void*) &(ls_port.current_output_max), "BatTarget_A"},
|
||||
{0x7A, TS_OUTPUT, TS_ACCESS_READ, TS_T_FLOAT32, 2, (void*) &(hs_bus.current), "Solar_A"},
|
||||
{0x7B, TS_OUTPUT, TS_ACCESS_READ, TS_T_FLOAT32, 2, (void*) &(ls_bus.chg_voltage_target), "BatTarget_V"},
|
||||
{0x7C, TS_OUTPUT, TS_ACCESS_READ, TS_T_FLOAT32, 2, (void*) &(ls_bus.chg_current_max), "BatTarget_A"},
|
||||
|
||||
// others
|
||||
{0x90, TS_OUTPUT, TS_ACCESS_READ, TS_T_FLOAT32, 0, (void*) &(latitude), "Latitude"},
|
||||
@@ -170,8 +157,8 @@ const data_object_t data_objects[] = {
|
||||
|
||||
{0x08, TS_REC, TS_ACCESS_READ, TS_T_UINT32, 0, (void*) &(log_data.solar_in_total_Wh), "SolarInTotal_Wh"},
|
||||
{0x09, TS_REC, TS_ACCESS_READ, TS_T_UINT32, 0, (void*) &(log_data.load_out_total_Wh), "LoadOutTotal_Wh"},
|
||||
{0x0A, TS_REC, TS_ACCESS_READ, TS_T_UINT32, 0, (void*) &(bat_state.chg_total_Wh), "BatChgTotal_Wh"},
|
||||
{0x0B, TS_REC, TS_ACCESS_READ, TS_T_UINT32, 0, (void*) &(bat_state.dis_total_Wh), "BatDisTotal_Wh"},
|
||||
{0x0A, TS_REC, TS_ACCESS_READ, TS_T_UINT32, 0, (void*) &(log_data.bat_chg_total_Wh), "BatChgTotal_Wh"},
|
||||
{0x0B, TS_REC, TS_ACCESS_READ, TS_T_UINT32, 0, (void*) &(log_data.bat_dis_total_Wh), "BatDisTotal_Wh"},
|
||||
{0x0C, TS_REC, TS_ACCESS_READ, TS_T_UINT16, 0, (void*) &(bat_state.num_full_charges), "FullChgCount"},
|
||||
{0x0D, TS_REC, TS_ACCESS_READ, TS_T_UINT16, 0, (void*) &(bat_state.num_deep_discharges), "DeepDisCount"},
|
||||
{0x0E, TS_REC, TS_ACCESS_READ, TS_T_FLOAT32, 0, (void*) &(bat_state.usable_capacity), "BatUsable_Ah"}, // usable battery capacity
|
||||
@@ -179,10 +166,10 @@ const data_object_t data_objects[] = {
|
||||
{0x10, TS_REC, TS_ACCESS_READ, TS_T_UINT16, 2, (void*) &(log_data.load_power_max_day), "LoadMaxDay_W"},
|
||||
|
||||
// accumulated data
|
||||
{0xA0, TS_REC, TS_ACCESS_READ, TS_T_FLOAT32, 2, (void*) &(log_data.solar_in_day_Wh), "SolarInDay_Wh"},
|
||||
{0xA1, TS_REC, TS_ACCESS_READ, TS_T_FLOAT32, 2, (void*) &(log_data.load_out_day_Wh), "LoadOutDay_Wh"},
|
||||
{0xA2, TS_REC, TS_ACCESS_READ, TS_T_FLOAT32, 2, (void*) &(bat_state.chg_day_Wh), "BatChgDay_Wh"},
|
||||
{0xA3, TS_REC, TS_ACCESS_READ, TS_T_FLOAT32, 2, (void*) &(bat_state.dis_day_Wh), "BatDisDay_Wh"},
|
||||
{0xA0, TS_REC, TS_ACCESS_READ, TS_T_FLOAT32, 2, (void*) &(hs_bus.dis_energy_Wh), "SolarInDay_Wh"},
|
||||
{0xA1, TS_REC, TS_ACCESS_READ, TS_T_FLOAT32, 2, (void*) &(load_bus.chg_energy_Wh), "LoadOutDay_Wh"},
|
||||
{0xA2, TS_REC, TS_ACCESS_READ, TS_T_FLOAT32, 2, (void*) &(ls_bus.chg_energy_Wh), "BatChgDay_Wh"},
|
||||
{0xA3, TS_REC, TS_ACCESS_READ, TS_T_FLOAT32, 2, (void*) &(ls_bus.dis_energy_Wh), "BatDisDay_Wh"},
|
||||
{0xA4, TS_REC, TS_ACCESS_READ, TS_T_FLOAT32, 0, (void*) &(bat_state.discharged_Ah), "Dis_Ah"}, // coulomb counter
|
||||
{0xA5, TS_REC, TS_ACCESS_READ, TS_T_UINT16, 0, (void*) &(bat_state.soh), "SOH_%"}, // output will be uint8_t
|
||||
{0xA6, TS_REC, TS_ACCESS_READ, TS_T_INT32, 0, (void*) &(log_data.day_counter), "DayCount"},
|
||||
@@ -194,10 +181,10 @@ const data_object_t data_objects[] = {
|
||||
{0xB4, TS_REC, TS_ACCESS_READ, TS_T_FLOAT32, 2, (void*) &(log_data.solar_voltage_max), "SolarMaxTotal_V"},
|
||||
{0xB5, TS_REC, TS_ACCESS_READ, TS_T_FLOAT32, 2, (void*) &(log_data.dcdc_current_max), "DCDCMaxTotal_A"},
|
||||
{0xB6, TS_REC, TS_ACCESS_READ, TS_T_FLOAT32, 2, (void*) &(log_data.load_current_max), "LoadMaxTotal_A"},
|
||||
{0xB7, TS_REC, TS_ACCESS_READ, TS_T_INT32, 1, (void*) &(log_data.bat_temp_max), "BatMax_degC"},
|
||||
{0xB8, TS_REC, TS_ACCESS_READ, TS_T_INT32, 1, (void*) &(log_data.int_temp_max), "IntMax_degC"},
|
||||
{0xB9, TS_REC, TS_ACCESS_READ, TS_T_INT32, 1, (void*) &(log_data.mosfet_temp_max), "MosfetMax_degC"},
|
||||
{0xBA, TS_REC, TS_ACCESS_READ, TS_T_UINT32, 1, (void*) &(log_data.error_flags), "ErrorFlags"},
|
||||
{0xB7, TS_REC, TS_ACCESS_READ, TS_T_INT32, 1, (void*) &(log_data.bat_temp_max), "BatMax_degC"},
|
||||
{0xB8, TS_REC, TS_ACCESS_READ, TS_T_INT32, 1, (void*) &(log_data.int_temp_max), "IntMax_degC"},
|
||||
{0xB9, TS_REC, TS_ACCESS_READ, TS_T_INT32, 1, (void*) &(log_data.mosfet_temp_max), "MosfetMax_degC"},
|
||||
{0xBA, TS_REC, TS_ACCESS_READ, TS_T_UINT32, 1, (void*) &(log_data.error_flags), "ErrorFlags"},
|
||||
|
||||
// CALIBRATION DATA ///////////////////////////////////////////////////////
|
||||
// using IDs >= 0xD0
|
||||
|
||||
@@ -0,0 +1,70 @@
|
||||
/* 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 "dc_bus.h"
|
||||
|
||||
void dc_bus_init_bat(dc_bus_t *bus, battery_conf_t *bat)
|
||||
{
|
||||
bus->dis_allowed = true;
|
||||
bus->dis_voltage_start = bat->voltage_load_reconnect;
|
||||
bus->dis_voltage_stop = bat->voltage_load_disconnect;
|
||||
bus->dis_current_max = -bat->charge_current_max; // TODO: discharge current
|
||||
bus->dis_droop_res = -(bat->internal_resistance + -bat->wire_resistance); // negative sign for compensation of actual resistance
|
||||
|
||||
bus->chg_allowed = true;
|
||||
bus->chg_voltage_target = bat->voltage_topping;
|
||||
bus->chg_voltage_min = bat->voltage_absolute_min;
|
||||
bus->chg_current_max = bat->charge_current_max;
|
||||
bus->chg_droop_res = -bat->wire_resistance; // negative sign for compensation of actual resistance
|
||||
}
|
||||
|
||||
void dc_bus_init_solar(dc_bus_t *bus)
|
||||
{
|
||||
bus->dis_allowed = true; // PV panel may provide power to solar input of DC/DC
|
||||
bus->dis_voltage_start = 16.0;
|
||||
bus->dis_voltage_stop = 14.0;
|
||||
bus->dis_current_max = -18.0;
|
||||
|
||||
bus->chg_allowed = false;
|
||||
}
|
||||
|
||||
void dc_bus_init_nanogrid(dc_bus_t *bus)
|
||||
{
|
||||
bus->dis_allowed = true;
|
||||
bus->dis_voltage_start = 30.0; // starting buck mode above this point
|
||||
bus->dis_voltage_stop = 20.0; // stopping buck mode below this point
|
||||
bus->dis_current_max = -5.0;
|
||||
bus->dis_droop_res = 0.1; // 1 Ohm means 1V change of target voltage per amp
|
||||
|
||||
bus->chg_allowed = true;
|
||||
bus->chg_voltage_target = 28.0; // starting idle mode above this point
|
||||
bus->chg_current_max = 5.0;
|
||||
bus->chg_voltage_min = 10.0;
|
||||
bus->chg_droop_res = 0.1; // 1 Ohm means 1V change of target voltage per amp
|
||||
}
|
||||
|
||||
// must be called exactly once per second, otherwise energy calculation gets wrong
|
||||
void dc_bus_energy_balance(dc_bus_t *bus)
|
||||
{
|
||||
// remark: timespan = 1s, so no multiplication with time necessary for energy calculation
|
||||
if (bus->current >= 0) {
|
||||
bus->chg_energy_Wh += bus->voltage * bus->current / 3600.0;
|
||||
}
|
||||
else {
|
||||
bus->dis_energy_Wh -= bus->voltage * bus->current / 3600.0;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -14,8 +14,8 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#ifndef POWER_PORT_H
|
||||
#define POWER_PORT_H
|
||||
#ifndef DC_BUS_H
|
||||
#define DC_BUS_H
|
||||
|
||||
/** @file
|
||||
*
|
||||
@@ -25,7 +25,7 @@
|
||||
#include <stdbool.h>
|
||||
#include "battery.h"
|
||||
|
||||
/** Power port type
|
||||
/** DC bus type
|
||||
*
|
||||
* Saves current target settings of either high-side or low-side port of a
|
||||
* DC/DC converter. In this way, e.g. a battery can be configured to be
|
||||
@@ -34,36 +34,43 @@
|
||||
*/
|
||||
typedef struct {
|
||||
float voltage; ///< measured voltage
|
||||
float current; ///< measured current (positive sign = out of the port to external device)
|
||||
float current; ///< sum of currents (positive sign = increasing voltage/charge of the bus)
|
||||
|
||||
float voltage_output_target; ///< target voltage if port is configured as output
|
||||
float droop_res_output; ///< v_target = v_out_max - r_droop_output * current
|
||||
float voltage_output_min; ///< minimum voltage to allow current output (necessary
|
||||
bool chg_allowed; ///< charging direction for battery port
|
||||
float chg_voltage_target; ///< target voltage if port is configured as output
|
||||
float chg_droop_res; ///< v_target = v_out_max - r_droop_output * current
|
||||
float chg_voltage_min; ///< minimum voltage to allow current output (necessary
|
||||
///< to prevent charging of deep-discharged Li-ion batteries)
|
||||
float chg_current_max; ///< charging direction for battery port
|
||||
|
||||
|
||||
float voltage_input_start; ///< minimum voltage to allow current input (= discharging of batteries),
|
||||
bool dis_allowed; ///< discharging direction for battery port
|
||||
float dis_voltage_start; ///< minimum voltage to allow current input (= discharging of batteries),
|
||||
///< starting point for discharging of batteries (load reconnect)
|
||||
float voltage_input_stop; ///< absolute minimum = load disconnect for batteries
|
||||
float droop_res_input; ///< v_stop = v_input_stop - r_droop_input * current
|
||||
float dis_voltage_stop; ///< absolute minimum = load disconnect for batteries
|
||||
float dis_droop_res; ///< v_stop = v_input_stop - r_droop_input * current
|
||||
float dis_current_max; ///< discharging direction for battery port: must be negative value !!!
|
||||
|
||||
float current_output_max; ///< charging direction for battery port
|
||||
float current_input_max; ///< discharging direction for battery port: must be negative value !!!
|
||||
float chg_energy_Wh; ///< cumulated energy in charge direction since last counter reset (Wh)
|
||||
float dis_energy_Wh; ///< cumulated energy in discharge direction since last counter reset (Wh)
|
||||
|
||||
bool output_allowed; ///< charging direction for battery port
|
||||
bool input_allowed; ///< discharging direction for battery port
|
||||
} power_port_t;
|
||||
} dc_bus_t;
|
||||
|
||||
/** Initialize power port for battery connection
|
||||
/** Initialize dc bus for battery connection
|
||||
*/
|
||||
void power_port_init_bat(power_port_t *port, battery_conf_t *bat);
|
||||
void dc_bus_init_bat(dc_bus_t *bus, battery_conf_t *bat);
|
||||
|
||||
/** Initialize power port for solar panel connection
|
||||
/** Initialize dc bus for solar panel connection
|
||||
*/
|
||||
void power_port_init_solar(power_port_t *port);
|
||||
void dc_bus_init_solar(dc_bus_t *bus);
|
||||
|
||||
/** Initialize power port for nanogrid connection
|
||||
/** Initialize dc bus for nanogrid connection
|
||||
*/
|
||||
void power_port_init_nanogrid(power_port_t *port);
|
||||
void dc_bus_init_nanogrid(dc_bus_t *bus);
|
||||
|
||||
#endif /* POWER_PORT_H */
|
||||
/** Energy balance calculation for dc bus
|
||||
*
|
||||
* Must be called exactly once per second, otherwise energy calculation gets wrong.
|
||||
*/
|
||||
void dc_bus_energy_balance(dc_bus_t *bus);
|
||||
|
||||
#endif /* DC_BUS_H */
|
||||
+17
-17
@@ -44,28 +44,28 @@ void dcdc_init(dcdc_t *dcdc)
|
||||
}
|
||||
|
||||
// returns if output power should be increased (1), decreased (-1) or switched off (0)
|
||||
int _dcdc_output_control(dcdc_t *dcdc, power_port_t *out, power_port_t *in)
|
||||
int _dcdc_output_control(dcdc_t *dcdc, dc_bus_t *out, dc_bus_t *in)
|
||||
{
|
||||
float dcdc_power_new = out->voltage * out->current;
|
||||
static int pwm_delta = 1;
|
||||
|
||||
//printf("P: %.2f, P_prev: %.2f, v_in: %.2f, v_out: %.2f, i_in: %.2f, i_out: %.2f, i_max: %.2f, PWM: %.1f, chg_en: %d\n",
|
||||
// dcdc_power_new, dcdc->power, in->voltage, out->voltage, in->current, out->current,
|
||||
// out->current_output_max, half_bridge_get_duty_cycle() * 100.0, out->output_allowed);
|
||||
// out->chg_current_max, half_bridge_get_duty_cycle() * 100.0, out->chg_allowed);
|
||||
|
||||
if (out->output_allowed == false || in->input_allowed == false
|
||||
|| (in->voltage < in->voltage_input_stop && out->current < 0.1))
|
||||
if (out->chg_allowed == false || in->dis_allowed == false
|
||||
|| (in->voltage < in->dis_voltage_stop && out->current < 0.1))
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
else if (out->voltage > (out->voltage_output_target - out->droop_res_output * out->current) // output voltage above target
|
||||
|| (in->voltage < (in->voltage_input_start - in->droop_res_input * in->current) && out->current > 0.1)) // input voltage below limit
|
||||
else if (out->voltage > (out->chg_voltage_target - out->chg_droop_res * out->current) // output voltage above target
|
||||
|| (in->voltage < (in->dis_voltage_start - in->dis_droop_res * in->current) && out->current > 0.1)) // input voltage below limit
|
||||
{
|
||||
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
|
||||
else if (out->current > out->chg_current_max // output current limit exceeded
|
||||
|| in->current < in->dis_current_max) // input current (negative signs) limit exceeded
|
||||
{
|
||||
dcdc->state = DCDC_STATE_CC;
|
||||
return -1; // decrease output power
|
||||
@@ -76,7 +76,7 @@ int _dcdc_output_control(dcdc_t *dcdc, power_port_t *out, power_port_t *in)
|
||||
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
|
||||
else if (out->current < 0.1 && out->voltage < out->dis_voltage_start) // no load condition (e.g. start-up of nanogrid) --> raise voltage
|
||||
{
|
||||
return 1; // increase output power
|
||||
}
|
||||
@@ -91,23 +91,23 @@ int _dcdc_output_control(dcdc_t *dcdc, power_port_t *out, power_port_t *in)
|
||||
}
|
||||
}
|
||||
|
||||
bool _dcdc_check_start_conditions(dcdc_t *dcdc, power_port_t *out, power_port_t *in)
|
||||
bool _dcdc_check_start_conditions(dcdc_t *dcdc, dc_bus_t *out, dc_bus_t *in)
|
||||
{
|
||||
return dcdc->enabled == true
|
||||
&& out->output_allowed == true
|
||||
&& out->voltage < out->voltage_output_target
|
||||
&& out->voltage > out->voltage_output_min
|
||||
&& in->input_allowed == true
|
||||
&& in->voltage > in->voltage_input_start
|
||||
&& out->chg_allowed == true
|
||||
&& out->voltage < out->chg_voltage_target
|
||||
&& out->voltage > out->chg_voltage_min
|
||||
&& in->dis_allowed == true
|
||||
&& in->voltage > in->dis_voltage_start
|
||||
//&& dcdc->hs_voltage - dcdc->ls_voltage > dcdc->offset_voltage_start
|
||||
&& time(NULL) > (dcdc->off_timestamp + dcdc->restart_interval);
|
||||
}
|
||||
|
||||
void dcdc_control(dcdc_t *dcdc, power_port_t *hs, power_port_t *ls)
|
||||
void dcdc_control(dcdc_t *dcdc, dc_bus_t *hs, dc_bus_t *ls)
|
||||
{
|
||||
if (half_bridge_enabled()) {
|
||||
int step;
|
||||
if (ls->current > 0.1) { // buck mode
|
||||
if (dcdc->ls_current > 0.1) { // buck mode
|
||||
//printf("-");
|
||||
step = _dcdc_output_control(dcdc, ls, hs);
|
||||
half_bridge_duty_cycle_step(step);
|
||||
|
||||
+2
-2
@@ -24,7 +24,7 @@
|
||||
|
||||
#include <stdbool.h>
|
||||
#include "battery.h"
|
||||
#include "power_port.h"
|
||||
#include "dc_bus.h"
|
||||
|
||||
/** DC/DC basic operation mode
|
||||
*
|
||||
@@ -99,7 +99,7 @@ void dcdc_init(dcdc_t *dcdc);
|
||||
* @param high_side High-side power port (e.g. solar input for typical MPPT charge controller application)
|
||||
* @param low_side Low-side power port (e.g. battery output for typical MPPT charge controller application)
|
||||
*/
|
||||
void dcdc_control(dcdc_t *dcdc, power_port_t *high_side, power_port_t *low_side);
|
||||
void dcdc_control(dcdc_t *dcdc, dc_bus_t *high_side, dc_bus_t *low_side);
|
||||
|
||||
/** Prevent overcharging of battery in case of shorted HS MOSFET
|
||||
*
|
||||
|
||||
+8
-8
@@ -23,7 +23,7 @@
|
||||
void load_init(load_output_t *load)
|
||||
{
|
||||
load->current_max = LOAD_CURRENT_MAX;
|
||||
load->enabled = false;
|
||||
//load->enabled = false;
|
||||
load->enabled_target = true;
|
||||
load->usb_enabled_target = true;
|
||||
hw_load_switch(false);
|
||||
@@ -38,7 +38,7 @@ void usb_state_machine(load_output_t *load)
|
||||
{
|
||||
switch (load->usb_state) {
|
||||
case LOAD_STATE_DISABLED:
|
||||
if (load->enabled && load->usb_enabled_target == true) {
|
||||
if (load->usb_enabled_target == true) {
|
||||
hw_usb_out(true);
|
||||
load->usb_state = LOAD_STATE_ON;
|
||||
}
|
||||
@@ -85,19 +85,19 @@ void load_state_machine(load_output_t *load, bool source_enabled)
|
||||
case LOAD_STATE_DISABLED:
|
||||
if (source_enabled == true && load->enabled_target == true) {
|
||||
hw_load_switch(true);
|
||||
load->enabled = true;
|
||||
//load->enabled = true;
|
||||
load->switch_state = LOAD_STATE_ON;
|
||||
}
|
||||
break;
|
||||
case LOAD_STATE_ON:
|
||||
if (load->enabled_target == false) {
|
||||
hw_load_switch(false);
|
||||
load->enabled = false;
|
||||
//load->enabled = false;
|
||||
load->switch_state = LOAD_STATE_DISABLED;
|
||||
}
|
||||
else if (source_enabled == false) {
|
||||
hw_load_switch(false);
|
||||
load->enabled = false;
|
||||
//load->enabled = false;
|
||||
load->switch_state = LOAD_STATE_OFF_LOW_SOC;
|
||||
}
|
||||
break;
|
||||
@@ -105,7 +105,7 @@ void load_state_machine(load_output_t *load, bool source_enabled)
|
||||
if (source_enabled == true) {
|
||||
if (load->enabled_target == true) {
|
||||
hw_load_switch(true);
|
||||
load->enabled = true;
|
||||
//load->enabled = true;
|
||||
load->switch_state = LOAD_STATE_ON;
|
||||
}
|
||||
else {
|
||||
@@ -143,7 +143,7 @@ void load_control(load_output_t *load)
|
||||
if (load->junction_temperature > MOSFET_MAX_JUNCTION_TEMP) {
|
||||
hw_load_switch(false);
|
||||
hw_usb_out(false);
|
||||
load->enabled = false;
|
||||
//load->enabled = false;
|
||||
load->switch_state = LOAD_STATE_OFF_OVERCURRENT;
|
||||
load->overcurrent_timestamp = time(NULL);
|
||||
}
|
||||
@@ -154,7 +154,7 @@ void load_control(load_output_t *load)
|
||||
if (debounce_counter > CONTROL_FREQUENCY) { // waited 1s before setting the flag
|
||||
hw_load_switch(false);
|
||||
hw_usb_out(false);
|
||||
load->enabled = false;
|
||||
//load->enabled = false;
|
||||
load->switch_state = LOAD_STATE_OFF_OVERVOLTAGE;
|
||||
load->overcurrent_timestamp = time(NULL);
|
||||
log_data.error_flags |= (1 << ERR_BAT_OVERVOLTAGE);
|
||||
|
||||
+1
-1
@@ -54,7 +54,7 @@ typedef struct {
|
||||
|
||||
float junction_temperature; ///< calculated using thermal model based on current and ambient temperature measurement (unit: °C)
|
||||
|
||||
bool enabled; ///< actual status
|
||||
//bool enabled; ///< actual status
|
||||
bool enabled_target; ///< target setting defined via communication port (overruled if battery is empty)
|
||||
bool usb_enabled_target; ///< same for USB output
|
||||
} load_output_t;
|
||||
|
||||
@@ -23,6 +23,7 @@
|
||||
* Structs needed for data logging (like max/min values, etc.)
|
||||
*/
|
||||
|
||||
#include "dc_bus.h"
|
||||
#include <stdbool.h>
|
||||
#include <stdint.h>
|
||||
|
||||
@@ -39,8 +40,8 @@ enum error_flag_t {
|
||||
* Stores error counters and some maximum ever measured values to EEPROM
|
||||
*/
|
||||
typedef struct {
|
||||
float solar_in_day_Wh;
|
||||
float load_out_day_Wh;
|
||||
uint32_t bat_chg_total_Wh;
|
||||
uint32_t bat_dis_total_Wh;
|
||||
uint32_t solar_in_total_Wh;
|
||||
uint32_t load_out_total_Wh;
|
||||
|
||||
@@ -60,4 +61,7 @@ typedef struct {
|
||||
uint32_t error_flags; ///< Instantaneous errors
|
||||
} log_data_t;
|
||||
|
||||
|
||||
void log_update_energy(log_data_t *log_data, dc_bus_t *bat, dc_bus_t *solar, dc_bus_t *load);
|
||||
|
||||
#endif /* LOG_H */
|
||||
|
||||
+25
-21
@@ -40,9 +40,10 @@
|
||||
Serial serial(PIN_SWD_TX, PIN_SWD_RX, "serial");
|
||||
|
||||
dcdc_t dcdc = {};
|
||||
power_port_t hs_port = {}; // high-side (solar for typical MPPT)
|
||||
power_port_t ls_port = {}; // low-side (battery for typical MPPT)
|
||||
power_port_t *bat_port = NULL;
|
||||
dc_bus_t hs_bus = {}; // high-side (solar for typical MPPT)
|
||||
dc_bus_t ls_bus = {}; // low-side (battery for typical MPPT)
|
||||
dc_bus_t *bat_bus = NULL;
|
||||
dc_bus_t load_bus = {}; // load terminal
|
||||
pwm_switch_t pwm_switch = {}; // only necessary for PWM charger
|
||||
battery_conf_t bat_conf; // actual (used) battery configuration
|
||||
battery_conf_t bat_conf_user; // temporary storage where the user can write to
|
||||
@@ -62,15 +63,15 @@ void system_control()
|
||||
static int counter = 0;
|
||||
|
||||
// convert ADC readings to meaningful measurement values
|
||||
update_measurements(&dcdc, &bat_state, &load, &hs_port, &ls_port);
|
||||
update_measurements(&dcdc, &bat_state, &load, &hs_bus, &ls_bus, &load_bus);
|
||||
|
||||
#ifdef CHARGER_TYPE_PWM
|
||||
pwm_switch_control(&pwm_switch, &hs_port, bat_port);
|
||||
pwm_switch_control(&pwm_switch, &hs_bus, bat_bus);
|
||||
leds_set_charging(pwm_switch_enabled());
|
||||
#else
|
||||
// control PWM of the DC/DC according to hs and ls port settings
|
||||
// (this function includes MPPT algorithm)
|
||||
dcdc_control(&dcdc, &hs_port, &ls_port);
|
||||
dcdc_control(&dcdc, &hs_bus, &ls_bus);
|
||||
leds_set_charging(half_bridge_enabled());
|
||||
#endif
|
||||
|
||||
@@ -82,8 +83,11 @@ void system_control()
|
||||
timestamp++;
|
||||
counter = 0;
|
||||
// energy + soc calculation must be called exactly once per second
|
||||
battery_update_energy(&bat_state, bat_port->voltage, bat_port->current, dcdc.ls_current, load.current);
|
||||
battery_update_soc(&bat_conf, &bat_state, bat_port->voltage, bat_port->current);
|
||||
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);
|
||||
battery_update_soc(&bat_conf, &bat_state, bat_bus->voltage, bat_bus->current);
|
||||
}
|
||||
counter++;
|
||||
}
|
||||
@@ -118,8 +122,8 @@ int main()
|
||||
dma_setup();
|
||||
adc_timer_start(1000); // 1 kHz
|
||||
wait(0.5); // wait for ADC to collect some measurement values
|
||||
update_measurements(&dcdc, &bat_state, &load, &hs_port, &ls_port);
|
||||
calibrate_current_sensors(&dcdc, &load);
|
||||
update_measurements(&dcdc, &bat_state, &load, &hs_bus, &ls_bus, &load_bus);
|
||||
calibrate_current_sensors();
|
||||
|
||||
// Communication interfaces
|
||||
uart_serial_init(&serial);
|
||||
@@ -130,19 +134,19 @@ int main()
|
||||
// Setup of DC/DC power stage
|
||||
switch(dcdc.mode) {
|
||||
case MODE_NANOGRID:
|
||||
power_port_init_nanogrid(&hs_port);
|
||||
power_port_init_bat(&ls_port, &bat_conf);
|
||||
bat_port = &ls_port;
|
||||
dc_bus_init_nanogrid(&hs_bus);
|
||||
dc_bus_init_bat(&ls_bus, &bat_conf);
|
||||
bat_bus = &ls_bus;
|
||||
break;
|
||||
case MODE_MPPT_BUCK: // typical MPPT charge controller operation
|
||||
power_port_init_solar(&hs_port);
|
||||
power_port_init_bat(&ls_port, &bat_conf);
|
||||
bat_port = &ls_port;
|
||||
dc_bus_init_solar(&hs_bus);
|
||||
dc_bus_init_bat(&ls_bus, &bat_conf);
|
||||
bat_bus = &ls_bus;
|
||||
break;
|
||||
case MODE_MPPT_BOOST: // for charging of e-bike battery via solar panel
|
||||
power_port_init_solar(&ls_port);
|
||||
power_port_init_bat(&hs_port, &bat_conf);
|
||||
bat_port = &hs_port;
|
||||
dc_bus_init_solar(&ls_bus);
|
||||
dc_bus_init_bat(&hs_bus, &bat_conf);
|
||||
bat_bus = &hs_bus;
|
||||
break;
|
||||
}
|
||||
|
||||
@@ -163,9 +167,9 @@ int main()
|
||||
|
||||
//printf("Still alive... time: %d, mode: %d\n", (int)time(NULL), dcdc.mode);
|
||||
|
||||
charger_state_machine(bat_port, &bat_conf, &bat_state, bat_port->voltage, bat_port->current);
|
||||
charger_state_machine(bat_bus, &bat_conf, &bat_state, bat_bus->voltage, bat_bus->current);
|
||||
|
||||
load_state_machine(&load, ls_port.input_allowed);
|
||||
load_state_machine(&load, ls_bus.dis_allowed);
|
||||
|
||||
eeprom_update();
|
||||
|
||||
|
||||
+5
-13
@@ -17,8 +17,6 @@
|
||||
#ifndef __PCB_PWM_02_H_
|
||||
#define __PCB_PWM_02_H_
|
||||
|
||||
#define CHARGER_TYPE_PWM 1 // PWM charge controller instead of MPPT
|
||||
|
||||
#define PWM_TIM 3 // use TIM3 timer
|
||||
|
||||
#define DCDC_CURRENT_MAX 20 // PCB maximum DCDC output current
|
||||
@@ -39,10 +37,6 @@
|
||||
#define PIN_LOAD_DIS 0
|
||||
#define PIN_USB_PWR_DIS 0
|
||||
|
||||
#define PIN_REF_I_DCDC 0
|
||||
|
||||
#define PIN_TEMP_INT_PD 0
|
||||
|
||||
enum pin_state_t { PIN_HIGH, PIN_LOW, PIN_FLOAT };
|
||||
|
||||
// assignment LED numbers on PCB to their meaning
|
||||
@@ -75,19 +69,17 @@ static const pin_state_t led_pin_setup[NUM_LEDS][NUM_LED_PINS] = {
|
||||
#define NTC_BETA_VALUE 3435
|
||||
#define NTC_SERIES_RESISTOR 8200.0
|
||||
|
||||
#define ADC_GAIN_V_BAT (132 / 12)
|
||||
#define ADC_GAIN_V_SOLAR (1 + 120/12 + 120/8.2)
|
||||
#define ADC_GAIN_I_LOAD (1000 / 2 / (68/2.2)) // op amp gain: 68/2.2, resistor: 2 mOhm
|
||||
#define ADC_GAIN_I_SOLAR (1000 / 2 / (68/2.2))
|
||||
|
||||
#define ADC_OFFSET_V_SOLAR (-120.0 / 8.2) // to be multiplied with VDDA to get absolute voltage offset
|
||||
#define ADC_GAIN_V_BAT (105.6 / 5.6) // both voltage dividers: 100k + 5.6k
|
||||
#define ADC_GAIN_V_SOLAR (105.6 / 5.6)
|
||||
#define ADC_GAIN_I_LOAD (1000 / 4 / 50) // amp gain: 50, resistor: 4 mOhm
|
||||
#define ADC_GAIN_I_DCDC (1000 / 4 / 50)
|
||||
|
||||
// position in the array written by the DMA controller
|
||||
enum {
|
||||
ADC_POS_V_BAT, // ADC 0 (PA_0)
|
||||
ADC_POS_V_SOLAR, // ADC 1 (PA_1)
|
||||
ADC_POS_I_LOAD, // ADC 5 (PA_5)
|
||||
ADC_POS_I_SOLAR, // ADC 6 (PA_6)
|
||||
ADC_POS_I_DCDC, // ADC 6 (PA_6)
|
||||
ADC_POS_TEMP_BAT, // ADC 7 (PA_7)
|
||||
ADC_POS_VREF_MCU, // ADC 17
|
||||
ADC_POS_TEMP_MCU, // ADC 18
|
||||
|
||||
@@ -1,59 +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.
|
||||
*/
|
||||
|
||||
#include "power_port.h"
|
||||
|
||||
void power_port_init_bat(power_port_t *port, battery_conf_t *bat)
|
||||
{
|
||||
port->input_allowed = true; // discharging allowed
|
||||
port->output_allowed = true; // charging allowed
|
||||
|
||||
port->voltage_input_start = bat->voltage_load_reconnect;
|
||||
port->voltage_input_stop = bat->voltage_load_disconnect;
|
||||
port->current_input_max = -bat->charge_current_max; // TODO: discharge current
|
||||
port->droop_res_input = -(bat->internal_resistance + -bat->wire_resistance); // negative sign for compensation of actual resistance
|
||||
|
||||
port->voltage_output_target = bat->voltage_topping;
|
||||
port->voltage_output_min = bat->voltage_absolute_min;
|
||||
port->current_output_max = bat->charge_current_max;
|
||||
port->droop_res_output = -bat->wire_resistance; // negative sign for compensation of actual resistance
|
||||
}
|
||||
|
||||
void power_port_init_solar(power_port_t *port)
|
||||
{
|
||||
port->input_allowed = true; // PV panel may provide power to solar input of DC/DC
|
||||
port->output_allowed = false;
|
||||
|
||||
port->voltage_input_start = 16.0;
|
||||
port->voltage_input_stop = 14.0;
|
||||
port->current_input_max = -18.0;
|
||||
}
|
||||
|
||||
void power_port_init_nanogrid(power_port_t *port)
|
||||
{
|
||||
port->input_allowed = true;
|
||||
port->output_allowed = true;
|
||||
|
||||
port->voltage_input_start = 30.0; // starting buck mode above this point
|
||||
port->voltage_input_stop = 20.0; // stopping buck mode below this point
|
||||
port->current_input_max = -5.0;
|
||||
port->droop_res_input = 0.1; // 1 Ohm means 1V change of target voltage per amp
|
||||
|
||||
port->voltage_output_target = 28.0; // starting idle mode above this point
|
||||
port->current_output_max = 5.0;
|
||||
port->voltage_output_min = 10.0;
|
||||
port->droop_res_output = 0.1; // 1 Ohm means 1V change of target voltage per amp
|
||||
}
|
||||
+12
-12
@@ -133,23 +133,23 @@ void pwm_switch_init(pwm_switch_t *pwm_switch)
|
||||
_enabled = false; // still disable actual switch
|
||||
}
|
||||
|
||||
void pwm_switch_control(pwm_switch_t *pwm_switch, power_port_t *solar_port, power_port_t *bat_port)
|
||||
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->output_allowed == false || solar_port->input_allowed == false
|
||||
|| (solar_port->voltage < solar_port->voltage_input_stop && bat_port->current < 0.1)
|
||||
if (bat_port->chg_allowed == false || solar_port->dis_allowed == false
|
||||
|| (solar_port->voltage < solar_port->dis_voltage_stop && bat_port->current < 0.1)
|
||||
|| pwm_switch->enabled == false)
|
||||
{
|
||||
pwm_switch_stop();
|
||||
printf("PWM charger stop.\n");
|
||||
}
|
||||
else if (bat_port->voltage > (bat_port->voltage_output_target - bat_port->droop_res_output * bat_port->current) // output voltage above target
|
||||
|| bat_port->current > bat_port->current_output_max // output current limit exceeded
|
||||
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->current_input_max // input current (negative signs) above 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
|
||||
{
|
||||
@@ -160,11 +160,11 @@ void pwm_switch_control(pwm_switch_t *pwm_switch, power_port_t *solar_port, powe
|
||||
}
|
||||
}
|
||||
else {
|
||||
if (bat_port->output_allowed == true
|
||||
&& bat_port->voltage < bat_port->voltage_output_target
|
||||
&& bat_port->voltage > bat_port->voltage_output_min
|
||||
&& solar_port->input_allowed == true
|
||||
&& solar_port->voltage > solar_port->voltage_input_start
|
||||
if (bat_port->chg_allowed == true
|
||||
&& bat_port->voltage < bat_port->chg_voltage_target
|
||||
&& bat_port->voltage > bat_port->chg_voltage_min
|
||||
&& solar_port->dis_allowed == true
|
||||
&& solar_port->voltage > solar_port->dis_voltage_start
|
||||
&& time(NULL) > (pwm_switch->off_timestamp + pwm_switch->restart_interval)
|
||||
&& pwm_switch->enabled == true)
|
||||
{
|
||||
@@ -178,7 +178,7 @@ void pwm_switch_control(pwm_switch_t *pwm_switch, power_port_t *solar_port, powe
|
||||
|
||||
// dummy functions for non-PWM charge controllers
|
||||
void pwm_switch_init(pwm_switch_t *pwm_switch) {}
|
||||
void pwm_switch_control(pwm_switch_t *pwm_switch, power_port_t *solar_port, power_port_t *bat_port) {}
|
||||
void pwm_switch_control(pwm_switch_t *pwm_switch, dc_bus_t *solar_port, dc_bus_t *bat_port) {}
|
||||
void pwm_switch_duty_cycle_step(int delta) {}
|
||||
|
||||
#endif
|
||||
|
||||
+2
-2
@@ -23,7 +23,7 @@
|
||||
*/
|
||||
|
||||
#include <stdbool.h>
|
||||
#include "power_port.h"
|
||||
#include "dc_bus.h"
|
||||
|
||||
/** PWM charger type
|
||||
*
|
||||
@@ -53,7 +53,7 @@ typedef struct {
|
||||
|
||||
void pwm_switch_init(pwm_switch_t *pwm_switch);
|
||||
|
||||
void pwm_switch_control(pwm_switch_t *pwm_switch, power_port_t *solar_port, power_port_t *bat_port);
|
||||
void pwm_switch_control(pwm_switch_t *pwm_switch, dc_bus_t *solar_port, dc_bus_t *bat_port);
|
||||
|
||||
void pwm_switch_duty_cycle_step(int delta);
|
||||
|
||||
|
||||
@@ -39,7 +39,7 @@ uint8_t buf_resp[1000]; // only one response buffer needed for USB and
|
||||
extern ThingSet ts;
|
||||
|
||||
bool uart_serial_command_flag = false;
|
||||
bool pub_uart_enabled = false; // start with sending data disabled
|
||||
bool pub_uart_enabled = true; // start with sending data disabled
|
||||
|
||||
bool usb_serial_command_flag = false;
|
||||
bool pub_usb_enabled = true;
|
||||
|
||||
+11
-10
@@ -25,15 +25,16 @@
|
||||
|
||||
#include "half_bridge.h"
|
||||
#include "dcdc.h"
|
||||
#include "power_port.h"
|
||||
#include "dc_bus.h"
|
||||
#include "battery.h"
|
||||
#include "load.h"
|
||||
#include "log.h"
|
||||
|
||||
extern log_data_t log_data;
|
||||
extern dcdc_t dcdc;
|
||||
extern power_port_t hs_port;
|
||||
extern power_port_t ls_port;
|
||||
extern dc_bus_t hs_bus;
|
||||
extern dc_bus_t ls_bus;
|
||||
extern dc_bus_t load_bus;
|
||||
extern battery_state_t bat_state;
|
||||
extern load_output_t load;
|
||||
|
||||
@@ -80,7 +81,7 @@ void uext_process_1s()
|
||||
oled.drawBitmap(34, 3, bmp_arrow_right, 5, 7, 1);
|
||||
}
|
||||
|
||||
if (load.enabled) {
|
||||
if (load.switch_state == LOAD_STATE_ON) {
|
||||
oled.drawBitmap(84, 3, bmp_arrow_right, 5, 7, 1);
|
||||
}
|
||||
else {
|
||||
@@ -97,7 +98,7 @@ void uext_process_1s()
|
||||
|
||||
// solar panel data
|
||||
if (half_bridge_enabled()) {
|
||||
tmp = -hs_port.voltage * hs_port.current;
|
||||
tmp = -hs_bus.voltage * hs_bus.current;
|
||||
oled.setTextCursor(0, 18);
|
||||
oled.printf("%4.0fW", (abs(tmp) < 1) ? 0 : tmp); // remove negative zeros
|
||||
}
|
||||
@@ -107,25 +108,25 @@ void uext_process_1s()
|
||||
}
|
||||
//if (solar_port->voltage > bat_port->voltage) {
|
||||
oled.setTextCursor(0, 26);
|
||||
oled.printf("%4.1fV", hs_port.voltage);
|
||||
oled.printf("%4.1fV", hs_bus.voltage);
|
||||
//}
|
||||
|
||||
// battery data
|
||||
tmp = ls_port.voltage * ls_port.current;
|
||||
tmp = ls_bus.voltage * ls_bus.current;
|
||||
oled.setTextCursor(42, 18);
|
||||
oled.printf("%5.1fW", (abs(tmp) < 0.1) ? 0 : tmp); // remove negative zeros
|
||||
oled.setTextCursor(42, 26);
|
||||
oled.printf("%5.1fV", ls_port.voltage);
|
||||
oled.printf("%5.1fV", ls_bus.voltage);
|
||||
|
||||
// load data
|
||||
tmp = ls_port.voltage * load.current;
|
||||
tmp = ls_bus.voltage * load.current;
|
||||
oled.setTextCursor(90, 18);
|
||||
oled.printf("%5.1fW", (abs(tmp) < 0.1) ? 0 : tmp); // remove negative zeros
|
||||
oled.setTextCursor(90, 26);
|
||||
oled.printf("%5.1fA\n", (abs(load.current) < 0.1) ? 0 : load.current);
|
||||
|
||||
oled.setTextCursor(0, 36);
|
||||
oled.printf("Day +%5.0fWh -%5.0fWh", log_data.solar_in_day_Wh, fabs(log_data.load_out_day_Wh));
|
||||
oled.printf("Day +%5.0fWh -%5.0fWh", hs_bus.dis_energy_Wh, fabs(load_bus.chg_energy_Wh));
|
||||
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);
|
||||
|
||||
@@ -0,0 +1,35 @@
|
||||
/* 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.
|
||||
* 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 "config.h"
|
||||
#include "pcb.h"
|
||||
#include "adc_dma.h"
|
||||
#include "adc_dma_stub.h"
|
||||
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
|
||||
extern uint32_t adc_filtered[NUM_ADC_CH];
|
||||
|
||||
|
||||
void prepare_adc_readings(adc_values_t values)
|
||||
{
|
||||
adc_filtered[ADC_POS_VREF_MCU] = (uint32_t)(1.224 / 3.3 * 4096) << (4 + ADC_FILTER_CONST);
|
||||
adc_filtered[ADC_POS_V_SOLAR] = (uint32_t)((values.solar_voltage / (ADC_GAIN_V_SOLAR)) / 3.3 * 4096) << (4 + ADC_FILTER_CONST);
|
||||
adc_filtered[ADC_POS_V_BAT] = (uint32_t)((values.battery_voltage / (ADC_GAIN_V_BAT)) / 3.3 * 4096) << (4 + ADC_FILTER_CONST);
|
||||
adc_filtered[ADC_POS_I_DCDC] = (uint32_t)((values.dcdc_current / (ADC_GAIN_I_DCDC)) / 3.3 * 4096) << (4 + ADC_FILTER_CONST);
|
||||
adc_filtered[ADC_POS_I_LOAD] = (uint32_t)((values.load_current / (ADC_GAIN_I_LOAD)) / 3.3 * 4096) << (4 + ADC_FILTER_CONST);
|
||||
}
|
||||
@@ -14,10 +14,9 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#include "mbed.h"
|
||||
#include "config.h"
|
||||
|
||||
/** Data for Processor-In-the-Loop test
|
||||
/** Data to fill adc_filtered array during unit-tests
|
||||
*/
|
||||
typedef struct {
|
||||
float solar_voltage;
|
||||
@@ -27,4 +26,6 @@ typedef struct {
|
||||
float bat_temperature;
|
||||
float mcu_temperature;
|
||||
float internal_temperature;
|
||||
} pil_test_data_t;
|
||||
} adc_values_t;
|
||||
|
||||
void prepare_adc_readings(adc_values_t values);
|
||||
+8
-4
@@ -19,9 +19,10 @@
|
||||
#include "tests.h"
|
||||
|
||||
dcdc_t dcdc = {};
|
||||
power_port_t hs_port = {}; // high-side (solar for typical MPPT)
|
||||
power_port_t ls_port = {}; // low-side (battery for typical MPPT)
|
||||
power_port_t *bat_port = NULL;
|
||||
dc_bus_t hs_bus = {}; // high-side (solar for typical MPPT)
|
||||
dc_bus_t ls_bus = {}; // low-side (battery for typical MPPT)
|
||||
dc_bus_t *bat_port = NULL;
|
||||
dc_bus_t load_bus = {}; // load terminal
|
||||
pwm_switch_t pwm_switch = {}; // only necessary for PWM charger
|
||||
battery_conf_t bat_conf; // actual (used) battery configuration
|
||||
battery_conf_t bat_conf_user; // temporary storage where the user can write to
|
||||
@@ -32,8 +33,11 @@ extern ThingSet ts; // defined in data_objects.cpp
|
||||
|
||||
time_t timestamp; // current unix timestamp (independent of time(NULL), as it is user-configurable)
|
||||
|
||||
int main() {
|
||||
int main()
|
||||
{
|
||||
adc_tests();
|
||||
charger_tests();
|
||||
dc_bus_tests();
|
||||
|
||||
// TODO
|
||||
//battery_tests();
|
||||
|
||||
@@ -4,3 +4,7 @@
|
||||
void charger_tests();
|
||||
|
||||
void battery_tests();
|
||||
|
||||
void adc_tests();
|
||||
|
||||
void dc_bus_tests();
|
||||
|
||||
@@ -0,0 +1,66 @@
|
||||
|
||||
#include "tests.h"
|
||||
#include "adc_dma.h"
|
||||
#include "adc_dma_stub.h"
|
||||
|
||||
extern dcdc_t dcdc;
|
||||
extern dc_bus_t hs_bus;
|
||||
extern dc_bus_t ls_bus;
|
||||
extern dc_bus_t *bat_port;
|
||||
extern dc_bus_t load_bus;
|
||||
extern battery_state_t bat_state;
|
||||
extern load_output_t load;
|
||||
|
||||
static adc_values_t adcval;
|
||||
|
||||
void check_solar_bus_readings()
|
||||
{
|
||||
TEST_ASSERT_EQUAL_FLOAT(adcval.solar_voltage, round(hs_bus.voltage * 10) / 10);
|
||||
TEST_ASSERT_EQUAL_FLOAT(adcval.dcdc_current / adcval.solar_voltage * adcval.battery_voltage, -round(hs_bus.current * 10) / 10);
|
||||
}
|
||||
|
||||
void check_battery_bus_readings()
|
||||
{
|
||||
TEST_ASSERT_EQUAL_FLOAT(adcval.battery_voltage, round(ls_bus.voltage * 10) / 10);
|
||||
TEST_ASSERT_EQUAL_FLOAT(adcval.dcdc_current - adcval.load_current, round(ls_bus.current * 10) / 10);
|
||||
}
|
||||
|
||||
void check_load_bus_readings()
|
||||
{
|
||||
TEST_ASSERT_EQUAL_FLOAT(adcval.battery_voltage, round(load_bus.voltage * 10) / 10);
|
||||
TEST_ASSERT_EQUAL_FLOAT(adcval.load_current, round(load_bus.current * 10) / 10);
|
||||
}
|
||||
|
||||
void check_temperature_readings()
|
||||
{
|
||||
TEST_ASSERT_EQUAL_FLOAT(adcval.bat_temperature, round(bat_state.temperature * 10) / 10);
|
||||
}
|
||||
|
||||
/** ADC conversion test
|
||||
*
|
||||
* Purpose: Check if raw data from 2 voltage and 2 current measurements are converted
|
||||
* to calculated voltage/current measurements of different DC buses
|
||||
*/
|
||||
void adc_tests()
|
||||
{
|
||||
adcval.bat_temperature = 25;
|
||||
adcval.battery_voltage = 12;
|
||||
adcval.dcdc_current = 3;
|
||||
adcval.internal_temperature = 25;
|
||||
adcval.load_current = 1;
|
||||
adcval.mcu_temperature = 25;
|
||||
adcval.solar_voltage = 30;
|
||||
prepare_adc_readings(adcval);
|
||||
|
||||
// call original update_measurements function
|
||||
update_measurements(&dcdc, &bat_state, &load, &hs_bus, &ls_bus, &load_bus);
|
||||
|
||||
UNITY_BEGIN();
|
||||
|
||||
RUN_TEST(check_solar_bus_readings);
|
||||
RUN_TEST(check_battery_bus_readings);
|
||||
RUN_TEST(check_load_bus_readings);
|
||||
//RUN_TEST(check_temperature_readings); // TODO
|
||||
|
||||
UNITY_END();
|
||||
}
|
||||
@@ -2,7 +2,7 @@
|
||||
#include "tests.h"
|
||||
|
||||
#include "battery.h"
|
||||
#include "power_port.h"
|
||||
#include "dc_bus.h"
|
||||
#include "charger.h"
|
||||
|
||||
#include <time.h>
|
||||
@@ -10,20 +10,20 @@
|
||||
|
||||
extern battery_conf_t bat_conf;
|
||||
extern battery_state_t bat_state;
|
||||
extern power_port_t ls_port;
|
||||
extern dc_bus_t ls_bus;
|
||||
|
||||
static void init_structs()
|
||||
{
|
||||
battery_conf_init(&bat_conf, BAT_TYPE_FLOODED, 6, 100);
|
||||
battery_state_init(&bat_state);
|
||||
power_port_init_bat(&ls_port, &bat_conf);
|
||||
dc_bus_init_bat(&ls_bus, &bat_conf);
|
||||
bat_state.chg_state = CHG_STATE_IDLE;
|
||||
}
|
||||
|
||||
void no_start_at_high_voltage()
|
||||
{
|
||||
init_structs();
|
||||
charger_state_machine(&ls_port, &bat_conf, &bat_state, bat_conf.voltage_recharge + 0.1, 0);
|
||||
charger_state_machine(&ls_bus, &bat_conf, &bat_state, bat_conf.voltage_recharge + 0.1, 0);
|
||||
TEST_ASSERT_EQUAL(CHG_STATE_IDLE, bat_state.chg_state);
|
||||
}
|
||||
|
||||
@@ -31,7 +31,7 @@ void no_start_after_short_rest()
|
||||
{
|
||||
init_structs();
|
||||
bat_state.time_state_changed = time(NULL) - bat_conf.time_limit_recharge + 1;
|
||||
charger_state_machine(&ls_port, &bat_conf, &bat_state, bat_conf.voltage_recharge - 0.1, 0);
|
||||
charger_state_machine(&ls_bus, &bat_conf, &bat_state, bat_conf.voltage_recharge - 0.1, 0);
|
||||
TEST_ASSERT_EQUAL(CHG_STATE_IDLE, bat_state.chg_state);
|
||||
}
|
||||
|
||||
@@ -39,11 +39,11 @@ void no_start_outside_temperature_limits()
|
||||
{
|
||||
init_structs();
|
||||
bat_state.temperature = bat_conf.charge_temp_max + 1;
|
||||
charger_state_machine(&ls_port, &bat_conf, &bat_state, bat_conf.voltage_recharge - 0.1, 0);
|
||||
charger_state_machine(&ls_bus, &bat_conf, &bat_state, bat_conf.voltage_recharge - 0.1, 0);
|
||||
TEST_ASSERT_EQUAL(CHG_STATE_IDLE, bat_state.chg_state);
|
||||
|
||||
bat_state.temperature = bat_conf.charge_temp_min - 1;
|
||||
charger_state_machine(&ls_port, &bat_conf, &bat_state, bat_conf.voltage_recharge - 0.1, 0);
|
||||
charger_state_machine(&ls_bus, &bat_conf, &bat_state, bat_conf.voltage_recharge - 0.1, 0);
|
||||
TEST_ASSERT_EQUAL(CHG_STATE_IDLE, bat_state.chg_state);
|
||||
}
|
||||
|
||||
@@ -51,7 +51,7 @@ void start_if_everything_just_fine()
|
||||
{
|
||||
init_structs();
|
||||
bat_state.time_state_changed = time(NULL) - bat_conf.time_limit_recharge - 1;
|
||||
charger_state_machine(&ls_port, &bat_conf, &bat_state, bat_conf.voltage_recharge - 0.1, 0);
|
||||
charger_state_machine(&ls_bus, &bat_conf, &bat_state, bat_conf.voltage_recharge - 0.1, 0);
|
||||
TEST_ASSERT_EQUAL(CHG_STATE_BULK, bat_state.chg_state);
|
||||
}
|
||||
|
||||
@@ -59,9 +59,9 @@ void enter_topping_at_voltage_setpoint()
|
||||
{
|
||||
init_structs();
|
||||
bat_state.time_state_changed = time(NULL) - bat_conf.time_limit_recharge - 1;
|
||||
charger_state_machine(&ls_port, &bat_conf, &bat_state, bat_conf.voltage_recharge - 0.1, 0);
|
||||
charger_state_machine(&ls_bus, &bat_conf, &bat_state, bat_conf.voltage_recharge - 0.1, 0);
|
||||
TEST_ASSERT_EQUAL(CHG_STATE_BULK, bat_state.chg_state);
|
||||
charger_state_machine(&ls_port, &bat_conf, &bat_state, bat_conf.voltage_topping + 0.1, 0);
|
||||
charger_state_machine(&ls_bus, &bat_conf, &bat_state, bat_conf.voltage_topping + 0.1, 0);
|
||||
TEST_ASSERT_EQUAL(CHG_STATE_TOPPING, bat_state.chg_state);
|
||||
}
|
||||
|
||||
@@ -70,11 +70,11 @@ void stop_topping_after_time_limit()
|
||||
enter_topping_at_voltage_setpoint();
|
||||
|
||||
bat_state.time_state_changed = time(NULL) - bat_conf.time_limit_topping + 1;
|
||||
charger_state_machine(&ls_port, &bat_conf, &bat_state, bat_conf.voltage_topping + 0.1, bat_conf.current_cutoff_topping + 0.1);
|
||||
charger_state_machine(&ls_bus, &bat_conf, &bat_state, bat_conf.voltage_topping + 0.1, bat_conf.current_cutoff_topping + 0.1);
|
||||
TEST_ASSERT_EQUAL(CHG_STATE_TOPPING, bat_state.chg_state);
|
||||
|
||||
bat_state.time_state_changed = time(NULL) - bat_conf.time_limit_topping - 1;
|
||||
charger_state_machine(&ls_port, &bat_conf, &bat_state, bat_conf.voltage_topping + 0.1, bat_conf.current_cutoff_topping + 0.1);
|
||||
charger_state_machine(&ls_bus, &bat_conf, &bat_state, bat_conf.voltage_topping + 0.1, bat_conf.current_cutoff_topping + 0.1);
|
||||
TEST_ASSERT_EQUAL(CHG_STATE_TRICKLE, bat_state.chg_state);
|
||||
}
|
||||
|
||||
@@ -83,7 +83,7 @@ void stop_topping_at_cutoff_current()
|
||||
enter_topping_at_voltage_setpoint();
|
||||
|
||||
bat_state.time_state_changed = time(NULL) - 1;
|
||||
charger_state_machine(&ls_port, &bat_conf, &bat_state, bat_conf.voltage_topping + 0.1, bat_conf.current_cutoff_topping - 0.1);
|
||||
charger_state_machine(&ls_bus, &bat_conf, &bat_state, bat_conf.voltage_topping + 0.1, bat_conf.current_cutoff_topping - 0.1);
|
||||
TEST_ASSERT_EQUAL(CHG_STATE_TRICKLE, bat_state.chg_state);
|
||||
}
|
||||
|
||||
@@ -93,7 +93,7 @@ void trickle_to_idle_for_li_ion()
|
||||
battery_conf_init(&bat_conf, BAT_TYPE_LFP, 4, 100);
|
||||
|
||||
bat_state.time_state_changed = time(NULL) - 1;
|
||||
charger_state_machine(&ls_port, &bat_conf, &bat_state, bat_conf.voltage_topping + 0.1, bat_conf.current_cutoff_topping - 0.1);
|
||||
charger_state_machine(&ls_bus, &bat_conf, &bat_state, bat_conf.voltage_topping + 0.1, bat_conf.current_cutoff_topping - 0.1);
|
||||
TEST_ASSERT_EQUAL(CHG_STATE_IDLE, bat_state.chg_state);
|
||||
}
|
||||
|
||||
@@ -103,7 +103,7 @@ void trickle_to_equalization_if_enabled()
|
||||
bat_conf.equalization_enabled = true;
|
||||
|
||||
bat_state.time_state_changed = time(NULL) - 1;
|
||||
charger_state_machine(&ls_port, &bat_conf, &bat_state, bat_conf.voltage_topping + 0.1, bat_conf.current_cutoff_topping - 0.1);
|
||||
charger_state_machine(&ls_bus, &bat_conf, &bat_state, bat_conf.voltage_topping + 0.1, bat_conf.current_cutoff_topping - 0.1);
|
||||
TEST_ASSERT_EQUAL(CHG_STATE_EQUALIZATION, bat_state.chg_state);
|
||||
}
|
||||
|
||||
@@ -113,11 +113,11 @@ void no_trickle_if_low_current_because_of_low_input()
|
||||
|
||||
bat_state.time_state_changed = time(NULL) - 200;
|
||||
bat_state.time_voltage_limit_reached = time(NULL) - 3;
|
||||
charger_state_machine(&ls_port, &bat_conf, &bat_state, bat_conf.voltage_topping - 0.1, bat_conf.current_cutoff_topping - 0.1);
|
||||
charger_state_machine(&ls_bus, &bat_conf, &bat_state, bat_conf.voltage_topping - 0.1, bat_conf.current_cutoff_topping - 0.1);
|
||||
TEST_ASSERT_EQUAL(CHG_STATE_TOPPING, bat_state.chg_state);
|
||||
|
||||
bat_state.time_voltage_limit_reached = time(NULL) - 1;
|
||||
charger_state_machine(&ls_port, &bat_conf, &bat_state, bat_conf.voltage_topping - 0.1, bat_conf.current_cutoff_topping - 0.1);
|
||||
charger_state_machine(&ls_bus, &bat_conf, &bat_state, bat_conf.voltage_topping - 0.1, bat_conf.current_cutoff_topping - 0.1);
|
||||
TEST_ASSERT_EQUAL(CHG_STATE_TRICKLE, bat_state.chg_state);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,121 @@
|
||||
|
||||
#include "tests.h"
|
||||
#include "adc_dma_stub.h"
|
||||
#include "adc_dma.h"
|
||||
|
||||
#include "battery.h"
|
||||
#include "dc_bus.h"
|
||||
#include "charger.h"
|
||||
#include "log.h"
|
||||
|
||||
#include <time.h>
|
||||
#include <stdio.h>
|
||||
#include <math.h>
|
||||
|
||||
extern dcdc_t dcdc;
|
||||
extern dc_bus_t hs_bus;
|
||||
extern dc_bus_t ls_bus;
|
||||
extern dc_bus_t *bat_port;
|
||||
extern dc_bus_t load_bus;
|
||||
extern battery_state_t bat_state;
|
||||
extern battery_conf_t bat_conf;
|
||||
extern load_output_t load;
|
||||
extern log_data_t log_data;
|
||||
|
||||
static void init_structs()
|
||||
{
|
||||
battery_conf_init(&bat_conf, BAT_TYPE_FLOODED, 6, 100);
|
||||
battery_state_init(&bat_state);
|
||||
dc_bus_init_bat(&ls_bus, &bat_conf);
|
||||
bat_state.chg_state = CHG_STATE_IDLE;
|
||||
}
|
||||
|
||||
static adc_values_t adcval;
|
||||
|
||||
const float dcdc_current_sun = 3;
|
||||
const float load_current = 1;
|
||||
const int sun_hours = 1;
|
||||
const int night_hours = 3;
|
||||
|
||||
void energy_calculation_init()
|
||||
{
|
||||
log_data.solar_in_total_Wh = 0;
|
||||
log_data.load_out_total_Wh = 0;
|
||||
log_data.bat_chg_total_Wh = 0;
|
||||
log_data.bat_dis_total_Wh = 0;
|
||||
hs_bus.dis_energy_Wh = 0.0;
|
||||
load_bus.chg_energy_Wh = 0.0;
|
||||
ls_bus.chg_energy_Wh = 0.0;
|
||||
ls_bus.dis_energy_Wh = 0.0;
|
||||
|
||||
// set desired measurement values
|
||||
adcval.bat_temperature = 25;
|
||||
adcval.battery_voltage = 12;
|
||||
adcval.dcdc_current = dcdc_current_sun;
|
||||
adcval.internal_temperature = 25;
|
||||
adcval.load_current = load_current;
|
||||
adcval.mcu_temperature = 25;
|
||||
adcval.solar_voltage = 30;
|
||||
|
||||
// insert values into ADC functions
|
||||
prepare_adc_readings(adcval);
|
||||
update_measurements(&dcdc, &bat_state, &load, &hs_bus, &ls_bus, &load_bus);
|
||||
|
||||
for (int i = 0; i < 60*60*sun_hours; i++) {
|
||||
dc_bus_energy_balance(&hs_bus);
|
||||
dc_bus_energy_balance(&ls_bus);
|
||||
dc_bus_energy_balance(&load_bus);
|
||||
}
|
||||
|
||||
// disable DC/DC = solar charging
|
||||
adcval.dcdc_current = 0;
|
||||
prepare_adc_readings(adcval);
|
||||
update_measurements(&dcdc, &bat_state, &load, &hs_bus, &ls_bus, &load_bus);
|
||||
|
||||
for (int i = 0; i < 60*60*night_hours; i++) {
|
||||
dc_bus_energy_balance(&hs_bus);
|
||||
dc_bus_energy_balance(&ls_bus);
|
||||
dc_bus_energy_balance(&load_bus);
|
||||
}
|
||||
}
|
||||
|
||||
void charging_energy_calculation_valid()
|
||||
{
|
||||
energy_calculation_init();
|
||||
// charging only during sun hours
|
||||
TEST_ASSERT_EQUAL_FLOAT(round(sun_hours * ls_bus.voltage * (dcdc_current_sun - load_current)), round(ls_bus.chg_energy_Wh));
|
||||
}
|
||||
|
||||
void discharging_energy_calculation_valid()
|
||||
{
|
||||
energy_calculation_init();
|
||||
// discharging (sum of current) only during dis hours
|
||||
TEST_ASSERT_EQUAL_FLOAT(round(night_hours * ls_bus.voltage * adcval.load_current), round(ls_bus.dis_energy_Wh));
|
||||
}
|
||||
|
||||
void solar_input_energy_calculation_valid()
|
||||
{
|
||||
energy_calculation_init();
|
||||
TEST_ASSERT_EQUAL_FLOAT(round(sun_hours * ls_bus.voltage * dcdc_current_sun), round(hs_bus.dis_energy_Wh));
|
||||
}
|
||||
|
||||
void load_output_energy_calculation_valid()
|
||||
{
|
||||
energy_calculation_init();
|
||||
TEST_ASSERT_EQUAL_FLOAT(round((sun_hours + night_hours) * ls_bus.voltage * adcval.load_current), round(load_bus.chg_energy_Wh));
|
||||
}
|
||||
|
||||
void dc_bus_tests()
|
||||
{
|
||||
energy_calculation_init();
|
||||
|
||||
UNITY_BEGIN();
|
||||
|
||||
// energy calculation
|
||||
RUN_TEST(charging_energy_calculation_valid);
|
||||
RUN_TEST(discharging_energy_calculation_valid);
|
||||
RUN_TEST(solar_input_energy_calculation_valid);
|
||||
RUN_TEST(load_output_energy_calculation_valid);
|
||||
|
||||
UNITY_END();
|
||||
}
|
||||
Reference in New Issue
Block a user