Files
MicrOBU/app/src/test/java/com/hawhamburg/micr0bu/YawRateEstimatorTest.kt
T
Ashin Walpola 727c9116dd Take the yaw rate about the vertical and stamp fixes with their GNSS time
The phone's CAM and VAM used the gyroscope's z axis as the yaw rate. That
is rotation about the screen's normal, so it only matched the bike's
turning with the phone flat, screen up: tilted on a handlebar it read about
64 % at 50 degrees, upright close to nothing. The gyroscope is now projected
onto gravity (TYPE_GRAVITY), which gives the rotation about the true
vertical however the phone is held, averaged over 100 ms. Gyroscope and
gravity run at SENSOR_DELAY_GAME for this; the yaw rate used to come from a
single sample at SENSOR_DELAY_UI.

Fixes now carry their own monotonic time (Location.getElapsedRealtimeNanos),
speed and bearing accuracy and provider, and sensor samples their own
SensorEvent.timestamp. What goes on air is the GNSS time of the fix itself,
from its monotonic age (GnssTimeSource.atElapsedRealtime), so neither the
phone's clock setting nor a step of it between the fix and transmission
reaches the CAM or GN timestamps. The loop's own intervals (elevated rate,
VAM generation rules) and the GNSS speed differencing for acceleration use
the monotonic clock too, so a clock set back can no longer hold VAMs off.

This is phase 0 of GNSS/IMU sensor fusion: YawRateEstimator is the first
piece of domain/fusion. Unit-tested; the bench and ride checks are in TODO.
2026-11-18 14:56:05 +01:00

122 lines
4.4 KiB
Kotlin

package com.hawhamburg.micr0bu
import com.hawhamburg.micr0bu.domain.fusion.YawRateEstimator
import org.junit.Assert.assertEquals
import org.junit.Assert.assertNotNull
import org.junit.Assert.assertNull
import org.junit.Test
import kotlin.math.cos
import kotlin.math.sin
/**
* The yaw rate must be the rotation about the true vertical whatever way up the phone is, which is
* what the old z-axis reading got wrong for anything but a phone lying flat, screen up.
*/
class YawRateEstimatorTest {
private val g = 9.81f
private val ms = 1_000_000L
private val radToDeg = 180.0 / Math.PI
/** Feeds [up] (a unit vector, device frame) as gravity and a pure yaw of [ccwRadPerSec] about it. */
private fun YawRateEstimator.yawAbout(up: DoubleArray, ccwRadPerSec: Double, startNanos: Long, samples: Int = 5) {
onGravity((up[0] * g).toFloat(), (up[1] * g).toFloat(), (up[2] * g).toFloat(), startNanos)
for (i in 0 until samples) {
onGyro(
(up[0] * ccwRadPerSec).toFloat(),
(up[1] * ccwRadPerSec).toFloat(),
(up[2] * ccwRadPerSec).toFloat(),
startNanos + i * 20 * ms,
)
}
}
@Test
fun `flat phone, left turn, is negative in the clockwise-positive convention`() {
val est = YawRateEstimator()
est.yawAbout(doubleArrayOf(0.0, 0.0, 1.0), 0.5, 0L)
// 0.5 rad/s counter-clockwise = turning left = -28.6 deg/s clockwise-positive, as the old z-axis path gave.
assertEquals(-0.5 * radToDeg, est.yawRateDps(80 * ms)!!, 1e-3)
}
@Test
fun `handlebar tilt does not shrink the yaw rate`() {
// Phone tilted 50 deg toward the rider about its x axis: up is (0, sin 50, cos 50) in the device frame.
val tilt = Math.toRadians(50.0)
val up = doubleArrayOf(0.0, sin(tilt), cos(tilt))
val est = YawRateEstimator()
est.yawAbout(up, 0.5, 0L)
assertEquals(-0.5 * radToDeg, est.yawRateDps(80 * ms)!!, 1e-3)
// The z axis alone would have read only cos 50 = 64 % of it.
assertEquals(0.643, up[2], 1e-3)
}
@Test
fun `upright phone, where the z axis reads nothing, still gives the yaw rate`() {
val est = YawRateEstimator()
est.yawAbout(doubleArrayOf(0.0, 1.0, 0.0), -0.3, 0L)
assertEquals(0.3 * radToDeg, est.yawRateDps(80 * ms)!!, 1e-3)
}
@Test
fun `rotation about a horizontal axis is not yaw`() {
// Flat phone pitching about its x axis, as on a bump: no yaw at all.
val est = YawRateEstimator()
est.onGravity(0f, 0f, g, 0L)
for (i in 0 until 5) est.onGyro(1.0f, 0f, 0f, i * 20 * ms)
assertEquals(0.0, est.yawRateDps(80 * ms)!!, 1e-9)
}
@Test
fun `averages over the window ending at the newest sample`() {
val est = YawRateEstimator(windowNanos = 100 * ms)
est.onGravity(0f, 0f, g, 0L)
est.onGyro(0f, 0f, 10f, 0L) // 300 ms before the newest: outside the window
est.onGyro(0f, 0f, 0.2f, 220 * ms)
est.onGyro(0f, 0f, 0.4f, 260 * ms)
est.onGyro(0f, 0f, 0.6f, 300 * ms)
assertEquals(-0.4 * radToDeg, est.yawRateDps(300 * ms)!!, 1e-3)
}
@Test
fun `no yaw rate before gravity is known`() {
val est = YawRateEstimator()
est.onGyro(0f, 0f, 0.5f, 0L)
assertNull(est.yawRateDps(0L))
}
@Test
fun `no yaw rate once the gyroscope has gone quiet`() {
val est = YawRateEstimator(staleNanos = 500 * ms)
est.yawAbout(doubleArrayOf(0.0, 0.0, 1.0), 0.5, 0L)
assertNotNull(est.yawRateDps(500 * ms))
assertNull(est.yawRateDps(600 * ms))
}
@Test
fun `gyroscope samples are dropped against a stale up direction`() {
val est = YawRateEstimator(staleNanos = 500 * ms)
est.onGravity(0f, 0f, g, 0L)
est.onGyro(0f, 0f, 0.5f, 900 * ms)
assertNull(est.yawRateDps(900 * ms))
}
@Test
fun `a gravity sample too weak to give a direction is ignored`() {
val est = YawRateEstimator()
est.onGravity(0f, 0f, 0.5f, 0L)
est.onGyro(0f, 0f, 0.5f, 0L)
assertNull(est.yawRateDps(0L))
}
@Test
fun `reset forgets the up direction and the samples`() {
val est = YawRateEstimator()
est.yawAbout(doubleArrayOf(0.0, 0.0, 1.0), 0.5, 0L)
est.reset()
assertNull(est.yawRateDps(80 * ms))
est.onGyro(0f, 0f, 0.5f, 100 * ms)
assertNull(est.yawRateDps(100 * ms))
}
}