Lesson 22

Advanced Robot Behaviors and State Machines

🎯 Learning Objectives

🔄 Introduction to State Machines

What is a State Machine?

A state machine is a way to organize robot behavior by breaking it down into different "states" or modes. Think of it like the different modes your phone can be in: silent mode, vibrate mode, or ring mode. Your phone behaves differently in each state, and it switches between states based on what you do.

For robots, states might be: "searching for objects," "following a line," "avoiding obstacles," or "returning home." The robot does different things in each state and switches states based on what its sensors detect.

Why Use State Machines?

  • Organization: Makes complex robot behavior easier to understand and debug
  • Predictability: You always know what the robot should be doing in each state
  • Flexibility: Easy to add new behaviors or modify existing ones
  • Reliability: Prevents the robot from getting confused or stuck
  • Modularity: Each state can be programmed and tested separately

Real-World Examples

State machines are everywhere in technology:

  • Traffic Lights: Red → Yellow → Green → Red (states change based on timers)
  • Washing Machine: Fill → Wash → Rinse → Spin → Done
  • Video Games: Menu → Playing → Paused → Game Over
  • Elevators: Idle → Moving Up → Moving Down → Door Open
  • Roomba Vacuum: Cleaning → Charging → Stuck → Returning Home

📋 Designing Simple State Machines

Step 1: Identify the States

Start by listing all the different things your robot needs to do. For example, a simple exploring robot might have:

  • EXPLORING: Moving forward and looking around
  • AVOIDING: Turning away from obstacles
  • STUCK: Backing up and trying a different direction
  • RETURNING: Going back to the starting point

Step 2: Define State Transitions

Next, decide when the robot should switch from one state to another. These are called "transitions":

// Example state transitions for exploring robot:

EXPLORING → AVOIDING: when obstacle detected
AVOIDING → EXPLORING: when path is clear
EXPLORING → STUCK: when same position for too long
STUCK → EXPLORING: after backup maneuver complete
ANY STATE → RETURNING: when battery is low

Step 3: Program Each State

Write the code for what the robot should do in each state:

// Simple state machine structure
enum RobotState {
  EXPLORING,
  AVOIDING,
  STUCK,
  RETURNING
};

RobotState currentState = EXPLORING;

void loop() {
  switch (currentState) {
    case EXPLORING:
      exploreMode();
      break;
    case AVOIDING:
      avoidMode();
      break;
    case STUCK:
      stuckMode();
      break;
    case RETURNING:
      returnMode();
      break;
  }
}

💻 Programming State Machines

Complete State Machine Example

Here's a complete example of a robot that explores, avoids obstacles, and returns home when the battery is low:

#include <Servo.h>

// Robot states
enum RobotState {
  EXPLORING,
  AVOIDING,
  STUCK,
  RETURNING,
  CHARGING
};

RobotState currentState = EXPLORING;
unsigned long stateStartTime = 0;
unsigned long lastPositionTime = 0;
float lastX = 0, lastY = 0;

void setup() {
  Serial.begin(9600);
  initializeRobot();
  stateStartTime = millis();
  Serial.println("Robot starting in EXPLORING state");
}

void loop() {
  // Check for state transitions first
  checkStateTransitions();
  
  // Execute current state behavior
  switch (currentState) {
    case EXPLORING:
      exploreMode();
      break;
    case AVOIDING:
      avoidMode();
      break;
    case STUCK:
      stuckMode();
      break;
    case RETURNING:
      returnMode();
      break;
    case CHARGING:
      chargingMode();
      break;
  }
  
  delay(100); // Small delay for stability
}

State Transition Logic

The key to good state machines is clear transition logic:

