Code documentation

class mpc.MPCController(Ad, Bd, Np=20, Nc=None, x0=None, xref=None, uref=None, uminus1=None, Qx=None, QxN=None, Qu=None, QDu=None, xmin=None, xmax=None, umin=None, umax=None, Dumin=None, Dumax=None, eps_feas=1000000.0, eps_rel=0.001, eps_abs=0.001)

This class implements an MPC controller

Attributes:
Ad : 2D array_like. Size: (nx, nx)

Discrete-time system matrix Ad.

Bd : 2D array-like. Size: (nx, nu)

Discrete-time system matrix Bd.

Np : int

Prediction horizon. Default value: 20.

Nc : int

Control horizon. It must be lower or equal to Np. If None, it is set equal to Np.

x0 : 1D array_like. Size: (nx,)

System state at time instant 0. If None, it is set to np.zeros(nx)

xref : 1D array-like. Size: (nx,)

System state reference (aka target, set-point).

uref : 1D array-like. Size: (nu, ).

System input reference. If None, it is set to np.zeros(nx)

uminus1 : 1D array_like

Input value assumed at time instant -1. If None, it is set to uref.

Qx : 2D array_like

State weight matrix. If None, it is set to eye(nx).

Qu : 2D array_like

Input weight matrix. If None, it is set to zeros((nu,nu)).

QDu : 2D array_like

Input delta weight matrix. If None, it is set to zeros((nu,nu)).

xmin : 1D array_like

State minimum value. If None, it is set to -np.inf*ones(nx).

xmax : 1D array_like

State maximum value. If None, it is set to np.inf*ones(nx).

umin : 1D array_like

Input minimum value. If None, it is set to -np.inf*ones(nx).

umax : 1D array_like

Input maximum value. If None, it is set to np.inf*ones(nx).

Dumin : 1D array_like

Input variation minimum value. If None, it is set to np.inf*ones(nx).

Dumax : 1D array_like

Input variation maximum value. If None, it is set to np.inf*ones(nx).

eps_feas : float

Scale factor for the matrix Q_eps. Q_eps = eps_feas*eye(nx).

eps_rel : float

Relative tolerance of the QP solver. Default value: 1e-3.

eps_abs : float

Absolute tolerance of the QP solver. Default value: 1e-3.

output(return_x_seq=False, return_u_seq=False, return_eps_seq=False, return_status=False, return_obj_val=False)

Return the MPC controller output uMPC, i.e., the first element of the optimal input sequence and assign is to self.uminus1_rh.

Parameters:
return_x_seq : bool

If true, the method also returns the optimal sequence of states

Returns:
array_like (nu,)

The first element of the optimal input sequence uMPC to be applied to the system.

setup(solve=True)

Set-up the QP problem.

Parameters:
solve : bool

If True, also solve the QP problem.

solve()

Solve the QP problem.

update(x, u=None, xref=None, solve=True)

Update the QP problem.

Parameters:
x : array_like. Size: (nx,)

The new value of x0.

u : array_like. Size: (nu,)

The new value of uminus1. If none, it is set to the previously computed u.

xref : array_like. Size: (nx,)

The new value of xref. If none, it is not changed

solve : bool

If True, also solve the QP problem.

kalman.kalman_filter_simple(A, B, C, D, Qn, Rn)

Design a Kalman filter for the discrete-time system

\[\begin{split}\begin{split} x_{k+1} &= Ax_{k} + Bu_{k} + Iw_{k}\\ y_{k} &= Cx_{k} + Du_{k} + I v_{k} \end{split}\end{split}\]

with known inputs u and stochastic disturbances w and v. In particular, w and v are zero mean, white Gaussian noise sources with E[vv’] = Qn, E[ww’] = Rn, E[‘wv’] = 0

The Kalman filter has structure

\[\begin{split}\begin{split} \hat x_{k+1} &= Ax_{k} + Bu_{k} + L(y_{k} - C\hat x{k} - Du_{k})\\ \hat y_{k} &= Cx_k + Du_k \end{split}\end{split}\]