StateMachine

class StateMachine<S>(initialState: S)

Generic state machine for subsystems.

Manages state transitions with history tracking and validation.

Example:

enum class ElevatorState(val position: Double) {
DEFAULT(0.0),
L1(0.5),
L2(1.0)
}

object Elevator : SubsystemBase() {
private val stateMachine = StateMachine(ElevatorState.DEFAULT)

var state: ElevatorState
get() = stateMachine.currentState
set(value) = stateMachine.transitionTo(value)

override fun periodic() {
// Execute behavior based on current state
setPosition(state.position)
}
}

Parameters

S

State type (typically an enum)

initialState

Starting state

Constructors

Link copied to clipboard
constructor(initialState: S)

Properties

Link copied to clipboard

Current state of the machine

Link copied to clipboard
var onTransition: (from: S, to: S) -> Unit?

Optional: Callback invoked on every state transition

Functions

Link copied to clipboard

Clear state history (useful for testing or memory management).

Link copied to clipboard
fun history(): List<S>

Get full state history.

Link copied to clipboard

Get the previous state (if any).

Link copied to clipboard
fun transitionTo(newState: S)

Transition to a new state.