Skip to content
Open
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 16 additions & 2 deletions choreolib/src/main/java/choreo/auto/AutoChooser.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
import java.util.function.Function;
import java.util.function.Supplier;

/**
Expand Down Expand Up @@ -51,6 +52,7 @@ public class AutoChooser implements Sendable {
private Optional<Alliance> allianceAtGeneration = Optional.empty();
private String nameAtGeneration = NONE_NAME;
private Command generatedCommand = Commands.none();
private Function<String, String> selectionOverride = null;

/** Constructs a new {@link AutoChooser}. */
public AutoChooser() {}
Expand All @@ -69,6 +71,9 @@ public String select(String selectStr) {

private String select(String selectStr, boolean force) {
selected = selectStr;
if (selectionOverride != null) {
selected = selectionOverride.apply(selected);
}
if (selected.equals(nameAtGeneration)
&& allianceAtGeneration.equals(DriverStation.getAlliance())) {
// early return if the selected auto matches the active auto
Expand All @@ -93,6 +98,15 @@ private String select(String selectStr, boolean force) {
return nameAtGeneration;
}

/**
* Sets a selection override function that modifies the selected option before it is applied.
*
* @param override The function that modifies the selected option.
*/
protected void setSelectionOverride(Function<String, String> override) {
selectionOverride = override;
}

/**
* Add an AutoRoutine to the chooser.
*
Expand Down Expand Up @@ -175,7 +189,7 @@ public void addCmd(String name, Supplier<Command> generator) {
* @return A command that runs the selected {@link AutoRoutine}
*/
public Command selectedCommandScheduler() {
return Commands.deferredProxy(() -> selectedCommand());
return Commands.deferredProxy(this::selectedCommand);
}

/**
Expand All @@ -187,7 +201,7 @@ public Command selectedCommandScheduler() {
* @return The currently selected command.
*/
public Command selectedCommand() {
if (RobotBase.isSimulation() && nameAtGeneration == NONE_NAME) {
if (RobotBase.isSimulation() && nameAtGeneration.equals(NONE_NAME)) {
select(selected, true);
}
return generatedCommand;
Expand Down
40 changes: 40 additions & 0 deletions docs/choreolib/auto-factory.md
Original file line number Diff line number Diff line change
Expand Up @@ -309,3 +309,43 @@ public class Robot extends TimedRobot {
}
}
```

## AutoChooser for AdvantageKit Users
Choreo's base `AutoChooser` does not support log replay. AdvantageKit users should instead copy-and-paste the code below, and use the `AutoChooserAK` class instead of the base `AutoChooser` class.

!!! tip
`AutoChooserAK` will automatically register the auto chooser to smart dashboard, meaning that
a manual `SmartDashboard.putData` call is no longer necessary.

```java title="AutoChooserAK.java"
package your.utilities.folder;

import edu.wpi.first.wpilibj.smartdashboard.SmartDashboard;
import org.littletonrobotics.junction.LogTable;
import org.littletonrobotics.junction.Logger;
import org.littletonrobotics.junction.inputs.LoggableInputs;

public class AutoChooserAK extends AutoChooserImpl {
Comment thread
Daniel1464 marked this conversation as resolved.
Outdated
private String selected = NONE_NAME;
private final LoggableInputs replayHandle = new LoggableInputs() {
@Override
public void toLog(LogTable table) {
table.put("selected", selected);
}

@Override
public void fromLog(LogTable table) {
selected = table.get("selected", selected);
}
};

public AutoChooserAK(String name) {
setOnSelectOverride(value -> {
selected = value;
Logger.processInputs(name, replayHandle);
return selected;
});
SmartDashboard.putData(name, this);
}
}
```
Loading