Please enable JavaScript to use CodeHS

Robotics // Algorithms // Beginner

How Do Robots Find Their Way? Path Planning Explained

A robot in a busy warehouse cannot just walk straight at its goal. There are shelves, boxes, and people in the way. Learn how a robot turns a map into a route, then paint your own maze, race the robot, and watch it re-plan when something moves.

Robotics ~15 min Beginner friendly
01 / The Big Idea

A robot needs a map, a start, and a goal

Think about the map app on a phone. You say where you want to go. The app already knows where you are. It looks at the roads and gives you a route. A robot does the same thing. This job is called path planning: picking a safe route from where you are to where you want to be.

Every plan needs three things.

A mapA picture of the world that shows which places are free and which are blocked.
A startWhere the robot is right now. Robots find this with sensors, like GPS outdoors or wheel counters indoors.
A goalThe place the robot needs to reach, like the shelf that holds the package.

Where does the map come from? Sometimes people give it to the robot. Often the robot builds its own with sensors. A sensor is a part that measures the world. A camera sees. A laser sensor called lidar measures how far away things are. A bump sensor feels a wall.

algorithmA list of exact steps for solving a problem. A path planner is an algorithm.
obstacleAnything the robot must not hit: a wall, a shelf, a box, a person.
pathThe list of squares the robot visits, in order, from start to goal.
searchTrying possible moves in a smart order until you find a path that reaches the goal.

02 / Grids and Obstacles

Turn the world into a grid

A real room is smooth and messy. Computers like things simple. So we chop the room into a grid, like graph paper. Each square is a cell. A cell is either free (the robot can stand there) or blocked (an obstacle fills it).

From any cell, the robot can move to its neighbors: the cells touching it on the top, right, bottom, and left. We skip diagonals to keep things simple. A cell that is blocked is never a neighbor you can enter.

free: can move wall: blocked free
From the orange cell the robot can try four moves. The wall above blocks one of them.

Now the hard question is simple to say: which chain of neighbor-to-neighbor moves gets from the start to the goal? A robot could try random moves, but that wastes time. Smart search methods try moves in a careful order. We will look at two famous ones.


03 / Search in Rings

Breadth-first search: ripples in a pond

Drop a pebble in a pond. A ripple spreads out in a ring. It reaches everything 1 step away, then everything 2 steps away, then 3, and so on. It never skips ahead. Breadth-first search (BFS) works like that ripple.

  1. Put the start cell in a waiting line. This line is called the frontier: cells we have found but not looked at yet.
  2. Take the first cell from the line. It is now explored.
  3. Add its free, never-seen neighbors to the back of the line. Remember which cell each one came from.
  4. Repeat until you take the goal out of the line. Then follow the "came from" notes backward to rebuild the path.

Because the line is first-in, first-out, the nearest cells always get checked first. So the first time BFS reaches the goal, it has found a path with the fewest steps. That is a great promise. The price is that BFS looks in every direction, even away from the goal. In a big room that is a lot of wasted looking.

Where we are going: in the Maze lab below, the glowing amber cells are the frontier and the blue-green cells are the explored ones. You will see BFS grow as a ring.


04 / A* and the Hint

A*: search with a hint

Imagine finding a friend in a big building. You know she is somewhere to the east. Would you check every room to the west first? No! You use a hint about where she probably is. A* (say "A-star") gives the robot a hint too.

The hint is called a heuristic, which just means a quick smart guess. Here it is the number of blocks left to the goal if there were no walls, like counting city blocks. For each cell, A* adds two numbers:

f = g + h

  • g is the cost of the path from the start to this cell. It is what we know.
  • h is the hint: our guess of how much is left to reach the goal.
  • f is the total guess for a route that goes through this cell. A* always checks the cell with the smallest f next.

Cells pointing toward the goal have a small h, so they get checked first. The search stretches toward the goal like a beam of light instead of a ring. If the hint hits a wall, g keeps growing and A* switches to other cells.

There is one rule. The hint may never guess more than the real distance. Counting blocks never does, because a real path can only be as long or longer than the no-walls count. Follow that rule and A* still finds the best path. It just looks at far fewer cells.

g (cost so far)The total price of the trip from the start to this cell.
h (the hint)Blocks left to the goal, ignoring walls. A guess, never too big.
f (total guess)g plus h. The cell with the lowest f goes next.

05 / Mud and Pavement

Not every step costs the same

"Shortest" is not always "best." A robot rolling on smooth pavement moves fast and saves battery. A robot in mud moves slowly and uses much more power. We write this as a cost, a number for how hard each step is. In this lesson, a pavement step costs 1 and a mud step costs 5.

The total path cost is the sum of all step costs. Now a short path can be worse than a long one:

RouteStepsPath cost
Straight through the mud1212 + 4 × 4 extra = 28
Around the mud, on pavement2424

