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

<!-- Almost every mission should start with an overall timeout and a NeedCommsTime. Drift Missions will have an acoustic timeout instead of a NeedCommsTime. -->

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

    <DefineArg Name="NumESPSamplers"><Description>
        Total number of ESP cartridges (Maximum 60. Set to a smaller number if
        not using all of them. Set to 0 to disable ESP sampling).
    </Description><Units:count/><Value>3</Value></DefineArg>

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

    <DefineArg Name="NeedCommsTimeInTransect"><Description>
        In transit, how often to surface for commumications
    </Description><Units:minute/><Value>60</Value></DefineArg>

    <DefineArg Name="AcousticTrackingTimeout"><Description>
        If the vehicle does not receive an acoustic signal for more than this
        length of time, it will surface for communications. Set longer than
        MissionTimeout to effectively disable.
    </Description><Units:hour/><Value>21</Value></DefineArg>

    <DefineArg Name="MaxWaitNoFiring"><Description>
        If no firing after more than MaxWaitNoFiring, terminate mission.
    </Description><Units:hour/><Value>4</Value></DefineArg>

    <DefineArg Name="CylinderTimeout"><Description>
        Maximum duration of cylinder yoyo.
    </Description><Units:minute/><Value>30</Value></DefineArg>

    <DefineArg Name="CylinderYoYo"><Description>
        Whether to yoyo on cylinder centered at the sampling location.
        Initialized to True.
<!--debug
    </Description><True/></DefineArg>
-->
    </Description><False/></DefineArg>

    <DefineArg Name="SpeedTransit"><Description>
        Vehicle speed when transiting.
    </Description><Units:meter_per_second/><Value>1.0</Value></DefineArg>

    <DefineArg Name="SpeedSmearSampling"><Description>
        Vehicle speed for smear sampling.
    </Description><Units:meter_per_second/><Value>1.0</Value></DefineArg>

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

    <DefineArg Name="ShallowBoundSmear"><Description>
        Shallow depth bound for smear sampling.
    </Description><Units:meter/><Value>5.0</Value></DefineArg>

    <DefineArg Name="DeepBoundSmear"><Description>
        Deep depth bound for smear sampling.
    </Description><Units:meter/><Value>25</Value></DefineArg>

    <DefineArg Name="TransitYoYoMinDepth"><Description>
        Minimum depth while performing the YoYo behavior during transit.
    </Description><Units:meter/><Value>1.5</Value></DefineArg>

    <DefineArg Name="TransitYoYoMaxDepth"><Description>
        Maximum depth while performing the YoYo behavior during transit.
    </Description><Units:meter/><Value>30</Value></DefineArg>

    <DefineArg Name="CylinderYoYoMinDep"><Description>
        Minimum depth while performing the YoYo behavior on cylinder centered at
        the sampling location.
    </Description><Units:meter/><Value>1.5</Value></DefineArg>

    <DefineArg Name="CylinderYoYoMaxDep"><Description>
        Maximum depth while performing the YoYo behavior on cylinder centered at
        the sampling location.
    </Description><Units:meter/><Value>30</Value></DefineArg>

    <DefineArg Name="CylinderRadius"><Description>
        Radius of cylinder centered at the sampling location.
    </Description><Units:meter/><Value>200</Value></DefineArg>

    <DefineArg Name="MaxCirclesOnCylinder"><Description>
        Maximum number of circles on cylinder.
    </Description><Units:count/><Value>1</Value></DefineArg>

    <DefineArg Name="RepeatTransects"><Description>
        Number of times the vehicle should try to cycle through waypoints..
    </Description><Units:count/><Value>1</Value></DefineArg>

    <DefineArg Name="Lat1"><Description>
        Latitude of waypoint 1. If nan, waypoint is skipped.
    </Description><Units:degree/><Value>NaN</Value></DefineArg>

<!--debug
    </Description><Units:degree/><Value>36.815</Value></DefineArg>
    </Description><Units:degree/><Value>36.810</Value></DefineArg>
-->

    <DefineArg Name="Lon1"><Description>
        Longitude of waypoint 1. If nan, waypoint is skipped.
    </Description><Units:degree/><Value>NaN</Value></DefineArg>

<!--debug
    </Description><Units:degree/><Value>-121.835</Value></DefineArg>
    </Description><Units:degree/><Value>-121.825</Value></DefineArg>
-->

    <DefineArg Name="Lat2"><Description>
        Latitude of waypoint 2. If nan, waypoint is skipped.
    </Description><Units:degree/><Value>NaN</Value></DefineArg>

<!--debug
    </Description><Units:degree/><Value>36.825</Value></DefineArg>
-->

    <DefineArg Name="Lon2"><Description>
        Longitude of waypoint 2. If nan, waypoint is skipped.
    </Description><Units:degree/><Value>NaN</Value></DefineArg>

<!--debug
    </Description><Units:degree/><Value>-121.845</Value></DefineArg>
-->

    <DefineArg Name="Lat3"><Description>
        Latitude of waypoint 3. If nan, waypoint is skipped.
    </Description><Units:degree/><Value>NaN</Value></DefineArg>

    <DefineArg Name="Lon3"><Description>
        Longitude of waypoint 3. If nan, waypoint is skipped.
    </Description><Units:degree/><Value>NaN</Value></DefineArg>

    <DefineArg Name="Lat4"><Description>
        Latitude of waypoint 4. If nan, waypoint is skipped.
    </Description><Units:degree/><Value>NaN</Value></DefineArg>

    <DefineArg Name="Lon4"><Description>
        Longitude of waypoint 4. If nan, waypoint is skipped.
    </Description><Units:degree/><Value>NaN</Value></DefineArg>

<!-- Use caution when changing these. -->

    <DefineArg Name="UpRate"><Description>
        Go-up depth rate (Negative depth rate means going up).
    </Description><Units:meter_per_second/><Value>-0.04</Value></DefineArg>

    <DefineArg Name="DownRate"><Description>
        Go-down depth rate (Positive depth rate means going down).
    </Description><Units:meter_per_second/><Value>0.04</Value></DefineArg>

    <DefineArg Name="YoYoMinAltitude"><Description>
        Minimum altitude while performing the YoYo behavior (for
        bottom-terminated YoYos).
    </Description><Units:meter/><Value>9</Value></DefineArg>

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

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

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

    <DefineArg Name="CylinderCircleMaxError"><Description>
        If this distance away from the circle, drive straight towards (or away
        from the center). Otherwise, try to reduce distance from the ideal
        circle.
    </Description><Units:meter/><Value>100</Value></DefineArg>

    <DefineArg Name="CylinderCircleTurnToPort"><Description>
        If true, vehicle turns to the left around the center point. If false,
        vehicle turns to the right.
    </Description><False/></DefineArg>

    <DefineArg Name="KwpHeading"><Description>
        Used to relax waypoint cross-track error constant that is adjusted for
        docking. (You can override this setting by passing an argument.)
    </Description><Units:radian_per_meter/><Value>0.010</Value></DefineArg>

    <DefineArg Name="YoYoUpPitch"><Description>
        Vehicle up pitch while performing the YoYo behavior.
    </Description><Units:degree/><Value>20</Value></DefineArg>

    <DefineArg Name="YoYoDownPitch"><Description>
        Vehicle down pitch while performing the YoYo behavior.
    </Description><Units:degree/><Value>-20</Value></DefineArg>

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

    <DefineArg Name="BuoyancyNeutral"><Description>
        Buoyancy bladder position while performing the YoYo behavior. Defaults
        to buoyancyNeutral setting in the Config/Control.cfg file. Set to NaN cc
        for active buoyancy
    </Description><Control:VerticalControl.buoyancyNeutral/></DefineArg>

    <DefineArg Name="MassDefault"><Description>
        Static setting for mass during the mission. Set to NaN mm for active
        mass position
    </Description><Control:VerticalControl.massDefault/></DefineArg>

<!-- Internal variables that you should not change -->

    <DefineArg Name="YoYoMinDepth"><Description>
        Minimum depth while performing the YoYo behavior. Program will set to
        TransitYoYoMinDepth, and then CylinderYoYoMinDep.
    </Description><Units:meter/><Value>1.5</Value></DefineArg>

    <DefineArg Name="YoYoMaxDepth"><Description>
        Maximum depth while performing the YoYo behavior. Program will set to
        TransitYoYoMaxDepth, and then CylinderYoYoMaxDep.
    </Description><Units:meter/><Value>30</Value></DefineArg>

    <DefineArg Name="SampleCompleted"><Description>
        Sampling completed. Initialized to false.
    </Description><False/></DefineArg>

    <DefineArg Name="StoppedForNoFiringForTooLong"><Description>
        Mission stopped because of no firing for too long. Initialized to false.
    </Description><False/></DefineArg>

    <DefineArg Name="DepthLogged"><Description></Description><Units:meter/><Value>0.0</Value></DefineArg>

    <DefineArg Name="TempLogged"><Description></Description><Units:celsius/><Value>0.0</Value></DefineArg>

    <DefineArg Name="ChlLogged"><Description></Description><Units:microgram_per_liter/><Value>0.0</Value></DefineArg>

    <DefineArg Name="MissionStartCommsCompleted"><Description>
        MissionStartComms is completed (initialized to false).
    </Description><False/></DefineArg>

    <DefineArg Name="StartSmear"><Description>
        Start vertical smear (initialized to false).
    </Description><False/></DefineArg>

    <DefineArg Name="FlagGoUp"><Description>
        Go-up flag (set to true when going up. Initialized to false).
    </Description><False/></DefineArg>

    <DefineArg Name="FlagGoDown"><Description>
        Go-down flag (set to true when going down. Initialized to false).
    </Description><False/></DefineArg>

    <DefineArg Name="EnableFiring"><Description>
        Enable firing. Initialized to true.
    </Description><True/></DefineArg>

    <DefineArg Name="FlagSamplingOngoing"><Description>
        Flag of water sampling in process (initialized to false).
    </Description><False/></DefineArg>

    <DefineArg Name="FlagFired"><Description>
        Flag of sample fired (initialized to false).
    </Description><False/></DefineArg>

    <DefineArg Name="FromLastTimeFixToStartSmear"><Description>
        Elapsed time from last time-fix to StartSmear.
    </Description><Units:minute/><Value>NaN</Value></DefineArg>

    <DefineArg Name="FromLastTimeFixToNow"><Description>
        Elapsed time from last time-fix to now.
    </Description><Units:minute/><Value>NaN</Value></DefineArg>

    <DefineArg Name="FromLastTimeFixToLastFiring"><Description>
        Elapsed time from last time-fix to last firing.
    </Description><Units:minute/><Value>NaN</Value></DefineArg>

    <DefineArg Name="CylinderLat"><Description>
        Latitude of cylinder center.
    </Description><Units:degree/><Value>NaN</Value></DefineArg>

    <DefineArg Name="CylinderLon"><Description>
        Longitude of cylinder center.
    </Description><Units:degree/><Value>NaN</Value></DefineArg>

    <DefineArg Name="CntSamples"><Description>
        Count of water samples (initilized to 1).
    </Description><Units:count/><Value>1</Value></DefineArg>

<!-- Missions should almost always start with a timeout and a NeedComms aggregate. Drift missions will not use NeedComms. -->

    <Timeout Duration="MissionTimeout"/>

<!-- Missions should almost always start with standard safety 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>

    <Assign><Sequence/><Arg Name="StandardEnvelopes:MinOffshore"/><Arg Name="MinOffshore"/></Assign>

    <Insert Filename="Insert/AbortDrift.xml">
        <RedefineArg Name="AcousticTimeout"><Arg Name="AcousticTrackingTimeout"/></RedefineArg>
    </Insert>

<!-- Many missions will keep mass position fixed at the default. This mission allows the buoyancy volume to change to track the chlorophyll peak while drifting. -->

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

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

    <Guidance:AltitudeEnvelope>
        <Description>
            Another altitude envelope for the YoYo behavior. This envelope
            should fall within the limits of the standard safety envelopes in
            Insert/StandardEnvelopes.xml in order to avoid commanding high pitch
            angles for bottom-terminated YoYos.
        </Description>
        <Parallel/>
        <Setting><Guidance:AltitudeEnvelope.minAltitude/><Arg Name="YoYoMinAltitude"/></Setting>
        <Setting><Guidance:AltitudeEnvelope.upPitch/><Arg Name="YoYoUpPitch"/></Setting>
    </Guidance:AltitudeEnvelope>

    <Aggregate Id="MissionStart">

        <Sequence/>

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

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

        <Assign><Sequence/><Arg Name="MissionStartCommsCompleted"/><True/></Assign>

    </Aggregate>

    <Aggregate Id="ESPSampling">

        <When>
            <Called/>
        </When>

        <Aggregate Id="TriggerESP">

            <Sequence/>

            <Assign><Sequence/><Arg Name="DepthLogged"/><Universal:depth/></Assign>

            <Assign><Sequence/><Arg Name="TempLogged"/><Universal:sea_water_temperature/></Assign>

            <Assign><Sequence/><Arg Name="ChlLogged"/><Universal:mass_concentration_of_chlorophyll_in_sea_water/></Assign>

            <Syslog Severity="Important">Trigger ESP sampling. DepthLogged, TempLogged, ChlLogged = <Arg Name="DepthLogged"/><Units:meter/>, <Arg Name="TempLogged"/><Units:celsius/>, <Arg Name="ChlLogged"/><Units:microgram_per_liter/>.</Syslog>

            <ReadDatum>
                <Science:ESPComponent.sampling/>
            </ReadDatum>

            <Syslog Severity="Important">Wait for ESP sampling to complete.</Syslog>

            <ReadDatum>
                <Science:ESPComponent.sample_number/>
            </ReadDatum>

        </Aggregate>

    </Aggregate>

    <Aggregate Id="TransitToSamplingLocation">

        <Sequence/>

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

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

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

        <Assign><Sequence/><Arg Name="YoYoMinDepth"/><Arg Name="TransitYoYoMinDepth"/></Assign>

        <Assign><Sequence/><Arg Name="YoYoMaxDepth"/><Arg Name="TransitYoYoMaxDepth"/></Assign>

        <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:YoYo>
            <Parallel/>
            <Setting><Guidance:YoYo.downPitch/><Arg Name="YoYoDownPitch"/></Setting>
            <Setting><Guidance:YoYo.upPitch/><Arg Name="YoYoUpPitch"/></Setting>
        </Guidance:YoYo>

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

        <Aggregate Id="Lap">

            <Sequence Repeat="RepeatTransects"/>

            <Aggregate Id="Wpt1">

                <Sequence/>

                <Break>
                    <IsNaN><Arg Name="Lat1"/>
                    </IsNaN>
                    <Or><IsNaN><Arg Name="Lon1"/>
                    </IsNaN></Or>
                </Break>

                <Guidance:Waypoint>
                    <Sequence/>
                    <Setting><Guidance:Waypoint.latitude/><Arg Name="Lat1"/></Setting>
                    <Setting><Guidance:Waypoint.longitude/><Arg Name="Lon1"/></Setting>
                </Guidance:Waypoint>

            </Aggregate>

            <Aggregate Id="Wpt2">

                <Sequence/>

                <Break>
                    <IsNaN><Arg Name="Lat2"/>
                    </IsNaN>
                    <Or><IsNaN><Arg Name="Lon2"/>
                    </IsNaN></Or>
                </Break>

                <Guidance:Waypoint>
                    <Sequence/>
                    <Setting><Guidance:Waypoint.latitude/><Arg Name="Lat2"/></Setting>
                    <Setting><Guidance:Waypoint.longitude/><Arg Name="Lon2"/></Setting>
                </Guidance:Waypoint>

            </Aggregate>

            <Aggregate Id="Wpt3">

                <Sequence/>

                <Break>
                    <IsNaN><Arg Name="Lat3"/>
                    </IsNaN>
                    <Or><IsNaN><Arg Name="Lon3"/>
                    </IsNaN></Or>
                </Break>

                <Guidance:Waypoint>
                    <Sequence/>
                    <Setting><Guidance:Waypoint.latitude/><Arg Name="Lat3"/></Setting>
                    <Setting><Guidance:Waypoint.longitude/><Arg Name="Lon3"/></Setting>
                </Guidance:Waypoint>

            </Aggregate>

            <Aggregate Id="Wpt4">

                <Sequence/>

                <Break>
                    <IsNaN><Arg Name="Lat4"/>
                    </IsNaN>
                    <Or><IsNaN><Arg Name="Lon4"/>
                    </IsNaN></Or>
                </Break>

                <Guidance:Waypoint>
                    <Sequence/>
                    <Setting><Guidance:Waypoint.latitude/><Arg Name="Lat4"/></Setting>
                    <Setting><Guidance:Waypoint.longitude/><Arg Name="Lon4"/></Setting>
                </Guidance:Waypoint>

            </Aggregate>

        </Aggregate>

        <Aggregate Id="SetSamplingLocationtoWaypoint1">

            <Sequence/>

            <Break>
                <Not><Arg Name="CylinderYoYo"/></Not>
                <Or><IsNaN><Arg Name="Lat1"/>
                </IsNaN></Or>
                <Or><IsNaN><Arg Name="Lon1"/>
                </IsNaN></Or>
            </Break>

            <Assign><Sequence/><Arg Name="CylinderLat"/><Arg Name="Lat1"/></Assign>

            <Assign><Sequence/><Arg Name="CylinderLon"/><Arg Name="Lon1"/></Assign>

            <Syslog Severity="Important">Set cylinder center to Waypoint1 <Arg Name="CylinderLat"/><Units:degree/>,<Arg Name="CylinderLon"/><Units:degree/>.</Syslog>

        </Aggregate>

        <Aggregate Id="CylinderYoYoAroundSamplingLocation">

            <Sequence/>

            <Timeout Duration="CylinderTimeout"/>

            <Aggregate Id="CylinderIfNonNaNLatLon">

                <Sequence/>

                <Break>
                    <Not><Arg Name="CylinderYoYo"/></Not>
                    <Or><IsNaN><Arg Name="CylinderLat"/>
                    </IsNaN></Or>
                    <Or><IsNaN><Arg Name="CylinderLon"/>
                    </IsNaN></Or>
                </Break>

                <Syslog Severity="Important">Yoyo profile on a cylinder around sampling location <Arg Name="CylinderLat"/><Units:degree/>,<Arg Name="CylinderLon"/><Units:degree/>, at radius = <Arg Name="CylinderRadius"/><Units:meter/></Syslog>

                <Assign><Sequence/><Arg Name="YoYoMinDepth"/><Arg Name="CylinderYoYoMinDep"/></Assign>

                <Assign><Sequence/><Arg Name="YoYoMaxDepth"/><Arg Name="CylinderYoYoMaxDep"/></Assign>

                <Aggregate Id="CylinderWrapper">

                    <Sequence Repeat="MaxCirclesOnCylinder"/>

                    <Assign><Parallel/><Control:HorizontalControl.kwpHeading/><Arg Name="KwpHeading"/></Assign>

                    <Guidance:Circle>
                        <Sequence/>
                        <Setting><Guidance:Circle.latitude/><Arg Name="CylinderLat"/></Setting>
                        <Setting><Guidance:Circle.longitude/><Arg Name="CylinderLon"/></Setting>
                        <Setting><Guidance:Circle.radius/><Arg Name="CylinderRadius"/></Setting>
                        <Setting><Guidance:Circle.maxError/><Arg Name="CylinderCircleMaxError"/></Setting>
                        <Setting><Guidance:Circle.turnToPort/><Arg Name="CylinderCircleTurnToPort"/></Setting>
                    </Guidance:Circle>

                </Aggregate>

            </Aggregate>

        </Aggregate>

        <Aggregate Id="GoToCylinderCenter">

            <Sequence/>

            <Break>
                <IsNaN><Arg Name="CylinderLat"/>
                </IsNaN>
                <Or><IsNaN><Arg Name="CylinderLon"/>
                </IsNaN></Or>
            </Break>

            <Syslog Severity="Important">Go to cylinder center (i.e., sampling location): <Arg Name="CylinderLat"/><Units:degree/>,<Arg Name="CylinderLon"/><Units:degree/></Syslog>

            <Guidance:Waypoint Id="ToCylinderCenter">
                <Sequence/>
                <Setting><Guidance:Waypoint.latitude/><Arg Name="CylinderLat"/></Setting>
                <Setting><Guidance:Waypoint.longitude/><Arg Name="CylinderLon"/></Setting>
            </Guidance:Waypoint>

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

        </Aggregate>

        <Assign><Sequence/><Arg Name="FlagGoDown"/><True/></Assign>

        <Syslog Severity="Important">Go down at <Universal:depth/><Units:meter/>.</Syslog>

        <Assign><Sequence/><Arg Name="StartSmear"/><True/></Assign>

        <Assign><Sequence/><Arg Name="FromLastTimeFixToStartSmear"/><Elapsed><Universal:time_fix/></Elapsed></Assign>

<!--debug
	<Syslog Severity="Important">FromLastTimeFixToStartSmear = <Arg Name="FromLastTimeFixToStartSmear"/><Units:minute/></Syslog>
-->

    </Aggregate>

    <Aggregate Id="GoDownGoUp">

        <Sequence/>

        <Aggregate Id="GoDownInSpiralMode">

            <While>
                <Arg Name="FlagGoDown"/>
                <And><Arg Name="StartSmear"/></And>
            </While>

            <Guidance:Pitch>
                <Parallel/>
                <Setting><Guidance:Pitch.depthRate/><Arg Name="DownRate"/></Setting>
            </Guidance:Pitch>

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

            <Guidance:Point>
                <Parallel/>
                <Setting><Guidance:Point.rudderAngle/><Arg Name="RudderAngle"/></Setting>
            </Guidance:Point>

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

        </Aggregate>

        <Aggregate Id="GoUpInSpiralMode">

            <While>
                <Arg Name="FlagGoUp"/>
                <And><Arg Name="StartSmear"/></And>
            </While>

            <Guidance:Pitch>
                <Parallel/>
                <Setting><Guidance:Pitch.depthRate/><Arg Name="UpRate"/></Setting>
            </Guidance:Pitch>

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

            <Guidance:Point>
                <Parallel/>
                <Setting><Guidance:Point.rudderAngle/><Arg Name="RudderAngle"/></Setting>
            </Guidance:Point>

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

        </Aggregate>

        <Aggregate Id="ResetFlagGoDownSetFlagGoUp">

            <When>
                <Universal:depth/>
                <Ge><Arg Name="DeepBoundSmear"/></Ge>
                <And><Arg Name="StartSmear"/></And>
                <And><Not><Arg Name="FlagGoUp"/></Not></And>
            </When>

            <Assign><Sequence/><Arg Name="FlagGoDown"/><False/></Assign>

            <Assign><Sequence/><Arg Name="FlagGoUp"/><True/></Assign>

            <Assign><Sequence/><Arg Name="EnableFiring"/><True/></Assign>

            <Syslog Severity="Important">Go up at <Universal:depth/><Units:meter/>.</Syslog>

        </Aggregate>

        <Aggregate Id="ResetFlagGoUpSetFlagGoDown">

            <When>
                <Universal:depth/>
                <Le><Arg Name="ShallowBoundSmear"/></Le>
                <And><Arg Name="StartSmear"/></And>
                <And><Not><Arg Name="FlagGoDown"/></Not></And>
            </When>

            <Assign><Sequence/><Arg Name="FlagGoUp"/><False/></Assign>

            <Assign><Sequence/><Arg Name="FlagGoDown"/><True/></Assign>

            <Assign><Sequence/><Arg Name="EnableFiring"/><True/></Assign>

            <Syslog Severity="Important">Go down at <Universal:depth/><Units:meter/>.</Syslog>

        </Aggregate>

    </Aggregate>

    <Aggregate Id="TakeSamples">

        <Parallel/>

        <Aggregate Id="NoFiringForTooLong">

            <When>
                <Elapsed><Universal:time_fix/></Elapsed>
                <Gt><Arg Name="FromLastTimeFixToStartSmear"/><Add><Arg Name="MaxWaitNoFiring"/></Add></Gt>
                <And><Not><Arg Name="FlagSamplingOngoing"/></Not></And>
                <And><Arg Name="CntSamples"/><Eq><Units:count/><Value>1</Value></Eq></And>
                <And><Not><Arg Name="FlagFired"/></Not></And>
                <And><Not><Arg Name="StoppedForNoFiringForTooLong"/></Not></And>
                <Or><Elapsed><Universal:time_fix/></Elapsed><Gt><Arg Name="FromLastTimeFixToLastFiring"/><Add><Arg Name="MaxWaitNoFiring"/></Add></Gt><And><Not><Arg Name="FlagSamplingOngoing"/></Not></And><And><Arg Name="CntSamples"/><Ge><Units:count/><Value>2</Value></Ge></And><And><Not><Arg Name="StoppedForNoFiringForTooLong"/></Not></And></Or>
            </When>

            <Assign><Sequence/><Arg Name="FromLastTimeFixToNow"/><Elapsed><Universal:time_fix/></Elapsed></Assign>

            <Syslog Severity="Important">No firing for too long. Stopping mission. FromLastTimeFixToNow, FromLastTimeFixToLastFiring = <Arg Name="FromLastTimeFixToNow"/><Units:minute/>, <Arg Name="FromLastTimeFixToLastFiring"/><Units:minute/></Syslog>

            <Guidance:Execute>
                <Sequence/>
                <Setting><Guidance:Execute.command/><String>stop</String></Setting>
            </Guidance:Execute>

            <Assign><Sequence/><Arg Name="StoppedForNoFiringForTooLong"/><True/></Assign>

        </Aggregate>

        <Aggregate Id="Firing">

            <When>
                <Arg Name="MissionStartCommsCompleted"/>
                <And><Arg Name="StartSmear"/></And>
                <And><Arg Name="EnableFiring"/></And>
                <And><Arg Name="FlagGoDown"/></And>
                <And><Universal:depth/><Ge><Arg Name="ShallowBoundSmear"/></Ge></And>
                <And><Not><Arg Name="FlagSamplingOngoing"/></Not></And>
                <And><Arg Name="CntSamples"/><Le><Arg Name="NumESPSamplers"/></Le></And>
                <And><Not><Arg Name="SampleCompleted"/></Not></And>
                <Or><Arg Name="StartSmear"/><And><Arg Name="EnableFiring"/></And><And><Arg Name="FlagGoUp"/></And><And><Universal:depth/><Le><Arg Name="DeepBoundSmear"/></Le></And><And><Not><Arg Name="FlagSamplingOngoing"/></Not></And><And><Arg Name="CntSamples"/><Le><Arg Name="NumESPSamplers"/></Le></And><And><Not><Arg Name="SampleCompleted"/></Not></And></Or>
            </When>

            <Assign><Sequence/><Arg Name="FlagSamplingOngoing"/><True/></Assign>

            <Syslog Severity="Important">Taking ESP sample No.<Arg Name="CntSamples"/><Units:count/>.</Syslog>

            <Assign><Sequence/><Arg Name="FlagFired"/><True/></Assign>

            <Assign><Sequence/><Arg Name="FromLastTimeFixToLastFiring"/><Elapsed><Universal:time_fix/></Elapsed></Assign>

<!--debug
	    <Syslog Severity="Important">FromLastTimeFixToLastFiring = <Arg Name="FromLastTimeFixToLastFiring"/><Units:minute/></Syslog>
-->

            <Call Id="Sampling" RefId="ESPSampling"/>

            <Syslog Severity="Important"> ESP sample No.<Arg Name="CntSamples"/><Units:count/> completed.</Syslog>

            <Assign><Sequence/><Arg Name="FlagSamplingOngoing"/><False/></Assign>

            <Assign><Sequence/><Arg Name="SampleCompleted"/><True/></Assign>

            <Assign><Sequence/><Arg Name="EnableFiring"/><False/></Assign>

        </Aggregate>

        <Aggregate Id="IncrementSampleCounter">

            <When>
                <Arg Name="SampleCompleted"/>
            </When>

            <Assign><Sequence/><Arg Name="CntSamples"/><Arg Name="CntSamples"/><Add><Units:count/> <Value>1</Value></Add></Assign>

            <Assign><Sequence/><Arg Name="SampleCompleted"/><False/></Assign>

        </Aggregate>

    </Aggregate>

    <Aggregate Id="MissionCompleted">

        <When>
            <Arg Name="CntSamples"/>
            <Eq><Arg Name="NumESPSamplers"/> <Add><Units:count/> <Value>1</Value></Add></Eq>
        </When>

        <Syslog Severity="Important">Completed sampling. Stopping mission.</Syslog>

        <Guidance:Execute>
            <Sequence/>
            <Setting><Guidance:Execute.command/><String>stop</String></Setting>
        </Guidance:Execute>

        <Assign><Sequence/><Arg Name="CntSamples"/><Arg Name="CntSamples"/><Add><Units:count/> <Value>1</Value></Add></Assign>

    </Aggregate>

</Mission>
