Back to Systems System 05

Self-Balancing
Robot

PID Control Sensor Fusion IMU Complementary Filter STM32 Motor Encoders Robofest 4.0
Self-Balancing Robot Demo
assets/videos/robofest_demo.mp4
Problem

A two-wheeled robot is inherently unstable โ€” it is a classic inverted pendulum that will fall within milliseconds without active control. The system must sense its tilt angle with high precision, reject gravitational pull and external disturbances in real time, and do so at control loop frequencies fast enough to prevent any perceptible wobble or fall.

Approach

Designed a cascaded PID control architecture operating on three nested loops โ€” angle stabilisation (innermost), angular velocity damping, and translational position hold (outermost). Sensor fusion combines IMU accelerometer and gyroscope readings through a complementary filter to produce a drift-free tilt estimate at high frequency. Wheel encoder feedback closes the position loop, preventing the robot from drifting while maintaining balance.

System Architecture
Sensors โ”‚ โ”œโ”€โ”€ IMU (MPU6050) โ”‚ โ”œโ”€โ”€ Accelerometer โ†’ Tilt angle (noisy, no drift) โ”‚ โ””โ”€โ”€ Gyroscope โ†’ Angular rate (clean, drifts) โ”‚ โ””โ”€โ”€ Complementary Filter โ†’ Fused tilt ฮธ โ”‚ โ”œโ”€โ”€ Wheel Encoders ร— 2 โ”‚ โ””โ”€โ”€ Position & velocity feedback โ”‚ โ””โ”€โ”€ Control Loop (STM32 @ 1kHz) โ”‚ โ”œโ”€โ”€ PID 1: Angle Loop โ”‚ โ””โ”€โ”€ Error = ฮธ_target โˆ’ ฮธ_measured โ”‚ โ”œโ”€โ”€ PID 2: Angular Velocity Loop โ”‚ โ””โ”€โ”€ Damping term for oscillation rejection โ”‚ โ”œโ”€โ”€ PID 3: Position Loop โ”‚ โ””โ”€โ”€ Prevents translational drift โ”‚ โ””โ”€โ”€ Output โ†’ PWM โ†’ Motor Drivers โ†’ DC Motors ร— 2
Implementation Details

The control firmware runs on an STM32 microcontroller at a 1 kHz loop rate, ensuring sub-millisecond response to disturbances. The complementary filter blends accelerometer tilt (accurate over long periods but noisy) with integrated gyroscope rate (smooth but subject to drift) using a tuned time constant. The cascaded PID structure allows each loop to be tuned independently โ€” the inner angle loop runs fastest for stability, while the outer position loop operates at a slower rate to prevent translational drift without interfering with the balance dynamics. Motor drivers receive PWM commands directly from the controller with minimal latency.

Competition Milestones
Design Report Stage

Thoroughly documented parametric CAD modelling and hardware selection. Passed with high marks for practical structural stability and feasibility.

Proof of Concept Stage

Successfully demonstrated the integrated sensor fusion and control system on breadboard prototypes and a scaled-down chassis, clearing the proof round.

Hardware & Testing
Drop photos into assets/images/hardware/ and assets/images/testing/
Result

The robot maintains continuous inverted pendulum balance, recovering from external disturbances within fractions of a second. The cascaded PID architecture with sensor fusion proved robust enough to pass both competition stages โ€” validating the control design under real-world conditions with noise, vibration, and surface irregularities.

View on GitHub โ† Mechanical Design