22
Real Time Code Generation for Nonlinear Model Predictive Control Behzad Samadi Department of Research and Development

Real Time Code Generation for Nonlinear Model Predictive Control

Embed Size (px)

Citation preview

Page 1: Real Time Code Generation for Nonlinear Model Predictive Control

Real Time Code Generation for

Nonlinear Model Predictive ControlBehzad Samadi

Department of Research and Development

Page 2: Real Time Code Generation for Nonlinear Model Predictive Control

Model Predictive Control (MPC)

I What is MPC?I MPC is a closed loop implementation of optimal control.

I Why not use a PID?I PID controllers do not perform well for highly nonlinear systems.I To control a multi-input multi-output (MIMO) system, many

PID controllers in different loops are required.I Tuning PID controllers is not an easy task, especially when

there are state and input constraints.

Page 3: Real Time Code Generation for Nonlinear Model Predictive Control

Why MPC?

I MPC can handle constraints on inputs and states.I MPC can be designed for MIMO nonlinear systems.I Tuning an MPC controller is more intuitive compared to a set

of PID controllers.I MPC is a systematic, model based approach.I There are tools for generating code for MPC controllers

automatically.

NonlinearSystem Model

MPC ControllerCode

AutomaticCode Generation

Page 4: Real Time Code Generation for Nonlinear Model Predictive Control

ExampleI A race driver needs to look forward! (prediction)I Minimize a cost function at each time instant depending on the

current situation (closed loop optimal control)I Optimization constraints:

I Vehicle’s dynamic behaviorI Limited powerI No skiddingI Following the roadI Avoiding collision

Page 5: Real Time Code Generation for Nonlinear Model Predictive Control

MPC Applications

I Chemical process controlI Oil refineriesI Pulp and paper

I MechatronicsI RoboticsI Washing machines

I AutomotiveI Engine controllers

I AerospaceI Aircraft controlI Spacecraft formation and attitude control

I Power generationI Computational biology

Page 6: Real Time Code Generation for Nonlinear Model Predictive Control

Nonlinear Model

I Consider the following nonlinear system:

x(t) =f (x(t), u(t))x(t0) =x◦

where:I x(t) is the state vectorI u(t) is the input vector

Page 7: Real Time Code Generation for Nonlinear Model Predictive Control

Optimal Control Problem

minimizeu

J(x0, t0) = φ(x(tf )) +∫ tf

t0L(x(τ), u(τ))dτ

subject to x(t) = f (x(t), u(t))x(t0) = x◦

gi (x(t), u(t)) = 0, for i = 1, . . . , ng

hi (x(t), u(t)) ≤ 0, for i = 1, . . . , nh

Page 8: Real Time Code Generation for Nonlinear Model Predictive Control

Barrier MethodI Using a particular interior-point algorithm, the barrier method,

the inequality constraints are converted to equality constraints:

minimizeu,α

φ(x(tf )) +∫ tf

t0

(L(x(τ), u(τ))− rTα(τ)

)dτ

subject to x(t) = f (x(t), u(t))x(t0) = x◦

gi (x(t), u(t)) = 0, for i = 1, . . . , ng

hi (x(t), u(t)) + αi (t)2 = 0, for i = 1, . . . , nh

where α(t) ∈ Rnh is a vector slack variable and the entries ofr ∈ Rnh are small positive numbers.

(Boyd and Vandenberghe 2004)(Diehl, Ferreau, and Haverbeke 2009)

Page 9: Real Time Code Generation for Nonlinear Model Predictive Control

DiscretizationI Discretize the problem into N steps from t0 to tf :

minimizeu,α

φd (xN) +N−1∑k=0

(L(xk , uk)− rTαk

)subject to xk+1 = fd (xk , uk)

x0 = x◦

gi (xk , uk) = 0, for i = 1, . . . , ng

hi (xk , uk) + α2ik = 0, for i = 1, . . . , nh

where ∆τ = tf −t0N and:

φd (xN) = φ(x(tf ), tf )∆τ

fd (xk , uk) = xk + f (xk , uk)∆τ

Page 10: Real Time Code Generation for Nonlinear Model Predictive Control

Optimization Problem

minimizeu,α

φd (xN) +N−1∑k=0

(L(xk , uk)− rTαk

)subject to xk+1 = fd (xk , uk)

x0 = x◦

G(xk , uk , αk) = 0

where:

G(xk , uk , αk) =

g1(xk , uk)...

gng (xk , uk)h1(xk , uk) + α2

1k...

hnh (xk , uk) + α2nhk

Page 11: Real Time Code Generation for Nonlinear Model Predictive Control

Lagrange Multipliers

I Lagrange multipliers:

L(x , u, α, λ, ν) = φd (xN ,N) + (x◦ − x0)Tλ0

+N−1∑k=0

(L(xk , uk)− rTαk

+ (fd (xk , uk)− xk+1)Tλk+1

+G(xk , uk , αk)Tνk)

I Optimality conditions:

Lxk = 0,Lλk = 0 for k = 0, . . . ,N

Lαk = 0,Luk = 0,Lνk = 0 for k = 0, . . . ,N − 1

