Embedded Control · from scratch

Self-Balancing Robot

A two-wheeled self-balancing robot built end-to-end — then three ways to keep it upright: cascaded PID, imitation learning, and RL.

▶It Actually Balances

Live demo — the robot recovering and holding balance in real time.

▶Three Ways to Stay Upright

Same simulated plant, four terrains, three controllers — orbit camera and a live metrics picture-in-picture. Traditional PID, then an imitation of that expert, then an RL policy held fixed while the ground changes (flat → grade → rough → shove).

Cascaded PIDFirmware architecture
ImitationBehavior cloning
RLLearns the lean
RL terrain tourSame weights, four grounds
Flat · SlopeRough · Shove
Orbit cameraCinematic path
画中画Live pitch / ω / u

Same plant, same four terrains. Imitation matches PID. RL cuts lean error on the grade and tracks rough ground — a strategy nobody typed in.

Bar charts comparing PID, imitation learning, and RL on fall rate, pitch, lean error, and effort across flat, slope, rough, and disturbance terrains.

iOverview

This project implements a two-wheeled self-balancing robot using an advanced (cascaded) PID control policy. The robot estimates its body angle and continuously adjusts motor output to maintain balance, forming a closed-loop control system similar to the basic principle behind Segway-like robots.

It combines hardware integration, sensor feedback, real-time control, and parameter tuning — demonstrating how classical control methods can stabilize an inherently unstable physical system, and laying a foundation for more advanced topics such as state feedback, trajectory tracking, and model-based control.

1Hardware Connection

Power regulation, sensors, controller, motor driver, and encoder-equipped motors.

Hardware wiring: 3S LiPo battery, buck converter, MPU6050 IMU, Arduino Nano, HC08 motor driver, and encoder motors.

2Control Architecture

Cascaded loops: an outer angle (pitch) loop feeding an inner velocity loop.

Cascaded PID block diagram: outer angle loop and inner velocity loop running on the Arduino Nano.

3PID Control Strategy

Angle loop stabilizes the robot upright; velocity loop tracks the target wheel speed.

PID control strategy with equations for the outer angle loop and inner velocity loop, and tuning tips.

4Encoder Speed Estimation

Two interrupt-driven methods — fixed time window vs. fixed pulse count — with a trade-off comparison.

Encoder speed estimation: Method A fixed time window and Method B fixed pulse count, with pros, cons and comparison.

5Control Performance

Single angle loop vs. cascaded control — angle, wheel speed, and control effort.

Performance comparison of single angle loop vs cascaded control across pitch angle, wheel speed and PWM effort.

+Highlights

Arduino NanoController
MPU60506-axis IMU
HC08Motor driver
DC + EncodersMotors
Cascaded PIDAngle + velocity
Real-timeClosed-loop