Lesson 23: Robot Swarms and Multi-Agent Systems

Programming Multiple Robots to Work Together

🎯 Learning Objectives

🐝 Introduction to Swarm Robotics

What is Swarm Robotics?

Swarm robotics is the study of how multiple robots can work together to accomplish tasks that would be difficult or impossible for a single robot. Just like bees in a hive or birds in a flock, robots can coordinate their actions to achieve common goals.

Key Characteristics of Robot Swarms:

  • Multiple robots: Usually many simple robots rather than one complex robot
  • Local communication: Robots talk to nearby neighbors, not a central controller
  • Simple rules: Each robot follows basic rules that create complex group behavior
  • Emergent behavior: The group accomplishes things no single robot could do
  • Fault tolerance: If one robot fails, the swarm continues working

Examples in Nature

Nature provides many examples of swarm behavior:

  • Ant colonies: Find the shortest path to food using pheromone trails
  • Bird flocks: Fly in formation without a leader, avoiding obstacles together
  • Fish schools: Move as one unit to avoid predators and find food
  • Bee swarms: Work together to build hives and collect nectar efficiently

📡 Communication and Coordination

How Robots Communicate

For robots to work together, they need ways to share information:

Communication Methods:

  • Wireless signals: WiFi, Bluetooth, or radio communication
  • Light signals: LEDs that flash patterns or colors
  • Sound signals: Beeps, tones, or ultrasonic communication
  • Movement patterns: Physical movements that other robots can see
  • Shared environment: Leaving markers or changing the environment
// Simple LED communication example
void sendSignal(int robotID, int message) {
  // Flash LED pattern to communicate with other robots
  for (int i = 0; i < robotID; i++) {
    digitalWrite(LED_PIN, HIGH);
    delay(200);
    digitalWrite(LED_PIN, LOW);
    delay(200);
  }
  
  delay(500); // Pause between ID and message
  
  for (int i = 0; i < message; i++) {
    digitalWrite(LED_PIN, HIGH);
    delay(100);
    digitalWrite(LED_PIN, LOW);
    delay(100);
  }
}

Coordination Strategies

Different ways robots can coordinate their actions:

  • Leader-follower: One robot leads, others follow its movements
  • Consensus: All robots agree on a common decision or direction
  • Task allocation: Different robots take on different jobs
  • Formation control: Robots maintain specific positions relative to each other
  • Distributed decision: Each robot makes decisions based on local information

💻 Programming Swarm Behaviors

Basic Flocking Behavior