Page 12: Real Time Code Generation for Nonlinear Model Predictive Control

Hamiltonian

I L(x , u, α, λ, ν) can be rewritten as:

L(x , u, α, λ, ν) =φd (xN) + xT◦ λ0 − xT

NλN

+N−1∑k=0

(H(xk , uk , αk , λk+1)− xT

k λk)

I Hamiltonian:

H(xk , uk , αk , λk+1, νk) =L(xk , uk)− rTαk

+ fd (xk , uk)Tλk+1 + G(xk , uk , αk)Tνk

Page 13: Real Time Code Generation for Nonlinear Model Predictive Control

Pontryagin’s Maximum Principle

Optimality Conditions

Lλk+1 = 0 x?k+1 = fd (x?k , u?k)Lλ0 = 0 x?0 = x◦Lxk = 0 λ?k = Hx (x?k , u?k , α?k , λ?k+1, ν

?k )

LxN = 0 λ?N = ∂∂xN

φd (x?N)Luk = 0 Hu(x?k , u?k , α?k , λ?k+1, ν

?k ) = 0

Lαk = 0 Hα(x?k , u?k , α?k , λ?k+1, ν?k ) = 0

Lνk = 0 G(x?k , u?k , α?k) = 0

for k = 0, . . . ,N − 1 where ? denote the optimal solution

Page 14: Real Time Code Generation for Nonlinear Model Predictive Control

Model Predictive Control

I This is optimal control but what is MPC?I MPC is the optimal controller in the loop:

1. Measure/estimate the current state xn.2. Solve the optimal control problem to compute uk for

k = n, . . . , n + N − 1.3. Return un as the value of the control input.4. Update n.5. Goto step 1.

I MPC is implemented in real time.

Page 15: Real Time Code Generation for Nonlinear Model Predictive Control

Continuation/GMRES Method

I Step 1: Compute xk and λk as functions of uk , αk and νk ,given the following equations:

xk+1 = fd (xk , uk)x0 = xn

λk = Hx (xk , uk , αk , λk+1, νk)

λN = ∂

∂xNφd (xN)

(Ohtsuka 2004)

Page 16: Real Time Code Generation for Nonlinear Model Predictive Control

Continuation/GMRES Method

I Step 2: For

U = [uT0 , . . . , uT

N−1, αT0 , . . . , α

TN−1, ν

T0 , . . . , ν

TN−1]T

solve the equation F (xn,U) = 0, where:

F (xn,U) =

Hu(x0, u0, α0, λ1, ν0)Hα(x0, u0, α0, λ1, ν0)

G(x0, u0, α0)...

Hu(xN−1, uN−1, αN−1, λN , νN−1)Hα(xN−1, uN−1, αN−1, λN , νN−1)

G(xN−1, uN−1, αN−1)

(Ohtsuka 2004)

Page 17: Real Time Code Generation for Nonlinear Model Predictive Control

Continuation/GMRES MethodI Continuation method: Instead of solving F (x ,U) = 0, find U

such that:F (x ,U) = AsF (x ,U)

where As is a matrix with negative eigenvalues.I Now, we have:

Fx x + FUU = AsF (x ,U)

I GMRES: To compute U using the following equation, which islinear in U, we use the generalized minimum residual (GMRES)algorithm.

FUU = AsF (x ,U)− Fx f (x , u)I To compute U at any given time, we need to have an initial

value for U and then use the above U to update it.

(Ohtsuka 2004)

Page 18: Real Time Code Generation for Nonlinear Model Predictive Control

Example

I Controling the position and orientation of a hovercraft:

(Seguchi and Ohtsuka 2003)

Page 19: Real Time Code Generation for Nonlinear Model Predictive Control

ExampleI Dynamic equations:

Mx =u1 cos(θ) + u2 cos(θ)My =u1 sin(θ) + u2 sin(θ)

I θ =u1r − u2r

I Variables:

Variable Type Unit

x position my position mθ orientation radu1 force Nu2 force N

(Seguchi and Ohtsuka 2003)

Page 20: Real Time Code Generation for Nonlinear Model Predictive Control

Example

I Nonlinear Model Predictive Control (NMPC)I Automatic code generation

Page 21: Real Time Code Generation for Nonlinear Model Predictive Control

References

Boyd, S.P., and L. Vandenberghe. 2004. Convex Optimization.Cambridge Univ Pr.

Diehl, Moritz, Hans Joachim Ferreau, and Niels Haverbeke. 2009.“Efficient Numerical Methods for Nonlinear MPC and MovingHorizon Estimation.” In Nonlinear Model Predictive Control,391–417. Springer.

Ohtsuka, Toshiyuki. 2004. “A Continuation/GMRES Method forFast Computation of Nonlinear Receding Horizon Control.”Automatica 40 (4). Elsevier: 563–74.

Seguchi, Hiroaki, and Toshiyuki Ohtsuka. 2003. “NonlinearReceding Horizon Control of an Underactuated Hovercraft.”International Journal of Robust and Nonlinear Control 13 (3-4).Wiley Online Library: 381–98.

Page 22: Real Time Code Generation for Nonlinear Model Predictive Control