18
0
mirror of https://github.com/MaSzyna-EU07/maszyna.git synced 2026-11-24 16:39:13 +01:00

fix(ai): a standing train starts only with its brakes charged, held on the locomotive brake while it charges (#444)

* fix(ai): a standing train starts only with its brakes charged, held on the locomotive brake while it charges

Prepared from cold, the AI filled the brake pipe at once with the locomotive brake off: the distributors
released while the auxiliary reservoirs were still empty, so the train had no air to brake with, and on
a gradient it rolled as it released. The train counted as ready as soon as it was released. A train placed
cold with its pipe below the lock of the FV4a driver's brake valve (2.75 bar) wasn't unlocked until the
order to go, and went on what its reservoirs got meanwhile (TMS77185 in linia_107_objazdy: 40 minutes at
1.2-1.9 bar, the last wagon at 4.3 bar at the start).

- Standing, PrepareEngine() applies the locomotive brake first, then fills the pipe. Where the locomotive
  brake alone doesn't hold the train (1.1 x the grade force), the train brake goes to the first service
  position instead, so the reservoirs charge with the train held. Multiple units with the
  electro-pneumatic brake are released as before.
- Standing to charge, the AI unlocks the pipe as it does to go (the releaser, or the unlock button). A
  handle which has to be moved to unlock is left where the light application holds the train.
- A train is ready only once the pipe and the auxiliary reservoir of every vehicle with a working brake
  are within 0.5 bar of the working pressure (4.5 of 5.0: the last wagon of a long train stays at about
  4.6-4.7 from what leaks along the pipe), with the pipe fed at the head of the train. Brakes cut out
  don't count.
- The locomotive brake comes off only once the train is charged, also against DecBrakeEIM(), which for a
  vehicle without the integrated handle releases it too; an uphill start begins only then.
- A human driver gets the same wait as a hint: "Wait for the train brakes to charge"
  (driver_hint::waitbrakecharge), and the hint to release the brakes waits with it once the train brake is
  off (brake_charge::release_waits()). The train brake may still come off: released, it fills the pipe.
- A charge that rises by less than 0.02 bar in 60 s lets the train go all the same, with a line in the
  log. The time counts only while there is air to charge with (the engine prepared, the main reservoir
  above what the pipe charges to, the pipe fed at the head): a train with its compressor not running yet
  or a locked pipe is waited out. Once let go, a train stays so until it is to stand again: otherwise the
  pipe refilling behind the release held it at a crawl (et22-1075 with 36 wagons, baltyk_interregio).
- The check applies to the pneumatic brake only, to the first start of a train that hasn't run (above
  1 km/h) since it was placed, and to a start with the pipe below 2.5 bar (near empty, after an emergency
  application). A start after an ordinary stop, a service stop (3.0-3.5 bar), a change of direction or a
  shunting move isn't held: waiting there only delayed the train, and the scenery set the routes of the
  others later.
- The release of the train brake in PrepareEngine() had a stray semicolon after its condition; the
  condition applies again in motion.
- Unit tests for the charge check (brake_charge::charged(), locomotive_brake_holds(), release_waits()).

Run on all 174 shipped sceneries (20 minutes each, against master): no train stuck that moved on master,
466 timetable departures compared, 459 within 15 s, mean -1.2 s. Waits for the charge: median 4 s, 90%
39 s, longest 106 s (a heavy freight train charged from 2.2 bar).

* test(ai): the brake charge test counts its looks in whole numbers, not in a floating-point time

SonarCloud took the loop of the test of a train with nothing to charge with yet for a bug (cpp:S2193, a
counter of type double). The same 120 looks, 5 s apart.

* fix(ai): the light application that holds a charging train stays on, and an empty pipe is no charge

Runs of a cold train on a gradient (P33000 at Lupkow in linia_107_objazdy, 13-16 per mille, with an SM42 and 32
wagons, 1556 t) showed three faults of the charged-brakes start:

- The locomotive brake was taken to hold the train with the force of its brake blocks alone, 1031 kN for an ST43:
  several times what its wheels pass on to the rails. It is limited to a quarter of the locomotive's weight
  (brake_charge::adhesion), so a light locomotive with a heavy train gets the light application of the train brake.
