diff --git a/DevControlBot_garage/DevControlBot/modules/ELM/elm_gun.nim b/DevControlBot_garage/DevControlBot/modules/ELM/elm_gun.nim index b5a4f7f..075e35e 100644 --- a/DevControlBot_garage/DevControlBot/modules/ELM/elm_gun.nim +++ b/DevControlBot_garage/DevControlBot/modules/ELM/elm_gun.nim @@ -313,14 +313,33 @@ proc predictAim*(g: ElmGun, b: GunBelief, power: float64 = FirePower, ## off, so the two differ by exactly one thing. let base = heuristicAim(g, b, power) if base.isNaN: return GunAim(aim: NaN, confidence: 0.0) + # One bearing, ONE coordinate computation: fold the learned term (when the + # model is active and returns a number) into `aim` first, then derive x/y + # from the final bearing exactly once. The old version recomputed x/y for the + # baseline and again for the ELM aim, which is how the two could drift apart. + var aim = base + if useElm and g.loaded: + let corr = elmCorrection(g, b, power) + if not corr.isNaN: + aim = base + corr + result.confidence = abs(corr) + result.correction = corr let d = hypot(b.ex - b.selfX, b.ey - b.selfY) - result = GunAim(aim: base, x: b.selfX + d * cos(base), y: b.selfY + d * sin(base), - confidence: 0.0) - if not useElm or not g.loaded: return - let corr = elmCorrection(g, b, power) - if corr.isNaN: return - result.aim = base + corr - result.x = b.selfX + d * cos(result.aim) - result.y = b.selfY + d * sin(result.aim) - result.confidence = abs(corr) - result.correction = corr \ No newline at end of file + result.aim = aim + result.x = b.selfX + d * cos(aim) + result.y = b.selfY + d * sin(aim) + +proc gunAimErrorDeg*(aimRad, gunDirDeg: float): float = + ## Short-way gun error in DEGREES between a predicted ABSOLUTE bearing + ## (`aimRad`, radians, 0 = east CCW+) and the gun's current heading + ## (`gunDirDeg`, degrees — the API's unit). + ## + ## THE single rad->deg conversion point at the fire-decision boundary. The + ## name carries the output unit, so `abs(gunAimErrorDeg(...)) <= FireSlopDeg` + ## can only ever compare degrees against degrees. The historical bug compared + ## a degree error against `degToRad(FireSlopDeg)`, i.e. 0.052 deg of slop. + ## Matches the API's `normalizeRelativeAngle` ([-180, 180)) without pulling + ## the API into this stdlib-only module. + result = (radToDeg(aimRad) - gunDirDeg) mod 360.0 + if result >= 180.0: result -= 360.0 + elif result < -180.0: result += 360.0 \ No newline at end of file diff --git a/DevControlBot_garage/tests/test_online.nim b/DevControlBot_garage/tests/test_online.nim index 2682fb1..ab3a086 100644 --- a/DevControlBot_garage/tests/test_online.nim +++ b/DevControlBot_garage/tests/test_online.nim @@ -71,6 +71,23 @@ block: check("pending cleared", g.pending.len == 0) check("trace cleared", g.traceTick.len == 0) +# ── 6. deg/rad fire boundary (regression: the FireSlopDeg mix-up) ─────────── +block: + echo "test 6: gun-aim error is DEGREES at the fire boundary" + # aim 0 rad = east; gun at +1 deg -> short-way error -1 deg, NOT -0.0175. + check("1 deg off gives a -1 deg error", + abs(gunAimErrorDeg(0.0, 1.0) + 1.0) < 1e-9, $gunAimErrorDeg(0.0, 1.0)) + # short way round: gun 350 deg from an east(0) aim is +10 deg to reach it. + check("350 deg wraps to +10 deg", + abs(gunAimErrorDeg(0.0, 350.0) - 10.0) < 1e-6, $gunAimErrorDeg(0.0, 350.0)) + # THE historical bug: a degree error compared against degToRad(FireSlopDeg) + # (radians) admitted only 0.052 deg. At exactly FireSlopDeg degrees off, the + # degrees-vs-degrees predicate admits the shot; the radian mix-up rejects it. + const slop = 1.0'f64 + let err = gunAimErrorDeg(0.0, slop) # exactly `slop` degrees off + check("|err| <= FireSlopDeg admits the boundary", abs(err) <= slop, $err) + check("the radian mix-up would REJECT it", abs(err) > degToRad(slop), $err) + if failed > 0: echo "\n", failed, " check(s) FAILED" quit(1)