elm_gun: single aim computation, explicit deg/rad fire boundary + test
predictAim folded the learned correction into the bearing and derived x/y once instead of recomputing them per branch. New gunAimErrorDeg carries the degree unit in its name so the FireSlopDeg comparison cannot fall back to the old degrees-vs-radians mix-up; test_online.nim adds a regression block for it.
This commit is contained in:
@@ -313,14 +313,33 @@ proc predictAim*(g: ElmGun, b: GunBelief, power: float64 = FirePower,
|
|||||||
## off, so the two differ by exactly one thing.
|
## off, so the two differ by exactly one thing.
|
||||||
let base = heuristicAim(g, b, power)
|
let base = heuristicAim(g, b, power)
|
||||||
if base.isNaN: return GunAim(aim: NaN, confidence: 0.0)
|
if base.isNaN: return GunAim(aim: NaN, confidence: 0.0)
|
||||||
let d = hypot(b.ex - b.selfX, b.ey - b.selfY)
|
# One bearing, ONE coordinate computation: fold the learned term (when the
|
||||||
result = GunAim(aim: base, x: b.selfX + d * cos(base), y: b.selfY + d * sin(base),
|
# model is active and returns a number) into `aim` first, then derive x/y
|
||||||
confidence: 0.0)
|
# from the final bearing exactly once. The old version recomputed x/y for the
|
||||||
if not useElm or not g.loaded: return
|
# 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)
|
let corr = elmCorrection(g, b, power)
|
||||||
if corr.isNaN: return
|
if not corr.isNaN:
|
||||||
result.aim = base + corr
|
aim = base + corr
|
||||||
result.x = b.selfX + d * cos(result.aim)
|
|
||||||
result.y = b.selfY + d * sin(result.aim)
|
|
||||||
result.confidence = abs(corr)
|
result.confidence = abs(corr)
|
||||||
result.correction = corr
|
result.correction = corr
|
||||||
|
let d = hypot(b.ex - b.selfX, b.ey - b.selfY)
|
||||||
|
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
|
||||||
@@ -71,6 +71,23 @@ block:
|
|||||||
check("pending cleared", g.pending.len == 0)
|
check("pending cleared", g.pending.len == 0)
|
||||||
check("trace cleared", g.traceTick.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:
|
if failed > 0:
|
||||||
echo "\n", failed, " check(s) FAILED"
|
echo "\n", failed, " check(s) FAILED"
|
||||||
quit(1)
|
quit(1)
|
||||||
|
|||||||
Reference in New Issue
Block a user