Design and Implementation of a Two-Wheeled Self-Balancing Vehicle Based on Cascade PID Control and Complementary Filtering
Abstract
This paper presents the modeling, embedded implementation, and functional validation of a two-wheeled selfbalancing vehicle. The platform is organized as an invertedpendulum system driven by two independently controlled dc geared motors. An STM32F103C8T6 microcontroller executes the real-time control loop, while an MPU6050 inertial measurement unit and two wheel encoders provide attitude and speed feedback. A physically consistent nonlinear model is established from the translational and rotational energies of the wheel-body system and is linearized about the upright equilibrium to obtain an explicit state-space representation. To obtain a stable attitude estimate with low computational cost, accelerometer and gyroscope measurements are fused by a discrete complementary filter. Motion control is implemented using a cascade structure: an outer speed loop generates the desired pitch angle, an inner attitude PID loop produces the common motor command, and a differential wheel-speed loop generates the steering command. PWM saturation, integral antiwindup, abnormal-angle shutdown, and command validation are included in the embedded software. Prototype tests on a flat indoor surface demonstrate upright balancing, recovery from small forward and backward disturbances, forward and reverse motion, and differential steering. Simulation further shows a damped pitch response and recovery after a representative disturbance. The results verify the feasibility of the proposed lowcost mechatronic architecture and provide a reproducible basis for subsequent quantitative control optimization.