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

    <Description>
        This mission tests sending DVL depth-averaged water current to shore,
        based on a vehicle depth envelope.
    </Description>

<!-- Mission-wide settings -->

    <DefineArg Name="MinAltitude"><Description>
        Minimum altitude for the entire mission.
    </Description><Units:meter/><Value>4</Value></DefineArg>

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

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

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

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

    <DefineArg Name="MassDefault"><Description>
        Static setting for mass during the mission. Defaults to massDefault
        setting in the Config/Control.cfg file.
    </Description><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.
    </Description><Control:VerticalControl.buoyancyNeutral/></DefineArg>

<!-- Waypoint list -->

    <DefineArg Name="WpLat"><Description>
        Latitude (default to EcoHAB 2013 offshore ESP buoy).
    </Description><Units:degree/><Value>33.5839</Value></DefineArg>

    <DefineArg Name="WpLon"><Description>
        Longitude (default to EcoHAB 2013 offshore ESP buoy).
    </Description><Units:degree/><Value>-118.0349</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>20</Value></DefineArg>

<!-- Science loop settings -->

    <DefineArg Name="ComputeWaterCurrent"><Description>
        Set "True" if you want depth-averaged water current calculated when the
        vehicle is between 2 and 5 meters deep. Enable
        platform_*_velocity_current in Config/logger.cfg if you want it sent to
        shore. Leaving this False but enabling shore output in Config/logger.cfg
        should allow us to control this without having to send configuration
        changes and 'restart app'.
    </Description><False/></DefineArg>

    <DefineArg Name="CircleRadius"><Description>
        Radius of circle around Inshore ESP during wait time
    </Description><Units:meter/><Value>500</Value></DefineArg>

    <DefineArg Name="CircleRepeat"><Description>
        Maximum number of times to circle the waypoint at the start of each lap.
    </Description><Units:count/><Value>13</Value></DefineArg>

    <DefineArg Name="CircleDivisor"><Description>
        UTC time between reporting events. (e.g., "set CircleDivisor 8 hour" for
        reports at 0000, 0800, 1600 UTC)
    </Description><Units:hour/><Value>8</Value></DefineArg>

    <DefineArg Name="CircleModulus"><Description>
        Current time modulo CircleDivisor (internal variable).
    </Description><Units:hour/><Value>NaN</Value></DefineArg>

    <DefineArg Name="CircleTimeout"><Description>
        Timeout for circling buoy at start of each lap (internal variable).
    </Description><Units:hour/><Value>8</Value></DefineArg>

    <DefineArg Name="YoYoMinDepth"><Description>
        Minimum depth during yo-yo's.
    </Description><Units:meter/><Value>4.0</Value></DefineArg>

    <DefineArg Name="YoYoMaxDepth"><Description>
        Maximum depth during yo-yo's.
    </Description><Units:meter/><Value>78.0</Value></DefineArg>

    <DefineArg Name="YoYoMinAltitude"><Description>
        Min altitude while in yo-yo mode.
    </Description><Units:meter/><Value>6.0</Value></DefineArg>

    <DefineArg Name="YoYoUpPitch"><Description>
        Pitch when yo-yo's are ascending.
    </Description><Units:degree/><Value>20.0</Value></DefineArg>

    <DefineArg Name="YoYoDownPitch"><Description>
        Pitch when yo-yo's are descending.
    </Description><Units:degree/><Value>-20.0</Value></DefineArg>

<!-- The mission -->

    <Timeout Duration="MissionTimeout"/>

<!-- High-priority directives that should always be near the top -->

    <Guidance:AltitudeEnvelope>
        <Parallel/>
        <Setting><Guidance:AltitudeEnvelope.minAltitude/><Arg Name="MinAltitude"/></Setting>
    </Guidance:AltitudeEnvelope>

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

    <Guidance:OffshoreEnvelope>
        <Parallel/>
        <Setting><Guidance:OffshoreEnvelope.minOffshore/><Arg Name="MinOffshore"/></Setting>
    </Guidance:OffshoreEnvelope>

<!-- Surfacing behaviors -->

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

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

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

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

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

<!-- can adjust SurfaceSafety:MaxTime using RedefineArg if necessary -->

<!-- TODO put all of these together into a master Surface insert file -->

<!-- mass and buoyancy defaults are lower priority than surfacing, but higher than everything else -->

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

