Turn a train that backs away steadily, however small each step

The direction now follows how far a train has come back from the farthest
point it reached, so a slow reversal of under 15 m per report still turns it,
while GPS jitter around one spot does not. Rail policy takes the 15 m figure
from pathBinding instead of repeating it.
This commit is contained in:
German Khokhlov committed 2026-11-18 14:32:36 +04:00
1 parent ebf2686e2f
commit ee5a6ab317
4 files changed
+100 -15

No files matched your search

+3 -2
View File
@@ -2792,8 +2792,9 @@ the playback fraction), not the newest report's, and null while that floor is
unknown, also far above the ground, where such a vehicle is drawn on the
ellipsoid. Rail's `path.direction` is the way the drawn train runs: once
playback has placed it, a report that runs back turns it only when playback
reaches that report, and a step of at most 15 m between two reports (a standing
train's GPS jitter) keeps the direction it had. `docs/APPLICATION.md` (Cockpit subjects) shows a minimal provider. `src/ui/cockpitSubjectView.test.mjs`,
reaches that report. A train turns once it is 15 m back from the farthest point
it reached, however slowly it backs, so a standing train's GPS jitter keeps the
direction it had. `docs/APPLICATION.md` (Cockpit subjects) shows a minimal provider. `src/ui/cockpitSubjectView.test.mjs`,
`src/cockpitSubjectProviders.test.mjs`, `src/cockpitProgress.test.mjs`,
`src/layers/rail/cockpitSubject.test.mjs` and the Transit lifecycle tests
cover them.
+34 -12
View File
@@ -48,18 +48,35 @@ export function pathWindowM(dtMs) {
}
/**
* 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.
* Arc a train must move before that says which way it runs (m): a standing
* train's GPS jitter, back and forth, stays within it.
*/
const DIRECTION_MIN_M = 15;
export const DIRECTION_MIN_M = 15;
/** +1 or -1 for an arc step longer than DIRECTION_MIN_M, else `known`. */
function directionOf(step, known) {
return Number.isFinite(step) && Math.abs(step) > DIRECTION_MIN_M
? Math.sign(step)
: known;
/**
* Track one more arc position of a train. `turn.s` is the farthest arc it
* has reached the way it runs (where it was first seen while that is
* unknown); it turns once it is DIRECTION_MIN_M or more back from there,
* however small each step.
* @param {number} direction +1, -1 or 0.
* @param {{s: number}} turn Updated in place.
* @param {number} s
* @returns {number} The direction now.
*/
function steer(direction, turn, s) {
if (!Number.isFinite(s)) return direction;
if (!Number.isFinite(turn.s)) {
turn.s = s;
return direction;
}
const step = s - turn.s;
if (direction && step * direction >= 0) {
turn.s = s;
return direction;
}
if (Math.abs(step) < DIRECTION_MIN_M) return direction;
turn.s = s;
return Math.sign(step);
}
function createBinding(shapeIndex, path, via, s, fix) {
@@ -77,6 +94,9 @@ function createBinding(shapeIndex, path, via, s, fix) {
*/
direction: 0,
directionPlayed: false,
/** How far the reports and the played segments have run (steer). */
reportTurn: { s },
playTurn: { s: NaN },
/** Arc and time of the newest report. */
fixS: s,
fixT: fix.t,
@@ -165,7 +185,7 @@ export function followFix(binding, fix) {
);
if (!hit || hit.offsetM > PATH_OFF_TRACK_M) return false;
if (!binding.directionPlayed)
binding.direction = directionOf(hit.s - binding.fixS, binding.direction);
binding.direction = steer(binding.direction, binding.reportTurn, hit.s);
binding.fixS = hit.s;
binding.fixT = fix.t;
binding.fixLat = fix.lat;
@@ -258,7 +278,9 @@ export function placeOnPath(binding, sample, segment, out) {
cache.sTo = sTo;
cache.ok = Number.isFinite(sFrom) && Number.isFinite(sTo);
if (cache.ok) {
binding.direction = directionOf(sTo - sFrom, binding.direction);
const turn = binding.playTurn;
binding.direction = steer(binding.direction, turn, sFrom);
binding.direction = steer(binding.direction, turn, sTo);
if (binding.direction) binding.directionPlayed = true;
}
}
+61
View File
@@ -323,3 +323,64 @@ test('GPS jitter in the newest reports before playback keeps the direction', ()
});
assert.deepEqual(learnt, [1, 1, 1, 1], 'from the newest reports');
});
/** The direction after each played segment, `[fromM, toM]` 15 s apart. */
function playDirections(binding, steps) {
return steps.map(([fromM, toM], seq) => {
const { sample, segment } = playback(
seq,
seq + 1,
fix(seq * 15_000, fromM, 0),
fix((seq + 1) * 15_000, toM, 0),
0.5,
);
assert.ok(placeOnPath(binding, sample, segment, {}));
return binding.direction;
});
}
test('a train that moves under 15 m a report still tells its direction', () => {
const p = pack();
const ids = { tripId: 'full', routeId: 'main' };
// It sets off slowly, runs east, then backs out 10 m a report.
const reports = bindTrainPath(p, ids, fix(0, 1000, 0));
const learnt = [1010, 1020, 1600, 1590, 1580, 1570].map((eastM, i) => {
assert.ok(followFix(reports, fix((i + 1) * 15_000, eastM, 0)));
return reports.direction;
});
assert.deepEqual(learnt, [0, 1, 1, 1, -1, -1], 'from the newest reports');
const played = bindTrainPath(p, ids, fix(0, 1000, 0));
assert.deepEqual(
playDirections(played, [
[1000, 1600],
[1600, 1590],
[1590, 1580],
[1580, 1570],
]),
[1, 1, -1, -1],
'from the played-back segments',
);
});
test('GPS jitter of up to 14 m at a standing train keeps its direction', () => {
const p = pack();
const ids = { tripId: 'full', routeId: 'main' };
const reports = bindTrainPath(p, ids, fix(0, 1000, 0));
const learnt = [1600, 1588, 1600, 1586, 1599, 1587].map((eastM, i) => {
assert.ok(followFix(reports, fix((i + 1) * 15_000, eastM, 0)));
return reports.direction;
});
assert.deepEqual(learnt, [1, 1, 1, 1, 1, 1], 'from the newest reports');
const played = bindTrainPath(p, ids, fix(0, 1000, 0));
assert.deepEqual(
playDirections(played, [
[1000, 1600],
[1600, 1588],
[1588, 1600],
[1600, 1586],
[1586, 1599],
]),
[1, 1, 1, 1, 1],
'from the played-back segments',
);
});
+2 -1
View File
@@ -16,6 +16,7 @@ import {
import { displayMotion } from '../transit/movement.js';
import { FIX_FLAGS, readFix } from '../../data/contactPlayback.js';
import { roundHeadingDegrees } from '../../data/headingDegrees.js';
import { DIRECTION_MIN_M } from './pathBinding.js';
import {
stopClock,
stopLateMinutes,
@@ -295,7 +296,7 @@ export function playbackModeFor(railMode) {
}
/** Two reports closer than this are one place: a parked train's jitter (m). */
export const FOLLOW_MIN_MOVE_M = 15;
export const FOLLOW_MIN_MOVE_M = DIRECTION_MIN_M;
const lastFix = {};
const priorFix = {};