/
AirSinglePipe.cs
1888 lines (1711 loc) · 110 KB
/
AirSinglePipe.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// COPYRIGHT 2009, 2010, 2011, 2012, 2013, 2014 by the Open Rails project.
//
// This file is part of Open Rails.
//
// Open Rails is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Open Rails is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Open Rails. If not, see <http://www.gnu.org/licenses/>.
// Debug for Airbrake operation - Train Pipe Leak
//#define DEBUG_TRAIN_PIPE_LEAK
using Microsoft.Xna.Framework;
using Orts.Common;
using Orts.Parsers.Msts;
using ORTS.Common;
using ORTS.Scripting.Api;
using System;
using System.Collections.Generic;
using System.IO;
using System.Diagnostics;
namespace Orts.Simulation.RollingStocks.SubSystems.Brakes.MSTS
{
public class AirSinglePipe : MSTSBrakeSystem
{
protected TrainCar Car;
readonly static float OneAtmospherePSI = 14.696f;
protected float HandbrakePercent;
protected float CylPressurePSI = 64;
public float AutoCylPressurePSI { get; protected set; } = 64;
protected float AuxResPressurePSI = 64;
protected float EmergResPressurePSI = 64;
protected float SupplyResPressurePSI = 64;
protected float ControlResPressurePSI = 64;
protected float FullServPressurePSI = 50;
protected float MaxCylPressurePSI = 64;
protected float MaxTripleValveCylPressurePSI;
protected float AuxCylVolumeRatio = 2.5f;
protected float AuxBrakeLineVolumeRatio;
protected float EmergBrakeLineVolumeRatio;
protected float SupplyBrakeLineVolumeRatio;
protected float CylBrakeLineVolumeRatio;
protected float EmergResVolumeM3 = 0.07f;
protected float SupplyResVolumeM3;
protected float RetainerPressureThresholdPSI;
protected float ReleaseRatePSIpS = 1.86f;
protected float MaxReleaseRatePSIpS = 1.86f;
protected float MaxApplicationRatePSIpS = .9f;
protected float MaxAuxilaryChargingRatePSIpS = 1.684f;
protected float BrakeInsensitivityPSIpS = 0.07f;
protected float EmergencyValveActuationRatePSIpS = 0;
protected bool LegacyEmergencyValve = false;
protected float EmergencyDumpValveRatePSIpS = 0;
protected float EmergencyDumpValveTimerS = 120;
protected float? EmergencyDumpStartTime;
protected bool QuickActionFitted;
protected float EmergResChargingRatePSIpS = 1.684f;
protected float SupplyResChargingRatePSIpS;
protected float EmergAuxVolumeRatio = 1.4f;
protected bool RelayValveFitted = false;
public float RelayValveRatio { get; protected set; } = 1;
public float EngineRelayValveRatio { get; protected set; } = 0;
protected float RelayValveApplicationRatePSIpS = 50;
protected float RelayValveReleaseRatePSIpS = 50;
protected string DebugType = string.Empty;
protected string RetainerDebugState = string.Empty;
protected bool MRPAuxResCharging;
protected float CylVolumeM3;
protected bool EmergResQuickRelease;
protected float UniformChargingThresholdPSI = 3.0f;
protected float UniformChargingRatio;
protected bool UniformChargingActive;
protected float UniformReleaseThresholdPSI = 3.0f;
protected float UniformReleaseRatio;
protected bool UniformReleaseActive;
protected bool QuickServiceActive;
protected bool QuickReleaseActive;
protected float QuickServiceLimitPSI;
protected float QuickServiceApplicationRatePSIpS;
protected float QuickServiceVentRatePSIpS;
protected float AcceleratedApplicationFactor;
protected float AcceleratedApplicationLimitPSIpS = 5.0f;
protected float InitialApplicationThresholdPSI;
protected float TripleValveSensitivityPSI;
protected float BrakeCylinderSpringPressurePSI;
protected float ServiceMaxCylPressurePSI;
protected float ServiceApplicationRatePSIpS;
protected float TwoStageLowPressurePSI;
protected float TwoStageSpeedUpMpS;
protected float TwoStageSpeedDownMpS;
protected bool TwoStageLowPressureActive;
protected float HighSpeedReducingPressurePSI;
protected float AcceleratedEmergencyReleaseThresholdPSI = 20.0f;
protected bool TrainBrakePressureChanging = false;
protected bool BrakePipePressureChanging = false;
protected float SoundTriggerCounter = 0;
protected float prevCylPressurePSI = 0f;
protected float prevBrakePipePressurePSI = 0f;
protected float prevBrakePipePressurePSI_sound = 0f;
protected float BrakePipeChangePSIpS;
protected SmoothedData SmoothedBrakePipeChangePSIpS;
/// <summary>
/// EP brake holding valve. Needs to be closed (Lap) in case of brake application or holding.
/// For non-EP brake types must default to and remain in Release.
/// </summary>
protected ValveState HoldingValve = ValveState.Release;
public enum ValveState
{
[GetString("Lap")] Lap,
[GetString("Apply")] Apply,
[GetString("Release")] Release,
[GetString("Emergency")] Emergency
};
protected ValveState TripleValveState = ValveState.Lap;
public AirSinglePipe(TrainCar car)
{
Car = car;
// taking into account very short (fake) cars to prevent NaNs in brake line pressures
BrakePipeVolumeM3 = (0.032f * 0.032f * (float)Math.PI / 4f) * Math.Max ( 5.0f, (1 + car.CarLengthM)); // Using DN32 (1-1/4") pipe
DebugType = "1P";
SmoothedBrakePipeChangePSIpS = new SmoothedData(0.25f);
// Force graduated releasable brakes. Workaround for MSTS with bugs preventing to set eng/wag files correctly for this.
if (Car.Simulator.Settings.GraduatedRelease) (Car as MSTSWagon).BrakeValve = MSTSWagon.BrakeValveType.Distributor;
if (Car.Simulator.Settings.RetainersOnAllCars && !(Car is MSTSLocomotive))
(Car as MSTSWagon).RetainerPositions = 4;
}
public override bool GetHandbrakeStatus()
{
return HandbrakePercent > 0;
}
public override void InitializeFromCopy(BrakeSystem copy)
{
AirSinglePipe thiscopy = (AirSinglePipe)copy;
MaxCylPressurePSI = thiscopy.MaxCylPressurePSI;
AuxCylVolumeRatio = thiscopy.AuxCylVolumeRatio;
AuxBrakeLineVolumeRatio = thiscopy.AuxBrakeLineVolumeRatio;
EmergBrakeLineVolumeRatio = thiscopy.EmergBrakeLineVolumeRatio;
SupplyBrakeLineVolumeRatio = thiscopy.SupplyBrakeLineVolumeRatio;
CylBrakeLineVolumeRatio = thiscopy.CylBrakeLineVolumeRatio;
EmergResVolumeM3 = thiscopy.EmergResVolumeM3;
SupplyResVolumeM3 = thiscopy.SupplyResVolumeM3;
BrakePipeVolumeM3 = thiscopy.BrakePipeVolumeM3;
CylVolumeM3 = thiscopy.CylVolumeM3;
RetainerPressureThresholdPSI = thiscopy.RetainerPressureThresholdPSI;
ReleaseRatePSIpS = thiscopy.ReleaseRatePSIpS;
MaxReleaseRatePSIpS = thiscopy.MaxReleaseRatePSIpS;
MaxApplicationRatePSIpS = thiscopy.MaxApplicationRatePSIpS;
MaxAuxilaryChargingRatePSIpS = thiscopy.MaxAuxilaryChargingRatePSIpS;
BrakeInsensitivityPSIpS = thiscopy.BrakeInsensitivityPSIpS;
EmergencyValveActuationRatePSIpS = thiscopy.EmergencyValveActuationRatePSIpS;
EmergencyDumpValveRatePSIpS = thiscopy.EmergencyDumpValveRatePSIpS;
EmergencyDumpValveTimerS = thiscopy.EmergencyDumpValveTimerS;
QuickActionFitted = thiscopy.QuickActionFitted;
EmergResChargingRatePSIpS = thiscopy.EmergResChargingRatePSIpS;
EmergAuxVolumeRatio = thiscopy.EmergAuxVolumeRatio;
SupplyResChargingRatePSIpS = thiscopy.SupplyResChargingRatePSIpS;
TwoPipes = thiscopy.TwoPipes;
MRPAuxResCharging = thiscopy.MRPAuxResCharging;
HoldingValve = thiscopy.HoldingValve;
RelayValveFitted = thiscopy.RelayValveFitted;
RelayValveRatio = thiscopy.RelayValveRatio;
EngineRelayValveRatio = thiscopy.EngineRelayValveRatio;
RelayValveApplicationRatePSIpS = thiscopy.RelayValveApplicationRatePSIpS;
RelayValveReleaseRatePSIpS = thiscopy.RelayValveReleaseRatePSIpS;
MaxTripleValveCylPressurePSI = thiscopy.MaxTripleValveCylPressurePSI;
EmergResQuickRelease = thiscopy.EmergResQuickRelease;
UniformChargingThresholdPSI = thiscopy.UniformChargingThresholdPSI;
UniformChargingRatio = thiscopy.UniformChargingRatio;
UniformReleaseThresholdPSI = thiscopy.UniformReleaseThresholdPSI;
UniformReleaseRatio = thiscopy.UniformReleaseRatio;
QuickServiceLimitPSI = thiscopy.QuickServiceLimitPSI;
QuickServiceApplicationRatePSIpS = thiscopy.QuickServiceApplicationRatePSIpS;
QuickServiceVentRatePSIpS = thiscopy.QuickServiceVentRatePSIpS;
AcceleratedApplicationFactor = thiscopy.AcceleratedApplicationFactor;
AcceleratedApplicationLimitPSIpS = thiscopy.AcceleratedApplicationLimitPSIpS;
InitialApplicationThresholdPSI = thiscopy.InitialApplicationThresholdPSI;
TripleValveSensitivityPSI = thiscopy.TripleValveSensitivityPSI;
BrakeCylinderSpringPressurePSI = thiscopy.BrakeCylinderSpringPressurePSI;
ServiceMaxCylPressurePSI = thiscopy.ServiceMaxCylPressurePSI;
ServiceApplicationRatePSIpS = thiscopy.ServiceApplicationRatePSIpS;
TwoStageLowPressurePSI = thiscopy.TwoStageLowPressurePSI;
TwoStageSpeedUpMpS = thiscopy.TwoStageSpeedUpMpS;
TwoStageSpeedDownMpS = thiscopy.TwoStageSpeedDownMpS;
HighSpeedReducingPressurePSI = thiscopy.HighSpeedReducingPressurePSI;
LegacyEmergencyValve = thiscopy.LegacyEmergencyValve;
}
// Get the brake BC & BP for EOT conditions
public override string GetStatus(Dictionary<BrakeSystemComponent, PressureUnit> units)
{
return $" {Simulator.Catalog.GetString("BC")} {FormatStrings.FormatPressure(CylPressurePSI, PressureUnit.PSI, units[BrakeSystemComponent.BrakeCylinder], true)}"
+ $" {Simulator.Catalog.GetString("BP")} {FormatStrings.FormatPressure(BrakeLine1PressurePSI, PressureUnit.PSI, units[BrakeSystemComponent.BrakePipe], true)}";
}
// Get Brake information for train
public override string GetFullStatus(BrakeSystem lastCarBrakeSystem, Dictionary<BrakeSystemComponent, PressureUnit> units)
{
var loco = Car as MSTSLocomotive;
var s = $" {Simulator.Catalog.GetString("EQ")} {FormatStrings.FormatPressure(Car.Train.EqualReservoirPressurePSIorInHg, PressureUnit.PSI, units[BrakeSystemComponent.EqualizingReservoir], true)}"
+ $" {Simulator.Catalog.GetString("BC")} {FormatStrings.FormatPressure(Car.Train.HUDWagonBrakeCylinderPSI, PressureUnit.PSI, units[BrakeSystemComponent.BrakeCylinder], true)}"
+ $" {Simulator.Catalog.GetString("BP")} {FormatStrings.FormatPressure(BrakeLine1PressurePSI, PressureUnit.PSI, units[BrakeSystemComponent.BrakePipe], true)}"
+ $" {Simulator.Catalog.GetString("Flow")} {FormatStrings.FormatAirFlow(Car.Train.TotalBrakePipeFlowM3pS, loco.IsMetric)}";
if (lastCarBrakeSystem != null && lastCarBrakeSystem != this)
s += $" {Simulator.Catalog.GetString("EOT")} {lastCarBrakeSystem.GetStatus(units)}";
if (HandbrakePercent > 0)
s += $" {Simulator.Catalog.GetString("Handbrake")} {HandbrakePercent:F0}%";
return s;
}
public override string[] GetDebugStatus(Dictionary<BrakeSystemComponent, PressureUnit> units)
{
return new string[] {
DebugType,
string.Format("{0}{1}",FormatStrings.FormatPressure(CylPressurePSI, PressureUnit.PSI, units[BrakeSystemComponent.BrakeCylinder], true), (Car as MSTSWagon).WheelBrakeSlideProtectionActive ? "???" : ""),
FormatStrings.FormatPressure(BrakeLine1PressurePSI, PressureUnit.PSI, units[BrakeSystemComponent.BrakePipe], true),
FormatStrings.FormatPressure(AuxResPressurePSI, PressureUnit.PSI, units[BrakeSystemComponent.AuxiliaryReservoir], true),
(Car as MSTSWagon).EmergencyReservoirPresent ? FormatStrings.FormatPressure(EmergResPressurePSI, PressureUnit.PSI, units[BrakeSystemComponent.EmergencyReservoir], true) : string.Empty,
TwoPipes ? FormatStrings.FormatPressure(BrakeLine2PressurePSI, PressureUnit.PSI, units[BrakeSystemComponent.MainPipe], true) : string.Empty,
(Car as MSTSWagon).BrakeValve == MSTSWagon.BrakeValveType.Distributor ? FormatStrings.FormatPressure(ControlResPressurePSI, PressureUnit.PSI, units[BrakeSystemComponent.AuxiliaryReservoir], true) : string.Empty,
(Car as MSTSWagon).SupplyReservoirPresent ? FormatStrings.FormatPressure(SupplyResPressurePSI, PressureUnit.PSI, units[BrakeSystemComponent.SupplyReservoir], true) : string.Empty,
(Car as MSTSWagon).RetainerPositions == 0 ? string.Empty : RetainerDebugState,
Simulator.Catalog.GetString(GetStringAttribute.GetPrettyName(TripleValveState)),
string.Empty, // Spacer because the state above needs 2 columns.
(Car as MSTSWagon).HandBrakePresent ? string.Format("{0:F0}%", HandbrakePercent) : string.Empty,
FrontBrakeHoseConnected ? "I" : "T",
string.Format("A{0} B{1}", AngleCockAOpenAmount >= 1 ? "+" : AngleCockAOpenAmount <= 0 ? "-" : "/", AngleCockBOpenAmount >= 1 ? "+" : AngleCockBOpenAmount <= 0 ? "-" : "/"),
BleedOffValveOpen ? Simulator.Catalog.GetString("Open") : string.Empty,
};
}
public override float GetCylPressurePSI()
{
return CylPressurePSI;
}
public override float GetCylVolumeM3()
{
return CylVolumeM3;
}
public float GetFullServPressurePSI()
{
return FullServPressurePSI;
}
public float GetMaxCylPressurePSI()
{
return MaxCylPressurePSI;
}
public float GetAuxCylVolumeRatio()
{
return AuxCylVolumeRatio;
}
public float GetMaxReleaseRatePSIpS()
{
return MaxReleaseRatePSIpS;
}
public float GetMaxApplicationRatePSIpS()
{
return MaxApplicationRatePSIpS;
}
public override float GetVacResPressurePSI()
{
return 0;
}
public override float GetVacResVolume()
{
return 0;
}
public override float GetVacBrakeCylNumber()
{
return 0;
}
public override void Parse(string lowercasetoken, STFReader stf)
{
switch (lowercasetoken)
{
case "wagon(brakecylinderpressureformaxbrakebrakeforce": MaxCylPressurePSI = AutoCylPressurePSI = stf.ReadFloatBlock(STFReader.UNITS.PressureDefaultPSI, null); break;
case "wagon(triplevalveratio": AuxCylVolumeRatio = stf.ReadFloatBlock(STFReader.UNITS.None, null); break;
case "wagon(brakedistributorreleaserate":
case "wagon(maxreleaserate": MaxReleaseRatePSIpS = ReleaseRatePSIpS = stf.ReadFloatBlock(STFReader.UNITS.PressureRateDefaultPSIpS, null); break;
case "wagon(brakedistributorapplicationrate":
case "wagon(maxapplicationrate": MaxApplicationRatePSIpS = stf.ReadFloatBlock(STFReader.UNITS.PressureRateDefaultPSIpS, null); break;
case "wagon(maxauxilarychargingrate": MaxAuxilaryChargingRatePSIpS = stf.ReadFloatBlock(STFReader.UNITS.PressureRateDefaultPSIpS, null); break;
case "wagon(emergencyreschargingrate": EmergResChargingRatePSIpS = stf.ReadFloatBlock(STFReader.UNITS.PressureRateDefaultPSIpS, null); break;
case "wagon(emergencyresvolumemultiplier": EmergAuxVolumeRatio = stf.ReadFloatBlock(STFReader.UNITS.None, null); break;
case "wagon(emergencyrescapacity": EmergResVolumeM3 = Me3.FromFt3(stf.ReadFloatBlock(STFReader.UNITS.VolumeDefaultFT3, null)); break;
// OpenRails specific parameters
case "wagon(brakepipevolume": BrakePipeVolumeM3 = Me3.FromFt3(stf.ReadFloatBlock(STFReader.UNITS.VolumeDefaultFT3, null)); break;
case "wagon(ortsbrakeinsensitivity": BrakeInsensitivityPSIpS = stf.ReadFloatBlock(STFReader.UNITS.PressureRateDefaultPSIpS, 0.07f); break;
case "wagon(ortsemergencyvalveactuationrate": EmergencyValveActuationRatePSIpS = stf.ReadFloatBlock(STFReader.UNITS.PressureRateDefaultPSIpS, 15f); break;
case "wagon(ortsemergencydumpvalverate": EmergencyDumpValveRatePSIpS = stf.ReadFloatBlock(STFReader.UNITS.PressureRateDefaultPSIpS, 15f); break;
case "wagon(ortsemergencydumpvalvetimer": EmergencyDumpValveTimerS = stf.ReadFloatBlock(STFReader.UNITS.Time, 120.0f); break;
case "wagon(ortsemergencyquickaction": QuickActionFitted = stf.ReadBoolBlock(false); break;
case "wagon(ortsmainrespipeauxrescharging": MRPAuxResCharging = this is AirTwinPipe && stf.ReadBoolBlock(true); break;
case "wagon(ortsbrakerelayvalveratio":
RelayValveRatio = stf.ReadFloatBlock(STFReader.UNITS.None, null);
if (RelayValveRatio != 0)
{
RelayValveFitted = true;
}
else
{
RelayValveRatio = 1;
RelayValveFitted = false;
}
break;
case "wagon(ortsenginebrakerelayvalveratio": EngineRelayValveRatio = stf.ReadFloatBlock(STFReader.UNITS.None, null); break;
case "wagon(ortsbrakerelayvalveapplicationrate": RelayValveApplicationRatePSIpS = stf.ReadFloatBlock(STFReader.UNITS.PressureRateDefaultPSIpS, null); break;
case "wagon(ortsbrakerelayvalvereleaserate": RelayValveReleaseRatePSIpS = stf.ReadFloatBlock(STFReader.UNITS.PressureRateDefaultPSIpS, null); break;
case "wagon(ortsmaxtriplevalvecylinderpressure": MaxTripleValveCylPressurePSI = stf.ReadFloatBlock(STFReader.UNITS.PressureDefaultPSI, null); break;
case "wagon(ortsbrakecylindervolume": CylVolumeM3 = Me3.FromFt3(stf.ReadFloatBlock(STFReader.UNITS.VolumeDefaultFT3, null)); break;
case "wagon(ortsemergencyresquickrelease": EmergResQuickRelease = stf.ReadBoolBlock(true); break;
case "wagon(ortsuniformchargingthreshold": UniformChargingThresholdPSI = stf.ReadFloatBlock(STFReader.UNITS.PressureDefaultPSI, 3.0f); break;
case "wagon(ortsuniformchargingratio": UniformChargingRatio = stf.ReadFloatBlock(STFReader.UNITS.None, null); break;
case "wagon(ortsuniformreleasethreshold": UniformReleaseThresholdPSI = stf.ReadFloatBlock(STFReader.UNITS.PressureDefaultPSI, 3.0f); break;
case "wagon(ortsuniformreleaseratio": UniformReleaseRatio = stf.ReadFloatBlock(STFReader.UNITS.None, null); break;
case "wagon(ortsquickservicelimit": QuickServiceLimitPSI = stf.ReadFloatBlock(STFReader.UNITS.PressureDefaultPSI, null); break;
case "wagon(ortsquickserviceapplicationrate": QuickServiceApplicationRatePSIpS = stf.ReadFloatBlock(STFReader.UNITS.PressureRateDefaultPSIpS, null); break;
case "wagon(ortsquickserviceventrate": QuickServiceVentRatePSIpS = stf.ReadFloatBlock(STFReader.UNITS.PressureRateDefaultPSIpS, null); break;
case "wagon(ortsacceleratedapplicationfactor": AcceleratedApplicationFactor = stf.ReadFloatBlock(STFReader.UNITS.None, null); break;
case "wagon(ortsacceleratedapplicationmaxventrate": AcceleratedApplicationLimitPSIpS = stf.ReadFloatBlock(STFReader.UNITS.PressureRateDefaultPSIpS, 5.0f); break;
case "wagon(ortsinitialapplicationthreshold": InitialApplicationThresholdPSI = stf.ReadFloatBlock(STFReader.UNITS.PressureDefaultPSI, null); break;
case "wagon(ortscylinderspringpressure": BrakeCylinderSpringPressurePSI = stf.ReadFloatBlock(STFReader.UNITS.PressureDefaultPSI, null); break;
case "wagon(ortsmaxservicecylinderpressure": ServiceMaxCylPressurePSI = stf.ReadFloatBlock(STFReader.UNITS.PressureDefaultPSI, null); break;
case "wagon(ortsmaxserviceapplicationrate": ServiceApplicationRatePSIpS = stf.ReadFloatBlock(STFReader.UNITS.PressureRateDefaultPSIpS, null); break;
case "wagon(ortstwostagelowpressure": TwoStageLowPressurePSI = stf.ReadFloatBlock(STFReader.UNITS.PressureDefaultPSI, null); break;
case "wagon(ortstwostageincreasingspeed": TwoStageSpeedUpMpS = stf.ReadFloatBlock(STFReader.UNITS.Speed, null); break;
case "wagon(ortstwostagedecreasingspeed": TwoStageSpeedDownMpS = stf.ReadFloatBlock(STFReader.UNITS.Speed, null); break;
case "wagon(ortshighspeedreducingpressure": HighSpeedReducingPressurePSI = stf.ReadFloatBlock(STFReader.UNITS.PressureDefaultPSI, null); break;
case "wagon(ortssupplyrescapacity": SupplyResVolumeM3 = Me3.FromFt3(stf.ReadFloatBlock(STFReader.UNITS.VolumeDefaultFT3, null)); break;
case "wagon(ortssupplyreschargingrate": SupplyResChargingRatePSIpS = stf.ReadFloatBlock(STFReader.UNITS.PressureRateDefaultPSIpS, null); break;
}
}
public override void Save(BinaryWriter outf)
{
outf.Write(BrakeLine1PressurePSI);
outf.Write(BrakeLine2PressurePSI);
outf.Write(BrakeLine3PressurePSI);
outf.Write(HandbrakePercent);
outf.Write(ReleaseRatePSIpS);
outf.Write(RetainerPressureThresholdPSI);
outf.Write(RetainerDebugState);
outf.Write(AutoCylPressurePSI);
outf.Write(CylPressurePSI);
outf.Write(AuxResPressurePSI);
outf.Write(EmergResPressurePSI);
outf.Write(SupplyResPressurePSI);
outf.Write(ControlResPressurePSI);
outf.Write(FullServPressurePSI);
outf.Write((int)TripleValveState);
outf.Write(FrontBrakeHoseConnected);
outf.Write(RearBrakeHoseConnected);
outf.Write(AngleCockAOpen);
outf.Write(AngleCockAOpenAmount);
outf.Write(AngleCockBOpen);
outf.Write(AngleCockBOpenAmount);
outf.Write(BleedOffValveOpen);
outf.Write((int)HoldingValve);
outf.Write(UniformChargingActive);
outf.Write(UniformReleaseActive);
outf.Write(QuickServiceActive);
outf.Write(QuickReleaseActive);
outf.Write(TwoStageLowPressureActive);
outf.Write(LegacyEmergencyValve);
}
public override void Restore(BinaryReader inf)
{
BrakeLine1PressurePSI = inf.ReadSingle();
BrakeLine2PressurePSI = inf.ReadSingle();
BrakeLine3PressurePSI = inf.ReadSingle();
HandbrakePercent = inf.ReadSingle();
ReleaseRatePSIpS = inf.ReadSingle();
RetainerPressureThresholdPSI = inf.ReadSingle();
RetainerDebugState = inf.ReadString();
AutoCylPressurePSI = inf.ReadSingle();
CylPressurePSI = inf.ReadSingle();
AuxResPressurePSI = inf.ReadSingle();
EmergResPressurePSI = inf.ReadSingle();
SupplyResPressurePSI = inf.ReadSingle();
ControlResPressurePSI = inf.ReadSingle();
FullServPressurePSI = inf.ReadSingle();
TripleValveState = (ValveState)inf.ReadInt32();
FrontBrakeHoseConnected = inf.ReadBoolean();
RearBrakeHoseConnected = inf.ReadBoolean();
AngleCockAOpen = inf.ReadBoolean();
AngleCockAOpenAmount = inf.ReadSingle();
AngleCockBOpen = inf.ReadBoolean();
AngleCockBOpenAmount = inf.ReadSingle();
BleedOffValveOpen = inf.ReadBoolean();
HoldingValve = (ValveState)inf.ReadInt32();
UniformChargingActive = inf.ReadBoolean();
UniformReleaseActive = inf.ReadBoolean();
QuickServiceActive = inf.ReadBoolean();
QuickReleaseActive = inf.ReadBoolean();
TwoStageLowPressureActive = inf.ReadBoolean();
LegacyEmergencyValve = inf.ReadBoolean();
}
public override void Initialize(bool handbrakeOn, float maxPressurePSI, float fullServPressurePSI, bool immediateRelease)
{
BrakeLine1PressurePSI = Car.Train.EqualReservoirPressurePSIorInHg;
BrakeLine2PressurePSI = Car.Train.BrakeLine2PressurePSI;
BrakeLine3PressurePSI = 0;
if (maxPressurePSI > 0)
ControlResPressurePSI = maxPressurePSI;
FullServPressurePSI = fullServPressurePSI;
AutoCylPressurePSI = immediateRelease ? 0 : Math.Min((maxPressurePSI - BrakeLine1PressurePSI) * AuxCylVolumeRatio, MaxCylPressurePSI);
CylPressurePSI = AutoCylPressurePSI * RelayValveRatio;
AuxResPressurePSI = Math.Max(TwoPipes && MRPAuxResCharging && !(Car as MSTSWagon).SupplyReservoirPresent ?
maxPressurePSI : maxPressurePSI - AutoCylPressurePSI / AuxCylVolumeRatio, BrakeLine1PressurePSI);
if ((Car as MSTSWagon).EmergencyReservoirPresent)
EmergResPressurePSI = Math.Max(AuxResPressurePSI, maxPressurePSI);
if ((Car as MSTSWagon).SupplyReservoirPresent)
SupplyResPressurePSI = Math.Max(maxPressurePSI, MRPAuxResCharging && TwoPipes ? BrakeLine2PressurePSI : 0);
TripleValveState = AutoCylPressurePSI < 1 ? ValveState.Release : ValveState.Lap;
HoldingValve = ValveState.Release;
HandbrakePercent = handbrakeOn & (Car as MSTSWagon).HandBrakePresent ? 100 : 0;
SetRetainer(RetainerSetting.Exhaust);
if (Car is MSTSLocomotive loco)
{
loco.MainResPressurePSI = loco.MaxMainResPressurePSI;
}
SmoothedBrakePipeChangePSIpS.ForceSmoothValue(0);
}
public override void Initialize()
{
// reducing size of Emergency Reservoir for short (fake) cars
if (Car.Simulator.Settings.CorrectQuestionableBrakingParams && Car.CarLengthM <= 1)
EmergResVolumeM3 = Math.Min (0.02f, EmergResVolumeM3);
// Install a plain triple valve if no brake valve defined
// Do not install it for tenders if not defined, to allow tenders with straight brake only
if (Car.Simulator.Settings.CorrectQuestionableBrakingParams && (Car as MSTSWagon).BrakeValve == MSTSWagon.BrakeValveType.None && (Car as MSTSWagon).WagonType != TrainCar.WagonTypes.Tender)
{
(Car as MSTSWagon).BrakeValve = MSTSWagon.BrakeValveType.TripleValve;
Trace.TraceWarning("{0} does not define a brake valve, defaulting to a plain triple valve", (Car as MSTSWagon).WagFilePath);
}
// Reducing reservoir charging rates when set unrealistically high
if (Car.Simulator.Settings.CorrectQuestionableBrakingParams && (MaxAuxilaryChargingRatePSIpS > 10 || EmergResChargingRatePSIpS > 10))
{
MaxAuxilaryChargingRatePSIpS = Math.Min(MaxAuxilaryChargingRatePSIpS, 10.0f);
EmergResChargingRatePSIpS = Math.Min(EmergResChargingRatePSIpS, 10.0f);
}
// In simple brake mode set emergency reservoir volume, override high volume values to allow faster brake release.
if (Car.Simulator.Settings.SimpleControlPhysics && EmergResVolumeM3 > 2.0)
EmergResVolumeM3 = 0.7f;
if (MaxTripleValveCylPressurePSI == 0) MaxTripleValveCylPressurePSI = MaxCylPressurePSI / RelayValveRatio;
if (EngineRelayValveRatio == 0) EngineRelayValveRatio = RelayValveRatio;
if (ServiceApplicationRatePSIpS == 0)
ServiceApplicationRatePSIpS = MaxApplicationRatePSIpS;
if ((Car as MSTSWagon).EmergencyReservoirPresent && EmergencyValveActuationRatePSIpS == 0)
{
EmergencyValveActuationRatePSIpS = 15;
LegacyEmergencyValve = true;
}
if (InitialApplicationThresholdPSI == 0)
{
if ((Car as MSTSWagon).BrakeValve == MSTSWagon.BrakeValveType.Distributor)
InitialApplicationThresholdPSI = 2.2f; // UIC spec: brakes should release if brake pipe is within 0.15 bar of control res
else
InitialApplicationThresholdPSI = 1.0f;
}
if (TripleValveSensitivityPSI == 0)
{
if ((Car as MSTSWagon).BrakeValve == MSTSWagon.BrakeValveType.Distributor)
TripleValveSensitivityPSI = 1.4f; // UIC spec: brakes should respond to 0.1 bar changes in brake pipe
else
TripleValveSensitivityPSI = 1.0f;
}
// Assume supply res matches aux res specs if supply res has been poorly defined
if ((Car as MSTSWagon).SupplyReservoirPresent && SupplyResChargingRatePSIpS == 0)
SupplyResChargingRatePSIpS = MaxAuxilaryChargingRatePSIpS;
if ((Car as MSTSWagon).SupplyReservoirPresent && SupplyResVolumeM3 == 0)
SupplyResVolumeM3 = EmergResVolumeM3 / EmergAuxVolumeRatio;
if (EmergResVolumeM3 > 0 && EmergAuxVolumeRatio > 0 && BrakePipeVolumeM3 > 0)
{
AuxBrakeLineVolumeRatio = EmergResVolumeM3 / EmergAuxVolumeRatio / BrakePipeVolumeM3;
EmergBrakeLineVolumeRatio = EmergResVolumeM3 / BrakePipeVolumeM3;
SupplyBrakeLineVolumeRatio = SupplyResVolumeM3 / BrakePipeVolumeM3;
}
else
{
AuxBrakeLineVolumeRatio = 3.1f;
EmergBrakeLineVolumeRatio = 4.34f;
SupplyBrakeLineVolumeRatio = 3.1f;
}
CylBrakeLineVolumeRatio = AuxBrakeLineVolumeRatio / AuxCylVolumeRatio;
if (CylVolumeM3 == 0) CylVolumeM3 = EmergResVolumeM3 / EmergAuxVolumeRatio / AuxCylVolumeRatio;
RelayValveFitted |= (Car is MSTSLocomotive loco && (loco.DynamicBrakeAutoBailOff || loco.DynamicBrakePartialBailOff)) ||
(Car as MSTSWagon).BrakeValve == MSTSWagon.BrakeValveType.DistributingValve || (Car as MSTSWagon).SupplyReservoirPresent;
// If user specified only one two stage speed, set the other to be equal
if (TwoStageSpeedDownMpS == 0 && TwoStageSpeedUpMpS > 0)
TwoStageSpeedDownMpS = TwoStageSpeedUpMpS;
else if (TwoStageSpeedUpMpS == 0 && TwoStageSpeedDownMpS > 0)
TwoStageSpeedUpMpS = TwoStageSpeedDownMpS;
// If speeds are set nonsensically, swap them
else if (TwoStageSpeedUpMpS < TwoStageSpeedDownMpS)
(TwoStageSpeedUpMpS, TwoStageSpeedDownMpS) = (TwoStageSpeedDownMpS, TwoStageSpeedUpMpS);
if (TwoStageLowPressurePSI == 0)
TwoStageLowPressurePSI = MaxCylPressurePSI;
}
/// <summary>
/// Used when initial speed > 0
/// </summary>
public override void InitializeMoving ()
{
Initialize(false, 0, FullServPressurePSI, true);
}
public override void LocoInitializeMoving() // starting conditions when starting speed > 0
{
}
public void UpdateTripleValveState(float elapsedClockSeconds)
{
var prevState = TripleValveState;
var valveType = (Car as MSTSWagon).BrakeValve;
bool disableGradient = !(Car.Train.LeadLocomotive is MSTSLocomotive) && Car.Train.TrainType != Orts.Simulation.Physics.Train.TRAINTYPE.STATIC;
// Legacy cars and static cars use a simpler check for emergency applications to ensure emergency applications occur despite simplified physics
bool emergencyTripped = (Car.Train.TrainType == Orts.Simulation.Physics.Train.TRAINTYPE.STATIC || LegacyEmergencyValve) ?
BrakeLine1PressurePSI <= 0.75f * EmergResPressurePSI * AuxCylVolumeRatio / (AuxCylVolumeRatio + 1) : Math.Max(-SmoothedBrakePipeChangePSIpS.SmoothedValue, 0) > EmergencyValveActuationRatePSIpS;
if (valveType == MSTSWagon.BrakeValveType.Distributor)
{
float applicationPSI = ControlResPressurePSI - BrakeLine1PressurePSI;
float targetPressurePSI = applicationPSI * AuxCylVolumeRatio;
if (!disableGradient && EmergencyValveActuationRatePSIpS > 0 && emergencyTripped)
{
if (prevState == ValveState.Release) // If valve transitions from release to emergency, quick service activates
{
QuickServiceActive = true;
UniformChargingActive = false;
UniformReleaseActive = false;
QuickReleaseActive = false;
}
TripleValveState = ValveState.Emergency;
}
else if (TripleValveState != ValveState.Emergency && targetPressurePSI > AutoCylPressurePSI + (TripleValveState == ValveState.Apply ? 0.0f : TripleValveSensitivityPSI * AuxCylVolumeRatio))
{
if (prevState == ValveState.Release)
{
if (applicationPSI > InitialApplicationThresholdPSI) // If valve transitions from release to apply, quick service activates
{
QuickServiceActive = true;
UniformChargingActive = false;
UniformReleaseActive = false;
QuickReleaseActive = false;
TripleValveState = ValveState.Apply;
}
}
else
{
TripleValveState = ValveState.Apply;
}
}
else if (targetPressurePSI < AutoCylPressurePSI - (TripleValveState == ValveState.Release ? 0.0f : TripleValveSensitivityPSI * AuxCylVolumeRatio) || applicationPSI < InitialApplicationThresholdPSI)
{
if (prevState != ValveState.Release) // If valve transitions to release, quick release activates, quick service deactivates
{
QuickReleaseActive = true;
QuickServiceActive = false;
}
TripleValveState = ValveState.Release;
}
else if (TripleValveState != ValveState.Emergency)
{
TripleValveState = ValveState.Lap;
}
}
else if (valveType == MSTSWagon.BrakeValveType.TripleValve || valveType == MSTSWagon.BrakeValveType.DistributingValve)
{
if (!disableGradient && EmergencyValveActuationRatePSIpS > 0 && emergencyTripped)
{
if (prevState == ValveState.Release) // If valve transitions from release to emergency, quick service activates
{
QuickServiceActive = true;
UniformChargingActive = false;
UniformReleaseActive = false;
QuickReleaseActive = false;
}
TripleValveState = ValveState.Emergency;
}
else if (TripleValveState != ValveState.Emergency && BrakeLine1PressurePSI < AuxResPressurePSI - (TripleValveState == ValveState.Apply ? 0.0f : TripleValveSensitivityPSI))
{
if (prevState == ValveState.Release)
{
if (BrakeLine1PressurePSI < AuxResPressurePSI - InitialApplicationThresholdPSI) // If valve transitions from release to apply, quick service activates
{
QuickServiceActive = true;
UniformChargingActive = false;
UniformReleaseActive = false;
QuickReleaseActive = false;
TripleValveState = ValveState.Apply;
}
}
else
{
TripleValveState = ValveState.Apply;
}
}
else if (BrakeLine1PressurePSI > AuxResPressurePSI + (TripleValveState == ValveState.Release ? 0.0f : TripleValveSensitivityPSI * 2))
{
if (prevState != ValveState.Release) // If valve transitions to release, quick release activates, quick service deactivates
{
QuickReleaseActive = true;
QuickServiceActive = false;
}
TripleValveState = ValveState.Release;
}
else if (TripleValveState == ValveState.Apply)
{
TripleValveState = ValveState.Lap;
}
}
else
{
TripleValveState = ValveState.Release;
}
if (TripleValveState == ValveState.Emergency)
{
if (prevState != ValveState.Emergency)
{
EmergencyDumpStartTime = (float)Car.Simulator.GameTime;
Car.SignalEvent(Event.EmergencyVentValveOn);
}
}
else EmergencyDumpStartTime = null;
}
public void UpdateAngleCockState(bool AngleCockOpen, ref float AngleCockOpenAmount, ref float? AngleCockOpenTime)
{
float currentTime = (float)this.Car.Simulator.GameTime;
if (AngleCockOpen && AngleCockOpenAmount < 1.0f)
{
if (AngleCockOpenTime == null)
{
AngleCockOpenTime = currentTime;
}
else if (currentTime - AngleCockOpenTime > AngleCockOpeningTime)
{
// Finish opening anglecock at a faster rate once time has elapsed
AngleCockOpenAmount = (currentTime - ((float)AngleCockOpenTime + AngleCockOpeningTime)) / 5 + 0.3f;
if (AngleCockOpenAmount >= 1.0f)
{
AngleCockOpenAmount = 1.0f;
AngleCockOpenTime = null;
}
}
else
{
// Gradually open anglecock toward 30% over 30 seconds
AngleCockOpenAmount = 0.3f * (currentTime - (float)AngleCockOpenTime) / AngleCockOpeningTime;
}
}
else if (!AngleCockOpen && AngleCockOpenAmount > 0.0f)
{
AngleCockOpenAmount = 0.0f;
AngleCockOpenTime = null;
}
}
public override void Update(float elapsedClockSeconds)
{
var valveType = (Car as MSTSWagon).BrakeValve;
// Two stage braking: higher brake force is allowed at higher speeds
if (TripleValveState == ValveState.Emergency || (TwoStageLowPressureActive && Math.Abs(Car.SpeedMpS) > TwoStageSpeedUpMpS))
TwoStageLowPressureActive = false;
else if (!TwoStageLowPressureActive && Math.Abs(Car.SpeedMpS) < TwoStageSpeedDownMpS)
TwoStageLowPressureActive = true;
// Determine target brake cylinder feed pressure
float threshold = 0;
if (TripleValveState == ValveState.Emergency)
{
threshold = MaxTripleValveCylPressurePSI; // Set pressure to max in emergency
}
else
{
if (valveType == MSTSWagon.BrakeValveType.Distributor)
{
threshold = Math.Max((ControlResPressurePSI - BrakeLine1PressurePSI) * AuxCylVolumeRatio, 0);
if (threshold < InitialApplicationThresholdPSI * AuxCylVolumeRatio) // Prevent brakes getting stuck with a small amount of air on distributor systems
threshold = 0;
if (MRPAuxResCharging && HighSpeedReducingPressurePSI > 0 && threshold > HighSpeedReducingPressurePSI)
threshold = HighSpeedReducingPressurePSI; // Small workaround to improve compatibility between modern systems and HSRV (such systems shouldn't have an HSRV equipped)
}
else
{
if (TripleValveState == ValveState.Release)
threshold = 0;
else
threshold = MaxTripleValveCylPressurePSI; // Set pressure limit to max for plain triple valves
}
if (TwoStageLowPressureActive && threshold > TwoStageLowPressurePSI)
threshold = TwoStageLowPressurePSI;
else if (ServiceMaxCylPressurePSI > 0 && threshold > ServiceMaxCylPressurePSI)
threshold = ServiceMaxCylPressurePSI;
else if (threshold > MaxTripleValveCylPressurePSI)
threshold = MaxTripleValveCylPressurePSI;
// Account for retainers
threshold = Math.Max(threshold, RetainerPressureThresholdPSI);
}
BrakePipeChangePSIpS = (BrakeLine1PressurePSI - prevBrakePipePressurePSI) / Math.Max(elapsedClockSeconds, 0.0001f);
SmoothedBrakePipeChangePSIpS.Update(Math.Max(elapsedClockSeconds, 0.0001f), BrakePipeChangePSIpS);
// Update anglecock opening. Anglecocks set to gradually open over 30 seconds, but close instantly.
// Gradual opening prevents undesired emergency applications
UpdateAngleCockState(AngleCockAOpen, ref AngleCockAOpenAmount, ref AngleCockAOpenTime);
UpdateAngleCockState(AngleCockBOpen, ref AngleCockBOpenAmount, ref AngleCockBOpenTime);
if (BleedOffValveOpen)
{
if (valveType == MSTSWagon.BrakeValveType.Distributor)
{
ControlResPressurePSI = 0;
BleedOffValveOpen = false;
}
else
{
if (AuxResPressurePSI < 0.01f && AutoCylPressurePSI < 0.01f && BrakeLine1PressurePSI < 0.01f && (EmergResPressurePSI < 0.01f || !(Car as MSTSWagon).EmergencyReservoirPresent))
{
BleedOffValveOpen = false;
}
else
{
AuxResPressurePSI -= elapsedClockSeconds * MaxApplicationRatePSIpS;
if (AuxResPressurePSI < 0)
AuxResPressurePSI = 0;
AutoCylPressurePSI -= elapsedClockSeconds * MaxReleaseRatePSIpS;
if (AutoCylPressurePSI < 0)
AutoCylPressurePSI = 0;
if ((Car as MSTSWagon).EmergencyReservoirPresent)
{
EmergResPressurePSI -= elapsedClockSeconds * EmergResChargingRatePSIpS;
if (EmergResPressurePSI < 0)
EmergResPressurePSI = 0;
}
TripleValveState = ValveState.Release;
}
}
}
else
UpdateTripleValveState(elapsedClockSeconds);
// triple valve is set to charge the brake cylinder
if ((TripleValveState == ValveState.Apply || TripleValveState == ValveState.Emergency) && !Car.WheelBrakeSlideProtectionActive)
{
float dp = 0;
float dpPipe = 0;
if (QuickServiceActive) // Quick service: Brake pipe pressure is locally reduced to speed up initial reduction
{
if (QuickServiceVentRatePSIpS > 0)
{
dpPipe = Math.Abs(elapsedClockSeconds * QuickServiceVentRatePSIpS);
if (AutoCylPressurePSI > QuickServiceLimitPSI * 0.75f) // Vent rate is reduced when quick service is nearly complete
{
dpPipe /= 3;
}
}
dp = elapsedClockSeconds * Math.Max(QuickServiceApplicationRatePSIpS, MaxApplicationRatePSIpS);
}
else
{
if (AcceleratedApplicationFactor > 0) // Accelerated application: Air is vented from the brake pipe to speed up service applications
{
// Amount of air vented is proportional to pressure reduction from external sources
dpPipe = MathHelper.Clamp(-SmoothedBrakePipeChangePSIpS.SmoothedValue * AcceleratedApplicationFactor, 0, AcceleratedApplicationLimitPSIpS) * elapsedClockSeconds;
}
if (TripleValveState == ValveState.Emergency)
dp = elapsedClockSeconds * MaxApplicationRatePSIpS;
else
dp = elapsedClockSeconds * ServiceApplicationRatePSIpS;
}
if (BrakeLine1PressurePSI - dpPipe < 0)
{
// Prevent pipe pressure from going negative, also reset quick service to prevent runaway condition
dpPipe = BrakeLine1PressurePSI;
QuickServiceActive = false;
}
if (TripleValveState != ValveState.Emergency && BrakeLine1PressurePSI > AuxResPressurePSI - 1)
dp *= MathHelper.Lerp(0.1f, 1.0f, AuxResPressurePSI - BrakeLine1PressurePSI); // Reduce application rate if nearing equalization to prevent rapid toggling between apply and lap
else if ((valveType == MSTSWagon.BrakeValveType.Distributor) && AutoCylPressurePSI > threshold - 1)
dp *= MathHelper.Lerp(0.1f, 1.0f, threshold - AutoCylPressurePSI); // Reduce application rate if nearing target pressure
if (AuxResPressurePSI - dp / AuxCylVolumeRatio < AutoCylPressurePSI + dp)
dp = (AuxResPressurePSI - AutoCylPressurePSI) * AuxCylVolumeRatio / (1 + AuxCylVolumeRatio);
if (AutoCylPressurePSI + dp > threshold)
dp = threshold - AutoCylPressurePSI;
if (BrakeLine1PressurePSI > AuxResPressurePSI - dp / AuxCylVolumeRatio && !BleedOffValveOpen)
dp = (AuxResPressurePSI - BrakeLine1PressurePSI) * AuxCylVolumeRatio;
if (dp < 0)
dp = 0;
AuxResPressurePSI -= dp / AuxCylVolumeRatio;
AutoCylPressurePSI += dp;
BrakeLine1PressurePSI -= dpPipe;
// Reset quick service if brake cylinder is above limiting valve setting
// Also reset quick service if cylinders manage to equalize to prevent runaway condition
if (QuickServiceActive && (AutoCylPressurePSI > QuickServiceLimitPSI || AutoCylPressurePSI >= AuxResPressurePSI))
QuickServiceActive = false;
if (TripleValveState == ValveState.Emergency)
{
if ((Car as MSTSWagon).EmergencyReservoirPresent)
{
if (EmergencyDumpValveTimerS != 0 && EmergencyDumpStartTime == null && BrakeLine1PressurePSI > AcceleratedEmergencyReleaseThresholdPSI)
{
// Accelerated emergency release: Aux res and BC air are routed into the brake pipe once the emergency application is complete, speeds up emergency release
// Triggers at 20 psi brake pipe pressure
dp = elapsedClockSeconds * MaxReleaseRatePSIpS;
if (AutoCylPressurePSI - dp < AuxResPressurePSI + dp / AuxCylVolumeRatio)
dp = Math.Max((AutoCylPressurePSI - AuxResPressurePSI) * (AuxCylVolumeRatio / (1 + AuxCylVolumeRatio)), 0);
AutoCylPressurePSI -= dp;
AuxResPressurePSI += dp / AuxCylVolumeRatio;
dp = elapsedClockSeconds * MaxAuxilaryChargingRatePSIpS;
if (AuxResPressurePSI - dp < BrakeLine1PressurePSI + dp * AuxBrakeLineVolumeRatio)
dp = Math.Max((AuxResPressurePSI - BrakeLine1PressurePSI) / (1 + AuxBrakeLineVolumeRatio), 0);
AuxResPressurePSI -= dp;
BrakeLine1PressurePSI += dp * AuxBrakeLineVolumeRatio;
}
else
{
dp = elapsedClockSeconds * MaxApplicationRatePSIpS;
if (EmergResPressurePSI - dp < AuxResPressurePSI + dp * EmergAuxVolumeRatio)
dp = (EmergResPressurePSI - AuxResPressurePSI) / (1 + EmergAuxVolumeRatio);
EmergResPressurePSI -= dp;
AuxResPressurePSI += dp * EmergAuxVolumeRatio;
}
}
else if ((Car as MSTSWagon).SupplyReservoirPresent && BrakeLine1PressurePSI < 15.0f)
{
// Supply res air directed to BC feed line to ensure full emergency force on cars with no emergency res
// Only activated with brake pipe pressure lower than 10-18 psi (assuming 15 is the design point)
// Ratio of displacement (dummy brake cylinder) volume to supply res volume
// We are supplying pressure to the brake cylinder line, not the brake cylinder itself
float dispalcementSupplyVolumeRatio = EmergResVolumeM3 / EmergAuxVolumeRatio / AuxCylVolumeRatio / SupplyResVolumeM3;
dp = elapsedClockSeconds * MaxApplicationRatePSIpS;
if (AutoCylPressurePSI + dp > SupplyResPressurePSI - dp * dispalcementSupplyVolumeRatio)
dp = (SupplyResPressurePSI - AutoCylPressurePSI) / (1 + dispalcementSupplyVolumeRatio);
if (AutoCylPressurePSI + dp > threshold)
dp = threshold - AutoCylPressurePSI;
if (dp < 0)
dp = 0;
SupplyResPressurePSI -= dp * dispalcementSupplyVolumeRatio;
AutoCylPressurePSI += dp;
}
if (EmergencyDumpValveTimerS == 0)
{
if (BrakeLine1PressurePSI < 1) EmergencyDumpStartTime = null;
}
else if (Car.Simulator.GameTime - EmergencyDumpStartTime > EmergencyDumpValveTimerS)
{
EmergencyDumpStartTime = null;
}
if (EmergencyDumpValveRatePSIpS > 0 && EmergencyDumpStartTime != null)
{
BrakeLine1PressurePSI -= elapsedClockSeconds * EmergencyDumpValveRatePSIpS;
if (BrakeLine1PressurePSI < 0)
BrakeLine1PressurePSI = 0;
}
if (QuickActionFitted && BrakeLine1PressurePSI > AutoCylPressurePSI) // Quick action: Uses air from the brake pipe to fill brake cylinder during emergency, usually used without an emrg. res
{
dp = elapsedClockSeconds * MaxApplicationRatePSIpS;
if (AutoCylPressurePSI + dp > MaxCylPressurePSI)
dp = MaxCylPressurePSI - AutoCylPressurePSI;
if (BrakeLine1PressurePSI - dp * CylBrakeLineVolumeRatio < AutoCylPressurePSI + dp)
dp = (BrakeLine1PressurePSI - AutoCylPressurePSI) / (1 + CylBrakeLineVolumeRatio);
if (dp < 0)
dp = 0;
AutoCylPressurePSI += dp;
BrakeLine1PressurePSI -= dp * CylBrakeLineVolumeRatio;
}
}
}
// triple valve set to release pressure in brake cylinder and EP valve set
if (TripleValveState == ValveState.Release && valveType != MSTSWagon.BrakeValveType.None)
{
if (valveType == MSTSWagon.BrakeValveType.Distributor)
{
if (ControlResPressurePSI < BrakeLine1PressurePSI)
{
ControlResPressurePSI = BrakeLine1PressurePSI;
}
else if (BrakeInsensitivityPSIpS > 0 && ControlResPressurePSI > BrakeLine1PressurePSI && ControlResPressurePSI < BrakeLine1PressurePSI + 1) // Overcharge elimination
{
float dp = elapsedClockSeconds * BrakeInsensitivityPSIpS;
ControlResPressurePSI = Math.Max(ControlResPressurePSI - dp, BrakeLine1PressurePSI);
}
}
if (BrakeInsensitivityPSIpS > 0 && AuxResPressurePSI > BrakeLine1PressurePSI) // Allow small flow from auxiliary reservoir to brake pipe so the triple valve is not sensible to small pressure variations when in release position
{
float dp = elapsedClockSeconds * BrakeInsensitivityPSIpS;
if (AuxResPressurePSI - dp < BrakeLine1PressurePSI + dp * AuxBrakeLineVolumeRatio)
dp = (AuxResPressurePSI - BrakeLine1PressurePSI) / (1 + AuxBrakeLineVolumeRatio);
AuxResPressurePSI -= dp;
BrakeLine1PressurePSI += dp * AuxBrakeLineVolumeRatio;
}
}
// triple valve set to hold current pressure in brake cylinder
if (TripleValveState == ValveState.Lap && valveType != MSTSWagon.BrakeValveType.None)
{
// Mitigation for brake cylinder leaks
// TODO: Actually implement air system leaks
if (AutoCylPressurePSI < QuickServiceLimitPSI) // Basic cylinder leak prevention, let air enter cylinder from brake pipe if pressure drops below the quick service limiting valve
{
float dp = elapsedClockSeconds * ServiceApplicationRatePSIpS;
if (AutoCylPressurePSI > BrakeLine1PressurePSI - 1)
dp *= MathHelper.Lerp(0.1f, 1.0f, BrakeLine1PressurePSI - AutoCylPressurePSI);
if (AutoCylPressurePSI + dp > QuickServiceLimitPSI)
dp = QuickServiceLimitPSI - AutoCylPressurePSI;
if (BrakeLine1PressurePSI - dp * CylBrakeLineVolumeRatio < AutoCylPressurePSI + dp)
dp = (BrakeLine1PressurePSI - AutoCylPressurePSI) / (1 + CylBrakeLineVolumeRatio);
AutoCylPressurePSI += dp;
BrakeLine1PressurePSI -= dp * CylBrakeLineVolumeRatio;
}
}
// Handle brake release: reduce cylinder pressure if all triple valve, EP holding valve and retainers allow so
if (TripleValveState == ValveState.Release && HoldingValve == ValveState.Release && AutoCylPressurePSI > threshold)
{
float dp = elapsedClockSeconds * ReleaseRatePSIpS;
if (UniformReleaseRatio > 0) // Uniform release: Brake release is slowed down when the brake pipe is substantially higher than the aux res
{
if (!UniformReleaseActive && AuxResPressurePSI < BrakeLine1PressurePSI - UniformReleaseThresholdPSI)
UniformReleaseActive = true;
else if (UniformReleaseActive && AuxResPressurePSI > BrakeLine1PressurePSI - UniformReleaseThresholdPSI / 2)
UniformReleaseActive = false;
if (UniformReleaseActive)
dp /= UniformReleaseRatio;
}
if (threshold > 0 && AutoCylPressurePSI < threshold + 1)
dp *= MathHelper.Lerp(0.1f, 1.0f, AutoCylPressurePSI - threshold); // Reduce release rate if nearing target pressure to prevent toggling between release and lap
if (AutoCylPressurePSI - dp < threshold)
dp = AutoCylPressurePSI - threshold;