classdef fixedwingMissionParser < handle
    %fixedwingMissionParser Generate trajectory for fixed-wing UAV from mission
    %
    %   fixedwingMissionParser parses mission defined in uavMission object
    %   and generates a flight trajectory as a fixedwingFlightTrajectory
    %   object.
    %
    %   P = fixedwingMissionParser creates a fixed-wing UAV mission parser
    %   P.
    %
    %   P = fixedwingMissionParser(Name=Value) specifies properties using
    %   one or more name-value arguments.
    %      TransitionRadius     - Transition radius in meters
    %                             Transition radius used to compute
    %                             trajectory waypoints based on mission
    %                             definition, specified as a positive
    %                             scalar, in meters.
    %
    %      TakeoffPitch         - Takeoff pitch angle in degrees
    %                             Takeoff pitch angle between the flight
    %                             path of the UAV and the horizontal plane
    %                             during takeoff action, specified as a
    %                             positive scalar, in degrees.
    %
    %   fixedwingMissionParser properties:
    %      TransitionRadius     - Transition radius in meters
    %      TakeoffPitch         - Takeoff pitch angle in degrees
    %
    %   fixedwingMissionParser methods:
    %      parse        - Generate reference trajectory in local NED frame
    %      copy         - Copy mission parser
    %
    %   Example:
    %
    %      % Create mission
    %      mission = uavMission(PlanFile="flight.plan");
    %
    %      % Create parser
    %      parser = fixedwingMissionParser;
    %
    %      % Generate flight trajectory
    %      traj = parse(parser, mission);
    %
    %      % Visualize
    %      figure()
    %      show(mission);
    %      hold on;
    %      show(traj);
    %
    %   See also uavMission, fixedwingFlightTrajectory

    %   Copyright 2022 The MathWorks, Inc.

    properties (Dependent)
        %TransitionRadius Transition radius in meters
        %    Transition radius used to compute trajectory waypoints based
        %    on mission definition, specified as a positive scalar, in
        %    meters. 
        %
        %    Default: 10
        TransitionRadius

        %TakeoffPitch Takeoff pitch angle in degrees
        %   Takeoff pitch angle between the flight path of the UAV and the
        %   horizontal plane during takeoff action, specified as a positive
        %   scalar, in degrees.
        %
        %   Default: 5
        TakeoffPitch
    end

    properties (Access=private)
        %FixedWingBehavior Internal implementation of fixed-wing behavior
        FixedWingBehavior
    end
    
    methods
        function obj = fixedwingMissionParser(varargin)
            %fixedwingMissionParser Constructor

            parser = inputParser;
            parser.addParameter("TransitionRadius", 10, ...
                @(x)validateattributes(x, "numeric", ["scalar", "positive", "finite"], "fixedwingMissionParser", "TransitionRadius"));
            parser.addParameter("TakeoffPitch", 5, ...
                @(x)validateattributes(x, "numeric", ["scalar", "positive", "finite"], "fixedwingMissionParser", "TakeoffPitch"));
            parse(parser, varargin{:});

            obj.FixedWingBehavior = uav.internal.mission.behavior.FixedWingWaypointBehavior;
            obj.FixedWingBehavior.TransitionRadius = double(parser.Results.TransitionRadius);
            obj.FixedWingBehavior.TakeoffPitch = double(parser.Results.TakeoffPitch);
        end

        function out = get.TransitionRadius(obj)
            %getter for TransitionRadius
            out = obj.FixedWingBehavior.TransitionRadius;
        end

        function set.TransitionRadius(obj, x)
            %setter for TransitionRadius
            validateattributes(x, "numeric", ["scalar", "positive", "finite"], "fixedwingMissionParser", "TransitionRadius");
            obj.FixedWingBehavior.TransitionRadius(:) = x;
        end

        function out = get.TakeoffPitch(obj)
            %getter for TakeoffPitch
            out = obj.FixedWingBehavior.TakeoffPitch;
        end

        function set.TakeoffPitch(obj, x)
            %setter for TakeoffPitch
            validateattributes(x, "numeric", ["scalar", "positive", "finite"], "fixedwingMissionParser", "TakeoffPitch");
            obj.FixedWingBehavior.TakeoffPitch(:) = x;
        end

        function cobj = copy(obj)
            %copy Copy mission parser
            %   COBJ = copy(OBJ) creates a deep copy of the
            %   fixedwingMissionParser object with the same properties.
            
            cobj = fixedwingMissionParser;
            cobj.TransitionRadius = obj.TransitionRadius;
            cobj.TakeoffPitch = obj.TakeoffPitch;
        end
        
        function traj = parse(obj, mission, varargin)
            %parse Generate reference trajectory in local NED frame
            %    TRAJ = parse(OBJ,MISSION) generates reference trajectory
            %    TRAJ for a fixed-wing UAV following UAV mission MISSION as
            %    a fixedwingFlightTrajectory object. The trajectory is
            %    defined in a local NED frame with the origin defined with
            %    the same global coordinates as the HomeLocation of
            %    MISSION.
            %
            %    TRAJ = parse(OBJ,MISSION,REFERENCELOCATION) generates the
            %    reference trajectory in a local NED frame, with the origin
            %    defined by global coordinates REFERENCELOCATION.
            %    REFERENCELOCATION is a 1x3 vector [latitude longitude
            %    altitude]. Latitude and longitude are in degrees. Altitude
            %    is height in meters above WGS84 ellipsoid.

            narginchk(2,3);
            trajImpl = obj.FixedWingBehavior.parse(mission.MissionData, varargin{:});
            traj = fixedwingFlightTrajectory([0 0 0; 1 0 0],zeros(2,3),[0 1]);
            traj.loadData(trajImpl);

        end
    end
end

