Model Based Code Generation for Nonlinear Model Predictive Control

0 downloads 0 Views 2MB Size Report
What is MPC? ▻ MPC is a closed loop implementation of optimal control. ... MPC can be designed for MIMO nonlinear systems. ... Figure: Autonomous Driving ...
Numerical Software Verification Workshop 2016

Model Based Code Generation for Nonlinear Model Predictive Control Behzad Samadi Department of Research and Development, Maplesoft

Outline

I

Control theory

I

Modeling

I

Model predictive control

I

Model based code generation

Control Theory

Control Theory is Rocket Science

Feedback Control

Modeling

Causal Modeling

Lorenz Attractor Ordinary differential equations (ODE):

d x (t) = −a(y (t) − x (t)) dt d y (t) = (b − z(t))x (t) − y (t) dt d z(t) = x (t)y (t) − cz(t) dt

Causal Modeling

Lorenz Attractor

Causal Modeling Lorenz Attractor

Acausal Modeling

Simple DC Motor

Acausal Modeling Manipuator Robot with 5 Degrees of Freedom

Acausal Modeling

Manipuator Robot with 5 Degrees of Freedom

Model Predictive Control (MPC)

Model Predictive Control (MPC)

I

What is MPC? I

I

MPC is a closed loop implementation of optimal control.

Why not use a PID? I I

I

PID controllers do not perform well for highly nonlinear systems. To control a multi-input multi-output (MIMO) system, several PID controllers should be connected in a non-trivial configuration. Tuning PID controllers is not an easy task, especially given state and input constraints.

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 much more intuitive comparing to a PID.

I

MPC is a systematic model based approach.

I

There are tools for generating code for MPC controllers automatically.

Nonlinear System Model

Automatic Code Generation

MPC Controller Code

Figure: Automatice Code Generation

Example I I I

A race driver needs to look forward! (prediction) Minimize a cost function at each time instant depending on the current situation (closed loop optimal control) Optimization constraints: I I I I I

Vehicle dynamics behavior Limited power No skidding Following the road Avoiding collision

Figure: Autonomous Driving

MPC Applications I

Chemical process control I I

I

Mechatronics I I

I

Robotics Washing machines

Automotive I

I

Oil refineries Pulp and paper

Engine controllers

Aerospace I I

Aircraft control Spacecraft formation and attitude control

I

Power generation

I

Computational biology

Nonlinear Model

I

Consider the following nonlinear system:

x˙ (t) =f (x (t), u(t)) x (t0 ) =x◦ where: I I

x (t) is the state vector u(t) is the input vector

Optimal Control Problem

Z tf

minimize J(x0 , t0 ) = φ(x (tf )) + u

L(x (τ ), u(τ ))dτ t0

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

Barrier Method I

Using a particular interior-point algorithm, the barrier method, the inequality constraints are converted to equality constraints:

minimize φ(x (tf )) + u,α

Z tf 



L(x (τ ), u(τ )) − r T α(τ ) dτ

t0

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 of r ∈ Rnh are small positive numbers. (Boyd and Vandenberghe 2004) (Diehl, Ferreau, and Haverbeke 2009)

Discretization I

Discretize the problem into N steps from t0 to tf :

minimize φd (xN ) + u,α

N−1 X

L(xk , uk ) − r T αk



k=0

subject to xk+1 = fd (xk , uk ) x0 = x◦ gi (xk , uk ) = 0, for i = 1, . . . , ng 2 = 0, for i = 1, . . . , nh hi (xk , uk ) + αik

where ∆τ =

tf −t0 N

and: φ(xN , N) =

φ(x (tf ), tf ) ∆τ

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

Optimization Problem

minimize φd (xN ) + u,α

N−1 X

L(xk , uk ) − r T αk

k=0

subject to xk+1 = fd (xk , uk ) x0 = x◦ G(xk , uk , αk ) = 0 where: 

g1 (xk , uk ) .. .

    gng (xk , uk )  G(xk , uk , αk ) =  2  h1 (xk , uk ) + α1k  ..   .

hnh (xk , uk ) + αn2h k

          



Lagrange Multipliers I

Lagrangian:

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

N−1 X

L(xk , uk ) − r T αk

k=0

+ (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

Hamiltonian

I

The Lagrangian can be rewritten as: L(x , u, α, λ, ν) =φd (xN ) + x◦T λ0 − xNT λN +

N−1 X

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



k=0 I

Hamiltonian: H(xk , uk , αk , λk+1 , νk ) =L(xk , uk ) − r T αk + fd (xk , uk )T λk+1 + G(xk , uk , αk )T νk

Pontryagin’s Maximum Principle

Optimality Conditions Lλk+1 = 0 L λ0 = 0 Lxk = 0 LxN = 0 Luk = 0 L αk = 0 Lνk = 0 for k = 0, . . . , N − 1

? xk+1 = fd (xk? , uk? ) x0? = x◦ ? λk = Hx (xk? , uk? , αk? , λ?k+1 , νk? ) λ?N = ∂x∂N φd (xN? ) Hu (xk? , uk? , αk? , λ?k+1 , νk? ) = 0 Hα (xk? , uk? , αk? , λ?k+1 , νk? ) = 0 G(xk? , uk? , αk? ) = 0

Model Predictive Control

I

I understand 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.

Continuation/GMRES Method

I

Step 1: Compute xk and λk as a function of uk and αk , given the following equations:

xk+1 = fd (xk , uk ) x0 = xn λk = Hx (xk , uk , αk , λk+1 , νk ) ∂ λN = φd (xN ) ∂xN (Ohtsuka 2004)

Continuation/GMRES Method

I

Step 2: Solve the equation F (xn , U) = 0, where: 

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

     F (xn , U) =     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 ) T , αT , . . . , αT T T T where U = [u0T , . . . , uN−1 0 N−1 , ν0 , . . . , νN−1 ]

(Ohtsuka 2004)

Continuation/GMRES Method I

Continuation method: Instead of solving F (x , U) = 0, find U such that: F˙ (x , U) = As F (x , U) where As is a matrix with negative eigenvalues.

I

Now, we have: Fx x˙ + FU U˙ = As F (x , U)

I

GMRES: To compute U˙ using the following equation, which is ˙ we use the generalized minimum residual (GMRES) linear in U, algorithm. FU U˙ = As F (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)

Example Electro Hydraulic Servo System

Electro Hydraulic Servo System

Automatic Code Generation

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 Moving Horizon Estimation.” In Nonlinear Model Predictive Control, 391–417. Springer. Ohtsuka, Toshiyuki. 2004. “A Continuation/GMRES Method for Fast Computation of Nonlinear Receding Horizon Control.” Automatica 40 (4). Elsevier: 563–74.