Skip to content
Jossue Sarango
All projects

Autonomous Robot Simulator

I wrote a 2D robot simulator from scratch in Python where a two-wheeled robot can be driven by hand or steer itself to a waypoint.

Differential-Drive Robot · Waypoint navigation · Python + Pygame

  • Python
  • Pygame
  • NumPy
  • Matplotlib

The Challenge

Differential-drive kinematics and closed-loop control are hard to feel from equations alone. I wanted a sandbox where both the motion model and the control law were visible and tunable, so that I could watch a change to a gain turn into a change in behavior. The project was mine alone, built in Python with Pygame, with NumPy and Matplotlib alongside.

The robot completing a loop in the simulator

The Motion Model

First I had to model the robot. A differential-drive robot has two independently driven wheels, and its pose follows from the two wheel velocities. I integrate the position from the average wheel speed along the current heading, and the heading, theta, from the difference between the wheel speeds divided by the wheelbase. It is only a few lines of integration, but watching the pose update live is what finally made the kinematics click for me.

The Controller

For autonomy I used a proportional controller. Click a waypoint, and the controller reads two errors: the Euclidean distance to the point and the heading error. The target angle comes from atan2, and I normalize the error to the range -pi to pi so the robot always turns the short way. The two errors set the wheel speeds through separate gains, 0.5 for angular and 0.3 for linear, and the robot counts as arrived within 5 pixels.

I picked a proportional controller because it is the smallest thing that already looks autonomous. Tuning the two gains taught me the tradeoff directly: with too much linear gain the robot overshoots the turn, and with too little it crawls.

The Build

I shipped three modes over one robot model. In manual mode, W and S and Q and A drive the wheels individually. In autonomous mode the robot goes to clicked waypoints. In the combined mode, a click hands control to the autopilot and it returns to you on arrival. Keeping them separate made it easy to compare driving the robot myself against watching it drive itself. The screen shows a live velocity and heading readout, a body-frame axis overlay, and a trail that draws the path the robot has taken.

The Results

The result is a working navigation sandbox: click anywhere and the robot turns and drives to the point, tracing its path, or take the wheels yourself. The code is on GitHub.

What I Learned

  • Differential-drive motion is only a few lines of integration — but watching the pose update live is what finally made the kinematics click.
  • A proportional controller is the smallest thing that already "looks autonomous"; tuning its two gains taught me more about control than any amount of reading about PID.
  • Keeping manual, autonomous, and hand-off as separate modes over one robot model made it easy to compare driving it against watching it drive itself.

Let's make something amazing.