RobotCore 1.0
WPILib port to raspberry Pi
Loading...
Searching...
No Matches
org.wpilib.command3.StateMachine Class Reference
Inheritance diagram for org.wpilib.command3.StateMachine:
org.wpilib.command3.Command

Classes

class  State
class  TransitionNeedsTargetStage
class  TransitionNeedsConditionStage

Public Member Functions

 StateMachine (String name)
void setPriority (int priority)
int priority ()
String name ()
Set< Mechanism > requirements ()
State addState (Command command)
TransitionNeedsTargetStage switchFromAny (State... states)
void setInitialState (State initialState)
void run (Coroutine coroutine)
Public Member Functions inherited from org.wpilib.command3.Command
default void onCancel ()
default void onExit ()
default boolean isLowerPriorityThan (Command other)
default boolean requires (Mechanism mechanism)
default boolean conflictsWith (Command other)
default Command withTimeout (Time timeout)
default ParallelGroupBuilder until (BooleanSupplier endCondition)
default SequentialGroupBuilder andThen (Command next)
default ParallelGroupBuilder alongWith (Command... parallel)
default ParallelGroupBuilder raceWith (Command... parallel)

Additional Inherited Members

Static Public Member Functions inherited from org.wpilib.command3.Command
static NeedsNameBuilderStage noRequirements (Consumer< Coroutine > body)
static NeedsExecutionBuilderStage requiring (Mechanism requirement, Mechanism... rest)
static NeedsExecutionBuilderStage requiring (Collection< Mechanism > requirements)
static ParallelGroupBuilder parallel (Command... commands)
static ParallelGroupBuilder race (Command... commands)
static SequentialGroupBuilder sequence (Command... commands)
static NeedsNameBuilderStage waitUntil (BooleanSupplier condition)
static NeedsNameBuilderStage waitFor (Time duration)
Public Attributes inherited from org.wpilib.command3.Command
int DEFAULT_PRIORITY = 0
int LOWEST_PRIORITY = Integer.MIN_VALUE
int HIGHEST_PRIORITY = Integer.MAX_VALUE

Detailed Description

A declarative state machine that can be used to implement complex command routines. State machine setup should be done in stages: first, a state machine is created and its name is set; second, states are added to the state machine using addState(Command); third, transitions between states can be specified using State#switchTo(State):


// Declare the state machine
StateMachine stateMachine = new StateMachine("Example State Machine");

// Declare states
State state1 = stateMachine.addState(...);
State state2 = stateMachine.addState(...);
State state3 = stateMachine.addState(...);

// Set initial state
stateMachine.setInitialState(state1);

// Declare transitions
state1.switchTo(state2).when(...);
state2.switchTo(state3).when(...);

Every state in a state machine runs a single command. While a state's command is running, the state machine will continually check all transitions that can be triggered from that state. If a transition is triggered, the state machine will cancel the state's command and move to the next state as defined by that transition. If no transition is triggered by the time the command completes, the state machine will exit unless a TransitionNeedsConditionStage#whenComplete() transition was specified from that state:


// switch from state1 to state2 when foo is true
state1.switchTo(state2).when(() -> foo == true);

// but if foo never becomes true, switch to state3 when state1 finishes
state1.switchTo(state3).whenComplete();

// no transitions are defined from state2 or state3,
// so the state machine will exit when either state completes

Definition at line 58 of file StateMachine.java.

Constructor & Destructor Documentation

◆ StateMachine()

org.wpilib.command3.StateMachine.StateMachine ( String name)

Creates a new state machine.

Parameters
nameThe name of the state machine. Cannot be null. This will appear in telemetry as the name of the state machine.

Definition at line 74 of file StateMachine.java.

Member Function Documentation

◆ addState()

State org.wpilib.command3.StateMachine.addState ( Command command)

Adds a new state to the state machine. State transitions can be specified on the new state using State#switchTo(State).

Parameters
commandThe command for the state to execute. Cannot be null.
Returns
The newly created state.
Exceptions
IllegalStateExceptionif switchFromAny(State...) has already been called with no arguments.

Definition at line 137 of file StateMachine.java.

◆ name()

String org.wpilib.command3.StateMachine.name ( )

