Files
MicrOBU/docs/07-sensor-fusion-logging.md
T
Ashin Walpola 7dd83dba3d Record motion sensors, raw GPS and clocks for sensor fusion, and add Ride settings
Phase 1 of GNSS/IMU sensor fusion: record what the filter needs, so it can
be built and tuned offline against real rides before it changes anything
on air (docs/07-sensor-fusion-logging.md).

Settings > Ride replaces Units, whose metric switch did nothing (the app is
metric throughout). It holds the phone position (handlebar, bag or pocket),
written into every V2X log as meta.phone_mount, and "Record motion
sensors", off by default.

With that switch on when a V2X log starts, the log (schema 2, file name
ending in _imu-<mount>) also records:
- accelerometer, gyroscope and gravity at 100 Hz into a new imu table, each
  sample with its own SensorEvent timestamp, collected on a background
  thread;
- raw GPS_PROVIDER fixes as gnss source phone_gps, next to the fused
  location, and for every phone fix its monotonic time, speed and bearing
  accuracy and provider;
- the monotonic, wall and GNSS clocks once a second into a new clock table,
  to line all timestamps up afterwards and show any phone clock step;
- the sensors' names and rates and the GNSS chip in meta.

Both settings are read once at the start, so one log is one mount and one
mode. The logger screen shows the mount, the motion sensor row count and
the raw GPS fix age while it runs.

tools/fusion_log_check.py checks a recorded log (sensor rates and gaps, both
phone sources, clock alignment and steps) and ends in PASS, WARN or FAIL.
Tested on synthetic logs built from V2xLogWriter's own table definitions;
the bench and ride checks on the Pixel 9 Pro are in TODO.
2026-11-19 15:17:02 +01:00

7.5 KiB
Raw Blame History

07 – Sensor fusion, phase 0 and 1: inputs and logging (2026-11-18)

The goal is a GNSS/IMU fusion filter on the phone that gives the CAM and VAM a fresh position at every send, plus a better heading, yaw rate and acceleration, on any phone mount. Phase 0 fixed the inputs the transmit path already had. Phase 1 records everything the filter needs, so the filter can be built and tuned offline against real rides before it touches what goes on air. Hardware checks still to do are in TODO.md ("Sensor fusion ...").

Phases

Phase What State
0 Correct inputs: yaw rate about the vertical, fix times on GNSS time, monotonic clock for intervals done (727c911, sign fix 6fa91d1)
1 Logging: motion sensors, both phone GNSS sources, clocks, phone mount done, rides pending
2 Filter core: 5-state EKF (position, heading, speed, gyro bias), pure Kotlin, replayed on phase 1 logs next
3 Any mount: forward-axis estimate, rigidity check, mount-dependent noise after 2
4 Offline tuning: GNSS outage drift, heading vs. course over ground, innovation statistics with 2 and 3
5 Into CAM/VAM, the confidence ellipse and the use-case engine, behind a switch last

Phase 0 in short

  • Yaw rate is the gyroscope projected onto gravity (domain/fusion/YawRateEstimator), the rotation about the true vertical however the phone is held, averaged over 100 ms, at SENSOR_DELAY_GAME. The gyroscope's z axis alone was only right with the phone flat, screen up.
  • Time on air is the GNSS time of the fix itself: its monotonic age (Location.getElapsedRealtimeNanos) taken off GNSS time now (GnssTimeSource.atElapsedRealtime). The phone's clock setting, or a step of it, cannot reach the CAM or GN timestamps.
  • Intervals (elevated CAM rate, VAM generation rules, acceleration from GNSS speed) use the monotonic clock, so setting the phone clock back cannot hold VAMs off.
  • Yaw rate sign: the CAM's YawRateValue is positive to the left; the app's Cam.yawRateDps is positive to the right. CamUperCodec and CamParser flip it; the CiT One's obu_gnss sends it right-positive already (checked against logged rides), so ObuGnssParser does not.

Settings > Ride

Replaces the old Units screen (the app is metric throughout; that switch did nothing).

  • Phone position: Handlebar, Bag (on the bike) or Pocket (also a backpack). Stored in RidePreferences as handlebar / bag / pocket and written into every V2X log as meta.phone_mount. The filter will use it to choose how much to trust the gyroscope: fixed to the bike on the handlebar (but turning with the steering), mostly fixed in a bag, moving with the rider's body in a pocket. It is a manual setting; detecting the mount from the sensors is phase 3 work, and these labelled logs are what it would be built and checked on.
  • Record motion sensors: off by default. When on at the moment a V2X log starts, that log also records the data below. It adds about 300 rows a second, tens of MB per hour.

Both are read once when a log starts, so one file is always one mount and one mode. Changing them mid-log takes effect from the next log.

What a fusion log contains

Settings > Developer > V2X data logger, with Record motion sensors on. File name v2x_log_<start>_<hardware>_imu-<mount>.db (without the suffix when it is off). Schema version 2 (meta.schema_version, PRAGMA user_version); version 1 logs have no imu or clock table and none of the new gnss columns.

imu (new)

Column
sensor accel (m/s², gravity included), gyro (rad/s), gravity (m/s², TYPE_GRAVITY, points up)
elapsed_ns the sample's own SensorEvent.timestamp, on SystemClock.elapsedRealtimeNanos
ts_ms the same instant as wall-clock epoch ms, for browsing only
x, y, z device frame: x right, y up the screen, z out of the screen

Requested at 100 Hz (meta.imu_period_us = 10000), on a background thread of its own. gravity is only logged when the phone has a real TYPE_GRAVITY sensor (the Pixel 9 Pro does); the sensors' names, vendors and maximum rates are in meta.sensor_*.

gnss (new columns)

Column
source phone = fused location (Google Play services, as before), phone_gps = raw GPS_PROVIDER (new, fusion logging only), obu = the CiT One's obu_gnss
elapsed_ns the fix's own monotonic time; phone sources only. What fusion lines fixes up with sensor samples on
speed_accuracy_mps, bearing_accuracy_deg 1-sigma, where the provider gives them
provider fused or gps

fix_ms is the fix's instant as wall-clock ms and ts_ms when the app received it, so ts_ms - fix_ms is the delivery delay. The fused and the raw source are logged side by side because the Pixel's fused location includes Google's city-street corrections while the raw one is the chip alone and not pre-filtered; the rides decide which one the filter uses. meta.gnss_hardware names the GNSS chip.

clock (new)

Once a second, the three phone clocks read back to back: elapsed_ns (monotonic), wall_ms (the phone clock), gnss_ms (SystemClock.currentGnssTimeClock, NULL without GNSS time). Sensor samples and phone fixes carry monotonic time, received messages and OBU fixes wall time, and the transmit path GNSS time; these rows convert between them, and show the phone clock's error and any step in it.

Checking a log

py -3.11 tools/fusion_log_check.py v2x_log_..._imu-handlebar.db

Read-only, standard library only. Per file it prints the metadata, each sensor's sample count, rate, interval distribution, gaps and magnitude, each GNSS source's rate, accuracy, delivery delay and whether its fixes fall inside the sensor recording, the distance between the fused and raw fixes (and the OBU's, on the CiT One), and the phone clock's error and steps. It ends with PASS, WARN or FAIL lines, and exits with 1 if anything failed:

  • FAIL: logging was off, no imu/clock table, a sensor missing, a sensor gap over 1 s, a phone source missing, phone fixes without elapsed_ns.
  • WARN: a sensor rate outside 80–130 Hz, gaps over 100 ms, an accelerometer or gravity magnitude far from g, a GNSS gap over 5 s, fixes with no gyroscope sample within 20 ms, no GNSS time, a phone clock step, a log that was not stopped cleanly.

Tested against synthetic logs built from V2xLogWriter's own table definitions (a clean ride, an injected 300 ms gyroscope gap, a 14-minute clock step, logging off, fixes without monotonic time); not yet against a real ride.

Test rides

Recorded per mount (handlebar, bag, pocket), 20–30 minutes each, with Record motion sensors on and the phone mount set before pressing Start. Each ride should include left and right turns, a roundabout, stops of at least 10 s (the filter estimates the gyroscope offset while standing), and if possible a bridge or underpass for a real GNSS outage. The list to tick off is in TODO.md.

Code

domain/fusion/PhoneMount.kt the mount setting and its stored ids
data/RidePreferences.kt, viewmodel/RideSettingsViewModel.kt Settings > Ride storage and screen state
ui/screens/SettingsScreen.kt (RideSettingsScreen) the screen
data/SensorRepository.kt gpsProviderFlow, sensor flows on a chosen thread, describeSensor
data/log/V2xLogger.kt, data/log/V2xLogWriter.kt the logging and the schema
ui/screens/V2xLogSettingsScreen.kt shows mount, motion sensor row count and raw GPS fix age while logging
tools/fusion_log_check.py the check above