Quadrotor
Example
Quadrotor/
Where you can find this system
M. Hehn and R. D’Andrea, “A flying inverted pendulum,” ICRA, Shanghai, China, 2011, pp. 763–770.
Problem Description
The quadrotor we want to control is like this:
-
The state vector of the quadrotor is x = [X,\dot{X} , Y ,\dot{Y} ,Z ,\dot{Z} ,\gamma ,\beta ,\alpha ]^{T}\in\mathbb{R}^{9}, where (X,Y,Z) and (\gamma,\beta,\alpha) are the position and angles of the quadrotor, respectively.
-
The input vector is u=[a,\omega_{X},\omega_{Y},\omega_{Z}]^T, where a represents the thrust and (\omega_{X},\omega_{Y},\omega_{Z}) the rotational rates. The control input is bounded by u_{min}=[0,-1,-1,-1]^T \leq u \leq [11,1,1,1]^T = u_{max}.
-
The dynamics of the quadrotor are given by the following equations:
The task is to use NMPC to control the quadrotor to track the given position reference under a variable prediction horizon.
OCP in ParNMPC
The inequality constraints are handled using the logarithmic barrier functions.
The underlying OCP defined in ParNMPC
is formulated as:
- State: x=[X,\dot{X} , Y ,\dot{Y} ,Z ,\dot{Z} ,\gamma ,\beta ,\alpha ]^T.
- Input: u=[a,\omega_{X},\omega_{Y},\omega_{Z}].
- Parameter: p=[X_{ref},Y_{ref},Z_{ref},\gamma,T]^T, where \gamma>0 is the barrier parameter.
- L(u,x,p) = L_s(u,x,p) + L_{u}(u,x,p), where L_s(u,x,p) = \frac{1}{2}\|x-x_{ref}\|_{Q}^2+\frac{1}{2}\|u-u_{ref}\|_{R}^2 with x_{ref}=\text{diag}([X_{ref},0,Y_{ref},0,Z_{ref},0,0,0,0]), and L_u(u,x,p) is the summation of the barrier functions with parameters \gamma.
- f(u,x,p) is shown before.
- Prediction horizon T.
- Number of the discritization grids N=40.
- Discretization method: Euler in reverse time (backward Euler).
Closed-loop Simulation using ParNMPC
Step 1. NMPC problem formulation
See Workflow of ParNMPC > NMPC Problem Formulation.
Example
Quadrotor/NMPC_Problem_Definition.m
Step 2. Code generation and deployment in Simulink
See Workflow of ParNMPC > Code Generation and Deployment > Simulink.
-
Code generation
Example
Quadrotor/Simu_Simulink_Setup.m
-
Deployment
Example
Quadrotor/Simu_Simulink.slx
Step 2. Speed up the closed-loop simulation in Matlab using mex
-
Code generation
Example
Quadrotor/Simu_Simulink_Setup.m
Modify the generation target to
mex
:and run.NMPC_Iter_CodeGen('mex','C',args_NMPC_Iter);
-
Deployment
Example
Quadrotor/Simu_Matlab.m
Modify
NMPC_Iter
toNMPC_Iter_mex
to call the generatedmex
function:and run.[lambdaSplit,muSplit,uSplit,xSplit,... thetaSplit,cost,error,timeElapsed] = NMPC_Iter_mex(x0,lambdaSplit,muSplit,... uSplit,xSplit,pSplit,... thetaSplit,discretizationMethod,... isMEnabled);