P1 · Model Predictive Control: the gantry crane
Topic tab: 01 MPC · 2024 exam link: Q1 (role of Q, QF, R; receding horizon)
The problem in plain words
A cart runs along a 12 m crane track with a load hanging below it, modelled as a rigid rod. A motor pushes the cart with at most 100 N. The cart must carry a container from the dock (p = 2 m) to the ship (p = 10 m) in under 10 s, and for safety the load may not swing more than 10° or faster than 50°/s.
| Item | Your answer (Q1) |
|---|---|
| State | \(x=[p,\ v,\ \theta,\ \omega]^\top\): position, velocity, rod angle, angular rate |
| Initial condition | \(x_0=[2,0,0,0]^\top\) (at rest over the containers) |
| Goal | \(x^*=[10,0,0,0]^\top\): over the ship, stopped, no swing, within 10 s |
| State constraints | \(0\le p\le 12\) m, \(|v|\le 3\) m/s, \(|\theta|\le 10^\circ=0.1745\) rad, \(|\omega|\le 50^\circ/\text{s}=0.8727\) rad/s |
| Input constraint | \(|u|\le 100\) N |
The angle limits do two jobs: they keep the load safe, and they keep the system in the small-angle region where the linearised model is valid.
Step 1: continuous → discrete time
Controllers run on a computer, so the model is sampled every \(T_s=0.1\) s with a zero-order hold (input held constant between samples):
That is the line ABdisc = expm([Ac Bc; zeros(1,5)]*Ts) in your code.
Step 2: choosing Q and R (Bryson's rule)
The states have very different units (metres and radians), so start by normalising each weight with the largest value you will tolerate:
| State | max allowed | \(Q_{ii}=1/\max^2\) |
|---|---|---|
| p (8 m move) | 8 | 0.016 |
| v | 3 | 0.11 |
| θ | 0.1745 | 32.8 |
| ω | 0.8727 | 1.31 |
| u | 100 | R = 1×10−4 |
Then you tuned by looking at B: one newton for one sample changes ω by about 1.05 rad/s but v by only 0.23 m/s. The swing reacts long before the cart moves fast, so the swing limits are the binding constraints and the 100 N limit is never close. That is why your final choice was Q = diag([0.5 1 500 50]), R = 1: large weights on θ and ω, a moderate weight on p.
Step 3: what LQR achieved and why it is conservative
| Design | Settling | peak θ | peak ω | peak v | Meets spec? |
|---|---|---|---|---|---|
| Q = I, R = 1 | 4.4 s | 46° | 251°/s | 3.67 m/s | No, every state limit broken |
| Bryson | 11.3 s | 7.0° | 38°/s | 1.42 | No, too slow |
| diag(0.5,1,500,50), R=1 | 7.3 s | 8.7° | 37°/s | 1.82 | Yes |
LQR is a fixed linear law \(u=-K(x-x^*)\), so its biggest push happens at t = 0 when the error is 8 m. The gains have to be detuned for that worst moment, which slows the rest of the move: the cart only reaches 1.82 of the allowed 3 m/s. The constraints are met only indirectly, by trial and error, and the swing scales with the move size: the same gains on a 10 m move give about 10.8°, which breaks the limit.
Step 4: the MPC code ↔ the batch equations
Your loop builds the prediction \(\vec x=\Phi\vec u+\Lambda x_k\) (code: Phi = Φ, Delta = Λ), with \(\bar Q=\text{blkdiag}(Q,\dots,Q)\) and \(\bar R=\text{blkdiag}(R,\dots,R)\). Tracking cost \((\vec x-\vec x^*)^\top\bar Q(\vec x-\vec x^*)+\vec u^\top\bar R\vec u\) becomes a QP in \(\vec u\):
State bounds \(\vec x_{\min}\le\Phi\vec u+\Lambda x_k\le\vec x_{\max}\) are split into two "≤" rows each:
H is fixed, but f and bin depend on the measured \(x_k\), so the QP is re-solved every sample and only uMPCout(1) is applied. That is the receding-horizon principle in one line.
Size of the QP with N = 30: N = 30 decision variables (one input per step) and \(2\cdot4N+2N=10N=300\) inequality rows. Each QP must finish within Ts = 100 ms.
| Horizon | 0.5 s | 1 s | 2 s | 3 s (chosen) | 5 s |
|---|---|---|---|---|---|
| Settling time | 19.0 s | 8.8 s | 5.7 s | 5.4 s | 5.4 s |
MPC with plain Q = I, R = 1 and N = 30 settled in 5.4 s with no overshoot, riding exactly on the 10° and 50°/s limits. The weights no longer have to enforce safety, because the constraints do that. A longer horizon than 3 s cost computation and gained nothing. With Q = diag(1,0,0,0) and a 1 s horizon the QP became infeasible at about 2.9 s: the cart rushed, and by the time the target came into view it could no longer brake within the swing limits. There is no terminal constraint, so recursive feasibility is not guaranteed.
Plant \(x_{k+1}=2x_k+u_k\), q = r = qF = 1, N = 1, constraint \(|u|\le 0.5\). Start at \(x_0=2\).
Unconstrained: \(J=x^2+u^2+(2x+u)^2\Rightarrow dJ/du=2u+2(2x+u)=0\Rightarrow u=-x=-2\).
Constrained: with one decision variable, J is a convex parabola in u, so the constrained optimum is the unconstrained one clipped to the bound: \(u_0=-0.5\). Then \(x_1=4-0.5=3.5\), \(u_1=-0.5\), \(x_2=6.5\): the state diverges.
Lesson: no controller can beat physics. To stop |x| growing we need \(|2x+u|\le|x|\), and the best case is \(2|x|-0.5\le|x|\iff|x|\le0.5\). Outside that region the constrained problem cannot be stabilised. This is why feasibility and terminal sets matter. (With more than one decision variable, clipping is not the QP solution in general; you need the QP solver.)
- Constraints are only enforced at the sample times. Simulating the continuous model between samples gave about 10.1° and 53.7°/s, slightly over the limits. Fixes: tighten the constraints (9.5°, 45°/s) or reduce Ts.
- Running exactly on the limits leaves no robustness margin against disturbances (wind, payload mass), so hard constraints can become infeasible. Fixes: soft constraints or constraint tightening.
- Without constraints, and with the Riccati solution as QF, MPC gives exactly the LQR law.
Exam questions from P1
Compare LQR and MPC under five headings: horizon, constraints, online computation, form of the control law, and guarantees.
Show answer
- Horizon: LQR is infinite-horizon. MPC is finite-horizon N, and the receding horizon recovers infinite-horizon behaviour.
- Constraints: LQR cannot include them; you can only tune Q, R and hope. MPC writes them into the QP as inequalities.
- Computation: LQR solves the Riccati equation once offline, then computes \(u=-Kx\) online. MPC solves a QP every sample, which must finish within Ts.
- Law: LQR is a fixed linear gain. Constrained MPC is a nonlinear (piecewise-affine) feedback that changes with the active constraints.
- Guarantees: LQR is stabilising if (A,B) is stabilisable and (A,Q½) detectable. MPC needs a suitable N, terminal cost or terminal set for stability and recursive feasibility.
Using Bryson's rule, give Q and R for the crane with limits p 8 m (move size), v 3 m/s, θ 10°, ω 50°/s, u 100 N. Why were the final θ and ω weights made even larger than this?
Show answer
Q = diag(1/64, 1/9, 1/0.1745², 1/0.8727²) = diag(0.016, 0.11, 32.8, 1.31); R = 1/100² = 10−4. Bryson's weights met every limit but took 11.3 s, which is too slow. The input enters ω much more strongly than v (B4 ≈ −1.05 vs B2 ≈ 0.23), so swing is the active constraint. The final design raised the θ and ω weights relative to p (and used R = 1 for smoothness) to get 7.3 s while staying inside 10° and 50°/s.
With a 1 s horizon and Q = diag(1,0,0,0), R = 0.01, the MPC solver reported "infeasible" part-way through the move. Explain why, and give two remedies.
Show answer
Only position error is penalised, so the optimiser drives the cart as fast as allowed. A 1 s window cannot see the target until it is close; by then braking hard enough to stop would break the swing limits, so no input sequence satisfies all constraints and the QP is infeasible. Hard constraints plus a short horizon and no terminal constraint mean recursive feasibility is not guaranteed. Remedies: a longer horizon (2–3 s worked); weights on v, θ, ω for a smooth approach; a terminal constraint or terminal set that guarantees recursive feasibility; soft constraints with slack variables.
For a 4-state, 1-input system with box constraints on every state and the input, how many decision variables and inequality rows does the condensed MPC QP have for N = 20?
Show answer
Decision variables: N·m = 20. Inequalities: upper and lower bounds on 4 states at each of the N steps give 2·4·20 = 160, plus 2·20 = 40 for the input, so 200 rows in total (10N).