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.
|
||||
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
|
||||
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
|
||||
Reference in New Issue
Block a user