<!-- Begin callable aggregates used in this mission. 
     (Note priority still matters, so these are done _below_ important safety 
     directives like OffshoreEnvelope.) 
     -->

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

    <Aggregate Id="CircleTarget">

        <Description>
            Circle a waypoint at the nominal location of the Offshore ESP buoy
            until timeout specified by CircleDivisor and current time.
        </Description>

        <When>
            <Called/>
        </When>

        <Aggregate Id="TellMeHowLong">

            <Description>
                Convenience aggregate to notify operator of estimated circling
                time _before_ starting to circle the waypoint. May be commented
                out, since the Timeout in CircleWaypointRepeatedly will still
                achieve the desired behavior.
            </Description>

            <Sequence/>

            <Syslog Severity="Important">CircleOffshoreESPBuoy should complete in <Arg Name="CircleTimeout"/><Units:minute/>, due to timeout in CircleWaypointRepeatedly aggregate.</Syslog>

            <Call RefId="SurfaceCommsShort"/>

        </Aggregate>

        <Assign><Sequence/><Arg Name="CircleWaypointRepeatedly:Timeout"/><Arg Name="CircleTimeout"/></Assign>

        <Assign><Sequence/><Arg Name="CircleWaypointRepeatedly:Radius"/><Arg Name="CircleRadius"/></Assign>

        <Assign><Sequence/><Arg Name="CircleWaypointRepeatedly:CenterLat"/><Arg Name="WpLat"/></Assign>

        <Assign><Sequence/><Arg Name="CircleWaypointRepeatedly:CenterLon"/><Arg Name="WpLon"/></Assign>

<!-- Use default values for: Timeout, MaxError, TurnToPort, KwpHeading -->

        <Call RefId="CircleWaypointRepeatedly"/>

        <Syslog Severity="Important">************* Finished circling. *************</Syslog>

        <Call RefId="SurfaceCommsSplit"/>

    </Aggregate>

<!-- End callable aggregates used in this mission. -->

    <Aggregate Id="ScienceOps">

        <Description>
            Collect science data.
        </Description>

        <Sequence/>

        <Insert Filename="Insert/Science.xml" Id="Science">
            <Description>
                Get science data, including PeakDetectChl
            </Description>
            <RedefineArg Name="PeakDetectChlActive"><True/></RedefineArg>
<!-- expose ComputeWaterCurrent boolean flag to top level of mission -->
            <RedefineArg Name="ComputeWaterCurrent"><True/></RedefineArg>
            <RedefineArg Name="ComputeWaterCurrentMinDepth"><Units:meter/><Value>3.0</Value></RedefineArg>
            <RedefineArg Name="ComputeWaterCurrentMaxDepth"><Units:meter/><Value>5.0</Value></RedefineArg>
            <RedefineArg Name="UpwardDerivativeOfTemperatureActive"><False/></RedefineArg>
        </Insert>

<!-- TODO also test VTHIdx insert aggregate at the same time... -->

        <Insert Filename="Insert/VerticalTemperatureHomogeneityIndex.xml" Id="VTHIdx">
            <RedefineArg Name="DepthShallow"><Units:meter/><Value>5</Value></RedefineArg>
            <RedefineArg Name="DepthMidShallow"><Units:meter/><Value>10</Value></RedefineArg>
            <RedefineArg Name="DepthMidDeep"><Units:meter/><Value>20</Value></RedefineArg>
            <RedefineArg Name="DepthDeep"><Units:meter/><Value>25</Value></RedefineArg>
            <RedefineArg Name="Verbosity"><Units:count/><Value>1</Value></RedefineArg>
        </Insert>

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

        <Guidance:DepthEnvelope>
            <Parallel/>
            <Setting><Guidance:DepthEnvelope.minDepth/><Arg Name="YoYoMinDepth"/></Setting>
            <Setting><Guidance:DepthEnvelope.maxDepth/><Arg Name="YoYoMaxDepth"/></Setting>
            <Setting><Guidance:DepthEnvelope.downPitch/><Arg Name="YoYoDownPitch"/></Setting>
            <Setting><Guidance:DepthEnvelope.upPitch/><Arg Name="YoYoUpPitch"/></Setting>
        </Guidance:DepthEnvelope>

        <Guidance:AltitudeEnvelope>
            <Parallel/>
            <Setting><Guidance:AltitudeEnvelope.minAltitude/><Arg Name="YoYoMinAltitude"/></Setting>
            <Setting><Guidance:AltitudeEnvelope.upPitch/><Arg Name="YoYoUpPitch"/></Setting>
        </Guidance:AltitudeEnvelope>

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

<!-- Call the behavior aggregates that we defined above. -->

        <Call RefId="CircleTarget"/>

    </Aggregate>

</Mission>
