Solar, bat and load power added to data objects

This commit is contained in:
Martin Jaeger
2019-06-18 17:04:33 +02:00
parent 375fcee514
commit 690e204a16
11 changed files with 36 additions and 29 deletions
+9 -7
View File
@@ -82,7 +82,7 @@ void calibrate_current_sensors()
}
//----------------------------------------------------------------------------
void update_measurements(dcdc_t *dcdc, charger_t *charger, load_output_t *load, dc_bus_t *hs, dc_bus_t *ls, dc_bus_t *load_bus)
void update_measurements(dcdc_t *dcdc, charger_t *charger, dc_bus_t *hs, dc_bus_t *ls, dc_bus_t *load_bus)
{
//int v_temp, rts;
@@ -100,7 +100,6 @@ void update_measurements(dcdc_t *dcdc, charger_t *charger, load_output_t *load,
(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 =
@@ -110,26 +109,29 @@ void update_measurements(dcdc_t *dcdc, charger_t *charger, load_output_t *load,
hs->voltage = ls->voltage + -(vcc * ADC_OFFSET_V_SOLAR / 1000.0 + hs->voltage);
#endif
load->current =
load_bus->current =
(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;
#ifdef CHARGER_TYPE_PWM
// 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;
ls->current = -hs->current - load_bus->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 + solar_current_offset;
ls->current = dcdc->ls_current - load->current;
ls->current = dcdc->ls_current - load_bus->current;
hs->current = -dcdc->ls_current * ls->voltage / hs->voltage;
#endif
// power calculations
hs->power = hs->voltage * hs->current;
ls->power = ls->voltage * ls->current;
load_bus->power = load_bus->voltage * load_bus->current;
/** \todo Improved (faster) temperature calculation:
https://www.embeddedrelated.com/showarticle/91.php
*/
+1 -1
View File
@@ -40,7 +40,7 @@ void detect_battery_temperature(charger_t *bat, float bat_temp);
/** Updates structures with data read from ADC
*/
void update_measurements(dcdc_t *dcdc, charger_t *charger, load_output_t *load, dc_bus_t *hs, dc_bus_t *ls, dc_bus_t *load_bus);
void update_measurements(dcdc_t *dcdc, charger_t *charger, dc_bus_t *hs, dc_bus_t *ls, dc_bus_t *load_bus);
/** Initializes registers and starts ADC timer
*/
+4 -1
View File
@@ -131,7 +131,7 @@ const data_object_t data_objects[] = {
{0x70, TS_OUTPUT, TS_READ_ALL, TS_T_FLOAT32, 2, (void*) &(ls_bus.voltage), "Bat_V"},
{0x71, TS_OUTPUT, TS_READ_ALL, TS_T_FLOAT32, 2, (void*) &(hs_bus.voltage), "Solar_V"},
{0x72, TS_OUTPUT, TS_READ_ALL, TS_T_FLOAT32, 2, (void*) &(ls_bus.current), "Bat_A"},
{0x73, TS_OUTPUT, TS_READ_ALL, TS_T_FLOAT32, 2, (void*) &(load.current), "Load_A"},
{0x73, TS_OUTPUT, TS_READ_ALL, TS_T_FLOAT32, 2, (void*) &(load.bus->current), "Load_A"},
{0x74, TS_OUTPUT, TS_READ_ALL, TS_T_FLOAT32, 1, (void*) &(charger.bat_temperature), "Bat_degC"},
{0x75, TS_OUTPUT, TS_READ_ALL, TS_T_BOOL, 1, (void*) &(charger.ext_temp_sensor), "BatTempExt"},
{0x76, TS_OUTPUT, TS_READ_ALL, TS_T_FLOAT32, 1, (void*) &(mcu_temp), "MCU_degC"},
@@ -143,6 +143,9 @@ const data_object_t data_objects[] = {
{0x7A, TS_OUTPUT, TS_READ_ALL, TS_T_FLOAT32, 2, (void*) &(hs_bus.current), "Solar_A"},
{0x7B, TS_OUTPUT, TS_READ_ALL, TS_T_FLOAT32, 2, (void*) &(ls_bus.chg_voltage_target), "BatTarget_V"},
{0x7C, TS_OUTPUT, TS_READ_ALL, TS_T_FLOAT32, 2, (void*) &(ls_bus.chg_current_max), "BatTarget_A"},
{0x7D, TS_OUTPUT, TS_READ_ALL, TS_T_FLOAT32, 2, (void*) &(ls_bus.power), "Bat_W"},
{0x7E, TS_OUTPUT, TS_READ_ALL, TS_T_FLOAT32, 2, (void*) &(hs_bus.power), "Solar_W"},
{0x7F, TS_OUTPUT, TS_READ_ALL, TS_T_FLOAT32, 2, (void*) &(load_bus.power), "Load_W"},
// RECORDED DATA ///////////////////////////////////////////////////////
// using IDs >= 0xA0
+1
View File
@@ -34,6 +34,7 @@
typedef struct {
float voltage; ///< measured voltage
float current; ///< sum of currents (positive sign = increasing voltage/charge of the bus)
float power; ///< multiplication of power & voltage
bool chg_allowed; ///< charging direction for battery port
float chg_voltage_target; ///< target voltage if port is configured as output
+5 -4
View File
@@ -178,8 +178,9 @@ void load_usb_set(bool enabled)
#endif
}
void load_init(load_output_t *load)
void load_init(load_output_t *load, dc_bus_t *bus)
{
load->bus = bus;
load->current_max = LOAD_CURRENT_MAX;
load->enabled_target = true;
load->usb_enabled_target = true;
@@ -275,7 +276,7 @@ void load_state_machine(load_output_t *load, bool source_enabled)
}
break;
case LOAD_STATE_OFF_OVERVOLTAGE:
if (load->voltage < LOW_SIDE_VOLTAGE_MAX) { // TODO: add hysteresis?
if (load->bus->voltage < LOW_SIDE_VOLTAGE_MAX) { // TODO: add hysteresis?
load->switch_state = LOAD_STATE_DISABLED; // switch to normal mode again
}
break;
@@ -305,7 +306,7 @@ void load_control(load_output_t *load)
// junction temperature calculation model for overcurrent detection
load->junction_temperature = load->junction_temperature + (
mcu_temp - load->junction_temperature +
load->current * load->current / (LOAD_CURRENT_MAX * LOAD_CURRENT_MAX) * (MOSFET_MAX_JUNCTION_TEMP - 25)
load->bus->current * load->bus->current / (LOAD_CURRENT_MAX * LOAD_CURRENT_MAX) * (MOSFET_MAX_JUNCTION_TEMP - 25)
) / (MOSFET_THERMAL_TIME_CONSTANT * CONTROL_FREQUENCY);
if (load->junction_temperature > MOSFET_MAX_JUNCTION_TEMP) {
@@ -316,7 +317,7 @@ void load_control(load_output_t *load)
}
static int debounce_counter = 0;
if (load->voltage > LOW_SIDE_VOLTAGE_MAX) {
if (load->bus->voltage > LOW_SIDE_VOLTAGE_MAX) {
debounce_counter++;
if (debounce_counter > CONTROL_FREQUENCY) { // waited 1s before setting the flag
load_switch_set(false);
+3 -3
View File
@@ -25,6 +25,7 @@
#include <stdbool.h>
#include <stdint.h>
#include "dc_bus.h"
/** Load/USB output states
*
@@ -47,9 +48,8 @@ typedef struct {
uint16_t switch_state; ///< Current state of load output switch
uint16_t usb_state; ///< Current state of USB output
float voltage; ///< actual voltage measurement
dc_bus_t *bus; ///< Pointer to DC bus containting actual voltage and current measurement
float current; ///< actual current measurement
float current_max; ///< maximum allowed current
int overcurrent_timestamp; ///< time at which an overcurrent event occured
@@ -61,7 +61,7 @@ typedef struct {
/** Initialize load_output_t struct and overcurrent / short circuit protection comparator (if existing)
*/
void load_init(load_output_t *load);
void load_init(load_output_t *load, dc_bus_t *bus);
/** Enable/disable load switch
*/
+5 -5
View File
@@ -81,12 +81,12 @@ void log_update_min_max_values(log_data_t *log_data, dcdc_t *dcdc, charger_t *ch
log_data->dcdc_current_max = dcdc->ls_current;
}
if (load->current > log_data->load_current_max) {
log_data->load_current_max = load->current;
if (load->bus->current > log_data->load_current_max) {
log_data->load_current_max = load->bus->current;
}
if (solar_bus->current < 0) {
uint16_t solar_power = -solar_bus->voltage * solar_bus->current;
uint16_t solar_power = -solar_bus->power;
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) {
@@ -95,8 +95,8 @@ void log_update_min_max_values(log_data_t *log_data, dcdc_t *dcdc, charger_t *ch
}
}
if (load->current > 0) {
uint16_t load_power = load->voltage * load->current;
if (load->bus->current > 0) {
uint16_t load_power = load->bus->voltage * load->bus->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) {
+3 -3
View File
@@ -67,7 +67,7 @@ void system_control()
static int counter = 0;
// convert ADC readings to meaningful measurement values
update_measurements(&dcdc, &charger, &load, &hs_bus, &ls_bus, &load_bus);
update_measurements(&dcdc, &charger, &hs_bus, &ls_bus, &load_bus);
#ifdef CHARGER_TYPE_PWM
pwm_switch_control(&pwm_switch, &hs_bus, bat_bus);
@@ -109,7 +109,7 @@ int main()
battery_conf_overwrite(&bat_conf, &bat_conf_user); // initialize conf_user with same values
charger_init(&charger);
load_init(&load);
load_init(&load, &load_bus);
#ifdef CHARGER_TYPE_PWM
pwm_switch_init(&pwm_switch);
@@ -129,7 +129,7 @@ int main()
dma_setup();
adc_timer_start(1000); // 1 kHz
wait(0.5); // wait for ADC to collect some measurement values
update_measurements(&dcdc, &charger, &load, &hs_bus, &ls_bus, &load_bus);
update_measurements(&dcdc, &charger, &hs_bus, &ls_bus, &load_bus);
calibrate_current_sensors();
// Communication interfaces
+2 -2
View File
@@ -42,7 +42,7 @@ void half_bridge_init(int freq_kHz, int deadtime_ns, float min_duty, float max_d
void half_bridge_set_duty_cycle(float duty)
{
float duty_target;
float duty_target = 0; // dummy
// protection against wrong settings which could destroy the hardware
if (duty < _min_duty) {
duty_target = _min_duty;
@@ -59,7 +59,7 @@ void half_bridge_set_duty_cycle(float duty)
void half_bridge_duty_cycle_step(int delta)
{
float duty_target;
float duty_target = 0; // dummy
//duty_target = (float)(TIM1->CCR1 + delta) / (_pwm_resolution / 2);
// protection against wrong settings which could destroy the hardware
+1 -1
View File
@@ -53,7 +53,7 @@ void adc_tests()
prepare_adc_readings(adcval);
// call original update_measurements function
update_measurements(&dcdc, &charger, &load, &hs_bus, &ls_bus, &load_bus);
update_measurements(&dcdc, &charger, &hs_bus, &ls_bus, &load_bus);
UNITY_BEGIN();
+2 -2
View File
@@ -58,7 +58,7 @@ void energy_calculation_init()
// insert values into ADC functions
prepare_adc_readings(adcval);
update_measurements(&dcdc, &charger, &load, &hs_bus, &ls_bus, &load_bus);
update_measurements(&dcdc, &charger, &hs_bus, &ls_bus, &load_bus);
for (int i = 0; i < 60*60*sun_hours; i++) {
dc_bus_energy_balance(&hs_bus);
@@ -69,7 +69,7 @@ void energy_calculation_init()
// disable DC/DC = solar charging
adcval.dcdc_current = 0;
prepare_adc_readings(adcval);
update_measurements(&dcdc, &charger, &load, &hs_bus, &ls_bus, &load_bus);
update_measurements(&dcdc, &charger, &hs_bus, &ls_bus, &load_bus);
for (int i = 0; i < 60*60*night_hours; i++) {
dc_bus_energy_balance(&hs_bus);