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:
Martin Jäger
2017-04-14 18:04:13 +02:00
parent 0908ef3aba
commit ead80a86ad
10 changed files with 771 additions and 573 deletions
+53
View File
@@ -0,0 +1,53 @@
/* mbed library for ADC readings
* Copyright (c) 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.
*/
#include "AnalogValue.h"
float AnalogValue::_vcc = 3.3;
AnalogValue::AnalogValue(PinName pin, float multiplier, const int num_readings, float filter_constant) :
_adc(pin), _num_readings(num_readings)
{
_multiplier = multiplier;
update(); // initialize value without filter
_filter_constant = filter_constant;
}
void AnalogValue::update()
{
float tmp = adc_read_avg(_adc, _num_readings) * _vcc * _multiplier;
_value = (1.0 - _filter_constant) * tmp + _filter_constant * _value;
}
float AnalogValue::adc_read_avg(AnalogIn& input, int num_readings)
{
// averaging: use read_u16 for faster calculation
unsigned int sum_adc_readings = 0;
for (int i = 0; i < num_readings; i++) {
sum_adc_readings += input.read_u16(); // normalized to 0xFFFF
}
return (float) (sum_adc_readings / num_readings) / (float)0xFFFF;
}
float AnalogValue::read()
{
return _value;
}
void AnalogValue::update_reference_voltage(AnalogIn& adc_ref, float ref_voltage, int num_readings)
{
_vcc = ref_voltage / adc_read_avg(adc_ref, num_readings);
}
+88
View File
@@ -0,0 +1,88 @@
/* mbed library for ADC readings
* Copyright (c) 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.
*/
#include "mbed.h"
class AnalogValue
{
public:
/** Create an AnalogValue object
*
* @param pin ADC input pin
* @param multiplier Factor to calculate desired output (e.g. for voltage dividers, gain, etc.)
* @param num_readings Number of ADC readings to be averaged
* @param filter_constant Low pass filter constant (0.0 to 1.0, 0.0 for no filtering)
*/
AnalogValue(PinName pin, float multiplier, const int num_readings = 8, float filter_constant = 0.0);
/** Updates ADC reading and applies low-pass filter. Should be called with a regular time interval
*/
void update();
/** Charger state machine update, should be called exactly once per second
*
* @param adc_ref AnalogIn for the pin with the reference voltage attached
* @param ref_voltage Rated reference voltage
* @param num_readings Number of ADC readings to be averaged
*/
static void update_reference_voltage(AnalogIn& adc_ref, float ref_voltage, int num_readings = 8);
/** Read the last updated (and filtered) ADC result (incl. multiplier)
*
* @returns
* Saved ADC result with multiplier applied
*/
float read();
/** An operator shorthand for read()
*
* The float() operator can be used as a shorthand for read() to simplify common code sequences
*
* Example:
* @code
* float x = volume.read();
* float x = volume;
*
* if(volume.read() > 0.25) { ... }
* if(volume > 0.25) { ... }
* @endcode
*/
operator float() {
return read();
}
private:
/** Read out ADC results and apply averaging and/or low pass filter
*
* @param input AnalogIn for the pin to be read
* @param num_readings Number of ADC readings to be averaged
*
* @returns
* Averaged and filtered ADC result
*/
static float adc_read_avg(AnalogIn& input, int num_readings);
static float _vcc; // actual VCC of ADC
AnalogIn _adc;
float _multiplier;
float _value;
const int _num_readings;
float _filter_constant;
};
+167
View File
@@ -0,0 +1,167 @@
/* mbed library for a battery charge controller
* Copyright (c) 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.
*/
#include "ChargeController.h"
ChargeController::ChargeController(ChargingProfile& profile) :
_profile(profile)
{
_charging_enabled = 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 ChargeController::update(float battery_voltage, float battery_current) {
// Load management
if (battery_voltage < _profile.cell_voltage_load_disconnect * _profile.num_cells) {
_discharging_enabled = false;
}
if (battery_voltage >= _profile.cell_voltage_load_reconnect * _profile.num_cells) {
_discharging_enabled = true;
}
// state machine
switch (_state) {
case CHG_IDLE: {
if (battery_voltage < _profile.num_cells * _profile.cell_voltage_recharge
&& (time(NULL) - _time_state_changed) > _profile.time_limit_recharge)
{
_target_current = _profile.charge_current_max;
_target_voltage = _profile.num_cells * _profile.cell_voltage_max;
_charging_enabled = true;
enter_state(CHG_CC);
}
break;
}
case CHG_CC: {
if (battery_voltage > _target_voltage) {
_target_voltage = _profile.num_cells * _profile.cell_voltage_max;
enter_state(CHG_CV);
}
break;
}
case CHG_CV: {
if (battery_voltage >= _target_voltage) {
_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 ((battery_current < _profile.current_cutoff_CV && (time(NULL) - _time_voltage_limit_reached) < 2)
|| (time(NULL) - _time_state_changed) > _profile.time_limit_CV)
{
if (_profile.equalization_enabled) {
// TODO: additional conditions!
_target_voltage = _profile.num_cells * _profile.cell_voltage_equalization;
_target_current = _profile.current_limit_equalization;
enter_state(CHG_EQUALIZATION);
}
else if (_profile.trickle_enabled) {
_target_voltage = _profile.num_cells * _profile.cell_voltage_trickle;
enter_state(CHG_TRICKLE);
}
else {
_target_current = 0;
_charging_enabled = false;
enter_state(CHG_IDLE);
}
}
break;
}
case CHG_TRICKLE: {
if (battery_voltage >= _target_voltage) {
_time_voltage_limit_reached = time(NULL);
}
if (time(NULL) - _time_voltage_limit_reached > _profile.time_trickle_recharge)
{
_target_current = _profile.charge_current_max;
_target_voltage = _profile.num_cells * _profile.cell_voltage_max;
enter_state(CHG_CC);
}
// assumtion: trickle does not harm the battery --> never go back to idle
// (for Li-ion battery: disable trickle!)
break;
}
}
}
void ChargeController::enter_state(int next_state)
{
printf("Enter State: %d\n", next_state);
_time_state_changed = time(NULL);
_state = next_state;
}
bool ChargeController::discharging_enabled()
{
return _discharging_enabled;
}
bool ChargeController::charging_enabled()
{
return _charging_enabled;
}
int ChargeController::get_state()
{
return _state;
}
float ChargeController::read_target_current()
{
return _target_current;
}
float ChargeController::read_target_voltage()
{
return _target_voltage;
}
+134
View File
@@ -0,0 +1,134 @@
/* mbed library for a battery charge controller
* Copyright (c) 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.
*/
// settings: battery_capacity, charge_profile
// input: actual charge current, actual battery voltage
// output: SOC, target_voltage, target_current (=max current)
#ifndef CHARGECONTROLLER_H
#define CHARGECONTROLLER_H
#include "mbed.h"
struct ChargingProfile {
int num_cells;
// State: Standby
int time_limit_recharge; // sec
float cell_voltage_recharge; // V
// State: CC/bulk
float charge_current_max; // A PCB maximum: 20 A
// State: CV/absorption
float cell_voltage_max; // max voltage per cell
int time_limit_CV; // sec
float current_cutoff_CV; // A
// State: float/trickle
bool trickle_enabled;
float cell_voltage_trickle; // target voltage for trickle charging of lead-acid batteries
int time_trickle_recharge; // sec
// State: equalization
bool equalization_enabled;
float cell_voltage_equalization; // V
int time_limit_equalization; // sec
float current_limit_equalization; // A
int equalization_trigger_time; // weeks
int equalization_trigger_deep_cycles; // times
float cell_voltage_load_disconnect;
float cell_voltage_load_reconnect;
// TODO
float temperature_compensation;
};
// possible charger states
enum charger_states {CHG_IDLE, CHG_CC, CHG_CV, CHG_TRICKLE, CHG_EQUALIZATION};
class ChargeController
{
public:
/** Create Charge Controller object
*
* @param profile ChargingProfile struct including voltage limits, etc.
*/
ChargeController(ChargingProfile& profile);
/** Get target battery current for current charger state
*
* @returns
* Target current (A)
*/
float read_target_current();
/** Get target battery voltage for current charger state
*
* @returns
* Target voltage (V)
*/
float read_target_voltage();
/** Determine if charging of the battery is allowed
*
* @returns
* True if charging is allowed
*/
bool charging_enabled();
/** Determine if discharging the battery is allowed
*
* @returns
* True if discharging is allowed
*/
bool discharging_enabled();
/** Charger state machine update, should be called exactly once per second
*
* @param battery_voltage Actual measured battery voltage (V)
* @param battery_current Actual measured battery current (A)
*/
void update(float battery_voltage, float battery_current);
/** Get current charge controller state
*
* @returns
* CHG_IDLE, CHG_CC, CHG_CV, CHG_TRICKLE or CHG_EQUALIZATION
*/
int get_state();
private:
/** Enter a different charger state
*
* @param next_state Next state (e.g. CHG_CC)
*/
void enter_state(int next_state);
ChargingProfile& _profile; // all charging profile variables
int _state; // valid states: enum charger_states
int _time_state_changed; // timestamp of last state change
float _target_voltage; // target voltage for current state
float _target_current; // target current for current state
int _time_voltage_limit_reached; // last time the CV limit was reached
bool _charging_enabled;
bool _discharging_enabled;
};
#endif // CHARGECONTROLLER_H
@@ -1,5 +1,5 @@
/* mbed library for MPPT buck converter
* Copyright (c) 2016 Martin Jäger (www.libre.solar)
/* mbed library for half bridge driver PWM generation
* 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.
@@ -14,59 +14,18 @@
* limitations under the License.
*/
#include "BuckConverter.h"
#include "HalfBridge.h"
BuckConverter::BuckConverter(int freq_kHz)
HalfBridge::HalfBridge(int freq_kHz)
{
init_registers();
frequency_kHz(freq_kHz);
last_time_CV_reset();
_pwm_delta = 1;
_enabled = false;
}
void BuckConverter::update(int input_voltage_mV, int output_voltage_mV, int output_current_mA) {
int output_power_mW = output_voltage_mV * output_current_mA / 1000;
//printf("P=%d P_prev=%d Vi=%d Vo=%d Io=%d\n", output_power_mW, _output_power_prev_mW, input_voltage_mV, output_voltage_mV, output_current_mA);
if (output_voltage_mV > _max_voltage_mV) {
// increase input voltage to lower output voltage
if (output_current_mA > _min_current_mA) {
duty_cycle_step(-1);
_time_voltage_limit_reached = time(NULL);
}
else {
// switch off and wait for voltage to go down
// (otherwise current could get negative, i.e. into solar panel)
TIM1->BDTR &= ~(TIM_BDTR_MOE);
}
//printf("output_voltage_mV = %d, _max_voltage_mV = %d\n", output_voltage_mV, _max_voltage_mV);
//fflush(stdout);
}
else if (~(TIM1->BDTR & TIM_BDTR_MOE) && _enabled == true) {
// switched off because of too high output voltage?
// now: output_voltage_mV <= _max_voltage_mV --> start again
start((float) output_voltage_mV / (input_voltage_mV - 1000.0));
}
else if (output_current_mA > _max_current_mA) {
// increase input voltage to decrease current
duty_cycle_step(-1);
}
else {
// start MPPT
if (_output_power_prev_mW > output_power_mW) {
_pwm_delta = -_pwm_delta;
}
duty_cycle_step(_pwm_delta);
}
_output_power_prev_mW = output_power_mW;
}
void BuckConverter::init_registers() {
void HalfBridge::init_registers() {
// Enable peripheral clock of GPIOA and GPIOB
RCC->AHBENR |= RCC_AHBENR_GPIOAEN | RCC_AHBENR_GPIOBEN;
@@ -112,27 +71,7 @@ void BuckConverter::init_registers() {
TIM1->EGR |= TIM_EGR_UG;
}
void BuckConverter::set_max_voltage(int voltage_mV) {
_max_voltage_mV = voltage_mV;
}
void BuckConverter::set_max_current(int current_mA) {
_max_current_mA = current_mA;
}
void BuckConverter::set_min_current(int current_mA) {
_min_current_mA = current_mA;
}
time_t BuckConverter::last_time_CV() {
return _time_voltage_limit_reached;
}
void BuckConverter::last_time_CV_reset() {
_time_voltage_limit_reached = 0x7FFFFFFF; // maximum value
}
void BuckConverter::frequency_kHz(int freq) {
void HalfBridge::frequency_kHz(int freq) {
_pwm_resolution = SystemCoreClock / (freq * 1000);
@@ -141,8 +80,7 @@ void BuckConverter::frequency_kHz(int freq) {
TIM1->ARR = _pwm_resolution / 2;
}
void BuckConverter::set_duty_cycle(float duty) {
void HalfBridge::set_duty_cycle(float duty) {
float duty_target;
// protection against wrong settings which could destroy the hardware
@@ -159,7 +97,7 @@ void BuckConverter::set_duty_cycle(float duty) {
TIM1->CCR1 = _pwm_resolution / 2 * duty_target;
}
void BuckConverter::duty_cycle_step(int delta)
void HalfBridge::duty_cycle_step(int delta)
{
float duty_target;
duty_target = (float)(TIM1->CCR1 + delta) / (_pwm_resolution / 2);
@@ -177,12 +115,12 @@ void BuckConverter::duty_cycle_step(int delta)
}
float BuckConverter::get_duty_cycle() {
float HalfBridge::get_duty_cycle() {
return (float)(TIM1->CCR1) / (_pwm_resolution / 2);;
}
void BuckConverter::deadtime_ns(int deadtime) {
void HalfBridge::deadtime_ns(int deadtime) {
uint8_t deadtime_clocks = (SystemCoreClock / 1000 / 1000) * deadtime / 1000;
@@ -194,7 +132,7 @@ void BuckConverter::deadtime_ns(int deadtime) {
}
void BuckConverter::start(float pwm_duty) {
void HalfBridge::start(float pwm_duty) {
set_duty_cycle(pwm_duty);
// Break and Dead-Time Register
@@ -205,7 +143,7 @@ void BuckConverter::start(float pwm_duty) {
}
void BuckConverter::stop() {
void HalfBridge::stop() {
// Break and Dead-Time Register
// MOE = 1: Main output enable
@@ -214,8 +152,13 @@ void BuckConverter::stop() {
_enabled = false;
}
bool HalfBridge::enabled() {
void BuckConverter::lock_settings() {
return _enabled;
}
void HalfBridge::lock_settings() {
// TODO: does not work properly... maybe HW bug?
@@ -223,7 +166,7 @@ void BuckConverter::lock_settings() {
TIM1->BDTR |= TIM_BDTR_LOCK_1 | TIM_BDTR_LOCK_0;
}
void BuckConverter::duty_cycle_limits(float min_duty, float max_duty) {
void HalfBridge::duty_cycle_limits(float min_duty, float max_duty) {
_min_duty = min_duty;
_max_duty = max_duty;
@@ -1,5 +1,5 @@
/* mbed library for MPPT buck converter
* Copyright (c) 2016 Martin Jäger (www.libre.solar)
/* mbed library for half bridge driver PWM generation
* 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.
@@ -14,21 +14,19 @@
* limitations under the License.
*/
#ifndef HALFBRIDGE_H
#define HALFBRIDGE_H
#include "mbed.h"
class BuckConverter
class HalfBridge
{
public:
/** Create a half bridge object
*/
BuckConverter(int freq_kHz);
void set_max_voltage(int voltage_mV);
void set_max_current(int current_mA);
void set_min_current(int current_mA);
time_t last_time_CV ();
void last_time_CV_reset();
HalfBridge(int freq_kHz);
/** Set the PWM frequency in kHz
*
@@ -57,13 +55,12 @@ public:
*/
void stop();
/** Updates duty cycle to match voltage levels based on measured values
/** Get status of the PWM output
*
* @param input_voltage_mV Measured input voltage (mV)
* @param output_voltage_mV Measured output voltage (mA)
* @param output_current_mA Measured output current (mA)
* @returns
* True if PWM output enabled
*/
void update(int input_voltage_mV, int output_voltage_mV, int output_current_mA);
bool enabled();
/** Set the duty cycle of the PWM signal between 0.0 and 1.0
*
@@ -103,10 +100,6 @@ private:
int _pwm_delta;
bool _enabled;
int _max_voltage_mV;
int _max_current_mA;
int _min_current_mA;
int _output_power_prev_mW;
time_t _time_voltage_limit_reached;
};
#endif // HALFBRIDGE_H