Zbotic Logo Zbotic Logo
  • Home
  • Shop
  • Sale
  • 3D Print Service
  • PCB Service
  • B2B
  • Blogs
  • Contact Us
0 0

View Wishlist Add all to cart

0 0
0 Shopping Cart
Shopping cart (0)
Subtotal: ₹0.00

View cartCheckout

  • Shop
  • About Us
  • Contact Us
  • Reseller
  • Blogs
020 69134444
1800 209 0998
[email protected]
Help Desk
Facebook Twitter Instagram Linkedin YouTube
Zbotic Logo Zbotic Logo
0 0

View Wishlist Add all to cart

0 0
0 Shopping Cart
Shopping cart (0)
Subtotal: ₹0.00

View cartCheckout

All departments
  • 3D Print Service
  • 3D Printer
  • Batteries & Chargers
  • Development Boards
  • Drone Parts
  • EBike parts
  • Sensor Modules
  • Electronic Components
  • Electronic Modules
  • IoT and Wireless
  • Mechanical Parts and Workbench Tools
  • Motors & Drivers & Pumps & Actuators
  • DIY and Robot Kits
  • Show more
  • Home
  • Shop
  • Sale
  • 3D Print Service
  • PCB Service
  • B2B
  • Blogs
  • Contact Us
Return to previous page
Home Robotics & DIY

Robot Path Planning: A* Algorithm Implementation in Python

Robot Path Planning: A* Algorithm Implementation in Python

March 11, 2026 /Posted byJayesh Jain / 0

Implementing robot path planning with the A* algorithm in Python is a foundational skill for anyone building autonomous robots. Whether you are working on a warehouse robot, a self-driving RC car, or a competition maze solver, A* provides an efficient and optimal solution to the question: “What is the shortest path from A to B, avoiding obstacles?” This tutorial walks through the complete implementation, from grid map representation to actual robot deployment.

Table of Contents

  • Why Path Planning Matters in Robotics
  • A* Algorithm: Theory and Intuition
  • Python Implementation of A*
  • Grid Maps and Occupancy Grids
  • Deploying A* on a Real Robot
  • Beyond A*: Advanced Planning Algorithms
  • Frequently Asked Questions

Why Path Planning Matters in Robotics

Path planning is the process of finding a collision-free trajectory from a start position to a goal position. Without it, a robot can only react to immediate obstacles (reactive navigation) but cannot plan efficient routes through complex environments. In real-world applications — from AGVs (Automated Guided Vehicles) in Indian factories to household robots — path planning is indispensable.

The core challenge is balancing three competing requirements:

  • Completeness: Will the algorithm always find a path if one exists?
  • Optimality: Does it find the shortest (or lowest-cost) path?
  • Computational efficiency: Does it run fast enough for real-time use?

A* satisfies all three when given an admissible heuristic — making it the go-to algorithm for grid-based robot path planning.

Recommended: Waveshare AlphaBot2 Robot Building Kit for Raspberry Pi — An ideal hardware platform for testing A* path planning algorithms on a real wheeled robot.

A* Algorithm: Theory and Intuition

A* is an informed search algorithm that combines the benefits of Dijkstra’s algorithm (guaranteed shortest path) with greedy best-first search (fast exploration towards the goal). It assigns each node a cost:

f(n) = g(n) + h(n)

Where:

  • g(n): The actual cost from the start node to node n (known)
  • h(n): A heuristic estimate of the cost from n to the goal (estimated)
  • f(n): The total estimated cost of the path through n

A* explores nodes in order of f(n), always expanding the most promising node first. Common heuristics for grid maps:

  • Manhattan distance: |dx| + |dy| — for 4-connected grids (no diagonal movement)
  • Euclidean distance: sqrt(dx² + dy²) — for 8-connected grids or continuous spaces
  • Diagonal distance: max(|dx|, |dy|) — for 8-connected grids with diagonal movement

A heuristic is admissible if it never overestimates the true cost. Admissibility guarantees A* finds the optimal path.

Python Implementation of A*

import heapq
import numpy as np
import matplotlib.pyplot as plt
from typing import List, Tuple, Optional

