## Contents

- Set Initial Conditions
- Set Flight & Ctrl Limits (for pass-fail conditions)
- Build Initial Condition Vectors
- Select Desired F-16 Plant
- Save results

    % Configures the settings for, runs, and saves an F-16 simulation.
    % Within this script, the user inputs all initial conditions and autopilot
    % instructions for a given simulation scenario. This is the only function
    % that must be to complete a simulation and save the results. The scripts
    % MakeAnimation and MakePicture can then be run and operate from the
    % results in the workspace.
    %
    % Note: This version of MAIN is a barebones sample of what is required to
    % run a single simulation. Results are printed to console, but not plotted.
    %
    % <a href="https://github.com/pheidlauf/AeroBenchVV">AeroBenchVV</a>
    % Copyright: GNU General Public License 2017
    %
    % See also: RUNF16SIM, GETDEFAULTSETTINGS, MAKEANIMATION, MAKEPICTURE,

## Set Initial Conditions

    close all; clear; clc;
    powg = 9;                   % Power

    % Default alpha & beta
    alphag = deg2rad(2.1215);   % Trim Angle of Attack (rad)
    betag = 0;                  % Side slip angle (rad)

    % Initial Attitude (for simpleGCAS)
    altg = 3500;
    Vtg = 540;                  % Airspeed (ft/s)
    phig = (pi/2)*0.5;          % Roll angle from wings level (rad)
    thetag = (-pi/2)*0.8;       % Pitch angle from nose level (rad)
    psig = -pi/4;               % Yaw angle from North (rad)
    t_vec = 0:0.01:15;          % Time vector for simulation output

## Set Flight & Ctrl Limits (for pass-fail conditions)

    [flightLimits,ctrlLimits,autopilot] = getDefaultSettings()
    autopilot.simpleGCAS = true;    % Run GCAS simulation

    flightLimits =

          altitudeMin: 0
          altitudeMax: 10000
         maneuverTime: 15
                NzMax: 9
                NzMin: -2
        psMaxAccelDeg: 500
                 vMin: 300
                 vMax: 900
          alphaMinDeg: -10
          alphaMaxDeg: 45
           betaMaxDeg: 30

    ctrlLimits =

           ThrottleMax: 1
           ThrottleMin: 0
        ElevatorMaxDeg: 25
        ElevatorMinDeg: -25
         AileronMaxDeg: 21.500000000000000
         AileronMinDeg: -21.500000000000000
          RudderMaxDeg: 30
          RudderMinDeg: -30
            MaxBankDeg: 60
                 NzMax: 6
                 NzMin: -1

    autopilot =

                        title: 'Default Simulation'
            basicSpeedControl: 1
        steadyLevelFlightHold: 0
             levelTurnControl: 0
                   simpleGCAS: 0
                turnToHeading: 0
         timeTriggeredControl: 0

## Build Initial Condition Vectors

state = [VT, alpha, beta, phi, theta, psi, P, Q, R, pn, pe, h, pow]

    initialState = [Vtg alphag betag phig thetag psig 0 0 0 0 0 altg powg];
    orient = 4;             % Orientation for trim
    analysisOn = true;
    printOn = true;
    plotOn = true;

## Select Desired F-16 Plant

