Skip to content

Instantly share code, notes, and snippets.

@bovlb
Last active October 13, 2023 22:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bovlb/6e151fb328ed4309e87d5b176850f7fc to your computer and use it in GitHub Desktop.
Save bovlb/6e151fb328ed4309e87d5b176850f7fc to your computer and use it in GitHub Desktop.
Sketch of how to create a WPILIB command that can be used in a group but which isn't created until initialization
// This class allows you to use a command that freezes configuration on construction that you would like to be deferred
// until initialization. For example, you might want to do path following with a trajectory you generate based on the
// current position.
//
// Example usage:
//
// MyAutoCommand extends SequentialCommandGroup {
// public MyAutoCommand(...args...) {
// Callable<Command> c = () -> { return new MyPathFollowerCommand(...args...); };
// addCommands(
// OnTheFlyCommand(c),
// ...more commands...
// );
// }
// };
class OnTheFlyCommand extends CommandBase {
Callable<Command> m_commandCallable;
Command m_command;
public OnTheFlyCommand(Callable<Command> commandCallable, Subsystem... requirements) {
m_commandCallable = commandCallable;
addRequirements(requirements);
}
@Override
public void initialize() {
m_command = m_commandCallable.call();
m_command.initialize();
addRequirements(m_command.getRequirements());
}
@Override
public void execute() {
m_command.execute();
}
@Override
public boolean isFinished() {
return m_command.isFinished();
}
@Override
public void end(boolean interrupted) {
m_command.end(interrupted);
m_command = null;
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment