Line Follower Simulation

A 2D physics simulaton of a robot to test line following algorithms. Personal Project

Project Overview

This project is a 2D physics-based simulation of a differential-drive line-following robot. The goal was to validate embedded line-following firmware without building physical hardware, effectively acting as a low-cost Hardware-in-the-Loop (HIL) test environment. The simulation communicates with a real microcontroller over USB serial, allowing the exact same firmware used on hardware to run unmodified against simulated sensors and actuators.

System Architecture

  1. Physics & Kinematics Simulation:
    A custom robot model simulates linear and angular motion using force-based dynamics, including motor back-EMF behavior, friction, mass, and rotational inertia. Wheel forces are derived from PWM commands, allowing realistic acceleration, turning behavior, and instability if the controller is poorly tuned.
  2. Sensor Simulation:
    Three forward-facing reflective sensors are simulated by sampling pixel brightness from a map image. Sensor values are computed by averaging grayscale values within a radius and inverted so dark lines register as high intensity, closely matching real IR line sensors.
  3. Serial HIL Interface:
    Sensor data is streamed to the microcontroller as formatted serial packets, while motor PWM commands are read back asynchronously. This allows the embedded controller to behave as if it were connected to real sensors and motors.
  4. Real-Time Visualization:
    Pygame is used to render the robot, wheel orientation, heading, sensor positions, and track layout. Multiple maps can be cycled dynamically to test controller robustness across different path geometries.
  5. Live Telemetry Plotting:
    A parallel Matplotlib thread plots sensor values and motor PWM outputs in real time, enabling rapid debugging and controller tuning without relying on firmware-level logging.

Key Technical Challenges

  1. Real-Time Synchronization:
    The simulation, serial communication, rendering loop, and plotting all run concurrently. Thread-safe buffering and non-blocking serial reads were required to prevent latency and dropped commands.
  2. Stable Physics Modeling:
    Naive kinematic models were insufficient. A force-based approach with friction and torque limits was required to prevent unrealistic behavior and expose controller weaknesses that would appear on real hardware.
  3. Sensor Noise & Realism:
    Averaging pixel regions instead of single-point sampling reduced aliasing and produced smoother, more realistic sensor readings, closer to physical reflectance sensors.

Results

The simulation successfully runs embedded line-following firmware in real time without modification. Controllers can be tuned, stress-tested, and debugged before deployment to physical hardware, significantly reducing development time and risk. Poorly tuned control loops visibly destabilize the robot, while robust algorithms generalize across multiple track layouts.

Conclusion

This project provided a practical HIL testing environment using only Python and consumer hardware. It bridges the gap between purely theoretical control design and real-world deployment, and mirrors the workflow used in professional robotics and automotive validation environments.