function [aircraft, state] = astC182()
%ASTC182 Fixed-wing object representation of the C182 aircraft
%
% [AIRCRAFT, STATE] = astC182()
%
% returns the fixed-wing object, AIRCRAFT, and fixed-wing cruise state, STATE,
% of a C182 aircraft.
% The Cessna C182 aircraft is a single engine, high-wing, standard configuration aircraft
% modeled using 6 degrees of freedom.
%
% Examples:
%   Create a fixed-wing aircraft object and fixed-wing state object
%   using the Cessna C182 aircraft model
%     [aircraft, state] = astC182()

% References
%
% [1] Roskam, J., "Airplane Flight Dynamics and Automatic Flight Controls (Part 1)", DAR Corporation, 2003.

% Copyright 2020-2021 The MathWorks, Inc.

aileron = Aero.FixedWing.Surface(1, ...
    "MaximumValue",deg2rad(20),"MinimumValue", deg2rad(-20),...
    "Properties", Aero.Aircraft.Properties(1, "Name", "Aileron"), ...
    "Controllable", true,...
    "Coefficients", Aero.FixedWing.Coefficient("StateVariables", "Aileron", "ReferenceFrame", "Stability"));

elevator = Aero.FixedWing.Surface(1, ...
    "MaximumValue",deg2rad(20),"MinimumValue", deg2rad(-20),...
    "Properties", Aero.Aircraft.Properties(1, "Name", "Elevator"), ...
    "Controllable", true,...
    "Coefficients", Aero.FixedWing.Coefficient("StateVariables", "Elevator", "ReferenceFrame", "Stability"));

rudder = Aero.FixedWing.Surface(1, ...
    "MaximumValue",deg2rad(20),"MinimumValue", deg2rad(-20),...
    "Properties", Aero.Aircraft.Properties(1, "Name", "Rudder"), ...
    "Controllable", true,...
    "Coefficients", Aero.FixedWing.Coefficient("StateVariables", "Rudder", "ReferenceFrame", "Stability"));

wing = Aero.FixedWing.Surface(1, "Surfaces", aileron, ...
    "Properties", Aero.Aircraft.Properties(1, "Name", "Wing"));

horizontalStabilizer = Aero.FixedWing.Surface(1, "Surfaces", elevator, ...
    "Properties", Aero.Aircraft.Properties(1, "Name", "HorizontalStabilizer"));

verticalStabilizer = Aero.FixedWing.Surface(1, "Surfaces", rudder, ...
    "Properties", Aero.Aircraft.Properties(1, "Name", "VerticalStabilizer"));

propeller = Aero.FixedWing.Thrust(1, "Properties", ...
    Aero.Aircraft.Properties(1, "Name","Propeller"), ...
    "Coefficients", Aero.FixedWing.Coefficient(...
    "StateVariables", ["Propeller", "U"], "ReferenceFrame", "Body"));

C182Properties = Aero.Aircraft.Properties(1, ...
    "Name", "Cessna C182", ...
    "Type", "General Aviation", ...
    "Version","1.0", ...
    "Description","Cessna 182 demo");

aircraft = Aero.FixedWing(1, ...
    "Properties",C182Properties, ...
    "UnitSystem","English (ft/s)", ...
    "AngleSystem","Radians", ...
    "TemperatureSystem","Fahrenheit", ...
    "ReferenceArea", 174, ...
    "ReferenceSpan", 36, ...
    "ReferenceLength", 4.9, ...
    "Surfaces",[wing, horizontalStabilizer, verticalStabilizer], ...
    "Thrusts",propeller);
aircraft.Coefficients.ReferenceFrame = "Stability";

aircraft = aircraft.update();

BodyCoefficients = {
    'CD', 'Zero', 0.027;
    'CL', 'Zero', 0.307;
    'Cm', 'Zero', 0.04;
    'CD', 'Alpha', 0.121;
    'CL', 'Alpha', 4.41;
    'Cm', 'Alpha', -0.613;
    'CD', 'AlphaDot', 0
    'CL', 'AlphaDot',  1.7;
    'Cm', 'AlphaDot', -7.27;
    'CD', 'Q', 0;
    'CL', 'Q', 3.9;
    'Cm', 'Q', -12.4;
    'CY', 'Beta', -0.393;
    'Cl', 'Beta', -0.0923;
    'Cn', 'Beta', 0.0587;
    'CY', 'P', -0.075;
    'Cl', 'P', -0.484;
    'Cn', 'P', -0.0278;
    'CY', 'R', 0.214;
    'Cl', 'R', 0.0798;
    'Cn', 'R', -0.0937;
    };
AileronCoefficients = {
    'CY', 'Aileron', 0;
    'Cl', 'Aileron', 0.229;
    'Cn', 'Aileron', -0.0504;
    };
RudderCoefficients = {
    'CY', 'Rudder', 0.187;
    'Cl', 'Rudder', 0.096;
    'Cn', 'Rudder', -0.0657;
    };
ElevatorCoefficients = {
    'CD', 'Elevator', 0;
    'CL', 'Elevator', 0.43;
    'Cm', 'Elevator', -1.369;
    };
PropellerCoefficients = {
    'CX', 'Propeller', 0.032;
    'CX', 'U'        , -0.096
    };

aircraft = setCoefficient(aircraft, BodyCoefficients(:, 1), BodyCoefficients(:, 2), BodyCoefficients(:, 3));
aircraft = setCoefficient(aircraft, AileronCoefficients(:, 1), AileronCoefficients(:, 2), AileronCoefficients(:, 3), "Component", "Aileron");
aircraft = setCoefficient(aircraft, ElevatorCoefficients(:, 1), ElevatorCoefficients(:, 2), ElevatorCoefficients(:, 3), "Component", "Elevator");
aircraft = setCoefficient(aircraft, RudderCoefficients(:, 1), RudderCoefficients(:, 2), RudderCoefficients(:, 3), "Component", "Rudder");
aircraft = setCoefficient(aircraft, PropellerCoefficients(:, 1), PropellerCoefficients(:, 2), PropellerCoefficients(:, 3), "Component", "Propeller");

state = CruiseState(aircraft);
state = setupControlStates(state, aircraft);
state = setState(state, ["Elevator", "Propeller"],[-0.00665, 0.75]);
end

function state = CruiseState(aircraft)
%CRUISESTATE Cruise state for the C182 fixed-wing aircraft

state = Aero.FixedWing.State(1, ...
    "UnitSystem","English (ft/s)",...
    "AngleSystem","Radians", ...
    "TemperatureSystem","Fahrenheit", ...
    "Mass",82.2981, ...
    "U", 220.1, ...
    "AltitudeMSL",5000, ...
    "Alpha",-0.00729, ...
    "Theta",-0.00729);

state.Environment = aircraftEnvironment(aircraft,"ISA",state.AltitudeMSL);

state.Inertia.Variables = [
    948, 0,    0   ;
    0  , 1346, 0   ;
    0  , 0   , 1967;
    ];

state.CenterOfGravity = [0.264, 0 , 0] .* 4.9;
state.CenterOfPressure = [0.25, 0, 0] .* 4.9;

end
