MATLAB implementation of data-driven Koopman modeling and model predictive control (MPC) for stabilizing the roll of a modified Chaplygin sleigh. The sleigh is underactuated: an internal reaction wheel applies torque in yaw, and that same torque must propel the vehicle and keep its elevated center of mass upright.
Fish-like robots can achieve efficient, agile underwater motion, but slender body shapes may be unstable in roll. The modified Chaplygin sleigh provides a simple nonholonomic model for studying how internal actuation can generate propulsion while simultaneously stabilizing this roll motion.
This repository accompanies:
K. Loya and P. Tallapragada, “Stabilization of an Inverted Pendulum on a Nonholonomic System,” IFAC-PapersOnLine, vol. 55, no. 37, pp. 764–769, 2022. doi:10.1016/j.ifacol.2022.11.274
The vehicle configuration is
This reduces the dynamics to the four-state model
where
The lifted model turns the stabilization problem into a constrained quadratic program. At each MPC update, the controller penalizes roll-angle error and control effort, enforces the lifted linear dynamics, bounds reaction-wheel torque, applies the first control move to the nonlinear plant, and repeats.
The GIF previews play directly on GitHub. Select an animation to open its original, higher-quality MP4 file.
| 3D simulation | 3D simulation, run 01 |
|---|---|
![]() |
![]() |
- MATLAB with:
- Symbolic Math Toolbox (
syms,subs); - Optimization Toolbox (
quadprog); - Statistics and Machine Learning Toolbox for training scripts that call
normrnd; - Parallel Computing Toolbox for the
parfor-based large training runs.
- Symbolic Math Toolbox (
- Model Predictive Control Toolbox only for
codes/No theta/NLMPC for paper/NLMPC_CSroll.m. - GPOPS-II and its solver dependencies only for
codes/Gpops_roll_stabilize.m. Edit that script's machine-specificaddpathbefore use.
The code is a MATLAB research snapshot rather than a packaged application. Scripts use their current directory for dependencies and output, so run them from the directory in which they are stored.
From MATLAB:
cd('codes/No theta')
rng(1) % optional, for repeatable random training data
run('Koopman_CS_roll_randn.m')Important:
*.matis ignored by Git. A fresh clone does not contain the trained models even if they are present in a researcher's local working tree; run a training script before predictor or MPC scripts that callload(...).
cd('codes/No theta')
run('Koopman_roll_CS_simulation_not.m')The script compares the lifted linear prediction with the nonlinear RK4/ODE45 response. Its checked-in defaults set both input amplitudes to zero; edit A1, A2, W1, and W2 near the top to test a periodic input. The paper's corresponding comparison uses
The pre-generated paper-style predictor plots are in Results/prediction_1.pdf, Results/prediction_2.pdf, and Results/prediction_3.pdf.
cd('codes/No theta')
run('Sparsed_MPC.m')Sparsed_MPC.m loads the reduced Koopman model, constructs the lifted finite-horizon quadratic program, constrains the total torque, propagates the nonlinear plant, and plots torque, longitudinal velocity, yaw rate, roll, and roll rate.
The main parameters are near the top of the script:
| Variable | Meaning | Current value |
|---|---|---|
dt |
model sample time, loaded from the trained model | 0.001 s |
tsim |
closed-loop simulation duration | 2 s |
tpred |
prediction horizon duration | 0.1 s |
tcont |
control/update horizon duration | 0.001 s |
x0 |
||
maxA |
total torque magnitude limit | 50 N·m |
ql(4) |
roll-angle lifted-state weight | 10 |
R |
control-effort weight |
The published experiment used x0 accordingly when comparing directly with the paper.|
The paper reports M = 1.25 and C_w = 0.25; check and align the constants in the selected training and validation scripts before claiming a strict numerical reproduction.
Angles are in radians, angular rates in rad/s, longitudinal velocity in m/s, time in seconds, and control torque in N·m. In filenames and comments, “No theta” means that the yaw angle
If this repository or its method is useful in your work, please cite:
@article{loya2022stabilization,
title = {Stabilization of an Inverted Pendulum on a Nonholonomic System},
author = {Loya, Kartik and Tallapragada, Phanindra},
journal = {IFAC-PapersOnLine},
volume = {55},
number = {37},
pages = {764--769},
year = {2022},
doi = {10.1016/j.ifacol.2022.11.274}
}The repository is distributed under the Apache License 2.0. The bundled SOSTOOLS utility files contain their own GPL notices; consult those file headers when redistributing them.