Plain BFS only counts steps, so it takes the 12-step road through the mud. To handle costs, we change the waiting line. Instead of "first in, first out," we take the cell with the lowest total cost so far (g). This idea is called Dijkstra's algorithm (say "DYKE-struh"). It is A* with no hint. A* adds the hint on top and gets the same best answer, usually faster. The lab has all three so you can compare.


06 / Hands-On Lab

Maze lab: watch the search think

Paint walls and mud. Move the start and goal. Pick a search method and step through it one cell at a time, or press Play. The real algorithm is running in your browser.

LAB 1

Paint a maze and compare searches

Tap or drag on the grid to paint with the selected tool. With a keyboard, focus the grid, use the arrow keys to move the blue cursor, and press Space or Enter to paint.

  • Frontier (waiting)
  • Explored (early to late)
  • Final path
Maps:
Search method

Run

5
Cells explored
0
In frontier
0
Path steps
-
Path cost
-

Scoreboard

MethodExploredStepsCost

Press Step to run one move of the search, or Finish to see the whole answer.

Try this: load "Mud river" and run BFS, then A*. BFS charges straight through the mud because it only counts steps. A* walks around. Next load "Open field" and compare the number of cells explored. The hint saves A* a lot of work.


07 / Be the Robot

Now you plan the route

Computers check thousands of cells in a blink. Can you beat that with your eyes? Drive the robot from S to G. Each pavement step costs 1 and each mud step costs 5. Walls stop you, and each bump gets counted. When you reach the goal, we show the best possible route in blue so you can compare.

LAB 2

Drive the robot, then compare with the best path

Click a square next to the robot, press the arrow keys while the grid is focused, or use the buttons.

  • Robot
  • Your trail
  • Best path (shown at the end)

Controls

Your steps
0
Your cost
0
Bumps
0
Best cost
?

Move the robot to the goal marked G.

Tip: turn on the hint. The numbers show the A* guess for each cell. Following the smaller numbers is exactly what A* does, but A* also adds up the real cost so far.


08 / When Things Move

Replanning: the world will not hold still

A plan is only as good as the map it was made from. People walk by. Boxes fall. A door closes. A robot that follows its first plan no matter what will crash or get stuck.

So robots re-plan. As the robot moves, its sensors keep checking the next square. If the square is now blocked, the robot updates its map and runs the search again from where it stands now. It does not go back to the start. Real robots do this many times each second.

LAB 3

Drop a box and watch the robot re-plan

Press Go. The robot follows its plan (blue dots). Press "Drop a box ahead" to put a crate on the route, or tap any free square to place or remove a crate yourself. The robot only notices a crate when it is the very next square.

  • Robot
  • Current plan
  • Where it has been
  • Crate
  • Shelf

Run

Steps taken
0
Re-plans
0
Steps to go
0
Crates
0

Robot log

    Fancier planners such as D* Lite reuse work from the old plan, so they re-plan faster. The idea is the same: sense, update the map, plan again.


    09 / Quick Answers

    Quick answers

    What is robot path planning?

    Path planning is how a robot picks a route from where it is to where it wants to be without hitting anything. The robot needs a map, a start, and a goal. A planning algorithm then searches the map for a safe route, often the shortest or cheapest one.

    What is the difference between breadth-first search and A*?

    Breadth-first search explores in rings, like ripples in a pond, checking every square at distance 1, then distance 2, and so on. A* adds a hint, an estimate of how far each square is from the goal, so it explores squares that look closer to the goal first. A* usually checks far fewer squares.

    Does A* always find the shortest path?

    Yes, as long as its hint never guesses a distance that is bigger than the real one. Straight-line or block-counting distance never overestimates on a grid, so A* with that hint finds a path with the lowest total cost.

    What is a heuristic?

    A heuristic is a quick, smart guess. In path planning it is an estimate of the distance left to the goal. It is cheap to compute and it helps the search decide which square to try next.

    How does a robot know what the map looks like?

    Some robots are given a map. Others build one with sensors such as cameras, lidar (laser distance sensors), or bump sensors. They mark each square as free or blocked, and many robots keep updating the map as they move.

    What happens when something blocks the robot's planned path?

    The robot re-plans. It updates the map with the new obstacle, then runs the search again from its current position to the same goal. Real robots do this many times per second.


    10 / Check Yourself

    Quick quiz

    1. How does breadth-first search explore a grid?

    2. What does A* add that plain BFS does not have?

    3. A box lands on the next square of the robot's planned route. What should it do?

    Keep learning about robots and search

    You just ran real search algorithms. Explore what makes a machine a robot, other ways computers search for good answers, and how a different kind of algorithm picks what you see next.

    The warehouse and its robot are made-up examples created for this tutorial.