Monitor the XIAO's battery and make the XIAO the production board
The production OBU is now a Seeed XIAO ESP32-C5 (8 MB flash, one native USB-C port) on a LiPo with no protection board. The firmware reads the cell on GPIO6 (divider switched by GPIO26) every 5 s, sends it in STATUS (battery_mv and battery_flags, octets 103-105; decoders accept 103 or >= 106 octets), warns below 3.50 V and deep-sleeps below 3.30 V when no phone is on USB, waking every 5 min to check the cell again. Thresholds are in Kconfig; host test `make battery`. The app shows voltage, a rough percentage and the low/critical state on the ESP32-C5 card and in the top bar. With several bonded micrOBUs (a replaced board stays bonded), the BLE link tries the one that last connected first and moves on to the next after each failure. tools/station_status_watch.py reads STATUS from the native port.
This commit is contained in:
@@ -0,0 +1,70 @@
|
||||
#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.
|
||||
#include <cstdint>
|
||||
|
||||
namespace microbu {
|
||||
|
||||
/// @brief Thresholds and timing of the battery policy. Millivolts at the cell, not at the ADC pin.
|
||||
struct BatteryConfig {
|
||||
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 BatteryMonitor {
|
||||
public:
|
||||
/// @param config Thresholds; the defaults suit a single LiPo cell.
|
||||
explicit BatteryMonitor(BatteryConfig 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:
|
||||
BatteryConfig config_;
|
||||
std::uint16_t ema_ = 0;
|
||||
bool valid_ = false;
|
||||
bool low_ = false;
|
||||
unsigned below_ = 0;
|
||||
};
|
||||
|
||||
} // namespace microbu
|
||||
Reference in New Issue
Block a user