Major updates of software structure
- new classes ChargeController and AnalogValue introduced - BuckConverter renamed back to HalfBridge - charging profile settings via struct
This commit is contained in:
+106
-307
@@ -17,7 +17,9 @@
|
||||
#include "mbed.h"
|
||||
//#include "config_12a.h" // choose old PCB revision (12A)
|
||||
#include "config.h" // choose new PCB revision (20A)
|
||||
#include "BuckConverter.h"
|
||||
#include "HalfBridge.h"
|
||||
#include "AnalogValue.h"
|
||||
#include "ChargeController.h"
|
||||
#include "Adafruit_SSD1306.h"
|
||||
//#include "SDFileSystem.h"
|
||||
#include <math.h> // log for thermistor calculation
|
||||
@@ -26,8 +28,6 @@
|
||||
//#define SDCARD_ENABLED
|
||||
#define OLED_ENABLED
|
||||
|
||||
#define ADC_AVG_SAMPLES 8 // number of ADC values to read for averaging
|
||||
|
||||
//----------------------------------------------------------------------------
|
||||
// global variables
|
||||
|
||||
@@ -44,7 +44,9 @@ Serial serial(PIN_SWD_TX, PIN_SWD_RX, "serial");
|
||||
Serial serial_uext(PIN_UEXT_TX, PIN_UEXT_RX, "serial_uext");
|
||||
|
||||
// PWM will be generated on pins PA8 (TIM1_CH1) and PB13 (TIM1_CH1N)
|
||||
BuckConverter mppt(_pwm_frequency);
|
||||
HalfBridge dcdc(_pwm_frequency);
|
||||
float dcdc_power; // saves previous output power
|
||||
int pwm_delta = 1;
|
||||
|
||||
DigitalOut led_green(PIN_LED_GREEN);
|
||||
DigitalOut led_red(PIN_LED_RED);
|
||||
@@ -53,24 +55,22 @@ DigitalOut load_disable(PIN_LOAD_DIS);
|
||||
//AnalogOut ref_i_dcdc(PIN_REF_I_DCDC);
|
||||
|
||||
AnalogIn v_ref(PIN_V_REF);
|
||||
AnalogIn v_bat(PIN_V_BAT);
|
||||
AnalogIn v_solar(PIN_V_SOLAR);
|
||||
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 = 0; // mA
|
||||
float temperature; // °C
|
||||
int solar_power = 0; // unit: mW
|
||||
#ifdef MPPT_20A
|
||||
AnalogValue solar_voltage(PIN_V_SOLAR, 1056.0 / 56.0); // solar voltage divider o-- 100k --o-- 5.6k --o
|
||||
AnalogValue battery_voltage(PIN_V_BAT, 110 / 10); // battery voltage divider o-- 100k --o-- 10k --o
|
||||
AnalogValue dcdc_current(PIN_I_DCDC, 1000 / 2 / (1500.0 / 22.0)); // op amp gain: 150/2.2 = 68.2, resistor: 2 mOhm
|
||||
AnalogValue load_current(PIN_I_LOAD, 1000 / 2 / (1500.0 / 22.0));
|
||||
AnalogValue thermistor_voltage(PIN_TEMP_INT, 1.0);
|
||||
#endif
|
||||
|
||||
ChargeController charger(profile);
|
||||
|
||||
float temperature; // °C
|
||||
|
||||
int dcdc_current_offset = 0;
|
||||
int load_current_offset = 0;
|
||||
|
||||
int time_state_changed = 0;
|
||||
|
||||
// for daily energy counting
|
||||
int seconds_zero_solar = 0;
|
||||
int seconds_day = 0;
|
||||
@@ -81,25 +81,18 @@ float output_Wh_day = 0.0;
|
||||
float input_Wh_total = 0.0;
|
||||
float output_Wh_total = 0.0;
|
||||
|
||||
int pwm_delta = 1;
|
||||
|
||||
enum charger_states {STANDBY, CHG_CC, CHG_CV, CHG_TRICKLE, CHG_EQUALIZATION, BAT_ERROR, CHG_BIKE, TEST}; // states for charger state machine
|
||||
int state = BAT_ERROR;
|
||||
|
||||
//----------------------------------------------------------------------------
|
||||
// function prototypes
|
||||
|
||||
void setup();
|
||||
void update_measurements();
|
||||
void enter_state(int next_state);
|
||||
void state_machine();
|
||||
void update_screen();
|
||||
void energy_counter();
|
||||
void load_management();
|
||||
void log_data();
|
||||
void rtc_write_backup_reg(uint32_t BackupRegister, uint32_t data);
|
||||
uint32_t rtc_read_backup_reg(uint32_t BackupRegister);
|
||||
float adc_read_avg(AnalogIn& input);
|
||||
void update_mppt();
|
||||
|
||||
//----------------------------------------------------------------------------
|
||||
int main()
|
||||
@@ -112,11 +105,9 @@ int main()
|
||||
time_t last_second = time(NULL);
|
||||
|
||||
tick1.attach(&update_measurements, 0.01);
|
||||
tick2.attach(&state_machine, 0.1);
|
||||
tick2.attach(&update_mppt, 0.1);
|
||||
|
||||
enter_state(STANDBY);
|
||||
time_state_changed = time(NULL) - 60*60*24; // start immediately
|
||||
//enter_state(TEST);
|
||||
//time_state_changed = time(NULL) - 60*60*24; // start immediately
|
||||
|
||||
while(1) {
|
||||
// called once per second
|
||||
@@ -148,20 +139,15 @@ void setup()
|
||||
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_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);
|
||||
dcdc.deadtime_ns(300);
|
||||
dcdc.lock_settings();
|
||||
dcdc.duty_cycle_limits((float) profile.cell_voltage_load_disconnect * profile.num_cells / solar_voltage_max, 0.97);
|
||||
|
||||
update_measurements();
|
||||
|
||||
// calibration of current sensors in no load condition
|
||||
load_current_offset = -load_current;
|
||||
dcdc_current_offset = -dcdc_current;
|
||||
//load_current_offset = -load_current;
|
||||
//dcdc_current_offset = -dcdc_current;
|
||||
|
||||
serial.baud(115200);
|
||||
serial.printf("\nSerial interface started...\n");
|
||||
@@ -173,186 +159,62 @@ void setup()
|
||||
freopen("/serial", "w", stdout); // retarget stdout
|
||||
}
|
||||
|
||||
void load_management() {
|
||||
|
||||
if (battery_voltage < cell_voltage_load_disconnect * num_cells) {
|
||||
void load_management()
|
||||
{
|
||||
if (charger.discharging_enabled()) {
|
||||
load_disable = 0;
|
||||
} else {
|
||||
load_disable = 1;
|
||||
}
|
||||
if (battery_voltage >= cell_voltage_load_reconnect * num_cells) {
|
||||
load_disable = 0;
|
||||
}
|
||||
}
|
||||
|
||||
// actions to be performed upon entering a state
|
||||
void enter_state(int next_state)
|
||||
void update_mppt()
|
||||
{
|
||||
switch (next_state)
|
||||
{
|
||||
case STANDBY:
|
||||
serial.printf("Enter State: STANDBY\n");
|
||||
mppt.stop();
|
||||
led_red = 0;
|
||||
break;
|
||||
|
||||
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_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;
|
||||
break;
|
||||
|
||||
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_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_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_max_voltage(num_cells * cell_voltage_equalization);
|
||||
mppt.last_time_CV_reset();
|
||||
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;
|
||||
|
||||
case BAT_ERROR:
|
||||
serial.printf("Enter State: BAT_ERROR\n");
|
||||
mppt.stop();
|
||||
led_red = 0;
|
||||
break;
|
||||
|
||||
default:
|
||||
mppt.stop();
|
||||
led_red = 0;
|
||||
break;
|
||||
}
|
||||
time_state_changed = time(NULL);
|
||||
state = next_state;
|
||||
}
|
||||
|
||||
/*****************************************************************************
|
||||
* Charger state machine
|
||||
|
||||
## 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()
|
||||
{
|
||||
mppt.update(solar_voltage, battery_voltage, dcdc_current);
|
||||
charger.update(battery_voltage.read(), dcdc_current.read());
|
||||
load_management();
|
||||
|
||||
switch (state) {
|
||||
float dcdc_power_new = battery_voltage * dcdc_current;
|
||||
|
||||
case STANDBY: {
|
||||
if (solar_voltage > battery_voltage + solar_voltage_offset_start
|
||||
&& battery_voltage < num_cells * cell_voltage_recharge
|
||||
&& (time(NULL) - time_state_changed) > time_limit_recharge)
|
||||
{
|
||||
enter_state(CHG_CC);
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
case CHG_CC: {
|
||||
if (mppt.last_time_CV() <= time(NULL)) {
|
||||
serial.printf("Last_time_cv = %d, time(NULL) = %d\n", mppt.last_time_CV(), time(NULL));
|
||||
enter_state(CHG_CV);
|
||||
}
|
||||
else if (dcdc_current < charge_current_cutoff
|
||||
|| solar_voltage < battery_voltage + solar_voltage_offset_stop) {
|
||||
enter_state(STANDBY);
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
case CHG_CV: {
|
||||
// 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 ((dcdc_current < current_cutoff_CV && (time(NULL) - mppt.last_time_CV()) < 2)
|
||||
|| (time(NULL) - time_state_changed) > time_limit_CV * 60)
|
||||
{
|
||||
if (equalization_enabled) {
|
||||
// TODO: additional conditions!
|
||||
enter_state(CHG_EQUALIZATION);
|
||||
}
|
||||
else if (trickle_enabled) {
|
||||
enter_state(CHG_TRICKLE);
|
||||
}
|
||||
else {
|
||||
enter_state(STANDBY);
|
||||
}
|
||||
}
|
||||
else if (dcdc_current < charge_current_cutoff
|
||||
|| 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)
|
||||
{
|
||||
enter_state(CHG_CC);
|
||||
}
|
||||
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;
|
||||
if (dcdc.enabled() == false && charger.charging_enabled() == true
|
||||
&& battery_voltage < charger.read_target_voltage()
|
||||
&& solar_voltage > battery_voltage + solar_voltage_offset_start)
|
||||
{
|
||||
dcdc.start(battery_voltage / (solar_voltage - 1.0));
|
||||
led_red = 1;
|
||||
}
|
||||
else if (solar_voltage < battery_voltage + solar_voltage_offset_stop
|
||||
|| dcdc_current < dcdc_current_min)
|
||||
{
|
||||
dcdc.stop();
|
||||
led_red = 0;
|
||||
}
|
||||
|
||||
if (battery_voltage > charger.read_target_voltage()) {
|
||||
// increase input voltage to lower output voltage
|
||||
if (dcdc_current > dcdc_current_min) {
|
||||
dcdc.duty_cycle_step(-1);
|
||||
}
|
||||
else {
|
||||
// switch off and wait for voltage to go down
|
||||
// (otherwise current could get negative, i.e. into solar panel)
|
||||
dcdc.stop();
|
||||
led_red = 0;
|
||||
}
|
||||
}
|
||||
else if (dcdc_current > charger.read_target_current()) {
|
||||
// increase input voltage to decrease current
|
||||
dcdc.duty_cycle_step(-1);
|
||||
}
|
||||
else {
|
||||
//printf("MPPT\n");
|
||||
// start MPPT
|
||||
if (dcdc_power > dcdc_power_new) {
|
||||
pwm_delta = -pwm_delta;
|
||||
}
|
||||
dcdc.duty_cycle_step(pwm_delta);
|
||||
}
|
||||
|
||||
dcdc_power = dcdc_power_new;
|
||||
}
|
||||
|
||||
|
||||
@@ -360,7 +222,7 @@ void state_machine()
|
||||
// must be called once per second
|
||||
void energy_counter()
|
||||
{
|
||||
if (solar_power < 100) {
|
||||
if (dcdc_power < 0.1) {
|
||||
seconds_zero_solar++;
|
||||
}
|
||||
else {
|
||||
@@ -372,7 +234,7 @@ void energy_counter()
|
||||
nighttime = true;
|
||||
}
|
||||
|
||||
if (nighttime && solar_power > 100) {
|
||||
if (nighttime && dcdc_power > 0.1) {
|
||||
nighttime = false;
|
||||
input_Wh_day = 0.0;
|
||||
output_Wh_day = 0.0;
|
||||
@@ -383,10 +245,10 @@ void energy_counter()
|
||||
seconds_day++;
|
||||
}
|
||||
|
||||
input_Wh_day += solar_power / 1000.0 / 3600.0;
|
||||
output_Wh_day += load_current / 1000.0 * battery_voltage / 1000.0 / 3600.0;
|
||||
input_Wh_total += solar_power / 1000.0 / 3600.0;
|
||||
output_Wh_total += load_current / 1000.0 * battery_voltage / 1000.0 / 3600.0;
|
||||
input_Wh_day += dcdc_power / 3600.0;
|
||||
output_Wh_day += load_current * battery_voltage / 3600.0;
|
||||
input_Wh_total += dcdc_power / 3600.0;
|
||||
output_Wh_total += load_current * battery_voltage/ 3600.0;
|
||||
|
||||
// save values to RTC backup register
|
||||
rtc_write_backup_reg(0, time(NULL));
|
||||
@@ -409,15 +271,15 @@ void log_data()
|
||||
|
||||
fprintf(fp, "%d;", (int)time(NULL));
|
||||
fprintf(fp, "%d;", day_counter);
|
||||
fprintf(fp, "%.2f;%.1f;", solar_voltage / 1000.0, solar_power / 1000.0);
|
||||
fprintf(fp, "%.2f;%.1f;", battery_voltage / 1000.0, dcdc_current / 1000.0);
|
||||
fprintf(fp, "%.1f;%.2f;", (load_current < 0 ? 0 : load_current) / 1000.0 * battery_voltage / 1000.0,
|
||||
(load_current < 0 ? 0 : load_current) / 1000.0);
|
||||
fprintf(fp, "%.2f;%.1f;", solar_voltage.read(), dcdc_power);
|
||||
fprintf(fp, "%.2f;%.1f;", battery_voltage.read(), dcdc_current.read());
|
||||
fprintf(fp, "%.1f;%.2f;", (load_current < 0 ? 0 : load_current.read()) * battery_voltage.read(),
|
||||
(load_current < 0 ? 0 : load_current.read()));
|
||||
fprintf(fp, "%d;", !load_disable);
|
||||
fprintf(fp, "%.0f;%.0f;", input_Wh_day, fabs(output_Wh_day));
|
||||
fprintf(fp, "%.1f;%.1f;", input_Wh_total / 1000.0, fabs(output_Wh_total / 1000.0));
|
||||
switch (state) {
|
||||
case STANDBY:
|
||||
switch (charger.get_state()) {
|
||||
case CHG_IDLE:
|
||||
fprintf(fp, "Standby;");
|
||||
break;
|
||||
case CHG_CC:
|
||||
@@ -429,9 +291,6 @@ void log_data()
|
||||
case CHG_TRICKLE:
|
||||
fprintf(fp, "Trickle chg;");
|
||||
break;
|
||||
case CHG_BIKE:
|
||||
fprintf(fp, "Bike gen;");
|
||||
break;
|
||||
default:
|
||||
fprintf(fp, "Error;");
|
||||
break;
|
||||
@@ -441,80 +300,27 @@ void log_data()
|
||||
fclose(fp);
|
||||
}
|
||||
|
||||
float adc_read_avg (AnalogIn& input) {
|
||||
// use read_u16 for faster calculation
|
||||
unsigned int sum_adc_readings = 0;
|
||||
for (int i = 0; i < ADC_AVG_SAMPLES; i++) {
|
||||
sum_adc_readings += input.read_u16(); // normalized to 0xFFFF
|
||||
}
|
||||
return (float) (sum_adc_readings / ADC_AVG_SAMPLES) / (float)0xFFFF;
|
||||
}
|
||||
|
||||
//----------------------------------------------------------------------------
|
||||
void update_measurements()
|
||||
{
|
||||
int vcc = 3300;
|
||||
float tmp = 0.0;
|
||||
|
||||
// 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.3; // filter constant
|
||||
|
||||
// first call of function
|
||||
if (battery_voltage == 0) {
|
||||
c = 0.0;
|
||||
}
|
||||
|
||||
#ifdef MPPT_12A
|
||||
// solar voltage divider o-- 150k --o-- 6.8k --o
|
||||
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_avg(v_bat) * vcc * 130 / 10;
|
||||
battery_voltage = (1.0 - c) * tmp + c * battery_voltage;
|
||||
|
||||
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_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;
|
||||
#ifdef MPPT_20A
|
||||
//AnalogValue::update_reference_voltage(v_ref, 2.5);
|
||||
#endif
|
||||
|
||||
solar_voltage.update();
|
||||
battery_voltage.update();
|
||||
dcdc_current.update();
|
||||
load_current.update();
|
||||
|
||||
#ifdef MPPT_20A
|
||||
// reference voltage (should be 2.5V)
|
||||
vcc = 2500 / adc_read_avg(v_ref);
|
||||
thermistor_voltage.update();
|
||||
|
||||
// solar voltage divider o-- 100k --o-- 5.6k --o
|
||||
tmp = adc_read_avg(v_solar) * vcc * 1056 / 56;
|
||||
solar_voltage = (1.0 - c) * tmp + c * solar_voltage;
|
||||
|
||||
// battery voltage divider o-- 100k --o-- 10k --o
|
||||
tmp = adc_read_avg(v_bat) * vcc * 110 / 10;
|
||||
battery_voltage = (1.0 - c) * tmp + c * battery_voltage;
|
||||
|
||||
// dcdc current (op amp gain: 150/2.2 = 68.2, resistor: 2 mOhm)
|
||||
tmp = adc_read_avg(i_dcdc) * vcc * 1000 / 2 / (1500 / 22);
|
||||
dcdc_current = (1.0 - c) * (tmp + dcdc_current_offset) + c * dcdc_current;
|
||||
|
||||
// load current (op amp gain: 150/2.2 = 68.2, resistor: 2 mOhm)
|
||||
tmp = adc_read_avg(i_load) * vcc * 1000 / 2 / (1500 / 22);
|
||||
load_current = (1.0 - c) * (tmp + load_current_offset) + c * load_current;
|
||||
|
||||
solar_power = battery_voltage * dcdc_current / 1000;
|
||||
|
||||
// temperature sensed by NTC
|
||||
tmp = adc_read_avg(v_temp) * vcc; // voltage read by ADC
|
||||
unsigned long rts = 10000.0 * tmp / (3300.0 - tmp); // Ohm
|
||||
// resistance of NTC (Ohm)
|
||||
unsigned long rts = 10000.0 * thermistor_voltage.read() / (3.3 - thermistor_voltage.read());
|
||||
// Temperature calculation using Beta equation for 10k thermistor
|
||||
// (25°C reference temperature for Beta equation assumed)
|
||||
float temp_K = 1.0/(1.0/(273.15+25) + 1.0/thermistorBetaValue*log(rts/10000.0)); // K
|
||||
temperature = (1.0 - c) * (temp_K - 273.15) + c * temperature;
|
||||
temperature = 1.0/(1.0/(273.15+25) + 1.0/thermistorBetaValue*log(rts/10000.0)) - 273.15;
|
||||
#endif
|
||||
|
||||
}
|
||||
|
||||
uint32_t rtc_read_backup_reg(uint32_t BackupRegister) {
|
||||
@@ -531,7 +337,6 @@ void rtc_write_backup_reg(uint32_t BackupRegister, uint32_t data) {
|
||||
HAL_PWR_DisableBkUpAccess();
|
||||
}
|
||||
|
||||
|
||||
#ifdef OLED_ENABLED
|
||||
void update_screen(void)
|
||||
{
|
||||
@@ -539,7 +344,7 @@ void update_screen(void)
|
||||
oled.drawBitmap(6, 0, bmp_pv_panel, 16, 16, 1);
|
||||
oled.drawBitmap(104, 0, bmp_load, 16, 16, 1);
|
||||
|
||||
if (state == STANDBY) {
|
||||
if (dcdc.enabled() == false) {
|
||||
oled.drawBitmap(27, 3, bmp_disconnected, 32, 8, 1);
|
||||
}
|
||||
else {
|
||||
@@ -562,29 +367,29 @@ void update_screen(void)
|
||||
oled.drawRect(66, 4, 2, 5, 1); // bar 5
|
||||
|
||||
oled.setTextCursor(0, 18);
|
||||
oled.printf("%4.1fV", solar_voltage / 1000.0);
|
||||
oled.printf("%4.1fV", solar_voltage.read());
|
||||
oled.setTextCursor(0, 26);
|
||||
oled.printf("%4.0fW", abs(solar_power) / 1000.0);
|
||||
oled.printf("%4.0fW", fabs(dcdc_power));
|
||||
|
||||
oled.setTextCursor(42, 18);
|
||||
oled.printf("%5.2fV", battery_voltage / 1000.0);
|
||||
oled.printf("%5.2fV", battery_voltage.read());
|
||||
oled.setTextCursor(42, 26);
|
||||
oled.printf("%5.2fA", abs(dcdc_current) / 1000.0);
|
||||
oled.printf("%5.2fA", fabs(dcdc_current));
|
||||
|
||||
oled.setTextCursor(90, 18);
|
||||
oled.printf("%5.2fA\n", abs(load_current) / 1000.0);
|
||||
oled.printf("%5.2fA\n", fabs(load_current));
|
||||
oled.setTextCursor(90, 26);
|
||||
oled.printf("%5.1fW", abs(load_current) / 1000.0 * battery_voltage / 1000.0);
|
||||
oled.printf("%5.1fW", fabs(load_current) * battery_voltage);
|
||||
|
||||
oled.setTextCursor(0, 36);
|
||||
oled.printf("Day +%5.0fWh -%5.0fWh", input_Wh_day, fabs(output_Wh_day));
|
||||
oled.printf("Tot +%4.1fkWh -%4.1fkWh", input_Wh_total / 1000.0, fabs(output_Wh_total) / 1000.0);
|
||||
|
||||
oled.setTextCursor(0, 56);
|
||||
oled.printf("Temp %.1f PWM %.1f%% ", temperature, mppt.get_duty_cycle() * 100.0);
|
||||
switch (state) {
|
||||
case STANDBY:
|
||||
oled.printf("Standby\n");
|
||||
oled.printf("T %.1f PWM %.1f%% ", temperature, dcdc.get_duty_cycle() * 100.0);
|
||||
switch (charger.get_state()) {
|
||||
case CHG_IDLE:
|
||||
oled.printf("Idle\n");
|
||||
break;
|
||||
case CHG_CC:
|
||||
oled.printf("CC charge\n");
|
||||
@@ -595,12 +400,6 @@ void update_screen(void)
|
||||
case CHG_TRICKLE:
|
||||
oled.printf("Trickle chg\n");
|
||||
break;
|
||||
case CHG_BIKE:
|
||||
oled.printf("Bike gen\n");
|
||||
break;
|
||||
case TEST:
|
||||
oled.printf("Test\n");
|
||||
break;
|
||||
default:
|
||||
oled.printf("Error\n");
|
||||
break;
|
||||
|
||||
Reference in New Issue
Block a user