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

<!-- some high priority behaviors -->

    <Guidance:AltitudeEnvelope RunIn="Parallel">
        <Set><Guidance:AltitudeEnvelope.minAltitude/><Units:meter/><Value>20</Value></Set>
    </Guidance:AltitudeEnvelope>

    <Guidance:OffshoreEnvelope RunIn="Parallel">
        <Set><Guidance:OffshoreEnvelope.minOffshore/><Units:kilometer/><Value>1</Value></Set>
        <Set><Guidance:OffshoreEnvelope.maxOffshore/><Units:kilometer/><Value>50</Value></Set>
    </Guidance:OffshoreEnvelope>

    <Guidance:DepthEnvelope RunIn="Parallel">
        <Set><Guidance:DepthEnvelope.minDepth/><Units:meter/><Value>5</Value></Set>
        <Set><Guidance:DepthEnvelope.maxDepth/><Units:meter/><Value>300</Value></Set>
    </Guidance:DepthEnvelope>

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

    <Stack Id="SurfaceOps" >

        <Guidance:GoToSurface RunIn="Parallel"/>

        <Guidance:GoToSurface RunIn="Sequence"/>

        <Guidance:ReadSensor RunIn="Sequence">
            <SetUri><Guidance:ReadSensor.uri/><Universal:latitude_fix/></SetUri>
        </Guidance:ReadSensor>

    </Stack>

    <Stack Id="SpiralStack">

        <Guidance:SetSpeed RunIn="Parallel">
            <Set><Guidance:SetSpeed.speed/><Units:meter_per_second/><Value>1</Value></Set>
        </Guidance:SetSpeed>

        <CustomCommand Name="SpiralCommand" RunIn="Sequence">
<!--
	        <Until>
	            <Universal:depth/><Gt/><Units:meter/><Value>200</Value>
	        </Until>
	        -->
            <Timeout Duration="P120M30S"></Timeout>
            <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>
                <Test InputValue="36.0 -122"/>
                <Script Language="Lua"><![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>
                <Test OutputValue="1.0 36.0 -122.0"/>
                <Script Language="Lua"><![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>
        </CustomCommand>

    </Stack>

</Stack>
