From d90fcea158212e2b759c79551ab38d774f7cfbb2 Mon Sep 17 00:00:00 2001
From: pawelw
Date: Fri, 27 Nov 2026 23:51:33 +0100
Subject: [PATCH] refactor(ai): the decisions of an uphill start in
uphill_start::, with unit tests of the release phase (#445)
* refactor(ai): the decisions of an uphill start in uphill_start::, with unit tests of the release phase
* refactor(ai): the grade force of an uphill start situation no longer named as the holding factor, std::array in its test
* refactor(ai): the situation of an uphill start filled by field name, so the compiler checks the order
---
tests/unit/test_uphill_start.cpp | 110 +++++++++++++++++++++++++++++++
vehicle/Driver.cpp | 74 ++++++++++-----------
vehicle/Driver.h | 1 -
vehicle/uphillstart.cpp | 31 +++++++++
vehicle/uphillstart.h | 36 ++++++++++
5 files changed, 211 insertions(+), 41 deletions(-)
diff --git a/tests/unit/test_uphill_start.cpp b/tests/unit/test_uphill_start.cpp
index d450af68b..e487fc703 100644
--- a/tests/unit/test_uphill_start.cpp
+++ b/tests/unit/test_uphill_start.cpp
@@ -101,3 +101,113 @@ TEST_CASE("uphill start: once ended, the top-up stays ended for the start") {
state = {};
CHECK(brake_top_up(state, 0.5 * grade, grade, 1.5, application, application) == top_up_action::step);
}
+
+// the rest of a start: when it begins, when it is given up, when the train brake may be released, when the release
+// phase is over and how much power is applied against the releasing brake
+
+namespace {
+
+// the ST43 train standing at the signal on its train brake, asked to go
+uphill_start::situation standing_train() {
+ uphill_start::situation now;
+ now.standing = true;
+ now.speed_up = true;
+ now.speed_asked = true;
+ now.train_brake_applied = true;
+ now.grade_force = grade;
+ return now;
+}
+
+} // namespace
+
+TEST_CASE("uphill start: begins with the whole train at a stand on the train brake, asked to go") {
+ auto now{standing_train()};
+ CHECK(uphill_start::begins(now));
+ SUBCASE("not while the head stands for a moment and the train rolls back") {
+ now.consist_velocity = -0.8;
+ CHECK_FALSE(uphill_start::begins(now));
+ }
+ SUBCASE("not without the train brake to hold the train") {
+ now.train_brake_applied = false;
+ CHECK_FALSE(uphill_start::begins(now));
+ }
+ SUBCASE("not before the brakes of the train are charged") {
+ now.consist_charged = false;
+ CHECK_FALSE(uphill_start::begins(now));
+ }
+ SUBCASE("not when not asked to go") {
+ now.speed_up = false;
+ CHECK_FALSE(uphill_start::begins(now));
+ now.speed_up = true;
+ now.speed_asked = false;
+ CHECK_FALSE(uphill_start::begins(now));
+ }
+ SUBCASE("not under way") {
+ now.standing = false;
+ CHECK_FALSE(uphill_start::begins(now));
+ }
+}
+
+TEST_CASE("uphill start: given up when the train as a whole rolls back, not its head") {
+ auto now{standing_train()};
+ // the locomotive runs back into the slack, the rest of the train stands
+ now.velocity = -1.0;
+ now.consist_velocity = -0.1;
+ CHECK_FALSE(uphill_start::rolls_back(now));
+ now.consist_velocity = -0.6;
+ CHECK(uphill_start::rolls_back(now));
+}
+
+TEST_CASE("uphill start: the train brake is released once the power holds the train") {
+ auto now{standing_train()};
+ now.tractive_force = 1.05 * grade;
+ CHECK_FALSE(uphill_start::power_holds(now, 10.0));
+ now.tractive_force = 1.15 * grade;
+ CHECK(uphill_start::power_holds(now, 10.0));
+ SUBCASE("or the train is under way") {
+ now.tractive_force = 0.5 * grade;
+ now.velocity = 1.2;
+ CHECK(uphill_start::power_holds(now, 10.0));
+ }
+ SUBCASE("or a locomotive that can't hold the train has had its time") {
+ now.tractive_force = 0.5 * grade;
+ CHECK_FALSE(uphill_start::power_holds(now, uphill_start::hold_time - 1.0));
+ CHECK(uphill_start::power_holds(now, uphill_start::hold_time + 1.0));
+ }
+}
+
+TEST_CASE("uphill start: the release phase ends when the brakes are off, or after its time") {
+ auto now{standing_train()};
+ CHECK_FALSE(uphill_start::released(now, 30.0));
+ now.consist_released = true;
+ CHECK(uphill_start::released(now, 30.0));
+ SUBCASE("a brake that doesn't come off ends it all the same") {
+ now.consist_released = false;
+ CHECK_FALSE(uphill_start::released(now, uphill_start::release_time - 1.0));
+ CHECK(uphill_start::released(now, uphill_start::release_time + 1.0));
+ }
+}
+
+TEST_CASE("uphill start: against the releasing brake no more power is added than holds the train") {
+ // outside the release phase the start doesn't limit the power
+ CHECK(uphill_start::may_add_power(false, 3.0 * grade, grade));
+ CHECK(uphill_start::may_add_power(true, 1.05 * grade, grade));
+ CHECK_FALSE(uphill_start::may_add_power(true, 1.15 * grade, grade));
+}
+
+TEST_CASE("uphill start: on coarse notches the power stays on the first one that holds the train") {
+ // an EU07 at a standstill on 3 kV gives about 29, 52, 79 and 104 kN on notches 1-4 (eu07/maszyna#396): with a
+ // train of 300 t on 10 per mille, 29.4 kN of grade force, notch 2 jumps from 0.99 to 1.77 x the grade force
+ std::array const notches{29000.0, 52000.0, 79000.0, 104000.0};
+ double const holding{300000.0 * 9.81 * 0.01};
+ auto notch{0};
+ auto force{0.0};
+ // in the release phase, a notch added an update as long as power may be added
+ for (auto update{0}; update < 10; ++update) {
+ if (uphill_start::may_add_power(true, force, holding) && (notch < static_cast(notches.size()))) {
+ force = notches[notch++];
+ }
+ }
+ CHECK(notch == 2);
+ CHECK(force / holding == doctest::Approx(1.77).epsilon(0.01));
+}
diff --git a/vehicle/Driver.cpp b/vehicle/Driver.cpp
index 4b8a68ed4..554d304cc 100644
--- a/vehicle/Driver.cpp
+++ b/vehicle/Driver.cpp
@@ -8945,10 +8945,8 @@ 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 ) &&
+ // starting uphill against the releasing brake, no more power than holds the train
+ uphill_start::may_add_power( bUphillRelease, fUphillTractiveForce, 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
@@ -9010,41 +9008,42 @@ void TController::control_uphill_start() {
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
- && ( IsConsistCharged ) ) { // and charged to what the pipe has, or the brake that holds the train has no air
- bUphillStart = true;
- bUphillRelease = false;
- UphillTopUp = {};
- fUphillStartTime = ElapsedTime;
- }
- else {
- return;
- }
- }
- if( AccDesired <= EU07_AI_NOACCELERATION ) {
- // no longer to start: the standing rules take over
- end_uphill_start();
- return;
- }
- 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;
- }
fUphillTractiveForce = 0.0;
for( auto const *vehicle { pVehicles[ end::front ] }; vehicle != nullptr; vehicle = vehicle->Next() ) {
// 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 ) );
}
fUphillHoldingForce = fMass * -fAccGravity;
+ uphill_start::situation const now {
+ .standing = std::abs( velocity ) < EU07_AI_NOMOVEMENT,
+ .velocity = velocity,
+ .consist_velocity = fConsistVelocity,
+ .speed_up = AccDesired > EU07_AI_NOACCELERATION,
+ .speed_asked = VelDesired > 0.0,
+ .train_brake_applied = BrakeCtrlPosition > 0.0,
+ .tractive_force = fUphillTractiveForce,
+ .grade_force = fUphillHoldingForce,
+ .consist_released = IsConsistReleased,
+ .consist_charged = IsConsistCharged };
+ if( false == bUphillStart ) {
+ if( false == uphill_start::begins( now ) ) {
+ return;
+ }
+ bUphillStart = true;
+ bUphillRelease = false;
+ UphillTopUp = {};
+ fUphillStartTime = ElapsedTime;
+ }
+ if( false == now.speed_up ) {
+ // no longer to start: the standing rules take over
+ end_uphill_start();
+ return;
+ }
+ if( uphill_start::rolls_back( now ) ) {
+ // the rules for a train slipping back take over and apply the brakes
+ end_uphill_start();
+ return;
+ }
bUphillBrakeTopUp = false;
if( false == bUphillRelease ) {
bUphillBrakeTopUp = uphill_brake_top_up();
@@ -9055,10 +9054,7 @@ void TController::control_uphill_start() {
&& ( 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 ) ) {
+ if( uphill_start::power_holds( now, ElapsedTime - fUphillStartTime ) ) {
// the power holds the train: the train brake can be released
bUphillRelease = true;
fUphillReleaseTime = ElapsedTime;
@@ -9067,9 +9063,7 @@ void TController::control_uphill_start() {
}
// 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 ) ) {
+ if( uphill_start::released( now, ElapsedTime - fUphillReleaseTime ) ) {
end_uphill_start();
}
}
diff --git a/vehicle/Driver.h b/vehicle/Driver.h
index 0eae2caf1..0fc445ce3 100644
--- a/vehicle/Driver.h
+++ b/vehicle/Driver.h
@@ -30,7 +30,6 @@ 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;
auto const EU07_AI_SANDEDSTARTVELOCITY = 15.0; // [km/h] a start that slipped keeps the sand on up to this speed
diff --git a/vehicle/uphillstart.cpp b/vehicle/uphillstart.cpp
index dd1895b11..534839093 100644
--- a/vehicle/uphillstart.cpp
+++ b/vehicle/uphillstart.cpp
@@ -12,6 +12,37 @@ http://mozilla.org/MPL/2.0/.
namespace uphill_start {
+bool begins(situation const &Now) {
+
+ return Now.standing
+ // the whole train at a stand, not only its head stopped for a moment while the train rolls back
+ && (Now.consist_velocity > rollback_velocity) && Now.speed_up && Now.speed_asked
+ // only with the train brake still applied to hold the train
+ && Now.train_brake_applied
+ // and charged to what the pipe has, or the brake that holds the train has no air
+ && Now.consist_charged;
+}
+
+bool rolls_back(situation const &Now) {
+
+ return Now.consist_velocity < rollback_velocity;
+}
+
+bool power_holds(situation const &Now, double const Holdtime) {
+
+ return (Now.velocity > moving_velocity) || (Now.tractive_force > holding_force * Now.grade_force) || (Holdtime > hold_time);
+}
+
+bool released(situation const &Now, double const Releasetime) {
+
+ return Now.consist_released || (Releasetime > release_time);
+}
+
+bool may_add_power(bool const Releasing, double const Tractiveforce, double const Holdingforce) {
+
+ return (false == Releasing) || (Tractiveforce < holding_force * Holdingforce);
+}
+
top_up_action brake_top_up(
top_up &State, double const Brakeforce, double const Holdingforce, double const Brakeposition, double const Heldtime,
double const Applicationtime) {
diff --git a/vehicle/uphillstart.h b/vehicle/uphillstart.h
index 108662cbe..687934990 100644
--- a/vehicle/uphillstart.h
+++ b/vehicle/uphillstart.h
@@ -20,6 +20,42 @@ auto const top_up_position{4.0}; // full service: the furthest the train brake i
// 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};
+auto const hold_time{45.0}; // [s] a locomotive that can't hold the train on its own power gets the brake off after it all the same
+auto const release_time{60.0}; // [s] the longest the power stays on against the releasing brake (a hand brake doesn't come off)
+auto const rollback_velocity{-0.5}; // [km/h] the train as a whole rolling back faster than this gives a start up
+auto const moving_velocity{1.0}; // [km/h] the head of the train going forward faster than this is under way
+
+// what a start goes by, at a moment
+struct situation {
+ bool standing{false}; // the head of the train at a stand
+ double velocity{0.0}; // of the head of the train, in the direction of travel [km/h]
+ // of the whole train, mass-weighted, in the direction of travel [km/h]: 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
+ double consist_velocity{0.0};
+ bool speed_up{false}; // the driver is asked to speed up
+ bool speed_asked{false}; // there is a speed to go at
+ bool train_brake_applied{false}; // the train brake handle is past running
+ double tractive_force{0.0}; // of the train, in the direction of travel [N]
+ double grade_force{0.0}; // the grade force on the train [N]
+ bool consist_released{false}; // the brakes of the train are off, the hold of the start not counted
+ // the brakes of the train charged to what the pipe has: else the brake that holds the train has no air
+ bool consist_charged{true};
+};
+
+// a start begins: the whole train at a stand on the train brake, charged, and asked to go
+bool begins(situation const &Now);
+// the train rolls back all the same: the rules for a train slipping back take over
+bool rolls_back(situation const &Now);
+// the power holds the train, so the train brake can be released. Holdtime: since the start began, or since the last
+// step of the top-up [s]
+bool power_holds(situation const &Now, double Holdtime);
+// the release phase is over: the brakes are off, or they don't come off. Releasetime: since it began [s]
+bool released(situation const &Now, double Releasetime);
+// power may be added. 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
+bool may_add_power(bool Releasing, double Tractiveforce, double Holdingforce);
+
// 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]