Martı Tag: Maze Solver Pi Car

Intelligent mechatronic systems course · team project
Raspberry Pi · OpenCV · BFS · Machine learning

Martı Tag: Maze Solver Pi Car

In the Intelligent Mechatronic Systems lecture, we were required to develop an intelligent project with a physical demonstration. To meet this requirement, my teammate and I developed a Raspberry Pi–based 4WD car.

The idea for the project came from me. I wanted to create a system to control the car’s direction with minimal cost. We couldn’t afford a LiDAR sensor, and I didn’t want to use four ultrasonic sensors. Additionally, we had only one camera, which made depth estimation or environmental mapping difficult.

As a child, I enjoyed solving mazes in kids’ magazines. I would always start from the end and find the beginning—because while there may be multiple paths from the start, there’s usually only one path leading directly to the end.

This inspired us to combine the concept of a maze with our robot and a single top-view webcam.

Maze Extraction and Environment Setup

We used the webcam to provide a top-down view and attempted to extract the maze from black lines drawn on a plastic rectangular plate. However, using basic OpenCV operations for direct extraction posed challenges:

  • If the distance between the camera and the maze increased, the image quality decreased.
  • Variations in lighting caused some lines to become undetectable.

To solve this, we trained a machine learning model using a Kaggle maze dataset to detect the maze structure more reliably. We also created a controlled environment to conduct our experiments.

Maze Solving Algorithm – BFS

There are various algorithms for maze solving, such as DFS, BFS, and A*.

We chose Breadth-First Search (BFS).

What is BFS?

BFS is a graph traversal algorithm that explores nodes level by level and always finds the shortest path in terms of the number of steps.

3029282716152122232613142021242512151914131011161815129817171611671810111054199129632878321765410

A small example maze, numbered from the end. The bold numbers are the way back.

How BFS Works in Our Application

In our MazeSolverApp, the find_path_bfs() function implements this logic:

  • Grid Representation:

The maze is represented as a 2D NumPy array.

  • 1 indicates a traversable path.
  • 0 (or other values) indicate walls or obstacles.
  • Start and End Nodes:

The user selects these points by clicking on the image. They are represented as (row, column) coordinates.

  • Queue and Visited Set:
  • A deque stores the current position and the path leading to it.
  • A set keeps track of visited coordinates to avoid repetition.
  • Possible Moves:

Eight directions are allowed: up, down, left, right, and the four diagonals.

  • Traversal Logic:

The algorithm explores each node’s neighbors, adds valid paths to the queue, and stops when the end point is found.

  • Path Reconstruction:

Since the full path is stored with each queue entry, once the target node is reached, the path is immediately returned.

Thus, BFS guarantees the shortest path between the start and end points based on pixel steps.

Vehicle Navigation Challenges

Another critical part of the project was translating the path into actual movement—such as turning 90 degrees left or right. Although this sounds simple, it was challenging in practice.

  • Initial Platform – Differential Drive:

We started with a 2-wheel differential drive setup, but the caster wheel caused the car to skew when moving in a straight line.

  • We tried balancing motor speeds with PWM,
  • Used encoders and a logic level shifter,
  • But couldn’t fully resolve the issue.
  • Switch to 4WD Platform:

We moved to a 4-wheel drive system but encountered new problems:

  • The motors were underpowered.
  • The tire material created too much friction with the plastic plate.
  • To fix this, we removed the rubber tires and added a LiPo battery to improve motor performance.
  • Precise Turning with MPU6050:

We used an MPU6050 sensor to improve turning accuracy.

For example, if we turned 86° instead of 90°, it would affect subsequent movements. The MPU6050 helped us make precise 90° turns.

Why Not Use Feedback for Orientation?

You may ask why we didn’t use feedback from the car’s position or orientation. Actually, we tried using ArUco markers for this.

However:

  • The camera sometimes failed to recognize the marker while the car was moving fast.
  • The camera’s height from the maze also reduced marker detection accuracy.

We used a Raspberry Pi connected to the car platform, and a webcam connected to a laptop. The communication between them was handled using Python’s socket library. The laptop is responsible for graphical processing (like image analysis and path planning), while the Raspberry Pi handles motor control and driving.

You can see the Fritzing diagram for the Raspberry Pi and other component connections.

Additionally, we used a power bank to supply power to the Raspberry Pi 4B.

Martı Tag: Maze Solver Pi Car

The next image shows the diagram of our project.

Martı Tag: Maze Solver Pi Car

We also have an image of our user interface.

How to Use
How to Use

First, start a hotspot on your computer and run the computerside.py script. This script starts a server and waits for the Raspberry Pi to connect.

At the same time, the webcam should be connected to the computer.

Next, run the raspberrypi.py script on the Raspberry Pi. This script acts as a client and connects to the server on the computer.

You have two options:

  • Choose a maze image from the gallery and run the car as a simulation or in hardware-in-the-loop mode.
  • Use the camera directly.

If you choose the camera, it will open and capture the current view. The user then selects two points, which represent the start and end positions. The algorithm finds all possible paths between these points.

After that, the system finds the optimal path and sends driving commands to the Raspberry Pi. Place the car manually at the start point, and when you press the “Drive Car” button, the car begins calibration and drives according to the extracted commands from the maze.

The intelligent aspect of this robot is its ability to extract solutions for different mazes and drive the car accordingly.

You can access the source files on GitHub, and I have also included a demonstration video showing the working progress.

github.com/muhammeddincmdx/maze_solver_pi_car_project

Intelligent Mechatronics Systems Project

Photos