132 lines
7.5 KiB
Markdown
132 lines
7.5 KiB
Markdown
# 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 |
|