From f53e184949614cab1aeed8411110694ddbef7c4a Mon Sep 17 00:00:00 2001 From: pwarszawski Date: Sat, 7 Nov 2026 15:37:14 +0100 Subject: [PATCH 1/7] AI braking: hold a train coming to a stand uphill Uphill a long train often came to a stand with its brake on the way off and rolled back several metres: the AI released the train brake to 0 about 20 m short of the stop (the gradient alone would have stopped the train short of the stopping window), applied only the first step again at the last moment, and a G train cannot apply its brake again within the few seconds the gradient takes to stop it. Coming to a stand on an up gradient (also when only part of the train is on it) the AI now: - applies the brake before the train stops on the gradient alone, once the time left to the stand is no longer than half of what the brake takes to apply; - does not let the train coast on or pull up while the brake is applied and the gradient would stop the train sooner than the brake of the consist can be released; - does not release the train brake any further below 10 km/h on the way to a stand (unless the train is to be driven on); - puts the locomotive brake on below 3 km/h when the train will reach the point it is to stop at (not when it has slowed down short of it and is to pull up), and at once if the train rolls back. --- vehicle/Driver.cpp | 59 ++++++++++++++++++++++++++++++++++++++++++++-- vehicle/Driver.h | 2 ++ 2 files changed, 59 insertions(+), 2 deletions(-) diff --git a/vehicle/Driver.cpp b/vehicle/Driver.cpp index ea78e655c..75f7bea7a 100644 --- a/vehicle/Driver.cpp +++ b/vehicle/Driver.cpp @@ -3203,6 +3203,12 @@ bool TController::DecBrake() { // a distributor applied at all gives at least its minimum stage, about what the first position does, so // 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( ( BrakeCtrlPosition > 0.0 ) + && ( uphill_stop_hold() ) ) { + // 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 (deltaAcc < 0 && /*GBH mvOccupied->BrakeCtrlPosR*/ BrakeCtrlPosition > 0) { OK = /*mvOccupied->*/ BrakeLevelAdd(-0.25); @@ -7341,8 +7347,15 @@ TController::pick_optimal_speed( double const Range ) { AccDesired = std::min( AccDesired, EU07_AI_NOACCELERATION ); } else if( ( vel < 0.0 ) - || ( ( vel < 5.0 ) && ( AccDesired < 0.0 ) ) ) { - AccDesired = std::min( AccDesired, fAccGravity - 0.15 ); + || ( ( vel < 5.0 ) && ( AccDesired < 0.0 ) ) + // rolling to a stand on the gradient alone, the brake has to be on its way before the train stops: + // once the time left to the stand is no longer than half of what the brake takes to apply, apply it; any + // sooner and the brake takes off more of the distance than the stop was planned with + || ( ( BrakeSystem == TBrakeSystem::Pneumatic ) + && ( vel / 3.6 / -fAccGravity <= 0.5 * brake_response_horizon( mvOccupied, BrakeSystem, 1.0, fBrake_a0[ 0 ] ) ) ) ) { + // the gradient alone already slows the train down nearly this much; ask for enough on top of it that + // the brake is applied for sure, not held back by the margin against small corrections + AccDesired = std::min( AccDesired, fAccGravity - std::max( 0.15, fBrake_a0[ 0 ] ) - brake_increase_margin() ); } } @@ -7651,6 +7664,7 @@ TController::adjust_desired_speed_for_target_speed( double const Range ) { ( VelNext * VelNext - vel * vel ) / ( 25.92 * stoppingroom ) ); if( ( fAccGravity < -0.025 ) && ( AccDesired >= fAccGravity - 0.001 ) // no brake needed at all + && ( false == uphill_brake_committed( vel ) ) && ( ActualProximityDist - uphill_stopping_distance( vel, fAccGravity ) > stopping_margin() ) ) { // uphill the gradient alone would bring the train to a stand short of the // middle of the stopping window; keep it rolling at the present speed until that changes, @@ -7726,6 +7740,7 @@ TController::adjust_desired_speed_for_target_speed( double const Range ) { } else if( ( fAccGravity < -0.025 ) && ( ( OrderCurrentGet() & Obey_train ) != 0 ) + && ( false == uphill_brake_committed( vel ) ) && ( ActualProximityDist - uphill_stopping_distance( vel, fAccGravity ) > brakingpointoffset ) ) { // uphill the gradient alone slows the train down more than it is asked to; // keep it rolling at the present speed until that changes @@ -8177,6 +8192,35 @@ TController::brake_increase_margin() const { return fBrake_a1[ 0 ]; } +// a train coming to a stand with at least part of it on an up gradient: a long train brake released now cannot be +// applied again before the train stops and starts to roll back, so the train brake is not released any further, and the +// locomotive brake, which acts at once, helps to hold the train +bool +TController::uphill_stop_hold() const { + + return ( + ( BrakeSystem == TBrakeSystem::Pneumatic ) && is_train() && ( ( OrderCurrentGet() & Obey_train ) != 0 ) + && ( VelNext == 0.0 ) + && ( ActualProximityDist <= fMaxProximityDist ) + && ( fAccGravity < -0.005 ) + && ( std::abs( DirectionalVel() ) < 10.0 ) + // not while the train is to be driven on + && ( AccDesired <= 0.0 ) ); +} + +// uphill with the train brake applied, the gradient brings a slow train to a stand sooner than the brake of the consist +// can be released: letting the train roll on (or pulling up) at this speed only releases the brake for nothing, and the +// train stands with the brake on its way off instead +bool +TController::uphill_brake_committed( double const Velocity ) const { + + return ( + ( BrakeSystem == TBrakeSystem::Pneumatic ) + && ( BrakeCtrlPosition > 0.0 ) + && ( fAccGravity < -0.025 ) + && ( Velocity / 3.6 / -fAccGravity < brake_response_horizon( mvOccupied, BrakeSystem, -1.0, fBrake_a0[ 0 ] ) ) ); +} + void TController::control_braking_force() { // don't touch the brakes when compressing buffers during coupling/uncoupling @@ -8276,6 +8320,17 @@ void TController::control_braking_force() { cue_action(driver_hint::brakingforcedecrease, (mvOccupied->BrakeDelayFlag > bdelay_G ? mvOccupied->BrakeDelay[0] : mvOccupied->BrakeDelay[2]) / 3.0 * 0.5); // Ra: tymczasowo, bo przeżyna S1 } + // uphill, the locomotive brake acts at once while the train brake of a long train takes many seconds to apply: + // put it on as the train comes to a stand, and at once if the train has started to roll back + if( ( mvOccupied->LocalBrake != TLocalBrake::ManualBrake ) + && ( mvOccupied->LocalBrakePosA < 1.0 ) + && ( ( uphill_stop_hold() && ( std::abs( velocity ) < 3.0 ) + // a train that has slowed down short of where it is to stop and is to pull up further does not get it + && ( VelDesired <= std::abs( velocity ) + 0.5 ) + && ( ActualProximityDist - uphill_stopping_distance( std::abs( velocity ), fAccGravity ) <= stopping_margin() ) ) + || ( ( BrakeSystem == TBrakeSystem::Pneumatic ) && ( fAccGravity < -0.005 ) && ( velocity < -0.1 ) && ( AccDesired <= 0.0 ) ) ) ) { + cue_action( driver_hint::independentbrakeapply ); + } // stop-gap measure to ensure cars actually brake to stop even when above calculactions go awry // instead of releasing the brakes and creeping into obstacle at 1-2 km/h if( is_car() && VelDesired == 0.0 && velocity > VelDesired && ActualProximityDist <= fMinProximityDist && mvOccupied->LocalBrakePosA < 0.01 ) { diff --git a/vehicle/Driver.h b/vehicle/Driver.h index f1975fca6..82899493a 100644 --- a/vehicle/Driver.h +++ b/vehicle/Driver.h @@ -312,6 +312,8 @@ private: return mvOccupied->Vel * sign( iDirection * mvOccupied->V ); } double brake_increase_margin() const; + bool uphill_stop_hold() const; + bool uphill_brake_committed( double const Velocity ) const; void update_timers( double const dt ); void update_logs( double const dt ); From 137eb9641f0ea9fa93596c8a4be959f2342d0ba3 Mon Sep 17 00:00:00 2001 From: pwarszawski Date: Sat, 7 Nov 2026 15:37:48 +0100 Subject: [PATCH 2/7] AI: start a train uphill on the train brake Starting uphill the AI released the train brake (and the locomotive brake with it) at once, while the power came on only later: a train on 10 per mille rolled back 20-30 m (E186 with 19 container wagons), and an E189 with 7 Z2 coaches rolled back for minutes without power, because the brake applied again for the rollback kept the power off. Many locomotives cannot pull while their own cylinder is under pressure (asynchronous drives switch to braking, others have a control pressure switch), so holding the train on the locomotive brake is no way out. A train standing on an up gradient with the train brake applied now starts as a driver of a heavy train does: - the train brake stays applied, the locomotive brake is released and the locomotive's own share of the train brake is let off with the releaser; - the power is applied against the braked train (Ready does not hold it back meanwhile, and the rule that brakes a train slipping back is off); - the train brake is released only once the tractive force exceeds 1.1 times the gradient force, the train moves forward, or after 45 s if the locomotive cannot hold the train on its own power. Multiple units are not affected. --- vehicle/Driver.cpp | 73 +++++++++++++++++++++++++++++++++++++++++++--- vehicle/Driver.h | 3 ++ 2 files changed, 72 insertions(+), 4 deletions(-) diff --git a/vehicle/Driver.cpp b/vehicle/Driver.cpp index 75f7bea7a..1a4d8e3a5 100644 --- a/vehicle/Driver.cpp +++ b/vehicle/Driver.cpp @@ -3204,6 +3204,10 @@ 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 ) { + // starting uphill the train brake holds the train until the power does + break; + } if( ( BrakeCtrlPosition > 0.0 ) && ( uphill_stop_hold() ) ) { // any release now raises the pipe, and a long train releases more than the handle is moved back, too late @@ -5947,6 +5951,8 @@ TController::determine_consist_state() { { // 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) { // bo jak coś nie odhamowane, to dalej nie ma co sprawdzać if( auto const isbraking { TestFlagAny( vehicle->Hamulec->GetBrakeStatus(), b_hld | b_on ) }; @@ -5955,7 +5961,9 @@ TController::determine_consist_state() { // 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 = false; + if( false == heldforstart ) { + Ready = false; + } } // 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 @@ -5973,7 +5981,7 @@ TController::determine_consist_state() { vehicle->BrakeReleaser(0); } } - fReady = std::max( bp, fReady ); // szukanie najbardziej zahamowanego + fReady = std::max( heldforstart ? 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 @@ -8109,6 +8117,8 @@ void TController::control_motor_connectors() { void TController::control_tractive_force() { + control_uphill_start(); + auto const velocity { DirectionalVel() }; // jeśli przyspieszenie pojazdu jest mniejsze niż żądane oraz... if( AccDesired > EU07_AI_NOACCELERATION // don't add power if not asked for actual speed-up @@ -8156,6 +8166,58 @@ bool TController::is_too_fast_uphill() const { return ( VelNext == 0.0 && ActualProximityDist <= fMaxProximityDist && AbsAccS > AccDesired + 0.05 ); } +// starting uphill a driver of a heavy train keeps the train brake applied, lets the locomotive's own brake off (the +// independent brake, and its share of the train brake with the releaser), applies the power, and releases the train +// brake only once the power holds the train. releasing everything first lets the train roll back before the power is +// there, and holding the train on the locomotive brake is no way out where that brake cuts the power (asynchronous +// drives, the control pressure switch) +void TController::control_uphill_start() { + + auto const velocity { DirectionalVel() }; + auto const uphill { + ( BrakeSystem == TBrakeSystem::Pneumatic ) + && is_train() + && ( false == is_emu() ) && ( false == is_dmu() ) // a multiple unit has no locomotive to hold a train behind it + && ( fAccGravity < -0.01 ) }; + if( false == uphill ) { + bUphillStart = false; + return; + } + if( false == bUphillStart ) { + if( ( std::abs( velocity ) < EU07_AI_NOMOVEMENT ) + && ( AccDesired > EU07_AI_NOACCELERATION ) + && ( VelDesired > 0.0 ) + && ( BrakeCtrlPosition > 0.0 ) ) { // only with the train brake still applied to hold the train + bUphillStart = true; + fUphillStartTime = ElapsedTime; + } + else { + return; + } + } + if( AccDesired <= EU07_AI_NOACCELERATION ) { + // no longer to start: the standing rules take over + bUphillStart = false; + return; + } + if( ( mvOccupied->LocalBrake != TLocalBrake::ManualBrake ) + && ( mvOccupied->LocalBrakePosA > 0.0 ) ) { + cue_action( driver_hint::independentbrakerelease ); + } + auto tractiveforce { 0.0 }; + for( auto *vehicle { pVehicles[ end::front ] }; vehicle != nullptr; vehicle = vehicle->Next() ) { + tractiveforce += std::max( 0.0, vehicle->MoverParameters->Ft ); + } + 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; + } +} + void TController::increase_tractive_force() { if( fActionTime < 0.0 ) { return; } // gdy jest nakaz poczekać z jazdą, to nie ruszać @@ -8285,7 +8347,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) && // also brake if uphill and slipping back + || fAccGravity < -0.05 && velocity < -0.1 && false == bUphillStart) && // 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) @@ -8401,7 +8463,10 @@ void TController::control_releaser() { // 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 if( mvOccupied->BrakePress > 0.4 - && mvOccupied->Hamulec->GetCRP() > 4.9 + && ( mvOccupied->Hamulec->GetCRP() > 4.9 + // starting uphill the train brake stays applied on purpose, and the locomotive's own share of it has to go + // for the power to come on (asynchronous drives and the pressure switch cut the power while it is applied) + || bUphillStart ) && mvOccupied->Vel < 1.0 ) { actuate = true; } diff --git a/vehicle/Driver.h b/vehicle/Driver.h index 82899493a..895e638c5 100644 --- a/vehicle/Driver.h +++ b/vehicle/Driver.h @@ -314,6 +314,7 @@ private: double brake_increase_margin() const; bool uphill_stop_hold() const; bool uphill_brake_committed( double const Velocity ) const; + void control_uphill_start(); void update_timers( double const dt ); void update_logs( double const dt ); @@ -408,6 +409,8 @@ private: double fShortfall = 0.0; // how far the train fell short of the braking demand at the last check [m/s2] double fShortfallTime = 0.0; // when that was checked 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 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 From 292092a7aa63184f62c7ff619b7ea3c2385cceff Mon Sep 17 00:00:00 2001 From: pwarszawski Date: Sat, 7 Nov 2026 21:40:02 +0100 Subject: [PATCH 3/7] AI braking: take care of the Sonar remarks on the new braking code No change in what the AI does; only the structure of the code that came in with the braking changes: - S107: shaped_braking_demand() takes the consist brake table as a struct instead of four separate parameters; its lambdas capture explicitly. - S3358: brake_response_horizon() picks the BrakeDelay entry without a nested conditional operator. - S5276: the speed passed to braking_distance_multiplier() is converted to float explicitly, as it was implicitly before. - S134: the stop case of the proximity range is an else-if of its own, and its body a method of its own, adjust_desired_speed_for_final_approach(), with early returns instead of nested ifs. The cargo train threshold push is a conditional subtraction instead of a nested if. - S3776: pick_optimal_speed() hands the uncoupling stages and the uphill stand to set_velocity_for_uncoupling() and hold_train_coming_to_stand_uphill(). --- vehicle/Driver.cpp | 257 ++++++++++++++++++++++++--------------------- vehicle/Driver.h | 3 + 2 files changed, 141 insertions(+), 119 deletions(-) diff --git a/vehicle/Driver.cpp b/vehicle/Driver.cpp index 303560e98..cbdd97c62 100644 --- a/vehicle/Driver.cpp +++ b/vehicle/Driver.cpp @@ -7239,6 +7239,59 @@ TController::handle_orders() { UpdateChangeDirection(); } +// the stages of uncoupling: each one sets the speed for the next +void +TController::set_velocity_for_uncoupling() { + + if( iVehicleCount < 0 ) { + // 4th stage: restore initial direction + if( ( iDrivigFlags & movePress ) != 0 ) { + SetVelocity( 0, 0, stopJoin ); + } + return; + } + // 3rd stage: compress buffers and uncouple + if( ( iDrivigFlags & movePress ) != 0 && iDirection == iDirectionOrder ) { + SetVelocity( 2, 0 ); // jazda w ustawionym kierunku z prędkością 2 + return; + } + // 1st stage: bring it to stop + // 2nd stage: apply consist brakes and change direction + SetVelocity( 0, 0, stopJoin ); +} + +// uphill, the gradient alone brings a slow train to a stand within metres, far sooner than a released +// brake applies; so the train brake goes on while the train is coming to a stand, stays on while it +// stands, and is applied for real if the train has started to roll back +void +TController::hold_train_coming_to_stand_uphill() { + + if( ( false == is_train() ) + || ( ( OrderCurrentGet() & Obey_train ) == 0 ) + || ( VelNext != 0.0 ) + || ( ActualProximityDist > fMaxProximityDist ) + || ( fAccGravity >= -0.025 ) ) { + return; + } + auto const vel { DirectionalVel() }; + if( std::abs( vel ) < EU07_AI_NOMOVEMENT ) { + VelDesired = 0.0; + AccDesired = std::min( AccDesired, EU07_AI_NOACCELERATION ); + return; + } + if( ( vel < 0.0 ) + || ( ( vel < 5.0 ) && ( AccDesired < 0.0 ) ) + // rolling to a stand on the gradient alone, the brake has to be on its way before the train stops: + // once the time left to the stand is no longer than half of what the brake takes to apply, apply it; any + // sooner and the brake takes off more of the distance than the stop was planned with + || ( ( BrakeSystem == TBrakeSystem::Pneumatic ) + && ( vel / 3.6 / -fAccGravity <= 0.5 * brake_response_horizon( mvOccupied, BrakeSystem, 1.0, fBrake_a0[ 0 ] ) ) ) ) { + // the gradient alone already slows the train down nearly this much; ask for enough on top of it that + // the brake is applied for sure, not held back by the margin against small corrections + AccDesired = std::min( AccDesired, fAccGravity - std::max( 0.15, fBrake_a0[ 0 ] ) - brake_increase_margin() ); + } +} + void TController::pick_optimal_speed( double const Range ) { @@ -7274,23 +7327,7 @@ TController::pick_optimal_speed( double const Range ) { } // uncoupling mode changes velocity/acceleration between stages if( ( OrderCurrentGet() & Disconnect ) != 0 ) { - if( iVehicleCount >= 0 ) { - // 3rd stage: compress buffers and uncouple - if( (iDrivigFlags & movePress) != 0 && iDirection == iDirectionOrder ) { - SetVelocity( 2, 0 ); // jazda w ustawionym kierunku z prędkością 2 - } - // 1st stage: bring it to stop - // 2nd stage: apply consist brakes and change direction - else { - SetVelocity( 0, 0, stopJoin ); - } - } - else { - // 4th stage: restore initial direction - if( ( iDrivigFlags & movePress ) != 0 ) { - SetVelocity( 0, 0, stopJoin ); - } - } + set_velocity_for_uncoupling(); } VelLimitLastDist = { VelDesired, -1 }; @@ -7343,32 +7380,8 @@ TController::pick_optimal_speed( double const Range ) { is_car() ? -2.0 : -0.9, is_car() ? 2.0 : 0.9 ); - if( is_train() - && ( ( OrderCurrentGet() & Obey_train ) != 0 ) - && ( VelNext == 0.0 ) - && ( ActualProximityDist <= fMaxProximityDist ) - && ( fAccGravity < -0.025 ) ) { - // uphill, the gradient alone brings a slow train to a stand within metres, far sooner than a released - // brake applies; so the train brake goes on while the train is coming to a stand, stays on while it - // stands, and is applied for real if the train has started to roll back. done last, so that none of - // the adjustments above, made for a train moving forward, can undo it - auto const vel { DirectionalVel() }; - if( std::abs( vel ) < EU07_AI_NOMOVEMENT ) { - VelDesired = 0.0; - AccDesired = std::min( AccDesired, EU07_AI_NOACCELERATION ); - } - else if( ( vel < 0.0 ) - || ( ( vel < 5.0 ) && ( AccDesired < 0.0 ) ) - // rolling to a stand on the gradient alone, the brake has to be on its way before the train stops: - // once the time left to the stand is no longer than half of what the brake takes to apply, apply it; any - // sooner and the brake takes off more of the distance than the stop was planned with - || ( ( BrakeSystem == TBrakeSystem::Pneumatic ) - && ( vel / 3.6 / -fAccGravity <= 0.5 * brake_response_horizon( mvOccupied, BrakeSystem, 1.0, fBrake_a0[ 0 ] ) ) ) ) { - // the gradient alone already slows the train down nearly this much; ask for enough on top of it that - // the brake is applied for sure, not held back by the margin against small corrections - AccDesired = std::min( AccDesired, fAccGravity - std::max( 0.15, fBrake_a0[ 0 ] ) - brake_increase_margin() ); - } - } + // done last, so that none of the adjustments above, made for a train moving forward, can undo it + hold_train_coming_to_stand_uphill(); // if the route ahead is blocked we might need to head the other way check_route_behind( 1000 ); // NOTE: legacy scan range value @@ -7543,13 +7556,10 @@ brake_response_horizon( TMoverParameters const *Vehicle, TBrakeSystem const Brak auto horizon { ( 1.0 - Brakea0 ) * 30.0 / 3.6 }; if( Brakesystem == TBrakeSystem::Pneumatic ) { + // BrakeDelay holds the release and the application time in G, then the same two in P + auto const delays { Vehicle->BrakeDelayFlag > bdelay_G ? 0 : 2 }; auto const isrelease { Acceleration < 0.0 }; - horizon = std::max( - horizon, - 0.5 * ( - Vehicle->BrakeDelayFlag > bdelay_G ? - Vehicle->BrakeDelay[ isrelease ? 0 : 1 ] : - Vehicle->BrakeDelay[ isrelease ? 2 : 3 ] ) ); + horizon = std::max( horizon, 0.5 * Vehicle->BrakeDelay[ delays + ( isrelease ? 0 : 1 ) ] ); } return horizon; } @@ -7569,15 +7579,26 @@ uphill_coasting_speed( double const Distance, double const Gravity ) { // 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( 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, - double const Vmax, double const *Brakea0, double const *Brakea1, int const Tablesize ) { + consist_brake_table const &Table ) { - auto const fullbraking = [&]( double const Kmh ) { - auto const index { std::clamp( static_cast( Tablesize * Kmh / std::max( 1.0, Vmax ) ), 1, Tablesize ) }; - return std::max( 0.01, Brakea0[ index ] + 12.0 * Brakea1[ index ] ); }; - auto const travel = [&]( double const Ratio ) { + 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 }; @@ -7603,6 +7624,53 @@ shaped_braking_demand( return Gravity - high * fullbraking( Velocity ); } +// the final approach to a stop, inside the proximity range: Vel is the present speed in the direction of travel +void +TController::adjust_desired_speed_for_final_approach( double const Vel ) { + + if( ( mvOccupied->CategoryFlag & 1 ) == 0 ) { + // for cars (and others) coast at low speed until we hit min proximity range + VelDesired = min_speed( VelDesired, 5.0 ); + return; + } + // trains + if( ( OrderCurrentGet() & ( Shunt | Connect ) ) + && ( Obstacle.distance < 50 ) ) { + // crude detection of edge case, if approaching another vehicle coast slowly until min distance + // this should allow to bunch up trainsets more on sidings + VelDesired = min_speed( 5.0, VelDesired ); + return; + } + // hamowanie tak, aby stanąć + if( ( OrderCurrentGet() & Obey_train ) == 0 ) { + VelDesired = VelNext; + AccDesired = ( VelNext * VelNext - Vel * Vel ) / ( 25.92 * ( ActualProximityDist + 0.1 - 0.5*fMinProximityDist ) ); + AccDesired = std::min( AccDesired, fAccThreshold ); + return; + } + // a train keeps its desired speed until the lower edge of the stopping window (a desired + // speed of 0 here asks for hard braking whatever the distance left), aims at the middle + // of the window, and asks only for what that takes + auto const stoppingroom { std::max( 0.5, ActualProximityDist - stopping_margin() ) }; + AccDesired = ( + BrakeSystem == TBrakeSystem::Pneumatic ? + // cast iron blocks grip harder as the train comes to a stand + shaped_braking_demand( + Vel, VelNext, stoppingroom, fAccGravity, + { fBrake_a0, fBrake_a1, BrakeAccTableSize, mvOccupied->Vmax } ) : + ( VelNext * VelNext - Vel * Vel ) / ( 25.92 * stoppingroom ) ); + if( ( fAccGravity < -0.025 ) + && ( AccDesired >= fAccGravity - 0.001 ) // no brake needed at all + && ( false == uphill_brake_committed( Vel ) ) + && ( ActualProximityDist - uphill_stopping_distance( Vel, fAccGravity ) > stopping_margin() ) ) { + // uphill the gradient alone would bring the train to a stand short of the + // middle of the stopping window; keep it rolling at the present speed until that changes, + // but no faster than the gradient alone can take off by the middle of the window + AccDesired = std::min( 0.0, AccPreferred ); + VelDesired = min_speed( VelDesired, uphill_coasting_speed( ActualProximityDist - stopping_margin(), fAccGravity ) ); + } +} + void TController::adjust_desired_speed_for_target_speed( double const Range ) { // ustalanie zadanego przyspieszenia @@ -7634,65 +7702,20 @@ TController::adjust_desired_speed_for_target_speed( double const Range ) { AccDesired = AccPreferred; } } + else if( ( ActualProximityDist > fMinProximityDist ) + // only a stop has a final approach of its own inside the proximity range; a lower speed ahead + // keeps the braking curve below right up to where it begins, or the train would stop braking + // (and even take power) that far out + && ( VelNext == 0.0 ) + && ( ActualProximityDist < fMaxProximityDist ) ) { + // jedzie szybciej, niż trzeba na końcu ActualProximityDist, ale jeszcze jest daleko + // jak minął już maksymalny dystans po prostu hamuj (niski stopień) + // ma stanąć, a jest w drodze hamowania albo ma jechać + adjust_desired_speed_for_final_approach( vel ); + AccDesired = std::min( AccDesired, AccPreferred ); + } else if (ActualProximityDist > fMinProximityDist) { // jedzie szybciej, niż trzeba na końcu ActualProximityDist, ale jeszcze jest daleko - // only a stop has a final approach of its own inside the proximity range; a lower speed ahead - // keeps the braking curve below right up to where it begins, or the train would stop braking - // (and even take power) that far out - if( ( VelNext == 0.0 ) && ( ActualProximityDist < fMaxProximityDist ) ) { - // jak minął już maksymalny dystans po prostu hamuj (niski stopień) - // ma stanąć, a jest w drodze hamowania albo ma jechać -/* - VelDesired = min_speed( VelDesired, VelNext ); -*/ - if( VelNext == 0.0 ) { - if( mvOccupied->CategoryFlag & 1 ) { - // trains - if( OrderCurrentGet() & (Shunt | Connect) - && Obstacle.distance < 50 ) { - // crude detection of edge case, if approaching another vehicle coast slowly until min distance - // this should allow to bunch up trainsets more on sidings - VelDesired = min_speed( 5.0, VelDesired ); - } - else { - // hamowanie tak, aby stanąć - if( ( OrderCurrentGet() & Obey_train ) == 0 ) { - VelDesired = VelNext; - AccDesired = ( VelNext * VelNext - vel * vel ) / ( 25.92 * ( ActualProximityDist + 0.1 - 0.5*fMinProximityDist ) ); - AccDesired = std::min( AccDesired, fAccThreshold ); - } - else { - // a train keeps its desired speed until the lower edge of the stopping window (a desired - // speed of 0 here asks for hard braking whatever the distance left), aims at the middle - // of the window, and asks only for what that takes - auto const stoppingroom { std::max( 0.5, ActualProximityDist - stopping_margin() ) }; - AccDesired = ( - BrakeSystem == TBrakeSystem::Pneumatic ? - // cast iron blocks grip harder as the train comes to a stand - shaped_braking_demand( - vel, VelNext, stoppingroom, fAccGravity, - mvOccupied->Vmax, fBrake_a0, fBrake_a1, BrakeAccTableSize ) : - ( VelNext * VelNext - vel * vel ) / ( 25.92 * stoppingroom ) ); - if( ( fAccGravity < -0.025 ) - && ( AccDesired >= fAccGravity - 0.001 ) // no brake needed at all - && ( false == uphill_brake_committed( vel ) ) - && ( ActualProximityDist - uphill_stopping_distance( vel, fAccGravity ) > stopping_margin() ) ) { - // uphill the gradient alone would bring the train to a stand short of the - // middle of the stopping window; keep it rolling at the present speed until that changes, - // but no faster than the gradient alone can take off by the middle of the window - AccDesired = std::min( 0.0, AccPreferred ); - VelDesired = min_speed( VelDesired, uphill_coasting_speed( ActualProximityDist - stopping_margin(), fAccGravity ) ); - } - } - } - } - else { - // for cars (and others) coast at low speed until we hit min proximity range - VelDesired = min_speed( VelDesired, 5.0 ); - } - } - } - else { // outside of max safe range AccDesired = AccPreferred; if( vel > min_speed( ActualProximityDist > 10.0 ? 10.0 : 5.0, VelDesired ) ) { @@ -7710,7 +7733,7 @@ TController::adjust_desired_speed_for_target_speed( double const Range ) { auto const brakingpointoffset = ( ( VelNext == 0.0 ) && is_train() && ( OrderCurrentGet() & Obey_train ) ? stopping_margin() : // the same stopping point as the final approach - VelNext * braking_distance_multiplier( VelNext ) ); + VelNext * braking_distance_multiplier( static_cast( VelNext ) ) ); // 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 auto velocity { vel }; @@ -7741,7 +7764,7 @@ TController::adjust_desired_speed_for_target_speed( double const Range ) { } deceleration = shaped_braking_demand( velocity, VelNext, room + 0.1 / 25.92, fAccGravity, - mvOccupied->Vmax, fBrake_a0, fBrake_a1, BrakeAccTableSize ); + { fBrake_a0, fBrake_a1, BrakeAccTableSize, mvOccupied->Vmax } ); } AccDesired = std::min( AccDesired, deceleration ); // najpierw hamuje mocniej, potem zluzuje if( vel > VelNext ) { @@ -7761,7 +7784,6 @@ TController::adjust_desired_speed_for_target_speed( double const Range ) { } } } - } AccDesired = std::min( AccDesired, AccPreferred ); } else { @@ -7885,11 +7907,8 @@ TController::adjust_desired_speed_for_current_speed() { -0.05 + fAccThreshold, std::max( EU07_AI_NOACCELERATION, AccPreferred ) ) ); // HACK: for cargo trains with high braking threshold ensure we cross that threshold - if( IsCargoTrain - && fBrake_a0[0] > 0.2 - && speedestimate > holdlimit ) { - AccDesired -= std::clamp( fBrake_a0[ 0 ] - 0.2, 0.0, 0.15 ); - } + auto const crossthreshold { IsCargoTrain && fBrake_a0[ 0 ] > 0.2 && speedestimate > holdlimit }; + AccDesired -= ( crossthreshold ? std::clamp( fBrake_a0[ 0 ] - 0.2, 0.0, 0.15 ) : 0.0 ); } // final tweaks if( vel > EU07_AI_NOMOVEMENT ) { diff --git a/vehicle/Driver.h b/vehicle/Driver.h index 895e638c5..846923df2 100644 --- a/vehicle/Driver.h +++ b/vehicle/Driver.h @@ -349,6 +349,9 @@ private: void pick_optimal_speed( double const Range ); void adjust_desired_speed_for_obstacles(); void adjust_desired_speed_for_limits(); + void set_velocity_for_uncoupling(); + void hold_train_coming_to_stand_uphill(); + void adjust_desired_speed_for_final_approach( double const Vel ); void adjust_desired_speed_for_target_speed( double const Range ); void adjust_desired_speed_for_current_speed(); void adjust_desired_speed_for_braking_test(); From 99cf46866a1c35dae84967bf86da113bb1b19541 Mon Sep 17 00:00:00 2001 From: pwarszawski Date: Sat, 7 Nov 2026 22:00:30 +0100 Subject: [PATCH 4/7] AI braking, ESt3: take care of the Sonar remarks on the braking PRs No change in what the AI or the distributor does: - S134: the step of the train brake sized from the consist brake table moves out of IncBrake() into increase_train_brake(), with early returns. - S134, S1066: while starting uphill, the check of the released brakes leaves Ready as it was by assigning the flag instead of nesting an if. - S6004: the uphill condition of control_uphill_start(), and the target cylinder pressure of TNESt3::CheckState(), are declared in the init-statement of their if. - S5350: the loop summing the tractive force walks the consist through a pointer to const. - S3776: the cylinder nozzle calibration for the AL2 relay moves out of TNESt3::SetSize() into ScaleNozzlesForAL2(). --- McZapkie/Oerlikon_ESt.cpp | 23 +++--- McZapkie/Oerlikon_ESt.h | 1 + vehicle/Driver.cpp | 152 +++++++++++++++++++------------------- vehicle/Driver.h | 1 + 4 files changed, 94 insertions(+), 83 deletions(-) diff --git a/McZapkie/Oerlikon_ESt.cpp b/McZapkie/Oerlikon_ESt.cpp index 1e7a5be66..0717eff5d 100644 --- a/McZapkie/Oerlikon_ESt.cpp +++ b/McZapkie/Oerlikon_ESt.cpp @@ -379,8 +379,7 @@ void TNESt3::CheckState(double const BCP, double & /*dV1*/) // glowny przyrzad r Przys_blok = false; // sprawdzanie stanu - double const cel{ CelBCP( CVP, VVP ) }; - if (BCP < cel - 0.01 * BVM && Przys_blok) + if (double const cel{ CelBCP( CVP, VVP ) }; BCP < cel - 0.01 * BVM && Przys_blok) BrakeStatus |= b_on | b_hld; // hamowanie stopniowe; else if (BCP - 0.1 > cel + 0.01 * BVM) BrakeStatus &= ~( b_on | b_hld ); // luzowanie; @@ -486,6 +485,18 @@ void TNESt3::SetLBP(double const P) LBP = P; } +void TNESt3::ScaleNozzlesForAL2() // dysze cylindra przy przekladniku AL2 +{ + if( PrzekCiagly == nullptr ) { + return; + } + // dysze cylindra skalibrowane na komore impulsowa przekladnika AL2 (1 l) wg czasow z tablicy 3-9 + Nozzles[dON] *= 0.914; + Nozzles[dTN] *= 0.885; + Nozzles[dOO] *= 0.82; + Nozzles[dTO] *= 0.8; +} + void TNESt3::SetSize( int const size, std::string const ¶ms ) // ustawianie dysz (rozmiaru ZR) { static double const dNO1l = 1.250; @@ -673,13 +684,7 @@ void TNESt3::SetSize( int const size, std::string const ¶ms ) // ustawianie Nozzles[dSd] = 1.1; Nozzles[dSm] = 0.9; - if( PrzekCiagly != nullptr ) { - // dysze cylindra skalibrowane na komore impulsowa przekladnika AL2 (1 l) wg czasow z tablicy 3-9 - Nozzles[dON] *= 0.914; - Nozzles[dTN] *= 0.885; - Nozzles[dOO] *= 0.82; - Nozzles[dTO] *= 0.8; - } + ScaleNozzlesForAL2(); // przeliczanie z mm^2 na l/m for (auto &nozzle : Nozzles) diff --git a/McZapkie/Oerlikon_ESt.h b/McZapkie/Oerlikon_ESt.h index 02d65a285..eaae2c032 100644 --- a/McZapkie/Oerlikon_ESt.h +++ b/McZapkie/Oerlikon_ESt.h @@ -201,6 +201,7 @@ class TNESt3 : public TBrake { void CheckState(double const BCP, double &dV1); // glowny przyrzad rozrzadczy void CheckReleaser(double const dt); // odluzniacz double CVs(double const BP) const; // napelniacz sterujacego + void ScaleNozzlesForAL2(); // dysze cylindra przy przekladniku AL2 double BVs(double const BCP) const; // napelniacz pomocniczego void SetSize( int const size, std::string const ¶ms ); // ustawianie dysz (rozmiaru ZR), przekladniki void PLC(double const mass) override; // wspolczynnik cisnienia przystawki wazacej diff --git a/vehicle/Driver.cpp b/vehicle/Driver.cpp index cbdd97c62..5ac1ccd12 100644 --- a/vehicle/Driver.cpp +++ b/vehicle/Driver.cpp @@ -2934,6 +2934,75 @@ bool TController::ReleaseEngine() { // defined further down, with the braking demand it is used for static double brake_response_horizon( TMoverParameters const *Vehicle, TBrakeSystem const Brakesystem, double const Acceleration, double const Brakea0 ); +// one step more of the train brake, sized from the consist brake table against the deceleration asked for +bool +TController::increase_train_brake() { + + // dodane dla towarowego + float pos_corr = 0; + + TDynamicObject *d; + d = pVehicles[0]; // pojazd na czele składu + while (d) + { // przeliczanie dodatkowego potrzebnego spadku ciśnienia + if( ( d->MoverParameters->Hamulec->GetBrakeStatus() & b_dmg ) == 0 ) { + pos_corr -= ( std::min( 5.0, d->MoverParameters->Hamulec->GetCRP() ) - 5.0 ) * d->MoverParameters->TotalMass; + } + d = d->Next(); // kolejny pojazd, podłączony od tyłu (licząc od czoła) + } + pos_corr = pos_corr / fMass * 2.5; + + if (mvOccupied->BrakeHandle == TBrakeHandle::FV4a) + { + pos_corr += mvOccupied->Handle->GetCP()*0.2; + + } + double deltaAcc = -AccDesired*BrakeAccFactor() - (fBrake_a0[0] + 4.0 * (/*GBH mvOccupied->BrakeCtrlPosR*/BrakeCtrlPosition - 1 - pos_corr)*fBrake_a1[0]); + + if( deltaAcc > brake_increase_margin() ) + { + if( /*GBH mvOccupied->BrakeCtrlPosR*/BrakeCtrlPosition < 0.1 ) { + auto const OK { /*mvOccupied->*/BrakeLevelAdd( BrakingInitialLevel ) }; //GBH + // HACK: stronger braking to overcome SA134 engine behaviour + if( is_dmu() + && VelNext == 0.0 + && fBrakeDist < 200.0 ) { + BrakeLevelAdd( + fBrakeDist / ActualProximityDist < 0.8 ? + 0.5 : + 1.0 ); + } + return OK; + } + auto const OK { /*mvOccupied->*/BrakeLevelAdd( BrakingLevelIncrease ) }; //GBH + // brake harder if the acceleration is much higher than desired + /*if( ( deltaAcc > 2 * fBrake_a1[ 0 ] ) + && ( mvOccupied->BrakeCtrlPosR + BrakingLevelIncrease <= 5.0 ) ) { + mvOccupied->BrakeLevelAdd( BrakingLevelIncrease ); + } GBH */ + if( deltaAcc > 2 * fBrake_a1[0] + && BrakeCtrlPosition + BrakingLevelIncrease <= 5.0 ) { + /*mvOccupied->*/BrakeLevelAdd( BrakingLevelIncrease ); + } + return OK; + } + if( BrakeCtrlPosition > 0.0 + && AccDesired < 0.0 // only while braking is asked for, not when called for a train slipping back while starting + && ( fBrakeHeldTime > ( + mvOccupied->BrakeDelayFlag > bdelay_G ? + mvOccupied->BrakeDelay[ 1 ] : + mvOccupied->BrakeDelay[ 3 ] ) + // a train falling further behind needs no full fill time to be seen not to get enough + || ( ( fShortfall > 0.02 ) + && ( fBrakeHeldTime > brake_response_horizon( mvOccupied, BrakeSystem, 1.0, fBrake_a0[ 0 ] ) ) ) ) ) { + // the table says the present position is enough, but the brake has had its full fill + // time since the handle last moved and the train is still not slowing down as asked: + // believe the train rather than the table + return BrakeLevelAdd( BrakingLevelIncrease ); + } + return false; +} + bool TController::IncBrake() { // zwiększenie hamowania auto OK { false }; @@ -3025,70 +3094,7 @@ bool TController::IncBrake() OK = false; } else { - // dodane dla towarowego - float pos_corr = 0; - - TDynamicObject *d; - d = pVehicles[0]; // pojazd na czele składu - while (d) - { // przeliczanie dodatkowego potrzebnego spadku ciśnienia - if( ( d->MoverParameters->Hamulec->GetBrakeStatus() & b_dmg ) == 0 ) { - pos_corr -= ( std::min( 5.0, d->MoverParameters->Hamulec->GetCRP() ) - 5.0 ) * d->MoverParameters->TotalMass; - } - d = d->Next(); // kolejny pojazd, podłączony od tyłu (licząc od czoła) - } - pos_corr = pos_corr / fMass * 2.5; - - if (mvOccupied->BrakeHandle == TBrakeHandle::FV4a) - { - pos_corr += mvOccupied->Handle->GetCP()*0.2; - - } - double deltaAcc = -AccDesired*BrakeAccFactor() - (fBrake_a0[0] + 4.0 * (/*GBH mvOccupied->BrakeCtrlPosR*/BrakeCtrlPosition - 1 - pos_corr)*fBrake_a1[0]); - - if( deltaAcc > brake_increase_margin() ) - { - if( /*GBH mvOccupied->BrakeCtrlPosR*/BrakeCtrlPosition < 0.1 ) { - OK = /*mvOccupied->*/BrakeLevelAdd( BrakingInitialLevel ); //GBH - // HACK: stronger braking to overcome SA134 engine behaviour - if( is_dmu() - && VelNext == 0.0 - && fBrakeDist < 200.0 ) { - BrakeLevelAdd( - fBrakeDist / ActualProximityDist < 0.8 ? - 0.5 : - 1.0 ); - } - } - else { - OK = /*mvOccupied->*/BrakeLevelAdd( BrakingLevelIncrease ); //GBH - // brake harder if the acceleration is much higher than desired - /*if( ( deltaAcc > 2 * fBrake_a1[ 0 ] ) - && ( mvOccupied->BrakeCtrlPosR + BrakingLevelIncrease <= 5.0 ) ) { - mvOccupied->BrakeLevelAdd( BrakingLevelIncrease ); - } GBH */ - if( deltaAcc > 2 * fBrake_a1[0] - && BrakeCtrlPosition + BrakingLevelIncrease <= 5.0 ) { - /*mvOccupied->*/BrakeLevelAdd( BrakingLevelIncrease ); - } - } - } - else if( BrakeCtrlPosition > 0.0 - && AccDesired < 0.0 // only while braking is asked for, not when called for a train slipping back while starting - && ( fBrakeHeldTime > ( - mvOccupied->BrakeDelayFlag > bdelay_G ? - mvOccupied->BrakeDelay[ 1 ] : - mvOccupied->BrakeDelay[ 3 ] ) - // a train falling further behind needs no full fill time to be seen not to get enough - || ( ( fShortfall > 0.02 ) - && ( fBrakeHeldTime > brake_response_horizon( mvOccupied, BrakeSystem, 1.0, fBrake_a0[ 0 ] ) ) ) ) ) { - // the table says the present position is enough, but the brake has had its full fill - // time since the handle last moved and the train is still not slowing down as asked: - // believe the train rather than the table - OK = BrakeLevelAdd( BrakingLevelIncrease ); - } - else - OK = false; + OK = increase_train_brake(); } } if( /*GBH mvOccupied->BrakeCtrlPos*/ @@ -5964,9 +5970,7 @@ TController::determine_consist_state() { // 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 - if( false == heldforstart ) { - Ready = false; - } + Ready = heldforstart; // ready all the same only when the power goes on against the brake on purpose } // 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 @@ -8196,12 +8200,12 @@ bool TController::is_too_fast_uphill() const { void TController::control_uphill_start() { auto const velocity { DirectionalVel() }; - auto const uphill { - ( BrakeSystem == TBrakeSystem::Pneumatic ) - && is_train() - && ( false == is_emu() ) && ( false == is_dmu() ) // a multiple unit has no locomotive to hold a train behind it - && ( fAccGravity < -0.01 ) }; - if( false == uphill ) { + if( auto const uphill { + ( BrakeSystem == TBrakeSystem::Pneumatic ) + && is_train() + && ( false == is_emu() ) && ( false == is_dmu() ) // a multiple unit has no locomotive to hold a train behind it + && ( fAccGravity < -0.01 ) }; + false == uphill ) { bUphillStart = false; return; } @@ -8227,7 +8231,7 @@ void TController::control_uphill_start() { cue_action( driver_hint::independentbrakerelease ); } auto tractiveforce { 0.0 }; - for( auto *vehicle { pVehicles[ end::front ] }; vehicle != nullptr; vehicle = vehicle->Next() ) { + for( auto const *vehicle { pVehicles[ end::front ] }; vehicle != nullptr; vehicle = vehicle->Next() ) { tractiveforce += std::max( 0.0, vehicle->MoverParameters->Ft ); } auto const holdingforce { fMass * -fAccGravity }; diff --git a/vehicle/Driver.h b/vehicle/Driver.h index 846923df2..700b41f75 100644 --- a/vehicle/Driver.h +++ b/vehicle/Driver.h @@ -350,6 +350,7 @@ private: void adjust_desired_speed_for_obstacles(); void adjust_desired_speed_for_limits(); void set_velocity_for_uncoupling(); + bool increase_train_brake(); void hold_train_coming_to_stand_uphill(); void adjust_desired_speed_for_final_approach( double const Vel ); void adjust_desired_speed_for_target_speed( double const Range ); From 95ceb815d2f4e9d8b87ed97c0482250256ecf1c4 Mon Sep 17 00:00:00 2001 From: pwarszawski Date: Sat, 7 Nov 2026 22:55:09 +0100 Subject: [PATCH 5/7] AI braking: the braking curve outside the final approach in a method of its own Takes care of the last two Sonar remarks, with no change in what the AI does: - S134: the braking curve followed outside the final approach moves out of adjust_desired_speed_for_target_speed() into adjust_desired_speed_for_braking_curve(), with early returns. - S6004: deltaAcc of increase_train_brake() is declared in the init-statement of its if. --- vehicle/Driver.cpp | 151 ++++++++++++++++++++++++--------------------- vehicle/Driver.h | 1 + 2 files changed, 81 insertions(+), 71 deletions(-) diff --git a/vehicle/Driver.cpp b/vehicle/Driver.cpp index 5ac1ccd12..92a693532 100644 --- a/vehicle/Driver.cpp +++ b/vehicle/Driver.cpp @@ -2957,9 +2957,8 @@ TController::increase_train_brake() { pos_corr += mvOccupied->Handle->GetCP()*0.2; } - double deltaAcc = -AccDesired*BrakeAccFactor() - (fBrake_a0[0] + 4.0 * (/*GBH mvOccupied->BrakeCtrlPosR*/BrakeCtrlPosition - 1 - pos_corr)*fBrake_a1[0]); - - if( deltaAcc > brake_increase_margin() ) + if( double const deltaAcc = -AccDesired*BrakeAccFactor() - (fBrake_a0[0] + 4.0 * (/*GBH mvOccupied->BrakeCtrlPosR*/BrakeCtrlPosition - 1 - pos_corr)*fBrake_a1[0]); + deltaAcc > brake_increase_margin() ) { if( /*GBH mvOccupied->BrakeCtrlPosR*/BrakeCtrlPosition < 0.1 ) { auto const OK { /*mvOccupied->*/BrakeLevelAdd( BrakingInitialLevel ) }; //GBH @@ -7628,6 +7627,83 @@ shaped_braking_demand( 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 +// direction of travel +void +TController::adjust_desired_speed_for_braking_curve( double const Vel ) { + + AccDesired = AccPreferred; + if( Vel <= min_speed( ActualProximityDist > 10.0 ? 10.0 : 5.0, VelDesired ) ) { + return; + } + // allow to coast at reasonably low speed + auto const brakingdistance { fBrakeDist * braking_distance_multiplier( VelNext ) }; + auto const slowdowndistance { ( + mvOccupied->CategoryFlag == 2 ? // cars can stop on a dime, for bigger vehicles we enforce some minimal braking distance + brakingdistance : + std::max( + (OrderCurrentGet() & Connect) == 0 ? 100.0 : 25.0, + brakingdistance ) ) }; + if( brakingdistance + std::max(slowdowndistance, fMaxProximityDist) < ActualProximityDist - fMaxProximityDist ) { + // don't slow down prematurely; as long as we have room to come to a full stop at a safe distance, we're good + return; + } + // ensure some minimal coasting speed, otherwise a vehicle entering this zone at very low speed will be crawling forever + auto const brakingpointoffset = ( + ( VelNext == 0.0 ) && is_train() && ( OrderCurrentGet() & Obey_train ) ? + stopping_margin() : // the same stopping point as the final approach + VelNext * braking_distance_multiplier( static_cast( VelNext ) ) ); + // 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 + auto velocity { Vel }; + auto distance { ActualProximityDist }; + if( ( BrakeSystem == TBrakeSystem::Pneumatic ) + && ( AbsAccS > 0.0 ) ) { + auto const horizon { brake_response_horizon( mvOccupied, BrakeSystem, AbsAccS, fBrake_a0[ 0 ] ) }; + velocity = Vel + 3.6 * horizon * AbsAccS; + distance = std::max( 0.5 * ActualProximityDist, ActualProximityDist - ( Vel + velocity ) * 0.5 / 3.6 * horizon ); + } + auto const brakingroom { + std::max( + distance - brakingpointoffset, + 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 + room = std::max( + 0.5 * brakingroom, + brakingroom - Vel / 3.6 * brake_response_horizon( mvOccupied, BrakeSystem, 1.0, fBrake_a0[ 0 ] ) ); + } + 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 + if( Vel <= VelNext ) { + return; + } + if( deceleration < fAccGravity - 0.001 ) { + // no power while still above the speed being braked for + AccDesired = std::min( AccDesired, EU07_AI_NOACCELERATION ); + } + else if( ( fAccGravity < -0.025 ) + && ( ( OrderCurrentGet() & Obey_train ) != 0 ) + && ( false == uphill_brake_committed( Vel ) ) + && ( ActualProximityDist - uphill_stopping_distance( Vel, fAccGravity ) > brakingpointoffset ) ) { + // uphill the gradient alone slows the train down more than it is asked to; + // keep it rolling at the present speed until that changes + AccDesired = std::min( 0.0, AccPreferred ); + VelDesired = min_speed( VelDesired, uphill_coasting_speed( ActualProximityDist - brakingpointoffset, fAccGravity ) ); + } +} + // the final approach to a stop, inside the proximity range: Vel is the present speed in the direction of travel void TController::adjust_desired_speed_for_final_approach( double const Vel ) { @@ -7720,74 +7796,7 @@ TController::adjust_desired_speed_for_target_speed( double const Range ) { } else if (ActualProximityDist > fMinProximityDist) { // jedzie szybciej, niż trzeba na końcu ActualProximityDist, ale jeszcze jest daleko - // outside of max safe range - AccDesired = AccPreferred; - if( vel > min_speed( ActualProximityDist > 10.0 ? 10.0 : 5.0, VelDesired ) ) { - // allow to coast at reasonably low speed - auto const brakingdistance { fBrakeDist * braking_distance_multiplier( VelNext ) }; - auto const slowdowndistance { ( - mvOccupied->CategoryFlag == 2 ? // cars can stop on a dime, for bigger vehicles we enforce some minimal braking distance - brakingdistance : - std::max( - (OrderCurrentGet() & Connect) == 0 ? 100.0 : 25.0, - brakingdistance ) ) }; - if( brakingdistance + std::max(slowdowndistance, fMaxProximityDist) >= ActualProximityDist - fMaxProximityDist ) { - // don't slow down prematurely; as long as we have room to come to a full stop at a safe distance, we're good - // ensure some minimal coasting speed, otherwise a vehicle entering this zone at very low speed will be crawling forever - auto const brakingpointoffset = ( - ( VelNext == 0.0 ) && is_train() && ( OrderCurrentGet() & Obey_train ) ? - stopping_margin() : // the same stopping point as the final approach - VelNext * braking_distance_multiplier( static_cast( VelNext ) ) ); - // 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 - auto velocity { vel }; - auto distance { ActualProximityDist }; - if( ( BrakeSystem == TBrakeSystem::Pneumatic ) - && ( AbsAccS > 0.0 ) ) { - auto const horizon { brake_response_horizon( mvOccupied, BrakeSystem, AbsAccS, fBrake_a0[ 0 ] ) }; - velocity = vel + 3.6 * horizon * AbsAccS; - distance = std::max( 0.5 * ActualProximityDist, ActualProximityDist - ( vel + velocity ) * 0.5 / 3.6 * horizon ); - } - auto const brakingroom { - std::max( - distance - brakingpointoffset, - 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 - room = std::max( - 0.5 * brakingroom, - brakingroom - vel / 3.6 * brake_response_horizon( mvOccupied, BrakeSystem, 1.0, fBrake_a0[ 0 ] ) ); - } - 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 - if( vel > VelNext ) { - if( deceleration < fAccGravity - 0.001 ) { - // no power while still above the speed being braked for - AccDesired = std::min( AccDesired, EU07_AI_NOACCELERATION ); - } - else if( ( fAccGravity < -0.025 ) - && ( ( OrderCurrentGet() & Obey_train ) != 0 ) - && ( false == uphill_brake_committed( vel ) ) - && ( ActualProximityDist - uphill_stopping_distance( vel, fAccGravity ) > brakingpointoffset ) ) { - // uphill the gradient alone slows the train down more than it is asked to; - // keep it rolling at the present speed until that changes - AccDesired = std::min( 0.0, AccPreferred ); - VelDesired = min_speed( VelDesired, uphill_coasting_speed( ActualProximityDist - brakingpointoffset, fAccGravity ) ); - } - } - } - } + adjust_desired_speed_for_braking_curve( vel ); AccDesired = std::min( AccDesired, AccPreferred ); } else { diff --git a/vehicle/Driver.h b/vehicle/Driver.h index 700b41f75..7698fedb7 100644 --- a/vehicle/Driver.h +++ b/vehicle/Driver.h @@ -353,6 +353,7 @@ private: bool increase_train_brake(); void hold_train_coming_to_stand_uphill(); void adjust_desired_speed_for_final_approach( double const Vel ); + void adjust_desired_speed_for_braking_curve( double const Vel ); void adjust_desired_speed_for_target_speed( double const Range ); void adjust_desired_speed_for_current_speed(); void adjust_desired_speed_for_braking_test(); From b992df7ab796fddaab97979f6f979aac6636519e Mon Sep 17 00:00:00 2001 From: pwarszawski Date: Sat, 7 Nov 2026 23:53:02 +0100 Subject: [PATCH 6/7] AI braking: slowdowndistance in the init-statement of its if (Sonar S6004) --- vehicle/Driver.cpp | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/vehicle/Driver.cpp b/vehicle/Driver.cpp index 92a693532..e27dff946 100644 --- a/vehicle/Driver.cpp +++ b/vehicle/Driver.cpp @@ -7638,13 +7638,13 @@ TController::adjust_desired_speed_for_braking_curve( double const Vel ) { } // allow to coast at reasonably low speed auto const brakingdistance { fBrakeDist * braking_distance_multiplier( VelNext ) }; - auto const slowdowndistance { ( - mvOccupied->CategoryFlag == 2 ? // cars can stop on a dime, for bigger vehicles we enforce some minimal braking distance - brakingdistance : - std::max( - (OrderCurrentGet() & Connect) == 0 ? 100.0 : 25.0, - brakingdistance ) ) }; - if( brakingdistance + std::max(slowdowndistance, fMaxProximityDist) < ActualProximityDist - fMaxProximityDist ) { + if( auto const slowdowndistance { ( + mvOccupied->CategoryFlag == 2 ? // cars can stop on a dime, for bigger vehicles we enforce some minimal braking distance + brakingdistance : + std::max( + (OrderCurrentGet() & Connect) == 0 ? 100.0 : 25.0, + brakingdistance ) ) }; + brakingdistance + std::max(slowdowndistance, fMaxProximityDist) < ActualProximityDist - fMaxProximityDist ) { // don't slow down prematurely; as long as we have room to come to a full stop at a safe distance, we're good return; } From 63fe5bde57ed6278d2c5587c76d36e80a2ca7d61 Mon Sep 17 00:00:00 2001 From: pwarszawski Date: Sun, 8 Nov 2026 15:56:32 +0100 Subject: [PATCH 7/7] AI on up gradients: what the review found - The tractive force that tells the start on the train brake is over is projected on the direction of travel: Ft is in the vehicle's own frame, so a locomotive driven from its rear cab, or turned the other way in the consist, counted as nothing. - Rolling back faster than 0.5 km/h ends the start on the train brake, so the rules for a train slipping back apply the brakes again. - One threshold for an up gradient that rolls a standing train back, EU07_AI_UPHILLGRAVITY (-0.025 m/s2, about 2.5 per mille), for the stop hold, the locomotive brake on rollback, the start, uphill_brake_committed() and hold_train_coming_to_stand_uphill(); the stop hold and the rollback rule started at 0.5 per mille before, on almost level track. - hold_train_coming_to_stand_uphill() brakes on the brake's response time only for a train that is not to pull up further. - Starting against the brake keeps the train Ready only at a standstill; in motion a braking force left in the train stops it as before. --- vehicle/Driver.cpp | 24 +++++++++++++++++------- vehicle/Driver.h | 1 + 2 files changed, 18 insertions(+), 7 deletions(-) diff --git a/vehicle/Driver.cpp b/vehicle/Driver.cpp index 4f9205bc3..726523725 100644 --- a/vehicle/Driver.cpp +++ b/vehicle/Driver.cpp @@ -6014,7 +6014,9 @@ TController::determine_consist_state() { // 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 = heldforstart; // ready all the same only when the power goes on against the brake on purpose + // 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 ); } // 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 @@ -7280,7 +7282,7 @@ TController::hold_train_coming_to_stand_uphill() { || ( ( OrderCurrentGet() & Obey_train ) == 0 ) || ( VelNext != 0.0 ) || ( ActualProximityDist > fMaxProximityDist ) - || ( fAccGravity >= -0.025 ) ) { + || ( fAccGravity >= EU07_AI_UPHILLGRAVITY ) ) { return; } auto const vel { DirectionalVel() }; @@ -7295,6 +7297,8 @@ TController::hold_train_coming_to_stand_uphill() { // once the time left to the stand is no longer than half of what the brake takes to apply, apply it; any // sooner and the brake takes off more of the distance than the stop was planned with || ( ( BrakeSystem == TBrakeSystem::Pneumatic ) + // not for a train that is short of where it is to stop and is to pull up further + && ( AccDesired <= 0.0 ) && ( vel / 3.6 / -fAccGravity <= 0.5 * brake_response_horizon( mvOccupied, BrakeSystem, 1.0, fBrake_a0[ 0 ] ) ) ) ) { // the gradient alone already slows the train down nearly this much; ask for enough on top of it that // the brake is applied for sure, not held back by the margin against small corrections @@ -8220,7 +8224,7 @@ void TController::control_uphill_start() { ( BrakeSystem == TBrakeSystem::Pneumatic ) && is_train() && ( false == is_emu() ) && ( false == is_dmu() ) // a multiple unit has no locomotive to hold a train behind it - && ( fAccGravity < -0.01 ) }; + && ( fAccGravity < EU07_AI_UPHILLGRAVITY ) }; false == uphill ) { bUphillStart = false; return; @@ -8242,13 +8246,19 @@ void TController::control_uphill_start() { bUphillStart = false; 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; + return; + } if( ( mvOccupied->LocalBrake != TLocalBrake::ManualBrake ) && ( mvOccupied->LocalBrakePosA > 0.0 ) ) { cue_action( driver_hint::independentbrakerelease ); } auto tractiveforce { 0.0 }; for( auto const *vehicle { pVehicles[ end::front ] }; vehicle != nullptr; vehicle = vehicle->Next() ) { - tractiveforce += std::max( 0.0, vehicle->MoverParameters->Ft ); + // Ft is signed in the vehicle's own frame + tractiveforce += std::max( 0.0, vehicle->MoverParameters->Ft * ( vehicle->DirectionGet() == iDirection ? 1 : -1 ) ); } auto const holdingforce { fMass * -fAccGravity }; if( ( velocity > 1.0 ) @@ -8306,7 +8316,7 @@ TController::uphill_stop_hold() const { ( BrakeSystem == TBrakeSystem::Pneumatic ) && is_train() && ( ( OrderCurrentGet() & Obey_train ) != 0 ) && ( VelNext == 0.0 ) && ( ActualProximityDist <= fMaxProximityDist ) - && ( fAccGravity < -0.005 ) + && ( fAccGravity < EU07_AI_UPHILLGRAVITY ) && ( std::abs( DirectionalVel() ) < 10.0 ) // not while the train is to be driven on && ( AccDesired <= 0.0 ) ); @@ -8321,7 +8331,7 @@ TController::uphill_brake_committed( double const Velocity ) const { return ( ( BrakeSystem == TBrakeSystem::Pneumatic ) && ( BrakeCtrlPosition > 0.0 ) - && ( fAccGravity < -0.025 ) + && ( fAccGravity < EU07_AI_UPHILLGRAVITY ) && ( Velocity / 3.6 / -fAccGravity < brake_response_horizon( mvOccupied, BrakeSystem, -1.0, fBrake_a0[ 0 ] ) ) ); } @@ -8432,7 +8442,7 @@ void TController::control_braking_force() { // a train that has slowed down short of where it is to stop and is to pull up further does not get it && ( VelDesired <= std::abs( velocity ) + 0.5 ) && ( ActualProximityDist - uphill_stopping_distance( std::abs( velocity ), fAccGravity ) <= stopping_margin() ) ) - || ( ( BrakeSystem == TBrakeSystem::Pneumatic ) && ( fAccGravity < -0.005 ) && ( velocity < -0.1 ) && ( AccDesired <= 0.0 ) ) ) ) { + || ( ( BrakeSystem == TBrakeSystem::Pneumatic ) && ( fAccGravity < EU07_AI_UPHILLGRAVITY ) && ( velocity < -0.1 ) && ( AccDesired <= 0.0 ) ) ) ) { cue_action( driver_hint::independentbrakeapply ); } // stop-gap measure to ensure cars actually brake to stop even when above calculactions go awry diff --git a/vehicle/Driver.h b/vehicle/Driver.h index 6a4db3003..4908936c0 100644 --- a/vehicle/Driver.h +++ b/vehicle/Driver.h @@ -24,6 +24,7 @@ auto const EU07_AI_NOACCELERATION = -0.05; 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_SPEEDLIMITEXTENDSBEYONDSCANRANGE = 10000.0; enum TOrders