<?xml version="1.0" encoding="UTF-8"?>
<Mission xmlns="Tethys"
       xmlns:Control="Tethys/Control"
       xmlns:Derivation="Tethys/Derivation"
       xmlns:Estimation="Tethys/Estimation"
       xmlns:Guidance="Tethys/Guidance"
       xmlns:Sensor="Tethys/Sensor"
       xmlns:Units="Tethys/Units"
       xmlns:Universal="Tethys/Universal"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="Tethys http://okeanids.mbari.org/tethys/Xml/Tethys.xsd
                           Tethys/Control http://okeanids.mbari.org/tethys/Xml/Control.xsd
                           Tethys/Derivation http://okeanids.mbari.org/tethys/Xml/Derivation.xsd
                           Tethys/Estimation http://okeanids.mbari.org/tethys/Xml/Estimation.xsd
                           Tethys/Guidance http://okeanids.mbari.org/tethys/Xml/Guidance.xsd
                           Tethys/Sensor http://okeanids.mbari.org/tethys/Xml/Sensor.xsd
                           Tethys/Units http://okeanids.mbari.org/tethys/Xml/Units.xsd
                           Tethys/Universal http://okeanids.mbari.org/tethys/Xml/Universal.xsd"
       Id="profile_station_backseat">

    <Description>
        This mission yoyos in a circle around a specified location.
    </Description>

<!-- Mission-wide settings -->

    <DefineArg Name="MissionTimeout"><Description>
        Maximum duration of mission
    </Description><Units:hour/><Value>4</Value></DefineArg>

    <DefineArg Name="NeedCommsTime"><Description>
        Elapsed time after previous surface communications when vehicle will
        begin to ascend for additional surface communications
    </Description><Units:minute/><Value>60</Value></DefineArg>

<!-- You probably need to change these. -->

    <DefineArg Name="Lat"><Description>
        The latitude of the center of the circle.
    </Description><Units:degree/><Value>36.806966</Value></DefineArg>

    <DefineArg Name="Lon"><Description>
        The longitude of the center of the circle.
    </Description><Units:degree/><Value>-121.824326</Value></DefineArg>

    <DefineArg Name="Radius"><Description>
        Radius to circle at
    </Description><Units:meter/><Value>300</Value></DefineArg>

<!-- You probably do not need to change these. -->

    <DefineArg Name="YoYoMinDepth"><Description>
        Minimum depth while performing the YoYo behavior.
    </Description><Units:meter/><Value>2</Value></DefineArg>

    <DefineArg Name="YoYoMaxDepth"><Description>
        Maximum depth while performing the YoYo behavior.
    </Description><Units:meter/><Value>20</Value></DefineArg>

    <DefineArg Name="YoYoMinAltitude"><Description>
        Minimum altitude while performing the YoYo behavior (for
        bottom-terminated YoYos).
    </Description><Units:meter/><Value>7</Value></DefineArg>

    <DefineArg Name="YoYoPitch"><Description>
        Pitch (plus and minus) for yo-yo behavior.
    </Description><Units:degree/><Value>20</Value></DefineArg>

    <DefineArg Name="YoYoDepthRate"><Description>
        Depth rate (plus and minus) for yo-yo behavior.
        Overides YoYoPitch when value is set.
    </Description><Units:meter_per_second/><Value>NaN</Value></DefineArg>

    <DefineArg Name="Speed"><Description>
        Vehicle speed.
    </Description><Units:meter_per_second/><Value>1</Value></DefineArg>

<!-- You are even less likely to need to change these. -->

    <DefineArg Name="CircleMaxError"><Description>
        If this distance away from the circle, drive straight towards (or away
        from the center). Otherwise, try to reduce distance from the ideal
        circle.
    </Description><Units:meter/><Value>100</Value></DefineArg>

    <DefineArg Name="CircleTurnToPort"><Description>
        If true, vehicle turns to the left around the center point. If false,
        vehicle turns to the right.
    </Description><False/></DefineArg>

    <DefineArg Name="KwpHeading"><Description>
        Used to relax waypoint cross-track error constant that is adjusted for
        docking. (You can override this setting by passing an argument.)
    </Description><Units:radian_per_meter/><Value>0.010</Value></DefineArg>

    <DefineArg Name="MinAltitude"><Description>
        Minimum height above the sea floor for the entire mission.
    </Description><Units:meter/><Value>5</Value></DefineArg>

    <DefineArg Name="MaxDepth"><Description>
        Maximum depth for the entire mission.
    </Description><Units:meter/><Value>205</Value></DefineArg>

    <DefineArg Name="MinOffshore"><Description>
        Minimum offshore distance for the entire mission.
    </Description><Units:kilometer/><Value>2</Value></DefineArg>

<!-- Missions should almost always start with a timeout -->

    <Timeout Duration="MissionTimeout"/>

    <Insert Filename="Insert/Science.xml">
        <Description>
            Most missions will run the science sensors. If you don't place this
            aggregate above NeedComms, science instruments get turned off on the
            last upcast and while floating on the surface.
        </Description>
        <RedefineArg Name="PeakDetectChlActive"><True/></RedefineArg>
    </Insert>

<!-- Most missions should use the NeedComms aggregate. -->

    <Insert Filename="Insert/NeedComms.xml" Id="NeedComms"/>

    <Assign><Sequence/><Arg Name="NeedComms:DiveInterval"/><Arg Name="NeedCommsTime"/></Assign>

<!-- Missions should almost always start with standard safety envelopes; most missions should not expose the parameters of these envelopes. -->

    <Insert Filename="Insert/StandardEnvelopes.xml"/>

    <Assign><Sequence/><Arg Name="StandardEnvelopes:MinAltitude"/><Arg Name="MinAltitude"/></Assign>

    <Assign><Sequence/><Arg Name="StandardEnvelopes:MaxDepth"/><Arg Name="MaxDepth"/></Assign>

    <Assign><Sequence/><Arg Name="StandardEnvelopes:MinOffshore"/><Arg Name="MinOffshore"/></Assign>

    <Aggregate Id="UpdateCenter">

        <When>
            <Elapsed><CustomUri Uri="_.Lat"/></Elapsed>
            <Lt><Elapsed><Arg Name="Lat"/></Elapsed></Lt>
            <Or><Elapsed><CustomUri Uri="_.Lon"/></Elapsed><Lt><Elapsed><Arg Name="Lon"/></Elapsed></Lt></Or>
        </When>

        <Assign><Sequence/><Arg Name="Lat"/><CustomUri Uri="_.Lat"/></Assign>

        <Assign><Sequence/><Arg Name="Lon"/><CustomUri Uri="_.Lon"/></Assign>

<!--        <Syslog>Upadating circle _.Lat/_.Lon <CustomUri Uri="_.Lat"/><Units:arcdeg/>/<CustomUri Uri="_.Lon"/><Units:arcdeg/></Syslog>-->

    </Aggregate>

<!-- Check for additional instructions first. -->

    <Call Id="StartingMission" RefId="NeedComms"/>

<!-- Many missions will keep mass position and buoyancy volume fixed at defaults. -->

    <Syslog>Powering up backseat...</Syslog>

    <Guidance:BackseatDriver>
        <Parallel/>
    </Guidance:BackseatDriver>

    <Syslog>Backseat powered.</Syslog>

    <Aggregate Id="Profile">

        <Sequence/>

        <Guidance:Pitch>
            <Parallel/>
            <Setting><Guidance:Pitch.massPosition/><Control:VerticalControl.massDefault/></Setting>
        </Guidance:Pitch>

        <Guidance:Buoyancy>
            <Parallel/>
            <Setting><Guidance:Buoyancy.position/><Control:VerticalControl.buoyancyNeutral/></Setting>
        </Guidance:Buoyancy>

        <Guidance:SetSpeed>
            <Parallel/>
            <Setting><Guidance:SetSpeed.speed/><Arg Name="Speed"/></Setting>
        </Guidance:SetSpeed>

        <Guidance:DepthEnvelope>
            <Description>
                Another depth envelope for the YoYo behavior. This envelope
                should fall within the limits of the standard safety envelopes
                in Insert/StandardEnvelopes.xml in order to avoid commanding
                high pitch angles for depth-terminated YoYos.
            </Description>
            <Parallel/>
            <Setting><Guidance:DepthEnvelope.minDepth/><Arg Name="YoYoMinDepth"/></Setting>
            <Setting><Guidance:DepthEnvelope.maxDepth/><Arg Name="YoYoMaxDepth"/></Setting>
            <Setting><Guidance:DepthEnvelope.pitch/><Arg Name="YoYoPitch"/></Setting>
        </Guidance:DepthEnvelope>

        <Guidance:AltitudeEnvelope>
            <Description>
                Another altitude envelope for the YoYo behavior. This envelope
                should fall within the limits of the standard safety envelopes
                in Insert/StandardEnvelopes.xml in order to avoid commanding
                high pitch angles for bottom-terminated YoYos.
            </Description>
            <Parallel/>
            <Setting><Guidance:AltitudeEnvelope.minAltitude/><Arg Name="YoYoMinAltitude"/></Setting>
            <Setting><Guidance:AltitudeEnvelope.pitch/><Arg Name="YoYoPitch"/></Setting>
        </Guidance:AltitudeEnvelope>

        <Aggregate Id="YoYoOnPitch">
            <Description>
                Set YoYo up/down pitch angle when YoYo depth-rate is not specified.
            </Description>
            <While>
                <IsNaN><Arg Name="YoYoDepthRate"/></IsNaN>
            </While>

            <Guidance:YoYo>
                <Parallel/>
                <Setting><Guidance:YoYo.pitch/><Arg Name="YoYoPitch"/></Setting>
            </Guidance:YoYo>

        </Aggregate>

        <Aggregate Id="YoYoOnDepthRate">
            <Description>
                Set YoYo up/down depth-rate when specified.
            </Description>

            <While>
                <Not><IsNaN><Arg Name="YoYoDepthRate"/></IsNaN></Not>
            </While>

            <Guidance:YoYo>
                <Parallel/>
                <Setting><Guidance:YoYo.depthRate/><Arg Name="YoYoDepthRate"/></Setting>
            </Guidance:YoYo>

        </Aggregate>

        <Aggregate Id="CircleWrapper">

            <Sequence Repeat="4096"/>

            <Assign><Parallel/><Control:HorizontalControl.kwpHeading/><Arg Name="KwpHeading"/></Assign>

            <Guidance:Circle>
                <Sequence/>
                <Setting><Guidance:Circle.latitude/><Arg Name="Lat"/></Setting>
                <Setting><Guidance:Circle.longitude/><Arg Name="Lon"/></Setting>
                <Setting><Guidance:Circle.radius/><Arg Name="Radius"/></Setting>
                <Setting><Guidance:Circle.maxError/><Arg Name="CircleMaxError"/></Setting>
                <Setting><Guidance:Circle.turnToPort/><Arg Name="CircleTurnToPort"/></Setting>
            </Guidance:Circle>

        </Aggregate>

    </Aggregate>

</Mission>