void checkStateTransitions() {
  float batteryLevel = getBatteryLevel();
  float distance = getUltrasonicDistance();
  bool isStuck = checkIfStuck();
  
  // Battery low - always return home
  if (batteryLevel < 20 && currentState != RETURNING && currentState != CHARGING) {
    changeState(RETURNING);
    return;
  }
  
  // State-specific transitions
  switch (currentState) {
    case EXPLORING:
      if (distance < 20) {  // Obstacle detected
        changeState(AVOIDING);
      } else if (isStuck) {  // Haven't moved in a while
        changeState(STUCK);
      }
      break;
      
    case AVOIDING:
      if (distance > 30) {  // Path is clear
        changeState(EXPLORING);
      }
      break;
      
    case STUCK:
      if (millis() - stateStartTime > 3000) {  // Been stuck for 3 seconds
        changeState(EXPLORING);
      }
      break;
      
    case RETURNING:
      if (isAtHome()) {
        changeState(CHARGING);
      }
      break;
  }
}

Individual State Functions

Each state has its own function that defines what the robot does:

void exploreMode() {
  // Move forward slowly and scan for interesting things
  moveForward(100);  // Slow speed for exploration
  
  // Occasionally turn to explore different directions
  if (millis() - stateStartTime > 5000) {
    turnRandom();
    stateStartTime = millis();
  }
}

void avoidMode() {
  // Stop and turn away from obstacle
  stopMotors();
  delay(500);
  
  // Turn right or left based on which side is clearer
  if (scanLeft() > scanRight()) {
    turnLeft();
  } else {
    turnRight();
  }
  
  delay(1000);  // Turn for 1 second
}

void stuckMode() {
  // Back up and turn to get unstuck
  moveBackward(150);
  delay(1000);
  
  // Turn a random amount
  if (random(2) == 0) {
    turnLeft();
  } else {
    turnRight();
  }
  delay(random(1000, 3000));  // Turn for 1-3 seconds
}

void returnMode() {
  // Navigate back to starting position using GPS or dead reckoning
  navigateToHome();
}

void chargingMode() {
  // Stop all movement and wait
  stopMotors();
  Serial.println("Charging... Press reset to restart exploration");
}

🔬 Hands-On Activity: Autonomous Pet Robot

Project Goal

Create an autonomous "pet" robot that exhibits lifelike behaviors using state machines. Your robot pet will explore, play, rest, and interact with its environment just like a real pet!

Pet Robot States

  1. EXPLORING: Wanders around curiously, looking for interesting things
  2. PLAYING: Follows moving objects or chases lights
  3. RESTING: Sits still and "sleeps" for a while
  4. SOCIALIZING: Responds to people (sound, movement, or touch)
  5. HUNGRY: Searches for "food" (specific colored objects)
  6. SCARED: Backs away from loud noises or sudden movements

Implementation Steps

  1. Design Your Pet's Personality: Decide what makes your robot pet unique
  2. Map Out State Transitions: When does your pet change behaviors?
  3. Program Basic States: Start with 3-4 simple states
  4. Add Sensors: Use ultrasonic, camera, microphone, or touch sensors
  5. Test and Refine: Observe your pet and adjust behaviors
  6. Add Personality: Include random behaviors and "moods"

Bonus Features

  • Memory: Remember where it found food or encountered obstacles
  • Learning: Adapt behavior based on past experiences
  • Communication: Make different sounds or light patterns for each state
  • Time Awareness: Different behaviors at different times of day
  • Energy System: Pet gets tired and needs to rest periodically

📋 Assessment & Homework

Practice Exercises

  1. Create a simple 3-state machine (explore, avoid, rest) and test it
  2. Add proper state transition logging to track your robot's behavior
  3. Implement a "stuck detection" system that works reliably
  4. Design a state machine for a specific task (cleaning, security, entertainment)
  5. Test your robot's state machine in different environments

Challenge Projects

  • Smart Security Robot: Patrol, investigate, alert, and return to base
  • Interactive Game Robot: Play hide-and-seek or tag with humans
  • Ecosystem Robot: Multiple robots with different roles working together
  • Adaptive Robot: Changes behavior based on environment and experience

Reflection Questions

  1. How do state machines make robot programming easier to understand and debug?
  2. What happens when a robot gets stuck between two states? How can you prevent this?
  3. How could you use state machines to create more realistic robot behaviors?
  4. What real-world systems use state machines that you interact with daily?
← Lesson 21: GPS Navigation 📚 Semester Overview Take Quiz →