Skip to content

Commit c934015

Browse files
committed
ATP: stop engine in case of wrong direction.
1 parent 592c6df commit c934015

File tree

3 files changed

+81
-3
lines changed

3 files changed

+81
-3
lines changed

app/src/main/java/cz/mendelu/xmarik/train_manager/activities/EngineController.java

Lines changed: 79 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -731,13 +731,15 @@ class ATP {
731731
// It takes 3.9 s for train in H0 at 40 km/h to pass 50 cm (sensor-signal distance)
732732
// -> 3 s should be safe for stopping before the signal
733733
static final int OVERSPEED_DELAY_EB_MS = 3000;
734+
static final int DIR_MISMATCH_DELAY_EB_MS = 3000;
734735
public enum Mode {
735736
TRAIN,
736737
SHUNT,
737738
}
738739

739740
public Mode mode = Mode.TRAIN;
740741
private boolean overspeed;
742+
private boolean dirMismatch;
741743
private MediaPlayer soundPlayer;
742744

743745
Handler t_overSpeedEB = new Handler(); // EB = emergency braking
@@ -746,6 +748,12 @@ public enum Mode {
746748
public void run() { overSpeedEB(); }
747749
};
748750

751+
Handler t_dirMismatchEB = new Handler(); // EB = emergency braking
752+
Runnable t_dirMismatchEBRunnable = new Runnable() {
753+
@Override
754+
public void run() { dirMismatchEB(); }
755+
};
756+
749757
public ATP() {
750758
try {
751759
this.soundPlayer = new MediaPlayer();
@@ -763,10 +771,13 @@ public ATP() {
763771
public void onDestroy() {
764772
if (this.overspeed)
765773
this.overSpeedEnd();
774+
if (this.dirMismatch)
775+
this.dirMismatchEnd();
766776
}
767777

768778
public void update() {
769779
this.overSpeedUpdate();
780+
this.directionUpdate();
770781
}
771782

772783
private void soundStart() {
@@ -779,13 +790,18 @@ private void soundStart() {
779790
}
780791
}
781792

782-
private void soundStop() {
783-
if ((this.soundPlayer != null) && (this.soundPlayer.isPlaying())) {
793+
private void soundCheckStop() {
794+
if ((!this.overspeed) && (!this.dirMismatch) && (this.soundPlayer != null) && (this.soundPlayer.isPlaying())) {
784795
this.soundPlayer.stop();
785796
this.soundPlayer.prepareAsync();
786797
}
787798
}
788799

800+
private void removeAllEBCallbacks() {
801+
this.t_overSpeedEB.removeCallbacks(t_overSpeedEBRunnable);
802+
this.t_dirMismatchEB.removeCallbacks(t_dirMismatchEBRunnable);
803+
}
804+
789805
private void overSpeedUpdate() {
790806
final Engine thisEngine = EngineController.this.engine;
791807

@@ -830,22 +846,82 @@ private void overSpeedEnd() {
830846
this.t_overSpeedEB.removeCallbacks(t_overSpeedEBRunnable);
831847
EngineController.this.tv_kmhSpeed.clearAnimation();
832848
EngineController.this.tv_expSpeed.clearAnimation();
833-
this.soundStop();
849+
this.soundCheckStop();
834850
}
835851

836852
private void overSpeedEB() {
837853
final SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(EngineController.this);
854+
this.removeAllEBCallbacks();
838855

839856
if ((EngineController.this.engine.isMyControl()) && (!sharedPreferences.getBoolean("ATPEBDisable", false))) {
840857
EngineController.this.emergencyStop();
841858

842859
new AlertDialog.Builder(EngineController.this)
843860
.setMessage(getString(R.string.ta_atp_overspeed_eb_msg))
861+
.setCancelable(false)
844862
.setPositiveButton(getString(R.string.dialog_ok), (dialog, which) -> {})
845863
.show();
846864
}
847865
}
848866

867+
private void directionUpdate() {
868+
final Engine thisEngine = EngineController.this.engine;
869+
870+
boolean dirMismatch = false;
871+
if ((thisEngine.isMyControl()) && (this.mode != Mode.SHUNT) && (thisEngine.stepsSpeed > 0)) {
872+
dirMismatch = (thisEngine.expDirection != Engine.ExpDirection.UNKNOWN) ? (!thisEngine.expDirectionMatch(thisEngine.direction)) : false;
873+
}
874+
this.dirMismatchSet(dirMismatch);
875+
}
876+
877+
private void dirMismatchSet(boolean mismatch) {
878+
if (mismatch == this.dirMismatch)
879+
return;
880+
this.dirMismatch = mismatch;
881+
882+
if (mismatch)
883+
this.dirMismatchBegin();
884+
else
885+
this.dirMismatchEnd();
886+
}
887+
888+
private void dirMismatchBegin() {
889+
this.t_dirMismatchEB.postDelayed(t_dirMismatchEBRunnable, DIR_MISMATCH_DELAY_EB_MS);
890+
891+
{ // Animation
892+
Animation blink = new AlphaAnimation(0.0f, 1.0f);
893+
blink.setDuration(100);
894+
blink.setRepeatMode(Animation.REVERSE);
895+
blink.setRepeatCount(Animation.INFINITE);
896+
EngineController.this.s_direction.startAnimation(blink);
897+
EngineController.this.tv_expDirection.startAnimation(blink);
898+
}
899+
900+
this.soundStart();
901+
}
902+
903+
private void dirMismatchEnd() {
904+
this.t_dirMismatchEB.removeCallbacks(t_dirMismatchEBRunnable);
905+
EngineController.this.s_direction.clearAnimation();
906+
EngineController.this.tv_expDirection.clearAnimation();
907+
this.soundCheckStop();
908+
}
909+
910+
private void dirMismatchEB() {
911+
final SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(EngineController.this);
912+
this.removeAllEBCallbacks();
913+
914+
if ((EngineController.this.engine.isMyControl()) && (!sharedPreferences.getBoolean("ATPEBDisable", false))) {
915+
EngineController.this.emergencyStop();
916+
917+
new AlertDialog.Builder(EngineController.this)
918+
.setMessage(getString(R.string.ta_atp_direction_eb_msg))
919+
.setCancelable(false)
920+
.setPositiveButton(getString(R.string.dialog_ok), (dialog, which) -> {})
921+
.show();
922+
}
923+
}
924+
849925
public boolean isMovementStartAllowed() {
850926
final Engine thisEngine = EngineController.this.engine;
851927
if (this.mode == Mode.SHUNT)

app/src/main/res/values-b+cs/strings.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
<string name="ta_atp_train">Vlak</string>
4545
<string name="ta_atp_shunt">Posun</string>
4646
<string name="ta_atp_overspeed_eb_msg">Vlakový zabezpečovač: zavedeno nouzové brzdění z důvodu překročení povolené rychlosti!</string>
47+
<string name="ta_atp_direction_eb_msg">Vlakový zabezpečovač: zavedeno nouzové brzdění z důvodu špatného směru!</string>
4748
<string name="ta_atp_direction_change_not_allowed_msg">Vlakový zabezpečovač: změna směru není povolena!</string>
4849
<string name="ta_atp_movement_not_allowed">Vlakový zabezpečovač: neoprávněný rozjezd (rychlost/směr)!</string>
4950

app/src/main/res/values/strings.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
<string name="ta_atp_train">Train</string>
4545
<string name="ta_atp_shunt">Shunting</string>
4646
<string name="ta_atp_overspeed_eb_msg">Automatic Train Protection: inhibited emergency braking due to overspeed!</string>
47+
<string name="ta_atp_direction_eb_msg">Automatic Train Protection: inhibited emergency braking due to wrong direction!</string>
4748
<string name="ta_atp_direction_change_not_allowed_msg">Automatic Train Protection: change of direction is not allowed!</string>
4849
<string name="ta_atp_movement_not_allowed">Automatic Train Protection: movement is not allowed (speed/direction)!</string>
4950

0 commit comments

Comments
 (0)