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

<!-- The following arguments are input arguments -->

    <DefineArg Name="ApproachDepth"><Units:meter/><Value>20.0</Value></DefineArg>

    <DefineArg Name="WpLat"><Units:degree/><Value>36.74</Value></DefineArg>

    <DefineArg Name="WpLon"><Units:degree/><Value>-121.94</Value></DefineArg>

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

    <DefineArg Name="Speed"><Units:meter_per_second/><Value>1</Value></DefineArg>

    <DefineArg Name="DepthDeadband"><Units:meter/><Value>2.0</Value></DefineArg>

    <DefineArg Name="BuoyancyDefault"><Control:VerticalControl.buoyancyDefault/></DefineArg>

<!-- The timeout -->

    <Timeout Duration="P400M"/>

<!-- And now the mission -->

    <Guidance:AltitudeEnvelope>
        <Parallel/>
        <Setting><Guidance:AltitudeEnvelope.minAltitude/><Units:meter/><Value>7</Value></Setting>
    </Guidance:AltitudeEnvelope>

    <Guidance:DepthEnvelope>
        <Parallel/>
        <Setting><Guidance:DepthEnvelope.maxDepth/><Units:meter/><Value>65</Value></Setting>
    </Guidance:DepthEnvelope>

    <Guidance:OffshoreEnvelope>
        <Parallel/>
        <Setting><Guidance:OffshoreEnvelope.minOffshore/><Units:kilometer/><Value>2.0</Value></Setting>
        <Setting><Guidance:OffshoreEnvelope.maxOffshore/><Units:kilometer/><Value>30.0</Value></Setting>
    </Guidance:OffshoreEnvelope>

    <Insert Filename="Insert/Surface.xml" Id="SurfaceComms" />

<!-- 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>
            <Or><Elapsed><Universal:time_fix/></Elapsed><Gt><Units:minute/><Value>120</Value></Gt></Or>
        </When>

        <Call RefId="SurfaceComms"/>

    </Aggregate>

<!-- If we're executing here or below, lets' get science data! -->

    <Insert Filename="Insert/Science.xml">
        <RedefineArg Name="PeakDetectChlActive"><True/></RedefineArg>
    </Insert>

    <Aggregate Id="Waypoint">

        <Sequence/>

        <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="WaypointW1">
            <Sequence/>
            <Setting><Guidance:Waypoint.latitude /><Arg Name="WpLat" /></Setting>
            <Setting><Guidance:Waypoint.longitude /><Arg Name="WpLon" /></Setting>
        </Guidance:Waypoint>

        <Call Id="PHONEHOMEWPT" RefId="SurfaceComms" />

    </Aggregate>

    <Aggregate Id="yo_yo">

        <Sequence/>

        <Guidance:SetSpeed>
            <Parallel/>
            <Setting><Guidance:SetSpeed.speed/><Units:meter_per_second/><Value>0</Value></Setting>
        </Guidance:SetSpeed>

        <Assign><Parallel/><Control:VerticalControl.depthDeadband/><Arg Name="DepthDeadband"/></Assign>

        <Aggregate Id="yo_yo_loop">

            <Sequence Repeat="5"/>

            <Aggregate Id="bottomDepth">

                <Sequence/>

                <Guidance:Pitch>
                    <Sequence/>
                    <Setting><Guidance:Pitch.depth/><Units:meter/><Value>30</Value></Setting>
                </Guidance:Pitch>

            </Aggregate>

            <Aggregate Id="topDepth">

                <Sequence/>

                <Guidance:Pitch>
                    <Sequence/>
                    <Setting><Guidance:Pitch.depth/><Units:meter/><Value>50</Value></Setting>
                </Guidance:Pitch>

            </Aggregate>

        </Aggregate>

        <Aggregate Id="Float_Up">

            <Sequence/>

            <Break>
                <Universal:depth/>
                <Lt><Units:meter/><Value>1</Value></Lt>
            </Break>

            <Guidance:SetSpeed>
                <Parallel/>
                <Setting><Guidance:SetSpeed.speed/><Units:meter_per_second/><Value>0</Value></Setting>
            </Guidance:SetSpeed>

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

            <Guidance:Wait>
                <Sequence/>
                <Timeout Duration="P15M"></Timeout>
            </Guidance:Wait>

        </Aggregate>

        <Call Id="PHONEHOME_drift" RefId="SurfaceComms" />

    </Aggregate>

</Mission>
