Skip to content

huynna12/robot-path

Repository files navigation

Autonomous Line-Following Robot (PicarX)

A Python-based autonomous robot that follows a line through turns up to 90° using three grayscale sensors. Built on the PicarX platform. Includes a project documentation website deployed on Netlify.

Project Site


Demo

Robot navigating a straight line Handling a sharp 90° turn
Robot1 Robot2

How It Works

The robot uses three grayscale sensors (left, center, right) that measure reflected light intensity:

Value > 1400  →  White (background)
Value < 1400  →  Black (line)

Each sensor reading is converted into a binary state (1 = line, 0 = background), and the combination of all three determines the robot's next action:

Sensor State [L, C, R] Detected Situation Action
[_, 1, _] Center on line Go straight
[1, 0, 0] Drifted right Steer left
[0, 0, 1] Drifted left Steer right
[0, 0, 0] Line lost Recovery maneuver

Recovery Maneuver

When all sensors read white (line lost), the robot uses last_state to remember which direction it was turning before losing the line, then steers back in that direction to re-acquire it — rather than stopping blindly.

def outHandle():
    if last_state == 'left':
        px.set_dir_servo_angle(-offset)
        px.forward(px_power)
    elif last_state == 'right':
        px.set_dir_servo_angle(offset)
        px.forward(px_power)

Project Challenges

The project was structured as a series of progressive tasks:

  1. Line-Following — Follow a line through turns up to 90°
  2. Intersection-Finding — Detect and stop at line intersections
  3. Intersection-Choosing — Exit intersections by selecting the nth spoke clockwise
  4. Intersection-Traversing — Execute a sequence of intersection exits (n1, n2, n3…)
  5. Search & Traverse — Given a graph topology and goal, generate and execute a movement sequence
  6. Localization — Determine current position/heading without prior knowledge, then navigate
  7. Exploration — Autonomously map the environment and find the most central node

Tech Stack

Layer Technology
Robot hardware PicarX (Raspberry Pi-based)
Robot software Python
Grayscale sensing px.get_grayscale_data() / px.get_line_status()
Documentation site HTML, CSS, JavaScript
Deployment Netlify

Project Structure

robot-path/
├── myLine-following.py     # Main robot control script
├── line-following/         # Additional line-following experiments
├── index.html              # Documentation site home
├── Code.html               # Code walkthrough page
├── Journal.html            # Development journal
├── Problem.html            # Task descriptions
├── Pictures.html           # Robot photos
├── Clips.html              # Demo video clips
└── style.css               # Site styling

What I Learned

  • Sensor threshold tuning — small changes to the 1400 threshold value significantly affected detection accuracy depending on floor color and lighting conditions
  • State machine design — tracking last_state was essential for smooth recovery; without it, the robot would freeze whenever it lost the line on a sharp turn
  • Control loop timingsleep(0.001) between sensor reads prevented servo jitter and gave the hardware time to respond to direction changes
  • Iterative hardware debugging — unlike software, hardware bugs required physical observation; watching the robot overshoot turns led directly to tuning the offset angle value

About

PicarX line-following robot using three grayscale sensors and threshold-based state detection. Built with Python.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors