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

<airframe name="Yapa v1">
  <description>
YAPA + XSens + XBee
  </description>

  <!-- ************************* FIRMWARE ************************* -->

  <firmware name="fixedwing">
    <target name="ap" board="yapa_2.0"/>
    <target name="sim" board="pc"/>
    <define name="STRONG_WIND"/>
    <define name="WIND_INFO"/>
    <define name="WIND_INFO_RET"/>
    <define name="LOITER_TRIM"/>
    <define name="TUNE_AGRESSIVE_CLIMB"/>
    <define name="AGR_CLIMB"/>
    <module name="radio_control" type="ppm"/>
    <!-- Communication -->
    <module name="telemetry" type="xbee_api">
      <configure name="MODEM_BAUD" value="B9600"/>
    </module>
    <!-- Actuators -->
    <module name="control"/>
    <!-- Sensors -->
    <module name="navigation"/>
    <module name="ins" type="xsens">
      <configure name="XSENS_PORT" value="uart0"/>
      <configure name="XSENS_BAUD" value="B230400"/>
    </module>
  </firmware>

  <!-- ************************* MODULES ************************* -->

  <modules>
    <module name="light">
      <define name="LIGHT_LED_STROBE" value="1"/>
      <!--      <define name="LIGHT_LED_NAV" value="2"/> -->
      <define name="STROBE_LIGHT_MODE_DEFAULT" value="6"/>
      <define name="NAV_LIGHT_MODE_DEFAULT" value="4"/>
    </module>
    <!--    <module name="digital_cam_i2c"/>  -->
    <!--    <module name="ins_ppzuavimu"/>  -->
    <module name="digital_cam">
      <define name="DC_SHUTTER_GPIO" value="GPIOB,GPIO16"/>
    </module>
    <module name="nav" type="line"/>
  </modules>

  <!-- ************************* ACTUATORS ************************* -->

  <servos>
    <servo name="THROTTLE" no="0" min="1000" neutral="1000" max="2000"/>
    <servo name="AILERON_LEFT" no="2" min="900" neutral="1500" max="2100"/>
    <servo name="AILERON_RIGHT" no="6" min="900" neutral="1500" max="2100"/>
    <servo name="ELEVATOR" no="7" min="1900" neutral="1500" max="1100"/>
    <servo name="RUDDER" no="3" min="1175" neutral="1575" max="1975"/>
  </servos>

  <commands>
    <axis name="THROTTLE" failsafe_value="0"/>
    <axis name="ROLL" failsafe_value="0"/>
    <axis name="PITCH" failsafe_value="0"/>
    <axis name="YAW" failsafe_value="0"/>
    <axis name="BRAKE" failsafe_value="9600"/>
    <!-- both elerons up as butterfly brake -->
  </commands>

  <rc_commands>
    <set command="THROTTLE" value="@THROTTLE"/>
    <set command="ROLL" value="@ROLL"/>
    <set command="YAW" value="@YAW"/>
    <set command="PITCH" value="@PITCH"/>
    <set command="BRAKE" value="@FLAPS"/>
  </rc_commands>

  <section name="AUTO1" prefix="AUTO1_">
    <define name="MAX_ROLL" value="RadOfDeg(75)"/>
    <define name="MAX_PITCH" value="RadOfDeg(45)"/>
  </section>

  <section name="SERVO_MIXER_GAINS">
    <define name="AILERON_NEUTRAL" value="0.3f"/>
    <define name="AILERON_RATE_UP" value="1.0f"/>
    <define name="AILERON_RATE_DOWN" value="0.5f"/>
    <define name="AILERON_RATE_UP_BRAKE" value="1.0f"/>
    <define name="AILERON_RATE_DOWN_BRAKE" value="1.0f"/>
    <define name="PITCH_GAIN" value="0.9f"/>
    <define name="BRAKE_AILEVON" value="-0.7f"/>
    <define name="BRAKE_PITCH" value="0.1f"/>
    <define name="MAX_BRAKE_RATE" value="80"/>
    <define name="RUDDER_OF_AILERON" value="0.3"/>
  </section>

  <command_laws>
    <!-- Brake Rate Limiter -->
    <let var="brake_value_nofilt" value="Chop(-@BRAKE, 0, MAX_PPRZ)"/>
    <ratelimit var="brake_value" value="$brake_value_nofilt" rate_min="-MAX_BRAKE_RATE" rate_max="MAX_BRAKE_RATE"/>
    <!-- Differential Aileron Depending on Brake Value -->
    <let var="aileron_up_rate" value="(AILERON_RATE_UP   * (MAX_PPRZ - $brake_value)) + (AILERON_RATE_UP_BRAKE   * $brake_value)"/>
    <let var="aileron_down_rate" value="(AILERON_RATE_DOWN * (MAX_PPRZ - $brake_value)) + (AILERON_RATE_DOWN_BRAKE * $brake_value)"/>
    <let var="aileron_up" value="((-@ROLL)  *  (((float)$aileron_up_rate) / ((float)MAX_PPRZ)))"/>
    <let var="aileron_down" value="((-@ROLL)  * (((float)$aileron_down_rate) / ((float)MAX_PPRZ)))"/>
    <let var="leftturn" value="(@ROLL >= 0? 0 : 1)"/>
    <let var="rightturn" value="(1 - $leftturn)"/>
    <set servo="AILERON_LEFT" value="($aileron_up * $leftturn) + ($aileron_down * $rightturn) - $brake_value*(BRAKE_AILEVON) - (MAX_PPRZ * AILERON_NEUTRAL)"/>
    <set servo="AILERON_RIGHT" value="($aileron_up * $rightturn) + ($aileron_down * $leftturn) + $brake_value*(BRAKE_AILEVON) + (MAX_PPRZ *AILERON_NEUTRAL)"/>
    <set servo="RUDDER" value="-(@YAW + @ROLL * RUDDER_OF_AILERON)"/>
    <set servo="THROTTLE" value="@THROTTLE"/>
    <!-- Pitch with Brake-Trim Function -->
    <set servo="ELEVATOR" value="@PITCH * PITCH_GAIN - BRAKE_PITCH * $brake_value"/>
  </command_laws>

  <section name="FAILSAFE" prefix="FAILSAFE_">
    <define name="DEFAULT_THROTTLE" value="0.35" unit="%"/>
    <define name="DEFAULT_ROLL" value="0.17" unit="rad"/>
    <define name="DEFAULT_PITCH" value="0.08" unit="rad"/>
    <define name="HOME_RADIUS" value="DEFAULT_CIRCLE_RADIUS" unit="m"/>
    <define name="KILL_MODE_DISTANCE" value="(MAX_DIST_FROM_HOME*1.5)"/>
    <define name="DELAY_WITHOUT_GPS" value="3" unit="s"/>
  </section>

  <!-- ************************* SENSORS ************************* -->

  <section name="AHRS" prefix="AHRS_">
    <define name="H_X" value="0.51562740288882"/>
    <define name="H_Y" value="-0.05707735220832"/>
    <define name="H_Z" value="0.85490967783446"/>
  </section>

  <section name="INS" prefix="INS_">
    <define name="ROLL_NEUTRAL_DEFAULT" value="0.0" unit="rad"/>
    <define name="PITCH_NEUTRAL_DEFAULT" value="-0.0885749980807" unit="rad"/>
  </section>

  <section name="XSENS">
    <define name="GPS_IMU_LEVER_ARM_X" value="-0.285f"/>
    <define name="GPS_IMU_LEVER_ARM_Y" value="0.0f"/>
    <define name="GPS_IMU_LEVER_ARM_Z" value="0.0f"/>
  </section>

  <!-- ************************* GAINS ************************* -->

  <section name="HORIZONTAL CONTROL" prefix="H_CTL_">
    <define name="COURSE_PGAIN" value="0.90"/>
    <define name="COURSE_DGAIN" value="0.35"/>
    <define name="COURSE_PRE_BANK_CORRECTION" value="1.0039999485"/>
    <define name="ROLL_MAX_SETPOINT" value="0.851999998093" unit="radians"/>
    <define name="PITCH_MAX_SETPOINT" value="0.5" unit="radians"/>
    <define name="PITCH_MIN_SETPOINT" value="-0.6" unit="radians"/>
    <define name="PITCH_PGAIN" value="16917.2929688"/>
    <define name="PITCH_DGAIN" value="7.73400020599"/>
    <define name="ELEVATOR_OF_ROLL" value="3007.81298828"/>
    <define name="ROLL_SLEW" value="2."/>
    <define name="ROLL_ATTITUDE_GAIN" value="11718.75"/>
    <define name="ROLL_RATE_GAIN" value="820.312011719"/>
  </section>

  <section name="VERTICAL CONTROL" prefix="V_CTL_">
    <define name="POWER_CTL_BAT_NOMINAL" value="11.1" unit="volt"/>
    <!-- outer loop proportional gain -->
    <define name="ALTITUDE_PGAIN" value="0.108000002801"/>
    <!-- outer loop saturation -->
    <define name="ALTITUDE_MAX_CLIMB" value="2."/>
    <!-- auto throttle inner loop -->
    <define name="AUTO_THROTTLE_NOMINAL_CRUISE_THROTTLE" value="0.6"/>
    <define name="AUTO_THROTTLE_MIN_CRUISE_THROTTLE" value="0.25"/>
    <define name="AUTO_THROTTLE_MAX_CRUISE_THROTTLE" value="0.9"/>
    <define name="AUTO_THROTTLE_LOITER_TRIM" value="1500"/>
    <define name="AUTO_THROTTLE_DASH_TRIM" value="-4000"/>
    <define name="AUTO_THROTTLE_CLIMB_THROTTLE_INCREMENT" value="0.158000007272" unit="%/(m/s)"/>
    <define name="AUTO_THROTTLE_PGAIN" value="0."/>
    <define name="AUTO_THROTTLE_IGAIN" value="0."/>
    <define name="AUTO_THROTTLE_PITCH_OF_VZ_PGAIN" value="0.361000001431"/>
    <define name="THROTTLE_SLEW_LIMITER" value="2" unit="s"/>
  </section>

  <section name="AGGRESSIVE" prefix="AGR_">
    <define name="BLEND_START" value="30"/>
    <define name="BLEND_END" value="15"/>
    <define name="CLIMB_THROTTLE" value="1."/>
    <define name="CLIMB_PITCH" value="0.40000000596"/>
    <define name="DESCENT_THROTTLE" value="0."/>
    <define name="DESCENT_PITCH" value="-0.10000000149"/>
    <define name="CLIMB_NAV_RATIO" value="0.800000011921"/>
    <define name="DESCENT_NAV_RATIO" value="0.834999978542"/>
  </section>

  <!-- ************************* MISC ************************* -->

  <section name="BAT">
    <define name="LOW_BAT_LEVEL" value="10.5" unit="V"/>
    <define name="CRITIC_BAT_LEVEL" value="10" unit="V"/>
    <define name="CATASTROPHIC_BAT_LEVEL" value="9.1" unit="V"/>
  </section>

  <section name="MISC">
    <define name="NOMINAL_AIRSPEED" value="8." unit="m/s"/>
    <define name="CARROT" value="5." unit="s"/>
    <define name="XBEE_INIT" value="ATPL4\rATRN5\rATTT80\r" type="string"/>
    <!--    <define name="NO_XBEE_API_INIT" value="TRUE"/> -->
    <define name="DEFAULT_CIRCLE_RADIUS" value="80."/>
    <define name="COMMAND_ROLL_TRIM" value="180"/>
    <define name="COMMAND_PITCH_TRIM" value="-194."/>
  </section>

  <section name="DIGITAL_CAMERA" prefix="DC_">
    <define name="AUTOSHOOT_PERIOD" value="1.5" unit="sec"/>
    <define name="AUTOSHOOT_DISTANCE_INTERVAL" value="50" unit="meter"/>
  </section>

  </airframe>
