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

    <Description>
        Vehicle spirals downward, then spirals upward, stopping at specified
        depths to collect samples.
    </Description>

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

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

    <DefineArg Name="Depth01"><Description>
        Depth of sample 1.
    </Description><Units:meter/><Value>200</Value></DefineArg>

    <DefineArg Name="Depth02"><Description>
        Depth of sample 2.
    </Description><Units:meter/><Value>150</Value></DefineArg>

    <DefineArg Name="Depth03"><Description>
        Depth of sample 3.
    </Description><Units:meter/><Value>80</Value></DefineArg>

    <DefineArg Name="Depth04"><Description>
        Depth of sample 4.
    </Description><Units:meter/><Value>60</Value></DefineArg>

    <DefineArg Name="Depth05"><Description>
        Depth of sample 5.
    </Description><Units:meter/><Value>60</Value></DefineArg>

    <DefineArg Name="Depth06"><Description>
        Depth of sample 6.
    </Description><Units:meter/><Value>40</Value></DefineArg>

    <DefineArg Name="Depth07"><Description>
        Depth of sample 7.
    </Description><Units:meter/><Value>40</Value></DefineArg>

    <DefineArg Name="Depth08"><Description>
        Depth of sample 8.
    </Description><Units:meter/><Value>30</Value></DefineArg>

    <DefineArg Name="Depth09"><Description>
        Depth of sample 9.
    </Description><Units:meter/><Value>30</Value></DefineArg>

    <DefineArg Name="Depth10"><Description>
        Depth of sample 10.
    </Description><Units:meter/><Value>20</Value></DefineArg>

    <DefineArg Name="Depth11"><Description>
        Depth of sample 11.
    </Description><Units:meter/><Value>20</Value></DefineArg>

    <DefineArg Name="Depth12"><Description>
        Depth of sample 12.
    </Description><Units:meter/><Value>10</Value></DefineArg>

    <DefineArg Name="Depth13"><Description>
        Depth of sample 13.
    </Description><Units:meter/><Value>10</Value></DefineArg>

    <DefineArg Name="Depth14"><Description>
        Depth of sample 14.
    </Description><Units:meter/><Value>2</Value></DefineArg>

    <DefineArg Name="Depth15"><Description>
        Depth of sample 15.
    </Description><Units:meter/><Value>2</Value></DefineArg>

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

    <DefineArg Name="SettleTime"><Description>
        How long to wait after reaching target depth before triggering sample.
    </Description><Units:second/><Value>0</Value></DefineArg>

    <DefineArg Name="RudderAngle"><Description>
        Rudder angle to use while performing the spiral cast.
    </Description><Units:degree/><Value>13</Value></DefineArg>

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

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

    <DefineArg Name="MinAltitude"><Description>
        Minimum altitude 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>210</Value></DefineArg>

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

    <Timeout Duration="MissionTimeout"/>

<!-- Most missions will run the science sensors. -->

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

