Skip to content

Add state transitions and command lifecycle documentation to StateMachine guide - #161

Draft
TaylerUva with Copilot wants to merge 4 commits into
mainfrom
copilot/update-statemachine-guide
Draft

Add state transitions and command lifecycle documentation to StateMachine guide#161
TaylerUva with Copilot wants to merge 4 commits into
mainfrom
copilot/update-statemachine-guide

Conversation

Copilot AI commented Jan 31, 2026

Copy link
Copy Markdown
Contributor

The StateMachine guide lacked practical implementation details for state transitions and command structure. Added comprehensive sections covering transition patterns and command lifecycle methods with proper state machine pattern guidance.

Changes

State Transitions

  • Button-triggered vs condition-based transition patterns
  • Validation logic and target state handling
  • Debounced button usage (getAButtonPressed())

Command Lifecycle Methods (State Machine Pattern)

  • initialize() - Setup: reset encoders, initialize flags, log command start, start motors for the state
  • execute() - Monitoring and feedback: sensor polling, adjustments (motors already running from initialize)
  • end(boolean interrupted) - DO NOT stop motors in state machines - only log completion, next state's initialize takes control
  • isFinished() - Completion patterns with guidance:
    • Sensor-based (autonomous actions needing confirmation)
    • Time-based (timeout/duration requirements)
    • Position-based (target tolerance checks)
    • Manual control (return false for teleoperated)

State Machine Pattern

In state machines, motor control transitions directly between states:

  • Motors start in the new state's initialize()
  • Motors do not stop in the old state's end()
  • This enables smooth transitions without interruption

Example

public class IntakeCommand extends Command {
    private final Intake subIntake;
    private final Timer timeout = new Timer();
    
    @Override
    public void initialize() {
        subIntake.resetSensors();
        timeout.restart();
        // Start motors for this state
        subIntake.setSpeed(constIntake.INTAKE_SPEED);
    }
    
    @Override
    public void execute() {
        // Monitor sensors (motors already running)
        // Adjust if needed based on feedback
    }
    
    @Override
    public void end(boolean interrupted) {
        // DO NOT stop motors here
        // Next state's initialize() will take control
        System.out.println("State transition...");
    }
    
    @Override
    public boolean isFinished() {
        return subIntake.hasGamePiece() || timeout.hasElapsed(3.0);
    }
}

Screenshot

Original prompt

This section details on the original issue you should resolve

<issue_title>Update StateMachine guide</issue_title>
<issue_description>- how to do state transitions

  • how to do commands
    • what to put in initialize
    • what to put in end
    • is finished should return true or return a sensor check</issue_description>

Comments on the Issue (you are @copilot in this section)


✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

Copilot AI and others added 2 commits January 31, 2026 18:04
…hine guide

Co-authored-by: TaylerUva <8679670+TaylerUva@users.noreply.github.com>
…at field declaration

Co-authored-by: TaylerUva <8679670+TaylerUva@users.noreply.github.com>
Copilot AI changed the title [WIP] Update StateMachine guide for commands and transitions Add state transitions and command lifecycle documentation to StateMachine guide Jan 31, 2026
Copilot AI requested a review from TaylerUva January 31, 2026 18:06

@TaylerUva TaylerUva left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For a state machine end should not be used

Co-authored-by: TaylerUva <8679670+TaylerUva@users.noreply.github.com>

Copilot AI commented Jan 31, 2026

Copy link
Copy Markdown
Contributor Author

For a state machine end should not be used

Fixed in 31c3c91. Updated the documentation to clarify that in state machines, end() should NOT stop motors. Motor control transfers directly to the next state's initialize() method.

Key changes:

  • Added warning that motors should not be stopped in end() for state machines
  • Moved motor setup to initialize() in all examples
  • Updated execute() to focus on monitoring since motors run from initialize()
  • Added clear "DO NOT" list for what to avoid in end()

Updated Documentation

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Update StateMachine guide

2 participants