You are currently viewing Inverted Pendulum MPC Controller

Inverted Pendulum MPC Controller

Designed and implemented Model Predictive Control (MPC) and PID controllers for balancing a rotary inverted pendulum system, achieving stable equilibrium control within ±0.6° through optimization-based control strategies and comprehensive experimental validation.

code: https://github.com/Aaaartemis/InvertedPendulumController

Project Overview:

Developed a complete control solution for the Quanser rotary inverted pendulum, progressing from classical PID control to advanced MPC using constrained optimization. The system required precise voltage control (±10V limits) to maintain the pendulum upright while respecting physical constraints on base rotation, angular velocities, and motor torque limits.

Mathematical Modeling:

  • State-Space Derivation: Derived continuous-time state-space representation from coupled nonlinear ODEs governing motor dynamics and pendulum motion, incorporating motor back-EMF, friction, and gravitational effects
  • Signal Flow Analysis: Constructed phase-variable canonical form diagrams to resolve algebraic loops in the coupled differential equations, enabling systematic separation of second-order terms
  • Discretization: Applied Euler discretization (2ms timestep) to convert continuous dynamics into discrete-time state-space matrices suitable for real-time implementation

Control Architecture:

PID Controller:

  • Iteratively tuned proportional (Kp=40), integral (Ki=0.00001), and derivative (Kd=2) gains through trial-and-error optimization
  • Achieved 0.12s rise time and 0.16s settling time with 3.45% overshoot
  • Maintained pendulum angle within ±0.6° for 20+ seconds during simulation trials

MPC Controller – Augmented State-Space Approach:

  • Implemented augmented state formulation with 5th state tracking previous control input (u_{k-1}), enabling optimization over incremental inputs (Δu) for smoother control action
  • Configured OSQP (quadratic programming) solver within YALMIP framework for real-time optimization
  • Designed cost function with Q matrix heavily penalizing pendulum angle deviation (Q₂₂=1e5) and previous input magnitude (Q₅₅=1e3), while allowing base angle freedom (Q₁₁=1)
  • Incorporated DARE-based terminal cost (infinite-horizon LQR solution) to guarantee closed-loop stability
  • Enforced hard constraints: voltage limits (±10V), state limits (base angle ±2 rad, velocities), and input-rate smoothing

Experimental Validation:

Conducted systematic experimental analysis in QLabs simulation environment across five investigation categories:

  1. PID vs MPC Comparison: MPC achieved 67% lower RMS input voltage (0.34V vs 1.10V) with superior constraint handling and guaranteed stability, though with 11x longer settling time (1.82s vs 0.16s)
  2. Cost Matrix Tuning (Q): Validated that penalizing pendulum angle (1e5) and previous input (1e3) most heavily produced optimal balance between performance and energy efficiency; mistuned values caused non-settling behavior or excessive overshoot (>75%)
  3. Prediction Horizon (N): Demonstrated computational time scaling from 1.5ms (N=2) to 991ms (N=1000) with marginal performance improvements beyond N=50, establishing N=50 as optimal trade-off; identified numerical instability threshold at N≥300
  4. Terminal Cost Impact: Proved terminal cost essential for feasibility—all configurations with N<50 without terminal cost failed completely; with terminal cost, achieved stable control even at N=2
  5. Constraint Analysis: Mapped constraint boundaries for feasibility (e.g., base velocity limits [-3.5, 3.5] rad/s critical threshold); demonstrated MPC’s ability to satisfy multiple simultaneous state and input constraints

Key Results:

  • MPC controller successfully stabilized pendulum with 0.002° steady-state error and 12.5% overshoot
  • Achieved 70% reduction in control effort compared to PID while maintaining comparable rise times (0.12s)
  • Demonstrated robust constraint satisfaction across voltage, position, and velocity limits simultaneously
  • Validated terminal cost necessity for ensuring optimization feasibility and closed-loop stability

Technical Implementation:

Developed complete Simulink models integrating MATLAB Function blocks for controller logic, with persistent variable optimization to cache solver configurations. Implemented comprehensive data logging for θ (base angle), α (pendulum angle), angular velocities, control inputs, and computation times across all experimental conditions.

Technologies: MATLAB/Simulink, YALMIP, OSQP solver, MPC, PID control, state-space modeling, constrained optimization, DARE equations, LQR, real-time control systems