LQR Controller
AdvancedThe LQRController block is a linear-quadratic regulator. You give it a state-space model of the plant (, ) and two weights: , how much each state's deviation costs, and , how much control effort costs. The block computes the optimal state-feedback gain itself and outputs . Where a PID loop looks at one error signal, LQR uses every state at once — which is what lets it balance an inverted pendulum and bring the cart back to its place in the same move.
Wire each state of the plant to the inputs in the order of ; up to six states. To regulate to a setpoint instead of to zero, feed the deviation into that input. The designed gains appear on the optional outputs , and Valid reports whether a design could be made at all.
Mathematical Model
For the continuous design the block minimises
The optimal gain comes from the stabilising solution of the continuous algebraic Riccati equation:
For the discrete design (, cost ) it solves the discrete Riccati equation:
The output is . The design is accepted only when the closed loop is stable.
Inputs & Outputs
| Direction | ID | Label | Type | Status |
|---|---|---|---|---|
| → In | in1 |
x₀ | number | Required |
| → In | in2 |
x₁ | number | Optional |
| → In | in3 |
x₂ | number | Optional |
| → In | in4 |
x₃ | number | Optional |
| → In | in5 |
x₄ | number | Optional |
| → In | in6 |
x₅ | number | Optional |
| ← Out | out |
u | number | Output |
| ← Out | valid |
Valid | number | Output |
| ← Out | k_0 |
K₀ | number | Output |
| ← Out | k_1 |
K₁ | number | Output |
| ← Out | k_2 |
K₂ | number | Output |
| ← Out | k_3 |
K₃ | number | Output |
| ← Out | k_4 |
K₄ | number | Output |
| ← Out | k_5 |
K₅ | number | Output |
Parameters
| Parameters | Label | Type | Default | Description |
|---|---|---|---|---|
design |
Design | select | continuous |
Continuous: dx/dt = A·x + B·u, minimising ∫(xᵀQx + R·u²)dt. Discrete: x[k+1] = A·x[k] + B·u[k], minimising Σ(xᵀQx + R·u²); use with a discrete plant |
A |
A (n×n) | matrix | 0 1; 0 0 |
State matrix, rows separated by ;. Up to six states |
B |
B (n×1) | matrix | 0; 1 |
Input matrix, one entry per state |
Q |
Q | matrix | 1 0; 0 1 |
State weight: n numbers for the diagonal, or the full n×n matrix. A larger entry makes that state settle faster |
R |
R | number | 1 |
Input weight. Larger R means gentler control effort; it must be above 0 |
Usage Examples
Basic LQRController
A double integrator (a cart without friction, , ) starts 1 m from the origin. With and the block designs and brings the cart back without overshoot.
Inverted pendulum: PID versus LQR
The _Inverted Pendulum: PID vs LQR_ template balances the same pendulum twice: once with a PID on the angle alone, once with this block on all four states. The PID keeps the rod up but lets the cart run away; LQR catches the push and parks the cart at its setpoint.
Remarks & Best Practices
- Tuning: start with and , then raise the entry of the state you care about most. Scaling and together changes nothing.
- When it says Valid = 0: the sizes of , and do not fit, , or the plant has an unstable mode the input cannot reach. No gain can stabilise such a plant, so the block outputs rather than a gain that would fail.
- The model is yours: LQR is optimal for the and you type, not for the plant you wire. Use diagram variables for both so they cannot drift apart.
- All states needed: LQR feeds back every state. When only some can be measured, estimate the rest with a KalmanFilter (together: LQG control).
- Design is cached: is computed once per run and recomputed only when , , or change, so the block costs one dot product per step.
- Single input: the block designs for one control input. A plant with several inputs needs one gain row per input.
Related Components
- StateSpace" class="bw-link">StateSpace
- DiscreteStateSpace" class="bw-link">DiscreteStateSpace
- KalmanFilter" class="bw-link">KalmanFilter
- PIDController" class="bw-link">PIDController