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

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

<!-- Wpt1 d=74m -->

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

    <DefineArg Name="BallastDepth"><Units:meter/><Value>25.0</Value></DefineArg>

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

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

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

    <DefineArg Name="HoldDuration"><Units:minute/><Value>25</Value></DefineArg>

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

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

    <DefineArg Name="BuoyPeriod"><Units:second/><Value>5.0</Value></DefineArg>

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

<!-- The timeout -->

    <Timeout Duration="P90M"/>

<!-- And now the mission -->

    <Assign><Parallel/><Control:VerticalControl.elevDeadband/><Units:degree/><Value>0.01</Value></Assign>

    <Assign><Parallel/><Control:VerticalControl.massDeadband/><Units:millimeter/><Value>0.01</Value></Assign>

    <Assign><Parallel/><Control:VerticalControl.maxDiveAccel/><Units:meter_per_second_squared/><Value>0.001</Value></Assign>

    <Assign><Parallel/><Control:VerticalControl.kpPitchMass/><Units:count/><Value>0.1</Value></Assign>

    <Assign><Parallel/><Control:VerticalControl.kiPitchMass/><Units:second/><Value>0.0007</Value></Assign>

    <Assign><Parallel/><Control:VerticalControl.kdPitchMass/><Units:second/><Value>0.2</Value></Assign>

    <Assign><Parallel/><Control:VerticalControl.kiDepthOff/><Units:meter_per_second/><Value>.2</Value></Assign>

    <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>52</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>65</Value></Gt></Or>
        </When>

        <Call RefId="SurfaceComms"/>

    </Aggregate>

    <Guidance:SetSpeed>
        <While>
            <Universal:platform_propeller_rotation_rate/>
            <Eq><Units:radian_per_second/><Value>0</Value></Eq>
        </While>
        <Setting><Guidance:SetSpeed.period/><Arg Name="BuoyPeriod"/></Setting>
    </Guidance:SetSpeed>

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

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

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

    <Aggregate Id="ballast">

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

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

        <Guidance:Wait>
            <Sequence/>
            <Setting><Guidance:Wait.duration/><Arg Name="HoldDuration"/></Setting>
        </Guidance:Wait>

        <Aggregate Id="ReportPositions">

            <Sequence Repeat="5"/>

            <Syslog Severity="Important">Buoyancy:<Universal:platform_buoyancy_position/><Units:cubic_centimeter/></Syslog>

            <Syslog Severity="Important">Mass:<Universal:platform_mass_position/><Units:centimeter/></Syslog>

            <Syslog Severity="Important">Pitch:<Universal:platform_pitch_angle/><Units:degree/></Syslog>

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

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