<?xml version="1.0" encoding="UTF-8"?>
<Mission xmlns="Tethys"
       xmlns:Control="Tethys/Control"
       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/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="TestMission">

    <DefineBehavior Name="SpiralCommand" Language="Lua">

        <Initialize>
            <Input Name="latitude"><Universal:latitude/><Units:degree/><DefaultValue>NaN</DefaultValue></Input>
            <Input Name="longitude"><Universal:longitude/><Units:degree/><DefaultValue>NaN</DefaultValue></Input>
            <Output><Control:VerticalControl.verticalMode/><Units:enum/></Output>
            <Output><Control:VerticalControl.pitchCmd/><Units:degree/></Output>
            <Script><![CDATA[
                angle = 26.0;               -- angle to dive and climb at
                radius = 0.0003;                -- radius of the spiral
                stayAboveSeaFloor = 25.0;   -- how far to stay above sea floor
                minDepth = 5;               -- min depth
                
                i = 0;
                angle = angle * -1.0;
                origLat = latitude;
                origLon = longitude;
                goalLat = (math.sin(i) * radius) + origLat;
                goalLon = (math.cos(i) * radius) + origLon;
                
                -- tell it to dive
                return 4.0, angle;                   
            ]]></Script>
        </Initialize>

        <Run>
            <Input Name="latitude"><Universal:latitude/><Units:degree/><DefaultValue>NaN</DefaultValue></Input>
            <Input Name="longitude"><Universal:longitude/><Units:degree/><DefaultValue>NaN</DefaultValue></Input>
            <Input Name="depth"><Universal:depth/><Units:meter/><DefaultValue>NaN</DefaultValue></Input>
            <Input Name="heightAboveFloor"><Universal:height_above_sea_floor/><Units:meter/><DefaultValue>NaN</DefaultValue></Input>
            <Output><Control:HorizontalControl.horizontalMode/><Units:enum/></Output>
            <Output><Control:HorizontalControl.latitudeCmd/><Units:degree/></Output>
            <Output><Control:HorizontalControl.longitudeCmd/><Units:degree/></Output>
            <Output><Control:VerticalControl.verticalMode/><Units:enum/></Output>
            <Output><Control:VerticalControl.pitchCmd/><Units:degree/></Output>
            <Script><![CDATA[
                diffLat = math.abs(goalLat - latitude);
                diffLon = math.abs(goalLon - longitude);
                if diffLat <= 0.0001 and diffLon <= 0.0001 then
                    i = (i + 0.2) % (2 * math.pi);
                    goalLat = (math.sin(i) * radius) + origLat;
                    goalLon = (math.cos(i) * radius) + origLon;
                end
                
                if angle <= 0 and heightAboveFloor <= stayAboveSeaFloor then
                    angle = angle * -1;
                end
                
                if angle >= 0 and depth <= minDepth then
                    angle = angle * -1;                         
                end
                                 
                -- tell it its next waypoint and tell it to dive        
                return 1.0, goalLat, goalLon, 4.0, angle;
             ]]></Script>
        </Run>

    </DefineBehavior>

<!-- some high priority behaviors -->

    <Guidance:AltitudeEnvelope>
        <Parallel/>
        <Setting><Guidance:AltitudeEnvelope.minAltitude/><Units:meter/><Value>20</Value></Setting>
    </Guidance:AltitudeEnvelope>

    <Guidance:OffshoreEnvelope>
        <Parallel/>
        <Setting><Guidance:OffshoreEnvelope.minOffshore/><Units:kilometer/><Value>1</Value></Setting>
        <Setting><Guidance:OffshoreEnvelope.maxOffshore/><Units:kilometer/><Value>50</Value></Setting>
    </Guidance:OffshoreEnvelope>

    <Guidance:DepthEnvelope>
        <Parallel/>
        <Setting><Guidance:DepthEnvelope.minDepth/><Units:meter/><Value>5</Value></Setting>
        <Setting><Guidance:DepthEnvelope.maxDepth/><Units:meter/><Value>300</Value></Setting>
    </Guidance:DepthEnvelope>

<!-- Initial GPS (and Iridium) Update -->

    <Aggregate Id="SurfaceOps">

        <Sequence/>

        <Guidance:GoToSurface>
            <Parallel/>
        </Guidance:GoToSurface>

        <Guidance:GoToSurface>
            <Sequence/>
        </Guidance:GoToSurface>

        <ReadDatum>
            <Universal:latitude_fix/>
        </ReadDatum>

    </Aggregate>

    <Aggregate Id="SpiralAggregate">

        <Sequence/>

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

        <CustomBehavior Name="SpiralCommand">
            <Sequence/>
<!--
	        <Break>
	            <Universal:depth/><Gt><Units:meter/><Value>200</Value></Gt>
	        </Break>
	        -->
            <Timeout Duration="P120M30S"></Timeout>
        </CustomBehavior>

    </Aggregate>

</Mission>
