Skip to content

Latest commit

 

History

1 Commit

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Nonlinear Control of a Two-Wheeled Inverted Pendulum (Segway)

Modeling, nonlinear controller design, Lyapunov stability analysis, and MATLAB simulation for a two-wheeled inverted pendulum (TWIP / Segway) — an underactuated, inherently unstable nonlinear system. Two controllers, Sliding Mode Control (SMC) and Feedback Linearization (FL), are derived, proven stable, and compared on settling time, overshoot, and disturbance rejection.

Control Method Analysis Tool

MEEN 655 — Design of Nonlinear Control Systems · Texas A&M University

TWIP schematic


🎯 Problem

A Segway is a wheeled inverted pendulum (WIP): a body balanced on a wheeled base that must actively stabilize its upright tilt. It is:

  • Nonlinear — tilt/translation coupling, gravity, trigonometric terms;
  • Underactuated — fewer independent actuators than degrees of freedom;
  • Open-loop unstable — the upright equilibrium ($\theta = 0$) diverges under the slightest perturbation.


Open-loop response — with no control, tilt and position blow up.

🧮 Modeling

Using a Lagrangian formulation with state $\mathbf{x} = [x,\ \dot{x},\ \theta,\ \dot{\theta}]^\top$ (cart position, velocity, tilt, tilt rate), the equations of motion are:

$$\left(2m + M + \tfrac{2J_w}{R^2}\right)\ddot{x} + Ml\ddot{\theta}\cos\theta - Ml\dot{\theta}^2\sin\theta = \tfrac{\tau_L+\tau_R}{R}$$

$$Ml\ddot{x}\cos\theta + (J_b + Ml^2)\ddot{\theta} - Ml\dot{x}\dot{\theta}\sin\theta + Mgl\sin\theta = 0$$

Jacobian linearization reveals two equilibria: the upright position ($\theta=0$) is an unstable saddle, while the hanging position ($\theta=\pi$) is stable — confirming that active control is required to balance.

🛠️ Controllers

1 · Sliding Mode Control (SMC)

Sliding surface and reaching law:

$$s = \dot{\theta} + \lambda\theta, \qquad \dot{s} = -\eta,\mathrm{sign}(s),\quad \eta>0$$

$$\tau = \frac{-f(x) - \lambda\dot{\theta} - \eta,\mathrm{sat}(s/\phi)}{g(x)}$$

A saturation function with boundary layer $\phi$ replaces $\mathrm{sign}(s)$ to suppress chattering.

2 · Feedback Linearization (FL)

Output $y=\theta$ has relative degree 2; since the system is 4th order with full relative degree, it is input-state linearizable. With virtual control $v = -k_1\theta - k_2\dot{\theta}$:

$$u = \frac{1}{k(x)}\big[-k_1\theta - k_2\dot{\theta} - h(x)\big] ;\Rightarrow; \ddot{\theta} = v$$

📐 Lyapunov Stability

Controller Lyapunov function Derivative Result
SMC $V = \tfrac{1}{2}s^2$ $\dot{V} = s\dot{s} = -\eta\lvert s\rvert \le 0$ Asymptotically stable; $s\to 0 \Rightarrow \theta\to 0$
FL $V = \tfrac{1}{2}\theta^2 + \tfrac{1}{2}\dot{\theta}^2$ $\dot{V} = \dot{\theta}\big((1-k_2)\theta - k_1\dot{\theta}\big)$ Stable for $k_2>1,\ k_1>0$

📊 Results

SMC — tilt driven to upright

Feedback Linearization — smooth convergence

SMC rejects a 20 N·m disturbance spike at t = 3 s

SMC vs FL — head-to-head

Controller comparison

Feature Sliding Mode Control Feedback Linearization
Settling time Often faster Slightly longer
Overshoot Lower (sliding behavior) Slightly higher
Disturbance rejection Very strong (inherent robustness) Weaker

Conclusion: both stabilize the upright equilibrium, but SMC outperforms Feedback Linearization, primarily due to its inherent robustness to disturbances. Note the underactuation: tilt is stabilized while the wheel position $x$ drifts (see 06-position-drift-underactuation.png).

💻 Code

All simulations are pure MATLAB (ode45), no toolboxes required.

File Purpose Generates
TWIP_OpenLoop.m Uncontrolled dynamics (motivation) Open-loop instability
segway_smc_main.m + segway_dynamics.m Sliding Mode Controller w/ boundary-layer saturation SMC stabilization
TWIP_FL.m Feedback Linearization controller FL stabilization
ProjectCompare.m SMC vs FL + disturbance-rejection test Comparison plots

Run it

cd matlab
TWIP_OpenLoop      % see why control is needed
segway_smc_main    % sliding mode stabilization
TWIP_FL            % feedback linearization
ProjectCompare     % compare both + disturbance rejection

📄 Report

The full IEEE-format report — derivations, Lyapunov proofs, and discussion — is in docs/Segway_Nonlinear_Control_Report.pdf.

📜 License

Code released under the MIT License. The report is © 2026 Ishan Sharma.


Authored by Ishan Sharma · M.S. Electrical Engineering, Texas A&M University · Control Systems & Robotics.

About

Nonlinear control of a two-wheeled inverted pendulum (Segway): Lagrangian modeling, Sliding Mode Control & Feedback Linearization with Lyapunov stability proofs, and MATLAB simulations comparing the controllers (MEEN 655, Texas A&M).

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages