classdef fixedwing < robotics.core.internal.NavigationModel
    %FIXEDWING Guidance model for fixed-wing UAVs
    %   The FIXEDWING class represents a reduced-order
    %   guidance model for a UAV (unmanned aerial vehicle). The model
    %   approximates the behavior of a closed-loop system consisting of an
    %   autopilot controller and a fixed-wing kinematic model for 3-D
    %   motion.
    %
    %   model = FIXEDWING creates a fixed-wing
    %   motion model with double precision values used in
    %   input, output, and configuration parameters.
    %
    %   model = FIXEDWING(DATATYPE) creates a
    %   fixed-wing motion model, with input, output, and configuration
    %   parameters typed according to DATATYPE. Supported data type
    %   includes "double" and "single".
    %
    %   FIXEDWING properties:
    %      Configuration - Fixed wing autopilot control gains and attitude
    %                      limits in radians
    %      DataType      - Interface and configuration value data type
    %      ModelType     - Motion model definition type
    %      Name          - Motion model name
    %
    %   FIXEDWING methods:
    %      control       - Control commands for UAV
    %      copy          - Copy UAV guidance model
    %      derivative    - Compute time derivative of the guidance model
    %                      states
    %      environment   - Environment inputs for the guidance model
    %      state         - UAV Guidance model states
    %
    %   Example:
    %
    %      % Construct a fixed-wing motion model with double precision
    %      % interface and computation type.
    %      model = fixedwing;
    %
    %      % Set state air speed.
    %      s = state(model);
    %      s(4) = 10;
    %
    %      % Set control command for air speed and roll angle.
    %      u = control(model);
    %      u.RollAngle = pi/12;
    %      u.AirSpeed = 10;
    %
    %      % Create default environment without wind.
    %      e = environment(model);
    %
    %      % Compute time derivative of the state given the current state,
    %      % control, and environment.
    %      sdot = derivative(model, s, u, e);
    %
    %      % Perform simulation using ODE45 integration.
    %      simOut = ode45(@(~,x)derivative(model,x,u,e), [0 50], s);

    %   Copyright 2018-2020 The MathWorks, Inc.

    %#codegen

    methods
        function obj = fixedwing(varargin)
            %fixedwing Construct fixed-wing guidance model
            
            obj@robotics.core.internal.NavigationModel('FixedWingGuidance', varargin{:});
        end
    end

    methods
        function stateDerivative = derivative(model, stateVector, controlStruct, environmentStruct)
            %DERIVATIVE Compute time derivative of the guidance model states
            %
            %   STATEDERIVATIVE = DERIVATIVE(MODEL, STATEVECTOR,
            %   CONTROLSTRUCT, ENVIRONMENTSTRUCT). The STATEVECTOR,
            %   CONTROLSTRUCT, and ENVIRONMENTSTRUCT inputs are generated
            %   by calling the functions: state(MODEL), control(MODEL), and
            %   environment(MODEL), respectively. These inputs are used to
            %   calculate the time derivative of the UAV states.

            stateDerivative = derivative@robotics.core.internal.NavigationModel(model, stateVector, controlStruct, environmentStruct);
        end

        function u = control(model)
            %CONTROL Control commands for UAV
            %
            %   U = CONTROL(MODEL) returns a struct with the following
            %   fields: Height, AirSpeed, and RollAngle. Each is a scalar
            %   of same type as MODEL.DataType.
            u = control@robotics.core.internal.NavigationModel(model);
        end

        function s = state(model)
            %STATE fixed-wing UAV states
            %
            %   S = STATE(MODEL) returns a 8-element vector of a UAV state:
            %      - scalar for position in north direction
            %      - scalar for position in east direction
            %      - scalar for flight height
            %      - scalar for UAV air speed
            %      - scalar for UAV heading angle
            %      - scalar for UAV flight path angle
            %      - scalar for UAV roll angle
            %      - scalar for UAV roll angle rate
            %   Default: zeros(8,1,model.DataType)
            
            s = state@robotics.core.internal.NavigationModel(model);
        end

        function e = environment(model)
            %ENVIRONMENT Environment inputs for fixed-wing UAV
            %
            %   E = ENVIRONMENT(MODEL) returns a struct with the following
            %   fields: WindNorth, WindEast, WindDown and Gravity. Each is
            %   a scalar of same type as MODEL.DataType.
            e = environment@robotics.core.internal.NavigationModel(model);
        end
        
        function copyObj = copy(model)
            %COPY Create deep copy of model
            %
            %   COPYOBJ = COPY(MODEL) returns a deep copy of the model
            
            copyObj = copy@robotics.core.internal.NavigationModel(model);
        end
    end
    
    methods (Access = protected)
        function model = getModelWithDefaultConfiguration(obj)
            %getModelWithDefaultConfiguration create a fixed-wing model of
            %the same data type as input
            
            model = fixedwing(obj.DataType);
        end
    end
end

