-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Protect state machine from improper use
- Loading branch information
Showing
2 changed files
with
20 additions
and
33 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,29 +1,33 @@ | ||
package frc.robot.subsystems; | ||
|
||
import edu.wpi.first.wpilibj2.command.Subsystem; | ||
import edu.wpi.first.wpilibj2.command.SubsystemBase; | ||
|
||
public abstract class StateMachine extends SubsystemBase { | ||
private SystemState m_currentState; | ||
|
||
public interface StateMachine extends Subsystem { | ||
/** | ||
* Get current system state | ||
* @return Current system state | ||
* Create a state machine | ||
* @param initialState Starting state for state machine | ||
*/ | ||
public SystemState getState(); | ||
public StateMachine(SystemState initialState) { | ||
this.m_currentState = initialState; | ||
setDefaultCommand(new StateCommand(this::getState, this).repeatedly()); | ||
} | ||
|
||
/** | ||
* Iterate system to next state | ||
* <p> | ||
* ONLY TO BE USED BY {@link StateCommand#end(boolean)} | ||
* <p> | ||
* DO NOT CALL THIS METHOD! | ||
*/ | ||
public void setState(SystemState state); | ||
void setState(SystemState state) { | ||
m_currentState = state; | ||
} | ||
|
||
/** | ||
* Sets the default command for this subsystem using the state machine architecture | ||
* @param commands List of state commands | ||
* Get current system state | ||
* @return Current system state | ||
*/ | ||
public default void setDefaultCommand() { | ||
// Set default command to a repeating select command, with the state getter as the selector | ||
setDefaultCommand(new StateCommand(() -> getState(), this).repeatedly()); | ||
public SystemState getState() { | ||
return m_currentState; | ||
} | ||
} |
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