class AStarPlanner:
    def __init__(self, grid: np.ndarray):
        """
        grid: 2D numpy array where 0=free, 1=obstacle
        """
        self.grid = grid
        self.rows, self.cols = grid.shape
    
    def heuristic(self, a: Tuple, b: Tuple) -> float:
        """Euclidean distance heuristic"""
        return np.sqrt((a[0] - b[0])**2 + (a[1] - b[1])**2)
    
    def get_neighbours(self, node: Tuple) -> List[Tuple]:
        """Get valid 8-connected neighbours"""
        r, c = node
        neighbours = []
        for dr in [-1, 0, 1]:
            for dc in [-1, 0, 1]:
                if dr == 0 and dc == 0:
                    continue
                nr, nc = r + dr, c + dc
                if (0 <= nr < self.rows and 
                    0 <= nc  Optional[List[Tuple]]:
        """Find optimal path from start to goal using A*"""
        if self.grid[start] == 1 or self.grid[goal] == 1:
            return None  # Start or goal is blocked
        
        # Priority queue: (f_cost, g_cost, node)
        open_set = [(0, 0, start)]
        came_from = {}
        g_scores = {start: 0}
        
        while open_set:
            f, g, current = heapq.heappop(open_set)
            
            if current == goal:
                # Reconstruct path
                path = []
                while current in came_from:
                    path.append(current)
                    current = came_from[current]
                path.append(start)
                return path[::-1]
            
            for nr, nc, move_cost in self.get_neighbours(current):
                neighbour = (nr, nc)
                tentative_g = g + move_cost
                
                if tentative_g < g_scores.get(neighbour, float('inf')):
                    came_from[neighbour] = current
                    g_scores[neighbour] = tentative_g
                    h = self.heuristic(neighbour, goal)
                    heapq.heappush(open_set, 
                                   (tentative_g + h, tentative_g, neighbour))
        
        return None  # No path found

# Example usage
grid = np.zeros((20, 20), dtype=int)
# Add some obstacles
grid[5:15, 8] = 1
grid[2:10, 14] = 1

planner = AStarPlanner(grid)
path = planner.plan((0, 0), (19, 19))

if path:
    print(f"Path found with {len(path)} steps")
    # Visualise
    fig, ax = plt.subplots()
    ax.imshow(grid, cmap='Greys', origin='upper')
    path_arr = np.array(path)
    ax.plot(path_arr[:, 1], path_arr[:, 0], 'b-', linewidth=2)
    ax.plot(path_arr[0, 1], path_arr[0, 0], 'go', markersize=10)  # Start
    ax.plot(path_arr[-1, 1], path_arr[-1, 0], 'ro', markersize=10)  # Goal
    plt.title('A* Path Planning')
    plt.savefig('astar_path.png', dpi=100)
    print("Path visualised and saved")
else:
    print("No path found!")

Grid Maps and Occupancy Grids

In real robotics, the environment is represented as an occupancy grid — a probabilistic map where each cell has a probability of being occupied. Sensors (lidar, ultrasonic, infrared) update these probabilities as the robot explores.

Converting sensor readings to a grid map:

class OccupancyGrid:
    def __init__(self, width, height, resolution=0.05):
        """resolution in metres per cell"""
        self.width = width
        self.height = height
        self.resolution = resolution
        # Log-odds representation for numerical stability
        self.log_odds = np.zeros((height, width))
    
    def update(self, x, y, occupied: bool):
        """Update cell at world coordinates (x, y) metres"""
        col = int(x / self.resolution)
        row = int(y / self.resolution)
        if 0 <= row < self.height and 0 <= col  threshold).astype(int)
Recommended: Waveshare General Driver Board for Robots (ESP32) — Handles motor control and sensor interfaces while Raspberry Pi runs the A* planning algorithm.

Deploying A* on a Real Robot

Connecting path planning to a physical robot involves several steps:

  1. Map the environment: Use SLAM (Simultaneous Localisation and Mapping) with ultrasonic or lidar sensors to build an occupancy grid.
  2. Localise the robot: Know where the robot is on the map using wheel odometry, IMU, or external markers.
  3. Plan the path: Run A* on the current occupancy grid from robot’s position to goal.
  4. Execute the path: Convert grid waypoints to motor commands. A PID controller handles the actual steering.
  5. Replan dynamically: If new obstacles appear, rerun A* with the updated grid.
Recommended: Waveshare ESP32 Servo Driver Expansion Board — WiFi connectivity allows remote monitoring of path planning progress and sending new goal positions.

Beyond A*: Advanced Planning Algorithms

