Fix off-route detection and TFT network lifecycle

This commit is contained in:
dasemu committed 2026-10-01 13:57:29 +02:00
1 parent 823d5db5d7
commit 6fec09f20f
11 files changed
+267 -18

No files matched your search

@@ -76,7 +76,7 @@ class RouteGuidanceTracker(private val route: RouteResult, private val config: G
}
val projection = line.project(fix.position, progress, forwardAllowance)
val outsideThreshold = max(config.offRouteMeters, fix.accuracyMeters * 2.0)
val offRoute = outside.observe(projection.distance > outsideThreshold, distinct, nowMs,
val offRoute = outside.observe(projection.localDistance > outsideThreshold, distinct, nowMs,
config.offRouteFixes, config.offRouteDurationMs)
// Freeze progress during inaccurate projection, so driving parallel to the road does not advance turns.
if (projection.distance <= outsideThreshold && projection.localDistance <= outsideThreshold && projection.local != null) {
@@ -87,14 +87,10 @@ class RouteGuidanceTracker(private val route: RouteResult, private val config: G
arrival.reset()
return remember(base.copy(navigationStatus = NavigationStatus.OFF_ROUTE))
}
if (projection.distance > outsideThreshold) {
if (projection.localDistance > outsideThreshold) {
arrival.reset()
return remember(base.copy(navigationStatus = NavigationStatus.NAVIGATING))
}
if (projection.local == null || projection.localDistance > outsideThreshold) {
arrival.reset()
return remember(base.copy(speed = 0f, navigationStatus = NavigationStatus.GPS_LOST))
}
val remaining = line.total - progress
val close = projection.distance <= outsideThreshold && remaining <= config.arrivalRemainingMeters &&
earthDistance(fix.position, route.geometry.last()) <= config.arrivalRadiusMeters
@@ -205,8 +201,21 @@ private class RouteLine(private val route: RouteResult) {
val distance = earthDistance(point, projected)
val along = cumulative[i - 1] + (cumulative[i] - cumulative[i - 1]) * fraction
nearestDistance = min(nearestDistance, distance)
if (along >= previous - 20 && along <= previous + forwardAllowance && distance < localDistance - 0.01) {
localDistance = distance; localProgress = along
// Compare against the reachable section, including its boundary points. A reliable
// position behind progress or on a distant later branch is a deviation, not GPS loss.
// Clamping the projection also avoids artificial gaps at the progress-window edges.
val start = max(cumulative[i - 1], previous - 20)
val end = min(cumulative[i], previous + forwardAllowance)
if (start <= end) {
val reachableAlong = along.coerceIn(start, end)
val length = cumulative[i] - cumulative[i - 1]
val reachableFraction = if (length == 0.0) 0.0 else (reachableAlong - cumulative[i - 1]) / length
val reachablePoint = GeoPoint(a.latitude + dy * reachableFraction,
wrappedLongitude(a.longitude + wrappedLongitude(b.longitude - a.longitude) * reachableFraction))
val reachableDistance = earthDistance(point, reachablePoint)
if (reachableDistance < localDistance - 0.01) {
localDistance = reachableDistance; localProgress = reachableAlong
}
}
}
return Projection(nearestDistance, localProgress, localDistance)
@@ -188,9 +188,36 @@ class RouteGuidanceTest {
tracker.update(fix(), 0, 0)
tracker.update(fix(lon = 0.005, time = 2), 1_000, 0)
val stableButDistant = tracker.update(fix(lon = 0.005, time = 3), 2_000, 0)
assertEquals(NavigationStatus.GPS_LOST, stableButDistant.navigationStatus)
assertEquals(NavigationStatus.NAVIGATING, stableButDistant.navigationStatus)
assertNull(stableButDistant.remainingDistanceMeters)
assertNull(stableButDistant.nextManeuver)
tracker.update(fix(lon = 0.005, time = 4), 5_000, 0)
val offRoute = tracker.update(fix(lon = 0.005, time = 5), 7_000, 0)
assertEquals(NavigationStatus.OFF_ROUTE, offRoute.navigationStatus)
assertTrue(ReroutePolicy().shouldRequest(offRoute, 7_000))
}
@Test fun `turning back on the same road confirms deviation rather than GPS loss`() {
val longRoute = RouteResult(listOf(GeoPoint(0.0, 0.0), GeoPoint(0.0, 0.02)), emptyList(), 2224, 180)
val tracker = RouteGuidanceTracker(longRoute)
tracker.update(fix(lon = 0.01), 0, 0)
tracker.update(fix(lon = 0.0097, time = 2), 1_000, 0)
tracker.update(fix(lon = 0.0094, time = 3), 2_000, 0)
tracker.update(fix(lon = 0.0091, time = 4), 4_000, 0)
val reversed = tracker.update(fix(lon = 0.0088, time = 5), 7_000, 0)
assertEquals(NavigationStatus.OFF_ROUTE, reversed.navigationStatus)
assertTrue(ReroutePolicy().shouldRequest(reversed, 7_000))
assertNull(reversed.nextManeuver)
assertEquals(GeoPoint(0.0, 0.0088), reversed.position)
}
@Test fun `small movement past progress window uses boundary instead of false GPS loss`() {
val longRoute = RouteResult(listOf(GeoPoint(0.0, 0.0), GeoPoint(0.0, 0.02)), emptyList(), 2224, 180)
val tracker = RouteGuidanceTracker(longRoute)
val initial = tracker.update(fix(lon = 0.01), 0, 0)
val reversed = tracker.update(fix(lon = 0.00975, time = 2), 1_000, 0)
assertEquals(NavigationStatus.NAVIGATING, reversed.navigationStatus)
assertEquals(initial.remainingDistanceMeters, reversed.remainingDistanceMeters)
}
@Test fun `maneuver arrows use type rather than translated prose`() {