Lesson 25: Autonomous Navigation and Path Planning

Teaching Robots to Navigate Complex Environments

🎯 Learning Objectives

πŸ—ΊοΈ Introduction to Autonomous Navigation

What is Autonomous Navigation?

Autonomous navigation is the ability of a robot to move from one location to another without human control. The robot must understand its environment, plan a safe route, and execute the movement while avoiding obstacles and adapting to changes.

Key Components of Navigation:

  • Localization: Knowing where the robot is in the environment
  • Mapping: Understanding the layout of the environment
  • Path Planning: Finding the best route to the destination
  • Obstacle Avoidance: Detecting and avoiding barriers
  • Motion Control: Executing the planned movements accurately

Real-World Examples

Autonomous navigation is used in many applications:

  • Self-driving cars: Navigate roads while avoiding other vehicles and pedestrians
  • Delivery drones: Fly to specific addresses while avoiding obstacles
  • Warehouse robots: Move products efficiently through storage facilities
  • Vacuum robots: Clean rooms systematically without getting stuck
  • Mars rovers: Explore planetary surfaces autonomously

🧭 Path Planning Algorithms

Simple Navigation Strategies

Different algorithms help robots find paths through environments:

1. Wall Following (Bug Algorithm):

  • Move toward goal until obstacle is encountered
  • Follow the obstacle boundary until goal direction is clear
  • Resume moving toward goal
  • Simple but effective for many environments

2. Grid-Based Planning:

  • Divide environment into a grid of cells
  • Mark cells as free or blocked
  • Find path through free cells to destination
  • Good for known environments

3. Potential Field Method:

  • Goal attracts robot like a magnet
  • Obstacles repel robot like same-pole magnets
  • Robot follows the combined force field
  • Works well for dynamic environments

A* (A-Star) Algorithm

A* is one of the most popular path planning algorithms because it finds the shortest path efficiently:

How A* Works:

  1. Start at current position, add to "open list"
  2. For each position, calculate cost to reach goal
  3. Choose position with lowest total cost
  4. Add neighboring positions to open list
  5. Repeat until goal is reached
  6. Trace back to find optimal path

πŸ’» Programming Navigation Systems

Basic Obstacle Avoidance

Let's start with simple obstacle avoidance using ultrasonic sensors:

// Simple obstacle avoidance navigation
void autonomousNavigation() {
  float frontDistance = getUltrasonicDistance();
  float leftDistance = getLeftSensorDistance();
  float rightDistance = getRightSensorDistance();
  
  const float SAFE_DISTANCE = 30; // cm
  const int MOVE_SPEED = 100;
  const int TURN_SPEED = 80;
  
  if (frontDistance > SAFE_DISTANCE) {
    // Path is clear - move forward
    moveForward(MOVE_SPEED);
    Serial.println("Moving forward");
  } else {
    // Obstacle detected - choose best direction
    stopMoving();
    delay(100);
    
    if (leftDistance > rightDistance && leftDistance > SAFE_DISTANCE) {
      // Turn left
      turnLeft(90);
      Serial.println("Turning left to avoid obstacle");
    } else if (rightDistance > SAFE_DISTANCE) {
      // Turn right
      turnRight(90);
      Serial.println("Turning right to avoid obstacle");
    } else {
      // Both sides blocked - turn around
      turnRight(180);
      Serial.println("Turning around - path blocked");
    }
    
    delay(500); // Allow turn to complete
  }
}

Wall Following Algorithm

This algorithm helps robots navigate by following walls or obstacles:

// Wall following navigation
enum WallFollowState {
  SEEKING_WALL,
  FOLLOWING_WALL,
  TURNING_CORNER
};

WallFollowState currentState = SEEKING_WALL;
const float WALL_DISTANCE = 20; // Desired distance from wall (cm)
const float WALL_TOLERANCE = 5;  // Acceptable distance variation

void wallFollowingNavigation() {
  float frontDistance = getUltrasonicDistance();
  float rightDistance = getRightSensorDistance();
  
  switch (currentState) {
    case SEEKING_WALL:
      if (frontDistance < 30) {
        // Found obstacle - start following
        currentState = FOLLOWING_WALL;
        turnRight(90); // Orient to follow wall
      } else {
        moveForward(100);
      }
      break;
      
    case FOLLOWING_WALL:
      if (frontDistance < 25) {
        // Corner or obstacle ahead
        currentState = TURNING_CORNER;
        stopMoving();
      } else if (rightDistance < WALL_DISTANCE - WALL_TOLERANCE) {
        // Too close to wall - turn left slightly
        turnLeft(15);
        moveForward(80);
      } else if (rightDistance > WALL_DISTANCE + WALL_TOLERANCE) {
        // Too far from wall - turn right slightly
        turnRight(15);
        moveForward(80);
      } else {
        // Good distance - move forward
        moveForward(100);
      }
      break;
      
    case TURNING_CORNER:
      turnLeft(90); // Turn around corner
      delay(1000);
      currentState = FOLLOWING_WALL;
      break;
  }
}

Simple Grid-Based Path Planning

For known environments, we can use a simple grid to plan paths:

// Simple grid-based navigation
const int GRID_SIZE = 10;
int environmentGrid[GRID_SIZE][GRID_SIZE];
int robotX = 0, robotY = 0; // Current position
int goalX = 8, goalY = 8;   // Destination

