Files
MicrOBU/obu-firmware/main/battery_policy.hpp
T

72 lines
3.7 KiB
C++
Raw Normal View History

#pragma once
// Cell-voltage policy for the XIAO ESP32-C5's LiPo: smoothing, the low-battery warning with
// hysteresis, and the decision to shut down. Pure logic with no ESP-IDF dependency, so the host
// tests (test/host/test_battery.cpp) compile this header unmodified. Kept out of BatteryMonitor, which
// only samples the cell.
#include <cstdint>
namespace microbu {
/// @brief Thresholds and timing of the battery policy. Millivolts at the cell, not at the ADC pin.
struct BatteryPolicyConfig {
std::uint16_t warn_mv = 3500; ///< low-battery warning below this
std::uint16_t warn_clear_mv = 3600;///< ... and cleared again at or above this (hysteresis)
std::uint16_t shutdown_mv = 3300; ///< shut down below this ...
unsigned shutdown_samples = 6; ///< ... for this many consecutive samples (6 x 5 s = 30 s: rides out TX sag)
std::uint32_t grace_ms = 60000; ///< no shutdown in the first minute after boot
std::uint16_t resume_mv = 3600; ///< after a shutdown the board only runs again at or above this
std::uint16_t valid_min_mv = 2500; ///< a reading outside valid_min..valid_max is "no cell / no sensor"
std::uint16_t valid_max_mv = 4500;
};
/// @brief Turns raw cell-voltage samples into a smoothed value, a warning flag and a shutdown decision.
class BatteryPolicy {
public:
/// @param config Thresholds; the defaults suit a single LiPo cell.
explicit BatteryPolicy(BatteryPolicyConfig config = {}) : config_(config) {}
/// @brief Feeds one sample, taken every few seconds.
/// @param mv Cell voltage in millivolts, or 0 when the measurement failed.
/// @param usb_active A phone is talking over USB, so the board is powered from the phone and
/// the cell cannot be the reason it would stop.
/// @param uptime_ms Milliseconds since boot.
void update(std::uint16_t mv, bool usb_active, std::uint32_t uptime_ms) {
if (mv < config_.valid_min_mv || mv > config_.valid_max_mv) {
// No cell (or no sensor): report "not measured" and never shut down on it.
valid_ = false; ema_ = 0; low_ = false; below_ = 0;
return;
}
if (!valid_) { valid_ = true; ema_ = mv; } // first sample after a gap: no ramp from zero
else ema_ = static_cast<std::uint16_t>(ema_ + (static_cast<int>(mv) - static_cast<int>(ema_)) / 4);
if (ema_ < config_.warn_mv) low_ = true;
else if (ema_ >= config_.warn_clear_mv) low_ = false;
if (ema_ < config_.shutdown_mv && !usb_active && uptime_ms >= config_.grace_ms) ++below_;
else below_ = 0;
}
/// @return Smoothed cell voltage in millivolts, 0 while there is no valid reading.
std::uint16_t millivolts() const { return valid_ ? ema_ : 0; }
/// @return true once the cell fell below the warning threshold, until it is back above the clear threshold.
bool low() const { return low_; }
/// @return true while the smoothed voltage is below the shutdown threshold (not yet sustained).
bool critical() const { return valid_ && ema_ < config_.shutdown_mv; }
/// @return true once the cell has stayed below the shutdown threshold with no USB link for long enough.
bool shutdown_due() const { return below_ >= config_.shutdown_samples; }
/// @param mv A single reading taken right after a wake-up.
/// @return true if the board may run: the cell recovered, or there is no valid reading to judge by.
bool resume_ok(std::uint16_t mv) const {
if (mv < config_.valid_min_mv || mv > config_.valid_max_mv) return true;
return mv >= config_.resume_mv;
}
private:
BatteryPolicyConfig config_;
std::uint16_t ema_ = 0;
bool valid_ = false;
bool low_ = false;
unsigned below_ = 0;
};
} // namespace microbu