Updated state machine and some clean-up

This commit is contained in:
Martin Jäger
2017-04-07 23:22:22 +02:00
parent ba91e0b6c2
commit 0908ef3aba
19 changed files with 407 additions and 1728 deletions
+8 -7
View File
@@ -80,7 +80,7 @@ const int thermistorBetaValue = 3435; // typical value for Semitec 103AT-5 ther
#define PIN_UEXT_MISO PB_4
#define PIN_UEXT_MOSI PB_5
#define PIN_UEXT_SCK PB_3
#define PIN_UEXT_SSEL PA_0
#define PIN_UEXT_SSEL PC_13
#define PIN_SWD_TX PA_9
#define PIN_SWD_RX PA_10
@@ -94,11 +94,12 @@ const int thermistorBetaValue = 3435; // typical value for Semitec 103AT-5 ther
#define PIN_REF_I_DCDC PA_4
#define PIN_V_REF PA_5
#define PIN_V_BAT PA_6
#define PIN_V_SOLAR PA_7
#define PIN_V_TEMP PA_1
#define PIN_I_LOAD PB_0
#define PIN_I_DCDC PB_1
#define PIN_V_REF PA_5
#define PIN_V_BAT PA_6
#define PIN_V_SOLAR PA_7
#define PIN_TEMP_INT PA_1
#define PIN_TEMP_BAT PA_0
#define PIN_I_LOAD PB_0
#define PIN_I_DCDC PB_1
#endif
+2 -2
View File
@@ -41,7 +41,7 @@ const int charge_current_max = 10000; // mA PCB maximum: 20 A
// State: CV/absorption
const int cell_voltage_max = 2400; // max voltage per cell
const int time_limit_CV = 120; // min
const int current_cutoff_CV = 1000; //2000; // mA
const int current_cutoff_CV = 300; //2000; // mA
// State: float/trickle
const bool trickle_enabled = true;
@@ -97,7 +97,7 @@ const int thermistorBetaValue = 3435; // typical value for Semitec 103AT-5 ther
#define PIN_V_REF PA_0 // not connected
#define PIN_V_BAT PA_4
#define PIN_V_SOLAR PA_5
#define PIN_V_TEMP PA_0 // not connected
#define PIN_TEMP_INT PA_0 // not connected
#define PIN_I_LOAD PA_6
#define PIN_I_DCDC PA_7
+104
View File
@@ -0,0 +1,104 @@
/* LibreSolar MPPT charge controller firmware
* Copyright (c) 2016-2017 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 CONFIG_H
#define CONFIG_H
#include "mbed.h"
#define MPPT_20A
// DC/DC converter settings
const int _pwm_frequency = 70; // kHz
const int charge_current_cutoff = 20; // mA --> if lower, charger is switched off
const int max_solar_voltage = 55000; // mV
const int num_cells = 6;
// State: Standby
const int solar_voltage_offset_start = 5000; // mV charging switched on if Vsolar > Vbat + offset
const int solar_voltage_offset_stop = 1000; // mV charging switched off if Vsolar < Vbat + offset
const int time_limit_recharge = 60;//5*60; // sec
const int cell_voltage_recharge = 2300; // mV
// State: CC/bulk
const int charge_current_max = 10000; // mA PCB maximum: 20 A
// State: CV/absorption
const int cell_voltage_max = 2400; // max voltage per cell
const int time_limit_CV = 120; // min
const int current_cutoff_CV = 2000; // mA
// State: float/trickle
const bool trickle_enabled = true;
const int cell_voltage_trickle = 2300; // voltage for trickle charging of lead-acid batteries
const int time_trickle_recharge = 30; // min
// State: equalization
const bool equalization_enabled = false;
const int cell_voltage_equalization = 2500; // mV
const int time_limit_equalization = 60; // min
const int current_limit_equalization = 1000; // mA
const int equalization_trigger_time = 8; // weeks
const int equalization_trigger_deep_cycles = 10; // times
const int cell_voltage_load_disconnect = 1950;
const int cell_voltage_load_reconnect = 2200;
/*
// typcial LiFePO4 battery
const int num_cells = 4;
const int v_cell_max = 3550; // max voltage per cell
const int v_cell_trickle = 3550; // voltage for trickle charging of lead-acid batteries
const int v_cell_load_disconnect = 3000;
const int v_cell_load_reconnect = 3150;
*/
const int thermistorBetaValue = 3435; // typical value for Semitec 103AT-5 thermistor: 3435
//----------------------------------------------------------------------------
// global variables
// 20A board (rev. 0.6)
#define PIN_UEXT_TX PA_2
#define PIN_UEXT_RX PA_3
#define PIN_UEXT_SCL PB_6
#define PIN_UEXT_SDA PB_7
#define PIN_UEXT_MISO PB_4
#define PIN_UEXT_MOSI PB_5
#define PIN_UEXT_SCK PB_3
#define PIN_UEXT_SSEL PA_0
#define PIN_SWD_TX PA_9
#define PIN_SWD_RX PA_10
#define PIN_PWM_HS PA_8
#define PIN_PWM_LS PB_13
#define PIN_LED_GREEN PB_14
#define PIN_LED_RED PB_15
#define PIN_LOAD_DIS PB_2
#define PIN_REF_I_DCDC PA_4
#define PIN_V_REF PA_5
#define PIN_V_BAT PA_6
#define PIN_V_SOLAR PA_7
#define PIN_TEMP_INT PA_1
#define PIN_I_LOAD PB_0
#define PIN_I_DCDC PB_1
#endif
+69 -55
View File
@@ -18,14 +18,11 @@
//#include "config_12a.h" // choose old PCB revision (12A)
#include "config.h" // choose new PCB revision (20A)
#include "BuckConverter.h"
//#include "DogLCD.h"
//#include "font_6x8.h"
#include "Adafruit_SSD1306.h"
//#include "SDFileSystem.h"
#include <math.h> // log for thermistor calculation
#include "display.h"
//#define DOGLCD_ENABLED
//#define SDCARD_ENABLED
#define OLED_ENABLED
@@ -34,11 +31,6 @@
//----------------------------------------------------------------------------
// global variables
#ifdef DOGLCD_ENABLED
SPI spi(PIN_UEXT_MOSI, NC, PIN_UEXT_SCK);
DogLCD lcd(spi, PIN_UEXT_SSEL, PIN_UEXT_RX, PIN_UEXT_TX); // spi, CS, CD, reset
#endif
#ifdef SDCARD_ENABLED
SDFileSystem sd(PIN_UEXT_MOSI, PIN_UEXT_MISO, PIN_UEXT_SCK, PIN_UEXT_SSEL, "sd");
#endif
@@ -63,15 +55,15 @@ DigitalOut load_disable(PIN_LOAD_DIS);
AnalogIn v_ref(PIN_V_REF);
AnalogIn v_bat(PIN_V_BAT);
AnalogIn v_solar(PIN_V_SOLAR);
AnalogIn v_temp(PIN_V_TEMP);
AnalogIn v_temp(PIN_TEMP_INT);
AnalogIn i_load(PIN_I_LOAD);
AnalogIn i_dcdc(PIN_I_DCDC);
int solar_voltage; // mV
int battery_voltage; // mV
int dcdc_current = 0; // mA
int load_current; // mA
float temperature; // mA
int load_current = 0; // mA
float temperature; // °C
int solar_power = 0; // unit: mW
int dcdc_current_offset = 0;
@@ -99,10 +91,8 @@ int state = BAT_ERROR;
void setup();
void update_measurements();
void update_controller();
void enter_state(int next_state);
void state_machine();
void adjust_input_voltage(int delta_mV);
void update_screen();
void energy_counter();
void load_management();
@@ -143,13 +133,8 @@ int main()
//----------------------------------------------------------------------------
void setup()
{
#ifdef DOGLCD_ENABLED
lcd.init();
lcd.view(VIEW_TOP);
#endif
led_green = 1;
load_disable = 0;
load_disable = 1;
// ref_i_dcdc = 0; // 0 for buck, 1 for boost (TODO)
// for RTC backup register
@@ -158,17 +143,18 @@ void setup()
set_time(rtc_read_backup_reg(0));
input_Wh_day = float(rtc_read_backup_reg(1)) / 1000.0; // mWh
output_Wh_day = float(rtc_read_backup_reg(2)) / 1000.0; // mWh
input_Wh_total = float(rtc_read_backup_reg(3)); // Wh
output_Wh_total = float(rtc_read_backup_reg(4)); // Wh
input_Wh_day = (float)rtc_read_backup_reg(1) / 1000.0; // mWh
output_Wh_day = (float)rtc_read_backup_reg(2) / 1000.0; // mWh
input_Wh_total = (float)rtc_read_backup_reg(3); // Wh
output_Wh_total = (float)rtc_read_backup_reg(4); // Wh
mppt.frequency_kHz(_pwm_frequency);
mppt.deadtime_ns(300);
mppt.set_duty_cycle(0.90);
mppt.lock_settings();
mppt.set_current_limit(charge_current_max);
mppt.set_voltage_limit(num_cells * cell_voltage_max);
mppt.set_max_current(charge_current_max);
mppt.set_min_current(charge_current_cutoff);
mppt.set_max_voltage(num_cells * cell_voltage_max);
mppt.duty_cycle_limits((float) cell_voltage_load_disconnect * num_cells / max_solar_voltage, 0.97);
update_measurements();
@@ -211,8 +197,8 @@ void enter_state(int next_state)
case CHG_CC:
serial.printf("Enter State: CHG_CC\n");
//serial.printf("P=%d Vsol=%d Vbat=%d Ibat=%d delta_time_CV=%d\n", solar_power, solar_voltage, battery_voltage, dcdc_current, time(NULL) - mppt.last_time_CV());
mppt.set_current_limit(charge_current_max);
mppt.set_voltage_limit(num_cells * cell_voltage_max);
mppt.set_max_current(charge_current_max);
mppt.set_max_voltage(num_cells * cell_voltage_max);
mppt.last_time_CV_reset();
mppt.start((float) battery_voltage / (solar_voltage - 1000.0));
led_red = 1;
@@ -221,25 +207,27 @@ void enter_state(int next_state)
case CHG_CV:
serial.printf("Enter State: CHG_CV\n");
//serial.printf("P=%d Vsol=%d Vbat=%d Ibat=%d delta_time_CV=%d\n", solar_power, solar_voltage, battery_voltage, dcdc_current, time(NULL) - mppt.last_time_CV());
mppt.set_voltage_limit(num_cells * cell_voltage_max);
mppt.set_max_voltage(num_cells * cell_voltage_max);
break;
case CHG_TRICKLE:
serial.printf("Enter State: CHG_TRICKLE\n");
//serial.printf("P=%d Vsol=%d Vbat=%d Ibat=%d delta_time_CV=%d\n", solar_power, solar_voltage, battery_voltage, dcdc_current, time(NULL) - mppt.last_time_CV());
mppt.set_voltage_limit(num_cells * cell_voltage_trickle);
mppt.set_max_voltage(num_cells * cell_voltage_trickle);
mppt.last_time_CV_reset();
break;
case CHG_EQUALIZATION:
serial.printf("Enter State: CHG_EQUALIZATION\n");
mppt.set_voltage_limit(num_cells * cell_voltage_equalization);
mppt.set_max_voltage(num_cells * cell_voltage_equalization);
mppt.last_time_CV_reset();
mppt.set_current_limit(current_limit_equalization);
mppt.set_max_current(current_limit_equalization);
break;
case TEST:
serial.printf("Enter State: TEST\n");
//mppt.set_current_limit(charge_current_max);
//mppt.set_voltage_limit(num_cells * cell_voltage_max);
mppt.start(0.50);
led_red = 1;
break;
@@ -261,16 +249,33 @@ void enter_state(int next_state)
/*****************************************************************************
* Charger state machine
*
* INIT Initial state when system boots up. Checks communication to BMS
* and turns the CHG and DSG FETs on
* STANDBY Battery can be discharged, but no charging power is available
* or battery is fully charged
* CHG_CC Constant current (CC) charging with maximum current possible
* (MPPT algorithm is running)
* CHG_CV Constant voltage (CV) charging at maximum cell voltage
* BAT_ERROR An error occured. Charging disabled.
*
## Standby
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 state_machine()
{
@@ -319,25 +324,31 @@ void state_machine()
}
}
else if (dcdc_current < charge_current_cutoff
|| solar_voltage < battery_voltage + solar_voltage_offset_stop) {
|| solar_voltage < battery_voltage + solar_voltage_offset_stop)
{
enter_state(STANDBY);
}
break;
}
case CHG_TRICKLE: {
if (time(NULL) - mppt.last_time_CV() > time_trickle_recharge * 60) {
if (time(NULL) - mppt.last_time_CV() > time_trickle_recharge * 60)
{
enter_state(CHG_CC);
}
else if (dcdc_current < charge_current_cutoff
|| solar_voltage < battery_voltage + solar_voltage_offset_stop) {
// current cutoff might never be reached because of lakage
// current inside battery --> stay in trickle till end of day
else if ((dcdc_current < charge_current_cutoff
&& battery_voltage < num_cells * cell_voltage_trickle)
|| solar_voltage < battery_voltage + solar_voltage_offset_stop)
{
enter_state(STANDBY);
}
break;
}
case TEST: {
break;
}
default:
mppt.stop();
break;
@@ -448,7 +459,7 @@ void update_measurements()
// low pass filter of measurement values:
// y(n) = (1-c)*x(n) + c*y(n-1) with c=exp(-Ta/RC)
// https://en.wikipedia.org/wiki/Low-pass_filter#Simple_infinite_impulse_response_filter
float c = 0.2; // filter constant
float c = 0.3; // filter constant
// first call of function
if (battery_voltage == 0) {
@@ -457,17 +468,17 @@ void update_measurements()
#ifdef MPPT_12A
// solar voltage divider o-- 150k --o-- 6.8k --o
tmp = adc_read(v_solar) * vcc * 1568 / 68;
solar_voltage = (100 - c) * tmp + c * solar_voltage;
tmp = adc_read_avg(v_solar) * vcc * 1568 / 68;
solar_voltage = (1.0 - c) * tmp + c * solar_voltage;
// battery voltage divider o-- 120k --o-- 10k --o
tmp = adc_read(v_bat) * vcc * 130 / 10;
battery_voltage = (100 - c) * tmp + c * battery_voltage;
tmp = adc_read_avg(v_bat) * vcc * 130 / 10;
battery_voltage = (1.0 - c) * tmp + c * battery_voltage;
tmp = adc_read(i_dcdc) * vcc * 1000 / 5 / 50;
tmp = adc_read_avg(i_dcdc) * vcc * 1000 / 5 / 50;
dcdc_current = (1.0 - c) * (tmp + dcdc_current_offset) + c * dcdc_current;
tmp = adc_read(i_load) * vcc * 1000 / 5 / 50;
tmp = adc_read_avg(i_load) * vcc * 1000 / 5 / 50;
load_current = (1.0 - c) * (tmp + load_current_offset) + c * load_current;
solar_power = battery_voltage * dcdc_current / 1000;
@@ -570,7 +581,7 @@ void update_screen(void)
oled.printf("Tot +%4.1fkWh -%4.1fkWh", input_Wh_total / 1000.0, fabs(output_Wh_total) / 1000.0);
oled.setTextCursor(0, 56);
oled.printf("PWM %.1f%% ", mppt.get_duty_cycle() * 100.0);
oled.printf("Temp %.1f PWM %.1f%% ", temperature, mppt.get_duty_cycle() * 100.0);
switch (state) {
case STANDBY:
oled.printf("Standby\n");
@@ -587,6 +598,9 @@ void update_screen(void)
case CHG_BIKE:
oled.printf("Bike gen\n");
break;
case TEST:
oled.printf("Test\n");
break;
default:
oled.printf("Error\n");
break;