pboon09
Institute of Field Robotics (FIBO), KMUTT

Hybrid Energy-LQR Control of a Rotary Inverted Pendulum

November 2025 – December 2025

Overview

A rotary inverted pendulum that swings itself up from hanging down and then balances upright, on a custom embedded board. It is a classic control benchmark because it is nonlinear, underactuated, and unstable. One motor drives a free-swinging link, so lifting and balancing need two different controllers and a safe hand-over between them.

Linear control cannot handle the large swing-up motion, so an energy-based controller first raises the pendulum's energy until an LQR (linear quadratic regulator) can hold it upright.

Swing-up and LQR balance on the real hardware

Hardware

MicrocontrollerSTM32 NUCLEO-G474RE, or the STM32G474RET6 on the surface-mount board
ActuatorBrushed DC motor with a Cytron MD20A driver
SensorsTwo AMT103-V encoders, one on the arm and one on the pendulum
InterfacesLED matrix, LCD, SD card, HC-05 Bluetooth, emergency switch
DimensionsBase \(220 \times 300\) mm, arm 133 mm, pendulum 135 mm
BoardsThrough-hole extension board (fabricated and running) and a surface-mount single board with USB-C

System

Control Rate1 kHz Timer Interrupt
State Estimation31-tap FIR filter with a 15 Hz cutoff on the velocities, and a Kalman filter on the motor
Motor LimitLQR output limited to 10 V, supply up to 24 V
ROS Linkmicro-ROS over UART at 115200 baud, about 55 to 57 Hz
SoftwareFreeRTOS on STM32 HAL, micro-ROS, ROS 2, RViz2, MATLAB

Process

  1. Derive the model

    Modified DH parameters, a \(6 \times 2\) Jacobian and Lagrangian dynamics are derived in MATLAB, with mass properties from the CAD design.

  2. Kick and swing up with energy

    The arm swings the pendulum up in two stages.

    • A short kick of half a second starts the motion.
    • An energy law, \(U = k_E\,\Delta E\,\dot\alpha\cos\alpha\), then pumps energy into the pendulum until it nears the top.
  3. Balance with LQR

    The LQR gain comes from the discrete-time Riccati equation, with the weights chosen by Bryson's rule.

    • When \(|\alpha-\pi|\lt 0.25\) rad, control switches from swing-up to LQR.
    • If \(|\alpha-\pi|\gt 0.5\) rad, control returns to swing-up.
  4. Estimate the state

    A FIR filter and a Kalman filter estimate joint velocities from the two encoders.

  5. Stream and compare

    micro-ROS streams the joint states to RViz, where measured motion is compared with the motion computed from the model and the Jacobian.

Results

  • The pendulum swings up and balances on the real hardware.
  • Measured and theoretical joint positions agree.
  • The velocity error between the model and the hardware stays below 0.01 rad/s.
  • The stair steps in the theory curves come from the 55 to 57 Hz micro-ROS link.

Table 1. Controller settings on the hardware

Swing-Up Energy Gain \(k_E\)1.5
Switch to LQR\(|\alpha-\pi|\lt 0.25\) rad
Fall Back to Swing-Up\(|\alpha-\pi|\gt 0.5\) rad
LQR Gain \(K\), for \(\theta,\ \alpha,\ \dot\theta,\ \dot\alpha\)\(-0.8197,\ 11.3205,\ -0.6469,\ 1.1683\)
LQR Voltage Limit10 V
Fig. 1. Measured and theoretical position and velocity, arm on top and pendulum below
Fig. 1. Measured and theoretical position and velocity, arm on top and pendulum below
Fig. 2. Measured and theoretical joint states in RViz

Figures and Video

The pendulum and its electronics
The pendulum and its electronics
Through-hole board, 3D view
Through-hole board, 3D view
Through-hole board schematic
Through-hole board schematic
Surface-mount board, 3D view
Surface-mount board, 3D view
Surface-mount schematic, top sheet
Surface-mount schematic, top sheet
Surface-mount schematic, MCU sheet
Surface-mount schematic, MCU sheet