<?xml version="1.0" encoding="UTF-8"?>
<Mission xmlns="Tethys"
       xmlns:Control="Tethys/Control"
       xmlns:Guidance="Tethys/Guidance"
       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/Guidance http://okeanids.mbari.org/tethys/Xml/Guidance.xsd
                           Tethys/Units http://okeanids.mbari.org/tethys/Xml/Units.xsd
                           Tethys/Universal http://okeanids.mbari.org/tethys/Xml/Universal.xsd"
       Id="front_tracking_2D">

    <Description>
        Vehicle nominally yo-yo's zigzagging across an upwelling front, with
        science turned on. Each time a front edge is detected, the vehicle turns
        around and samples across the front again. Front edge is defined by
        difference in temperature between several depths.
    </Description>

<!-- Almost every mission should start with an overall timeout and a NeedCommsTime. -->

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

    <DefineArg Name="NeedCommsTime"><Description>
        How often to surface for commumications
    </Description><Units:minute/><Value>60</Value></DefineArg>

    <DefineArg Name="TimePastFront"><Description>
        Time to swim past front when tracking.
    </Description><Units:minute/><Value>40</Value></DefineArg>

<!--debug    
    </Description><Units:minute/><Value>5</Value></DefineArg>
-->

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

    <DefineArg Name="MassDefault"><Control:VerticalControl.massDefault/></DefineArg>

    <DefineArg Name="BuoyancyNeutral"><Description>
        Buoyancy bladder position while performing the YoYo behavior. Defaults
        to buoyancyNeutral setting in the Config/Control.cfg file. Set to NaN cc
        for active buoyancy.
    </Description><Control:VerticalControl.buoyancyNeutral/></DefineArg>

    <DefineArg Name="Speed"><Description>
        Speed while performing the YoYo behavior.
    </Description><Units:meter_per_second/><Value>1</Value></DefineArg>

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

    <DefineArg Name="YoYoMaxDepth"><Description>
        Maximum depth while performing the YoYo behavior.
    </Description><Units:meter/><Value>50</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="YoYoUpPitch"><Description>
        Vehicle up-pitch while performing the YoYo behavior.
    </Description><Units:degree/><Value>20</Value></DefineArg>

    <DefineArg Name="YoYoDownPitch"><Description>
        Vehicle down-pitch while performing the YoYo behavior.
    </Description><Units:degree/><Value>-20</Value></DefineArg>

    <DefineArg Name="HeadingNorth"><Units:degree/><Value>0.0</Value></DefineArg>

    <DefineArg Name="HeadingSouth"><Units:degree/><Value>180.0</Value></DefineArg>

    <DefineArg Name="HeadingEast"><Units:degree/><Value>90.0</Value></DefineArg>

    <DefineArg Name="HeadingWest"><Units:degree/><Value>270.0</Value></DefineArg>

    <DefineArg Name="HeadingNortheast"><Units:degree/><Value>45.0</Value></DefineArg>

    <DefineArg Name="HeadingNorthwest"><Units:degree/><Value>315.0</Value></DefineArg>

    <DefineArg Name="HeadingSoutheast"><Units:degree/><Value>135.0</Value></DefineArg>

    <DefineArg Name="HeadingSouthwest"><Units:degree/><Value>225.0</Value></DefineArg>

    <DefineArg Name="SearchHeading"><Description>
        Initialized to the starting heading (default 270.0). Will be updated
        when detecting front or hitting wall.
    </Description><Units:degree/><Value>270.0</Value></DefineArg>

    <DefineArg Name="ShapeZ"><Description>
        Is the front-tracking in Z shape on north-south sweeps (versus reverse-Z
        shape)? Initialized to True.
    </Description><True/></DefineArg>

    <DefineArg Name="ShapeN"><Description>
        Is the front-tracking in N shape on east-west sweeps (versus reverse-N
        shape)? Initialized to False.
    </Description><True/></DefineArg>

    <DefineArg Name="FlagNorthSouthSweep"><Description>
        Initialized to True.
    </Description><True/></DefineArg>

<!--debug
    </Description><False/></DefineArg>
-->

    <DefineArg Name="FlagNorthwardSweep"><Description>
        Initialized to True.
    </Description><True/></DefineArg>

    <DefineArg Name="FlagEastwardSweep"><Description>
        Initialized to False.
    </Description><False/></DefineArg>

<!-- Tight walls for fast tests of wall bouncing. Don't delete this note.
LonEastWall: -121.82
LonWestWall: -121.84
LatNorthWall: 36.82
LatSouthWall: 36.80
-->

    <DefineArg Name="LonEastWall"><Units:degree/><Value>-121.82</Value></DefineArg>

<!--debug
    <DefineArg Name="LonEastWall"><Units:degree/><Value>-121.82</Value></DefineArg>
-->

    <DefineArg Name="LonWestWall"><Units:degree/><Value>-122.0</Value></DefineArg>

<!--debug
    <DefineArg Name="LonWestWall"><Units:degree/><Value>-121.84</Value></DefineArg>
-->

    <DefineArg Name="LatNorthWall"><Units:degree/><Value>36.9</Value></DefineArg>

<!--debug
    <DefineArg Name="LatNorthWall"><Units:degree/><Value>36.82</Value></DefineArg>    
-->

    <DefineArg Name="LatSouthWall"><Units:degree/><Value>36.80</Value></DefineArg>

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

    <DefineArg Name="MinAltitude"><Description>
        Minimum height above the sea floor for the entire mission.
    </Description><Units:meter/><Value>7</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>

<!-- These are internal variables that should not be changed. -->

<!-- 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>

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

    <Guidance:Pitch>
        <Parallel/>
        <Setting><Guidance:Pitch.massPosition/><Arg Name="MassDefault"/></Setting>
    </Guidance:Pitch>

    <Guidance:Buoyancy>
        <Parallel/>
        <Setting><Guidance:Buoyancy.position/><Arg Name="BuoyancyNeutral"/></Setting>
    </Guidance:Buoyancy>

<!-- Run at constant speed. -->

    <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.upPitch/><Arg Name="YoYoUpPitch"/></Setting>
        <Setting><Guidance:DepthEnvelope.downPitch/><Arg Name="YoYoDownPitch"/></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="YoYoUpPitch"/></Setting>
    </Guidance:AltitudeEnvelope>

    <Guidance:YoYo>
        <Parallel/>
        <Setting><Guidance:YoYo.upPitch/><Arg Name="YoYoUpPitch"/></Setting>
        <Setting><Guidance:YoYo.downPitch/><Arg Name="YoYoDownPitch"/></Setting>
    </Guidance:YoYo>

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

    <Aggregate Id="2DTrackFront">

        <Guidance:FrontTracking Id="tracking">
            <Sequence/>
            <Setting><Guidance:FrontTracking.searchHeading/><Arg Name="SearchHeading"/></Setting>
            <Setting><Guidance:FrontTracking.northHeading/><Arg Name="HeadingNorth"/></Setting>
            <Setting><Guidance:FrontTracking.southHeading/><Arg Name="HeadingSouth"/></Setting>
            <Setting><Guidance:FrontTracking.eastHeading/><Arg Name="HeadingEast"/></Setting>
            <Setting><Guidance:FrontTracking.westHeading/><Arg Name="HeadingWest"/></Setting>
            <Setting><Guidance:FrontTracking.neHeading/><Arg Name="HeadingNortheast"/></Setting>
            <Setting><Guidance:FrontTracking.nwHeading/><Arg Name="HeadingNorthwest"/></Setting>
            <Setting><Guidance:FrontTracking.neHeading/><Arg Name="HeadingNortheast"/></Setting>
            <Setting><Guidance:FrontTracking.swHeading/><Arg Name="HeadingSouthwest"/></Setting>
            <Setting><Guidance:FrontTracking.seHeading/><Arg Name="HeadingSoutheast"/></Setting>
            <Setting><Guidance:FrontTracking.lonEastWall/><Arg Name="LonEastWall"/></Setting>
            <Setting><Guidance:FrontTracking.lonWestWall/><Arg Name="LonWestWall"/></Setting>
            <Setting><Guidance:FrontTracking.latNorthWall/><Arg Name="LatNorthWall"/></Setting>
            <Setting><Guidance:FrontTracking.latSouthWall/><Arg Name="LatSouthWall"/></Setting>
            <Setting><Guidance:FrontTracking.flagNorthSouthSweep/><Arg Name="FlagNorthSouthSweep"/></Setting>
            <Setting><Guidance:FrontTracking.flagNorthwardSweep/><Arg Name="FlagNorthwardSweep"/></Setting>
            <Setting><Guidance:FrontTracking.flagEastwardSweep/><Arg Name="FlagEastwardSweep"/></Setting>
            <Setting><Guidance:FrontTracking.shapeZ/><Arg Name="ShapeZ"/></Setting>
            <Setting><Guidance:FrontTracking.shapeN/><Arg Name="ShapeN"/></Setting>
            <Setting><Guidance:FrontTracking.waitSeconds/><Arg Name="TimePastFront"/></Setting>
        </Guidance:FrontTracking>

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

    </Aggregate>

</Mission>
