Skip to content

Commit dbb06bd

Browse files
committed
CC: introduce parameter to allow CC mode switch also when throttle is not at 0
1 parent 5a8da77 commit dbb06bd

File tree

4 files changed

+52
-7
lines changed

4 files changed

+52
-7
lines changed

Source/Documentation/Manual/cruisecontrol.rst

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -135,8 +135,7 @@ A list of the available .eng file CC parameters follows here below.
135135
"ControllerCruiseControlLogic", "Can have values 'None', 'SpeedOnly', 'Full'", "Enum", "Full"
136136
"HasProportionalSpeedSelector", "Speed selector is performed by a lever ranging from 0 to max speed", "Boolean", "FALSE"
137137
"SpeedSelectorIsDiscrete", "Speed selected can have only values multiple of NominalSpeedStep, even if selection is through mouse", "Boolean", "FALSE"
138-
"DisableManualSwitchToManualWhenSetForceNotAtZero", "Manual Switch to Cruise Control Manual Mode can't occur when max force set is not at 0", "Boolean", "FALSE"
139-
"DisableManualSwitchToAutoWhenThrottleNotAtZero", "Manual Switch to Cruise Control Auto Mode can't occur when max force set is not at 0", "Boolean", "FALSE"
138+
"ModeSwitchAllowedWithThrottleNotAtZero", "Switch from manual to auto and vice-versa can occur also when throttle lever is not at 0", "Boolean", "FALSE"
140139
"DisableManualSwitchToAutoWhenSetSpeedNotAtTop", "Manual Switch to Cruise Control Auto Mode can't occur when speed is not set at maximum value and at the same moment train speed is not 0", "Boolean", "FALSE"
141140
"UseTrainBrakeAndDynBrake", "CC uses train brake and dyn brake together", "Boolean", "FALSE"
142141
"SpeedDeltaToEnableTrainBrake", "This is the minimum speed delta between actual speed and desired speed for the CC to use also the train brake", "Float(speed)", "5m/s"

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

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3736,6 +3736,7 @@ public void SetThrottleValue(float value)
37363736
if (CruiseControl.UseThrottleAsForceSelector && CruiseControl.SpeedRegMode == CruiseControl.SpeedRegulatorMode.Auto)
37373737
{
37383738
CruiseControl.SetMaxForcePercent((float)Math.Round(value * 100, 0));
3739+
if (!CruiseControl.UseThrottleInCombinedControl) ThrottleController.SetValue(value);
37393740
return;
37403741
}
37413742
if (CruiseControl.UseThrottleAsSpeedSelector && CruiseControl.SpeedRegMode == CruiseControl.SpeedRegulatorMode.Auto)
@@ -3767,6 +3768,7 @@ public void SetThrottlePercent(float percent)
37673768
if (CruiseControl.UseThrottleAsForceSelector && CruiseControl.SpeedRegMode == CruiseControl.SpeedRegulatorMode.Auto)
37683769
{
37693770
CruiseControl.SetMaxForcePercent(percent);
3771+
if (!CruiseControl.UseThrottleInCombinedControl) ThrottleController.SetPercent(percent);
37703772
return;
37713773
}
37723774
else
@@ -3839,6 +3841,30 @@ public void StartThrottleToZero(float? target)
38393841
CommandStartTime = Simulator.ClockTime;
38403842
}
38413843

3844+
/// <summary>
3845+
/// Returns the position of the throttle handle considering
3846+
/// whether it is used for cruise control or not
3847+
/// </summary>
3848+
/// <param name="intermediateValue">Whather asking for intermediate (for mouse operation) or notched (for displaying) value.</param>
3849+
/// <returns>Combined position into 0-1 range</returns>
3850+
public float GetThrottleHandleValue(float data)
3851+
{
3852+
if (CruiseControl?.SpeedRegMode == CruiseControl.SpeedRegulatorMode.Auto && CruiseControl.SelectedMaxAccelerationPercent != 0
3853+
&& CruiseControl.HasIndependentThrottleDynamicBrakeLever)
3854+
return ThrottleController.CurrentValue;
3855+
if (CruiseControl?.SpeedRegMode == CruiseControl.SpeedRegulatorMode.Auto && CruiseControl.UseThrottleAsForceSelector)
3856+
return CruiseControl.SelectedMaxAccelerationPercent / 100;
3857+
if (CruiseControl?.SpeedRegMode == CruiseControl.SpeedRegulatorMode.Auto && CruiseControl.UseThrottleAsSpeedSelector)
3858+
return CruiseControl.SelectedSpeedMpS / MaxSpeedMpS;
3859+
3860+
3861+
if (CruiseControl == null || CruiseControl.SpeedRegMode == CruiseControl.SpeedRegulatorMode.Manual)
3862+
return data;
3863+
else
3864+
return ThrottleController.CurrentValue;
3865+
3866+
}
3867+
38423868
#endregion
38433869