- That application was taken off again as soon as the order to go came, by the rule that puts the handle back to
  running with the main reservoir below 5 bar, and by DecBrake(). The train then stood on the locomotive brake
  that doesn't hold it, and moved as its wagons let go (to 0.3 km/h). It stays on until the train is charged
  (brake_charge::holds_application()).
- With the train brake applied the train counted as charged to what the head of the pipe has, which is nothing with
  the pipe empty: charged at 0.02 bar the moment the brake was applied. The pipe at the head counts no lower than a
  full service application leaves (brake_charge::reference()).

The SM42 with the 32 wagons now stands still while it charges and starts without rolling back, 2 min 30 s after it
was prepared (4 min 40 s before, with a start that rolled back at 0.3 km/h). An ST43 with 33 wagons and a 754 with
8 coaches there are as before, their locomotive brake holds them. In eight of the sceneries run before
(l053_poranek, calkowo_v2_cementa, calkowo_v2_ctl-wegiela, calkowo_v2_osoba_jar_poranek, calkowo_retro_zima,
linia61_towarowy2, l053-sluzba-brutto-posp, baltyk_interregio) the 60 timetable departures compared are the same,
within 15 s, mean 0.0 s. Unit tests for the two new decisions.

* fix(ai): the locomotive brake counted of the occupied vehicle alone

locomotive_brake_force() added the brake of every powered vehicle of the train. The locomotive brake handle acts on the
occupied vehicle only (LocalBrakePosA is set on mvOccupied and passed to no other vehicle), so a second locomotive, in
multiple control or hauled dead, made the brake count as holding a train it doesn't hold, and the train brake was
released on a gradient with the leading locomotive alone holding.

The estimate of one vehicle is brake_charge::locomotive_brake_force(), with a test.

brakecharge.h named TController::control_brake_charge(), which doesn't exist: the logic is in
determine_consist_state().
This commit is contained in:
maj00r
2026-11-24 12:07:52 +01:00
committed by GitHub
parent 7c12b83ba4
commit e0b2bdfb7f
8 changed files with 501 additions and 12 deletions

View File

@@ -3329,9 +3329,13 @@ bool TController::PrepareEngine()
}
}
// set up train brake
if( mvOccupied->fBrakeCtrlPos != mvOccupied->Handle->GetPos( bh_RP ) ) {
if ( !mvOccupied->Handle->Time
|| mvOccupied->Handle->GetCP() < mvOccupied->HighPipePress - 0.05 );
if( mvOccupied->Vel < EU07_AI_NOMOVEMENT ) {
// standing: the locomotive brake first, then the pipe is filled
secure_standing_train();
}
else if( ( mvOccupied->fBrakeCtrlPos != mvOccupied->Handle->GetPos( bh_RP ) )
&& ( ( false == mvOccupied->Handle->Time )
|| ( mvOccupied->Handle->GetCP() < mvOccupied->HighPipePress - 0.05 ) ) ) {
cue_action( driver_hint::trainbrakerelease );
}
// sync spring brake state across consist
@@ -3760,6 +3764,13 @@ bool TController::DecBrake() {
// any release now raises the pipe, and a long train releases more than the handle is moved back, too late
// to apply again before the train stands: keep the brake as it is, and the locomotive brake with it
break;
}
if( brake_charge::holds_application(
bBrakeChargeHold, IsConsistCharged, mvOccupied->Vel < EU07_AI_NOMOVEMENT, BrakeCtrlPosition ) ) {
// the order to go came while the train still charges: released now, the wagons let go as their
// reservoirs fill and the train stands on a locomotive brake that doesn't hold it. the locomotive
// brake stays on with it
break;
}
if (deltaAcc < 0 && /*GBH mvOccupied->BrakeCtrlPosR*/ BrakeCtrlPosition > 0) {
OK = /*mvOccupied->*/ BrakeLevelAdd(-0.25);
@@ -3769,10 +3780,16 @@ bool TController::DecBrake() {
if (BrakeCtrlPosition < 0.74)
/*mvOccupied->*/ BrakeLevelSet(gbh_RP);
}
if( !OK ) {
// standing, the locomotive brake holds the train until its brakes are charged: off before, a train
// released on a gradient rolls with no air to brake with. nor through DecBrakeEIM(), which takes it off
// as well (the locomotive brake of a vehicle without the integrated handle)
auto const holdlocomotive {
( false == IsConsistCharged )
&& ( mvOccupied->Vel < EU07_AI_NOMOVEMENT ) };
if( ( !OK ) && ( false == holdlocomotive ) ) {
OK = mvOccupied->DecLocalBrakeLevel(2);
}
if( !OK ) {
if( ( !OK ) && ( false == holdlocomotive ) ) {
OK = DecBrakeEIM();
}
/*
@@ -6555,12 +6572,18 @@ TController::determine_consist_state() {
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 };
auto chargelevel { std::numeric_limits<double>::max() }; // the lowest pipe or auxiliary reservoir pressure of a working brake
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 ) ) };
momentum += vehicle->TotalMass * vehicle->V * ( p->DirectionGet() == iDirection ? 1 : -1 );
mostapplied = std::max( bp, mostapplied );
if( ( vehicle->BrakeSystem != TBrakeSystem::Individual )
&& ( vehicle->MaxBrakePress[ 3 ] > 0.0 )
&& ( false == TestFlag( vehicle->Hamulec->GetBrakeStatus(), b_dmg ) ) ) { // a brake cut out doesn't count
chargelevel = std::min( { chargelevel, vehicle->PipePress, vehicle->Hamulec->GetBRP() } );
}
auto const wasready { Ready };
if( released || Ready ) {
// bo jak coś nie odhamowane, to dalej nie ma co sprawdzać
@@ -6695,6 +6718,69 @@ TController::determine_consist_state() {
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 );
// standing, the train starts only with its brakes charged: released alone, a train prepared from cold has no air to
// brake with while its auxiliary reservoirs fill. with the train brake applied (held for an uphill start, a light
// application while charging) they charge to what the head of the pipe has.
// only the start the charge is in question for: the first after the train was prepared standing, or one with the
// pipe near empty (after an emergency application, brake_charge::empty_level). after an ordinary stop the
// reservoirs refill as the brake releases, and waiting for the last tenths of a bar there only delays the train and
// those behind it.
// and only the pneumatic brake: multiple units with the electro-pneumatic one start as before
if( mvOccupied->Vel > 1.0 ) {
// the start is made. a shunting engine coupling wagons moves at a walking pace: a higher speed would leave its
// every start the first, waiting for each cut of wagons placed cold to charge
bBrakeChargeStart = false;
bBrakeChargeHold = false;
bHasRun = true;
}
auto const chargeable {
( BrakeSystem == TBrakeSystem::Pneumatic )
&& ( mvOccupied->Vel < EU07_AI_NOMOVEMENT )
&& ( chargelevel < std::numeric_limits<double>::max() ) };
if( chargeable && ( chargelevel < brake_charge::empty_level ) ) {
bBrakeChargeStart = true;
}
if( chargeable && bBrakeChargeStart ) {
if( ( VelDesired <= 0.0 ) || ( AccDesired <= EU07_AI_NOACCELERATION ) ) {
// to stand: the next start is checked anew
BrakeCharge.going = false;
}
auto const trainbrakeapplied {
AIControllFlag ?
BrakeCtrlPosition > 0.0 :
// a human driver's handle, which BrakeCtrlPosition doesn't follow
mvOccupied->fBrakeCtrlPos > mvOccupied->Handle->GetPos( bh_RP ) + 0.2 };
auto const reference {
brake_charge::reference( mvOccupied->PipePress, std::min( mvOccupied->Handle->GetRP(), 5.0 ), trainbrakeapplied ) };
// air to charge with: the engine prepared, the main reservoir above what the pipe charges to, and the driver's
// brake valve feeding the pipe at the head of the train. a train still being prepared from cold can't charge,
// nor one whose pipe is locked (unlocked by control_releaser()): it waits without that counting as a stall, held
// by its brakes as it can't release them either
auto const supplied {
iEngineActive
&& ( std::max( mvControlling->Compressor, mvOccupied->Compressor ) > reference )
&& ( mvOccupied->PipePress >= reference - brake_charge::margin ) };
auto const wasstalled { BrakeCharge.stalled };
IsConsistCharged = brake_charge::charged( BrakeCharge, chargelevel, reference, ElapsedTime, supplied );
if( BrakeCharge.stalled && ( false == wasstalled ) ) {
WriteLog(
"AI: " + mvOccupied->Name + " stopped charging its brakes at " + to_string( chargelevel, 2 )
+ " bar of " + to_string( reference, 2 ) + ", starting all the same" );
}
if( false == IsConsistCharged ) {
// a human driver is told to wait as the ai does, the locomotive brake applied
cue_action( driver_hint::waitbrakecharge );
}
}
else {
IsConsistCharged = true;
BrakeCharge = {};
}
if( AIControllFlag
&& ( false == IsConsistCharged )
&& ( false == heldforstart ) ) {
Ready = false;
}
auto const departblocked {
AIControllFlag
&& mvOccupied->Vel < EU07_AI_NOMOVEMENT
@@ -8804,7 +8890,8 @@ void TController::control_uphill_start() {
&& ( fConsistVelocity > -0.5 )
&& ( AccDesired > EU07_AI_NOACCELERATION )
&& ( VelDesired > 0.0 )
&& ( BrakeCtrlPosition > 0.0 ) ) { // only with the train brake still applied to hold the train
&& ( 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 = {};
@@ -8898,6 +8985,55 @@ void TController::end_uphill_start() {
bUphillBrakeTopUp = false;
}
// standing, a train is prepared as a driver does it: the locomotive brake first, then the brake pipe is filled. the pipe
// filled at once releases the distributors while the auxiliary reservoirs are still empty: the train has no brake left,
// and on a gradient rolls away as it releases. where the locomotive brake alone doesn't hold the train, the train brake
// is applied lightly, so the reservoirs charge with the train held. the locomotive brake comes off only once the train
// is charged and to start (DecBrake())
void TController::secure_standing_train() {
if( BrakeSystem != TBrakeSystem::Pneumatic ) {
// the electro-pneumatic brake of a multiple unit is released as before: nothing there takes the locomotive
// brake off again when the unit starts
cue_action( driver_hint::trainbrakerelease );
return;
}
// the first start of a train that hasn't run yet waits for the brakes to charge. one that has run, prepared again
// (a change of direction, an engine started anew), has its reservoirs charged from the run and starts as before
if( false == bHasRun ) {
bBrakeChargeStart = true;
}
if( mvOccupied->LocalBrake != TLocalBrake::ManualBrake ) {
cue_action( driver_hint::independentbrakeapply );
}
if( false == brake_charge::locomotive_brake_holds( locomotive_brake_force(), fMass * fAccGravity ) ) {
// the first service position: the pipe and the reservoirs charge to a little below the working pressure, the
// distributors keep a light application. a stronger application already there is left as it is, but not
// the emergency one or an empty pipe's, which don't charge
if( AIControllFlag
&& ( ( BrakeCtrlPosition < 1.0 ) || ( BrakeCtrlPosition > uphill_start::top_up_position ) ) ) {
BrakeLevelSet( 1.0 );
}
bBrakeChargeHold = true;
return;
}
bBrakeChargeHold = false;
cue_action( driver_hint::trainbrakerelease );
}
// the brake force of the locomotive brake at full application [N]. of the occupied vehicle alone: its handle acts on
// no other, not on a second section or a locomotive in multiple control either, let alone one hauled dead
double TController::locomotive_brake_force() const {
if( ( mvOccupied->LocalBrake == TLocalBrake::ManualBrake )
|| ( mvOccupied->MaxBrakePress[ 0 ] <= 0.0 ) ) {
return 0.0;
}
return brake_charge::locomotive_brake_force(
mvOccupied->BrakeForceP( mvOccupied->MaxBrakePress[ 0 ], 0.0 ),
mvOccupied->TotalMassxg );
}
void TController::increase_tractive_force() {
if( fActionTime < 0.0 ) { return; } // gdy jest nakaz poczekać z jazdą, to nie ruszać
@@ -9060,7 +9196,14 @@ void TController::control_braking_force() {
}
if ( (accdesiredresultant < fAccGravity - 0.05
|| AccDesired >= EU07_AI_NOACCELERATION) // no braking asked for, yet the train is slowing down more than that
&& accdesiredresultant - fBrake_a1[0] * 0.51 - AbsAccS > 0.05 && OrderCurrentGet() != Disconnect && VelDesired > 0.0 ) {
&& accdesiredresultant - fBrake_a1[0] * 0.51 - AbsAccS > 0.05 && OrderCurrentGet() != Disconnect && VelDesired > 0.0
// a human driver standing with the train not charged yet keeps the locomotive brake on, told so by the
// waitbrakecharge hint; the ai is held back in DecBrake()
&& ( AIControllFlag
|| ( false == brake_charge::release_waits(
IsConsistCharged,
mvOccupied->Vel < EU07_AI_NOMOVEMENT,
mvOccupied->fBrakeCtrlPos > mvOccupied->Handle->GetPos( bh_RP ) + 0.2 ) ) ) ) { // the human driver's handle
// jak hamuje, to nie tykaj kranu za często
// yB: luzuje hamulec dopiero przy różnicy opóźnień rzędu 0.2
// przy odłączaniu nie zwalniamy tu hamulca
@@ -9136,7 +9279,16 @@ void TController::control_releaser() {
auto actuate { false };
auto isbrakehandleinrightposition { true };
if( AccDesired > EU07_AI_NOACCELERATION ) {
// a train prepared standing charges its brakes before its first start. a pipe left below the lock of the driver's
// brake valve (a train placed cold, as after an emergency application) is unlocked as the train stands, not only
// once it is to go: else it charges only after the order to go, and starts on what the reservoirs got by then.
// a handle which has to be moved to unlock is left where it holds the train with the train brake
auto const chargesstanding {
bBrakeChargeStart
&& ( mvOccupied->Vel < EU07_AI_NOMOVEMENT )
&& ( ( mvOccupied->HandleUnlock == -3 ) || ( BrakeCtrlPosition <= 0.0 ) ) };
if( ( AccDesired > EU07_AI_NOACCELERATION ) || chargesstanding ) {
// fill up brake pipe if needed
if( mvOccupied->PipePress < 3.0 ) {
actuate = true;
@@ -9146,6 +9298,8 @@ void TController::control_releaser() {
isbrakehandleinrightposition &= BrakeCtrlPosition == mvOccupied->HandleUnlock;
}
}
}
if( AccDesired > EU07_AI_NOACCELERATION ) {
// wyluzuj lokomotywę, to szybciej ruszymy
// this rule is for starting off, so only below 1 km/h: at speed, just after an application, it would vent
// the control reservoir of a locomotive that may have to brake again before the pipe has refilled it
@@ -9186,7 +9340,9 @@ void TController::control_releaser() {
void TController::control_main_pipe() {
// unlocking main pipe
if( AccDesired > -0.03
if( ( AccDesired > -0.03
// standing to charge before the first start, as in control_releaser()
|| ( bBrakeChargeStart && ( mvOccupied->Vel < EU07_AI_NOMOVEMENT ) ) )
&& true == mvOccupied->LockPipe ) {
UniversalBrakeButtons |= TUniversalBrake::ub_UnlockPipe;
}
@@ -9218,9 +9374,12 @@ void TController::control_main_pipe() {
update_brake_charging();
if( mvOccupied->Compressor < 5.0
|| ( BrakeCtrlPosition < gbh_RP
&& ( mvOccupied->EqvtPipePress > (fReady < 0.25 ? 5.1 : 5.2) || is_brake_charging_done() ) ) ) {
if( ( mvOccupied->Compressor < 5.0
|| ( BrakeCtrlPosition < gbh_RP
&& ( mvOccupied->EqvtPipePress > (fReady < 0.25 ? 5.1 : 5.2) || is_brake_charging_done() ) ) )
// the light application a charging train stands on stays, as in DecBrake()
&& ( false == brake_charge::holds_application(
bBrakeChargeHold, IsConsistCharged, mvOccupied->Vel < EU07_AI_NOMOVEMENT, BrakeCtrlPosition ) ) ) {
cue_action( driver_hint::trainbrakerelease );
}
}