<?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="depthLimit">

    <DefineArg Name="Depth"><Description>
        Maximum depth of vehicle. Please set configuration for stop and abort
        depths appropriately!
    </Description><Units:meter /><Value>300</Value></DefineArg>

    <DefineArg Name="Wpt1Lat"><Units:degree /><Value>36.774</Value></DefineArg>

    <DefineArg Name="Wpt1Lon"><Units:degree /><Value>-121.977</Value></DefineArg>

    <DefineArg Name="Wpt2Lat"><Units:degree /><Value>36.78</Value></DefineArg>

    <DefineArg Name="Wpt2Lon"><Units:degree /><Value>-121.944</Value></DefineArg>

    <DefineArg Name="MinAltitude"><Units:meter/><Value>10</Value></DefineArg>

    <DefineArg Name="MaxDepth"><Units:meter/><Value>310</Value></DefineArg>

    <DefineArg Name="MaxDepthTransit"><Units:meter/><Value>30</Value></DefineArg>

    <DefineArg Name="MinOffshore"><Units:meter/><Value>3000</Value></DefineArg>

    <DefineArg Name="NeedCommsTime"><Units:minute/><Value>60</Value></DefineArg>

    <DefineArg Name="MaxDuration"><Units:hour/><Value>6</Value></DefineArg>

    <DefineArg Name="MassDefault"><Description>
        Static setting for mass during the transit portion of the mission.
    </Description><Control:VerticalControl.massDefault/></DefineArg>

    <DefineArg Name="BuoyancyNeutral"><Description>
        Buoyancy bladder position while driving to station. Defaults to
        buoyancyNeutral setting in the Config/Control.cfg file.
    </Description><Control:VerticalControl.buoyancyNeutral/></DefineArg>

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

    <DefineArg Name="ApproachDepth"><Description>
        Depth for initial approach to station.
    </Description><Units:meter/><Value>15.0</Value></DefineArg>

    <DefineArg Name="TimeoutDuration"><Description>
        Maximum length of mission. Not designed to include transit.
    </Description><Units:minute/><Value>135</Value></DefineArg>

    <Timeout Duration="TimeoutDuration"/>

    <Aggregate Id="AltEnvWrapper">

        <When>
            <Arg Name="MinAltitude"/>
            <Gt><Units:meter/><Value>0</Value></Gt>
        </When>

        <Guidance:AltitudeEnvelope>
            <Description>
                Safety behavior that keeps vehicle off sea floor
            </Description>
            <Parallel/>
            <Setting><Guidance:AltitudeEnvelope.minAltitude/><Arg Name="MinAltitude"/></Setting>
        </Guidance:AltitudeEnvelope>

    </Aggregate>

    <Guidance:DepthEnvelope>
        <Parallel/>
        <Setting><Guidance:DepthEnvelope.maxDepth /><Arg Name="MaxDepth"/></Setting>
    </Guidance:DepthEnvelope>

    <Aggregate Id="SURFACECOMMS">

        <Sequence/>

        <Guidance:GoToSurface>
            <Progression/>
        </Guidance:GoToSurface>

        <Aggregate>

            <Sequence/>

            <Preemptive>
                <True />
            </Preemptive>

            <ReadDatum>
                <Universal:latitude_fix />
            </ReadDatum>

            <ReadDatum>
                <Timeout Duration="P60M">
                    <Assign Id="TouchComms"><Sequence/><Universal:platform_communications/></Assign>
                </Timeout>
                <Universal:platform_communications />
            </ReadDatum>

            <ReadDatum>
                <Universal:latitude_fix />
            </ReadDatum>

        </Aggregate>

    </Aggregate>

    <Aggregate Id="OffshoreWrapper">

        <When>
            <Arg Name="MinOffshore"/>
            <Gt><Units:meter/><Value>0</Value></Gt>
        </When>

        <Guidance:OffshoreEnvelope>
            <Description>
                Safety behavior that keeps vehicle away from the shore
            </Description>
            <Parallel/>
            <Setting><Guidance:OffshoreEnvelope.minOffshore/><Arg Name="MinOffshore"/></Setting>
        </Guidance:OffshoreEnvelope>

    </Aggregate>

<!-- GPS Update.  Don't go too long w/o a GPS fix. -->

    <Aggregate Id="NeedComms">

        <When>
            <Elapsed><Universal:time_fix/></Elapsed>
            <Gt><Arg Name="NeedCommsTime"/></Gt>
        </When>

        <Call RefId="SURFACECOMMS" PriorityHere="false"/>

    </Aggregate>

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

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

    <Aggregate Id="TransitToWpt1">

        <Sequence/>

        <Guidance:DepthEnvelope>
            <Parallel/>
            <Setting><Guidance:DepthEnvelope.maxDepth /><Arg Name="MaxDepthTransit"/></Setting>
        </Guidance:DepthEnvelope>

        <Guidance:Pitch>
            <Parallel/>
            <Setting><Guidance:Pitch.depth/><Arg Name="ApproachDepth"/></Setting>
        </Guidance:Pitch>

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

        <Guidance:Waypoint Id="Wpt1">
            <Sequence/>
            <Setting><Guidance:Waypoint.latitude/><Arg Name="Wpt1Lat"/></Setting>
            <Setting><Guidance:Waypoint.longitude/><Arg Name="Wpt1Lon"/></Setting>
        </Guidance:Waypoint>

        <Call Id="TransitToWpt1" RefId="SURFACECOMMS"/>

    </Aggregate>

    <Aggregate Id="Wpt2">

        <Sequence/>

        <Guidance:Pitch>
            <Parallel/>
            <Setting><Guidance:Pitch.depth/><Arg Name="Depth"/></Setting>
        </Guidance:Pitch>

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

        <Guidance:Waypoint Id="Wpt2">
            <Sequence/>
            <Setting><Guidance:Waypoint.latitude/><Arg Name="Wpt2Lat"/></Setting>
            <Setting><Guidance:Waypoint.longitude/><Arg Name="Wpt2Lon"/></Setting>
        </Guidance:Waypoint>

        <Call Id="Wpt2" RefId="SURFACECOMMS" />

    </Aggregate>

</Mission>
