Modelling, system identification and control of a real double inverted pendulum: a cart on a rack, driven by a DC motor, carrying two hinged rods that must be balanced upright. The controller is an LQR state-feedback design with integral action on cart position, built in MATLAB/Simulink and run on the lab hardware. A Python reproduction lets you rerun the model and controller without MATLAB.
Course project for MECH 6741 Mechatronics, Concordia University, Winter 2019. Full write-up: docs/report.pdf. Codebase tidied up and tested in November 2026.
- Model. Equations of motion from Lagrange's method, written as
D(q) q'' + C(q, q') q' + G(q) = H uwith eight lumped parameters (d1..d6, f1, f2). - System identification. The rig was driven open loop from the hanging position while the three optical encoders were logged. Motor work over each interval equals the change in total energy, which is linear in the eight parameters, so they were fitted with bounded least squares. The identified model tracked the measured cart position to within 6 mm (standard error).
- Control.
- Swing-up: an energy-based controller derived from a Lyapunov function pumps energy into the rods until they approach upright.
- Stabilisation: the model is linearised about upright and an LQR gain is computed for the state
[x0, θ1, θ2, x0', θ1', θ2']plus the integral of cart position, so the cart also returns to centre.
- Experiments. Each controller worked on the rig on its own: the swing-up raised the rods to the 20° hand-off angle, and the LQR held the pendulum upright and rejected step disturbances. Run back to back, the hand-off failed, because mechanical pins that limit the upper rod's rotation add a nonlinearity the linear controller does not model. The report details this and the fix (removing the limit).
| Path | Contents |
|---|---|
matlab/DIP.m |
Builds the linear model and computes the LQR gain used by Simulink |
matlab/params_calc.m |
Measured masses, lengths and motor/encoder constants of the rig |
matlab/doublePendWork2015b.mdl |
Simulink model: swing-up controller, LQR, hardware I/O (MATLAB R2015b) |
matlab/identify_parameters.m |
Energy-based parameter identification (from the report's appendix) |
matlab/plotter.m |
Plots logged cart position and rod angles |
dip/ |
Python reproduction: nonlinear model, LQR design, closed-loop simulation |
tests/ |
Checks that the open loop is unstable, the closed loop is stable, and the nonlinear pendulum is balanced |
docs/report.pdf |
Project report |
MATLAB / Simulink (R2015b or later, Control System Toolbox):
cd matlab
DIP % computes K
open doublePendWork2015b % run the modelidentify_parameters.m needs the original encoder recording (doublePend.mat), which is not included.
Python (3.10+):
pip install -r requirements.txt
python -m dip.simulate --theta1 10 --theta2 5 --force-limit 10
python -m pytest--params identified switches from the measured physical parameters to the identified ones. --force-limit caps the cart force (the rig's motor delivers about 10 N at 6 V).
MATLAB, Simulink, Control System Toolbox, Optimization Toolbox · Python, NumPy, SciPy, Matplotlib · linear servo cart, Q4 data-acquisition board, optical encoders
Saud Altamimi, Amir Sayadi, Shervin Foroughi. The work was split evenly; the task breakdown is in section 8 of the report.
- A. Bogdanov, Optimal Control of a Double Inverted Pendulum on a Cart, OGI School of Science & Engineering, Tech. Rep. CSE-04-006, 2004.
- S. K. Yadav, S. Sharma, N. Singh, "Optimal control of double inverted pendulum using LQR controller," IJARCSSE 2(2), 2012.