void initializeGrid() {
  // 0 = free space, 1 = obstacle
  for (int x = 0; x < GRID_SIZE; x++) {
    for (int y = 0; y < GRID_SIZE; y++) {
      environmentGrid[x][y] = 0; // Start with empty grid
    }
  }
  
  // Add some obstacles
  environmentGrid[3][3] = 1;
  environmentGrid[3][4] = 1;
  environmentGrid[4][3] = 1;
  environmentGrid[6][7] = 1;
}

void moveToGoal() {
  // Simple greedy approach - move toward goal if possible
  int deltaX = goalX - robotX;
  int deltaY = goalY - robotY;
  
  if (abs(deltaX) > abs(deltaY)) {
    // Move in X direction
    if (deltaX > 0 && canMoveTo(robotX + 1, robotY)) {
      moveEast();
      robotX++;
    } else if (deltaX < 0 && canMoveTo(robotX - 1, robotY)) {
      moveWest();
      robotX--;
    } else {
      // X blocked, try Y
      if (deltaY > 0 && canMoveTo(robotX, robotY + 1)) {
        moveNorth();
        robotY++;
      } else if (deltaY < 0 && canMoveTo(robotX, robotY - 1)) {
        moveSouth();
        robotY--;
      }
    }
  } else {
    // Move in Y direction
    if (deltaY > 0 && canMoveTo(robotX, robotY + 1)) {
      moveNorth();
      robotY++;
    } else if (deltaY < 0 && canMoveTo(robotX, robotY - 1)) {
      moveSouth();
      robotY--;
    } else {
      // Y blocked, try X
      if (deltaX > 0 && canMoveTo(robotX + 1, robotY)) {
        moveEast();
        robotX++;
      } else if (deltaX < 0 && canMoveTo(robotX - 1, robotY)) {
        moveWest();
        robotX--;
      }
    }
  }
  
  // Check if goal reached
  if (robotX == goalX && robotY == goalY) {
    Serial.println("Goal reached!");
    celebrateGoal();
  }
}

bool canMoveTo(int x, int y) {
  // Check if position is valid and not blocked
  if (x < 0 || x >= GRID_SIZE || y < 0 || y >= GRID_SIZE) {
    return false; // Out of bounds
  }
  return environmentGrid[x][y] == 0; // Not blocked
}

void moveEast() {
  turnToDirection(0);   // Face east
  moveForward(50);      // Move one grid cell
  delay(1000);
}

void moveNorth() {
  turnToDirection(90);  // Face north
  moveForward(50);
  delay(1000);
}

void moveWest() {
  turnToDirection(180); // Face west
  moveForward(50);
  delay(1000);
}

void moveSouth() {
  turnToDirection(270); // Face south
  moveForward(50);
  delay(1000);
}

πŸ› οΈ Hands-On Activity: Autonomous Maze Navigator

Project Overview

Create an autonomous navigation system that can navigate through a simple maze or obstacle course to reach a target destination.

Materials Needed:

  • miniAuto robot with ultrasonic sensors
  • Cardboard boxes or books to create maze walls
  • Tape to mark start and goal positions
  • Open floor space (classroom or hallway)

Implementation Steps:

  1. Design maze: Create a simple maze with walls and clear paths
  2. Program navigation: Implement wall-following or obstacle avoidance
  3. Test basic movement: Verify robot can detect and avoid walls
  4. Add goal detection: Use color sensor or marker to detect destination
  5. Optimize path: Improve algorithm for faster navigation
  6. Challenge mode: Try more complex mazes or moving obstacles

⚠️ Safety and Setup Notes

  • Clear boundaries: Ensure robot cannot fall off tables or hit people
  • Emergency stop: Always have a way to quickly stop the robot
  • Sensor calibration: Test sensors in the actual environment
  • Speed limits: Use moderate speeds for safety and accuracy
  • Backup plan: Have manual control ready if autonomous mode fails

Observation and Testing

  1. How well does the robot avoid obstacles?
  2. Does it get stuck in corners or dead ends?
  3. How does it handle narrow passages?
  4. What happens when the environment changes?
  5. How could the navigation algorithm be improved?

πŸ“ Assessment & Homework

Reflection Questions

  1. What are the five key components of autonomous navigation?
  2. Explain how the wall-following algorithm works and when it's useful.
  3. What are the advantages and disadvantages of grid-based path planning?
  4. How do self-driving cars use autonomous navigation principles?
  5. What challenges might a robot face when navigating in a dynamic environment?

Programming Challenge

Smart Delivery Robot: Create a navigation system for a robot that can deliver items between multiple locations in a building.

Requirements:

  • Navigate between at least 3 different "rooms" (marked areas)
  • Avoid obstacles and navigate around furniture
  • Return to a "home base" when delivery is complete
  • Handle situations where paths are blocked
  • Provide status updates during navigation

Research Project

Research one real-world application of autonomous navigation and write a one-page report covering:

  • What type of environment the system navigates
  • What sensors and algorithms are used
  • What challenges the system faces
  • How the system handles unexpected situations
  • Future improvements or developments planned

Suggested topics: Autonomous vehicles, delivery drones, warehouse robots, Mars rovers, or underwater exploration vehicles.

← Lesson 24: Face Recognition πŸ“š Semester Overview Take Quiz πŸ“