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

    <Description>
        Vehicle drifts at the surface, continuously recording GPS signals. If
        enabled, it also estimates the evolving location of a surface drifter
        using the HFRCM components.
    </Description>

    <DefineArg Name="MinAltitude"><Description>
        Minimum height above the sea floor for the entire mission.
    </Description><Units:meter/><Value>10.0</Value></DefineArg>

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

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

    <DefineArg Name="NeedCommsTime"><Description>
        Elapsed time after previous surface communications when vehicle will
        begin to attempt additional surface communications.
    </Description><Units:minute/><Value>12</Value></DefineArg>

    <DefineArg Name="Timeout"><Description>
        Maximum length of mission
    </Description><Units:hour/><Value>6</Value></DefineArg>

    <DefineArg Name="BuoyancyMax"><Description>
        Buoyancy bladder position for drifting at the surface. This defaults to
        buoyancyLimitHiCC setting in the Config/Control.cfg file.
    </Description><Control:VerticalControl.buoyancyLimitHiCC/></DefineArg>

    <DefineArg Name="MassDefault"><Description>
        Static setting for mass during the mission. This defaults to massDefault
        setting in the Config/Confrol.cfg file.
    </Description><Control:VerticalControl.massDefault/></DefineArg>

<!--
	massPositionLimitFwd setting in the Config/Confrol.cfg file.
    </Description><Control:VerticalControl.massPositionLimitFwd/></DefineArg>
-->

    <Timeout Duration="Timeout"/>

    <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>
        <Description>
            Safety behavior that keeps vehicle from going too deep
        </Description>
        <Parallel/>
        <Setting><Guidance:DepthEnvelope.maxDepth/><Arg Name="MaxDepth"/></Setting>
    </Guidance:DepthEnvelope>

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

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

<!-- Communication update: Don't go too long without calling home. -->

    <Aggregate Id="NeedComms">

        <Description>
            Initiate communications with shore. (Don't use Insert/NeedComms.xml
            here since that operates based on Universal:time_fix, which is
            continuously updated in this drift mission.) Note that there is a
            two-minute timeout on platform_communications, after which the
            mission will exit this aggregate. If communications are not
            completed, it will re-enter this aggregate immediately, take one GPS
            fix, and then try to communicate again.
        </Description>

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

        <ReadDatum>
            <Universal:time_fix/>
        </ReadDatum>

        <ReadDatum>
            <Timeout Duration="P2M"/>
            <Universal:platform_communications/>
        </ReadDatum>

    </Aggregate>

    <Insert Filename="Insert/Science.xml">
        <Description>
            Record standard science data at the surface during the drift.
        </Description>
    </Insert>

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

    <Aggregate Id="ClearCommsBuffer">

        <Description>
            Clean up any existing communications before starting to drift.
        </Description>

        <Sequence/>

        <ReadDatum>
            <Universal:platform_communications/>
        </ReadDatum>

    </Aggregate>

<!-- (re)initialize the virtual surface drifter -->

    <Aggregate Id="ReinitializeHFRCMVirtualSurfaceDrifter">

        <Sequence/>

<!-- TODO: Would really prefer an "if" conditional here. Only want to do it once. -->

<!--
        <When>
            <CustomUri Uri="HFRCMVirtualSurfaceDrifter.loadAtStartup"/>
        </When>
-->

        <Syslog Severity="Important">
	    HFRCMVSD.loadAtStartup: <CustomUri Uri="HFRCMVirtualSurfaceDrifter.loadAtStartup"/><Units:bool/></Syslog>

        <Estimation:ReinitializeHFRCMVirtualSurfaceDrifter>
            <Sequence/>
            <Setting><Estimation:ReinitializeHFRCMVirtualSurfaceDrifter.initial_latitude_setting/><Universal:latitude/></Setting>
            <Setting><Estimation:ReinitializeHFRCMVirtualSurfaceDrifter.initial_longitude_setting/><Universal:longitude/></Setting>
        </Estimation:ReinitializeHFRCMVirtualSurfaceDrifter>

        <Syslog Severity="Important">Reinitialized virtual drifter location to current vehicle location.</Syslog>

    </Aggregate>

<!-- Continuously record GPS signals. -->

    <Aggregate Id="ContinuousGPS">

        <Description>
            Continuously query for GPS data.
        </Description>

        <Parallel/>

        <ReadData Strategy="MinError">
            <Universal:time_fix/>
        </ReadData>

    </Aggregate>

    <Guidance:Wait>
        <Sequence/>
    </Guidance:Wait>

</Mission>
