% Script reproducing the steps of Part 2.

% Copyright 1986-2020 The MathWorks, Inc.
load csthl20_TrimData G7 CS

ST0 = slTuner('csthl20_control');
ST0.Ts = 0;   % ask for continuous-time linearizations
if CTYPE==2
   AP = {'da;de;dr'
      'HL20 Airframe/pqr'
      'Alpha_deg'
      'Beta_deg'
      'Phi_deg'
      'Controller/Classical/Demands'  % angular demands
      'p_demand'
      'q_demand'
      'r_demand'};
else
   AP = {'da;de;dr'
      'HL20 Airframe/pqr'
      'Alpha_deg'
      'Beta_deg'
      'Phi_deg'
      'Controller/MIMO/Demands'  % angular demands
      'p_demand'
      'q_demand'
      'r_demand'};
end

% call model update to make sure only active variant signals are analyzed during linearization
set_param('csthl20_control', 'SimulationCommand', 'update');

ST0.addPoint(AP)

BlockSub1 = struct('Name','csthl20_control/HL20 Airframe','Value',G7);
BlockSub2 = struct('Name','csthl20_control/Flight Control System/Controls Selector','Value',CS);
BlockSub3 = struct('Name','csthl20_control/Actuators','Value',eye(6));
ST0.BlockSubstitutions = [BlockSub1 ; BlockSub2 ; BlockSub3];

% Apply results from Part 2 to model
load csthl20_GainData Kp Kq Kr
MWS = get_param('csthl20_control','ModelWorkspace');
MWS.assignin('Kp',squeeze(Kp))
MWS.assignin('Kq',squeeze(Kq))
MWS.assignin('Kr',squeeze(Kr))

refresh(ST0)
