Merge branch 'main' into development

This commit is contained in:
dasemu authored and GitHub committed 2026-10-26 06:48:25 +01:00
commit b6eeb62929
10 files changed
+232 -22

No files matched your search

@@ -32,6 +32,9 @@ class RouteGuidanceTracker(private val route: RouteResult, private val config: G
private var lastTimestamp: Long? = null
private val outside = SustainedFixes()
private val arrival = SustainedFixes()
private val rejoin = SustainedFixes()
private var importedDeviation = false
private var rejoinProgress: Double? = null
private var arrived = false
private var needsRecovery = false
private var lastState: NavigationState? = null
@@ -45,7 +48,7 @@ class RouteGuidanceTracker(private val route: RouteResult, private val config: G
fix.accuracyMeters.isFinite() && fix.accuracyMeters in 0f..config.maxAccuracyMeters &&
(lastNow == null || nowMs >= lastNow!!)
if (!usable) {
outside.reset(); arrival.reset()
outside.reset(); arrival.reset(); rejoin.reset(); rejoinProgress = null
needsRecovery = lastPoint != null
return remember(base.copy(speed = 0f, navigationStatus = NavigationStatus.GPS_LOST))
}
@@ -54,7 +57,7 @@ class RouteGuidanceTracker(private val route: RouteResult, private val config: G
if (arrived) return remember(base.copy(speed = 0f, remainingDistanceMeters = 0,
etaEpochMillis = if (route.imported) null else epochMs, navigationStatus = NavigationStatus.ARRIVED))
if (needsRecovery || lastNow?.let { nowMs - it > config.maxFixAgeMs } == true) {
outside.reset(); arrival.reset()
outside.reset(); arrival.reset(); rejoin.reset(); rejoinProgress = null
needsRecovery = false
acquired = false
lastPoint = fix.position
@@ -71,11 +74,30 @@ class RouteGuidanceTracker(private val route: RouteResult, private val config: G
lastNow = nowMs
lastTimestamp = fix.timestampMs
if (jumped) {
outside.reset(); arrival.reset()
outside.reset(); arrival.reset(); rejoin.reset(); rejoinProgress = null
return remember(base.copy(speed = 0f, navigationStatus = NavigationStatus.GPS_LOST))
}
val projection = line.project(fix.position, progress, forwardAllowance)
var projection = line.project(fix.position, progress, forwardAllowance)
val outsideThreshold = max(config.offRouteMeters, fix.accuracyMeters * 2.0)
if (route.imported && importedDeviation) {
// A detour may rejoin anywhere on the original trace, including behind progress.
// Require stable fresh fixes before widening the normal crossing-safe progress window.
val candidate = line.project(fix.position, 0.0, line.total)
val candidateProgress = candidate.local
val stable = candidateProgress != null && candidate.localDistance <= outsideThreshold &&
(rejoinProgress == null || abs(candidateProgress - rejoinProgress!!) <= movementAllowance)
val confirmed = rejoin.observe(stable, distinct, nowMs, 3, 2_000)
rejoinProgress = candidateProgress.takeIf { stable }
if (!confirmed) {
arrival.reset()
return remember(base.copy(navigationStatus = NavigationStatus.OFF_ROUTE))
}
progress = requireNotNull(candidateProgress)
acquired = true
importedDeviation = false
outside.reset(); rejoin.reset(); rejoinProgress = null
projection = candidate
}
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.
@@ -84,6 +106,7 @@ class RouteGuidanceTracker(private val route: RouteResult, private val config: G
acquired = true
}
if (offRoute) {
if (route.imported) importedDeviation = true
arrival.reset()
return remember(base.copy(navigationStatus = NavigationStatus.OFF_ROUTE))
}
@@ -2,6 +2,7 @@ package dev.navframe.core
import org.junit.Assert.*
import org.junit.Test
import kotlin.math.abs
class RouteGuidanceTest {
// Equatorial segments are ~111.2m each; original route duration is 30 seconds.
@@ -220,6 +221,82 @@ class RouteGuidanceTest {
assertEquals(initial.remainingDistanceMeters, reversed.remainingDistanceMeters)
}
private val importedTrace = RouteResult(
listOf(GeoPoint(0.0, 0.0), GeoPoint(0.0, 0.02)), emptyList(), 2224, 0, imported = true)
private fun deviatedImportedTracker(): RouteGuidanceTracker {
val tracker = RouteGuidanceTracker(importedTrace)
tracker.update(fix(lon = 0.005), 0, 0)
tracker.update(fix(lat = -0.0007, lon = 0.005, time = 2), 3_000, 0)
tracker.update(fix(lat = -0.0007, lon = 0.005, time = 3), 6_000, 0)
val deviated = tracker.update(fix(lat = -0.0007, lon = 0.005, time = 4), 9_000, 0)
assertEquals(NavigationStatus.OFF_ROUTE, deviated.navigationStatus)
assertSame(importedTrace, deviated.route)
return tracker
}
@Test fun `imported detour rejoins ahead or behind frozen progress without replacing trace`() {
for (longitude in listOf(0.012, 0.002)) {
val tracker = deviatedImportedTracker()
// The isolated relocation is rejected. Stable fixes then reacquire the original trace.
assertEquals(NavigationStatus.GPS_LOST,
tracker.update(fix(lon = longitude, time = 5), 10_000, 0).navigationStatus)
for (i in 6L..7L) {
assertEquals(NavigationStatus.OFF_ROUTE,
tracker.update(fix(lon = longitude, time = i), (i + 5) * 1_000, 0).navigationStatus)
}
val resumed = tracker.update(fix(lon = longitude, time = 8), 13_000, 0)
assertEquals(NavigationStatus.NAVIGATING, resumed.navigationStatus)
assertTrue(abs(((0.02 - longitude) * 111_195) - resumed.remainingDistanceMeters!!) < 1)
assertSame(importedTrace, resumed.route)
assertNull(resumed.nextManeuver)
assertNull(resumed.etaEpochMillis)
}
}
@Test fun `repeated stale and inaccurate fixes cannot confirm imported rejoin`() {
val tracker = deviatedImportedTracker()
tracker.update(fix(lon = 0.012, time = 5), 10_000, 0)
val candidate = fix(lon = 0.012, time = 6)
assertEquals(NavigationStatus.OFF_ROUTE, tracker.update(candidate, 11_000, 0).navigationStatus)
assertEquals(NavigationStatus.OFF_ROUTE, tracker.update(candidate, 14_000, 0).navigationStatus)
assertEquals(NavigationStatus.GPS_LOST,
tracker.update(fix(lon = 0.012, time = 7, accuracy = 40f), 15_000, 0).navigationStatus)
assertEquals(NavigationStatus.GPS_LOST,
tracker.update(fix(lon = 0.012, time = 8), 16_000, 0, 15_001).navigationStatus)
tracker.update(fix(lon = 0.012, time = 9), 17_000, 0)
tracker.update(fix(lon = 0.012, time = 10), 18_000, 0)
tracker.update(fix(lon = 0.012, time = 11), 19_000, 0)
assertEquals(NavigationStatus.NAVIGATING,
tracker.update(fix(lon = 0.012, time = 12), 20_000, 0).navigationStatus)
}
@Test fun `closed imported trace rejoin at shared start and finish cannot falsely arrive`() {
val loop = importedTrace.copy(geometry = listOf(GeoPoint(0.0, 0.0), GeoPoint(0.0, 0.002),
GeoPoint(0.002, 0.002), GeoPoint(0.002, 0.0), GeoPoint(0.0, 0.0)))
val tracker = RouteGuidanceTracker(loop)
tracker.update(fix(lon = 0.0005), 0, 0)
for (i in 2L..4L) tracker.update(fix(lat = -0.0007, lon = 0.0005, time = i), (i - 1) * 3_000, 0)
for (i in 5L..9L) {
val state = tracker.update(fix(time = i), (i + 5) * 1_000, 0)
assertNotEquals(NavigationStatus.ARRIVED, state.navigationStatus)
if (i >= 7) {
assertEquals(NavigationStatus.NAVIGATING, state.navigationStatus)
assertTrue(state.remainingDistanceMeters!! > 800)
}
}
}
@Test fun `imported crossing preserves progress until deviation is sustained`() {
val crossing = importedTrace.copy(geometry = listOf(GeoPoint(0.0, 0.0), GeoPoint(0.0, 0.002),
GeoPoint(0.002, 0.002), GeoPoint(0.002, 0.0), GeoPoint(0.0, 0.0), GeoPoint(0.0, -0.002)))
val tracker = RouteGuidanceTracker(crossing)
val initial = tracker.update(fix(), 0, 0)
val next = tracker.update(fix(lon = -0.00005, time = 2), 1_000, 0)
assertEquals(initial.remainingDistanceMeters, next.remainingDistanceMeters)
assertEquals(NavigationStatus.NAVIGATING, next.navigationStatus)
}
@Test fun `maneuver arrows use type rather than translated prose`() {
assertEquals(ManeuverDirection.LEFT, maneuverDirection(15))
assertEquals(ManeuverDirection.RIGHT, maneuverDirection(10))