% PredatorMainProgram.m is the first program you should run. It has two
% primary functions: 1. compute the vector of constants needed by the 
% Simulink model of the aircraft (FlatEarth); 2. trim the aircraft and establish 
% trim values of the controls and state variables for use by Simulink.
% This program must be executed before the Simulink model FlatEarth is run.
%
% *********************************************
% There are aircraft specific files. For the Predator aircraft these files are named.
%  PredatorMainProgram.m
%  PredatorMakeConstants.m
%  PredatorBasicConstants.m
% Each of these need to be customized for a new aircraft. Typically, you
% would rename these files for use in analyzing another aircraft, i.e., Boeing747MainProgram.m.
%
% PredatorMakeConstants.m has only one function. It is called by
% PredatorMainProgram.m to fill the array called constant required by
% the Simulink model FlatEarth.
%
% PredatorBasicConstants contains numerical values for the physical properties of
% the aircraft that are necessary to know in order to compute the
% vector called constant.
%
%**********************************************
% To use this code execute the scripts in the fillowing order.
%  First, execute the script PredatorMainProgram.m to get the Predator 
%  "constant" vector defined and to establish trim conditions.
%
%  Second, run the nonlinear simulation in SIMULINK using FlatEarth.mdl.
%
%  Third, run FlatEarthAnal.m to get linear models and to 
%     overplot linear and nonlinear time responses.


% Start
disp(' ')
disp('Start here')

% Step 1: Get the matrix of constants that define the aircraft.
PredatorMakeConstants  % Remember that this code clears MATLAB memory
%                    and can clear time histories that may have been previously 
%                    generated by SIMULINK.
disp(aircraft); % Display aircraft identification
disp(' The array called constant is now defined')
%
% Step 2: Trim the Predator
% Establish a trim condition (initial condition).
Vt=constant(58)% ft/sec (assumed to be airspeed)
Hp=constant(59)
% Steady winds
disp('NED components of the steady wind')
VwindKnots=0;
Vwx=-VwindKnots*1.687808; %ft/sec, - sign means headwind
Vwy=0
Vwz=0 
[xIC,uIC,CL,CD,CM,alphadeg]=QuickTrim2(Vt,Hp,constant,Vwx,Vwy,Vwz)
disp(' The trim conditions are now known')
flag=1;
t=0;
% Check to insure that the derivatives of the state vector (sys) are small 
% for this trim condition.
[sys,x0,str,ts] = aircraft7(t,xIC,uIC,flag,constant,xIC)
disp(' Most of the elements of sys, a.k.a. Xdots, should be "small" at trim')
% Now we are ready to run the nonlinear simulation using
% SIMULINK and FlatEarth.mdl.
%
disp(' You are now ready to run the FlatEarth Simulink model.')
disp(' Type simulink from the Matlab command window')
disp(' From the simulink file window, open the simulink model FlatEarth7.mdl')
disp(' Determine whather you want to generate an elevator step input')
disp(' aileron step, rudder step, or horsepower step')
disp(' Change the step function blocks called Elevator Perturbation,')
disp(' Aileron Perturbation, Rudder Perturbation, Horsepower Perturbation')
disp(' to reflect the particular step input you want to generate')
disp(' Click the Run Simulation button to run the simulation. You may want to')
disp(' open one of the scope windows to insure that you have generated the')
disp(' correct time history')
disp(' After running the Simulink simulation you can run the script called FlatEarthAnal.m')
