18
0
mirror of https://github.com/MaSzyna-EU07/maszyna.git synced 2026-11-20 12:44:59 +01:00

AI: shunting brakes for a stop point again; test mode: no false SPAD for a train set across a signal (#409)

* fix(ai): shunting AI brakes for a stop point again, the braking curve doesn't plan from a brake table left at zero

The braking curve outside the final approach plans from the consist brake table since ce87aa0e (#187, merged
with #264) whenever the brake is pneumatic. That table is worked out only for train running (Obey_train, Bank);
in shunting it stays at zero, the planned full braking falls to its floor of 0.01 m/s2, and the demand comes out
at a few hundredths of a m/s2, above the shunting brake threshold of -0.2. A shunting engine then rolled on
without braking until the final approach, too late, and ran through the signal it was to stop at (seed
480873929 of l053_poranek: ep07p-2003 at sandomierz_g4_sem_info, sm42-1096, et22-654).

With no worked out table the curve is again the constant deceleration over the distance left, as it was before
ce87aa0e and as #187 meant to keep it for shunting. Train running is unchanged.

The curve moves out of TController into vehicle/brakingcurve.{h,cpp} so that it is unit tested:
tests/unit/test_braking_curve.cpp checks a shunting engine braking to a stop point with the zeroed table, and
that a train with a worked out table gets the same demand as before.

* test(ai): pin the train braking-curve demand to the results of the code before the move

A brake table that grips harder as the train slows and a gradient, so that a wrong gravity sign, table index or a dropped term in the shaped demand shows; the expected values were worked out with the code removed from TController::adjust_desired_speed_for_braking_curve.

* fix(test): a train set across a signal doesn't pass it at danger the moment the AI first looks at it

The speed table takes in events up to a train length behind the head, so a signal the train stands across at the start of a scenario is found already behind the head, becomes elapsed on its first update, and the test mode reported it as passed at danger (calkowo_lotos: st43-189 at mac_daleko3_sem_info, manewrowo3: st43-100 at suw_szopa_a02_sem_info, both at 13.6 s with the train at a stand, the signal 20 m behind the head). A point now counts as passed only when the head of the train had it ahead once; the blackbox record of passed signals follows the same rule.

* fix(test): a signal found up to 5 m behind the head still counts as seen ahead

The distance to an event is found in steps of a metre from the position of its memory cell, so a signal just ahead of the head (a train stopped short of it when the autopilot takes over, or set right at it at the start) could come out slightly behind it, and passing it would not be reported. The trains set across a signal that the previous commit is about stood 20 m past it.

Still not reported: a signal crossed while the speed table is being traced anew in motion (a Timetable: command to a running train, the autopilot taking over in motion), as it is found behind the head when the trace is made.

* docs(test): say what the seen-ahead tolerance gives up

A train set less than 5 m past a signal at stop is still reported to pass it at danger: standing that close it can't be told from a train stopped just short of the signal, which the tolerance is there for.

---------

Co-authored-by: maj00r <maj00r@host>
This commit is contained in:
maj00r
2026-11-19 04:55:33 +01:00
committed by GitHub
parent 80fd5aad2b
commit 267d5b3a8f
6 changed files with 366 additions and 86 deletions

View File

@@ -0,0 +1,195 @@
#include "stdafx.h"
#include "doctest.h"
#include "vehicle/brakingcurve.h"
#include <algorithm>
#include <array>
// the deceleration the AI asks for on its way down to a stop or a lower speed ahead, outside of the final approach
// (TController::adjust_desired_speed_for_braking_curve). in shunting the brake table of the consist isn't worked out
// and stays at zero; planned from it, the curve asked for next to no braking, and shunting engines ran through the
// signals they were to stop at.
namespace {
auto constexpr tablesize{20};
using brake_values = std::array<double, tablesize + 1>;
// what the AI holds in shunting: nothing worked out
struct zeroed_table {
brake_values a0{};
brake_values a1{};
braking_curve::brake_table table() const { return {a0.data(), a1.data(), tablesize, 100.0}; }
};
// a train brake that gives the same deceleration at every speed, full service 0.05 + 12 * 0.1 = 1.25 m/s2
struct flat_table {
brake_values a0{};
brake_values a1{};
flat_table() {
a0.fill(0.05);
a1.fill(0.1);
}
braking_curve::brake_table table() const { return {a0.data(), a1.data(), tablesize, 120.0}; }
};
braking_curve::approach stop_at(double const Speed, double const Distance) {
braking_curve::approach approach{};
approach.speed = Speed;
approach.target = 0.0;
approach.distance = Distance;
approach.offset = 0.0; // the braking point of a stop, outside of a train, is the stop itself (VelNext * multiplier)
approach.speedup_horizon = 30.0 / 3.6;
approach.fill_horizon = 30.0 / 3.6;
return approach;
}
// the AI in shunting leaves the brake alone for a demand weaker than this (TController::fAccThreshold)
auto constexpr shunting_brake_threshold{-0.2};
struct shunting_run {
double speed; // [km/h]
double left; // to the stop point [m]
bool braked_at_once;
};
// a shunting engine braking to a stop point the way the AI drives it: the brake follows the demand once it is past
// the threshold, the engine coasts otherwise. the braking curve gives way near the point to the final approach, at
// walking pace (10 km/h, 5 km/h in the last 10 m)
shunting_run brake_to_point(double const Speed, double const Distance, zeroed_table const &Table) {
auto const table{Table.table()};
auto const timestep{0.1};
shunting_run run{Speed, Distance, false};
run.braked_at_once = braking_curve::demand(stop_at(run.speed, run.left), &table) < shunting_brake_threshold;
while ((run.speed > (run.left > 10.0 ? 10.0 : 5.0)) && (run.left > -50.0)) {
auto const demand{braking_curve::demand(stop_at(run.speed, run.left), &table)};
auto const acceleration{demand < shunting_brake_threshold ? demand : 0.0};
run.speed = std::max(0.0, run.speed + 3.6 * acceleration * timestep);
run.left -= run.speed / 3.6 * timestep;
}
return run;
}
} // namespace
TEST_CASE("braking curve: a brake table left at zero isn't known, one worked out is") {
CHECK_FALSE(zeroed_table{}.table().known());
CHECK(flat_table{}.table().known());
}
TEST_CASE("braking curve: shunting to a point asks for the constant deceleration over the distance left") {
zeroed_table const shunting;
auto const table{shunting.table()};
auto approach{stop_at(33.0, 186.0)};
SUBCASE("coasting") { approach.acceleration = 0.0; }
SUBCASE("still speeding up") { approach.acceleration = 0.2; }
auto const demand{braking_curve::demand(approach, &table)};
CHECK(demand == doctest::Approx(-33.0 * 33.0 / (25.92 * 186.0 + 0.1)));
CHECK(demand == doctest::Approx(braking_curve::demand(approach, nullptr)));
// enough for the AI to put the brake on
CHECK(demand < shunting_brake_threshold);
}
TEST_CASE("braking curve: a shunting engine braking to a point gets down to walking pace short of it") {
zeroed_table const shunting;
auto speed{0.0};
auto distance{0.0};
SUBCASE("33 km/h, 186 m") {
speed = 33.0;
distance = 186.0;
}
SUBCASE("40 km/h, 186 m") {
speed = 40.0;
distance = 186.0;
}
SUBCASE("33 km/h, 120 m") {
speed = 33.0;
distance = 120.0;
}
SUBCASE("25 km/h, 60 m") {
speed = 25.0;
distance = 60.0;
}
CAPTURE(speed);
CAPTURE(distance);
auto const run{brake_to_point(speed, distance, shunting)};
CHECK(run.braked_at_once);
CHECK(run.left > 0.0);
CHECK(run.speed <= 10.0);
}
TEST_CASE("braking curve: a train with a worked out brake table plans along the shape of its brake") {
flat_table const train;
auto const table{train.table()};
auto approach{stop_at(80.0, 600.0)};
approach.offset = 20.0;
approach.fill_horizon = 6.0;
approach.speedup_horizon = 5.0;
SUBCASE("before the brake is applied, the room it takes to fill is kept") {
auto const room{580.0 - 80.0 / 3.6 * 6.0};
auto const demand{braking_curve::demand(approach, &table)};
CHECK(demand == doctest::Approx(braking_curve::shaped_demand(80.0, 0.0, room + 0.1 / 25.92, 0.0, table)));
// with the same deceleration at every speed the shape is the constant deceleration over that room
CHECK(demand == doctest::Approx(-(80.0 / 3.6) * (80.0 / 3.6) / (2.0 * room)).epsilon(0.01));
}
SUBCASE("once the brake is applied, the whole distance is used") {
approach.brake_applied = true;
auto const demand{braking_curve::demand(approach, &table)};
CHECK(demand == doctest::Approx(braking_curve::shaped_demand(80.0, 0.0, 580.0 + 0.1 / 25.92, 0.0, table)));
}
SUBCASE("still speeding up, it plans from the speed reached once the brake acts") {
approach.acceleration = 0.3;
auto const reached{80.0 + 3.6 * 5.0 * 0.3};
auto const distance{600.0 - (80.0 + reached) * 0.5 / 3.6 * 5.0};
auto const demand{braking_curve::demand(approach, &table)};
CHECK(demand == doctest::Approx(braking_curve::shaped_demand(reached, 0.0, distance - 20.0 + 0.1 / 25.92, 0.0, table)));
}
}
TEST_CASE("braking curve: a train gets the demand it got before the curve moved out of the AI") {
// a table that grips harder as the train slows, on a gradient; the demands were worked out with the
// code of TController::adjust_desired_speed_for_braking_curve before the move (origin/master 80fd5aad)
brake_values a0{};
brake_values a1{};
for (auto i{1}; i <= tablesize; ++i) {
a0[i] = 0.12 - 0.003 * i;
a1[i] = 0.06 - 0.001 * i;
}
braking_curve::brake_table const table{a0.data(), a1.data(), tablesize, 120.0};
REQUIRE(table.full_braking(80.0) == doctest::Approx(0.645));
REQUIRE(table.full_braking(10.0) == doctest::Approx(0.825));
auto approach{stop_at(80.0, 600.0)};
approach.offset = 20.0;
approach.gravity = 0.1;
approach.speedup_horizon = 5.0;
approach.fill_horizon = 6.0;
auto expected{0.0};
SUBCASE("downhill, coasting, the brake still to be applied") { expected = -0.4926656801067293; }
SUBCASE("downhill, the brake applied") {
approach.brake_applied = true;
expected = -0.37739905908238147;
}
SUBCASE("downhill, still speeding up") {
approach.acceleration = 0.3;
expected = -0.5330117415264249;
}
SUBCASE("uphill, down to 40 km/h") {
approach = stop_at(100.0, 900.0);
approach.target = 40.0;
approach.gravity = -0.08;
approach.speedup_horizon = 5.0;
approach.fill_horizon = 6.0;
expected = -0.40930710539221765;
}
CHECK(braking_curve::demand(approach, &table) == doctest::Approx(expected).epsilon(1e-7));
}