Skip to content

Commit 99a4650

Browse files
committed
Rename speed delta function mode
1 parent 607b9c6 commit 99a4650

File tree

1 file changed

+25
-11
lines changed

1 file changed

+25
-11
lines changed

Source/Orts.Simulation/Simulation/RollingStocks/SubSystems/CruiseControl.cs

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
using Orts.Simulation.RollingStocks.SubSystems.Controllers;
2727
using ORTS.Common;
2828
using ORTS.Scripting.Api;
29+
using static Orts.Simulation.RollingStocks.TrainCar;
2930
namespace Orts.Simulation.RollingStocks.SubSystems
3031
{
3132
public class CruiseControl
@@ -200,7 +201,12 @@ public enum SpeedSelectorMode { Parking, Neutral, On, Start }
200201
public bool WasBraking = false;
201202
public bool WasForceReset = true;
202203

203-
protected float DeltaAccelerationExponent = 0.5f;
204+
protected enum SpeedDeltaMode
205+
{
206+
Linear,
207+
Sqrt
208+
}
209+
protected SpeedDeltaMode SpeedDeltaFunctionMode = SpeedDeltaMode.Sqrt;
204210

205211
AccelerationController ThrottlePID;
206212
AccelerationController DynamicBrakePID;
@@ -245,7 +251,7 @@ public CruiseControl(CruiseControl other, MSTSLocomotive locomotive)
245251
MaxForceSelectorIsDiscrete = other.MaxForceSelectorIsDiscrete;
246252
SpeedRegulatorOptions = other.SpeedRegulatorOptions;
247253
CruiseControlLogic = other.CruiseControlLogic;
248-
DeltaAccelerationExponent = other.DeltaAccelerationExponent;
254+
SpeedDeltaFunctionMode = other.SpeedDeltaFunctionMode;
249255
SpeedRegulatorNominalSpeedStepMpS = other.SpeedRegulatorNominalSpeedStepMpS;
250256
MaxAccelerationMpSS = other.MaxAccelerationMpSS;
251257
MaxDecelerationMpSS = other.MaxDecelerationMpSS;
@@ -398,7 +404,19 @@ public void Parse(STFReader stf)
398404
case "throttleneutralposition": ThrottleNeutralPosition = stf.ReadBoolBlock(false); break;
399405
case "modeswitchallowedwiththrottlenotatzero": ModeSwitchAllowedWithThrottleNotAtZero = stf.ReadBoolBlock(false); break;
400406
case "docomputenumberofaxles": DoComputeNumberOfAxles = stf.ReadBoolBlock(false); break;
401-
case "deltaaccelerationexponent": DeltaAccelerationExponent = stf.ReadFloatBlock(STFReader.UNITS.Any, 1f); break;
407+
case "speeddeltafunctionmode":
408+
stf.MustMatch("(");
409+
var speedDeltaMode = stf.ReadString();
410+
try
411+
{
412+
SpeedDeltaFunctionMode = (SpeedDeltaMode)Enum.Parse(typeof(SpeedDeltaMode), speedDeltaMode, true);
413+
}
414+
catch
415+
{
416+
STFException.TraceWarning(stf, "Skipped unknown speed delta function mode " + speedDeltaMode);
417+
SpeedDeltaFunctionMode = SpeedDeltaMode.Linear;
418+
}
419+
break;
402420
case "options":
403421
foreach (var speedRegulatorOption in stf.ReadStringBlock("").ToLower().Replace(" ", "").Split(','))
404422
{
@@ -486,7 +504,7 @@ public void Initialize()
486504
if (SpeedDeltaToStartAcceleratingMpS < SpeedDeltaToStopAcceleratingMpS) SpeedDeltaToStopAcceleratingMpS = SpeedDeltaToStartAcceleratingMpS;
487505
if (SpeedDeltaToStartBrakingMpS > SpeedDeltaToStopBrakingMpS) SpeedDeltaToStopBrakingMpS = SpeedDeltaToStartBrakingMpS;
488506

489-
if (DeltaAccelerationExponent == 0.5f) StartReducingSpeedDeltaDownwards /= 3;
507+
if (SpeedDeltaFunctionMode == SpeedDeltaMode.Sqrt) StartReducingSpeedDeltaDownwards /= 3;
490508

491509
if (StartInAutoMode) SpeedRegMode = SpeedRegulatorMode.Auto;
492510

@@ -1239,18 +1257,14 @@ public void UpdateRequiredForce(float elapsedClockSeconds, bool tractionAllowed)
12391257
// However, this means that near the set speed the algorithm oscillates, since small changes in speed
12401258
// correspond to higher throttle demands:
12411259
// da/dv = -srsd / 2 / sqrt((vset-v) * srsd) which diverges when v = vset
1242-
if (DeltaAccelerationExponent == 0.5f)
1260+
if (SpeedDeltaFunctionMode == SpeedDeltaMode.Sqrt)
12431261
demandedAccelerationMpSS = (float)-Math.Sqrt(-demandedAccelerationMpSS);
1244-
else if (DeltaAccelerationExponent != 1)
1245-
demandedAccelerationMpSS = (float)-Math.Pow(-demandedAccelerationMpSS, DeltaAccelerationExponent);
12461262
}
12471263
else if (deltaSpeedMpS > SpeedDeltaToStartAcceleratingMpS || (deltaSpeedMpS > SpeedDeltaToStopAcceleratingMpS && prevDemandedAccelerationMpSS > 0))
12481264
{
12491265
demandedAccelerationMpSS = (deltaSpeedMpS - SpeedDeltaAcceleratingOffsetMpS) * StartReducingSpeedDelta;
1250-
if (DeltaAccelerationExponent == 0.5f)
1266+
if (SpeedDeltaFunctionMode == SpeedDeltaMode.Sqrt)
12511267
demandedAccelerationMpSS = (float)Math.Sqrt(demandedAccelerationMpSS);
1252-
else if (DeltaAccelerationExponent != 1)
1253-
demandedAccelerationMpSS = (float)Math.Pow(demandedAccelerationMpSS, DeltaAccelerationExponent);
12541268
}
12551269
prevDemandedAccelerationMpSS = demandedAccelerationMpSS;
12561270
if (ASCAccelerationMpSS > 0)
@@ -1348,7 +1362,7 @@ void UpdateTrainBrakePercent(float elapsedClockSeconds, float demandedAccelerati
13481362
{
13491363
enabled = true;
13501364
}
1351-
else if (SpeedDeltaToEnableTrainBrake <= 0 || demandedAccelerationMpSS <= -(float)Math.Pow(SpeedDeltaToEnableTrainBrake * StartReducingSpeedDeltaDownwards, DeltaAccelerationExponent))
1365+
else if (SpeedDeltaToEnableTrainBrake <= 0 || demandedAccelerationMpSS <= (float)-(SpeedDeltaFunctionMode == SpeedDeltaMode.Sqrt ? Math.Sqrt(SpeedDeltaToEnableTrainBrake * StartReducingSpeedDeltaDownwards) : SpeedDeltaToEnableTrainBrake * StartReducingSpeedDeltaDownwards))
13521366
{
13531367
// Otherwise, only enabled if dynamic brake cannot provide enough force
13541368
enabled = CCThrottleOrDynBrakePercent < -MinDynamicBrakePercentToEnableTrainBrake || TrainBrakePercent > 0;

0 commit comments

Comments
 (0)