Lamp-Da 0.1
A compact lantern project
Loading...
Searching...
No Matches
battery.h
Go to the documentation of this file.
1
5#ifndef COMPONENT_BATTERY_H
6#define COMPONENT_BATTERY_H
7
8#include <cstdint>
9
11
13
14namespace lampda {
15namespace component {
16
18namespace battery {
19
21extern uint16_t get_raw_battery_voltage_mv();
22
34extern bool can_battery_be_charged();
35
42inline uint16_t liion_mv_to_battery_percent(const uint16_t liionLevel_mv, const uint8_t batteryCountSerie)
43{
45 static curve_t liionVoltagePercentToRealPercent =
46 ::lampda::common::curves::make_linear_curve( // low end of the curve, sharp drop
47 curve_t::point_t {3000, 0},
48 curve_t::point_t {3210, 500},
49 curve_t::point_t {3350, 1000},
50 curve_t::point_t {3430, 1500},
51 curve_t::point_t {3470, 2000},
52 // linear approximation works well enough in range 20-90%
53 curve_t::point_t {4080, 9000},
54 curve_t::point_t {4110, 9500},
55 curve_t::point_t {4180, 10000});
56
57 // sample the curve
58 return liionVoltagePercentToRealPercent.sample(liionLevel_mv / batteryCountSerie);
59}
60
66inline uint16_t get_level_percent(const uint16_t batteryVoltage_mV,
67 const uint8_t cellCount = physical_constants::batterySeriesCount)
68{
69 return liion_mv_to_battery_percent(batteryVoltage_mV, cellCount);
70}
71
75inline uint16_t get_level_safe(const uint16_t battery_mv,
76 const uint8_t cellCount = physical_constants::batterySeriesCount)
77{
78 // save the init values
79 static const uint16_t minSafeLevel_percent = get_level_percent(physical_constants::batteryMinVoltageSafe_mV);
80 static const uint16_t maxSafeLevel_percent = get_level_percent(physical_constants::batteryMaxVoltageSafe_mV);
81
82 // get the result of the total battery life, map it to the safe battery level
83 // indicated by user
84 return lmpd_constrain<uint16_t>(
85 lmpd_map<uint16_t>(
86 get_level_percent(battery_mv, cellCount), minSafeLevel_percent, maxSafeLevel_percent, 0, 10000),
87 0,
88 10000);
89}
90
92inline uint16_t get_battery_level()
93{
94 // get the result of the total battery life, map it to the safe battery level
95 // indicated by user
97}
98
103
108
109} // namespace battery
110} // namespace component
111} // namespace lampda
112
113#endif
Given a set of points, will fit multiple linear segments to it.
Definition: curves.h:36
U sample(const T x) const
Sample a point Y from a given x.
Definition: curves.h:92
Define curves types, that can be sampled.
uint16_t get_raw_battery_voltage_mv()
Return the battery voltage and raise the battery incoherent alert if needed.
Definition: battery.cpp:56
uint16_t get_level_percent(const uint16_t batteryVoltage_mV, const uint8_t cellCount=physical_constants::batterySeriesCount)
transform a battery voltage measurment to a percent * 100 estimate
Definition: battery.h:66
uint16_t get_level_safe(const uint16_t battery_mv, const uint8_t cellCount=physical_constants::batterySeriesCount)
returns the battery level, mapped to the desired safe battery level
Definition: battery.h:75
uint16_t liion_mv_to_battery_percent(const uint16_t liionLevel_mv, const uint8_t batteryCountSerie)
convert a single liion battery voltage to a percent level model
Definition: battery.h:42
uint16_t get_battery_minimum_cell_level()
Return the level of the cell of the stack with the minimum battery.
Definition: battery.cpp:106
uint16_t get_battery_level()
returns the battery level, corresponding to user safe choice (0-10000)
Definition: battery.h:92
bool can_battery_be_charged()
Return true if this battery can be charged Check only for validity, not voltage. If this is false,...
Definition: battery.cpp:104
bool is_battery_usable_as_power_source()
return true if the battery pack can be used as a energy source This status is updated after calling g...
Definition: battery.cpp:102
uint16_t get_battery_maximum_cell_level()
Return the level of the cell of the stack with the maximum battery.
Definition: battery.cpp:135
static constexpr uint8_t batterySeriesCount
number of batteries in series for this model
Definition: constants.h:61
static constexpr uint16_t batteryMaxVoltageSafe_mV
max voltage of a li-ion cell to maximise lifetime
Definition: constants.h:119
static constexpr uint16_t batteryMinVoltageSafe_mV
min voltage of a li-ion cell to maximise lifetime
Definition: constants.h:125
Program scope.
Definition: control_fixed_modes.hpp:14
Define useful functions.