Lamp-Da 0.1
A compact lantern project
Loading...
Searching...
No Matches
battery.h
Go to the documentation of this file.
1
5#ifndef BATTERY_H
6#define BATTERY_H
7
8#include <cstdint>
9
12
13namespace lampda {
14namespace physical {
15
17namespace battery {
18
20extern uint16_t get_raw_battery_voltage_mv();
21
33extern bool can_battery_be_charged();
34
41inline uint16_t liion_mv_to_battery_percent(const uint16_t liionLevel_mv, const uint8_t batteryCountSerie)
42{
44 static curve_t liionVoltagePercentToRealPercent({// low end of the curve, sharp drop
45 curve_t::point_t {3000, 0},
46 curve_t::point_t {3210, 500},
47 curve_t::point_t {3350, 1000},
48 curve_t::point_t {3430, 1500},
49 curve_t::point_t {3470, 2000},
50 // linear approximation works well enough in range 20-90%
51 curve_t::point_t {4080, 9000},
52 curve_t::point_t {4110, 9500},
53 curve_t::point_t {4180, 10000}});
54
55 // sample the curve
56 return liionVoltagePercentToRealPercent.sample(liionLevel_mv / batteryCountSerie);
57}
58
64inline uint16_t get_level_percent(const uint16_t batteryVoltage_mV, const uint8_t cellCount = batteryCount)
65{
66 return liion_mv_to_battery_percent(batteryVoltage_mV, cellCount);
67}
68
72inline uint16_t get_level_safe(const uint16_t battery_mv, const uint8_t cellCount = batteryCount)
73{
74 // save the init values
75 static const uint16_t minSafeLevel_percent = get_level_percent(batteryMinVoltageSafe_mV);
76 static const uint16_t maxSafeLevel_percent = get_level_percent(batteryMaxVoltageSafe_mV);
77
78 // get the result of the total battery life, map it to the safe battery level
79 // indicated by user
80 return lmpd_constrain<uint16_t>(
81 lmpd_map<uint16_t>(
82 get_level_percent(battery_mv, cellCount), minSafeLevel_percent, maxSafeLevel_percent, 0, 10000),
83 0,
84 10000);
85}
86
88inline uint16_t get_battery_level()
89{
90 // get the result of the total battery life, map it to the safe battery level
91 // indicated by user
93}
94
99
104
105} // namespace battery
106} // namespace physical
107} // namespace lampda
108
109#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:83
Define curves types, that can be sampled.
uint16_t get_level_safe(const uint16_t battery_mv, const uint8_t cellCount=batteryCount)
returns the battery level, mapped to the desired safe battery level
Definition: battery.h:72
uint16_t get_raw_battery_voltage_mv()
Return the battery voltage and raise the battery incoherent alert if needed.
Definition: battery.cpp:54
uint16_t get_battery_minimum_cell_level()
Return the level of the cell of the stack with the minimum battery.
Definition: battery.cpp:90
uint16_t get_battery_level()
returns the battery level, corresponding to user safe choice (0-10000)
Definition: battery.h:88
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:41
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:86
uint16_t get_battery_maximum_cell_level()
Return the level of the cell of the stack with the maximum battery.
Definition: battery.cpp:119
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:88
uint16_t get_level_percent(const uint16_t batteryVoltage_mV, const uint8_t cellCount=batteryCount)
transform a battery voltage measurment to a percent * 100 estimate
Definition: battery.h:64
Program scope.
Definition: control_fixed_modes.hpp:12
static constexpr uint8_t batteryCount
number of batteries for this model
Definition: constants.h:54
static constexpr uint16_t batteryMaxVoltageSafe_mV
max voltage of a li-ion cell to maximise lifetime
Definition: constants.h:111
static constexpr uint16_t batteryMinVoltageSafe_mV
min voltage of a li-ion cell to maximise lifetime
Definition: constants.h:117
Define useful functions.