<!-- Most missions will use the NeedComms aggregate. In this case, we use NeedComms for the start of the mission and for the upcast, but set the dive interval to be the same as the mission timeout. -->

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

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

    <Assign><Sequence/><Arg Name="NeedComms:SurfaceSpeed"/><Arg Name="Speed"/></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>

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

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

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

    <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:Point>
        <Parallel/>
        <Setting><Guidance:Point.rudderAngle/><Arg Name="RudderAngle"/></Setting>
    </Guidance:Point>

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

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

    <Assign><Sequence/><Arg Name="SampleAtDepth:SettleTime"/><Arg Name="SettleTime"/></Assign>

    <Aggregate Id="SampleWithinEnvelopesWrapper">

        <Description>
            Avoid bouncing in and out of safety envelopes. If your specified
            sample depth is too deep or too close to the seafloor, skip it.
        </Description>

        <DefineArg Name="TargetDepth"><Description>
            Depth to sample at.
        </Description><Units:meter/><Value>1</Value></DefineArg>

        <When>
            <Called/>
        </When>

        <Break>
            <Universal:depth/>
            <Ge><Arg Name="MaxDepth"/></Ge>
            <And><Arg Name="TargetDepth"/><Ge><Arg Name="MaxDepth"/></Ge></And>
        </Break>

        <Assign><Sequence/><Arg Name="SampleAtDepth:TargetDepth"/><Arg Name="TargetDepth"/></Assign>

        <Call Id="SampleWithinEnvelopes" RefId="SampleAtDepth"/>

    </Aggregate>

    <Aggregate Id="Cast">

        <Sequence/>

        <Assign><Sequence/><Arg Name="SampleWithinEnvelopesWrapper:TargetDepth"/><Arg Name="Depth01"/></Assign>

        <Call Id="Depth01" RefId="SampleWithinEnvelopesWrapper"/>

        <Assign><Sequence/><Arg Name="SampleWithinEnvelopesWrapper:TargetDepth"/><Arg Name="Depth02"/></Assign>

        <Call Id="Depth02" RefId="SampleWithinEnvelopesWrapper"/>

        <Assign><Sequence/><Arg Name="SampleWithinEnvelopesWrapper:TargetDepth"/><Arg Name="Depth03"/></Assign>

        <Call Id="Depth03" RefId="SampleWithinEnvelopesWrapper"/>

        <Assign><Sequence/><Arg Name="SampleWithinEnvelopesWrapper:TargetDepth"/><Arg Name="Depth04"/></Assign>

        <Call Id="Depth04" RefId="SampleWithinEnvelopesWrapper"/>

        <Assign><Sequence/><Arg Name="SampleWithinEnvelopesWrapper:TargetDepth"/><Arg Name="Depth05"/></Assign>

        <Call Id="Depth05" RefId="SampleWithinEnvelopesWrapper"/>

        <Assign><Sequence/><Arg Name="SampleWithinEnvelopesWrapper:TargetDepth"/><Arg Name="Depth06"/></Assign>

        <Call Id="Depth06" RefId="SampleWithinEnvelopesWrapper"/>

        <Assign><Sequence/><Arg Name="SampleWithinEnvelopesWrapper:TargetDepth"/><Arg Name="Depth07"/></Assign>

        <Call Id="Depth07" RefId="SampleWithinEnvelopesWrapper"/>

        <Assign><Sequence/><Arg Name="SampleWithinEnvelopesWrapper:TargetDepth"/><Arg Name="Depth08"/></Assign>

        <Call Id="Depth08" RefId="SampleWithinEnvelopesWrapper"/>

        <Assign><Sequence/><Arg Name="SampleWithinEnvelopesWrapper:TargetDepth"/><Arg Name="Depth09"/></Assign>

        <Call Id="Depth09" RefId="SampleWithinEnvelopesWrapper"/>

        <Assign><Sequence/><Arg Name="SampleWithinEnvelopesWrapper:TargetDepth"/><Arg Name="Depth10"/></Assign>

        <Call Id="Depth10" RefId="SampleWithinEnvelopesWrapper"/>

        <Assign><Sequence/><Arg Name="SampleWithinEnvelopesWrapper:TargetDepth"/><Arg Name="Depth11"/></Assign>

        <Call Id="Depth11" RefId="SampleWithinEnvelopesWrapper"/>

        <Assign><Sequence/><Arg Name="SampleWithinEnvelopesWrapper:TargetDepth"/><Arg Name="Depth12"/></Assign>

        <Call Id="Depth12" RefId="SampleWithinEnvelopesWrapper"/>

        <Assign><Sequence/><Arg Name="SampleWithinEnvelopesWrapper:TargetDepth"/><Arg Name="Depth13"/></Assign>

        <Call Id="Depth13" RefId="SampleWithinEnvelopesWrapper"/>

        <Assign><Sequence/><Arg Name="SampleWithinEnvelopesWrapper:TargetDepth"/><Arg Name="Depth14"/></Assign>

        <Call Id="Depth14" RefId="SampleWithinEnvelopesWrapper"/>

        <Assign><Sequence/><Arg Name="SampleWithinEnvelopesWrapper:TargetDepth"/><Arg Name="Depth15"/></Assign>

        <Call Id="Depth15" RefId="SampleWithinEnvelopesWrapper"/>

    </Aggregate>

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

</Mission>
