16
16
// along with Open Rails. If not, see <http://www.gnu.org/licenses/>.
17
17
18
18
using System ;
19
- using Orts . Simulation . RollingStocks . SubSystems . Controllers ;
20
19
using ORTS . Scripting . Api ;
21
20
22
- namespace ORTS . Scripting . Script
21
+ namespace Orts . Simulation . RollingStocks . SubSystems . Controllers
23
22
{
24
23
public class PBL2BrakeController : BrakeController
25
24
{
@@ -54,10 +53,9 @@ public enum ControllerPosition
54
53
private Timer ResetTimer { get ; set ; }
55
54
56
55
// brake controller values
57
- private float OverchargePressureBar = 0.4f ;
58
- private float OverchargeEleminationPressureRateBarpS = 0.0025f ;
59
56
private float FirstDepressureBar = 0.5f ;
60
57
private float BrakeReleasedDepressureBar = 0.2f ;
58
+ private float EpActivationThresholdBar = 0.15f ;
61
59
62
60
private State CurrentState ;
63
61
private ControllerPosition CurrentPosition = ControllerPosition . Hold ;
@@ -67,8 +65,6 @@ public enum ControllerPosition
67
65
private bool OverchargeElimination = false ;
68
66
private bool QuickRelease = false ;
69
67
70
- private float RegulatorPressureBar = 0.0f ;
71
-
72
68
private float ? AiBrakeTargetPercent = null ;
73
69
74
70
public override void Initialize ( )
@@ -84,8 +80,10 @@ public override void Initialize()
84
80
ReleaseValue = notch . Value ;
85
81
ReleaseNotch = Notches ( ) . IndexOf ( notch ) ;
86
82
break ;
87
- case ControllerState . FullQuickRelease :
83
+ case ControllerState . Overcharge :
88
84
OverchargeValue = notch . Value ;
85
+ break ;
86
+ case ControllerState . FullQuickRelease :
89
87
QuickReleaseValue = notch . Value ;
90
88
break ;
91
89
case ControllerState . Lap :
@@ -146,104 +144,129 @@ public override float Update(float elapsedSeconds)
146
144
147
145
public override void UpdatePressure ( ref float pressureBar , float elapsedClockSeconds , ref float epPressureBar )
148
146
{
149
- RegulatorPressureBar = Math . Min ( MaxPressureBar ( ) , MainReservoirPressureBar ( ) ) ;
150
-
151
147
if ( AiBrakeTargetPercent != null )
152
148
{
153
- pressureBar = RegulatorPressureBar - ( float ) AiBrakeTargetPercent * FullServReductionBar ( ) ;
154
- epPressureBar = ( float ) AiBrakeTargetPercent * MaxPressureBar ( ) ;
149
+ pressureBar = MaxPressureBar ( ) - ( float ) AiBrakeTargetPercent / 100 * FullServReductionBar ( ) ;
150
+ epPressureBar = ( float ) AiBrakeTargetPercent / 100 ;
155
151
return ;
156
152
}
157
153
158
- if ( ! FirstDepression && CurrentPosition == ControllerPosition . Apply && pressureBar > Math . Max ( RegulatorPressureBar - FirstDepressureBar , 0 ) )
154
+ if ( ! FirstDepression && CurrentPosition == ControllerPosition . Apply && pressureBar > Math . Max ( MaxPressureBar ( ) - FirstDepressureBar , 0 ) )
159
155
FirstDepression = true ;
160
- else if ( FirstDepression && pressureBar <= Math . Max ( RegulatorPressureBar - FirstDepressureBar , 0 ) )
156
+ else if ( FirstDepression && pressureBar <= Math . Max ( MaxPressureBar ( ) - FirstDepressureBar , 0 ) )
161
157
FirstDepression = false ;
162
158
159
+ if ( QuickReleaseButtonPressed ( ) )
160
+ QuickRelease = true ;
161
+ if ( OverchargeButtonPressed ( ) )
162
+ {
163
+ Overcharge = true ;
164
+ OverchargeElimination = false ;
165
+ }
166
+ else if ( Overcharge )
167
+ {
168
+ Overcharge = false ;
169
+ OverchargeElimination = true ;
170
+ }
163
171
if ( CurrentPosition == ControllerPosition . Apply && Overcharge )
164
172
Overcharge = false ;
165
173
if ( CurrentPosition == ControllerPosition . Apply && QuickRelease )
166
174
QuickRelease = false ;
167
175
168
176
if ( EmergencyBrakingPushButton ( ) || TCSEmergencyBraking ( ) )
169
177
CurrentState = State . Emergency ;
178
+ else if ( TCSFullServiceBraking ( ) && pressureBar > MaxPressureBar ( ) - FullServReductionBar ( ) )
179
+ CurrentState = State . Apply ;
170
180
else if (
171
- CurrentPosition == ControllerPosition . Apply && pressureBar > RegulatorPressureBar - FullServReductionBar ( )
172
- || FirstDepression && CurrentPosition != ControllerPosition . Release && ! QuickRelease && pressureBar > RegulatorPressureBar - FirstDepressureBar
181
+ CurrentPosition == ControllerPosition . Apply && pressureBar > MaxPressureBar ( ) - FullServReductionBar ( )
182
+ || FirstDepression && CurrentPosition != ControllerPosition . Release && ! QuickRelease && pressureBar > MaxPressureBar ( ) - FirstDepressureBar
173
183
)
174
184
CurrentState = State . Apply ;
175
- else if ( OverchargeElimination && pressureBar > RegulatorPressureBar )
185
+ else if ( OverchargeElimination && pressureBar > MaxPressureBar ( ) )
176
186
CurrentState = State . OverchargeElimination ;
177
- else if ( Overcharge && pressureBar <= RegulatorPressureBar + OverchargePressureBar )
187
+ else if ( Overcharge && pressureBar <= Math . Min ( MaxOverchargePressureBar ( ) , MainReservoirPressureBar ( ) ) )
178
188
CurrentState = State . Overcharge ;
179
- else if ( QuickRelease && ! NeutralModeOn && pressureBar < RegulatorPressureBar )
189
+ else if ( QuickRelease && ! NeutralModeOn && pressureBar < Math . Min ( MaxPressureBar ( ) , MainReservoirPressureBar ( ) ) )
180
190
CurrentState = State . QuickRelease ;
181
191
else if (
182
192
! NeutralModeOn && (
183
- CurrentPosition == ControllerPosition . Release && pressureBar < RegulatorPressureBar
184
- || ! FirstDepression && pressureBar > RegulatorPressureBar - BrakeReleasedDepressureBar && pressureBar < RegulatorPressureBar
185
- || pressureBar < RegulatorPressureBar - FullServReductionBar ( )
193
+ CurrentPosition == ControllerPosition . Release && pressureBar < Math . Min ( MaxPressureBar ( ) , MainReservoirPressureBar ( ) )
194
+ || ! FirstDepression && pressureBar > MaxPressureBar ( ) - BrakeReleasedDepressureBar && pressureBar < Math . Min ( MaxPressureBar ( ) , MainReservoirPressureBar ( ) )
195
+ || pressureBar < MaxPressureBar ( ) - FullServReductionBar ( )
186
196
)
187
197
)
188
198
CurrentState = State . Release ;
189
199
else
190
200
CurrentState = State . Hold ;
191
201
202
+ float targetPressureBar = pressureBar ;
192
203
switch ( CurrentState )
193
204
{
194
205
case State . Overcharge :
195
- SetUpdateValue ( - 1 ) ;
196
-
197
- pressureBar += QuickReleaseRateBarpS ( ) * elapsedClockSeconds ;
198
- epPressureBar -= QuickReleaseRateBarpS ( ) * elapsedClockSeconds ;
206
+ {
207
+ SetUpdateValue ( - 1 ) ;
199
208
200
- if ( pressureBar > MaxPressureBar ( ) + OverchargePressureBar )
201
- pressureBar = MaxPressureBar ( ) + OverchargePressureBar ;
202
- break ;
209
+ float dp = QuickReleaseRateBarpS ( ) * elapsedClockSeconds ;
210
+ if ( pressureBar + dp > MaxOverchargePressureBar ( ) )
211
+ dp = MaxOverchargePressureBar ( ) - pressureBar ;
212
+ if ( pressureBar + dp > MainReservoirPressureBar ( ) )
213
+ dp = Math . Max ( MainReservoirPressureBar ( ) - pressureBar , 0 ) ;
214
+ pressureBar += dp ;
203
215
216
+ break ;
217
+ }
204
218
case State . OverchargeElimination :
205
- SetUpdateValue ( - 1 ) ;
206
-
207
- pressureBar -= OverchargeEleminationPressureRateBarpS * elapsedClockSeconds ;
219
+ {
220
+ SetUpdateValue ( - 1 ) ;
208
221
209
- if ( pressureBar < MaxPressureBar ( ) )
210
- pressureBar = MaxPressureBar ( ) ;
211
- break ;
222
+ float dp = OverchargeEliminationRateBarpS ( ) * elapsedClockSeconds ;
223
+ if ( pressureBar - dp < MaxPressureBar ( ) )
224
+ dp = Math . Max ( pressureBar - MaxPressureBar ( ) , 0 ) ;
225
+ pressureBar -= dp ;
212
226
227
+ break ;
228
+ }
213
229
case State . QuickRelease :
214
- SetUpdateValue ( - 1 ) ;
215
-
216
- pressureBar += QuickReleaseRateBarpS ( ) * elapsedClockSeconds ;
217
- epPressureBar -= QuickReleaseRateBarpS ( ) * elapsedClockSeconds ;
230
+ {
231
+ SetUpdateValue ( - 1 ) ;
218
232
219
- if ( pressureBar > RegulatorPressureBar )
220
- pressureBar = RegulatorPressureBar ;
221
- break ;
233
+ float dp = QuickReleaseRateBarpS ( ) * elapsedClockSeconds ;
234
+ if ( pressureBar + dp > MaxPressureBar ( ) )
235
+ dp = MaxPressureBar ( ) - pressureBar ;
236
+ if ( pressureBar + dp > MainReservoirPressureBar ( ) )
237
+ dp = Math . Max ( MainReservoirPressureBar ( ) - pressureBar , 0 ) ;
238
+ pressureBar += dp ;
222
239
240
+ break ;
241
+ }
223
242
case State . Release :
224
- SetUpdateValue ( - 1 ) ;
243
+ {
244
+ SetUpdateValue ( - 1 ) ;
225
245
226
- pressureBar + = ReleaseRateBarpS ( ) * elapsedClockSeconds ;
227
- epPressureBar -= ReleaseRateBarpS ( ) * elapsedClockSeconds ;
228
-
229
- if ( pressureBar > RegulatorPressureBar )
230
- pressureBar = RegulatorPressureBar ;
231
- break ;
246
+ float dp = ReleaseRateBarpS ( ) * elapsedClockSeconds ;
247
+ if ( pressureBar + dp > MaxPressureBar ( ) )
248
+ dp = MaxPressureBar ( ) - pressureBar ;
249
+ if ( pressureBar + dp > MainReservoirPressureBar ( ) )
250
+ dp = Math . Max ( MainReservoirPressureBar ( ) - pressureBar , 0 ) ;
251
+ pressureBar += dp ;
232
252
253
+ break ;
254
+ }
233
255
case State . Hold :
234
256
SetUpdateValue ( 0 ) ;
235
257
break ;
236
258
237
259
case State . Apply :
238
- SetUpdateValue ( 1 ) ;
239
-
240
- pressureBar -= ApplyRateBarpS ( ) * elapsedClockSeconds ;
241
- epPressureBar += ApplyRateBarpS ( ) * elapsedClockSeconds ;
260
+ {
261
+ SetUpdateValue ( 1 ) ;
242
262
243
- if ( pressureBar < Math . Max ( RegulatorPressureBar - FullServReductionBar ( ) , 0.0f ) )
244
- pressureBar = Math . Max ( RegulatorPressureBar - FullServReductionBar ( ) , 0.0f ) ;
245
- break ;
263
+ float dp = ApplyRateBarpS ( ) * elapsedClockSeconds ;
264
+ if ( pressureBar - dp < MaxPressureBar ( ) - FullServReductionBar ( ) )
265
+ dp = Math . Max ( pressureBar - ( MaxPressureBar ( ) - FullServReductionBar ( ) ) , 0 ) ;
266
+ pressureBar -= dp ;
246
267
268
+ break ;
269
+ }
247
270
case State . Emergency :
248
271
SetUpdateValue ( 1 ) ;
249
272
@@ -254,12 +277,14 @@ public override void UpdatePressure(ref float pressureBar, float elapsedClockSec
254
277
break ;
255
278
}
256
279
257
- if ( epPressureBar > MaxPressureBar ( ) )
258
- epPressureBar = MaxPressureBar ( ) ;
259
- if ( epPressureBar < 0 )
260
- epPressureBar = 0 ;
280
+ if ( BrakePipePressureBar ( ) > Math . Max ( MaxPressureBar ( ) - FullServReductionBar ( ) , pressureBar ) + EpActivationThresholdBar )
281
+ epPressureBar = 1 ; // EP application wire
282
+ else if ( CurrentState != State . Emergency && BrakePipePressureBar ( ) >= MaxPressureBar ( ) - FullServReductionBar ( ) && BrakePipePressureBar ( ) < Math . Min ( MaxPressureBar ( ) , pressureBar ) - EpActivationThresholdBar )
283
+ epPressureBar = 0 ; // EP release wire
284
+ else
285
+ epPressureBar = - 1 ;
261
286
262
- if ( QuickRelease && pressureBar == RegulatorPressureBar )
287
+ if ( QuickRelease && pressureBar >= Math . Min ( MaxPressureBar ( ) , MainReservoirPressureBar ( ) ) )
263
288
QuickRelease = false ;
264
289
}
265
290
@@ -365,6 +390,8 @@ public override bool IsValid()
365
390
366
391
public override ControllerState GetState ( )
367
392
{
393
+ if ( CurrentState != State . Emergency && NeutralModeOn )
394
+ return ControllerState . Neutral ;
368
395
switch ( CurrentState )
369
396
{
370
397
case State . Overcharge :
@@ -383,15 +410,16 @@ public override ControllerState GetState()
383
410
return ControllerState . Hold ;
384
411
385
412
case State . Apply :
386
- return ControllerState . Apply ;
413
+ if ( TCSFullServiceBraking ( ) )
414
+ return ControllerState . TCSFullServ ;
415
+ else
416
+ return ControllerState . Apply ;
387
417
388
418
case State . Emergency :
389
419
if ( EmergencyBrakingPushButton ( ) )
390
420
return ControllerState . EBPB ;
391
421
else if ( TCSEmergencyBraking ( ) )
392
422
return ControllerState . TCSEmergency ;
393
- else if ( TCSFullServiceBraking ( ) )
394
- return ControllerState . TCSFullServ ;
395
423
else
396
424
return ControllerState . Emergency ;
397
425
0 commit comments