An open, tutorial-oriented build of an inverted pendulum on a cart, taken from Lagrangian dynamics all the way to working hardware. The single pendulum is the validated milestone; the double inverted pendulum on a cart (DIPC) is the target the same architecture extends to.
This repository is written to be read, not just run. Each stage pairs a theory notebook (derivations in LaTeX), a Python simulation, and the firmware that puts it on real hardware, so the path from equations to a moving system is fully traceable.
Status: single pendulum validated on hardware · double pendulum swing-up working in simulation, hardware in progress.
The inverted pendulum on a cart is the standard benchmark for underactuated, nonlinear, unstable control. The double version raises every one of those difficulties: it is strongly coupled, has multiple unstable equilibria, and cannot be stabilized by classical single-loop methods.
The design follows a strict design-time versus runtime split. All heavy computation (dynamics derivation, LQR synthesis, trajectory optimization) happens offline on a laptop. Only lightweight lookups and small matrix operations run on the microcontroller.
- Swing-up uses energy shaping: the controller pumps energy toward the level of the upright equilibrium, with a small deliberate overshoot to compensate for pivot friction.
- Capture uses LQR designed on the linearized upright model. The dominant limit on the basin of attraction is actuator saturation rather than gain tuning.
- For the double pendulum, energy shaping alone is insufficient because reaching the correct energy leaves the link configuration almost unconstrained. The planned architecture is a trajectory library: nominal trajectories from direct collocation, tracked by time-varying LQR, handing off to an LQR catch at each equilibrium.
Primary controller is the Arduino UNO R3. A Raspberry Pi (with a MicroPython port of the firmware) is retained as a backup platform.
Electrical components:
- Arduino UNO R3 (primary microcontroller)
- DRV8825 stepper motor driver
- NEMA 17 stepper motor
- AS5600 magnetic encoder (pendulum angle, I2C)
- BNO055 IMU (cart wheel slip prevention, I2C)
- 24V DC power supply
- 470 uF capacitor
- Connecting wires
- Laptop (powers the microcontroller and uploads code)
Mechanical build: a cart on a linear guide, driven by the stepper through a friction wheel running in a channel rail, with the pendulum on a low-friction pivot. Instead of the conventional pulley-belt drive, the motor is mounted directly on the cart. The control formulation treats cart acceleration as the input, which removes rail and drivetrain friction from the model and leaves pivot friction as the only dissipative term, making the model much simpler.
Dependencies: numpy, scipy, sympy, python-control, pygame.
Simulated double-pendulum swing-up (Day 7). Both links start hanging (180°) and are driven upright (0°) by time-varying LQR tracking a direct-collocation nominal trajectory, then handed to an LQR catch. Panels show link angles, cart position, applied force against the ±60 N actuator limit (red), and tracking error.
The repository follows the order in which I learned the theory and built the project. Each day covers one milestone, pairing a Jupyter notebook with the theory and Python files that apply it in simulation.
| Folder | Milestone |
|---|---|
Day_1 |
Cart-pole dynamics from the Lagrangian |
Day_2 |
LQR stabilization about the upright |
Day_3 |
Energy-shaping swing-up with LQR capture |
Day_4 |
Model predictive control (MPC) |
Day_5 |
On-chip controller with serial and pygame telemetry viewers |
Day_6 |
Double pendulum dynamics, LQR design, basin-of-attraction analysis |
Day_7 |
Trajectory optimization and time-varying LQR swing-up |
Day_8 |
Friction modelling, controller tuning, and hardware firmware |
Julia |
Julia port of the pendulum simulation |
Tutorials |
Primers on NumPy, SciPy, SymPy, Matplotlib, pygame, python-control and Manim |
- The double pendulum swing-up is verified in simulation only; the hardware build currently runs the single pendulum.
- Pivot friction is the only modelled dissipation, so the model depends on the cart-acceleration input assumption holding on the real drivetrain.
Institute Technical Summer Project, IIT Bombay.
Team members: Srinath Sridhar, Adhrit Majumdar, Ishaan Tulara, Suhas Kashyap Mentor: Srivathsa Thotakura
Built under the Institute Technical Summer Projects (ITSP) program of the Institute Technical Council, IIT Bombay.