Polynomial

    [output, passFail] = RunF16Sim(initialState, t_vec, orient, 'morelli',...
        flightLimits, ctrlLimits, autopilot, analysisOn, printOn, plotOn);

    ------------------------------------------------------------
    F-16 Decoupled LQR Controller for Nz, P_s, and Ny+r tracking
    ------------------------------------------------------------

    Operator Inputs =
                            Vt            h        gamma       psidot     thetadot
                     540.00000   3500.00000            0            0            0

    Trim Orientation Selected:   Steady Pull Up

    Inititial Conditions =
                            Vt        alpha         beta          phi        theta
                     540.00000      0.03703            0      0.78540     -1.25664

                           psi            p            q            r           pn
                      -0.78540            0            0            0            0

                            pe          alt          pow
                             0   3500.00000      9.00000

    Control Guess =
                      throttle     elevator      aileron       rudder
                       0.20000            0            0            0

    ------------------------------------------------------------
    Equilibrium / Trim Conditions

    State Equilibrium =
                            Vt        alpha         beta          phi        theta
                     540.00000      0.03441            0            0      0.03441

                           psi            p            q            r           pn
                             0            0            0            0            0

                            pe          alt          pow
                             0   3500.00000     10.35403

    Control Equilibrium =
                      throttle     elevator      aileron       rudder
                       0.15944     -0.77048            0            0

    ------------------------------------------------------------
    Running jacobFun.m
    Linearized F-16 SS Model

    lin_f16 =

      A =
                      Vt       alpha        beta         phi       theta
       Vt       -0.01658        10.6  -8.377e-06  -2.766e-06      -32.17
       alpha  -0.0002195     -0.9855   3.055e-06  -1.488e-06  -1.025e-05
       beta            0           0     -0.3124     0.05954           0
       phi             0           0           0           0           0
       theta           0           0           0           0           0
       psi             0           0           0           0           0
       p               0           0       -31.6           0           0
       q       6.662e-13       0.858           0           0           0
       r               0           0       8.904           0           0
       pn              1  -6.607e-06           0  -1.592e-06  -6.607e-06
       pe              0           0         540      -18.58           0
       alt    -6.579e-16        -540           0    4.64e-06         540
       pow             0           0           0           0           0

                     psi           p           q           r          pn
       Vt              0           0     -0.4842           0           0
       alpha           0           0      0.9146           0           0
       beta            0     0.03387           0     -0.9925           0
       phi             0           1           0     0.03443           0
       theta           0           0           1           0           0
       psi             0           0           0       1.001           0
       p               0      -3.574   0.0002627      0.6331           0
       q               0  -8.795e-06      -1.045   -0.002858           0
       r               0    -0.02249    0.002539     -0.4621           0
       pn              0           0           0           0           0
       pe            540           0           0           0           0
       alt             0           0           0           0           0
       pow             0           0           0           0           0

                      pe         alt         pow
       Vt              0    3.81e-05      0.3717
       alpha           0   1.774e-06   -2.37e-05
       beta            0           0           0
       phi             0           0           0
       theta           0           0           0
       psi             0           0           0
       p               0           0           0
       q               0  -5.338e-15           0
       r               0           0           0
       pn              0           0           0
       pe              0           0           0
       alt             0           0           0
       pow             0           0          -1

      B =
               Throttle   Elevator    Aileron     Rudder
       Vt             0     0.1848          0          0
       alpha          0  -0.002085          0          0
       beta           0          0  0.0002863  0.0007816
       phi            0          0          0          0
       theta          0          0          0          0
       psi            0          0          0          0
       p              0          0     -0.765     0.1376
       q              0    -0.1833          0          0
       r              0          0   -0.03332   -0.06472
       pn             0          0          0          0
       pe             0          0          0          0
       alt            0          0          0          0
       pow        64.94          0          0          0

      C =
                      Vt       alpha        beta         phi       theta
       Az       0.003698        16.9  -5.296e-08           0           0
       q               0           0           0           0           0
       alpha           0           1           0           0           0
       theta           0           0           0           0           1
       Vt              1           0           0           0           0
       Ay              0           0       -5.24           0           0
       p               0           0           0           0           0
       r               0           0           0           0           0
       beta            0           0           1           0           0
       phi             0           0           0           1           0

                     psi           p           q           r          pn
       Az              0    -4.1e-08      0.9454   -0.001336           0
       q               0           0           1           0           0
       alpha           0           0           0           0           0
       theta           0           0           0           0           0
       Vt              0           0           0           0           0
       Ay              0   -0.008953           0      0.1154           0
       p               0           1           0           0           0
       r               0           0           0           1           0
       beta            0           0           0           0           0
       phi             0           0           0           0           0

                      pe         alt         pow
       Az              0  -2.979e-05           0
       q               0           0           0
       alpha           0           0           0
       theta           0           0           0
       Vt              0           0           0
       Ay              0           0           0
       p               0           0           0
       r               0           0           0
       beta            0           0           0
       phi             0           0           0

      D =
              Throttle  Elevator   Aileron    Rudder
       Az            0  -0.05062         0         0
       q             0         0         0         0
       alpha         0         0         0         0
       theta         0         0         0         0
       Vt            0         0         0         0
       Ay            0         0  0.004801   0.01311
       p             0         0         0         0
       r             0         0         0         0
       beta          0         0         0         0
       phi           0         0         0         0

    Name: Linearized F-16 SS Model
    Continuous-time state-space model.

    Decoupled LQR Controller Gains =
                         alpha            q         Nz_i         beta            p
         elevator   -156.88015    -31.03701    -38.72983            0            0
          aileron            0            0            0     38.02751     -5.65497
           rudder            0            0            0     17.56400      1.58391

                             r         ps_i       Ny_r_i
         elevator            0            0            0
          aileron    -14.08804    -34.06416     -9.95406
           rudder    -41.43509      6.29550    -53.86016

    ------------------------------------------------------------
    Running Nonlinear Simulation in ODE45

    Simulation Complete
    Time to simulate (MM:SS.ms):
    00:01.820

    Begin back-calculation of controls
    Back-Calculation of Controls Complete
    Time to calculate (MM:SS.ms):
    00:10.504

    Begin struct generation & pass/fail analysis
    Pass/Fail Analysis Complete
    Time to calculate (MM:SS.ms):
    00:00.024

    ----------------------------------------------------------
    RESULTS:
                  Min & Max Values:
    Min Altitude:             96.178   ft
    Altitude Lost:          3403.822   ft
    Min Airspeed:            540.000   ft/sec
    Max Airspeed:            639.104   ft/sec
    Max Down Force:            5.222   g's
    Min Down Force:           -0.426   g's
    Max Side Force:            0.061   g's
    Max Roll Accel:          233.522   deg/sec^2

                  Maneuver Event Times:
    Maneuver Start:            2.000   sec
    Checkpoint  1:             3.395   sec
    Maneuver Complete:         8.000   sec
    Final Time:               15.000   sec
    (if Checkpoint 2 < Checkpoint 1, it is due to ODE45
    stepping backwards in time)

                  F-16 Attitude at Start:
    Roll   (phi):             45.000   deg
    Pitch  (theta):          -72.000   deg
    Yaw    (psi):            -45.000   deg

                  F-16 Attitude at End:
    Roll   (phi):             -0.000   deg
    Pitch  (theta):            2.249   deg
    Yaw    (psi):            -33.514   deg

                  Pass Fail Conditions:
               stable: 1
             airspeed: 1
                alpha: 1
                 beta: 1
                   Nz: 1
        psMaxAccelDeg: 1
             altitude: 1
         maneuverTime: 1

    ----------------------------------------------------------
    Begin plotting
    Plot Generation Complete
    Time to calculate (MM:SS.ms):
    00:03.280

## Save results

Save output to workspace

    save('SimResults.mat','output','passFail');
    disp('Script Complete');

    Script Complete