Controls · Simulation
Nonlinear MPC for a simulated quadrotor
A nonlinear model predictive controller (MPC) flies a simulated 1 kg quadrotor through three courses of static and moving spheres, with no hardware involved.
The MPC finished all three courses without contact, while a tuned PID baseline finished two. Its solver averaged 4.8 ms per solve in the reported run and 8 to 15 ms in an earlier one, against a 20 ms control period.
The problem
A reactive controller acts only once an obstacle is close. The repulsive potential field in my PID baseline reaches 2 m past an obstacle's surface.
I wanted to measure what prediction buys, and whether a nonlinear program fits inside a 20 ms control period in Python. Both controllers fly the same simulated quadrotor along a straight reference that ignores the obstacles.
What I built
The plant is a 13-state rigid-body model of a 1 kg quadrotor, integrated with RK4 at 500 Hz and checked against closed-form physics. A motor mixer keeps each motor's thrust between 0 and 6 N.
The baseline is a PX4-style PID cascade with a Khatib potential field, plus a tangential term and closing-speed damping to make it harder to beat.
The MPC plans on a 10-state model and commands thrust and body rates. Its horizon is 20 stages of 75 ms, posed as direct multiple shooting in CasADi and solved by IPOPT every 20 ms.
Obstacles are keep-out spheres inflated by the 0.30 m body radius plus a 0.10 m margin. Moving ones are sampled along their known trajectories, so the plan avoids where each will be.
The first version, on CasADi's Opti interface, averaged 46 to 72 ms per solve. Three changes mattered most:
- Build the problem once as an SX-expanded nlpsol function, then only pass new parameters.
- Warm-start primal and dual variables from the previous solution, shifted one stage.
- Cap IPOPT at 15 iterations and a loose tolerance: the next solve is 20 ms away.
Results
Each controller flew each course once, with no random inputs. The MPC reached the goal on all three courses without contact. At its closest, the vehicle's center was 0.38 m from an obstacle surface, 0.08 m outside the 0.30 m body radius.
The PID baseline finished the two courses that have moving spheres. On the slalom, which has only static spheres, its body sphere touched an obstacle at 2.72 s and it ended 0.63 m from the goal.
Where both finished, the MPC was slightly slower: 5.54 s against 5.42 s on the crossing course and 6.32 s against 6.28 s on the gauntlet.
In the reported run, the solver averaged 4.8 ms over 1,894 solves (p95 11.1 ms). Four solves overran the 20 ms period, the slowest at 39.0 ms.
Limits and next steps
The MPC is given every obstacle's future trajectory and the true state, with no estimator or sensor noise. Commands apply with no computation delay, so a slow solve costs nothing here.
Solve times are wall-clock on Windows and vary between runs. An earlier logged run of the same solver code averaged 8 to 15 ms per course, and 150 of its 1,894 solves overran the period. In the reported run only 870 of the 1,894 solves came before arrival at the goal, averaging 6.9 ms.
Three hand-built courses, one run each, is a small sample. The keep-outs are nonconvex, so IPOPT only finds a locally optimal side of each obstacle.
A hardware version would first need a state estimator and a modeled computation delay.
Ask a question about this project
An AI assistant answers from this site's text. It can be wrong; this page is the source.