38443870
#region CombinedHandle

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

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,7 @@ public enum SpeedSelectorMode { Parking, Neutral, On, Start }
146146
public bool DisableManualSwitchToAutoWhenThrottleNotAtZero = false;
147147
public bool DisableManualSwitchToAutoWhenSetSpeedNotAtTop = false;
148148
public bool EnableSelectedSpeedSelectionWhenManualModeSet = false;
149+
public bool ModeSwitchAllowedWithThrottleNotAtZero = false;
149150
public bool UseTrainBrakeAndDynBrake = false;
150151
protected float SpeedDeltaToEnableTrainBrake = 5;
151152
protected float SpeedDeltaToEnableFullTrainBrake = 10;
@@ -267,6 +268,7 @@ public CruiseControl(CruiseControl other, MSTSLocomotive locomotive)
267268
TrainBrakeMaxPercentValue = other.TrainBrakeMaxPercentValue;
268269
StartInAutoMode = other.StartInAutoMode;
269270
ThrottleNeutralPosition = other.ThrottleNeutralPosition;
271+
ModeSwitchAllowedWithThrottleNotAtZero = other.ModeSwitchAllowedWithThrottleNotAtZero;
270272

271273
}
272274

@@ -360,6 +362,7 @@ public void Parse(STFReader stf)
360362
case "trainbrakemaxpercentvalue": TrainBrakeMaxPercentValue = stf.ReadFloatBlock(STFReader.UNITS.Any, 0.85f); break;
361363
case "startinautomode": StartInAutoMode = stf.ReadBoolBlock(false); break;
362364
case "throttleneutralposition": ThrottleNeutralPosition = stf.ReadBoolBlock(false); break;
365+
case "modeswitchallowedwiththrottlenotatzero": ModeSwitchAllowedWithThrottleNotAtZero = stf.ReadBoolBlock(false); break;
363366
case "docomputenumberofaxles": DoComputeNumberOfAxles = stf.ReadBoolBlock(false); break;
364367
case "options":
365368
foreach (var speedRegulatorOption in stf.ReadStringBlock("").ToLower().Replace(" ", "").Split(','))
@@ -497,7 +500,7 @@ public void Update(float elapsedClockSeconds)
497500
}
498501
else if (DynamicBrakePriority) WasForceReset = false;
499502
else if (SpeedSelMode == SpeedSelectorMode.Start) WasForceReset = true;
500-
else if (SelectedMaxAccelerationPercent == 0)
503+
else if (SelectedMaxAccelerationPercent == 0 || ModeSwitchAllowedWithThrottleNotAtZero && UseThrottleAsForceSelector)
501504
{
502505
WasBraking = false;
503506
WasForceReset = true;
@@ -556,7 +559,6 @@ public void Update(float elapsedClockSeconds)
556559
if (SpeedRegMode == SpeedRegulatorMode.Manual)
557560
SkipThrottleDisplay = false;
558561

559-
if (maxForceIncreasing) SpeedRegulatorMaxForceIncrease(elapsedClockSeconds);
560562
if (maxForceIncreasing) SpeedRegulatorMaxForceIncrease(elapsedClockSeconds);
561563
if (maxForceDecreasing)
562564
{
@@ -621,7 +623,7 @@ public void SpeedRegulatorModeIncrease()
621623
SpeedRegulatorMode previousMode = SpeedRegMode;
622624
if (SpeedRegMode == SpeedRegulatorMode.Testing) return;
623625
if (SpeedRegMode == SpeedRegulatorMode.Manual &&
624-
((DisableManualSwitchToAutoWhenThrottleNotAtZero && (Locomotive.ThrottlePercent != 0 ||
626+
((!ModeSwitchAllowedWithThrottleNotAtZero && (Locomotive.ThrottlePercent != 0 ||
625627
(Locomotive.DynamicBrakePercent != -1 && Locomotive.DynamicBrakePercent != 0))) ||
626628
(DisableManualSwitchToAutoWhenSetSpeedNotAtTop && SelectedSpeedMpS != Locomotive.MaxSpeedMpS && Locomotive.AbsSpeedMpS > Simulator.MaxStoppedMpS)))
627629
return;
@@ -635,6 +637,7 @@ public void SpeedRegulatorModeIncrease()
635637
{
636638
if (SpeedRegulatorOptions.Contains("regulatorauto")) test = true;
637639
if (!DisableManualSwitchToAutoWhenSetSpeedNotAtTop && !KeepSelectedSpeedWhenManualModeSet) SelectedSpeedMpS = Locomotive.AbsSpeedMpS;
640+
if (UseThrottleAsForceSelector && ModeSwitchAllowedWithThrottleNotAtZero) SelectedMaxAccelerationPercent = Locomotive.ThrottleController.CurrentValue * 100;
638641
break;
639642
}
640643
case SpeedRegulatorMode.Testing: if (SpeedRegulatorOptions.Contains("regulatortest")) test = true; break;
@@ -652,7 +655,7 @@ public void SpeedRegulatorModeDecrease()
652655
Locomotive.SignalEvent(Common.Event.CruiseControlSpeedRegulator);
653656
if (SpeedRegMode == SpeedRegulatorMode.Manual) return;
654657
if (SpeedRegMode == SpeedRegulatorMode.Auto &&
655-
(DisableManualSwitchToManualWhenSetForceNotAtZero && SelectedMaxAccelerationPercent != 0))
658+
(!ModeSwitchAllowedWithThrottleNotAtZero && SelectedMaxAccelerationPercent != 0))
656659
return;
657660
bool test = false;
658661
while (!test)
@@ -663,7 +666,8 @@ public void SpeedRegulatorModeDecrease()
663666
case SpeedRegulatorMode.Auto: if (SpeedRegulatorOptions.Contains("regulatorauto")) test = true; break;
664667
case SpeedRegulatorMode.Manual:
665668
{
666-
Locomotive.ThrottleController.SetPercent(0);
669+
if (!ModeSwitchAllowedWithThrottleNotAtZero)
670+
Locomotive.ThrottleController.SetPercent(0);
667671
if (SpeedRegulatorOptions.Contains("regulatormanual")) test = true;
668672
if (ZeroSelectedSpeedWhenPassingToThrottleMode || UseThrottleAsSpeedSelector) SelectedSpeedMpS = 0;
669673
if (UseThrottleAsForceSelector) SelectedMaxAccelerationPercent = 0;
@@ -779,13 +783,17 @@ protected void SpeedRegulatorMaxForceIncrease(float elapsedClockSeconds)
779783
return;
780784
speedRegulatorIntermediateValue += StepSize * elapsedClockSeconds;
781785
selectedMaxAccelerationPercent = Math.Min((float)Math.Truncate(speedRegulatorIntermediateValue + 1), 100);
786+
if (UseThrottleAsForceSelector && ModeSwitchAllowedWithThrottleNotAtZero && !UseThrottleInCombinedControl)
787+
Locomotive.ThrottleController.SetPercent(selectedMaxAccelerationPercent);
782788
}
783789
else
784790
{
785791
if (selectedMaxAccelerationStep == SpeedRegulatorMaxForceSteps)
786792
return;
787793
speedRegulatorIntermediateValue += MaxForceSelectorIsDiscrete ? elapsedClockSeconds : StepSize * elapsedClockSeconds * SpeedRegulatorMaxForceSteps / 100.0f;
788794
selectedMaxAccelerationStep = Math.Min((float)Math.Truncate(speedRegulatorIntermediateValue + 1), SpeedRegulatorMaxForceSteps);
795+
if (UseThrottleAsForceSelector && ModeSwitchAllowedWithThrottleNotAtZero && !UseThrottleInCombinedControl)
796+
Locomotive.ThrottleController.SetPercent(selectedMaxAccelerationStep * 100 / SpeedRegulatorMaxForceSteps);
789797
}
790798
Simulator.Confirmer.ConfirmWithPerCent(CabControl.MaxAcceleration, SelectedMaxAccelerationPercent);
791799
}
@@ -811,6 +819,8 @@ protected void SpeedRegulatorMaxForceDecrease(float elapsedClockSeconds)
811819
return;
812820
speedRegulatorIntermediateValue -= StepSize * elapsedClockSeconds;
813821
selectedMaxAccelerationPercent = Math.Max((int)speedRegulatorIntermediateValue, 100);
822+
if (UseThrottleAsForceSelector && ModeSwitchAllowedWithThrottleNotAtZero && !UseThrottleInCombinedControl)
823+
Locomotive.ThrottleController.SetPercent(selectedMaxAccelerationPercent);
814824
if (selectedMaxAccelerationPercent == 0)
815825
{
816826
Locomotive.SignalEvent(Common.Event.LeverToZero);
@@ -823,6 +833,8 @@ protected void SpeedRegulatorMaxForceDecrease(float elapsedClockSeconds)
823833
return;
824834
speedRegulatorIntermediateValue -= MaxForceSelectorIsDiscrete ? elapsedClockSeconds : StepSize * elapsedClockSeconds * SpeedRegulatorMaxForceSteps / 100.0f;
825835
selectedMaxAccelerationStep = Math.Max((int)speedRegulatorIntermediateValue, DisableZeroForceStep ? 1 : 0);
836+
if (UseThrottleAsForceSelector && ModeSwitchAllowedWithThrottleNotAtZero && !UseThrottleInCombinedControl)
837+
Locomotive.ThrottleController.SetPercent(selectedMaxAccelerationStep * 100 / SpeedRegulatorMaxForceSteps);
826838
if (selectedMaxAccelerationStep <= (DisableZeroForceStep ? 1 : 0))
827839
{
828840
Locomotive.SignalEvent(Common.Event.LeverToZero);
@@ -855,6 +867,8 @@ public void SpeedRegulatorMaxForceChangeByMouse(float movExtension, float maxVal
855867
{
856868
selectedMaxAccelerationPercent += movExtension * maxValue;
857869
selectedMaxAccelerationPercent = MathHelper.Clamp(selectedMaxAccelerationPercent, 0, 100);
870+
if (UseThrottleAsForceSelector && ModeSwitchAllowedWithThrottleNotAtZero && !UseThrottleInCombinedControl)
871+
Locomotive.ThrottleController.SetPercent(selectedMaxAccelerationPercent);
858872
if (selectedMaxAccelerationPercent == 0)
859873
{
860874
Locomotive.SignalEvent(Common.Event.LeverToZero);
@@ -875,6 +889,8 @@ public void SpeedRegulatorMaxForceChangeByMouse(float movExtension, float maxVal
875889
{
876890
selectedMaxAccelerationStep += movExtension * maxValue;
877891
selectedMaxAccelerationStep = MathHelper.Clamp(selectedMaxAccelerationStep, DisableZeroForceStep ? 1 : 0, SpeedRegulatorMaxForceSteps);
892+
if (UseThrottleAsForceSelector && ModeSwitchAllowedWithThrottleNotAtZero && !UseThrottleInCombinedControl)
893+
Locomotive.ThrottleController.SetPercent(selectedMaxAccelerationStep * 100 / SpeedRegulatorMaxForceSteps);
878894
if (selectedMaxAccelerationStep == (DisableZeroForceStep ? 1 : 0))
879895
{
880896
Locomotive.SignalEvent(Common.Event.LeverToZero);
@@ -897,6 +913,7 @@ public void SpeedRegulatorSelectedSpeedStartIncrease()
897913
Locomotive.ThrottleController.CurrentValue == 0 && Locomotive.DynamicBrakeController.CurrentValue == 0))
898914
{
899915
SpeedRegMode = SpeedRegulatorMode.Auto;
916+
if (UseThrottleAsForceSelector && ModeSwitchAllowedWithThrottleNotAtZero) SelectedMaxAccelerationPercent = Locomotive.ThrottleController.CurrentValue * 100;
900917
}
901918

902919
mpc.DoMovement(MultiPositionController.Movement.Forward);

Source/RunActivity/Viewer3D/RollingStock/MSTSLocomotiveViewer.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2071,6 +2071,9 @@ public virtual int GetDrawIndex()
20712071
index = PercentToIndex(data);
20722072
break;
20732073
case CABViewControlTypes.THROTTLE:
2074+
2075+
index = PercentToIndex(Locomotive.GetThrottleHandleValue(data));
2076+
break;
20742077
case CABViewControlTypes.THROTTLE_DISPLAY:
20752078
index = PercentToIndex(data);
20762079
break;

0 commit comments

Comments
 (0)