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.
152 lines
5.5 KiB
C++
152 lines
5.5 KiB
C++
// Host tests for the battery policy (battery_monitor.hpp) and the STATUS battery extension
|
|
// (link_protocol.cpp). No ESP-IDF involved: both sources compile unmodified on the PC.
|
|
#include "battery_monitor.hpp"
|
|
#include "link_protocol.hpp"
|
|
|
|
#include <cstdio>
|
|
#include <cstdlib>
|
|
|
|
using namespace microbu;
|
|
|
|
static int failures = 0;
|
|
#define CHECK(cond) do { if (!(cond)) { std::printf("FAIL %s:%d %s\n", __FILE__, __LINE__, #cond); ++failures; } } while (0)
|
|
|
|
// Feeds the same sample n times, 5 s apart, starting at t_ms.
|
|
static void feed(BatteryMonitor& m, std::uint16_t mv, int n, bool usb, std::uint32_t& t_ms) {
|
|
for (int i = 0; i < n; ++i) { m.update(mv, usb, t_ms); t_ms += 5000; }
|
|
}
|
|
|
|
static void test_smoothing_and_validity() {
|
|
BatteryMonitor m;
|
|
CHECK(m.millivolts() == 0); // nothing measured yet
|
|
m.update(4100, false, 0);
|
|
CHECK(m.millivolts() == 4100); // first sample taken as is, no ramp from zero
|
|
m.update(4000, false, 5000);
|
|
CHECK(m.millivolts() == 4075); // 1/4 of the step
|
|
m.update(0, false, 10000); // failed measurement
|
|
CHECK(m.millivolts() == 0 && !m.low() && !m.critical() && !m.shutdown_due());
|
|
m.update(4100, false, 15000);
|
|
CHECK(m.millivolts() == 4100); // restarts from the new reading
|
|
m.update(5200, false, 20000); // implausible: no cell connected
|
|
CHECK(m.millivolts() == 0);
|
|
}
|
|
|
|
static void test_warning_hysteresis() {
|
|
BatteryMonitor m;
|
|
std::uint32_t t = 0;
|
|
feed(m, 3700, 3, false, t);
|
|
CHECK(!m.low());
|
|
feed(m, 3450, 20, false, t);
|
|
CHECK(m.low());
|
|
feed(m, 3550, 20, false, t); // above the warning threshold but below the clear threshold
|
|
CHECK(m.low());
|
|
feed(m, 3650, 20, false, t);
|
|
CHECK(!m.low());
|
|
}
|
|
|
|
static void test_shutdown_rules() {
|
|
{ // sustained low, no USB, after the grace period
|
|
BatteryMonitor m;
|
|
std::uint32_t t = 120000;
|
|
feed(m, 3250, 5, false, t);
|
|
CHECK(m.critical() && !m.shutdown_due()); // five samples are not enough
|
|
feed(m, 3250, 1, false, t);
|
|
CHECK(m.shutdown_due());
|
|
}
|
|
{ // a phone on USB powers the board: never shut down, and the count restarts
|
|
BatteryMonitor m;
|
|
std::uint32_t t = 120000;
|
|
feed(m, 3250, 5, false, t);
|
|
feed(m, 3250, 1, true, t);
|
|
CHECK(!m.shutdown_due());
|
|
feed(m, 3250, 5, false, t);
|
|
CHECK(!m.shutdown_due());
|
|
feed(m, 3250, 1, false, t);
|
|
CHECK(m.shutdown_due());
|
|
}
|
|
{ // not within the first minute after boot
|
|
BatteryMonitor m;
|
|
std::uint32_t t = 0;
|
|
feed(m, 3250, 11, false, t); // 0..50 s
|
|
CHECK(!m.shutdown_due());
|
|
feed(m, 3250, 7, false, t); // 55..85 s: six of them at or past the 60 s grace period
|
|
CHECK(m.shutdown_due());
|
|
}
|
|
{ // a dip that recovers (TX sag) does not count
|
|
BatteryMonitor m;
|
|
std::uint32_t t = 120000;
|
|
feed(m, 3250, 4, false, t);
|
|
feed(m, 3900, 10, false, t);
|
|
feed(m, 3250, 4, false, t);
|
|
CHECK(!m.shutdown_due());
|
|
}
|
|
{ // a normal cell never trips it
|
|
BatteryMonitor m;
|
|
std::uint32_t t = 120000;
|
|
feed(m, 3800, 100, false, t);
|
|
CHECK(!m.shutdown_due() && !m.low() && !m.critical());
|
|
}
|
|
{ // no cell / no sensor: no shutdown
|
|
BatteryMonitor m;
|
|
std::uint32_t t = 120000;
|
|
feed(m, 0, 100, false, t);
|
|
CHECK(!m.shutdown_due());
|
|
}
|
|
}
|
|
|
|
static void test_resume() {
|
|
BatteryMonitor m;
|
|
CHECK(!m.resume_ok(3300));
|
|
CHECK(!m.resume_ok(3599));
|
|
CHECK(m.resume_ok(3600));
|
|
CHECK(m.resume_ok(4100));
|
|
CHECK(m.resume_ok(0)); // cannot judge: run
|
|
}
|
|
|
|
static link::Status sample_status() {
|
|
link::Status s;
|
|
s.uptime_ms = 123456; s.configured = 1; s.tickets = 7; s.signed_messages = 42; s.poti_updates = 9;
|
|
s.its_time_ms = 0x0102030405060708ULL;
|
|
s.battery_mv = 3871; s.battery_flags = link::battery_flag_low;
|
|
return s;
|
|
}
|
|
|
|
static void test_status_protocol() {
|
|
const auto body = link::encode(sample_status());
|
|
CHECK(body.size() == link::status_size_battery);
|
|
CHECK(body.size() == 106);
|
|
link::Status d;
|
|
CHECK(link::decode(body, d));
|
|
CHECK(d.battery_mv == 3871 && d.battery_flags == link::battery_flag_low);
|
|
CHECK(d.uptime_ms == 123456 && d.tickets == 7 && d.its_time_ms == 0x0102030405060708ULL);
|
|
|
|
// the 103-octet layout of the colleague's station (and of this firmware before the battery)
|
|
link::Bytes legacy(body.begin(), body.begin() + link::status_size_base);
|
|
link::Status l;
|
|
l.battery_mv = 999; l.battery_flags = 0xFF;
|
|
CHECK(link::decode(legacy, l));
|
|
CHECK(l.battery_mv == 0 && l.battery_flags == 0);
|
|
CHECK(l.uptime_ms == 123456 && l.its_time_ms == 0x0102030405060708ULL);
|
|
|
|
// trailing octets of a later extension are ignored; half a field is malformed
|
|
link::Bytes longer = body;
|
|
longer.push_back(0xEE); longer.push_back(0xEE);
|
|
link::Status x;
|
|
CHECK(link::decode(longer, x) && x.battery_mv == 3871);
|
|
CHECK(!link::decode(link::Bytes(body.begin(), body.begin() + 104), x));
|
|
CHECK(!link::decode(link::Bytes(body.begin(), body.begin() + 105), x));
|
|
CHECK(!link::decode(link::Bytes(body.begin(), body.begin() + 102), x));
|
|
CHECK(!link::decode(link::Bytes(), x));
|
|
}
|
|
|
|
int main() {
|
|
test_smoothing_and_validity();
|
|
test_warning_hysteresis();
|
|
test_shutdown_rules();
|
|
test_resume();
|
|
test_status_protocol();
|
|
if (failures) { std::printf("%d check(s) failed\n", failures); return EXIT_FAILURE; }
|
|
std::printf("battery: all checks passed\n");
|
|
return EXIT_SUCCESS;
|
|
}
|