18
0
mirror of https://github.com/MaSzyna-EU07/maszyna.git synced 2026-11-27 21:45:16 +01:00
Files
maszyna/tests/unit/test_braking_curve.cpp
jerrrrycho d3e6736f78 fix(ai): the braking margin ends once the independent brake is on (#479)
* fix(ai): the braking margin ends once the independent brake is on

The braking curve outside the final approach keeps the room the brake
takes to fill only while the brake is still to be applied. It took the
train brake handle past running as the only sign that the brake was on.
A vehicle that runs alone, a light locomotive or a railcar such as the
SN61, brakes with its independent brake and leaves the handle in
running, so the margin never ended for it: on every look it planned to
stop short by the distance the brake takes to fill, slowed down too
early, released, braked again, and crawled the last tens of metres to a
W4 or a signal at stop. This followed #264 and #297.

The independent brake now counts as well (braking_curve::brake_acting,
LocalBrakePosA above 0). On td.scn the light EU07 reaches the W4 of
Przystanek 6.6 s sooner and covers the last 100 m in 20.3 s instead of
26.3 s; on a 20 per mille downgrade it reaches a signal at stop 11.7 s
sooner (last 100 m in 20.6 s instead of 31.7 s), with 3 applications of
the independent brake in the last 300 m instead of 5. A train braking
with the train brake runs as before (its physicslog is the same byte
for byte).

* test(scenarios): light locomotive, railcar and train stopping down a 20 per mille gradient

The *_td_downgrade_w4_signal cases run on a line the fragment adds
beside td.scn: 300 m level, then 3000 m down a gradient of 20 per mille
under a wire fed by a substation of its own. The train stops at the W4
of Dol 1500 m down the gradient and then before a signal at stop 1100 m
further down. They run with a light EU07 and an SN61 alone, which brake
with the independent brake, and with an EU07 and three coaches, which
brakes with the train brake. eu07_coaches_td_timetable is that train on
the td/rozklad timetable to Przystanek.

They show the approach to a stop: before the previous commit the light
EU07 needed 31.7 s for the last 100 m to the signal, now 20.6 s; the
train with coaches runs the same in both.

* fix(ai): the braking margin counts the independent brake only on a vehicle that has one

* test(scenarios): the light EU07 and the SN61 stop at the signal down the gradient in time
2026-11-25 05:15:02 +01:00

231 lines
8.8 KiB
C++

#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: the train brake handle or the independent brake counts as braking") {
CHECK_FALSE(braking_curve::brake_acting(0.0, 0.0));
// the handle in its release or filling positions is below running
CHECK_FALSE(braking_curve::brake_acting(-1.0, 0.0));
CHECK(braking_curve::brake_acting(1.0, 0.0));
CHECK(braking_curve::brake_acting(0.5, 0.0));
// a light locomotive or a railcar alone brakes with the independent brake, the handle stays in running
CHECK(braking_curve::brake_acting(0.0, 0.25));
CHECK(braking_curve::brake_acting(-1.0, 1.0));
CHECK(braking_curve::brake_acting(2.0, 0.5));
}
TEST_CASE("braking curve: a vehicle braking with its independent brake uses the whole distance, as with the train brake") {
// before, only the train brake handle counted: a light locomotive that had put its independent brake on still
// planned, on every look, to stop short by the distance the brake takes to fill, so it slowed down too early
// and crawled the last tens of metres to the stop
flat_table const light;
auto const table{light.table()};
auto approach{stop_at(60.0, 300.0)};
approach.offset = 20.0;
approach.fill_horizon = 8.0;
approach.brake_applied = braking_curve::brake_acting(0.0, 0.3);
auto const demand{braking_curve::demand(approach, &table)};
CHECK(demand == doctest::Approx(braking_curve::shaped_demand(60.0, 0.0, 280.0 + 0.1 / 25.92, 0.0, table)));
auto trainbrake{approach};
trainbrake.brake_applied = braking_curve::brake_acting(1.0, 0.0);
CHECK(demand == doctest::Approx(braking_curve::demand(trainbrake, &table)));
auto released{approach};
released.brake_applied = braking_curve::brake_acting(0.0, 0.0);
// with no brake on, the room to fill is kept, and more is asked for
CHECK(braking_curve::demand(released, &table) < demand);
}
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));
}