Lamp-Da 0.1
A compact lantern project
Loading...
Searching...
No Matches
charge_policy.h
Go to the documentation of this file.
1
7#ifndef COMPONENT_CHARGE_POLICY_H
8#define COMPONENT_CHARGE_POLICY_H
9
10#include <cstdint>
11
13
14namespace lampda {
15namespace component {
16namespace charger {
18namespace policy {
19
26static constexpr uint16_t fullLevel = 9900;
28static constexpr uint16_t rechargeLevel = 9500;
30static constexpr uint32_t endOfChargeDeglitch_ms = 5000;
31
33static constexpr uint16_t cellOverVoltageMargin_mV = 10;
42static constexpr uint32_t overVoltagePause_ms = 20000;
43
45struct Inputs
46{
48 bool isBalancerValid = false;
50 uint16_t maxCell_mV = 0;
52 uint16_t pack_mV = 0;
54 uint16_t level = 0;
56 uint16_t chargeCurrent_mA = 0;
58 bool isCharging = false;
59};
60
62inline bool is_over_voltage(const Inputs& in)
63{
65}
66
68inline bool is_end_of_charge(const Inputs& in)
69{
70 if (not in.isCharging)
71 return false;
72 // pack voltage reached the target
74 return true;
75 // constant voltage phase, current tapered off.
76 // The level condition prevents a weak power source (low charge current) to be mistaken for an end of charge
78}
79
84{
85public:
90 bool update(const uint32_t time_ms, const Inputs& in)
91 {
92 // safety: over voltage stops the charge, whatever the state
93 if (is_over_voltage(in))
94 {
95 _isOverVoltage = true;
96 _overVoltageTime_ms = time_ms;
97 _isEndOfChargeDeglitching = false;
98 // already full: end the charge
99 if (in.level >= fullLevel)
100 _isFull = true;
101 return false;
102 }
103
104 // stay stopped until the cells are back under the resume voltage
105 if (_isOverVoltage)
106 {
107 if (time_ms - _overVoltageTime_ms < overVoltagePause_ms or not in.isBalancerValid or
109 return false;
110 _isOverVoltage = false;
111 }
112
113 // battery full: wait for the level to drop
114 if (_isFull)
115 {
116 if (in.level >= rechargeLevel)
117 return false;
118 _isFull = false;
119 }
120
121 if (is_end_of_charge(in))
122 {
123 if (not _isEndOfChargeDeglitching)
124 {
125 _isEndOfChargeDeglitching = true;
126 _endOfChargeTime_ms = time_ms;
127 }
128 else if (time_ms - _endOfChargeTime_ms > endOfChargeDeglitch_ms)
129 {
130 _isEndOfChargeDeglitching = false;
131 _isFull = true;
132 return false;
133 }
134 }
135 else
136 {
137 _isEndOfChargeDeglitching = false;
138 }
139 return true;
140 }
141
144 {
145 _isFull = false;
146 _isEndOfChargeDeglitching = false;
147 }
148
150 bool is_full() const { return _isFull; }
152 bool is_over_voltage_stopped() const { return _isOverVoltage; }
153
154private:
155 bool _isFull = false;
156 bool _isOverVoltage = false;
157 uint32_t _overVoltageTime_ms = 0;
158 bool _isEndOfChargeDeglitching = false;
159 uint32_t _endOfChargeTime_ms = 0;
160};
161
162} // namespace policy
163} // namespace charger
164} // namespace component
165} // namespace lampda
166
167#endif
Track the charge stop state (battery full, over voltage)
Definition: charge_policy.h:84
bool is_full() const
True if the battery charge is done.
Definition: charge_policy.h:150
bool update(const uint32_t time_ms, const Inputs &in)
Update the state with new measurements.
Definition: charge_policy.h:90
void clear_full()
Forget the battery full state (power source change). The over voltage state is kept,...
Definition: charge_policy.h:143
bool is_over_voltage_stopped() const
True if the charge is stopped by an over voltage.
Definition: charge_policy.h:152
static constexpr uint32_t endOfChargeDeglitch_ms
The end of charge condition must stay true for this time before the charge is considered done.
Definition: charge_policy.h:30
static constexpr uint16_t fullLevel
Battery level (percent * 100) from which the battery is considered full.
Definition: charge_policy.h:26
static constexpr uint16_t packOverVoltage_mV
A pack over this voltage stops the charge immediately.
Definition: charge_policy.h:37
bool is_end_of_charge(const Inputs &in)
True if the battery reached the end of charge: pack at the target voltage, or charge current tapered ...
Definition: charge_policy.h:68
static constexpr uint32_t overVoltagePause_ms
After an over voltage, the charge stays stopped at least this long (lets the balancer work)
Definition: charge_policy.h:42
static constexpr uint16_t cellResumeVoltage_mV
After an over voltage, the charge can restart when all cells are below this voltage.
Definition: charge_policy.h:40
static constexpr uint16_t cellOverVoltageMargin_mV
Accepted cell voltage above the safe maximum (cell imbalance, measure noise)
Definition: charge_policy.h:33
static constexpr uint16_t cellOverVoltage_mV
A cell over this voltage stops the charge immediately.
Definition: charge_policy.h:35
static constexpr uint16_t rechargeLevel
Once full, the charge restarts below this battery level (percent * 100)
Definition: charge_policy.h:28
bool is_over_voltage(const Inputs &in)
True if a cell or the pack is over the charge voltage limit.
Definition: charge_policy.h:62
static constexpr uint8_t batterySeriesCount
number of batteries in series for this model
Definition: constants.h:61
static constexpr uint32_t batteryEndOfChargeCurrent_mA
End of charge current (C/20), in milliAmps: in constant voltage phase, the charge is done below this ...
Definition: constants.h:138
static constexpr uint16_t batteryMaxVoltageSafe_mV
max voltage of a li-ion cell to maximise lifetime
Definition: constants.h:119
static constexpr uint16_t maxSafeLiionVoltage_mV
maximum allowed li-ion voltage safe zone
Definition: constants.h:89
Program scope.
Definition: control_fixed_modes.hpp:14
Snapshot of the measurements used by the charge stop rules.
Definition: charge_policy.h:46
uint16_t maxCell_mV
Highest cell voltage, from the balancer.
Definition: charge_policy.h:50
uint16_t chargeCurrent_mA
Measured charge current.
Definition: charge_policy.h:56
bool isBalancerValid
True if the balancer measurements below are valid.
Definition: charge_policy.h:48
uint16_t level
Battery level, in percent * 100.
Definition: charge_policy.h:54
uint16_t pack_mV
Pack voltage, from the balancer.
Definition: charge_policy.h:52
bool isCharging
True if the charger is charging.
Definition: charge_policy.h:58
Define the system hardware constants.