18
0
mirror of https://github.com/MaSzyna-EU07/maszyna.git synced 2026-12-01 22:07:55 +01:00
Files
maszyna/tests/unit/test_braking_curve.cpp
T
jerrrrycho b4b342c523 fix(ai): down a gradient a train at walking pace in the last metres before its stop stops there
In the final approach the braking curve asks for next to nothing once the train is down to walking pace a
metre or two short of the point it aims at, or past it. Down a gradient the pneumatic brake let off for that
lets the gradient take the train again: it crept on at about 1 km/h and braked again, a release and an
application at a time. EU07 alone down 20 per mille to the signal gr_sem (eu07_td_downgrade_w4_signal) did
it three times and stood 12 s later than it had stopped. Below 5 km/h within 2 m of that point, or past it,
on a descent, the AI now stops where it is (the desired speed 0), as it does once it stands there.
2026-11-29 02:19:43 +01:00

247 lines
9.9 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));
}
TEST_CASE("braking curve: down a gradient a train at walking pace in the last metres before its stop stops there") {
// EU07 alone down 20 per mille to the signal gr_sem (eu07_td_downgrade_w4_signal): 3.5 km/h 1.3 m short of the
// middle of the stopping window, the curve asked for next to no braking; the independent brake let off for it, the
// gradient took the locomotive again, and it crept on and braked again three times, for some 12 s
auto constexpr descent{0.196}; // [m/s2] 20 per mille
CHECK(braking_curve::stops_where_it_is(3.5, 1.3, descent));
// past the point it aims at, it doesn't creep on half a metre at a time either
CHECK(braking_curve::stops_where_it_is(0.2, -1.2, descent));
// faster, or further out, it follows the curve down to the point
CHECK_FALSE(braking_curve::stops_where_it_is(5.8, 2.6, descent));
CHECK_FALSE(braking_curve::stops_where_it_is(3.5, 10.0, descent));
// on the flat and uphill the gradient doesn't take the train when the brake lets off
CHECK_FALSE(braking_curve::stops_where_it_is(3.5, 1.3, 0.0));
CHECK_FALSE(braking_curve::stops_where_it_is(3.5, 1.3, -0.196));
}