The name of the command.

Returns
the name of the command

Implements org.wpilib.command3.Command.

Definition at line 110 of file StateMachine.java.

◆ priority()

int org.wpilib.command3.StateMachine.priority ( )

The priority of the command. If a command is scheduled that conflicts with another running or pending command, their priority values are compared to determine which should run. If the scheduled command is lower priority than the running command, then it will not be scheduled and the running command will continue to run. If it is the same or higher priority, then the running command will be canceled and the scheduled command will start to run.

Returns
the priority of the command

Because state machines do not directly require any mechanisms, the priority level will be inherited by any state commands that have inherent priority levels lower than the state machine's. This allows state machines to effectively make state commands higher priority than normal, which can be useful for ensuring that commands in the state machine cannot be interrupted by commands that would otherwise be able to interrupt them.

Implements org.wpilib.command3.Command.

Definition at line 105 of file StateMachine.java.

◆ requirements()

Set< Mechanism > org.wpilib.command3.StateMachine.requirements ( )

The mechanisms required by the command. This is used by the scheduler to determine if two commands conflict with each other. No mechanism may be required by more than one running command at a time.

Returns
the set of mechanisms required by the command

State machine commands do not have any requirements of their own. Commands bound to the various states that the machine moves through may have requirements, however.

Implements org.wpilib.command3.Command.

Definition at line 121 of file StateMachine.java.

◆ run()

void org.wpilib.command3.StateMachine.run ( Coroutine coroutine)

Runs the command. Commands that need to periodically run until a goal state is reached should simply run a while loop like while (!atGoal()) { ... } and call Coroutine#yield() at the end of the loop.

Warning: any loops in a command must call coroutine.yield(). Failure to do so will prevent anything else in your robot code from running. Commands are opt-in collaborative constructs; don't be greedy!

Parameters
coroutinethe coroutine backing the command's execution

Implements org.wpilib.command3.Command.

Definition at line 204 of file StateMachine.java.

◆ setInitialState()

void org.wpilib.command3.StateMachine.setInitialState ( State initialState)

Sets the initial state for the state machine. This must be called before the state machine is scheduled. Failure to do so will result in an IllegalStateException being thrown when the state machine is started. Usage of this method is enforced by the WPILib compiler plugin; creating a state machine and neglecting to call this method will result in a compilation error.

Parameters
initialStateThe new initial state. Cannot be null.
See also
PostConstructionInitializer

Definition at line 195 of file StateMachine.java.

◆ setPriority()

void org.wpilib.command3.StateMachine.setPriority ( int priority)

Sets the priority level of the state machine command.

Because state machines do not directly require any mechanisms, the priority level will be inherited by any state commands that have inherent priority levels lower than the state machine's. This allows state machines to effectively make state commands higher priority than normal, which can be useful for ensuring that commands in the state machine cannot be interrupted by commands that would otherwise be able to interrupt them.

Parameters
priorityThe new priority level of the state machine command.
See also
priority()

Definition at line 91 of file StateMachine.java.

◆ switchFromAny()

TransitionNeedsTargetStage org.wpilib.command3.StateMachine.switchFromAny ( State... states)

Sets up a transition from any of the given states to a specific state. If no states are given, the transition will apply to all states in the state machine at the time this method is called and the state machine will no longer permit states to be added.


stateMachine.switchFromAny(state1, state2, state3).to(state4).when(() -> foo == true);

// Functionally equivalent to:
state1.switchTo(state4).when(() -> foo == true);
state2.switchTo(state4).when(() -> foo == true);
state3.switchTo(state4).when(() -> foo == true);

// Set up an early exit condition from any state.
// Using this no-arg variant will prevent states from being added later!
stateMachine.switchFromAny().toExitStateMachine().when(() -> bar == true);

// Functionally equivalent to:
state1.exitStateMachine().when(() -> bar == true);
state2.exitStateMachine().when(() -> bar == true);
state3.exitStateMachine().when(() -> bar == true);
state4.exitStateMachine().when(() -> bar == true);
Parameters
statesThe states to transition from.
Returns
A builder for the transition.

Definition at line 176 of file StateMachine.java.


The documentation for this class was generated from the following file: