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:
@@ -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;
|
||||
}
|
||||
@@ -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
|
||||
Reference in New Issue
Block a user