Restructuring of data types (structs) and files

- dcdc_port_t renamed to power_port_t for future use in PWM charge controllers
- structs.h split into battery.h, log.h and charger.h
- bat_charger.cpp/h split into battery.cpp/h and charger.cpp/h
This commit is contained in:
Martin Jaeger
2019-03-10 17:19:44 +01:00
parent 871c8e46be
commit 4e0b7f2a12
19 changed files with 428 additions and 324 deletions
+8 -3
View File
@@ -18,6 +18,7 @@
#include "mbed.h"
#include "pcb.h" // contains defines for pins
#include <math.h> // log for thermistor calculation
#include "log.h"
// factory calibration values for internal voltage reference and temperature sensor (see MCU datasheet, not RM)
#if defined(STM32F0)
@@ -60,7 +61,7 @@ void calibrate_current_sensors(dcdc_t *dcdc, load_output_t *load)
}
//----------------------------------------------------------------------------
void update_measurements(dcdc_t *dcdc, battery_t *bat, load_output_t *load, dcdc_port_t *hs, dcdc_port_t *ls)
void update_measurements(dcdc_t *dcdc, battery_t *bat, load_output_t *load, power_port_t *hs, power_port_t *ls)
{
//int v_temp, rts;
@@ -81,6 +82,10 @@ void update_measurements(dcdc_t *dcdc, battery_t *bat, load_output_t *load, dcdc
(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
load->current =
(float)(((adc_filtered[ADC_POS_I_LOAD] >> (4 + ADC_FILTER_CONST)) * vcc) / 4096) *
ADC_GAIN_I_LOAD / 1000.0 + load_current_offset;
@@ -288,7 +293,7 @@ void adc_setup()
#if defined(STM32F0)
void adc_timer_setup(int freq_Hz) // max. 10 kHz
void adc_timer_start(int freq_Hz) // max. 10 kHz
{
// Enable TIM15 clock
RCC->APB2ENR |= RCC_APB2ENR_TIM15EN;
@@ -319,7 +324,7 @@ extern "C" void TIM15_IRQHandler(void)
#elif defined(STM32L0)
void adc_timer_setup(int freq_Hz) // max. 10 kHz
void adc_timer_start(int freq_Hz) // max. 10 kHz
{
// Enable TIM6 clock
RCC->APB1ENR |= RCC_APB1ENR_TIM6EN;
+3 -3
View File
@@ -19,12 +19,12 @@
#include "dcdc.h"
#include "load.h"
#include "structs.h"
#include "battery.h"
void calibrate_current_sensors(dcdc_t *dcdc, load_output_t *load);
void update_measurements(dcdc_t *dcdc, battery_t *bat, load_output_t *load, dcdc_port_t *hs, dcdc_port_t *ls);
void update_measurements(dcdc_t *dcdc, battery_t *bat, load_output_t *load, power_port_t *hs, power_port_t *ls);
void adc_timer_setup(int freq_Hz);
void adc_timer_start(int freq_Hz);
void adc_setup(void);
void dma_setup(void);
+13 -148
View File
@@ -14,32 +14,22 @@
* limitations under the License.
*/
#include "bat_charger.h"
#include "mbed.h"
#include "battery.h"
#include "config.h"
#include "pcb.h"
#include "half_bridge.h"
#include "load.h"
#include <time.h> // for time(NULL) function
#include "power_port.h"
#include "load.h"
#include "log.h"
#include <math.h> // for fabs function
extern load_output_t load; // ToDo: Remove global definitions!
extern dcdc_port_t ls_port;
extern dcdc_port_t hs_port;
// ToDo: Remove global definitions!
extern power_port_t ls_port;
extern power_port_t hs_port;
extern load_output_t load;
extern log_data_t log_data;
extern dcdc_t dcdc;
// private function
void _enter_state(battery_t* bat, int next_state)
{
//printf("Enter State: %d\n", next_state);
bat->time_state_changed = time(NULL);
bat->state = next_state;
}
// weak function to be overridden by user in config.cpp
WEAK void battery_init_user(battery_t *bat, battery_user_settings_t *bat_user) {;}
void battery_init(battery_t *bat, battery_user_settings_t *bat_user)
{
@@ -150,6 +140,9 @@ void battery_init(battery_t *bat, battery_user_settings_t *bat_user)
battery_init_user(bat, bat_user);
}
// weak function to be overridden by user in config.cpp
WEAK void battery_init_user(battery_t *bat, battery_user_settings_t *bat_user) {;}
// checks settings in bat_user for plausibility and writes them to bat if everything is fine
bool battery_user_settings(battery_t *bat, battery_user_settings_t *bat_user)
{
@@ -198,134 +191,6 @@ bool battery_user_settings(battery_t *bat, battery_user_settings_t *bat_user)
return false;
}
/*****************************************************************************
* Charger state machine
## Idle
Initial state of the charge controller. If the solar voltage is high enough
and the battery is not full, charging in CC mode is started.
## CC / bulk charging
The battery is charged with maximum possible current (MPPT algorithm is
active) until the CV voltage limit is reached.
## CV / absorption charging
Lead-acid batteries are charged for some time using a slightly higher charge
voltage. After a current cutoff limit or a time limit is reached, the charger
goes into trickle or equalization mode for lead-acid batteries or back into
Standby for Li-ion batteries.
## Trickle charging
This mode is kept forever for a lead-acid battery and keeps the battery at
full state of charge. If too much power is drawn from the battery, the
charger switches back into CC / bulk charging mode.
## Equalization charging
This mode is only used for lead-acid batteries after several deep-discharge
cycles or a very long period of time with no equalization. Voltage is
increased to 15V or above, so care must be taken for the other system
components attached to the battery. (currently, no equalization charging is
enabled in the software)
*/
void charger_state_machine(dcdc_port_t *port, battery_t *bat, float voltage, float current)
{
//printf("time_state_change = %d, time = %d, v_bat = %f, i_bat = %f\n", bat->time_state_changed, time(NULL), voltage, current);
// Load management
// battery port input state (i.e. battery discharging direction) defines load state
if (port->input_allowed == true &&
voltage < bat->cell_voltage_load_disconnect * bat->num_cells)
{
port->input_allowed = false;
bat->num_deep_discharges++;
if (bat->useable_capacity < 0.1) {
bat->useable_capacity = bat->discharged_Ah;
} else {
// slowly adapt new measurements with low-pass filter
bat->useable_capacity = 0.8 * bat->useable_capacity + 0.2 * bat->discharged_Ah;
}
// simple SOH estimation
bat->soh = bat->useable_capacity / bat->nominal_capacity;
}
if (voltage >= bat->cell_voltage_load_reconnect * bat->num_cells) {
port->input_allowed = true;
}
// state machine
switch (bat->state) {
case CHG_STATE_IDLE: {
if (voltage < bat->num_cells * bat->cell_voltage_recharge
&& (time(NULL) - bat->time_state_changed) > bat->time_limit_recharge)
{
port->voltage_output_target = bat->num_cells * bat->cell_voltage_max;
port->current_output_max = bat->charge_current_max;
port->output_allowed = true;
bat->full = false;
_enter_state(bat, CHG_STATE_CC);
}
break;
}
case CHG_STATE_CC: {
if (voltage > port->voltage_output_target) {
port->voltage_output_target = bat->num_cells * bat->cell_voltage_max;
_enter_state(bat, CHG_STATE_CV);
}
break;
}
case CHG_STATE_CV: {
if (voltage >= port->voltage_output_target) {
bat->time_voltage_limit_reached = time(NULL);
}
// cut-off limit reached because battery full (i.e. CV mode still
// reached by available solar power within last 2s) or CV period long enough?
if ((current < bat->current_cutoff_CV && (time(NULL) - bat->time_voltage_limit_reached) < 2)
|| (time(NULL) - bat->time_state_changed) > bat->time_limit_CV)
{
bat->full = true;
bat->num_full_charges++;
bat->discharged_Ah = 0; // reset coulomb counter
if (bat->equalization_enabled) {
// TODO: additional conditions!
port->voltage_output_target = bat->num_cells * bat->cell_voltage_equalization;
port->current_output_max = bat->current_limit_equalization;
_enter_state(bat, CHG_STATE_EQUALIZATION);
}
else if (bat->trickle_enabled) {
port->voltage_output_target = bat->num_cells * bat->cell_voltage_trickle;
_enter_state(bat, CHG_STATE_TRICKLE);
}
else {
port->current_output_max = 0;
port->output_allowed = false;
_enter_state(bat, CHG_STATE_IDLE);
}
}
break;
}
case CHG_STATE_TRICKLE: {
if (voltage >= port->voltage_output_target) {
bat->time_voltage_limit_reached = time(NULL);
}
if (time(NULL) - bat->time_voltage_limit_reached > bat->time_trickle_recharge)
{
port->current_output_max = bat->charge_current_max;
port->voltage_output_target = bat->num_cells * bat->cell_voltage_max;
bat->full = false;
_enter_state(bat, CHG_STATE_CC);
}
// assumtion: trickle does not harm the battery --> never go back to idle
// (for Li-ion battery: disable trickle!)
break;
}
}
}
//----------------------------------------------------------------------------
// must be called exactly once per second, otherwise energy calculation gets wrong
void battery_update_energy(battery_t *bat, float voltage, float current)
+22 -26
View File
@@ -1,6 +1,6 @@
#ifndef __STRUCTS_H_
#define __STRUCTS_H_
#ifndef __BATTERY_H_
#define __BATTERY_H_
#include <stdbool.h>
#include <stdint.h>
@@ -16,7 +16,6 @@ extern "C" {
* Voltage: V/100 = 10 mV --> int16_t max. 327.68 V
*/
// battery types
enum bat_type {
BAT_TYPE_NONE = 0, // safe standard settings
@@ -148,34 +147,31 @@ typedef struct
} battery_user_settings_t;
// possible charger states
enum charger_state {
CHG_STATE_IDLE,
CHG_STATE_CC,
CHG_STATE_CV,
CHG_STATE_TRICKLE,
CHG_STATE_EQUALIZATION
};
/* Log Data
/* Basic initialization of battery
*
* Saves maximum ever measured values to EEPROM
* Configures battery based on settings defined in config.h and initializes
* bat_user with same values
*/
typedef struct {
float battery_voltage_max;
float solar_voltage_max;
float dcdc_current_max;
float load_current_max;
float temp_int; // °C (internal MCU temperature sensor)
float temp_int_max; // °C
float temp_mosfets_max;
int day_counter;
} log_data_t;
void battery_init(battery_t *bat, battery_user_settings_t *bat_user);
/* User settings of battery
*
* This function should be implemented in config.cpp
*/
void battery_init_user(battery_t *bat, battery_user_settings_t *bat_user);
/* Update battery settings
*
* Settings specified in bat_user will be copied to actual battery_t,
* if suggested updates are valid (includes plausibility check!)
*/
bool battery_user_settings(battery_t *bat, battery_user_settings_t *bat_user);
void battery_update_energy(battery_t *bat, float voltage, float current);
#ifdef __cplusplus
}
#endif
#endif // STRUCTS_H
#endif // __BATTERY_H_
+157
View File
@@ -0,0 +1,157 @@
/* 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 "charger.h"
#include "config.h"
#include "pcb.h"
#include "half_bridge.h"
#include "log.h"
// private function
void _enter_state(battery_t* bat, int next_state)
{
//printf("Enter State: %d\n", next_state);
bat->time_state_changed = time(NULL);
bat->state = next_state;
}
/*****************************************************************************
* Charger state machine
## Idle
Initial state of the charge controller. If the solar voltage is high enough
and the battery is not full, charging in CC mode is started.
## CC / bulk charging
The battery is charged with maximum possible current (MPPT algorithm is
active) until the CV voltage limit is reached.
## CV / absorption charging
Lead-acid batteries are charged for some time using a slightly higher charge
voltage. After a current cutoff limit or a time limit is reached, the charger
goes into trickle or equalization mode for lead-acid batteries or back into
Standby for Li-ion batteries.
## Trickle charging
This mode is kept forever for a lead-acid battery and keeps the battery at
full state of charge. If too much power is drawn from the battery, the
charger switches back into CC / bulk charging mode.
## Equalization charging
This mode is only used for lead-acid batteries after several deep-discharge
cycles or a very long period of time with no equalization. Voltage is
increased to 15V or above, so care must be taken for the other system
components attached to the battery. (currently, no equalization charging is
enabled in the software)
*/
void charger_state_machine(power_port_t *port, battery_t *bat, float voltage, float current)
{
//printf("time_state_change = %d, time = %d, v_bat = %f, i_bat = %f\n", bat->time_state_changed, time(NULL), voltage, current);
// Load management
// battery port input state (i.e. battery discharging direction) defines load state
if (port->input_allowed == true &&
voltage < bat->cell_voltage_load_disconnect * bat->num_cells)
{
port->input_allowed = false;
bat->num_deep_discharges++;
if (bat->useable_capacity < 0.1) {
bat->useable_capacity = bat->discharged_Ah;
} else {
// slowly adapt new measurements with low-pass filter
bat->useable_capacity = 0.8 * bat->useable_capacity + 0.2 * bat->discharged_Ah;
}
// simple SOH estimation
bat->soh = bat->useable_capacity / bat->nominal_capacity;
}
if (voltage >= bat->cell_voltage_load_reconnect * bat->num_cells) {
port->input_allowed = true;
}
// state machine
switch (bat->state) {
case CHG_STATE_IDLE: {
if (voltage < bat->num_cells * bat->cell_voltage_recharge
&& (time(NULL) - bat->time_state_changed) > bat->time_limit_recharge)
{
port->voltage_output_target = bat->num_cells * bat->cell_voltage_max;
port->current_output_max = bat->charge_current_max;
port->output_allowed = true;
bat->full = false;
_enter_state(bat, CHG_STATE_CC);
}
break;
}
case CHG_STATE_CC: {
if (voltage > port->voltage_output_target) {
port->voltage_output_target = bat->num_cells * bat->cell_voltage_max;
_enter_state(bat, CHG_STATE_CV);
}
break;
}
case CHG_STATE_CV: {
if (voltage >= port->voltage_output_target) {
bat->time_voltage_limit_reached = time(NULL);
}
// cut-off limit reached because battery full (i.e. CV mode still
// reached by available solar power within last 2s) or CV period long enough?
if ((current < bat->current_cutoff_CV && (time(NULL) - bat->time_voltage_limit_reached) < 2)
|| (time(NULL) - bat->time_state_changed) > bat->time_limit_CV)
{
bat->full = true;
bat->num_full_charges++;
bat->discharged_Ah = 0; // reset coulomb counter
if (bat->equalization_enabled) {
// TODO: additional conditions!
port->voltage_output_target = bat->num_cells * bat->cell_voltage_equalization;
port->current_output_max = bat->current_limit_equalization;
_enter_state(bat, CHG_STATE_EQUALIZATION);
}
else if (bat->trickle_enabled) {
port->voltage_output_target = bat->num_cells * bat->cell_voltage_trickle;
_enter_state(bat, CHG_STATE_TRICKLE);
}
else {
port->current_output_max = 0;
port->output_allowed = false;
_enter_state(bat, CHG_STATE_IDLE);
}
}
break;
}
case CHG_STATE_TRICKLE: {
if (voltage >= port->voltage_output_target) {
bat->time_voltage_limit_reached = time(NULL);
}
if (time(NULL) - bat->time_voltage_limit_reached > bat->time_trickle_recharge)
{
port->current_output_max = bat->charge_current_max;
port->voltage_output_target = bat->num_cells * bat->cell_voltage_max;
bat->full = false;
_enter_state(bat, CHG_STATE_CC);
}
// assumption: trickle does not harm the battery --> never go back to idle
// (for Li-ion battery: disable trickle!)
break;
}
}
}
+10 -22
View File
@@ -21,36 +21,24 @@
#include <stdint.h>
#include "dcdc.h"
#include "power_port.h"
#ifdef __cplusplus
extern "C" {
#endif
/* Basic initialization of battery
*
* Configures battery based on settings defined in config.h and initializes
* bat_user with same values
*/
void battery_init(battery_t *bat, battery_user_settings_t *bat_user);
/* User settings of battery
*
* This function should be implemented in config.cpp
*/
void battery_init_user(battery_t *bat, battery_user_settings_t *bat_user);
/* Update battery settings
*
* Settings specified in bat_user will be copied to actual battery_t,
* if suggested updates are valid (includes plausibility check!)
*/
bool battery_user_settings(battery_t *bat, battery_user_settings_t *bat_user);
// possible charger states
enum charger_state {
CHG_STATE_IDLE,
CHG_STATE_CC,
CHG_STATE_CV,
CHG_STATE_TRICKLE,
CHG_STATE_EQUALIZATION
};
/* Charger state machine update, should be called exactly once per second
*/
void charger_state_machine(dcdc_port_t *port, battery_t *bat, float voltage, float current);
void battery_update_energy(battery_t *bat, float voltage, float current);
void charger_state_machine(power_port_t *port, battery_t *bat, float voltage, float current);
#ifdef __cplusplus
}
+19 -17
View File
@@ -19,7 +19,8 @@
#include "config.h"
#include "thingset.h"
#include "structs.h"
#include "battery.h"
#include "log.h"
#include "dcdc.h"
#include "load.h"
#include "hardware.h"
@@ -29,8 +30,8 @@ extern log_data_t log_data;
extern battery_t bat;
extern dcdc_t dcdc;
extern load_output_t load;
extern dcdc_port_t hs_port;
extern dcdc_port_t ls_port;
extern power_port_t hs_port;
extern power_port_t ls_port;
extern battery_user_settings_t bat_user;
const char* manufacturer = "Libre Solar";
@@ -54,14 +55,13 @@ const data_object_t data_objects[] = {
{0x08, TS_CAT_OUTPUT, TS_ACCESS_READ, TS_T_UINT32, 0, (void*) &(bat.output_Wh_total), "OutputTotal_Wh"},
{0x09, TS_CAT_OUTPUT, TS_ACCESS_READ, TS_T_UINT16, 0, (void*) &(bat.soc), "SOC_%"}, // output will be uint8_t
{0x0A, TS_CAT_OUTPUT, TS_ACCESS_READ, TS_T_UINT16, 0, (void*) &(bat.useable_capacity), "BatUse_Ah"}, // useable battery capacity
{0x10, TS_CAT_OUTPUT, TS_ACCESS_READ, TS_T_UINT16, 0, (void*) &(bat.num_full_charges), "NumFullChg"},
{0x11, TS_CAT_OUTPUT, TS_ACCESS_READ, TS_T_UINT16, 0, (void*) &(bat.num_deep_discharges), "NumDeepDis"},
// normal priority data objects (IDs > 23 = 0x17)
{0x18, TS_CAT_INFO, TS_ACCESS_READ | TS_ACCESS_WRITE, TS_T_UINT32, 0, (void*) &(deviceID), "DeviceID"},
{0x19, TS_CAT_INFO, TS_ACCESS_READ, TS_T_STRING, 0, (void*) &(manufacturer), "Manufacturer"},
{0x1A, TS_CAT_INFO, TS_ACCESS_READ, TS_T_STRING, 0, (void*) &(deviceName), "DeviceName"},
{0x18, TS_CAT_INFO, TS_ACCESS_READ | TS_ACCESS_WRITE, TS_T_UINT32, 0, (void*) &(deviceID), "DeviceID"},
{0x19, TS_CAT_INFO, TS_ACCESS_READ, TS_T_STRING, 0, (void*) &(manufacturer), "Manufacturer"},
{0x1A, TS_CAT_INFO, TS_ACCESS_READ, TS_T_STRING, 0, (void*) &(deviceName), "DeviceName"},
// config
{0x20, TS_CAT_CONF, TS_ACCESS_READ | TS_ACCESS_WRITE, TS_T_FLOAT32, 1, (void*) &(bat_user.nominal_capacity), "BatNom_Ah"},
@@ -72,15 +72,15 @@ const data_object_t data_objects[] = {
{0x25, TS_CAT_CONF, TS_ACCESS_READ | TS_ACCESS_WRITE, TS_T_FLOAT32, 1, (void*) &(bat_user.current_cutoff_CV), "BatCutoff_A"},
{0x26, TS_CAT_CONF, TS_ACCESS_READ | TS_ACCESS_WRITE, TS_T_INT32, 0, (void*) &(bat_user.time_limit_CV), "BatCutoff_s"},
{0x27, TS_CAT_CONF, TS_ACCESS_READ | TS_ACCESS_WRITE, TS_T_BOOL, 0, (void*) &(bat_user.trickle_enabled), "TrickleEn"},
{0x27, TS_CAT_CONF, TS_ACCESS_READ | TS_ACCESS_WRITE, TS_T_FLOAT32, 0, (void*) &(bat_user.voltage_trickle), "Trickle_V"},
{0x28, TS_CAT_CONF, TS_ACCESS_READ | TS_ACCESS_WRITE, TS_T_INT32, 0, (void*) &(bat_user.time_trickle_recharge), "TrickleRecharge_s"},
{0x29, TS_CAT_CONF, TS_ACCESS_READ | TS_ACCESS_WRITE, TS_T_FLOAT32, 2, (void*) &(bat_user.voltage_load_disconnect), "LoadDisconnect_V"},
{0x29, TS_CAT_CONF, TS_ACCESS_READ | TS_ACCESS_WRITE, TS_T_FLOAT32, 2, (void*) &(bat_user.voltage_load_reconnect), "LoadReconnect_V"},
{0x2A, TS_CAT_CONF, TS_ACCESS_READ | TS_ACCESS_WRITE, TS_T_FLOAT32, 2, (void*) &(bat_user.temperature_compensation), "TempFactor"},
{0x28, TS_CAT_CONF, TS_ACCESS_READ | TS_ACCESS_WRITE, TS_T_FLOAT32, 0, (void*) &(bat_user.voltage_trickle), "Trickle_V"},
{0x29, TS_CAT_CONF, TS_ACCESS_READ | TS_ACCESS_WRITE, TS_T_INT32, 0, (void*) &(bat_user.time_trickle_recharge), "TrickleRecharge_s"},
{0x2A, TS_CAT_CONF, TS_ACCESS_READ | TS_ACCESS_WRITE, TS_T_FLOAT32, 2, (void*) &(bat_user.voltage_load_disconnect), "LoadDisconnect_V"},
{0x2B, TS_CAT_CONF, TS_ACCESS_READ | TS_ACCESS_WRITE, TS_T_FLOAT32, 2, (void*) &(bat_user.voltage_load_reconnect), "LoadReconnect_V"},
{0x2C, TS_CAT_CONF, TS_ACCESS_READ | TS_ACCESS_WRITE, TS_T_FLOAT32, 2, (void*) &(bat_user.temperature_compensation), "TempFactor"},
// input data
{0x30, TS_CAT_INPUT, TS_ACCESS_READ | TS_ACCESS_WRITE, TS_T_BOOL, 0, (void*) &(load.enabled_target), "LoadEn"},
{0x31, TS_CAT_INPUT, TS_ACCESS_READ | TS_ACCESS_WRITE, TS_T_BOOL, 0, (void*) &(load.usb_enabled_target), "USBEn"},
{0x31, TS_CAT_INPUT, TS_ACCESS_READ | TS_ACCESS_WRITE, TS_T_BOOL, 0, (void*) &(load.enabled_target), "LoadEnTarget"},
{0x32, TS_CAT_INPUT, TS_ACCESS_READ | TS_ACCESS_WRITE, TS_T_BOOL, 0, (void*) &(load.usb_enabled_target), "USBEnTarget"},
// output data
{0x41, TS_CAT_OUTPUT, TS_ACCESS_READ, TS_T_FLOAT32, 2, (void*) &(ls_port.voltage), "Bat_V"},
@@ -91,9 +91,9 @@ const data_object_t data_objects[] = {
{0x46, TS_CAT_OUTPUT, TS_ACCESS_READ, TS_T_FLOAT32, 2, (void*) &(log_data.dcdc_current_max), "BatMax_A"},
{0x47, TS_CAT_OUTPUT, TS_ACCESS_READ, TS_T_FLOAT32, 2, (void*) &(load.current), "Load_A"},
{0x48, TS_CAT_OUTPUT, TS_ACCESS_READ, TS_T_FLOAT32, 2, (void*) &(log_data.load_current_max), "LoadMax_A"},
{0x49, TS_CAT_OUTPUT, TS_ACCESS_READ, TS_T_FLOAT32, 1, (void*) &(bat.temperature), "Bat_degC"},
{0x4A, TS_CAT_OUTPUT, TS_ACCESS_READ, TS_T_FLOAT32, 1, (void*) &(dcdc.temp_mosfets), "Internal_degC"},
{0x4B, TS_CAT_OUTPUT, TS_ACCESS_READ, TS_T_FLOAT32, 1, (void*) &(log_data.temp_mosfets_max), "InternalMax_degC"},
{0x49, TS_CAT_OUTPUT, TS_ACCESS_READ, TS_T_FLOAT32, 1, (void*) &(bat.temperature), "Bat_°C"},
{0x4A, TS_CAT_OUTPUT, TS_ACCESS_READ, TS_T_FLOAT32, 1, (void*) &(dcdc.temp_mosfets), "Internal_°C"},
{0x4B, TS_CAT_OUTPUT, TS_ACCESS_READ, TS_T_FLOAT32, 1, (void*) &(log_data.temp_mosfets_max), "InternalMax_°C"},
{0x4D, TS_CAT_OUTPUT, TS_ACCESS_READ, TS_T_FLOAT32, 2, (void*) &(bat.input_Wh_day), "InputDay_Wh"},
{0x4E, TS_CAT_OUTPUT, TS_ACCESS_READ, TS_T_FLOAT32, 2, (void*) &(bat.output_Wh_day), "OutputDay_Wh"},
{0x50, TS_CAT_OUTPUT, TS_ACCESS_READ, TS_T_UINT16, 0, (void*) &(bat.state), "ChgState"},
@@ -101,6 +101,7 @@ const data_object_t data_objects[] = {
{0x52, TS_CAT_OUTPUT, TS_ACCESS_READ, TS_T_UINT16, 0, (void*) &(bat.soh), "SOH_%"}, // output will be uint8_t
{0x53, TS_CAT_OUTPUT, TS_ACCESS_READ, TS_T_UINT16, 0, (void*) &(bat.discharged_Ah), "Dis_Ah"}, // coulomb counter
{0x54, TS_CAT_OUTPUT, TS_ACCESS_READ, TS_T_UINT16, 0, (void*) &(bat.useable_capacity), "BatUse_Ah"}, // useable battery capacity
// ToDo: GPS position (latitude / longitude), see here: https://github.com/allthingstalk/cbor/blob/master/CBOR-Tag103-Geographic-Coordinates.md
// calibration data (write only after authentication)
{0x61, TS_CAT_CAL, TS_ACCESS_READ | TS_ACCESS_WRITE_AUTH, TS_T_FLOAT32, 1, (void*) &(dcdc.ls_current_min), "DcdcMin_A"},
@@ -111,6 +112,7 @@ const data_object_t data_objects[] = {
// rpc
{0x71, TS_CAT_EXEC, TS_ACCESS_EXEC, TS_T_BOOL, 0, (void*) &start_dfu_bootloader, "bootloader"},
{0x72, TS_CAT_EXEC, TS_ACCESS_EXEC, TS_T_BOOL, 0, (void*) &NVIC_SystemReset, "reset"},
};
// stores object-ids of values to be published via Serial
+3 -44
View File
@@ -20,7 +20,6 @@
#include "half_bridge.h"
//#include "data_objects.h"
#include <time.h> // for time(NULL) function
#include <math.h> // for fabs function
@@ -38,48 +37,8 @@ void dcdc_init(dcdc_t *dcdc)
dcdc->pwm_delta = 1;
}
void dcdc_port_init_bat(dcdc_port_t *port, battery_t *bat)
{
port->input_allowed = true; // discharging allowed
port->output_allowed = true; // charging allowed
port->voltage_input_start = bat->cell_voltage_load_reconnect * bat->num_cells;
port->voltage_input_stop = bat->cell_voltage_load_disconnect * bat->num_cells;
port->current_input_max = -bat->charge_current_max; // TODO: discharge current
port->voltage_output_target = bat->cell_voltage_max * bat->num_cells;
port->voltage_output_min = bat->cell_voltage_absolute_min * bat->num_cells;
port->current_output_max = bat->charge_current_max;
}
void dcdc_port_init_solar(dcdc_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 dcdc_port_init_nanogrid(dcdc_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->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_resistance = 1.0; // 1 Ohm means 1V change of target voltage per amp
}
// returns if output power should be increased (1), decreased (-1) or switched off (0)
int _dcdc_output_control(dcdc_t *dcdc, dcdc_port_t *out, dcdc_port_t *in)
int _dcdc_output_control(dcdc_t *dcdc, power_port_t *out, power_port_t *in)
{
//static float dcdc_power;
float dcdc_power_new = out->voltage * out->current;
@@ -120,7 +79,7 @@ int _dcdc_output_control(dcdc_t *dcdc, dcdc_port_t *out, dcdc_port_t *in)
}
}
bool _dcdc_check_start_conditions(dcdc_t *dcdc, dcdc_port_t *out, dcdc_port_t *in)
bool _dcdc_check_start_conditions(dcdc_t *dcdc, power_port_t *out, power_port_t *in)
{
return out->output_allowed == true
&& out->voltage < out->voltage_output_target
@@ -131,7 +90,7 @@ bool _dcdc_check_start_conditions(dcdc_t *dcdc, dcdc_port_t *out, dcdc_port_t *i
&& time(NULL) > (dcdc->off_timestamp + dcdc->restart_interval);
}
void dcdc_control(dcdc_t *dcdc, dcdc_port_t *hs, dcdc_port_t *ls)
void dcdc_control(dcdc_t *dcdc, power_port_t *hs, power_port_t *ls)
{
if (half_bridge_enabled()) {
int step;
+3 -33
View File
@@ -18,39 +18,13 @@
#define __DCDC_H_
#include <stdbool.h>
#include "structs.h"
#include "battery.h"
#include "power_port.h"
#ifdef __cplusplus
extern "C" {
#endif
/* DC/DC port 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
* connected to high or low side of DC/DC converter w/o having to rewrite
* the control algorithm.
*/
typedef struct {
float voltage; // actual measurements
float current;
float voltage_output_target; // target voltage if port is configured as output
float droop_resistance; // v_target = v_out_max - r_droop * current
float voltage_output_min; // minimum voltage to allow current output (necessary
// to prevent charging of deep-discharged Li-ion batteries)
// minimum voltage to allow current input (= discharging of batteries)
float voltage_input_start; // starting point for discharging of batteries (load reconnect)
float voltage_input_stop; // absolute minimum = load disconnect for batteries
float current_output_max; // charging direction for battery port
float current_input_max; // discharging direction for battery port: must be negative value !!!
bool output_allowed; // charging direction for battery port
bool input_allowed; // discharging direction for battery port
} dcdc_port_t;
/* DC/DC basic operation mode
*
* Defines which type of device is connected to the high side and low side ports
@@ -101,11 +75,7 @@ typedef struct {
*/
void dcdc_init(dcdc_t *dcdc);
void dcdc_port_init_bat(dcdc_port_t *port, battery_t *bat);
void dcdc_port_init_solar(dcdc_port_t *port);
void dcdc_port_init_nanogrid(dcdc_port_t *port);
void dcdc_control(dcdc_t *dcdc, dcdc_port_t *high_side, dcdc_port_t *low_side);
void dcdc_control(dcdc_t *dcdc, power_port_t *high_side, power_port_t *low_side);
#ifdef __cplusplus
}
+11 -1
View File
@@ -18,6 +18,10 @@ DigitalOut load_disable(PIN_LOAD_DIS);
DigitalOut usb_pwr_en(PIN_USB_PWR_EN);
#endif
#ifdef PIN_USB_PWR_DIS
DigitalOut usb_pwr_dis(PIN_USB_PWR_DIS);
#endif
#ifdef PIN_UEXT_DIS
DigitalOut uext_dis(PIN_UEXT_DIS);
#endif
@@ -41,7 +45,9 @@ void hw_load_switch(bool enabled)
#ifdef PIN_LOAD_EN
if (enabled) load_enable = 1;
else load_enable = 0;
#else
#endif
#ifdef PIN_LOAD_DIS
printf("Load enabled = %d\n", enabled);
if (enabled) load_disable = 0;
else load_disable = 1;
#endif
@@ -59,6 +65,10 @@ void hw_usb_out(bool enabled)
if (enabled) usb_pwr_en = 1;
else usb_pwr_en = 0;
#endif
#ifdef PIN_USB_PWR_DIS
if (enabled) usb_pwr_dis = 0;
else usb_pwr_dis = 1;
#endif
}
#if defined(STM32F0)
-2
View File
@@ -3,8 +3,6 @@
#ifndef __HARDWARE_H_
#define __HARDWARE_H_
#include "structs.h"
void init_load();
void hw_load_switch(bool enabled);
void hw_usb_out(bool enabled);
+3 -2
View File
@@ -4,12 +4,13 @@
#include "mbed.h"
#include <stdint.h>
#include "dcdc.h"
#include "structs.h"
#include "power_port.h"
#include "battery.h"
#include "load.h"
/* OLED display based on SSD1306 IC, connected to UEXT port via I2C
*/
void oled_output(dcdc_t *dcdc, dcdc_port_t *solar_port, dcdc_port_t *bat_port, battery_t *bat, load_output_t *load);
void oled_output(dcdc_t *dcdc, power_port_t *solar_port, power_port_t *bat_port, battery_t *bat, load_output_t *load);
/* UART serial interface (either in UEXT connector or from additional SWD serial)
*/
+1 -1
View File
@@ -49,7 +49,7 @@ const unsigned char bmp_disconnected [] = {
I2C i2c(PIN_UEXT_SDA, PIN_UEXT_SCL);
Adafruit_SSD1306_I2c oled(i2c, PIN_UEXT_SSEL, 0x78, 64, 128);
void oled_output(dcdc_t *dcdc, dcdc_port_t *solar_port, dcdc_port_t *bat_port, battery_t *bat, load_output_t *load)
void oled_output(dcdc_t *dcdc, power_port_t *solar_port, power_port_t *bat_port, battery_t *bat, load_output_t *load)
{
float tmp;
+3 -3
View File
@@ -8,7 +8,7 @@
#include "pcb.h"
#include "half_bridge.h"
#include "ATCmdParser.h"
#include "log.h"
#include "ESP32.h"
@@ -25,8 +25,8 @@ enum wifi_states {
};
extern log_data_t log_data;
extern dcdc_port_t hs_port;
extern dcdc_port_t ls_port;
extern power_port_t hs_port;
extern power_port_t ls_port;
extern load_output_t load;
extern battery_t bat;
extern dcdc_t dcdc;
-3
View File
@@ -3,8 +3,6 @@
#ifndef __LEDS_H__
#define __LEDS_H__
#include "structs.h"
void leds_init();
void led_timer_start(int freq_Hz);
void charlieplexing();
@@ -16,5 +14,4 @@ void trigger_rx_led();
void update_soc_led(int soc);
void toggle_led_blink();
#endif /* __LEDS_H__ */
+33
View File
@@ -0,0 +1,33 @@
#ifndef __LOG_H_
#define __LOG_H_
#include <stdbool.h>
#include <stdint.h>
#ifdef __cplusplus
extern "C" {
#endif
/* Log Data
*
* Saves maximum ever measured values to EEPROM
*/
typedef struct {
float battery_voltage_max;
float solar_voltage_max;
float dcdc_current_max;
float load_current_max;
float temp_int; // °C (internal MCU temperature sensor)
float temp_int_max; // °C
float temp_mosfets_max;
int day_counter;
} log_data_t;
#ifdef __cplusplus
}
#endif
#endif // __LOG_H_
+19 -16
View File
@@ -25,12 +25,15 @@
#include "half_bridge.h" // PWM generation for DC/DC converter
#include "hardware.h" // hardware-related functions like load switch, LED control, watchdog, etc.
#include "dcdc.h" // DC/DC converter control (hardware independent)
#include "bat_charger.h" // battery settings and charger state machine
#include "pwm_chg.h" // PWM charge controller
#include "battery.h" // battery settings
#include "charger.h" // charger state machine
#include "adc_dma.h" // ADC using DMA and conversion to measurement values
#include "interface.h" // communication interfaces, displays, etc.
#include "eeprom.h" // external I2C EEPROM
#include "load.h" // load and USB output management
#include "leds.h"
#include "log.h"
//----------------------------------------------------------------------------
// global variables
@@ -40,14 +43,15 @@ Serial serial(PIN_SWD_TX, PIN_SWD_RX, "serial");
bool led_states[NUM_LEDS];
dcdc_t dcdc = {};
dcdc_port_t hs_port = {}; // high-side (solar for typical MPPT)
dcdc_port_t ls_port = {}; // low-side (battery for typical MPPT)
dcdc_port_t *bat_port = NULL;
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;
pwm_chg_t pwm_chg = {}; // only for PWM charger
battery_t bat;
battery_user_settings_t bat_user;
load_output_t load;
log_data_t log_data;
extern ThingSet ts; // defined in data_objects.cpp
extern ThingSet ts; // defined in data_objects.cpp
time_t timestamp; // current unix timestamp (independent of time(NULL), as it is user-configurable)
@@ -99,18 +103,18 @@ int main()
switch(dcdc.mode)
{
case MODE_NANOGRID:
dcdc_port_init_nanogrid(&hs_port);
dcdc_port_init_bat(&ls_port, &bat);
power_port_init_nanogrid(&hs_port);
power_port_init_bat(&ls_port, &bat);
bat_port = &ls_port;
break;
case MODE_MPPT_BUCK: // typical MPPT charge controller operation
dcdc_port_init_solar(&hs_port);
dcdc_port_init_bat(&ls_port, &bat);
power_port_init_solar(&hs_port);
power_port_init_bat(&ls_port, &bat);
bat_port = &ls_port;
break;
case MODE_MPPT_BOOST: // for charging of e-bike battery via solar panel
dcdc_port_init_solar(&ls_port);
dcdc_port_init_bat(&hs_port, &bat);
power_port_init_solar(&ls_port);
power_port_init_bat(&hs_port, &bat);
bat_port = &hs_port;
break;
}
@@ -148,7 +152,7 @@ int main()
void interfaces_init()
{
uart_serial_init(&serial);
usb_serial_init();
//usb_serial_init();
#ifdef GSM_ENABLED
gsm_init();
@@ -199,15 +203,14 @@ void interfaces_process_1s()
void setup()
{
serial.baud(115200);
//freopen("/serial", "w", stdout); // retarget stdout
leds_init();
led_timer_start(5000); // 5 kHz
battery_init(&bat, &bat_user);
load_init(&load);
dcdc_init(&dcdc);
dcdc_init(&dcdc);
half_bridge_init(PWM_FREQUENCY, 300, 12 / dcdc.hs_voltage_max, 0.97); // lower duty limit might have to be adjusted dynamically depending on LS voltage
eeprom_restore_data();
@@ -216,9 +219,9 @@ void setup()
adc_setup();
dma_setup();
adc_timer_setup(1000); // 1 kHz
adc_timer_start(1000); // 1 kHz
wait(0.2); // wait for ADC to collect some measurement values
wait(0.5); // wait for ADC to collect some measurement values
update_measurements(&dcdc, &bat, &load, &hs_port, &ls_port);
calibrate_current_sensors(&dcdc, &load);
+56
View File
@@ -0,0 +1,56 @@
/* 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_t *bat)
{
port->input_allowed = true; // discharging allowed
port->output_allowed = true; // charging allowed
port->voltage_input_start = bat->cell_voltage_load_reconnect * bat->num_cells;
port->voltage_input_stop = bat->cell_voltage_load_disconnect * bat->num_cells;
port->current_input_max = -bat->charge_current_max; // TODO: discharge current
port->voltage_output_target = bat->cell_voltage_max * bat->num_cells;
port->voltage_output_min = bat->cell_voltage_absolute_min * bat->num_cells;
port->current_output_max = bat->charge_current_max;
}
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->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_resistance = 1.0; // 1 Ohm means 1V change of target voltage per amp
}
+64
View File
@@ -0,0 +1,64 @@
/* 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 __POWER_PORT_H__
#define __POWER_PORT_H__
#include <stdbool.h>
#include "battery.h"
#ifdef __cplusplus
extern "C" {
#endif
/* Power port 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
* connected to high or low side of DC/DC converter w/o having to rewrite
* the control algorithm.
*/
typedef struct {
float voltage; // actual measurements
float current;
float voltage_output_target; // target voltage if port is configured as output
float droop_resistance; // v_target = v_out_max - r_droop * current
float voltage_output_min; // minimum voltage to allow current output (necessary
// to prevent charging of deep-discharged Li-ion batteries)
// minimum voltage to allow current input (= discharging of batteries)
float voltage_input_start; // starting point for discharging of batteries (load reconnect)
float voltage_input_stop; // absolute minimum = load disconnect for batteries
float current_output_max; // charging direction for battery port
float current_input_max; // discharging direction for battery port: must be negative value !!!
bool output_allowed; // charging direction for battery port
bool input_allowed; // discharging direction for battery port
} power_port_t;
void power_port_init_bat(power_port_t *port, battery_t *bat);
void power_port_init_solar(power_port_t *port);
void power_port_init_nanogrid(power_port_t *port);
#ifdef __cplusplus
}
#endif
#endif // __POWER_PORT_H__