Last active
October 13, 2023 22:54
-
-
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 file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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