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

    <Description>
        Control depth within deadband, settle, and then estimate until
        measurements converge and report.
    </Description>

    <DefineArg Name="InsertDepth"><Units:meter/><Value>25</Value></DefineArg>

    <DefineArg Name="InsertDepthDeadband"><Units:meter/><Value>1</Value></DefineArg>

    <DefineArg Name="InsertSettleTime"><Description>
        How long to wait after reaching target depth before starting ballast and
        trim.
    </Description><Units:minute/><Value>10</Value></DefineArg>

    <DefineArg Name="EstTimeout"><Description>
        Timeout for ballast and trim estimation.
    </Description><Units:minute/><Value>45</Value></DefineArg>

    <DefineArg Name="MinEstTime"><Description>
        Minimum time to run ballast and trim estimation before determining
        convergence (must be shorter than timeout).
    </Description><Units:minute/><Value>10</Value></DefineArg>

    <DefineArg Name="MassEstErrorBound"><Description>
        Desired range of the estimated sample mean from the true mean statistic
        (smaller values take longer to converge).
    </Description><Units:millimeter/><Value>0.25</Value></DefineArg>

    <DefineArg Name="BuoyEstErrorBound"><Description>
        Desired range of the estimated sample mean from the true mean statistic
        (smaller values take longer to converge).
    </Description><Units:cubic_centimeter/><Value>15</Value></DefineArg>

    <DefineArg Name="EstConfidence"><Description>
        Confidence level that the estimator has reached the desired error range
        (larger values take longer to converge).
    </Description><Units:percent/><Value>99.9</Value></DefineArg>

    <Sequence/>

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

    <Assign><Parallel/><Control:VerticalControl.kpPitchMass/><Units:ratio/><Value>0.01</Value></Assign>

    <Assign><Parallel/><Control:VerticalControl.kiPitchMass/><Units:reciprocal_second/><Value>0.0015</Value></Assign>

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

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

    <Guidance:Pitch>
        <Progression/>
        <Setting><Guidance:Pitch.depth/><Arg Name="InsertDepth"/></Setting>
    </Guidance:Pitch>

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

    <Estimation:BallastAndTrim>
        <Sequence/>
        <Setting><Estimation:BallastAndTrim.estimationTimeout/><Arg Name="EstTimeout"/></Setting>
        <Setting><Estimation:BallastAndTrim.minEstimationTime/><Arg Name="MinEstTime"/></Setting>
        <Setting><Estimation:BallastAndTrim.massErrorBound/><Arg Name="MassEstErrorBound"/></Setting>
        <Setting><Estimation:BallastAndTrim.buoyErrorBound/><Arg Name="BuoyEstErrorBound"/></Setting>
        <Setting><Estimation:BallastAndTrim.confidenceLevel/><Arg Name="EstConfidence"/></Setting>
    </Estimation:BallastAndTrim>

</Aggregate>
