/
Axle.cs
1341 lines (1214 loc) · 51.8 KB
/
Axle.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 2011 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 Adhesion
//#define DEBUG_ADHESION
using System;
using System.IO;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using Microsoft.Xna.Framework;
using ORTS.Common;
using Orts.Parsers.Msts;
using Orts.Simulation.RollingStocks.SubSystems.PowerTransmissions;
using SharpDX.Direct2D1;
using SharpDX.Direct3D9;
using Orts.Formats.OR;
using static Orts.Simulation.RollingStocks.SubSystems.PowerTransmissions.Axle;
namespace Orts.Simulation.RollingStocks.SubSystems.PowerTransmissions
{
/// <summary>
/// Axle drive type to determine an input and solving method for axles
/// </summary>
public enum AxleDriveType
{
/// <summary>
/// Without any drive
/// </summary>
NotDriven = 0,
/// <summary>
/// Traction motor connected through gearbox to axle
/// </summary>
MotorDriven = 1,
/// <summary>
/// Simple force driven axle
/// </summary>
ForceDriven = 2
}
/// <summary>
/// Sums individual axle values to create a total value
/// </summary>
public class Axles : ISubSystem<Axles>
{
/// <summary>
/// List of axles
/// </summary>
public List<Axle> AxleList = new List<Axle>();
/// <summary>
/// Number of axles
/// </summary>
public int Count { get { return AxleList.Count; } }
/// <summary>
/// Reference to the car
/// </summary>
protected readonly TrainCar Car;
/// <summary>
/// Get total axle out force with brake force substracted
/// </summary>
public float CompensatedForceN
{
get
{
float forceN = 0;
foreach (var axle in AxleList)
{
forceN += axle.CompensatedAxleForceN;
}
return forceN;
}
}
/// <summary>
/// Get total axle out force
/// </summary>
public float AxleForceN
{
get
{
float forceN = 0;
foreach (var axle in AxleList)
{
forceN += axle.AxleForceN;
}
return forceN;
}
}
public bool IsWheelSlip
{
get
{
foreach (var axle in AxleList)
{
if (axle.IsWheelSlip) return true;
}
return false;
}
}
public bool IsWheelSlipWarning
{
get
{
foreach (var axle in AxleList)
{
if (axle.IsWheelSlipWarning) return true;
}
return false;
}
}
public int NumOfSubstepsPS
{
get
{
int sub = 0;
foreach (var axle in AxleList)
{
if (axle.NumOfSubstepsPS > sub) sub = axle.NumOfSubstepsPS;
}
return sub;
}
}
public float SlipSpeedMpS
{
get
{
float speed = 0;
foreach (var axle in AxleList)
{
if (Math.Abs(axle.SlipSpeedMpS) > speed) speed = axle.SlipSpeedMpS;
}
return speed;
}
}
public float SlipSpeedPercent
{
get
{
float slip = 0;
foreach (var axle in AxleList)
{
if (Math.Abs(axle.SlipSpeedPercent) > slip) slip = axle.SlipSpeedPercent;
}
return slip;
}
}
public float SlipDerivationPercentpS
{
get
{
float slip = 0;
foreach (var axle in AxleList)
{
if (Math.Abs(axle.SlipDerivationPercentpS) > slip) slip = axle.SlipDerivationPercentpS;
}
return slip;
}
}
public double ResetTime;
public Axles(TrainCar car)
{
Car = car;
}
public Axle this[int i]
{
get { return AxleList[i]; }
set { AxleList[i] = value; }
}
public void Add(Axle axle)
{
AxleList.Add(axle);
}
/// <summary>
/// Parses all the parameters within the ENG file
/// </summary>
/// <param name="stf">reference to the ENG file reader</param>
public void Parse(string lowercasetoken, STFReader stf)
{
switch (lowercasetoken)
{
case "wagon(ortsadhesion(wheelset":
AxleList.Clear();
stf.MustMatch("(");
stf.ParseBlock(
new[] {
new STFReader.TokenProcessor(
"axle",
() => {
var axle = new Axle();
AxleList.Add(axle);
axle.Parse(stf);
}
)
});
if (AxleList.Count == 0)
throw new InvalidDataException("Wheelset block with no axles");
break;
}
}
public void Copy(Axles other)
{
AxleList = new List<Axle>();
foreach (var ax in other.AxleList)
{
var axle = new Axle();
axle.Copy(ax);
AxleList.Add(axle);
}
}
public void Initialize()
{
ResetTime = Car.Simulator.GameTime;
foreach (var axle in AxleList)
{
if (Car is MSTSLocomotive locomotive)
{
if (axle.InertiaKgm2 <= 0) axle.InertiaKgm2 = locomotive.AxleInertiaKgm2 / AxleList.Count;
if (axle.AxleWeightN <= 0) axle.AxleWeightN = 9.81f * locomotive.DrvWheelWeightKg / AxleList.Count; //remains fixed for diesel/electric locomotives, but varies for steam locomotives
if (axle.NumAxles <= 0) axle.NumAxles = locomotive.LocoNumDrvAxles;
if (axle.WheelRadiusM <= 0) axle.WheelRadiusM = locomotive.DriverWheelRadiusM;
if (axle.WheelFlangeAngleRad <= 0) axle.WheelFlangeAngleRad = locomotive.MaximumWheelFlangeAngleRad;
if (axle.DampingNs <= 0) axle.DampingNs = locomotive.MassKG / 1000.0f / AxleList.Count;
if (axle.FrictionN <= 0) axle.FrictionN = locomotive.MassKG / 1000.0f / AxleList.Count;
}
axle.Initialize();
}
}
public void InitializeMoving()
{
ResetTime = Car.Simulator.GameTime;
foreach (var axle in AxleList)
{
axle.TrainSpeedMpS = Car.SpeedMpS;
axle.InitializeMoving();
}
}
/// <summary>
/// Saves status of each axle on the list
/// </summary>
/// <param name="outf"></param>
public void Save(BinaryWriter outf)
{
outf.Write(AxleList.Count);
foreach (var axle in AxleList)
axle.Save(outf);
}
/// <summary>
/// Restores status of each axle on the list
/// </summary>
/// <param name="inf"></param>
public void Restore(BinaryReader inf)
{
int count = inf.ReadInt32();
for (int i = 0; i < count; i++)
{
if (i >= AxleList.Count)
{
AxleList.Add(new Axle());
AxleList[i].Initialize();
}
AxleList[i].Restore(inf);
}
}
/// <summary>
/// Updates each axle on the list
/// </summary>
/// <param name="elapsedClockSeconds">Time span within the simulation cycle</param>
public void Update(float elapsedClockSeconds)
{
foreach (var axle in AxleList)
{
axle.Update(elapsedClockSeconds);
}
}
public List<Axle>.Enumerator GetEnumerator()
{
return AxleList.GetEnumerator();
}
}
/// <summary>
/// Axle class by Matej Pacha (c)2011, University of Zilina, Slovakia (matej.pacha@kves.uniza.sk)
/// The class is used to manage and simulate axle forces considering adhesion problems.
/// Basic configuration:
/// - Motor generates motive torque what is converted into a motive force (through gearbox)
/// or the motive force is passed directly to the DriveForce property
/// - With known TrainSpeed the Update(timeSpan) method computes a dynamic model of the axle
/// - additional (optional) parameters are weather conditions and correction parameter
/// - Finally an output motive force is stored into the AxleForce
///
/// Every computation within Axle class uses SI-units system with xxxxxUUU unit notation
/// </summary>
public class Axle : ISubSystem<Axle>
{
public int NumOfSubstepsPS { get; set; }
/// <summary>
/// Positive only brake force to the axle, in Newtons
/// </summary>
public float BrakeRetardForceN;
/// <summary>
/// Damping force covered by DampingForceN interface
/// </summary>
protected float dampingNs;
/// <summary>
/// Read/Write positive only damping force to the axle, in Newton-second
/// </summary>
public float DampingNs { set { dampingNs = Math.Abs(value); } get { return dampingNs; } }
int count;
protected float frictionN;
public float FrictionN { set { frictionN = Math.Abs(value); } get { return frictionN; } }
/// <summary>
/// Axle drive type covered by DriveType interface
/// </summary>
public AxleDriveType DriveType;
/// <summary>
/// Axle drive represented by a motor, covered by ElectricMotor interface
/// </summary>
ElectricMotor motor;
/// <summary>
/// Read/Write Motor drive parameter.
/// With setting a value the totalInertiaKgm2 is updated
/// </summary>
public ElectricMotor Motor
{
set
{
motor = value;
DriveType = motor != null ? AxleDriveType.MotorDriven : AxleDriveType.ForceDriven;
switch(DriveType)
{
case AxleDriveType.MotorDriven:
totalInertiaKgm2 = inertiaKgm2 + transmissionRatio * transmissionRatio * motor.InertiaKgm2;
break;
default:
totalInertiaKgm2 = inertiaKgm2;
break;
}
}
get
{
return motor;
}
}
/// <summary>
/// Drive force covered by DriveForceN interface, in Newtons
/// </summary>
protected float driveForceN;
/// <summary>
/// Read/Write drive force used to pass the force directly to the axle without gearbox, in Newtons
/// </summary>
public float DriveForceN { set { driveForceN = value; } get { return driveForceN; } }
/// <summary>
/// Sum of inertia over all axle conected rotating mass, in kg.m^2
/// </summary>
float totalInertiaKgm2;
/// <summary>
/// Axle inertia covered by InertiaKgm2 interface, in kg.m^2
/// </summary>
float inertiaKgm2;
/// <summary>
/// Read/Write positive non zero only axle inertia, in kg.m^2
/// By setting this parameter the totalInertiaKgm2 is updated
/// Throws exception when zero or negative value is passed
/// </summary>
public float InertiaKgm2
{
set
{
if (value <= 0.0)
throw new NotSupportedException("Inertia must be greater than zero");
inertiaKgm2 = value;
switch (DriveType)
{
case AxleDriveType.NotDriven:
break;
case AxleDriveType.MotorDriven:
totalInertiaKgm2 = inertiaKgm2 + transmissionRatio * transmissionRatio * motor.InertiaKgm2;
break;
case AxleDriveType.ForceDriven:
totalInertiaKgm2 = inertiaKgm2;
break;
default:
totalInertiaKgm2 = inertiaKgm2;
break;
}
}
get
{
return inertiaKgm2;
}
}
/// <summary>
/// Pre-calculation of r^2/I
/// </summary>
float forceToAccelerationFactor;
/// <summary>
/// switch between Polach and Pacha adhesion calculation
/// </summary>
public static bool UsePolachAdhesion = false; // "static" so it's shared by all axles of the Player's loco
/// <summary>
/// Pre-calculation of slip characteristics at 0 slip speed
/// </summary>
double axleStaticForceN;
/// <summary>
/// Transmission ratio on gearbox covered by TransmissionRatio interface
/// </summary>
float transmissionRatio;
/// <summary>
/// Read/Write positive nonzero transmission ratio, given by n1:n2 ratio
/// Throws an exception when negative or zero value is passed
/// </summary>
public float TransmissionRatio
{
set
{
if (value <= 0.0)
throw new NotSupportedException("Transmission ratio must be greater than zero");
transmissionRatio = value;
}
get
{
return transmissionRatio;
}
}
/// <summary>
/// Transmission efficiency, relative to 1.0, covered by TransmissionEfficiency interface
/// </summary>
float transmissionEfficiency;
/// <summary>
/// Read/Write transmission efficiency, relative to 1.0, within range of 0.0 to 1.0 (1.0 means 100%, 0.5 means 50%)
/// Throws an exception when out of range value is passed
/// When 0.0 is set the value of 0.99 is used instead
/// </summary>
public float TransmissionEfficiency
{
set
{
if (value > 1.0f)
throw new NotSupportedException("Value must be within the range of 0.0 and 1.0");
if (value <= 0.0f)
transmissionEfficiency = 0.99f;
else
transmissionEfficiency = value;
}
get
{
return transmissionEfficiency;
}
}
/// <summary>
/// Radius of wheels connected to axle
/// </summary>
public float WheelRadiusM;
/// <summary>
/// Flange angle wheels connected to axle
/// </summary>
public float WheelFlangeAngleRad;
/// <summary>
/// Gauge of Track
/// </summary>
public float WheelDistanceGaugeM;
/// <summary>
/// Radius of Track Curve
/// </summary>
public float CurrentCurveRadiusM;
/// <summary>
/// Bogie Rigid Wheel Base - distance between wheel in the bogie
/// </summary>
public float BogieRigidWheelBaseM;
/// <summary>
/// Axles in group of wheels
/// </summary>
public float NumAxles;
/// <summary>
/// Static adhesion coefficient, as given by Curtius-Kniffler formula
/// </summary>
public float AdhesionLimit;
/// <summary>
/// Static adhesion coefficient, as given by Curtius-Kniffler formula, at zero speed, ie UMax
/// </summary>
public float CurtiusKnifflerZeroSpeed;
/// <summary>
/// Wheel adhesion as calculated by Polach
/// </summary>
public float WheelAdhesion;
/// <summary>
/// Maximum wheel adhesion as calculated by Polach at the slip threshold speed
/// </summary>
public float MaximumPolachWheelAdhesion;
/// <summary>
/// Correction parameter of adhesion, it has proportional impact on adhesion limit
/// Should be set to 1.0 for most cases
/// </summary>
public float AdhesionK = 0.7f;
/// <summary>
/// Axle speed value, in metric meters per second
/// </summary>
public double AxleSpeedMpS { get; private set; }
/// <summary>
/// Axle angular position in radians
/// </summary>
public double AxlePositionRad { get; private set; }
/// <summary>
/// Read only axle force value, in Newtons
/// </summary>
public float AxleForceN { get; private set; }
/// <summary>
/// Compensated Axle force value, this provided the motive force equivalent excluding brake force, in Newtons
/// </summary>
public float CompensatedAxleForceN { get; protected set; }
/// <summary>
/// Read/Write axle weight parameter in Newtons
/// </summary>
public float AxleWeightN;
/// <summary>
/// Read/Write train speed parameter in metric meters per second
/// </summary>
public float TrainSpeedMpS;
/// <summary>
/// Wheel slip indicator
/// - is true when absolute value of SlipSpeedMpS is greater than WheelSlipThresholdMpS, otherwise is false
/// </summary>
public bool IsWheelSlip { get; private set; }
float WheelSlipTimeS;
/// <summary>
/// Wheelslip threshold value used to indicate maximal effective slip
/// - its value is computed as a maximum of slip function
/// maximum can be found as a derivation f'(dV) = 0
/// </summary>
public float WheelSlipThresholdMpS;
public void ComputeWheelSlipThresholdMpS()
{
// Bisection algorithm. We assume adhesion maximum is between 0 (0.005) and 4 m/s
double a = 0.005f;
double b = 4;
// We have to find the zero of the derivative of adhesion curve
// i.e. the point where slope changes from positive (adhesion region)
// to negative (slip region)
double dx = 0.001;
double fa = SlipCharacteristicsPolach(a + dx) - SlipCharacteristicsPolach(a);
double fb = SlipCharacteristicsPolach(b + dx) - SlipCharacteristicsPolach(b);
double SlipSpeedMpS = AxleSpeedMpS - TrainSpeedMpS;
MaximumPolachWheelAdhesion = (float)SlipCharacteristicsPolach(WheelSlipThresholdMpS);
if (SlipSpeedMpS == 0)
{
// For display purposes threshold = 0 when no slip speed
WheelSlipThresholdMpS = 0;
return;
}
if (fa * fb > 0)
{
// If sign does not change, bisection fails
WheelSlipThresholdMpS = MpS.FromKpH(0.1f);
return;
}
while (Math.Abs(b - a) > MpS.FromKpH(0.1f))
{
double c = (a + b) / 2;
double fc = SlipCharacteristicsPolach(c + dx) - SlipCharacteristicsPolach(c);
if (fa * fc > 0)
{
a = c;
fa = fc;
}
else
{
b = c;
}
}
WheelSlipThresholdMpS = (float)Math.Max((a + b) / 2, MpS.FromKpH(0.1f));
}
/// <summary>
/// Wheelslip warning indication
/// - is true when SlipSpeedMpS is greater than zero and
/// SlipSpeedPercent is greater than SlipWarningThresholdPercent in both directions,
/// otherwise is false
/// </summary>
public bool IsWheelSlipWarning { get; private set; }
float WheelSlipWarningTimeS;
/// <summary>
/// Read only slip speed value in metric meters per second
/// - computed as a substraction of axle speed and train speed
/// </summary>
public float SlipSpeedMpS
{
get
{
return Math.Abs((float)(AxleSpeedMpS - TrainSpeedMpS));
}
}
/// <summary>
/// Read only relative slip speed value, in percent
/// - the value is relative to WheelSlipThreshold value
/// </summary>
public float SlipSpeedPercent
{
get
{
var temp = (SlipSpeedMpS / WheelSlipThresholdMpS) * 100.0f;
if (float.IsNaN(temp)) temp = 0;//avoid NaN on HuD display when first starting OR
return Math.Abs(temp);
}
}
/// <summary>
/// Slip speed rate of change value, in metric (meters per second) per second
/// </summary>
protected float slipDerivationMpSS;
/// <summary>
/// Slip speed memorized from previous iteration
/// </summary>
protected float previousSlipSpeedMpS;
/// <summary>
/// Read only slip speed rate of change, in metric (meters per second) per second
/// </summary>
public float SlipDerivationMpSS
{
get
{
return slipDerivationMpSS;
}
}
/// <summary>
/// Relative slip rate of change
/// </summary>
protected float slipDerivationPercentpS;
/// <summary>
/// Relativ slip speed from previous iteration
/// </summary>
protected float previousSlipPercent;
/// <summary>
/// Read only relative slip speed rate of change, in percent per second
/// </summary>
public float SlipDerivationPercentpS
{
get
{
return slipDerivationPercentpS;
}
}
double integratorError;
int waitBeforeSpeedingUp;
int waitBeforeChangingRate;
/// <summary>
/// Read/Write relative slip speed warning threshold value, in percent of maximal effective slip
/// </summary>
public float SlipWarningTresholdPercent { set; get; }
PolachCalculator Polach;
public List<string> AnimatedParts = new List<string>();
/// <summary>
/// Nonparametric constructor of Axle class instance
/// - sets motor parameter to null
/// - sets TtransmissionEfficiency to 0.99 (99%)
/// - sets SlipWarningThresholdPercent to 70%
/// - sets axle DriveType to ForceDriven
/// - updates totalInertiaKgm2 parameter
/// </summary>
public Axle()
{
transmissionEfficiency = 0.99f;
SlipWarningTresholdPercent = 70.0f;
DriveType = AxleDriveType.ForceDriven;
totalInertiaKgm2 = inertiaKgm2;
Polach = new PolachCalculator(this);
}
public void Initialize()
{
motor?.Initialize();
}
public void InitializeMoving()
{
AxleSpeedMpS = TrainSpeedMpS;
motor?.InitializeMoving();
}
public void Parse(STFReader stf)
{
stf.MustMatch("(");
while (!stf.EndOfBlock())
{
switch (stf.ReadItem().ToLower())
{
case "ortsradius":
WheelRadiusM = stf.ReadFloatBlock(STFReader.UNITS.Distance, null);
break;
case "ortsflangeangle":
WheelFlangeAngleRad = stf.ReadFloatBlock(STFReader.UNITS.Angle, null);
break;
case "ortsnumberwheelaxles":
NumAxles = stf.ReadFloatBlock(STFReader.UNITS.Distance, null);
break;
case "ortsinertia":
InertiaKgm2 = stf.ReadFloatBlock(STFReader.UNITS.RotationalInertia, null);
break;
case "weight":
AxleWeightN = 9.81f * stf.ReadFloatBlock(STFReader.UNITS.Mass, null);
break;
case "animatedparts":
foreach (var part in stf.ReadStringBlock("").ToUpper().Replace(" ", "").Split(','))
{
if (part != "") AnimatedParts.Add(part);
}
break;
case "(":
stf.SkipRestOfBlock();
break;
}
}
}
public void Copy(Axle other)
{
WheelRadiusM = other.WheelRadiusM;
WheelFlangeAngleRad = other.WheelFlangeAngleRad;
NumAxles = other.NumAxles;
InertiaKgm2 = other.InertiaKgm2;
AxleWeightN = other.AxleWeightN;
AnimatedParts.Clear();
AnimatedParts.AddRange(other.AnimatedParts);
}
/// <summary>
/// Restores the game state.
/// </summary>
/// <param name="inf">The save stream to read from.</param>
public void Restore(BinaryReader inf)
{
previousSlipPercent = inf.ReadSingle();
previousSlipSpeedMpS = inf.ReadSingle();
AxleForceN = inf.ReadSingle();
AxleSpeedMpS = inf.ReadDouble();
NumOfSubstepsPS = inf.ReadInt32();
integratorError = inf.ReadDouble();
}
/// <summary>
/// Save the game state.
/// </summary>
/// <param name="outf">The save stream to write to.</param>
public void Save(BinaryWriter outf)
{
outf.Write(previousSlipPercent);
outf.Write(previousSlipSpeedMpS);
outf.Write(AxleForceN);
outf.Write(AxleSpeedMpS);
outf.Write(NumOfSubstepsPS);
outf.Write(integratorError);
}
/// <summary>
/// Compute variation in axle dynamics. Calculates axle speed, axle angular position and in/out forces.
/// </summary>
public (double, double, double, double) GetAxleMotionVariation(double axleSpeedMpS, double elapsedClockSeconds)
{
double slipSpeedMpS = axleSpeedMpS - TrainSpeedMpS;
double axleOutForceN = 0;
if (UsePolachAdhesion)
{
axleOutForceN = Math.Sign(slipSpeedMpS) * AxleWeightN * SlipCharacteristicsPolach(slipSpeedMpS);
}
else
{
axleOutForceN = AxleWeightN * SlipCharacteristicsPacha((float)axleSpeedMpS - TrainSpeedMpS, TrainSpeedMpS, AdhesionK, AdhesionLimit);
}
double axleInForceN = 0;
if (DriveType == AxleDriveType.ForceDriven)
axleInForceN = DriveForceN * transmissionEfficiency;
else if (DriveType == AxleDriveType.MotorDriven)
axleInForceN = motor.GetDevelopedTorqueNm(axleSpeedMpS * transmissionRatio / WheelRadiusM) * transmissionEfficiency / WheelRadiusM;
double motionForceN = axleInForceN - dampingNs * (axleSpeedMpS - TrainSpeedMpS); // Drive force + heat losses
double frictionForceN = BrakeRetardForceN + frictionN; // Dissipative forces: they will never increase wheel speed
double totalAxleForceN = motionForceN - Math.Sign(axleSpeedMpS) * frictionForceN;
if (Math.Abs(TrainSpeedMpS) < 0.001f && Math.Abs(slipSpeedMpS) < 0.001f && Math.Abs(motionForceN) < frictionForceN)
{
return (-slipSpeedMpS / elapsedClockSeconds, axleSpeedMpS / WheelRadiusM, 0, axleInForceN);
}
if (Math.Abs(totalAxleForceN) < axleStaticForceN)
{
if (Math.Abs(slipSpeedMpS) < 0.001f || Math.Sign(slipSpeedMpS) != Math.Sign(slipSpeedMpS + (totalAxleForceN - axleOutForceN) * forceToAccelerationFactor * elapsedClockSeconds))
{
axleOutForceN = slipSpeedMpS / elapsedClockSeconds / forceToAccelerationFactor + totalAxleForceN;
}
}
totalAxleForceN -= axleOutForceN;
return (totalAxleForceN * forceToAccelerationFactor, axleSpeedMpS / WheelRadiusM, axleOutForceN, axleInForceN);
}
/// <summary>
/// Integrates the wheel rotation movement using a RK4 method,
/// calculating the required number of substeps
/// To maintain the accuracy of the integration method, the number of substeps needs to increase when slip speed approaches the slip threshold speed.
/// The following section attempts to calculate the optimal substep limit. This is a trade off between the accuracy of the slips calculations and the CPU load which impacts the screen FPS
/// Outputs: wheel speed, wheel angular position and motive force
/// </summary>
void Integrate(float elapsedClockSeconds)
{
if (elapsedClockSeconds <= 0) return;
double prevSpeedMpS = AxleSpeedMpS;
if (UsePolachAdhesion)
{
float upperSubStepLimit = 100;
float lowerSubStepLimit = 1;
// use straight line graph approximation to increase substeps as slipspeed increases towards the threshold speed point
// Points are 1 = (0, upperLimit) and 2 = (threshold, lowerLimit)
var AdhesGrad = ((upperSubStepLimit - lowerSubStepLimit) / (WheelSlipThresholdMpS - 0));
var targetNumOfSubstepsPS = Math.Abs((AdhesGrad * SlipSpeedMpS) + lowerSubStepLimit);
if (float.IsNaN((float)targetNumOfSubstepsPS)) targetNumOfSubstepsPS = 1;
if (SlipSpeedMpS > WheelSlipThresholdMpS) // if in wheel slip then maximise the substeps
{
targetNumOfSubstepsPS = upperSubStepLimit;
}
if (Math.Abs(integratorError) < 0.000277 && !IsWheelSlip && !IsWheelSlipWarning && SlipSpeedMpS < 0.25 * WheelSlipThresholdMpS && SlipSpeedMpS < previousSlipSpeedMpS)
{
if (--waitBeforeChangingRate <= 0) //wait for a while before changing the integration rate
{
NumOfSubstepsPS -= 2; // decrease substeps when under low slip conditions
waitBeforeChangingRate = 30;
}
}
else if (targetNumOfSubstepsPS > NumOfSubstepsPS) // increase substeps
{
if (--waitBeforeChangingRate <= 0) //wait for a while before changing the integration rate
{
if (IsWheelSlip || IsWheelSlipWarning || SlipSpeedMpS > previousSlipSpeedMpS)
{
// this speeds up the substep increase if the slip speed approaches the threshold or has exceeded it, ie "critical conditions".
NumOfSubstepsPS += 10;
waitBeforeChangingRate = 5;
}
else
{
// this speeds ups the substeps under "non critical" conditions
NumOfSubstepsPS += 3;
waitBeforeChangingRate = 30;
}
}
}
else if (targetNumOfSubstepsPS < NumOfSubstepsPS) // decrease sub steps
{
if (--waitBeforeChangingRate <= 0) //wait for a while before changing the integration rate
{
NumOfSubstepsPS -= 3;
waitBeforeChangingRate = 30;
}
}
// keeps the substeps to a relevant upper and lower limits
if (NumOfSubstepsPS < lowerSubStepLimit)
NumOfSubstepsPS = (int)lowerSubStepLimit;
if (NumOfSubstepsPS > upperSubStepLimit)
NumOfSubstepsPS = (int)upperSubStepLimit;
}
else
{
if (Math.Abs(integratorError) > Math.Max((Math.Abs(SlipSpeedMpS) - 1) * 0.01f, 0.001f))
{
++NumOfSubstepsPS;
waitBeforeSpeedingUp = 100;
}
else
{
if (--waitBeforeSpeedingUp <= 0) //wait for a while before speeding up the integration
{
--NumOfSubstepsPS;
waitBeforeSpeedingUp = 10; //not so fast ;)
}
}
NumOfSubstepsPS = Math.Max(Math.Min(NumOfSubstepsPS, 50), 1);
}
double dt = elapsedClockSeconds / NumOfSubstepsPS;
double hdt = dt / 2;
double axleInForceSumN = 0;
double axleOutForceSumN = 0;
for (int i = 0; i < NumOfSubstepsPS; i++)
{
var k1 = GetAxleMotionVariation(AxleSpeedMpS, dt);
if (i == 0 && !UsePolachAdhesion)
{
if (k1.Item1 * dt > Math.Max((Math.Abs(SlipSpeedMpS) - 1) * 10, 1) / 100)
{
NumOfSubstepsPS = Math.Min(NumOfSubstepsPS + 5, 50);
dt = elapsedClockSeconds / NumOfSubstepsPS;
hdt = dt / 2;
}
if (Math.Sign(AxleSpeedMpS + k1.Item1 * dt) != Math.Sign(AxleSpeedMpS) && BrakeRetardForceN + frictionN > Math.Abs(driveForceN - k1.Item3))
{
AxlePositionRad += AxleSpeedMpS * hdt;
AxlePositionRad = MathHelper.WrapAngle((float)AxlePositionRad);
AxleSpeedMpS = 0;
AxleForceN = 0;
DriveForceN = (float)k1.Item4;
return;
}
}
var k2 = GetAxleMotionVariation(AxleSpeedMpS + k1.Item1 * hdt, hdt);
var k3 = GetAxleMotionVariation(AxleSpeedMpS + k2.Item1 * hdt, hdt);
var k4 = GetAxleMotionVariation(AxleSpeedMpS + k3.Item1 * dt, dt);
AxleSpeedMpS += (integratorError = (k1.Item1 + 2 * (k2.Item1 + k3.Item1) + k4.Item1) * dt / 6);
AxlePositionRad += (k1.Item2 + 2 * (k2.Item2 + k3.Item2) + k4.Item2) * dt / 6;
axleOutForceSumN += (k1.Item3 + 2 * (k2.Item3 + k3.Item3) + k4.Item3);
axleInForceSumN += (k1.Item4 + 2 * (k2.Item4 + k3.Item4) + k4.Item4);
}
AxleForceN = (float)(axleOutForceSumN / (NumOfSubstepsPS * 6));
DriveForceN = (float)(axleInForceSumN / (NumOfSubstepsPS * 6));
AxlePositionRad = MathHelper.WrapAngle((float)AxlePositionRad);
}
/// <summary>
/// Main Update method
/// - computes slip characteristics to get new axle force
/// - computes axle dynamic model according to its driveType
/// - computes wheelslip indicators
/// </summary>