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:
@@ -12,6 +12,7 @@ import com.hawhamburg.micr0bu.data.transport.StationStatus
|
||||
import com.hawhamburg.micr0bu.data.transport.credentialsSegment
|
||||
import org.junit.Assert.assertArrayEquals
|
||||
import org.junit.Assert.assertEquals
|
||||
import org.junit.Assert.assertFalse
|
||||
import org.junit.Assert.assertNotNull
|
||||
import org.junit.Assert.assertNull
|
||||
import org.junit.Assert.assertTrue
|
||||
@@ -107,6 +108,83 @@ class StationLinkTest {
|
||||
@Test
|
||||
fun `a STATUS of the wrong length is refused, as the Python decoder does`() {
|
||||
assertNull(StationStatus.decode(ByteArray(StationStatus.SIZE + 1)))
|
||||
assertNull(StationStatus.decode(ByteArray(StationStatus.SIZE + 2)))
|
||||
assertNull(StationStatus.decode(ByteArray(StationStatus.SIZE - 1)))
|
||||
}
|
||||
|
||||
/** The STATUS body of the Python-encoder test above, without its 4-octet message header. */
|
||||
private val legacyStatusBody = (
|
||||
"40e20100010800021122334455b80b49387a4c12eb00010b0000000c0000000d0000000e000000" +
|
||||
"0f000000100000001100000012000000130000001400000015000000160000001700000018000000" +
|
||||
"190000001a0000001b0000001c000000fbb2b7ffa6000000"
|
||||
).hexToBytes()
|
||||
|
||||
@Test
|
||||
fun `a 103 octet STATUS from an older board has no battery`() {
|
||||
val legacy = legacyStatusBody
|
||||
assertEquals(StationStatus.SIZE, legacy.size)
|
||||
val status = StationStatus.decode(legacy)!!
|
||||
assertEquals(0, status.batteryMv)
|
||||
assertNull(status.batteryPercent)
|
||||
assertFalse(status.batteryLow)
|
||||
assertFalse(status.batteryCritical)
|
||||
assertEquals(28L, status.potiUpdates)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `the battery fields after the 103 octets are decoded`() {
|
||||
// 3871 mV = 0x0F1F, flags: low
|
||||
val body = legacyStatusBody + "1f0f01".hexToBytes()
|
||||
assertEquals(StationStatus.SIZE_WITH_BATTERY, body.size)
|
||||
val status = StationStatus.decode(body)!!
|
||||
assertEquals(3871, status.batteryMv)
|
||||
assertEquals(StationStatus.BATTERY_FLAG_LOW, status.batteryFlags)
|
||||
assertTrue(status.batteryLow)
|
||||
assertFalse(status.batteryCritical)
|
||||
assertEquals(60, status.batteryPercent)
|
||||
assertEquals(28L, status.potiUpdates)
|
||||
assertEquals(717_254_800_123L, status.itsTimeMs)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `critical flag, and octets of a later extension are tolerated`() {
|
||||
// 3290 mV = 0x0CDA, flags: low + critical, then two octets of a later extension
|
||||
val body = legacyStatusBody + "da0c03aabb".hexToBytes()
|
||||
val status = StationStatus.decode(body)!!
|
||||
assertEquals(3290, status.batteryMv)
|
||||
assertTrue(status.batteryLow)
|
||||
assertTrue(status.batteryCritical)
|
||||
assertEquals(0, status.batteryPercent)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `half a battery field is malformed`() {
|
||||
assertNull(StationStatus.decode(legacyStatusBody + "1f".hexToBytes()))
|
||||
assertNull(StationStatus.decode(legacyStatusBody + "1f0f".hexToBytes()))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `battery flags are ignored while no voltage is reported`() {
|
||||
val body = legacyStatusBody + "000003".hexToBytes()
|
||||
val status = StationStatus.decode(body)!!
|
||||
assertFalse(status.batteryLow)
|
||||
assertFalse(status.batteryCritical)
|
||||
assertNull(status.batteryPercent)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `LiPo percent is monotonic, clamped and anchored`() {
|
||||
assertEquals(0, StationStatus.lipoPercent(3000))
|
||||
assertEquals(0, StationStatus.lipoPercent(3300))
|
||||
assertEquals(5, StationStatus.lipoPercent(3500))
|
||||
assertEquals(100, StationStatus.lipoPercent(4200))
|
||||
assertEquals(100, StationStatus.lipoPercent(4300))
|
||||
var last = -1
|
||||
for (mv in 3000..4300 step 10) {
|
||||
val p = StationStatus.lipoPercent(mv)
|
||||
assertTrue("$mv mV -> $p% went down", p >= last)
|
||||
last = p
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
Reference in New Issue
Block a user