A-Level Computer Science project, 2025–26

Atlas, a self-balancing desk robot

A two-wheeled robot that keeps itself upright on my desk. It’s built and it balances, using an LQR controller with the gains worked out in MATLAB from a model of the robot. It’s also a revision timer, counting down study sessions on its round screen. Now I’m writing up the documentation.

Controller
LQR (it started as PID)
Gains from
MATLAB
Microcontroller
ESP32-S3
Tilt sensor
MPU-6500
Atlas balancing on my desk: a black 3D-printed body on two yellow wheels, with its round screen showing a 25-minute Maths timer
Balancing on my desk, with a 25-minute Maths session ready to start.

Why LQR instead of PID

My first plan was a PID controller on the lean angle, with a complementary filter to estimate the angle. I switched Atlas to an LQR controller instead.

Atlas side on, leaning over The body leans at angle theta from upright. Gravity, mg, pulls down at the centre of mass and tips it further over. The motors push the wheels with force F, and the wheels roll a distance x from where they started, to get back underneath the body. θ upright mg centre of mass F from the motors x start
Side on. Gravity tips the body further over, so the motors have to drive the wheels back underneath it. The job is to keep θ near zero without rolling too far along x.

A PID loop on the angle only looks at one number. It can push the wheels to stop the robot falling, but it doesn't know the robot is slowly rolling off across the desk, and its three gains have to be found by trial and error.

LQR looks at all four states at once: how far the wheels have moved, how fast they're going, the lean angle, and how fast it's leaning. I don't guess the gains. I decide how much I care about each state and how much motor effort I'm willing to spend, and the maths gives me the best gains for that trade-off.

The maths is in my paper

I wrote up PID, the state-space model, the A and B matrices and the Riccati equation in Controlling an inverted pendulum. This page is about how it's used on Atlas.

How the controller is designed

  1. Measure the four states

    The MPU-6500 gives the lean angle and how fast it's changing. The wheel encoders give how far the robot has moved and how fast.

  2. Model the robot

    I modelled Atlas as a body sitting on a pair of wheels and used Newton's laws to write it in state-space form, x˙=Ax+Bu. The body's moment of inertia comes from treating it as a box, m(h2+d2)12, rather than measuring it.

  3. Choose the weights

    Q and R come from Bryson's rule, which sets each weight from the largest value I'm happy to see: 10 cm of drift, 0.5 m/s, 5° of lean, 90°/s, and 4 V on the motors.

  4. Let MATLAB solve it

    A MATLAB script solves the Riccati equation and gives the gain vector K. I wrote it without any toolboxes, so it also runs in Octave.

  5. Run it on the robot

    Every loop, the ESP32-S3 works out u=−Kx and turns it into a motor voltage. The balance code runs as its own FreeRTOS task on one core, so the screen, sound and Wi-Fi can't hold it up.

The parts

PartWhat I usedWhat it's for
MicrocontrollerESP32-S3 dev board (N16R8)Runs the balance loop and everything else
Tilt sensorMPU-6500Accelerometer and gyro for the lean angle and rate. My first IMU board was dead, so this is the replacement
Motors2 × JGA25-370 with encodersGeared wheel motors. The encoders measure how far each wheel has turned
Motor driverDRV8833Drives the two wheel motors
Distance sensorVL53L0XSees obstacles in front
Edge sensorTCRT5000Stops it driving forward off the desk
Touch2 × TTP223Buttons on the body
ScreenGC9A01, roundThe timer and menus
SoundMAX98357A amplifierChimes and spoken reminders
FirmwareC++ in the Arduino IDEFreeRTOS tasks for balance, screen and network

More than balancing

For a Computer Science project it has to do something useful too, so Atlas is also a revision timer. It times study sessions and breaks, plays a chime and a spoken reminder when a break is due, and keeps a weekly chart of how much I've done against a goal.

It also does a full spin on the spot to say hello. That's harder than it sounds, because turning needs its own controller on the wheel encoders, and that has to hold the heading while the LQR loop is busy keeping it upright.

Where it's up to

Last updated October 2026

  • Chassis and electronicsBuilt
  • LQR design in MATLABDone
  • Balancing on the real robotBalancing
  • Revision timer on the round screenWorking
  • DocumentationWriting it now
  • Talking to the light switchLater

What's next

It balances now. Getting there wasn't quick: the gearboxes on the motors have a bit of slack in them, and my first set of gains made it shake badly until I tuned it down. The main job now is the documentation for my Computer Science project, writing up how it works and how I tested it.

Longer term, I'd like it to talk to my light switch over MQTT. I'll add more photos and test results here as I write it up.

Ask me about Atlas