Skip to content

Commit 7fc8de1

Browse files
committed
Indicate if CC braking is active or not for non self-lapping controllers
1 parent 2391bc0 commit 7fc8de1

File tree

3 files changed

+20
-16
lines changed

3 files changed

+20
-16
lines changed

Source/Orts.Simulation/Common/Scripting/BrakeController.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ public float IntermediateValue
187187
public List<MSTSNotch> Notches() => Host.Notches;
188188

189189
/// <summary>
190-
/// Fraction of train brake demanded by cruise control
190+
/// Fraction of train brake demanded by cruise control. Returns -1 if CC is inactive
191191
/// </summary>
192192
public float CruiseControlBrakeDemand() => Host.CruiseControlBrakeDemand;
193193

Source/Orts.Simulation/Simulation/RollingStocks/SubSystems/Controllers/BrakeController.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -156,9 +156,9 @@ public float CruiseControlBrakeDemand
156156
{
157157
get
158158
{
159-
if (Locomotive.CruiseControl == null) return 0;
160-
if (this == Locomotive.EngineBrakeController) return Locomotive.CruiseControl.EngineBrakePercent / 100;
161-
else return Locomotive.CruiseControl.TrainBrakePercent / 100;
159+
if (Locomotive.CruiseControl == null) return -1;
160+
if (this == Locomotive.EngineBrakeController) return Locomotive.CruiseControl.EngineBrakePercent / 100 ?? -1;
161+
else return Locomotive.CruiseControl.TrainBrakePercent / 100 ?? -1;
162162
}
163163
}
164164
InterpolatorDiesel2D DynamicBrakeBlendingTable;

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

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -117,8 +117,8 @@ public float SetSpeedKpHOrMpH
117117
public float MaxDecelerationMpSS = 2;
118118
public float? ThrottlePercent { get; private set;}
119119
public float? DynamicBrakePercent { get; private set;}
120-
public float TrainBrakePercent { get; private set; }
121-
public float EngineBrakePercent { get; private set; }
120+
public float? TrainBrakePercent { get; private set; }
121+
public float? EngineBrakePercent { get; private set; }
122122
protected float trainLength = 0;
123123
public int TrainLengthMeters = 0;
124124
OdoMeter RestrictedRegionOdometer;
@@ -588,8 +588,8 @@ public void Update(float elapsedClockSeconds)
588588
CCThrottleOrDynBrakePercent = 0;
589589
ThrottlePercent = null;
590590
DynamicBrakePercent = null;
591-
TrainBrakePercent = 0;
592-
EngineBrakePercent = 0;
591+
TrainBrakePercent = null;
592+
EngineBrakePercent = null;
593593
return;
594594
}
595595
if (firstIteration) // if this is executed the first time, let's check all other than player engines in the consist, and record them for further throttle manipulation
@@ -643,8 +643,8 @@ public void Update(float elapsedClockSeconds)
643643
CCThrottleOrDynBrakePercent = 0;
644644
ThrottlePercent = null;
645645
DynamicBrakePercent = null;
646-
TrainBrakePercent = 0;
647-
EngineBrakePercent = 0;
646+
TrainBrakePercent = null;
647+
EngineBrakePercent = null;
648648
}
649649
else if (SpeedRegMode == SpeedRegulatorMode.Auto)
650650
{
@@ -755,14 +755,14 @@ public void Update(float elapsedClockSeconds)
755755
if (CCThrottleOrDynBrakePercent > 0 && !tractionAllowed) CCThrottleOrDynBrakePercent = 0;
756756
if (AbsWheelSpeedMpS == 0 && CCThrottleOrDynBrakePercent < 0) CCThrottleOrDynBrakePercent = 0;
757757
CCThrottleOrDynBrakePercent = MathHelper.Clamp(CCThrottleOrDynBrakePercent, -100, 100);
758-
TrainBrakePercent = MathHelper.Clamp(TrainBrakePercent, 0, 100);
758+
TrainBrakePercent = MathHelper.Clamp(TrainBrakePercent.Value, 0, 100);
759759

760760
if (TrainBrakePercent > 0) CCIsUsingTrainBrake = true;
761761
}
762762
else
763763
{
764764
CCThrottleOrDynBrakePercent = 0;
765-
TrainBrakePercent = 0;
765+
TrainBrakePercent = null;
766766
ThrottlePID.Active = false;
767767
DynamicBrakePID.Active = false;
768768
TrainBrakePID.Active = false;
@@ -772,7 +772,8 @@ public void Update(float elapsedClockSeconds)
772772
if (DynamicBrakePriority) DynamicBrakePercent = null;
773773
else if (CCThrottleOrDynBrakePercent < 0) DynamicBrakePercent = -CCThrottleOrDynBrakePercent;
774774
else DynamicBrakePercent = -1;
775-
if (SpeedSelMode == SpeedSelectorMode.Parking && !EngineBrakePriority)
775+
if (EngineBrakePriority) EngineBrakePercent = null;
776+
else if (SpeedSelMode == SpeedSelectorMode.Parking)
776777
{
777778
if (Locomotive.AbsWheelSpeedMpS <= ParkingBrakeEngageSpeedMpS)
778779
EngineBrakePercent = ParkingBrakePercent;
@@ -795,7 +796,7 @@ public void Save(BinaryWriter outf)
795796
outf.Write(DynamicBrakePriority);
796797
outf.Write((int)SpeedRegMode);
797798
outf.Write((int)SpeedSelMode);
798-
outf.Write(TrainBrakePercent);
799+
outf.Write(TrainBrakePercent ?? -1);
799800
outf.Write(TrainLengthMeters);
800801
outf.Write(CCIsUsingTrainBrake);
801802
}
@@ -813,6 +814,7 @@ public void Restore(BinaryReader inf)
813814
SpeedRegMode = (SpeedRegulatorMode)inf.ReadInt32();
814815
SpeedSelMode = (SpeedSelectorMode)inf.ReadInt32();
815816
TrainBrakePercent = inf.ReadSingle();
817+
if (TrainBrakePercent < 0) TrainBrakePercent = null;
816818
TrainLengthMeters = inf.ReadInt32();
817819
CCIsUsingTrainBrake = inf.ReadBoolean();
818820
}
@@ -1380,14 +1382,16 @@ void UpdateTrainBrakePercent(float elapsedClockSeconds, float demandedAccelerati
13801382

13811383
float target = TrainBrakePID.Percent;
13821384

1385+
if (TrainBrakePercent == null) TrainBrakePercent = 0;
1386+
13831387
if (target > TrainBrakePercent && target >= TrainBrakeMinPercentValue)
13841388
{
13851389
if (TrainBrakePercent < TrainBrakeMinPercentValue) TrainBrakePercent = TrainBrakeMinPercentValue;
1386-
TrainBrakePercent = Math.Min(target, TrainBrakePercent + 100 / TrainBrakeFullRangeIncreaseTimeSeconds * elapsedClockSeconds);
1390+
TrainBrakePercent = Math.Min(target, TrainBrakePercent.Value + 100 / TrainBrakeFullRangeIncreaseTimeSeconds * elapsedClockSeconds);
13871391
}
13881392
else if (target < TrainBrakePercent)
13891393
{
1390-
TrainBrakePercent = Math.Max(target, TrainBrakePercent - 100 / TrainBrakeFullRangeDecreaseTimeSeconds * elapsedClockSeconds);
1394+
TrainBrakePercent = Math.Max(target, TrainBrakePercent.Value - 100 / TrainBrakeFullRangeDecreaseTimeSeconds * elapsedClockSeconds);
13911395
if (TrainBrakePercent <= TrainBrakeMinPercentValue)
13921396
{
13931397
if (target == 0) TrainBrakePercent = 0;

0 commit comments

Comments
 (0)