Skip to content

Commit

Permalink
Add LoggedDashboarChooser constructor that copies from a `SendableC…
Browse files Browse the repository at this point in the history
…hooser`
  • Loading branch information
jwbonner committed Nov 2, 2023
1 parent 1738cc1 commit 05e36cf
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 8 deletions.
13 changes: 13 additions & 0 deletions docs/CODE-STRUCTURE.md
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,19 @@ public Command getAutonomousCommand() {
}
```

A `LoggedDashboardChooser` can also be constructed using an existing `SendableChooser`, which allows for compatibility PathPlanner's `AutoBuilder` API:

```java
private final LoggedDashboardChooser<Command> autoChooser;

public RobotContainer() {
// ...

// buildAutoChooser() returns a SendableChooser
autoChooser = new LoggedDashboardChooser<>("Auto Routine", AutoBuilder.buildAutoChooser());
}
```

## Logging Outputs

Output data consists of any calculated values which could be recreated in the simulator, including...
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,7 @@ public class RobotContainer {
private final CommandXboxController controller = new CommandXboxController(0);

// Dashboard inputs
private final LoggedDashboardChooser<Command> autoChooser =
new LoggedDashboardChooser<>("Auto Choices");
private final LoggedDashboardChooser<Command> autoChooser;
private final LoggedDashboardNumber flywheelSpeedInput =
new LoggedDashboardNumber("Flywheel Speed", 1500.0);

Expand Down Expand Up @@ -98,8 +97,7 @@ public RobotContainer() {
() -> flywheel.runVelocity(flywheelSpeedInput.get()), flywheel::stop, flywheel));

// Set up auto routines
autoChooser.addDefaultOption("Do Nothing", Commands.none());
autoChooser.addOption("Example Auto", new PathPlannerAuto("Example Auto"));
autoChooser = new LoggedDashboardChooser<>("Auto Choices", AutoBuilder.buildAutoChooser());

// Set up FF characterization routines
autoChooser.addOption(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.littletonrobotics.junction.networktables;

import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Map;

Expand Down Expand Up @@ -27,11 +28,11 @@ public void fromLog(LogTable table) {
};

/**
* Creates a new LoggedDashboardString, for handling a string input sent via
* Creates a new LoggedDashboardChooser, for handling a chooser input sent via
* NetworkTables.
*
* @param key The key for the chooser, published to
* "/SmartDashboard/{key}" for NT or
*
* @param key The key for the chooser, published to "/SmartDashboard/{key}" for
* NT or
* "/DashboardInputs/{key}" when logged.
*/
public LoggedDashboardChooser(String key) {
Expand All @@ -41,6 +42,55 @@ public LoggedDashboardChooser(String key) {
Logger.registerDashboardInput(this);
}

/**
* Creates a new LoggedDashboardChooser, for handling a chooser input sent via
* NetworkTables. This constructor copies the options from a SendableChooser.
* Note that updates to the original SendableChooser will not affect this
* object.
*
* @param key The key for the chooser, published to "/SmartDashboard/{key}" for
* NT or "/DashboardInputs/{key}" when logged.
*/
@SuppressWarnings("unchecked")
public LoggedDashboardChooser(String key, SendableChooser<V> chooser) {
this(key);

// Get options map
Map<String, V> options = new HashMap<>();
try {
Field mapField = SendableChooser.class.getDeclaredField("m_map");
mapField.setAccessible(true);
options = (Map<String, V>) mapField.get(chooser);
} catch (NoSuchFieldException
| SecurityException
| IllegalArgumentException
| IllegalAccessException e) {
e.printStackTrace();
}

// Get default option
String defaultString = "";
try {
Field defaultField = SendableChooser.class.getDeclaredField("m_defaultChoice");
defaultField.setAccessible(true);
defaultString = (String) defaultField.get(chooser);
} catch (NoSuchFieldException
| SecurityException
| IllegalArgumentException
| IllegalAccessException e) {
e.printStackTrace();
}

// Add options
for (String optionKey : options.keySet()) {
if (optionKey.equals(defaultString)) {
addDefaultOption(optionKey, options.get(optionKey));
} else {
addOption(optionKey, options.get(optionKey));
}
}
}

/** Adds a new option to the chooser. */
public void addOption(String key, V value) {
sendableChooser.addOption(key, key);
Expand Down

0 comments on commit 05e36cf

Please sign in to comment.