%% ORR - lab 9 - Design an LQG regulator for an F16 aircraft subsytem model
% Design LQG regulator (LQR + Kalman filter) for the model of an F16 aircraft subsystem. The system matrices defined in the code below. This exercise should be a repetition for you as a vast majority of you should be familiar with the LQG controller from the previous course on linear systems.
% There is plenty of ways of designing and implementing LQG in Matlab. You can separately design LQR and Kalman directly by solving Continuous Algebraic Riccati Equation, or you can use functions lqr() and kalman(), or you can use function lqg() and design both the state feedback and the observer at once just by calling one function. Alternatively, you can also use the Kalman Filter block in Simulink to design and implement the Kalman filter. Try at least two of the mentioned approaches.
% Choose the covariance matrices of the Kalman filter so that the filter works well in simulations. In this exercise, you can view these matrices as tuning knobs. Nevertheless, recall that these matrices actually have some physical interpretation. You can learn how to design these matrices properly and how to check that the filter works properly in the course B3M35OFD.
% When you finish the design of the LQQ, implement the regulator in the attached Simulink model, and simulate the response of the closed-loop system. By the way, even though you have access to all the states of the model in the Simulink model, you are supposed to use them only for checking that your Kalman filter gives good estimates of the true values of the states.

% Continous state-space description
A = [-.0151 -60.5651 0 -32.174;
     -.0001 -1.3411 .9929 0;
     .00018 43.2541 -.86939 0;
      0      0       1      0];

%  Inputs: elevator angle [deg] and flaperon angle [deg]
B = [-2.516 -13.136;
     -.1689 -.2514;
     -17.251 -1.5766;
     0        0];
% Outputs: attack angle [deg] and pitch angle [deg]
C = [0 1 0 0;
     0 0 0 1];
D = [0 0;
     0 0];
 
sys = ss(A, B, C, D);