<!DOCTYPE flight_plan SYSTEM "../flight_plan.dtd">

<flight_plan alt="1.5" ground_alt="0" lat0="51.990634" lon0="4.376789" max_dist_from_home="8" name="Bebop avoid orange TU Delft Cyberzoo" security_height="0.4">
  <header>
#include "autopilot.h"
#include "subsystems/ahrs.h"
#include "subsystems/electrical.h"
#include "subsystems/datalink/datalink.h"
</header>
  <waypoints>
    <waypoint height="0" name="HOME" x="0.0" y="0.0"/>
    <waypoint height="5" name="CLIMB" x="1.2" y="-0.6"/>
    <waypoint name="GOAL" x="2.0" y="2.0"/>
    <waypoint height="1.342321" name="STDBY" x="-0.7" y="-0.8"/>
    <waypoint name="TD" x="0.8" y="-1.7"/>
    <waypoint lat="51.9906213" lon="4.3768628" name="_CZ1"/>
    <waypoint lat="51.9905874" lon="4.3767766" name="_CZ2"/>
    <waypoint lat="51.9906409" lon="4.3767226" name="_CZ3"/>
    <waypoint lat="51.990667" lon="4.376806" name="_CZ4"/>
    <waypoint lat="51.990624" lon="4.376845" name="_OZ1"/>
    <waypoint lat="51.990601" lon="4.376782" name="_OZ2"/>
    <waypoint alt="11.5" lat="51.990638" lon="4.376748" name="_OZ3"/>
    <waypoint lat="51.990657" lon="4.376804" name="_OZ4"/>
  </waypoints>
  <sectors>
    <sector color="red" name="CyberZoo">
      <corner name="_CZ1"/>
      <corner name="_CZ2"/>
      <corner name="_CZ3"/>
      <corner name="_CZ4"/>
    </sector>
    <sector color="#FF9922" name="ObstacleZone">
      <corner name="_OZ1"/>
      <corner name="_OZ2"/>
      <corner name="_OZ3"/>
      <corner name="_OZ4"/>
    </sector>
  </sectors>
  <exceptions>
    <exception cond="!InsideCyberZoo(GetPosX(), GetPosY())" deroute="Standby"/>
    <exception cond="datalink_time > 2" deroute="Land here"/>
    <exception cond="electrical.bat_low && !(nav_block == IndexOfBlock('Land')) && !(nav_block == IndexOfBlock('Flare')) && !(nav_block == IndexOfBlock('Landed'))" deroute="Land"/>
    <exception cond="electrical.bat_critical && !(nav_block == IndexOfBlock('Land')) && !(nav_block == IndexOfBlock('Flare')) && !(nav_block == IndexOfBlock('Landed'))" deroute="Land here"/>
  </exceptions>
  <blocks>
    <block name="Wait GPS">
      <call_once fun="NavKillThrottle()"/>
      <while cond="!GpsFixValid()"/>
    </block>
    <block name="Geo init">
      <while cond="LessThan(NavBlockTime(), 2)"/>
      <call_once fun="NavSetAltitudeReferenceHere()"/>
    </block>
    <block name="Holding point">
      <call_once fun="NavKillThrottle()"/>
      <attitude pitch="0" roll="0" throttle="0" until="FALSE" vmode="throttle"/>
    </block>
    <block key="r" name="Start Engine">
      <call_once fun="NavResurrect()"/>
      <attitude pitch="0" roll="0" throttle="0" until="FALSE" vmode="throttle"/>
    </block>
    <block key="t" name="Takeoff" strip_button="Takeoff" strip_icon="takeoff.png">
      <exception cond="stateGetPositionEnu_f()->z > 1.25" deroute="Standby"/>
      <call_once fun="NavSetWaypointHere(WP_CLIMB)"/>
      <stay climb="nav_climb_vspeed" vmode="climb" wp="CLIMB"/>
    </block>
    <block key="s" name="Standby" strip_button="Standby" strip_icon="home.png">
      <call_once fun="NavSetWaypointHere(WP_STDBY)"/>
      <stay wp="STDBY"/>
    </block>
    <block key="g" name="START" strip_button="Go" strip_icon="lookfore.png">
      <call_once fun="NavSetWaypointHere(WP_GOAL)"/>
    </block>
    <block name="StayGoal">
      <go wp="GOAL"/>
      <exception cond="block_time > 1" deroute="MoveGoalForward"/>
      <exception cond="!safeToGoForwards" deroute="ChangeHeading"/>
      <exception cond="!InsideObstacleZone(GetPosX(),GetPosY())" deroute="PrepareObstacleRun"/>
    </block>
    <block name="MoveGoalForward">
      <call_once fun="moveWaypointForwards(WP_GOAL,3.0)"/>
      <deroute block="StayGoal"/>
      <exception cond="!safeToGoForwards" deroute="ChangeHeading"/>
      <exception cond="!InsideObstacleZone(WaypointX(WP_GOAL),WaypointY(WP_GOAL))" deroute="PrepareObstacleRun"/>
    </block>
    <block name="ChangeHeading">
      <call_once fun="NavSetWaypointHere(WP_STDBY)"/>
      <while cond="1">
        <call_once fun="increase_nav_heading(&nav_heading, incrementForAvoidance)"/>
        <call_once fun="moveWaypointForwards(WP_GOAL,3.0)"/>
        <go wp="STDBY"/>
      </while>
      <exception cond="safeToGoForwards" deroute="StayGoal"/>
    </block>
    <block name="PrepareObstacleRun">
      <call_once fun="NavSetWaypointHere(WP_STDBY)"/>
      <call_once fun="nav_set_heading_towards_waypoint(WP_HOME)"/>
      <stay wp="STDBY"/>
      <exception cond="block_time > 1" deroute="GetOutOfObstacleZone"/>
    </block>
    <block name="GetOutOfObstacleZone">
      <call_once fun="moveWaypointForwards(WP_GOAL,0.5)"/>
      <call_once fun="chooseRandomIncrementAvoidance()"/>
      <stay wp="GOAL"/>
      <exception cond="block_time > 1" deroute="START"/>
    </block>
    <block key="l" name="Land here" strip_button="Land Here" strip_icon="land-right.png">
      <call_once fun="NavSetWaypointHere(WP_TD)"/>
    </block>
    <block name="Land">
      <go wp="TD"/>
    </block>
    <block name="Flare">
      <exception cond="NavDetectGround()" deroute="Holding point"/>
      <exception cond="!nav_is_in_flight()" deroute="Landed"/>
      <call_once fun="NavStartDetectGround()"/>
      <stay climb="nav_descend_vspeed" vmode="climb" wp="TD"/>
    </block>
    <block name="Landed">
      <call_once fun="NavKillThrottle()"/>
      <attitude pitch="0" roll="0" throttle="0" until="FALSE" vmode="throttle"/>
    </block>
  </blocks>
</flight_plan>
