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:
Ashin Walpola
2026-10-28 16:48:18 +01:00
parent d0fcb45a55
commit 9f77f9f0e4
28 changed files with 955 additions and 31 deletions
+53
View File
@@ -3,6 +3,8 @@
// enqueues frames. Link messages: implementation/station-link/README.md.
#include "link_protocol.hpp"
#include "link_service.hpp"
#include "battery.hpp"
#include "battery_monitor.hpp"
#include "board_controls.hpp"
#include "simple_ble.hpp"
#include "serial_link.hpp"
@@ -30,6 +32,9 @@ struct Incoming {
microbu::serial::Frame frame;
};
constexpr std::int64_t battery_interval_us = 5000000; // one cell sample every 5 s
constexpr std::uint32_t battery_usb_window_ms = 10000; // a USB frame this recently means a phone powers the board
QueueHandle_t frames = nullptr; // FIFO of Incoming* (8 bytes each); frame bytes never enter the queue itself
/// @brief Attempts to heap-allocate an Incoming so it can be pushed onto the frames queue.
@@ -74,6 +79,36 @@ void pause_ble_while_usb_in_use() {
microbu::ble::set_advertising_allowed(!usb_in_use);
}
/// @brief MicrOBU: the battery policy with this build's Kconfig thresholds.
microbu::BatteryConfig battery_config() {
microbu::BatteryConfig config;
config.warn_mv = CONFIG_MICROBU_BATTERY_WARN_MV;
config.warn_clear_mv = CONFIG_MICROBU_BATTERY_WARN_MV + 100; // 100 mV of hysteresis
config.shutdown_mv = CONFIG_MICROBU_BATTERY_SHUTDOWN_MV;
config.resume_mv = CONFIG_MICROBU_BATTERY_RESUME_MV;
return config;
}
/// @brief MicrOBU: samples the cell, publishes it in STATUS and powers the board down before the
/// unprotected cell is over-discharged. A phone on USB powers the board itself, so the cell does not
/// force a shutdown while one is talking (see BatteryMonitor::update).
void check_battery(microbu::Battery& battery, microbu::BatteryMonitor& monitor, microbu::LinkService& link) {
const auto now_ms = static_cast<std::uint32_t>(esp_timer_get_time() / 1000);
const auto last_usb = microbu::serial::last_frame_ms();
const bool usb_active = last_usb != 0 && now_ms - last_usb < battery_usb_window_ms;
monitor.update(battery.measure(), usb_active, now_ms);
std::uint8_t flags = 0;
if (monitor.low()) flags |= microbu::link::battery_flag_low;
if (monitor.critical()) flags |= microbu::link::battery_flag_critical;
link.set_battery(monitor.millivolts(), flags);
if (monitor.shutdown_due()) {
ESP_LOGW(TAG, "cell at %u mV and no phone on USB: shutting down", unsigned(monitor.millivolts()));
link.send_status_now(); // the phone sees the critical flag before the link goes away
vTaskDelay(pdMS_TO_TICKS(500));
microbu::Battery::deep_sleep();
}
}
/// @brief FreeRTOS task body: owns the station, link service and board controls. Drains the incoming queue.
void station_task(void*) {
microbu::Station station;
@@ -81,6 +116,9 @@ void station_task(void*) {
microbu::BoardControls controls;
station.on_disseminated([&controls] { controls.blink(); });
station.on_received([&controls] { controls.blink(); });
microbu::Battery battery;
microbu::BatteryMonitor monitor(battery_config());
std::int64_t next_battery_us = 0;
ESP_LOGI(TAG, "station task ready; the phone/PC configures the station over BLE or USB Serial-JTAG");
for (;;) { /// main loop: drain the incoming queue, tick the station and link service, tick the controls
Incoming* incoming = nullptr;
@@ -95,12 +133,27 @@ void station_task(void*) {
link.tick();
controls.tick();
pause_ble_while_usb_in_use();
if (esp_timer_get_time() >= next_battery_us) {
next_battery_us = esp_timer_get_time() + battery_interval_us;
check_battery(battery, monitor, link);
}
}
}
}
/// @brief Firmware entry point: initializes NVS, watchdog, serial/BLE transports, and starts the station task.
extern "C" void app_main() {
// MicrOBU: woken from a low-battery shutdown. Judge the cell before anything else starts, and
// go back to sleep unless it has recovered (charged over USB).
if (microbu::Battery::woke_from_battery_sleep()) {
microbu::Battery battery;
const auto millivolts = battery.measure();
if (!microbu::BatteryMonitor(battery_config()).resume_ok(millivolts)) {
ESP_LOGW(TAG, "cell still at %u mV, back to sleep", unsigned(millivolts));
microbu::Battery::deep_sleep();
}
ESP_LOGI(TAG, "cell at %u mV, starting", unsigned(millivolts));
}
// NVS: the credential store (and the Wi-Fi driver's calibration data)
esp_err_t nvs = nvs_flash_init();
if (nvs == ESP_ERR_NVS_NO_FREE_PAGES || nvs == ESP_ERR_NVS_NEW_VERSION_FOUND) {