Clear, robust subsystem behavior comes from well-structured state machines. Use this guide to design, implement, and debug yours effectively.


Overview

A state machine is a way to organize the different tasks your subsystem needs to complete. For an oven, example states might be "Off", "Warming", "Baking", or "Convection". The state machine tells the robot what it should be doing now and what it should do next.


What is a state?

A state bundles the actions your robot should take for a specific mode of operation.


Programming a state machine

  1. Define your states
  2. Write the periodic loop
  3. Implement behavior per state

From request to state

This is a function that will go inside of your companion object along with your enum of states. It will take a “Request” which is created by your superstructure and set your current state to that request. See example code below for how to write this.


What kinds of states should I add?

Start with a minimal, reliable set and expand as needed.