From 7c12b83ba470a83a391e61b4e654f2af29daa535 Mon Sep 17 00:00:00 2001
From: pawelw
Date: Sun, 22 Nov 2026 12:27:18 +0100
Subject: [PATCH] fix(ai): an uphill start keeps the power on until the train
brake is off (#396)
* fix(ai): an uphill start keeps the power on until the train brake is off
The uphill start of #265 ended as soon as the power held the train (or the head of the
train moved back 0.5 km/h). From then on the brake still on in the wagons made Ready false,
the power went off, the train rolled back, was braked again and the start began anew; only
the cylinders falling below 0.8 bar let the train get away.
- Once the power holds the train, the start goes on while the train brake is released:
Ready holds, the power stays on, bounded to 1.5 x the grade force. It ends when the
consist is released (the same test as Ready, without the hold) or after 60 s.
- A start is given up for rolling back on the mass-weighted speed of the consist, not on
the speed of the locomotive, which runs back into the slack of the train held by its brake.
- A train slipping back uphill is not released by the rule for a train to go on.
* fix(ai): an uphill start begins only with the whole train at a stand
* fix(ai): an uphill start applies the train brake further while it doesn't hold the train without the locomotive brake
* fix(ai): the speed and the tractive force of an uphill start in the direction of travel also from cab 2
* fix(ai): an uphill start tops up the train brake a full application time a step, at most to full service, and keeps the power between 1.1 and 1.5 times the grade force while the brake releases
* fix(ai): an uphill start no longer takes a notch back in the release phase: the notch below doesn't hold the train
* refactor(ai): the train brake top-up of an uphill start in uphill_start::brake_top_up(), with unit tests
---
CMakeLists.txt | 1 +
tests/unit/test_uphill_start.cpp | 103 +++++++++++++++++++++
vehicle/Driver.cpp | 150 ++++++++++++++++++++++++-------
vehicle/Driver.h | 12 +++
vehicle/uphillstart.cpp | 36 ++++++++
vehicle/uphillstart.h | 41 +++++++++
6 files changed, 312 insertions(+), 31 deletions(-)
create mode 100644 tests/unit/test_uphill_start.cpp
create mode 100644 vehicle/uphillstart.cpp
create mode 100644 vehicle/uphillstart.h
diff --git a/CMakeLists.txt b/CMakeLists.txt
index e563e587f..69dc5fbab 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -557,6 +557,7 @@ set(CORE_SOURCES
"vehicle/brakesetting.cpp"
"vehicle/brakingcurve.cpp"
"vehicle/handover.cpp"
+"vehicle/uphillstart.cpp"
"vehicle/horn.cpp"
"vehicle/linebreaker.cpp"
"vehicle/smoke.cpp"
diff --git a/tests/unit/test_uphill_start.cpp b/tests/unit/test_uphill_start.cpp
new file mode 100644
index 000000000..d450af68b
--- /dev/null
+++ b/tests/unit/test_uphill_start.cpp
@@ -0,0 +1,103 @@
+#include "stdafx.h"
+#include "doctest.h"
+
+#include "vehicle/uphillstart.h"
+
+// starting uphill, the train brake is applied further, a step at a time, until the brake of the unpowered vehicles
+// holds the train without the locomotive brake (eu07/maszyna#396). the numbers are those of an ST43 with 37 empty
+// wagons on 12.5 per mille: 1040 t, 128.6 kN of grade force, the wagons on G with about 22 s to apply
+
+using uphill_start::brake_top_up;
+using uphill_start::top_up;
+using uphill_start::top_up_action;
+
+namespace {
+
+double const grade{128600.0}; // [N]
+double const application{22.0}; // [s]
+
+// the train standing with the brake handle at Position, held there long enough for the cylinders to fill, while
+// each step adds Gain of brake force (as a multiple of the grade force) to the Start it had. runs the top-up until it
+// lets the locomotive brake off, and returns the handle position it stopped at and the number of steps
+struct outcome {
+ double position;
+ int steps;
+};
+outcome run_top_up(double Start, double Gain, double Position = 1.25) {
+ top_up state;
+ auto force{Start * grade};
+ auto steps{0};
+ for (auto guard{0}; guard < 100; ++guard) {
+ auto const action{brake_top_up(state, force, grade, Position, application, application)};
+ if (action == top_up_action::none) {
+ break;
+ }
+ REQUIRE(action == top_up_action::step);
+ Position += 0.25;
+ force += Gain * grade;
+ ++steps;
+ }
+ return {Position, steps};
+}
+
+} // namespace
+
+TEST_CASE("uphill start: a brake that holds the train isn't applied further") {
+ top_up state;
+ CHECK(brake_top_up(state, 1.1 * grade, grade, 1.25, application, application) == top_up_action::none);
+ CHECK(state.last_force < 0.0);
+}
+
+TEST_CASE("uphill start: each step waits the full application time of the brake") {
+ top_up state;
+ CHECK(brake_top_up(state, 0.9 * grade, grade, 1.25, 0.5 * application, application) == top_up_action::wait);
+ CHECK(brake_top_up(state, 0.9 * grade, grade, 1.25, application, application) == top_up_action::step);
+ // the handle has just moved
+ CHECK(brake_top_up(state, 0.95 * grade, grade, 1.5, 0.0, application) == top_up_action::wait);
+ CHECK(brake_top_up(state, 0.95 * grade, grade, 1.5, application - 0.1, application) == top_up_action::wait);
+}
+
+TEST_CASE("uphill start: a light application is topped up until the brake holds the train") {
+ // before Nowy Zagorz on linia_107_objazdy: 0.64 bar in the wagons, 128 kN against 129 kN
+ auto const result{run_top_up(0.995, 0.15)};
+ CHECK(result.steps == 1);
+ CHECK(result.position == doctest::Approx(1.5));
+}
+
+TEST_CASE("uphill start: a brake that doesn't get stronger ends the top-up after one more step") {
+ // all the wagon brakes cut out: no step adds anything
+ auto const result{run_top_up(0.0, 0.0)};
+ CHECK(result.steps == 1);
+ CHECK(result.position < uphill_start::top_up_position);
+}
+
+TEST_CASE("uphill start: a brake that gets stronger by too little ends the top-up") {
+ // a step adds 1% of the grade force, below the 2% that counts
+ auto const result{run_top_up(0.3, 0.01)};
+ CHECK(result.steps == 1);
+ SUBCASE("and one that gets stronger by enough goes on") {
+ auto const going{run_top_up(0.3, 0.05)};
+ CHECK(going.steps > 1);
+ }
+}
+
+TEST_CASE("uphill start: the top-up goes no further than full service") {
+ // every step adds enough to go on, but never enough to hold the train
+ auto const result{run_top_up(0.0, 0.03)};
+ CHECK(result.position == doctest::Approx(uphill_start::top_up_position));
+ CHECK(result.steps == 11);
+ top_up state;
+ CHECK(brake_top_up(state, 0.0, grade, uphill_start::top_up_position, application, application) == top_up_action::none);
+}
+
+TEST_CASE("uphill start: once ended, the top-up stays ended for the start") {
+ top_up state;
+ REQUIRE(brake_top_up(state, 0.2 * grade, grade, 1.25, application, application) == top_up_action::step);
+ REQUIRE(brake_top_up(state, 0.2 * grade, grade, 1.5, application, application) == top_up_action::none);
+ CHECK(state.done);
+ // the brake gets stronger later all the same: the start has gone on without the top-up
+ CHECK(brake_top_up(state, 0.5 * grade, grade, 1.5, application, application) == top_up_action::none);
+ // a new start begins with a new top-up
+ state = {};
+ CHECK(brake_top_up(state, 0.5 * grade, grade, 1.5, application, application) == top_up_action::step);
+}
diff --git a/vehicle/Driver.cpp b/vehicle/Driver.cpp
index 0d80e105f..2deac6621 100644
--- a/vehicle/Driver.cpp
+++ b/vehicle/Driver.cpp
@@ -3751,7 +3751,7 @@ bool TController::DecBrake() {
// a position short of the first one brakes no weaker than that
deltaAcc = -AccDesired * BrakeAccFactor() - ( fBrake_a0[ 0 ] + 4 * std::max( 0.0, BrakeCtrlPosition - pos_diff ) * fBrake_a1[ 0 ] );
}
- if( bUphillStart ) {
+ if( bUphillStart && ( false == bUphillRelease ) ) {
// starting uphill the train brake holds the train until the power does
break;
}
@@ -6549,24 +6549,35 @@ TController::determine_consist_state() {
ConsistShade = 0.0;
auto *p { pVehicles[ end::front ] }; // pojazd na czole składu
double dy; // składowa styczna grawitacji, w przedziale <0,1>
+ // starting uphill the power is applied against the train brake on purpose, until the power holds the train and
+ // then while the brake releases
+ auto const heldforstart { bUphillStart };
+ auto released { true }; // the brakes are off, the hold of an uphill start not counted
+ auto mostapplied { 0.0 }; // the most applied brake, the hold of an uphill start not counted
+ auto momentum { 0.0 };
while (p)
{ // sprawdzenie odhamowania wszystkich połączonych pojazdów
auto *vehicle { p->MoverParameters };
auto const bp { std::max( 0.0, vehicle->BrakePress - ( vehicle->SpeedCtrlUnit.Parking ? vehicle->MaxBrakePress[ 0 ] * vehicle->StopBrakeDecc : 0.0 ) ) };
- // starting uphill the power is applied against the train brake on purpose, until the power holds the train
- auto const heldforstart { bUphillStart };
- if (Ready) {
+ momentum += vehicle->TotalMass * vehicle->V * ( p->DirectionGet() == iDirection ? 1 : -1 );
+ mostapplied = std::max( bp, mostapplied );
+ auto const wasready { Ready };
+ if( released || Ready ) {
// bo jak coś nie odhamowane, to dalej nie ma co sprawdzać
- if( auto const isbraking { TestFlagAny( vehicle->Hamulec->GetBrakeStatus(), b_hld | b_on ) };
- vehicle->Vel < 1.0 ?
+ auto const isbraking { TestFlagAny( vehicle->Hamulec->GetBrakeStatus(), b_hld | b_on ) };
+ if( vehicle->Vel < 1.0 ?
isbraking || bp > 0.4 : // ensure the brakes are sufficiently released when starting to move
// once in motion a brake down to 0.4 bar counts as released (UIC), even if its distributor
// is still in the lap position while the brake pipe recharges
( isbraking && bp > 0.4 ) || vehicle->Fb > 10000.0 ) { // 10 kN
- // ready all the same only at a standstill with the power going on against the brake on purpose;
- // in motion a braking force left anywhere in the train (a hand brake) stops it as before
- Ready = heldforstart && ( vehicle->Vel < 1.0 );
+ released = false;
+ // ready all the same with the power going on against the brake on purpose: at a standstill, and once
+ // the power holds the train, while the brake releases; otherwise in motion a braking force left
+ // anywhere in the train (a hand brake) stops it as before
+ Ready = Ready && heldforstart && ( bUphillRelease || ( vehicle->Vel < 1.0 ) );
}
+ }
+ if( wasready ) {
// Ra: odluźnianie przeładowanych lokomotyw, ciągniętych na zimno - prowizorka...
if( bp >= 0.4 && (AIControllFlag || Global.AITrainman && mvOccupied->Vel < EU07_AI_NOMOVEMENT && !is_emu() && !is_dmu()) && BrakeCtrlPosition == gbh_RP // jest pozycja jazdy
&& false == TestFlag(vehicle->Hamulec->GetBrakeStatus(), b_dmg) // brake isn't broken
@@ -6583,7 +6594,8 @@ TController::determine_consist_state() {
vehicle->BrakeReleaser(0);
}
}
- fReady = std::max( heldforstart ? 0.0 : bp, fReady ); // szukanie najbardziej zahamowanego
+ // the brake held for the start counts again once it is being released
+ fReady = std::max( ( heldforstart && ( false == bUphillRelease ) ) ? 0.0 : bp, fReady ); // szukanie najbardziej zahamowanego
if( ( dy = p->VectorFront().y ) != 0.0 ) {
// istotne tylko dla pojazdów na pochyleniu
// ciężar razy składowa styczna grawitacji
@@ -6679,6 +6691,10 @@ TController::determine_consist_state() {
&& fReady < 0.8 ) { // delikatniejszy warunek, obejmuje wszystkie wagony
Ready = true; //żeby uznać za odhamowany
}
+ // the same test without the hold of an uphill start
+ IsConsistReleased = released || ( fAccGravity < -0.05 && mostapplied < 0.8 );
+ // the frame of the vehicle the AI drives from, as for fAccGravity and AbsAccS
+ fConsistVelocity = 3.6 * momentum / std::max( 1.0, fMass ) * ( iDirection >= 0 ? 1 : -1 );
auto const departblocked {
AIControllFlag
&& mvOccupied->Vel < EU07_AI_NOMOVEMENT
@@ -8717,6 +8733,12 @@ void TController::control_tractive_force() {
if( AccDesired > EU07_AI_NOACCELERATION // don't add power if not asked for actual speed-up
&& (AbsAccS < AccDesired /*- std::min( 0.05, 0.01 * iDriverFailCount )*/ || mvOccupied->SpeedCtrlUnit.IsActive && velocity < mvOccupied->SpeedCtrlUnit.FullPowerVelocity) &&
false == TestFlag(iDrivigFlags, movePress) &&
+ // starting uphill against the releasing brake, the power that holds the train is enough: the brake coming off
+ // speeds the train up, more power only drags the brake and slips the wheels. nor is a notch past that taken
+ // back: the power got there in one notch from below, and the notch below doesn't hold the train
+ ( false == bUphillRelease || fUphillTractiveForce < uphill_start::holding_force * fUphillHoldingForce ) &&
+ // nor while the train brake is still being applied further to hold the train
+ false == bUphillBrakeTopUp &&
velocity < (VelDesired == 1.0 ? // work around for trains getting stuck on tracks with speed limit = 1
VelDesired :
VelDesired - fVelMinus) &&
@@ -8773,15 +8795,19 @@ void TController::control_uphill_start() {
&& ( false == is_emu() ) && ( false == is_dmu() ) // a multiple unit has no locomotive to hold a train behind it
&& ( fAccGravity < EU07_AI_UPHILLGRAVITY ) };
false == uphill ) {
- bUphillStart = false;
+ end_uphill_start();
return;
}
if( false == bUphillStart ) {
if( ( std::abs( velocity ) < EU07_AI_NOMOVEMENT )
+ // the whole train at a stand, not only its head stopped for a moment while the train rolls back
+ && ( fConsistVelocity > -0.5 )
&& ( AccDesired > EU07_AI_NOACCELERATION )
&& ( VelDesired > 0.0 )
&& ( BrakeCtrlPosition > 0.0 ) ) { // only with the train brake still applied to hold the train
bUphillStart = true;
+ bUphillRelease = false;
+ UphillTopUp = {};
fUphillStartTime = ElapsedTime;
}
else {
@@ -8790,31 +8816,86 @@ void TController::control_uphill_start() {
}
if( AccDesired <= EU07_AI_NOACCELERATION ) {
// no longer to start: the standing rules take over
- bUphillStart = false;
+ end_uphill_start();
return;
}
- if( velocity < -0.5 ) {
- // rolling back all the same: the rules for a train slipping back take over and apply the brakes
- bUphillStart = false;
+ if( fConsistVelocity < -0.5 ) {
+ // the train rolls back all the same: the rules for a train slipping back take over and apply the brakes.
+ // the train as a whole, not its head: with the locomotive brake off, the locomotive and the first vehicles
+ // run back into the slack of a long train still held by its brake
+ end_uphill_start();
return;
}
- if( ( mvOccupied->LocalBrake != TLocalBrake::ManualBrake )
- && ( mvOccupied->LocalBrakePosA > 0.0 ) ) {
- cue_action( driver_hint::independentbrakerelease );
- }
- auto tractiveforce { 0.0 };
+ fUphillTractiveForce = 0.0;
for( auto const *vehicle { pVehicles[ end::front ] }; vehicle != nullptr; vehicle = vehicle->Next() ) {
- // Ft is signed in the vehicle's own frame
- tractiveforce += std::max( 0.0, vehicle->MoverParameters->Ft * ( vehicle->DirectionGet() == iDirection ? 1 : -1 ) );
+ // Ft is signed in the vehicle's own frame, and turned into the direction of travel as AbsAccS is
+ fUphillTractiveForce += std::max( 0.0, vehicle->MoverParameters->Ft * ( vehicle->DirectionGet() == iDirection ? 1 : -1 ) * ( iDirection >= 0 ? 1 : -1 ) );
}
- auto const holdingforce { fMass * -fAccGravity };
- if( ( velocity > 1.0 )
- || ( tractiveforce > 1.1 * holdingforce )
- // a locomotive that can't hold the train on its own power gets the brake off in the end all the same
- || ( ElapsedTime - fUphillStartTime > 45.0 ) ) {
- // the power holds the train: the train brake can be released
- bUphillStart = false;
+ fUphillHoldingForce = fMass * -fAccGravity;
+ bUphillBrakeTopUp = false;
+ if( false == bUphillRelease ) {
+ bUphillBrakeTopUp = uphill_brake_top_up();
+ if( bUphillBrakeTopUp ) {
+ return;
+ }
+ if( ( mvOccupied->LocalBrake != TLocalBrake::ManualBrake )
+ && ( mvOccupied->LocalBrakePosA > 0.0 ) ) {
+ cue_action( driver_hint::independentbrakerelease );
+ }
+ if( ( velocity > 1.0 )
+ || ( fUphillTractiveForce > uphill_start::holding_force * fUphillHoldingForce )
+ // a locomotive that can't hold the train on its own power gets the brake off in the end all the same
+ || ( ElapsedTime - fUphillStartTime > 45.0 ) ) {
+ // the power holds the train: the train brake can be released
+ bUphillRelease = true;
+ fUphillReleaseTime = ElapsedTime;
+ }
+ return;
}
+ // the train brake is being released with the power on. the start ends once the brakes are off: ended before,
+ // the brake still on takes the power off, the train rolls back and has to be braked to a stand again
+ if( IsConsistReleased
+ // a brake that doesn't come off (a hand brake) ends it all the same
+ || ( ElapsedTime - fUphillReleaseTime > EU07_AI_UPHILLRELEASETIME ) ) {
+ end_uphill_start();
+ }
+}
+
+// the locomotive brake comes off only once the brake of the rest of the train holds the train on its own: a train
+// stopped on a light application stands on the locomotive brake too, and rolls back as soon as it is off. until then
+// the train brake is applied further, a step at a time; true while it is
+bool TController::uphill_brake_top_up() {
+
+ auto trainbrakeforce { 0.0 };
+ auto haswagons { false };
+ for( auto const *vehicle { pVehicles[ end::front ] }; vehicle != nullptr; vehicle = vehicle->Next() ) {
+ if( vehicle->MoverParameters->Power < 0.01 ) {
+ trainbrakeforce += vehicle->MoverParameters->Fb;
+ haswagons = true;
+ }
+ }
+ if( false == haswagons ) {
+ return false;
+ }
+ auto const action { uphill_start::brake_top_up(
+ UphillTopUp, trainbrakeforce, fUphillHoldingForce, BrakeCtrlPosition, fBrakeHeldTime,
+ mvOccupied->BrakeDelay[ mvOccupied->BrakeDelayFlag > bdelay_G ? 1 : 3 ] ) };
+ if( action == uphill_start::top_up_action::none ) {
+ return false;
+ }
+ // the time for the power to hold the train starts once the brake does
+ fUphillStartTime = ElapsedTime;
+ if( action == uphill_start::top_up_action::step ) {
+ BrakeLevelAdd( BrakingLevelIncrease );
+ }
+ return true;
+}
+
+void TController::end_uphill_start() {
+
+ bUphillStart = false;
+ bUphillRelease = false;
+ bUphillBrakeTopUp = false;
}
void TController::increase_tractive_force() {
@@ -8888,10 +8969,17 @@ void TController::control_braking_force() {
if( TestFlag( iDrivigFlags, movePress ) ) { return; }
auto const velocity { DirectionalVel() };
+ // a train slipping back uphill is braked, also when it is to go on: released now it only rolls back further.
+ // its head, or the train as a whole: the locomotive may stop for a moment while the rest of the train runs into it
+ auto const slippingbackuphill {
+ ( fAccGravity < -0.05 )
+ && ( std::min( velocity, fConsistVelocity ) < -0.1 )
+ && ( false == bUphillStart ) };
// jeśli przyspieszamy, to nie hamujemy
if( AccDesired > 0.0 && OrderCurrentGet() != Disconnect // przy odłączaniu nie zwalniamy tu hamulca
- && false == TestFlag(iDrivigFlags, movePress) ) {
+ && false == TestFlag(iDrivigFlags, movePress)
+ && false == slippingbackuphill ) {
cue_action(driver_hint::brakingforcesetzero);
}
@@ -8946,7 +9034,7 @@ void TController::control_braking_force() {
else {
// a stara wersja w miarę dobrze działa na składy wagonowe
if( (accdesiredresultant < fAccGravity - 0.1 && AbsAccS > accdesiredresultant + brake_increase_margin() // regular braking
- || fAccGravity < -0.05 && velocity < -0.1 && false == bUphillStart) && // also brake if uphill and slipping back
+ || slippingbackuphill) && // also brake if uphill and slipping back
(fBrakeTime < 0.0
// the size of the brake demand decides whether to skip the reaction time, so the brake-only part
// (on a descent a heavy train needs its brake built up at once, the gravity does not help it slow)
diff --git a/vehicle/Driver.h b/vehicle/Driver.h
index 0794799dc..62b326696 100644
--- a/vehicle/Driver.h
+++ b/vehicle/Driver.h
@@ -19,6 +19,7 @@ http://mozilla.org/MPL/2.0/.
#include "world/mtable.h"
#include "utilities/translation.h"
#include "application/driverhints.h"
+#include "vehicle/uphillstart.h"
#include "vehicle/blackbox.h"
auto const EU07_AI_ACCELERATION = 0.05;
@@ -27,6 +28,7 @@ auto const EU07_AI_BRAKINGTESTACCELERATION = -0.06;
auto const EU07_AI_NOMOVEMENT = 0.05; // standstill velocity threshold
auto const EU07_AI_MOVEMENT = 1.0; // deliberate movement velocity threshold
auto const EU07_AI_UPHILLGRAVITY = -0.025; // [m/s2] about 2.5 per mille up: the gradient that rolls a standing train back
+auto const EU07_AI_UPHILLRELEASETIME = 60.0; // [s] the longest an uphill start keeps the power on while the train brake releases
auto const EU07_AI_SPEEDLIMITEXTENDSBEYONDSCANRANGE = 10000.0;
enum TOrders
@@ -337,6 +339,8 @@ private:
bool uphill_stop_hold() const;
bool uphill_brake_committed( double const Velocity ) const;
void control_uphill_start();
+ void end_uphill_start();
+ bool uphill_brake_top_up();
std::pair train_proximity_margins( bool const Passengerstop ) const;
void update_timers( double const dt );
@@ -441,6 +445,14 @@ private:
int iShortfallGrowth = 0; // how many checks in a row the shortfall has grown
bool bUphillStart = false; // starting uphill: the train brake holds the train until the power does
double fUphillStartTime = 0.0; // when the uphill start began
+ bool bUphillRelease = false; // starting uphill, the power holds the train and the train brake is being released
+ bool bUphillBrakeTopUp = false; // starting uphill, the train brake is applied further before the locomotive brake comes off
+ uphill_start::top_up UphillTopUp; // starting uphill, what has been done to apply the train brake further
+ double fUphillReleaseTime = 0.0; // when the release began
+ double fUphillTractiveForce = 0.0; // the tractive force of the consist while starting uphill [N]
+ double fUphillHoldingForce = 0.0; // the grade force on the consist while starting uphill [N]
+ bool IsConsistReleased = true; // the brakes of the consist are off, the hold of an uphill start not counted
+ double fConsistVelocity = 0.0; // the mass-weighted speed of the consist, positive in the direction of travel [km/h]
double fAccDesiredGravity = 0.0; // część AccDesired odjęta na spadku jako kompensacja pochylenia; AbsAccS zawiera ją już sam
double VelforDriver = -1.0; // prędkość, używana przy zmianie kierunku (ograniczenie przy nieznajmości szlaku?)
double VelSignal = 0.0; // ograniczenie prędkości z kompilacji znaków i sygnałów // normalnie na początku ma stać, no chyba że jedzie
diff --git a/vehicle/uphillstart.cpp b/vehicle/uphillstart.cpp
new file mode 100644
index 000000000..dd1895b11
--- /dev/null
+++ b/vehicle/uphillstart.cpp
@@ -0,0 +1,36 @@
+/*
+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/uphillstart.h"
+
+namespace uphill_start {
+
+top_up_action brake_top_up(
+ top_up &State, double const Brakeforce, double const Holdingforce, double const Brakeposition, double const Heldtime,
+ double const Applicationtime) {
+
+ if (State.done || (Brakeforce >= holding_force * Holdingforce) || (Brakeposition >= top_up_position)) {
+ return top_up_action::none;
+ }
+ // each step is given the full application time of the train brake, for the cylinders along the train to fill
+ if (Heldtime < Applicationtime) {
+ return top_up_action::wait;
+ }
+ if ((State.last_force >= 0.0) && (Brakeforce - State.last_force < top_up_gain * Holdingforce)) {
+ // the last step added next to nothing: the brake of this train can't hold it (brakes cut out, a load setting
+ // too low), and the start goes on without
+ State.done = true;
+ return top_up_action::none;
+ }
+ State.last_force = Brakeforce;
+ return top_up_action::step;
+}
+
+} // namespace uphill_start
diff --git a/vehicle/uphillstart.h b/vehicle/uphillstart.h
new file mode 100644
index 000000000..108662cbe
--- /dev/null
+++ b/vehicle/uphillstart.h
@@ -0,0 +1,41 @@
+/*
+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
+
+// starting uphill (TController::control_uphill_start()), the AI's driver lets the locomotive brake off only once
+// the brake of the rest of the train holds the train on its own: a train stopped on a light application stands on
+// the locomotive brake too, and rolls back as soon as it is off. until then the train brake is applied further, a
+// step at a time
+namespace uphill_start {
+
+auto const holding_force{1.1}; // a force that holds a train uphill, as a multiple of the grade force
+auto const top_up_position{4.0}; // full service: the furthest the train brake is applied to hold the train
+// a step that adds less brake force than this, as a multiple of the grade force, is the last one
+auto const top_up_gain{0.02};
+
+// what has been done to apply the train brake further, in one start
+struct top_up {
+ double last_force{-1.0}; // the brake force when the last step was given, -1 before the first [N]
+ bool done{false}; // a step added next to nothing: no more of them in this start
+};
+
+enum class top_up_action {
+ none, // the brake holds the train, or can't be made to: the start goes on
+ wait, // the cylinders are still filling after the last step
+ step // apply the train brake one step further
+};
+
+// Brakeforce: the brake force of the unpowered vehicles [N]; Holdingforce: the grade force on the train [N];
+// Brakeposition: the train brake handle, as BrakeCtrlPosition; Heldtime: how long the handle has been where it
+// is [s]; Applicationtime: the time the train brake takes to apply, for the cylinders along the train to fill [s]
+top_up_action brake_top_up(
+ top_up &State, double Brakeforce, double Holdingforce, double Brakeposition, double Heldtime, double Applicationtime);
+
+} // namespace uphill_start