Programming Multiple Robots to Work Together
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.
Nature provides many examples of swarm behavior:
For robots to work together, they need ways to share information:
// 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);
}
} Different ways robots can coordinate their actions:
Flocking is one of the most famous swarm behaviors. It follows three simple rules:
// 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));
}
}
} 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)
} 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();
}
} Create a simple multi-robot system where one robot leads and others follow, using LED signals and distance sensing.
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).
Research one real-world application of swarm robotics and write a one-page report covering:
Suggested topics: Drone swarms, warehouse robots, environmental monitoring, military applications, or space exploration.