Flocking is one of the most famous swarm behaviors. It follows three simple rules:

  1. Separation: Avoid crowding neighbors (don't crash into other robots)
  2. Alignment: Steer towards the average heading of neighbors
  3. Cohesion: Steer towards the average position of neighbors
// Simple flocking behavior for miniAuto
void flockingBehavior() {
  float separationDistance = 30; // cm
  float alignmentDistance = 50;  // cm
  float cohesionDistance = 60;   // cm
  
  // Check for nearby robots using ultrasonic sensor
  float frontDistance = getUltrasonicDistance();
  
  // Rule 1: Separation - avoid getting too close
  if (frontDistance < separationDistance) {
    // Turn away from obstacle/robot
    turnRight(90);
    moveForward(20);
    return;
  }
  
  // Rule 2: Alignment - try to match direction of group
  // (In a real swarm, this would use communication)
  // For now, we'll use a simple forward movement
  
  // Rule 3: Cohesion - stay with the group
  // Look for other robots and move towards them if too far
  if (frontDistance > cohesionDistance) {
    // Move forward to stay with group
    moveForward(10);
  } else {
    // Normal exploration movement
    moveForward(15);
    
    // Occasionally turn to explore
    if (random(100) < 10) { // 10% chance
      turnRight(random(30, 60));
    }
  }
}

Leader-Follower Behavior

A simpler approach where one robot leads and others follow:

// Leader robot behavior
void leaderBehavior() {
  // Leader explores and sends signals
  exploreEnvironment();
  
  // Send periodic signals to followers
  if (millis() % 2000 < 100) { // Every 2 seconds
    sendSignal(1, 1); // Leader ID=1, Message=1 (follow me)
  }
}

// Follower robot behavior
void followerBehavior() {
  float followDistance = 40; // cm behind leader
  float frontDistance = getUltrasonicDistance();
  
  // Try to maintain following distance
  if (frontDistance < followDistance - 10) {
    // Too close - slow down or stop
    stopMoving();
    delay(500);
  } else if (frontDistance > followDistance + 10) {
    // Too far - speed up
    moveForward(20);
  } else {
    // Good distance - normal following
    moveForward(15);
  }
  
  // Listen for leader signals
  // (In real implementation, this would check for LED/radio signals)
}

Search and Rescue Swarm

Multiple robots working together to find objects or explore an area:

// Search and rescue behavior
void searchAndRescue() {
  static bool objectFound = false;
  static unsigned long searchStartTime = millis();
  
  // Search pattern - each robot covers different area
  int robotID = getRobotID(); // Each robot has unique ID
  
  if (!objectFound) {
    // Spread out to cover more area
    switch (robotID % 4) {
      case 0: // Robot 0 - search north
        moveForward(30);
        turnRight(90);
        break;
      case 1: // Robot 1 - search east
        turnRight(90);
        moveForward(30);
        break;
      case 2: // Robot 2 - search south
        turnRight(180);
        moveForward(30);
        break;
      case 3: // Robot 3 - search west
        turnRight(270);
        moveForward(30);
        break;
    }
    
    // Check if object found (using sensors)
    if (detectObject()) {
      objectFound = true;
      signalObjectFound();
    }
  } else {
    // Object found - converge on location
    moveTowardsSignal();
  }
}

🛠️ Hands-On Activity: Robot Follow-the-Leader

Project Overview

Create a simple multi-robot system where one robot leads and others follow, using LED signals and distance sensing.

Materials Needed:

  • 2-3 miniAuto robots (or work with other students)
  • Open space for robot movement (classroom or hallway)
  • Obstacles to navigate around (books, boxes)

Implementation Steps:

  1. Program the leader: Create exploration behavior with LED signals
  2. Program followers: Create following behavior based on distance
  3. Test individually: Make sure each robot works on its own
  4. Test together: Run multiple robots and observe coordination
  5. Add obstacles: See how the swarm navigates challenges
  6. Experiment: Try different following distances and patterns

⚠️ Safety and Setup Notes

  • Clear space: Ensure adequate room for multiple robots to move safely
  • Coordination: Work with other students to synchronize robot testing
  • Observation: Have team members watch and take notes on behavior
  • Backup plan: If only one robot available, simulate swarm with manual control

Observation Questions

  1. How well do the follower robots maintain their distance from the leader?
  2. What happens when the leader encounters an obstacle?
  3. Do the robots successfully navigate around obstacles as a group?
  4. What improvements could make the coordination better?
  5. How does this compare to animal behavior you've observed?

📝 Assessment & Homework

Reflection Questions

  1. Describe three examples of swarm behavior in nature and explain how they work.
  2. What are the advantages of using multiple simple robots instead of one complex robot?
  3. Explain the three rules of flocking behavior and why each one is important.
  4. How could robot swarms be useful in real-world applications like search and rescue?
  5. What challenges did you encounter when programming multiple robots to work together?

Programming Challenge

Design a Robot Cleanup Crew: Create a program for multiple robots that work together to "clean up" an area by finding and collecting objects (represented by obstacles they detect).

Requirements:

  • Robots should spread out to cover different areas
  • When a robot finds an "object," it should signal others
  • Other robots should come help or continue searching elsewhere
  • Include obstacle avoidance and coordination behaviors

Research Project

Research one real-world application of swarm robotics and write a one-page report covering:

  • What problem the robot swarm solves
  • How many robots are used and how they coordinate
  • What sensors and communication methods they use
  • Advantages over traditional single-robot solutions
  • Current limitations and future possibilities

Suggested topics: Drone swarms, warehouse robots, environmental monitoring, military applications, or space exploration.

← Lesson 22: State Machines 📚 Semester Overview Take Quiz 📝