18
0
mirror of https://github.com/MaSzyna-EU07/maszyna.git synced 2026-11-20 22:04: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

@@ -554,6 +554,7 @@ set(CORE_SOURCES
"utilities/physicslog.cpp" "utilities/physicslog.cpp"
"vehicle/enginestart.cpp" "vehicle/enginestart.cpp"
"vehicle/brakesetting.cpp" "vehicle/brakesetting.cpp"
"vehicle/brakingcurve.cpp"
"vehicle/handover.cpp" "vehicle/handover.cpp"
"vehicle/horn.cpp" "vehicle/horn.cpp"
"vehicle/linebreaker.cpp" "vehicle/linebreaker.cpp"

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));
}

View File

@@ -21,6 +21,7 @@ http://mozilla.org/MPL/2.0/.
#include "utilities/physicslog.h" #include "utilities/physicslog.h"
#include "vehicle/Train.h" #include "vehicle/Train.h"
#include "vehicle/brakesetting.h" #include "vehicle/brakesetting.h"
#include "vehicle/brakingcurve.h"
#include "vehicle/handover.h" #include "vehicle/handover.h"
#include "world/mtable.h" #include "world/mtable.h"
#include "vehicle/DynObj.h" #include "vehicle/DynObj.h"
@@ -210,6 +211,7 @@ void TSpeedPos::Clear()
fSectionVelocityDist = 0.0; //brak długości fSectionVelocityDist = 0.0; //brak długości
fPointDist = 0.0; fPointDist = 0.0;
fDist = 0.0; fDist = 0.0;
bSeenAhead = false;
vPos = glm::dvec3(0, 0, 0); vPos = glm::dvec3(0, 0, 0);
trTrack = nullptr; // brak wskaźnika trTrack = nullptr; // brak wskaźnika
}; };
@@ -326,6 +328,9 @@ bool TSpeedPos::Update()
// trzeba zazanaczyć, że minięty // trzeba zazanaczyć, że minięty
iFlags |= spElapsed; iFlags |= spElapsed;
} }
else {
bSeenAhead = true;
}
if (iFlags & spTrack) { if (iFlags & spTrack) {
// road/track // road/track
if (trTrack) { if (trTrack) {
@@ -411,9 +416,18 @@ bool TSpeedPos::IsProperSemaphor(TOrders order)
return false; // true gdy zatrzymanie, wtedy nie ma po co skanować dalej return false; // true gdy zatrzymanie, wtedy nie ma po co skanować dalej
} }
// how far behind the head an event found in the scan may lie and still count as one the head had ahead [m]
constexpr double SeenAheadTolerance { 5.0 };
bool TSpeedPos::Set(basic_event *event, double dist, double length, TOrders order) bool TSpeedPos::Set(basic_event *event, double dist, double length, TOrders order)
{ // zapamiętanie zdarzenia { // zapamiętanie zdarzenia
fDist = dist; fDist = dist;
// an event already behind the head when found (under a train set across a signal) wasn't passed by it. the
// distance is found in steps of a metre from the position of the memory cell, so a signal just ahead of the head
// may come out a few metres behind it: those still count as seen ahead. the price is that a train set less than
// the tolerance past a signal at stop is still reported to pass it; a train standing that close to a signal can't
// be told from one stopped just short of it, which really passes the signal if it goes on
bSeenAhead = ( dist >= -SeenAheadTolerance );
iFlags = spEvent; iFlags = spEvent;
evEvent = event; evEvent = event;
vPos = event->input_location(); // współrzędne eventu albo komórki pamięci (zrzutować na tor?) vPos = event->input_location(); // współrzędne eventu albo komórki pamięci (zrzutować na tor?)
@@ -908,6 +922,7 @@ void TController::CheckSignalPassed( TSpeedPos const &Point, bool const Passedbe
{ {
if( Passedbefore if( Passedbefore
|| false == TestFlag( Point.iFlags, spElapsed ) || false == TestFlag( Point.iFlags, spElapsed )
|| false == Point.bSeenAhead // found already behind the head: the train stood across it, it didn't pass it
|| ( mvOccupied->CategoryFlag & 1 ) == 0 ) { // railway only || ( mvOccupied->CategoryFlag & 1 ) == 0 ) { // railway only
return; return;
} }
@@ -8189,54 +8204,6 @@ uphill_coasting_speed( double const Distance, double const Gravity ) {
return 3.6 * std::sqrt( 2.0 * std::max( 0.01, -Gravity ) * std::max( 0.0, Distance ) ); return 3.6 * std::sqrt( 2.0 * std::max( 0.01, -Gravity ) * std::max( 0.0, Distance ) );
} }
// resultant acceleration [m/s2] to ask for now so that a steady application of the pneumatic brake takes the train
// from Velocity down to Target [km/h] over Distance [m], with the brake force following the speed the way the
// consist brake table says it does (cast iron blocks grip harder as the train slows)
struct consist_brake_table {
double const *a0; // deceleration at handle position 1, by speed
double const *a1; // deceleration added by each quarter of a position, by speed
int size; // entries, up to the vehicle's top speed
double vmax; // the vehicle's top speed [km/h]
// deceleration [m/s2] of a full service application at Kmh
double full_braking( double const Kmh ) const {
auto const index { std::clamp( static_cast<int>( size * Kmh / std::max( 1.0, vmax ) ), 1, size ) };
return std::max( 0.01, a0[ index ] + 12.0 * a1[ index ] ); }
};
static double
shaped_braking_demand(
double const Velocity, double const Target, double const Distance, double const Gravity,
consist_brake_table const &Table ) {
auto const fullbraking = [&Table]( double const Kmh ) {
return Table.full_braking( Kmh ); };
auto const travel = [Velocity, Target, Gravity, &fullbraking]( double const Ratio ) {
auto const steps { 24 };
auto const step { ( Velocity - Target ) / steps };
auto distance { 0.0 };
for( auto i { 0 }; i < steps; ++i ) {
auto const kmh { Target + ( i + 0.5 ) * step };
auto const deceleration { Ratio * fullbraking( kmh ) - Gravity };
if( deceleration <= 0.001 ) {
return std::numeric_limits<double>::max();
}
distance += ( kmh / 3.6 ) * ( step / 3.6 ) / deceleration;
}
return distance; };
auto low { 0.0 };
auto high { 3.0 };
if( travel( high ) > Distance ) {
return Gravity - high * fullbraking( Velocity );
}
for( auto i { 0 }; i < 30; ++i ) {
auto const middle { 0.5 * ( low + high ) };
( travel( middle ) > Distance ? low : high ) = middle;
}
return Gravity - high * fullbraking( Velocity );
}
// outside of max safe range: follow the braking curve down to the speed ahead, Vel is the present speed in the // outside of max safe range: follow the braking curve down to the speed ahead, Vel is the present speed in the
// direction of travel // direction of travel
void void
@@ -8263,43 +8230,19 @@ TController::adjust_desired_speed_for_braking_curve( double const Vel ) {
( VelNext == 0.0 ) && is_train() && ( OrderCurrentGet() & Obey_train ) ? ( VelNext == 0.0 ) && is_train() && ( OrderCurrentGet() & Obey_train ) ?
stopping_margin() : // the same stopping point as the final approach stopping_margin() : // the same stopping point as the final approach
VelNext * braking_distance_multiplier( static_cast<float>( VelNext ) ) ); VelNext * braking_distance_multiplier( static_cast<float>( VelNext ) ) );
// a pneumatic brake acts on the state the train will be in once it has responded; while the // both plans of a pneumatic brake are made from the brake table of the consist; with none (in shunting it isn't
// train is still speeding up, plan the deceleration from there, so more is asked for at once // worked out) the braking curve is the constant deceleration over the distance left, as before them
auto velocity { Vel }; braking_curve::brake_table const braketable { fBrake_a0, fBrake_a1, BrakeAccTableSize, mvOccupied->Vmax };
auto distance { ActualProximityDist }; // making for a passenger stop only the time the brake takes to fill: the look-ahead the horizon
if( ( BrakeSystem == TBrakeSystem::Pneumatic ) // keeps as its floor was set for the stops at a signal, and a vehicle that brakes with its
&& ( AbsAccS > 0.0 ) ) { // independent brake, which leaves the train brake handle where it is, kept braking earlier
auto const horizon { brake_response_horizon( mvOccupied, BrakeSystem, AbsAccS, fBrake_a0[ 0 ] ) }; // and earlier the whole way in, to crawl the last tens of metres to the stop point
velocity = Vel + 3.6 * horizon * AbsAccS; auto const fillbrakea0 { bStopTargetIsPassengerStop ? 1.0 : fBrake_a0[ 0 ] };
distance = std::max( 0.5 * ActualProximityDist, ActualProximityDist - ( Vel + velocity ) * 0.5 / 3.6 * horizon ); braking_curve::approach const approach {
} Vel, VelNext, ActualProximityDist, brakingpointoffset, fAccGravity, AbsAccS, BrakeCtrlPosition > 0.0,
auto const brakingroom { brake_response_horizon( mvOccupied, BrakeSystem, AbsAccS, fBrake_a0[ 0 ] ),
std::max( brake_response_horizon( mvOccupied, BrakeSystem, 1.0, fillbrakea0 ) };
distance - brakingpointoffset, auto const deceleration { braking_curve::demand( approach, BrakeSystem == TBrakeSystem::Pneumatic ? &braketable : nullptr ) };
std::min(
distance,
brakingpointoffset ) ) };
auto deceleration { ( VelNext * VelNext - velocity * velocity ) / ( 25.92 * brakingroom + 0.1 ) };
if( ( BrakeSystem == TBrakeSystem::Pneumatic ) && ( velocity > VelNext ) ) {
// follow the shape of the consist brake force over speed rather than assume a constant
// deceleration, over the distance left once the brake has had time to fill (while the
// train is still speeding up, the prediction above has taken that off already)
auto room { brakingroom };
if( ( AbsAccS <= 0.0 )
&& ( BrakeCtrlPosition <= 0.0 ) ) { // only while the brake is still to be applied: once it acts, the room it needs to respond is behind
// making for a passenger stop only the time the brake takes to fill: the look-ahead the horizon
// keeps as its floor was set for the stops at a signal, and a vehicle that brakes with its
// independent brake, which leaves the train brake handle where it is, kept braking earlier
// and earlier the whole way in, to crawl the last tens of metres to the stop point
auto const brakea0 { bStopTargetIsPassengerStop ? 1.0 : fBrake_a0[ 0 ] };
room = std::max(
0.5 * brakingroom,
brakingroom - Vel / 3.6 * brake_response_horizon( mvOccupied, BrakeSystem, 1.0, brakea0 ) );
}
deceleration = shaped_braking_demand(
velocity, VelNext, room + 0.1 / 25.92, fAccGravity,
{ fBrake_a0, fBrake_a1, BrakeAccTableSize, mvOccupied->Vmax } );
}
AccDesired = std::min( AccDesired, deceleration ); // najpierw hamuje mocniej, potem zluzuje AccDesired = std::min( AccDesired, deceleration ); // najpierw hamuje mocniej, potem zluzuje
if( Vel <= VelNext ) { if( Vel <= VelNext ) {
return; return;
@@ -8350,7 +8293,7 @@ TController::adjust_desired_speed_for_final_approach( double const Vel ) {
AccDesired = ( AccDesired = (
BrakeSystem == TBrakeSystem::Pneumatic ? BrakeSystem == TBrakeSystem::Pneumatic ?
// cast iron blocks grip harder as the train comes to a stand // cast iron blocks grip harder as the train comes to a stand
shaped_braking_demand( braking_curve::shaped_demand(
Vel, VelNext, stoppingroom, fAccGravity, Vel, VelNext, stoppingroom, fAccGravity,
{ fBrake_a0, fBrake_a1, BrakeAccTableSize, mvOccupied->Vmax } ) : { fBrake_a0, fBrake_a1, BrakeAccTableSize, mvOccupied->Vmax } ) :
( VelNext * VelNext - Vel * Vel ) / ( 25.92 * stoppingroom ) ); ( VelNext * VelNext - Vel * Vel ) / ( 25.92 * stoppingroom ) );

View File

@@ -164,6 +164,7 @@ class TSpeedPos
int iFlags{ spNone }; // flagi typu wpisu do tabelki int iFlags{ spNone }; // flagi typu wpisu do tabelki
bool bMoved{ false }; // czy przesunięty (dotyczy punktu zatrzymania w peronie) bool bMoved{ false }; // czy przesunięty (dotyczy punktu zatrzymania w peronie)
double fMoved{ 0.0 }; // ile przesunięty (dotyczy punktu zatrzymania w peronie) double fMoved{ 0.0 }; // ile przesunięty (dotyczy punktu zatrzymania w peronie)
bool bSeenAhead{ false }; // the head of the train had it ahead once: only such a point can be passed by the train
glm::dvec3 vPos; // współrzędne XYZ do liczenia odległości glm::dvec3 vPos; // współrzędne XYZ do liczenia odległości
struct struct
{ {

86
vehicle/brakingcurve.cpp Normal file
View File

@@ -0,0 +1,86 @@
/*
This Source Code Form is subject to the
terms of the Mozilla Public License, v.
2.0. If a copy of the MPL was not
distributed with this file, You can
obtain one at
http://mozilla.org/MPL/2.0/.
*/
#include "stdafx.h"
#include "vehicle/brakingcurve.h"
#include <algorithm>
#include <limits>
namespace braking_curve {
double brake_table::full_braking(double const Kmh) const {
auto const index{std::clamp(static_cast<int>(size * Kmh / std::max(1.0, vmax)), 1, size)};
return std::max(0.01, a0[index] + 12.0 * a1[index]);
}
bool brake_table::known() const {
// a worked out table holds at least the rolling resistance term at every speed
return a0[size] > 0.0;
}
double shaped_demand(double const Velocity, double const Target, double const Distance, double const Gravity, brake_table const &Table) {
auto const travel = [Velocity, Target, Gravity, &Table](double const Ratio) {
auto const steps{24};
auto const step{(Velocity - Target) / steps};
auto distance{0.0};
for (auto i{0}; i < steps; ++i) {
auto const kmh{Target + (i + 0.5) * step};
auto const deceleration{Ratio * Table.full_braking(kmh) - Gravity};
if (deceleration <= 0.001) {
return std::numeric_limits<double>::max();
}
distance += (kmh / 3.6) * (step / 3.6) / deceleration;
}
return distance;
};
auto low{0.0};
auto high{3.0};
if (travel(high) > Distance) {
return Gravity - high * Table.full_braking(Velocity);
}
for (auto i{0}; i < 30; ++i) {
auto const middle{0.5 * (low + high)};
(travel(middle) > Distance ? low : high) = middle;
}
return Gravity - high * Table.full_braking(Velocity);
}
double demand(approach const &Approach, brake_table const *Pneumatictable) {
// a pneumatic brake acts on the state the train will be in once it has responded; while the
// train is still speeding up, plan the deceleration from there, so more is asked for at once
// both plans are made from the brake table of the consist; with none (in shunting it isn't worked out, and planning
// from its zeroes would ask for no braking at all) the braking curve is the constant deceleration over the distance left
auto const *table{(Pneumatictable != nullptr) && Pneumatictable->known() ? Pneumatictable : nullptr};
auto velocity{Approach.speed};
auto distance{Approach.distance};
if ((table != nullptr) && (Approach.acceleration > 0.0)) {
auto const horizon{Approach.speedup_horizon};
velocity = Approach.speed + 3.6 * horizon * Approach.acceleration;
distance = std::max(0.5 * Approach.distance, Approach.distance - (Approach.speed + velocity) * 0.5 / 3.6 * horizon);
}
auto const brakingroom{std::max(distance - Approach.offset, std::min(distance, Approach.offset))};
if ((table == nullptr) || (velocity <= Approach.target)) {
return (Approach.target * Approach.target - velocity * velocity) / (25.92 * brakingroom + 0.1);
}
// follow the shape of the consist brake force over speed rather than assume a constant
// deceleration, over the distance left once the brake has had time to fill (while the
// train is still speeding up, the prediction above has taken that off already)
auto room{brakingroom};
if ((Approach.acceleration <= 0.0) && (false == Approach.brake_applied)) {
// only while the brake is still to be applied: once it acts, the room it needs to respond is behind
room = std::max(0.5 * brakingroom, brakingroom - Approach.speed / 3.6 * Approach.fill_horizon);
}
return shaped_demand(velocity, Approach.target, room + 0.1 / 25.92, Approach.gravity, *table);
}
} // namespace braking_curve

54
vehicle/brakingcurve.h Normal file
View File

@@ -0,0 +1,54 @@
/*
This Source Code Form is subject to the
terms of the Mozilla Public License, v.
2.0. If a copy of the MPL was not
distributed with this file, You can
obtain one at
http://mozilla.org/MPL/2.0/.
*/
#pragma once
// the deceleration the AI asks for on its way down to a lower speed or a stop ahead, outside of the final
// approach (TController::adjust_desired_speed_for_braking_curve)
namespace braking_curve {
// the deceleration table of the consist brake, by speed (TController::fBrake_a0, fBrake_a1). it is worked out
// for a train only (Obey_train, Bank); in shunting it stays at zero
struct brake_table {
double const *a0; // deceleration at handle position 1, by speed; a0[1..size]
double const *a1; // deceleration added by each quarter of a position, by speed
int size; // entries, up to the vehicle's top speed
double vmax; // the vehicle's top speed [km/h]
// deceleration [m/s2] of a full service application at Kmh
double full_braking(double Kmh) const;
// whether the table is worked out
bool known() const;
};
// resultant acceleration [m/s2] to ask for now so that a steady application of the pneumatic brake takes the train
// from Velocity down to Target [km/h] over Distance [m], with the brake force following the speed the way the
// table says it does (cast iron blocks grip harder as the train slows)
double shaped_demand(double Velocity, double Target, double Distance, double Gravity, brake_table const &Table);
// where the train is on its way down to Target
struct approach {
double speed; // present speed in the direction of travel [km/h]
double target; // speed to get down to [km/h]
double distance; // to where the speed is to be down [m]
double offset; // the braking point that far short of it [m]
double gravity; // tangential acceleration of the gradient [m/s2], negative uphill
double acceleration; // present acceleration of the train [m/s2]
bool brake_applied; // the train brake handle is past running
double speedup_horizon; // how long the present acceleration lasts while speeding up [s]
double fill_horizon; // how long the brake takes to fill once applied [s]
};
// acceleration [m/s2] to ask for now, negative to brake. with the worked out table of a pneumatic brake
// (Pneumatictable) the demand follows the brake: planned from the state the train is in once the brake has
// responded, along the shape of its force over speed. without one (nullptr for another brake, or a table left at
// zero, as in shunting) it is the constant deceleration over the distance left
double demand(approach const &Approach, brake_table const *Pneumatictable);
} // namespace braking_curve