diff --git a/src/layers/rail/cockpitSubject.js b/src/layers/rail/cockpitSubject.js index 80cfc1daa6..286160d3aa 100644 --- a/src/layers/rail/cockpitSubject.js +++ b/src/layers/rail/cockpitSubject.js @@ -205,7 +205,10 @@ export function createRailCockpitSubject({ finiteOrNull(drawn?.lat) ?? finiteOrNull(sample?.lat) ?? record.lat; position.longitude = finiteOrNull(drawn?.lon) ?? finiteOrNull(sample?.lon) ?? record.lon; - position.railHeightM = Number.isFinite(drawn?.h) ? drawn.h - liftM : null; + // Quality 0 is the ellipsoid a train is drawn on while no floor is + // known: no height, so a view waits for the surface. + position.railHeightM = + Number.isFinite(drawn?.h) && drawn.q > 0 ? drawn.h - liftM : null; position.heightQuality = Number.isFinite(drawn?.q) ? drawn.q : 0; position.atMs = finiteOrNull(drawn?.at); diff --git a/src/layers/rail/cockpitSubject.test.mjs b/src/layers/rail/cockpitSubject.test.mjs index b439e1eecc..c8c764aad3 100644 --- a/src/layers/rail/cockpitSubject.test.mjs +++ b/src/layers/rail/cockpitSubject.test.mjs @@ -44,6 +44,13 @@ const PACK = Object.freeze({ routes: { R1: [0] }, }); +/** A floor service that knows the ground everywhere, at 12 m. */ +const WARM_GROUND = Object.freeze({ + GROUND_FLOOR_LIFT_M: 1.5, + cachedGroundFloor: () => 12, + warmGroundFloor() {}, +}); + /** Two trains on the line, T1 running east and T2 west, a step a poll. */ async function onTheLine(t, options = {}) { const app = harness(t, { @@ -76,7 +83,7 @@ const T1 = railTrainKey('mbta-rail', 'T1'); const T2 = railTrainKey('mbta-rail', 'T2'); test('the selected train reads as a snapshot of its place, motion and sources', async (t) => { - const app = await onTheLine(t); + const app = await onTheLine(t, { ground: WARM_GROUND }); assert.equal(app.layer.getSelectedTrain(), null, 'nothing selected'); app.parts().selection.selectTrain(T1); const view = app.layer.getSelectedTrain(); @@ -105,6 +112,7 @@ test('the selected train reads as a snapshot of its place, motion and sources', assert.equal(view.provenance.positionSource, 'gps'); assert.equal(view.provenance.timestampSource, 'vehicle'); const drawn = app.entry(T1).drawnHeight; + assert.ok(view.position.heightQuality > 0, 'on a known floor'); assert.equal(view.position.railHeightM, drawn.h - 1.5); assert.ok(Number.isFinite(view.position.latitude)); assert.ok(Array.isArray(view.stops)); @@ -129,6 +137,20 @@ const stopList = (...names) => state: 'upcoming', })); +test('a train drawn on an unknown floor reports no rail height', async (t) => { + // The harness's floor service is always cold: the train is drawn on the + // ellipsoid, which is no floor for a view to stand on. + const app = await onTheLine(t); + app.parts().selection.selectTrain(T1); + const view = app.layer.getSelectedTrain(); + assert.equal(view.position.heightQuality, 0, 'no floor is known'); + assert.equal( + view.position.railHeightM, + null, + 'an unknown floor is no height', + ); +}); + test('a snapshot read again while nothing changed is the one read before', async (t) => { let list = stopList('Alpha', 'Bravo', 'Charlie'); const app = await onTheLine(t, { diff --git a/src/layers/rail/pathBinding.js b/src/layers/rail/pathBinding.js index 9eba452d37..a432095b7a 100644 --- a/src/layers/rail/pathBinding.js +++ b/src/layers/rail/pathBinding.js @@ -47,8 +47,13 @@ export function pathWindowM(dtMs) { return window > PATH_WINDOW_MAX_M ? NaN : window; } -/** Arc moved between two reports before it says which way a train runs (m). */ -const DIRECTION_MIN_M = 1; +/** + * Arc moved between two reports before it says which way a train runs (m). + * A standing train's GPS jitter, a few metres back and forth, stays under it + * (the same parked-train jitter as FOLLOW_MIN_MOVE_M, policy.js) and keeps + * the direction learnt. + */ +const DIRECTION_MIN_M = 15; /** +1 or -1 for an arc step longer than DIRECTION_MIN_M, else `known`. */ function directionOf(step, known) { @@ -65,13 +70,13 @@ function createBinding(shapeIndex, path, via, s, fix) { /** * Which way the drawn train runs along the shape: +1 along it, -1 * against it, 0 until two reports or a playback segment tell. Kept until - * a rebind. Once playback has placed the train, only playback changes - * it: the newest reports run ahead of the drawn train, so a reversal - * they show turns it when playback reaches it. + * a rebind, and while the train stands. Once a playback segment has told + * it (`directionPlayed`), only playback changes it: the newest reports + * run ahead of the drawn train, so a reversal they show turns it when + * playback reaches it. */ direction: 0, - /** The direction the last placed playback segment ran, 0 before one. */ - drawDirection: 0, + directionPlayed: false, /** Arc and time of the newest report. */ fixS: s, fixT: fix.t, @@ -159,7 +164,7 @@ export function followFix(binding, fix) { pathWindowM(fix.t - binding.fixT), ); if (!hit || hit.offsetM > PATH_OFF_TRACK_M) return false; - if (!binding.drawDirection) + if (!binding.directionPlayed) binding.direction = directionOf(hit.s - binding.fixS, binding.direction); binding.fixS = hit.s; binding.fixT = fix.t; @@ -253,11 +258,8 @@ export function placeOnPath(binding, sample, segment, out) { cache.sTo = sTo; cache.ok = Number.isFinite(sFrom) && Number.isFinite(sTo); if (cache.ok) { - binding.drawDirection = directionOf( - sTo - sFrom, - binding.drawDirection || binding.direction || 0, - ); - binding.direction = binding.drawDirection; + binding.direction = directionOf(sTo - sFrom, binding.direction); + if (binding.direction) binding.directionPlayed = true; } } if (!cache.ok) return false; diff --git a/src/layers/rail/pathBinding.test.mjs b/src/layers/rail/pathBinding.test.mjs index 7b9e11c0cc..e257fb86da 100644 --- a/src/layers/rail/pathBinding.test.mjs +++ b/src/layers/rail/pathBinding.test.mjs @@ -209,8 +209,9 @@ test('a binding learns which way its train runs, and keeps it through a stop', ( assert.equal(along.direction, 0, 'one report tells no direction'); assert.ok(followFix(along, fix(10_000, 700, 0))); assert.equal(along.direction, 1); - // Standing still (under a metre) keeps what it learnt. - assert.ok(followFix(along, fix(20_000, 700.4, 0))); + // Standing at a stop, its reports jitter back a few metres: it keeps what + // it learnt. + assert.ok(followFix(along, fix(20_000, 690, 0))); assert.equal(along.direction, 1); const against = bindTrainPath(p, ids, fix(0, 1500, 0)); assert.ok(followFix(against, fix(10_000, 1200, 0))); @@ -262,3 +263,63 @@ test('a report that runs back turns the drawn train only when playback does', () ); assert.equal(binding.direction, -1); }); + +test('jitter segments played back at a standing train keep the direction it learnt', () => { + const p = pack(); + const ids = { tripId: 'full', routeId: 'main' }; + const binding = bindTrainPath(p, ids, fix(0, 1000, 0)); + assert.ok(followFix(binding, fix(15_000, 1600, 0))); + // Playback runs the train east, 1000 m -> 1600 m: direction +1. + const out = {}; + let seq = 0; + const play = (fromM, toM, t0) => { + const segment = { + from: { seq, ...fix(t0, fromM, 0) }, + to: { seq: seq + 1, ...fix(t0 + 15_000, toM, 0) }, + }; + assert.ok( + placeOnPath( + binding, + { fromSeq: seq, toSeq: seq + 1, fraction: 0.5 }, + segment, + out, + ), + ); + seq += 1; + return binding.direction; + }; + assert.equal(play(1000, 1600, 0), 1); + assert.equal( + binding.directionPlayed, + true, + 'playback owns the direction now', + ); + // At the platform: reports 3-5 m apart, back and forth, polled and played. + const learnt = []; + let t0 = 15_000; + for (const [fromM, toM] of [ + [1600, 1597], + [1597, 1600], + [1600, 1596], + [1596, 1601], + ]) { + assert.ok(followFix(binding, fix(t0 + 15_000, toM, 0))); + learnt.push(play(fromM, toM, t0)); + t0 += 15_000; + } + assert.deepEqual(learnt, [1, 1, 1, 1], 'from the played-back segments'); +}); + +test('GPS jitter in the newest reports before playback keeps the direction', () => { + const p = pack(); + const ids = { tripId: 'full', routeId: 'main' }; + const binding = bindTrainPath(p, ids, fix(0, 1000, 0)); + assert.ok(followFix(binding, fix(15_000, 1600, 0))); + assert.equal(binding.direction, 1); + const learnt = []; + [1597, 1600, 1596, 1601].forEach((eastM, i) => { + assert.ok(followFix(binding, fix(30_000 + i * 15_000, eastM, 0))); + learnt.push(binding.direction); + }); + assert.deepEqual(learnt, [1, 1, 1, 1], 'from the newest reports'); +});