Once you are comfortable with A*, explore these more advanced algorithms:

  • D* Lite: An incremental version of A* that efficiently replans when obstacles change — critical for dynamic environments.
  • RRT (Rapidly-exploring Random Tree): Better for high-dimensional spaces (robot arms, 6-DOF robots) where grid-based approaches are impractical.
  • RRT* (RRT-star): Asymptotically optimal version of RRT — finds better paths with more planning time.
  • Theta*: Any-angle path planning on grids — produces smoother, shorter paths than A* by allowing movement in any direction, not just grid directions.

Frequently Asked Questions

What is the time complexity of A*?

A*’s worst-case time complexity is O(b^d) where b is the branching factor and d is the depth of the optimal solution. With a good heuristic, it typically explores far fewer nodes than this worst case. For a 100×100 grid, A* usually completes in milliseconds on a Raspberry Pi.

How do I handle moving obstacles with A*?

For moving obstacles, use dynamic replanning. The simplest approach: run A* periodically (e.g., every 500ms) with the latest sensor data. More sophisticated: use D* Lite for efficient incremental replanning, or add time as a third dimension to the grid.

Can A* be used for drone path planning?

Yes, but you need a 3D grid instead of a 2D one, which increases computational cost significantly. For drones, RRT or its variants are often preferred for 3D continuous spaces. A* works well for 2D floor plan navigation of indoor drones.

How do I implement A* in Arduino?

Arduino has limited RAM (2KB on Uno) which severely limits grid size. A 10×10 grid with 4 bytes per cell uses just 400 bytes — feasible. Use int8_t arrays and static allocation. For larger maps, use ESP32 (520KB RAM) or Raspberry Pi.

What is the difference between A* and Dijkstra’s algorithm?

Dijkstra’s algorithm is A* with h(n) = 0 — it expands all nodes in order of their distance from the start, guaranteeing optimality but exploring many more nodes than A*. A* uses the heuristic to focus the search towards the goal, making it much faster in practice.

Shop Robotics & Automation at Zbotic →

Tags: A* algorithm, autonomous navigation, path planning, Python robotics, robot programming
Share Post
  • Facebook
  • Linkedin
  • Whatsapp
Gold vs HASL vs ENIG PCB Finis...
blog gold vs hasl vs enig pcb finish which should you choose 598107
blog mobile robot control via web interface flask raspberry pi 598111
Mobile Robot Control via Web I...

Related posts

Svg%3E
Read more

Caterpillar Track Robot: Tank-Drive Build for All Terrain

April 1, 2026 0
When wheels lose grip on sand, gravel, grass, or loose surfaces, caterpillar tracks keep moving. A tank-track robot distributes its... Continue reading
Svg%3E
Read more

RC Car to Robot: Convert a Toy Car into an Autonomous Robot

April 1, 2026 0
That old RC toy car gathering dust can be transformed into an Arduino-controlled autonomous robot with just a few electronic... Continue reading
Svg%3E
Read more

Robotic Arm Kit India: Best Options for Students and Hobbyists

April 1, 2026 0
If you are a student or hobbyist looking to get into robotics, a robotic arm kit is one of the... Continue reading
Svg%3E
Read more

Sumo Robot: Competition Build Guide India

April 1, 2026 0
Sumo robot competitions are among the most exciting events in Indian robotics, pitting small autonomous robots against each other in... Continue reading
Svg%3E
Read more

Robot Arm Build: 6-DOF Servo Arm with Arduino Control

April 1, 2026 0
Building a 6-DOF robot arm with servo motors and Arduino is one of the most rewarding robotics projects you can... Continue reading

Add comment Cancel reply

Your email address will not be published. Required fields are marked

Facebook Twitter Instagram Pinterest Linkedin Youtube

Get the latest deals and more.

Download on Google Play Download on the App Store

Call us: 020 69134444 / 1800 209 0998

Monday - Saturday 09:30 AM - 06:00 PM
For Technical Supports Email: [email protected]
For Sales / Enquiries Email: [email protected]

  • My Account

    • Cart

    • Wishlist

    • Checkout

    • My Orders

    • Track Order

    • My Account

  • Information

    • FAQs

    • Blogs

    • Career

    • About Us

    • Contact Us

    • Payment Options

  • Policies

    • Privacy Policy

    • Terms & Conditions

    • GST Input Tax Credit

    • Shipping Return Policy

    • E-Waste Collection Points

    • Our Sitemap

© Zbotic.in is registered trademark of Moxie Supply Pvt Ltd – All Rights Reserved
Login
Use Phone Number
Use Email Address
Not a member yet? Register Now
Reset Password
Use Phone Number
Use Email Address
Register
Already a member? Login Now