<!DOCTYPE flight_plan SYSTEM "flight_plan.dtd">

<flight_plan alt="1" ground_alt="0" lat0="51.990634" lon0="4.376789" max_dist_from_home="20" name="Rotorcraft Optitrack (Delft)" security_height="2">
  <header>
#include "autopilot.h"
#include "subsystems/electrical.h"
#include "modules/stereocam/nav_line_avoid/avoid_navigation.h"
</header>
  <waypoints>
    <waypoint name="HOME" x="0.0" y="0.0"/>
    <waypoint name="CLIMB" x="1.2" y="-0.6"/>
    <waypoint name="STDBY" x="-3.7" y="1.2"/>
    <waypoint name="TD" x="0.8" y="-1.7"/>
    <waypoint name="W0" x="5.6" y="-10.9"/>
    <waypoint name="W1" x="5.6" y="-10.9"/>
    <waypoint name="last_wp" x="1.2" y="0.4"/>
    <waypoint name="route_START" x="5.6" y="-10.9"/>
    <waypoint lat="51.9906213" lon="4.3768628" name="FA1"/>
    <waypoint lat="51.9905874" lon="4.3767766" name="FA2"/>
    <waypoint lat="51.9906409" lon="4.3767226" name="FA3"/>
    <waypoint lat="51.9906737" lon="4.3768074" name="FA4"/>
    <waypoint lat="51.9906125" lon="4.3768214" name="GOAL"/>
  </waypoints>
  <sectors>
    <sector color="red" name="Flight_Area">
      <corner name="FA4"/>
      <corner name="FA3"/>
      <corner name="FA2"/>
      <corner name="FA1"/>
    </sector>
  </sectors>
  <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 name="Start Engine" strip_button="Start" strip_icon="takeoff.png">
      <call_once fun="NavResurrect()"/>
      <set value="stateGetNedToBodyEulers_i()->psi" var="nav_heading"/>
      <attitude pitch="0" roll="0" throttle="0" until="stage_time > 1" vmode="throttle"/>
    </block>
    <block name="Takeoff">
      <exception cond="stateGetPositionEnu_f()->z > 0.5" deroute="Standby"/>
      <call_once fun="NavSetWaypointHere(WP_CLIMB)"/>
      <stay climb="nav_climb_vspeed" vmode="climb" wp="CLIMB"/>
    </block>
    <block name="Standby" strip_button="Standby" strip_icon="home.png">
      <exception cond="GpsIsLost()" deroute="attitude_land"/>
      <exception cond="electrical.bat_low" deroute="land here"/>
      <call_once fun="NavCopyWaypoint(WP_last_wp,WP_W0)"/>
      <stay wp="STDBY"/>
    </block>
    <block name="heading_to_GOAL" strip_button="GOAD" strip_icon="line.png">
      <exception cond="GpsIsLost()" deroute="attitude_land"/>
      <exception cond="electrical.bat_low" deroute="land here"/>
      <call_once fun="nav_set_heading_towards_waypoint(WP_GOAL)"/>
      <call_once fun="NavSetWaypointHere(WP_route_START)"/>
      <stay until="stage_time > 3" wp="route_START"/>
    </block>
    <block name="route_to_GOAL">
      <exception cond="GpsIsLost()" deroute="attitude_land"/>
      <exception cond="electrical.bat_low" deroute="land here"/>
      <exception cond="obstacle_detected==1" deroute="stay_W0"/>
      <call_once fun="nav_set_heading_towards_waypoint(WP_GOAL)"/>
      <go from="STDBY" hmode="route" wp="GOAL"/>
      <call_once fun="NavCopyWaypoint(WP_last_wp,WP_GOAL)"/>
      <deroute block="land here"/>
    </block>
    <block name="stay_W0" pre_call="increase_nav_heading(&nav_heading, 30)">
      <exception cond="obstacle_detected==0" deroute="route_to_W1"/>
      <exception cond="GpsIsLost()" deroute="attitude_land"/>
      <exception cond="electrical.bat_low" deroute="land here"/>
      <call_once fun="NavSetWaypointHere(WP_W0)"/>
      <call_once fun="NavCopyWaypoint(WP_last_wp,WP_W0)"/>
      <stay wp="W0"/>
    </block>
    <block name="route_to_W1">
      <exception cond="GpsIsLost()" deroute="attitude_land"/>
      <exception cond="electrical.bat_low" deroute="land here"/>
      <exception cond="obstacle_detected==1" deroute="stay_W0"/>
      <go from="W0" hmode="route" wp="W1"/>
      <call_once fun="NavCopyWaypoint(WP_last_wp,WP_W1)"/>
      <deroute block="heading_to_GOAL"/>
    </block>
    <block name="Mission" strip_button="Mission">
      <exception cond="GpsIsLost()" deroute="attitude_land"/>
      <exception cond="electrical.bat_low" deroute="land here"/>
      <call fun="mission_run()"/>
      <deroute block="Standby"/>
    </block>
    <block name="land here" strip_button="Land Here" strip_icon="land-right.png">
      <call_once fun="NavSetWaypointHere(WP_TD)"/>
    </block>
    <block name="land">
      <exception cond="GpsIsLost()" deroute="attitude_land"/>
      <go wp="TD"/>
    </block>
    <block name="flare">
      <exception cond="NavDetectGround()" deroute="landed"/>
      <exception cond="!nav_is_in_flight()" deroute="landed"/>
      <call_once fun="NavStartDetectGround()"/>
      <stay climb="nav_descend_vspeed" until="stage_time > 4" vmode="climb" wp="TD"/>
    </block>
    <block name="landed">
      <call_once fun="NavKillThrottle()"/>
      <attitude pitch="0" roll="0" throttle="0" until="FALSE" vmode="throttle"/>
    </block>
    <block name="attitude_land">
      <attitude climb="-1.0" pitch="0" roll="0" until="stage_time >4" vmode="climb"/>
      <deroute block="landed"/>
    </block>
  </blocks>
</flight_plan>
