Skip to content

Commit ec07d8b

Browse files
committed
Resolved issue where closed instances cannot change control mode
1 parent b9634da commit ec07d8b

5 files changed

Lines changed: 56 additions & 64 deletions

File tree

src/main/java/com/funkylogclient/Dashboard.java

Lines changed: 39 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -535,30 +535,37 @@ private void updateControlsVisibility(HBox controls) {
535535
}
536536

537537
private void setSimulationMode(String mode) {
538+
if (!NetworkTablesClient.isConnected()) {
539+
NetworkTablesClient.connect();
540+
new Thread(() -> {
541+
try {
542+
long start = System.currentTimeMillis();
543+
while (!NetworkTablesClient.isConnected() && (System.currentTimeMillis() - start) < 1200) {
544+
Thread.sleep(25);
545+
}
546+
} catch (InterruptedException e) {
547+
Thread.currentThread().interrupt();
548+
}
549+
writeSimulationMode(mode);
550+
}, "FunkyLog-SetSimMode").start();
551+
} else {
552+
writeSimulationMode(mode);
553+
}
554+
}
555+
556+
private void writeSimulationMode(String mode) {
538557
try {
539558
NetworkTableInstance instance = NetworkTableInstance.getDefault();
540-
if (instance == null) {
541-
return;
542-
}
543-
544-
int modeValue = 0;
545-
if (mode.equals("disabled")) {
546-
modeValue = 0;
547-
} else if (mode.equals("teleop")) {
548-
modeValue = 1;
549-
} else if (mode.equals("auto")) {
550-
modeValue = 2;
551-
} else if (mode.equals("test")) {
552-
modeValue = 3;
553-
}
559+
if (instance == null) return;
554560

555561
NetworkTable funkyFMSTable = instance.getTable("FunkyFMS");
556-
if (funkyFMSTable != null) {
557-
System.out.println("FunkyLogs: Setting simulation controlMode to " + modeValue + " (" + mode + ")");
558-
NetworkTableEntry controlModeEntry = funkyFMSTable.getEntry("controlMode");
559-
controlModeEntry.setInteger(modeValue);
560-
instance.flush();
561-
}
562+
if (funkyFMSTable == null) return;
563+
long controlMode = 0;
564+
if ("teleop".equals(mode)) controlMode = 1;
565+
else if ("auto".equals(mode)) controlMode = 2;
566+
else if ("test".equals(mode)) controlMode = 3;
567+
funkyFMSTable.getEntry("controlMode").setDouble((double) controlMode);
568+
instance.flush();
562569
} catch (Exception e) {
563570
System.err.println("Error setting simulation mode: " + e.getMessage());
564571
}
@@ -590,33 +597,23 @@ private void updateSimulationButtons(Button[] buttons, String[] currentMode, HBo
590597
return;
591598
}
592599

593-
NetworkTable fmsTable = instance.getTable("FMSInfo");
594-
if (fmsTable == null) {
600+
NetworkTable funkyFMS = instance.getTable("FunkyFMS");
601+
if (funkyFMS == null) {
595602
return;
596603
}
597604

598-
NetworkTableEntry fmsControlDataEntry = fmsTable.getEntry("FMSControlData");
599-
String mode = "disabled";
600-
601-
if (fmsControlDataEntry != null && fmsControlDataEntry.exists()) {
602-
Number modeValueNumber = fmsControlDataEntry.getNumber(32.0);
603-
int modeValue = modeValueNumber.intValue();
604-
605-
boolean isEnabled = (modeValue & 1) != 0;
606-
boolean isAuto = (modeValue & 2) != 0;
607-
boolean isTest = (modeValue & 4) != 0;
608-
609-
if (!isEnabled) {
610-
mode = "disabled";
611-
} else if (isTest) {
612-
mode = "test";
613-
} else if (isAuto) {
614-
mode = "auto";
615-
} else {
616-
mode = "teleop";
617-
}
605+
NetworkTableEntry controlModeEntry = funkyFMS.getEntry("controlMode");
606+
long cm = 0;
607+
if (controlModeEntry != null) {
608+
cm = (long) controlModeEntry.getDouble(0.0);
618609
}
619610

611+
String mode;
612+
if (cm == 1) mode = "teleop";
613+
else if (cm == 2) mode = "auto";
614+
else if (cm == 3) mode = "test";
615+
else mode = "disabled";
616+
620617
final String finalMode = mode;
621618
Platform.runLater(() -> {
622619
String baseStyle = "-fx-font-size: 11px; " +

src/main/java/com/funkylogclient/FieldViewWidget.java

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -351,12 +351,6 @@ private void redrawDynamicContent() {
351351

352352
double[] trajA = fieldObjects.get("trajA");
353353
double[] trajB = fieldObjects.get("trajB");
354-
if (trajA != null && trajA.length >= 2) {
355-
System.out.println("trajA: " + java.util.Arrays.toString(trajA));
356-
}
357-
if (trajB != null && trajB.length >= 2) {
358-
System.out.println("trajB: " + java.util.Arrays.toString(trajB));
359-
}
360354
if (trajA != null && trajA.length >= 2 && trajB != null && trajB.length >= 2) {
361355
double x1 = offsetX + ((trajA[0] + POSITION_OFFSET_X_M) / FIELD_LENGTH) * displayWidth;
362356
double y1 = offsetY + displayHeight - ((trajA[1] + POSITION_OFFSET_Y_M) / FIELD_WIDTH) * displayHeight;

src/main/java/com/funkylogclient/FunkyLogs.java

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -248,8 +248,7 @@ public ListCell<Message> call(ListView<Message> listView) {
248248
if (dashboard != null) {
249249
dashboard.shutdown();
250250
}
251-
SidebarNetworkTablesChooser.shutdown();
252-
SidebarJoystickInput.shutdown();
251+
shutDownInstances();
253252
System.exit(0);
254253
});
255254
primaryStage.show();
@@ -673,8 +672,6 @@ private HBox createUtilityBar(Stage primaryStage) {
673672
if (dashboard != null) {
674673
dashboard.shutdown();
675674
}
676-
SidebarNetworkTablesChooser.shutdown();
677-
SidebarJoystickInput.shutdown();
678675
animateClose(primaryStage);
679676
});
680677
Button minimizeButton = createWindowsButton("—", false, () -> animateMinimize(primaryStage));
@@ -1099,16 +1096,17 @@ private void animateClose(Stage stage) {
10991096

11001097
ParallelTransition closeAnim = new ParallelTransition(fadeOut, scaleDown);
11011098
closeAnim.setOnFinished(e -> {
1102-
if (dashboard != null) {
1103-
dashboard.shutdown();
1104-
}
1105-
SidebarNetworkTablesChooser.shutdown();
1106-
SidebarJoystickInput.shutdown();
1099+
shutDownInstances();
11071100
System.exit(0);
11081101
});
11091102
closeAnim.play();
11101103
}
11111104

1105+
private static void shutDownInstances() {
1106+
SidebarNetworkTablesChooser.shutdown();
1107+
SidebarJoystickInput.shutdown();
1108+
}
1109+
11121110
public static void main(String[] args) {
11131111
launch(args);
11141112
}

src/main/java/com/funkylogclient/NetworkTablesClient.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,8 @@ private static void startMonitoring() {
103103
return;
104104

105105
try {
106+
if (ntInstance == null)
107+
return;
106108
boolean isConnected = ntInstance.isConnected();
107109

108110
if (isConnected && ntInstance != null) {

src/main/java/com/funkylogclient/SimDriverStationInput.java

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -346,13 +346,14 @@ private boolean isTeleop() {
346346
if (!NetworkTablesClient.isConnected()) return false;
347347
NetworkTableInstance instance = NetworkTableInstance.getDefault();
348348
if (instance == null) return false;
349-
NetworkTable fmsTable = instance.getTable("FMSInfo");
350-
if (fmsTable == null) return false;
351-
NetworkTableEntry entry = fmsTable.getEntry("FMSControlData");
352-
if (entry == null || !entry.exists()) return false;
353-
Number n = entry.getNumber(32.0);
354-
int mode = n.intValue();
355-
return mode == 33;
349+
NetworkTable funkyFMS = instance.getTable("FunkyFMS");
350+
if (funkyFMS == null) return false;
351+
NetworkTableEntry modeEntry = funkyFMS.getEntry("controlMode");
352+
long mode = 0;
353+
if (modeEntry != null) {
354+
mode = (long) modeEntry.getDouble(0.0);
355+
}
356+
return mode == 1;
356357
} catch (Exception e) {
357358
return false;
358359
}

0 commit comments

Comments
 (0)