Skip to main content
Torqheart
← Foundations

Programming

State machines: states and transitions

A state machine splits behaviour into clear states and the conditions that move between them, so robot code stays readable and never does two conflicting things at once.

See it first

3D teardown

Inside a quadruped robot

Open full screen
FreeParts, Gaits🔒 In an upcoming courseCentre of massDrag to turn, use the buttons to take it apart. Numbers come from published research; our own estimates are marked on the page. A model for learning, not a product spec.

The teardown opens on its “Gaits” mode, which shows this principle. Drag to turn it.

One step at a time

Step by step

State machines: states and transitions

Step 1 of 5

Name the states

List them all, the fault state included.

In words

Each leg of a quadruped switches between two states: stance, carrying weight, and swing, stepping forward. A gait is the schedule of which leg is in which state when.

The whole system works the same way: idle → ready → running → fault, and every state must have a path to a safe stop.

In code

enum class Mode { Idle, Ready, Walking, Fault };
Mode step(Mode m, const Inputs& in) {
  if (in.fault) return Mode::Fault;        // every state can stop
  switch (m) {
    case Mode::Idle:    return in.armed ? Mode::Ready : m;
    case Mode::Ready:   return in.go ? Mode::Walking : m;
    case Mode::Walking: return in.stop ? Mode::Ready : m;
    case Mode::Fault:   return in.reset ? Mode::Idle : m;
  }
  return m;
}

Where you meet this in a robot

In the quadruped teardown, watch each leg alternate stance and swing in rhythm: a state machine you can see.