Skip to content

Commit 2df2ab4

Browse files
committed
Add adjustment os adhesion substeps due to decreasing screen fps
1 parent 1fdec87 commit 2df2ab4

File tree

4 files changed

+39
-73
lines changed

4 files changed

+39
-73
lines changed

Source/Orts.Simulation/Simulation/Physics/Train.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,8 @@ public int ActivityDurationS
195195

196196
public bool HasControlCarWithGear = false;
197197

198+
public float ScreenFrameRate;
199+
198200
//To investigate coupler breaks on route
199201
private bool numOfCouplerBreaksNoted = false;
200202
public static int NumOfCouplerBreaks = 0;//Debrief Eval

Source/Orts.Simulation/Simulation/RollingStocks/MSTSLocomotive.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,7 @@ public enum SoundState
149149

150150
float ZeroSpeedAdhesionBase;
151151

152+
152153
public float FilteredBrakePipeFlowM3pS;
153154
public IIRFilter AFMFilter;
154155

@@ -2799,13 +2800,14 @@ public virtual void AdvancedAdhesion(float elapsedClockSeconds)
27992800

28002801
foreach (var axle in LocomotiveAxles)
28012802
{
2802-
axle.BrakeRetardForceN = BrakeRetardForceN/LocomotiveAxles.Count;
2803+
axle.BrakeRetardForceN = BrakeRetardForceN / LocomotiveAxles.Count;
28032804
axle.TrainSpeedMpS = SpeedMpS; //Set the train speed of the axle mod
28042805
axle.WheelRadiusM = DriverWheelRadiusM;
28052806
axle.WheelDistanceGaugeM = TrackGaugeM;
28062807
axle.CurrentCurveRadiusM = CurrentCurveRadiusM;
28072808
axle.BogieRigidWheelBaseM = RigidWheelBaseM;
28082809
axle.CurtiusKnifflerZeroSpeed = ZeroSpeedAdhesionBase;
2810+
axle.ScreenFrameRate = Train.ScreenFrameRate;
28092811
}
28102812

28112813
LocomotiveAxles.Update(elapsedClockSeconds);

Source/Orts.Simulation/Simulation/RollingStocks/SubSystems/PowerTransmissions/Axle.cs

Lines changed: 30 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -516,6 +516,11 @@ public float TransmissionEfficiency
516516
/// </summary>
517517
public float CurtiusKnifflerZeroSpeed;
518518

519+
/// <summary>
520+
/// Simulator FPS
521+
/// </summary>
522+
public float ScreenFrameRate;
523+
519524
/// <summary>
520525
/// Wheel adhesion as calculated by Polach
521526
/// </summary>
@@ -643,15 +648,6 @@ public void ComputeWheelSlipThresholdMpS()
643648
}
644649
}
645650
WheelSlipThresholdMpS = (float)Math.Max((a + b) / 2, MpS.FromKpH(0.1f));
646-
647-
if (IsWheelSlip)
648-
{
649-
// Trace.TraceInformation("Fx Values - a {0} fa1 {1} b {2} fb1 {3} s {4} fs {5} t {6} ft {7} u {8} fu {9} v {10} fv {11} x {12} fx {13} y {14} fy {15} Speed {16} Threshold {17} Fslip {18} SlipSpeed {19}", a, fa1, b, fb1, s, fs, t, ft, u, fu, v, fv, x, fx, y, fy, TrainSpeedMpS, WheelSlipThresholdMpS, fslip, SlipSpeedMpS);
650-
651-
// Trace.TraceInformation("Threshold Speed - {0}", WheelSlipThresholdMpS);
652-
653-
}
654-
655651
}
656652

657653
/// <summary>
@@ -878,53 +874,47 @@ void Integrate(float elapsedClockSeconds)
878874
{
879875
if (elapsedClockSeconds <= 0) return;
880876
double prevSpeedMpS = AxleSpeedMpS;
881-
/*
882-
if (Math.Abs(integratorError) > Math.Max((Math.Abs(SlipSpeedMpS) - 1) * 0.01, 0.001))
883-
{
884-
++NumOfSubstepsPS;
885-
waitBeforeSpeedingUp = 100;
877+
878+
var upperSubStepStartingLimit = 120;
879+
var upperSubStepLimit = upperSubStepStartingLimit;
880+
var lowerSubStepLimit = 1;
886881

887-
// Trace.TraceInformation("Algorithim Increase - Steps {0} Err {1}", NumOfSubstepsPS, integratorError);
882+
var screenFrameUpperLimit = 60;
883+
var screenFrameLowerLimit = 40;
888884

885+
// Reduces the number of substeps if screen FPS drops
886+
if (ScreenFrameRate >= screenFrameUpperLimit)
887+
{
888+
upperSubStepLimit = upperSubStepStartingLimit;
889+
}
890+
else if (ScreenFrameRate < screenFrameLowerLimit)
891+
{
892+
upperSubStepLimit = upperSubStepStartingLimit * (screenFrameLowerLimit / screenFrameUpperLimit);
889893
}
890894
else
891895
{
892-
if (--waitBeforeSpeedingUp <= 0) //wait for a while before speeding up the integration
893-
{
894-
--NumOfSubstepsPS;
895-
waitBeforeSpeedingUp = 10; //not so fast ;)
896-
897-
// Trace.TraceInformation("Algorithim Decrease - Steps {0} Err {1}", NumOfSubstepsPS, integratorError);
898-
}
896+
upperSubStepLimit = (int) ((ScreenFrameRate / 60) * upperSubStepStartingLimit);
899897
}
900898

901-
NumOfSubstepsPS = Math.Max(Math.Min(NumOfSubstepsPS, 130), 1);
902-
*/
903-
// use straight line graph approximation
904-
// Points are 1 = (0, upperLimit) and 2 = (threshold, lowerLimit)
905-
906-
var upperLimit = 130;
907-
var lowerLimit = 1;
908-
var AdhesGrad = ((upperLimit - lowerLimit) / (WheelSlipThresholdMpS - 0));
909-
var targetNumOfSubstepsPS = Math.Abs((AdhesGrad * SlipSpeedMpS) + lowerLimit);
899+
// use straight line graph approximation to increase substeps as slipspeed increases towards the threshold speed point
900+
// Points are 1 = (0, upperLimit) and 2 = (threshold, lowerLimit)
901+
var AdhesGrad = ((upperSubStepLimit - lowerSubStepLimit) / (WheelSlipThresholdMpS - 0));
902+
var targetNumOfSubstepsPS = Math.Abs((AdhesGrad * SlipSpeedMpS) + lowerSubStepLimit);
910903
if (float.IsNaN((float)targetNumOfSubstepsPS)) targetNumOfSubstepsPS = 1;
911904

912905
if (SlipSpeedMpS > WheelSlipThresholdMpS) // if in wheel slip then maximise the substeps
913906
{
914-
targetNumOfSubstepsPS = 130;
907+
targetNumOfSubstepsPS = upperSubStepLimit;
915908
}
916909

917-
// Trace.TraceInformation("Grad - {0} AdhesGrad {1} SlipSpeedMps {2} Threshold {3}", temp, AdhesGrad, SlipSpeedMpS, WheelSlipThresholdMpS);
918-
919910
if (Math.Abs(integratorError) < 0.000277 && !IsWheelSlip && !IsWheelSlipWarning && SlipSpeedMpS < 0.4 * WheelSlipThresholdMpS)
920911
{
921912
if (--waitBeforeIntegreationRate <= 0) //wait for a while before changing the integration rate
922913
{
923-
NumOfSubstepsPS -= 2;
914+
NumOfSubstepsPS -= 2; // decrease substeps when under low slip conditions
924915
waitBeforeIntegreationRate = 20;
925916
}
926917
}
927-
// else if (targetNumOfSubstepsPS > NumOfSubstepsPS && Math.Abs(integratorError) > Math.Max((Math.Abs(SlipSpeedMpS) - 1) * 0.01, 0.001)) // increase substeps
928918
else if (targetNumOfSubstepsPS > NumOfSubstepsPS) // increase substeps
929919
{
930920
if (--waitBeforeIntegreationRate <= 0 ) //wait for a while before changing the integration rate
@@ -942,13 +932,11 @@ void Integrate(float elapsedClockSeconds)
942932
}
943933
}
944934

945-
if (NumOfSubstepsPS < lowerLimit)
946-
NumOfSubstepsPS = lowerLimit;
935+
if (NumOfSubstepsPS < lowerSubStepLimit)
936+
NumOfSubstepsPS = lowerSubStepLimit;
947937

948-
if (NumOfSubstepsPS > upperLimit)
949-
NumOfSubstepsPS = upperLimit;
950-
951-
// Trace.TraceInformation("Grad - {0} AdhesGrad {1} SlipSpeedMps {2} Threshold {3} NumStepsPS {4} Error {5}", targetNumOfSubstepsPS, AdhesGrad, SlipSpeedMpS, WheelSlipThresholdMpS, NumOfSubstepsPS, integratorError);
938+
if (NumOfSubstepsPS > upperSubStepLimit)
939+
NumOfSubstepsPS = upperSubStepLimit;
952940

953941
double dt = elapsedClockSeconds / NumOfSubstepsPS;
954942
double hdt = dt / 2;
@@ -957,18 +945,6 @@ void Integrate(float elapsedClockSeconds)
957945
for (int i = 0; i < NumOfSubstepsPS; i++)
958946
{
959947
var k1 = GetAxleMotionVariation(AxleSpeedMpS, dt);
960-
/*
961-
if (i == 0)
962-
{
963-
if (k1.Item1 * dt > Math.Max((Math.Abs(SlipSpeedMpS) - 1) * 10, 1) / 100)
964-
{
965-
// NumOfSubstepsPS = Math.Min(NumOfSubstepsPS + 5, 130);
966-
// Trace.TraceInformation("Algorithim Change - k1 - Number of Steps {0}", NumOfSubstepsPS);
967-
dt = elapsedClockSeconds / NumOfSubstepsPS;
968-
hdt = dt / 2;
969-
}
970-
}
971-
*/
972948
var k2 = GetAxleMotionVariation(AxleSpeedMpS + k1.Item1 * hdt, hdt);
973949
var k3 = GetAxleMotionVariation(AxleSpeedMpS + k2.Item1 * hdt, hdt);
974950
var k4 = GetAxleMotionVariation(AxleSpeedMpS + k3.Item1 * dt, dt);
@@ -998,8 +974,6 @@ public virtual void Update(float timeSpan)
998974
axleStaticForceN = AxleWeightN * SlipCharacteristics(0);
999975
ComputeWheelSlipThresholdMpS();
1000976

1001-
// Trace.TraceInformation("Threshold - Threshold {0} SlipSpeed {1} Speed {2}", WheelSlipThresholdMpS, SlipSpeedMpS, TrainSpeedMpS);
1002-
1003977
if (count < 6 && count++ == 5)
1004978
{
1005979
TrainSpeedMpS = 10 / 3.6f;
@@ -1050,7 +1024,6 @@ public virtual void Update(float timeSpan)
10501024
if (WheelSlipTimeS > 1)
10511025
{
10521026
IsWheelSlip = IsWheelSlipWarning = true;
1053-
// Trace.TraceInformation("Wheel Slip Triggered");
10541027
}
10551028
WheelSlipTimeS += timeSpan;
10561029
}
@@ -1153,13 +1126,9 @@ public void Update()
11531126
if (float.IsNaN((float)Sy)) Sy = 0;//avoid NaN when first starting OR
11541127
Sy2 = Sy * Sy;
11551128

1156-
// Trace.TraceInformation("Spin - {0} FlangeAngle {1} wheelRadius {2} Speed {3}", spinM1, Axle.WheelFlangeAngleRad, wheelRadiusMM, trainSpeedMpS);
1157-
11581129
Syc = Sy + (spinM1 * a_HertzianM);
11591130
Syc2 = Syc * Syc;
11601131

1161-
// Trace.TraceInformation("Sy - {0} Syc {1} Spin {2} a_hertz {3}", Sy, Syc, spinM1, a_HertzianMM);
1162-
11631132
// calculate "standard" Polach adhesion parameters as straight line approximations as u varies - these values are capped at the moment at the u=0.3 level
11641133
// Taking them lower may reduce the stability of the calculations
11651134
polach_A = 0.4;
@@ -1176,8 +1145,6 @@ public double SlipCharacteristics(double slipSpeedMpS)
11761145
{
11771146
var polach_uadhesion = zeroSpeedAdhesion * (((1 - polach_A) * Math.Exp(-polach_B * slipSpeedMpS)) + polach_A);
11781147

1179-
// Trace.TraceInformation("Polach Adhesion - {0} ZeroAdhesion {1} RawPolach {2} SlipSpeed {3}", polach_uadhesion, zeroSpeedAdhesion, (((1 - polach_A) * Math.Exp(-polach_B * slipSpeedMpS)) + polach_A), slipSpeedMpS);
1180-
11811148
if (trainSpeedMpS < 0.05f)
11821149
return polach_uadhesion;
11831150

@@ -1205,15 +1172,6 @@ public double SlipCharacteristics(double slipSpeedMpS)
12051172

12061173
var fx = (f * Sx / Sc) / wheelLoadN;
12071174

1208-
1209-
// if (slipSpeedMpS == 0.025f || slipSpeedMpS == 0.05f || slipSpeedMpS == 0.1f || slipSpeedMpS == 0.15f || slipSpeedMpS == 0.2f)
1210-
if (Axle.IsWheelSlip)
1211-
{
1212-
// Trace.TraceInformation("Negative Fx - Fx {0} Speed {1} SlipSpeed {2} Sx {3} PolachAdhesion {4} adhesionComponent {5} slipComponent {6} Polach_Ks {7} Stiffness2 {8} SlipForce {9} Sc {10} WheelLoad {11} Syc {12} Hertz_a {13} Sy {14} Threshold {15} DriveForce {16} Steps {17} IsWheelSlip {18} IsWheelSlipWarning {19}", fx, (float)trainSpeedMpS, (float)slipSpeedMpS, (float)Sx, polach_uadhesion, adhesionComponent, slipComponent, polach_Ks, Stiffness2, f, Sc, wheelLoadN, Syc, a_HertzianMM, Sy, Axle.WheelSlipThresholdMpS, Axle.DriveForceN, Axle.NumOfSubstepsPS, Axle.IsWheelSlip, Axle.IsWheelSlipWarning);
1213-
1214-
}
1215-
1216-
12171175
return fx;
12181176
}
12191177
}

Source/RunActivity/Viewer3D/Popups/HUDWindow.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -474,6 +474,10 @@ void TextPageCommon(TableData table)
474474
TableAddLabelValue(table, Viewer.Catalog.GetString("FPS"), "{0:F0}", Viewer.RenderProcess.FrameRate.SmoothedValue);
475475
TableAddLine(table);
476476

477+
// Used in axle module to adjust substeps for frame rate drops
478+
var mstsLocomotive = Viewer.PlayerLocomotive as MSTSLocomotive;
479+
Viewer.PlayerTrain.ScreenFrameRate = Viewer.RenderProcess.FrameRate.SmoothedValue;
480+
477481
if (Viewer.PlayerLocomotive.Train.TrainType == Train.TRAINTYPE.AI_PLAYERHOSTING)
478482
TableAddLine(table, Viewer.Catalog.GetString("Autopilot") + "???");
479483

0 commit comments

Comments
 (0)