Simulation
I created a barebones URDF that resembled the self-balancing robot I was going to build and dropped it into PyBullet. A simulation script let me send control signals to each of the robot's wheels, with limits on how fast the wheels could turn.
For each controller I ran 3 trials and counted a trial as a success if the robot balanced for at least 5 seconds, one of the success criteria in my project proposal. Every simulation used the same random seed. To mimic sensor noise, I added uniform noise (+/- 0.5) to the pitch reading before it reached the controller. To mimic inconsistent motor output, I also added uniform noise to each control signal: +/- 2 on the left wheel and +/- 1 on the right.
Each recorded demo below is trial 1. The time after each controller is the mean time to compute one control signal.
PID
P = 10 · I = 100 · D = 0 · 2.012 µs / step
Tuning the PID controller was fairly intuitive once I realized the robot needed to be aggressive in its corrections (high I gain). Through trial and error I learned that adding any damping (more D gain) made the robot eventually fall, because it slowed down each corrective step.

LQR
Linear Quadratic Regulator · 7.450 µs / step
LQR was trickier than PID because it needs a mathematical model of the system. When I wrote the URDF I wasn't thinking about this. In fact, I used PyBullet + URDF to avoid modelling the dynamics. After trying random A, B, Q and R matrices to no avail, I turned to black-box system identification: set the robot at different pitches, apply random control signals, watch how it responds, and fit a linear model with least-squares regression. A basic grid search then found Q and R matrices that gave the results below.

MPC
Model Predictive Control · 677.376 µs / step
MPC shares LQR's A, B, Q and R matrices but adds one parameter: the horizon N it optimizes over. A small script tried every N and sorted them by pitch RMSE across the whole run. N = 3 was enough to balance for 10 seconds.

Hardware Implementation
Hardware Used
- MCU: ESP32 w/ FreeRTOS
- Sensors: MPU6050, Quadrature Encoders (built into motors)
- Actuators: JGA25-371 500RPM (w/ L298N to drive them)
- Power: 3S1P 18650, LM2596S
Initial Challenges
I suspected the robot was oscillating a lot because of poor PID gains. There was also some steady-state error, which eventually tipped the robot over once it passed a certain angle.
Improvement
Compensate for weak motors by increasing the wheel diameter from 8.8 cm to 13 cm.

Autonomous Demo
With the bigger wheels and retuned gains, the robot balanced for well over 5 seconds and kept balancing when pushed. Final gains: P = 10, I = 12, D = 0.4.